-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIP.cpp
More file actions
126 lines (122 loc) · 2.59 KB
/
IP.cpp
File metadata and controls
126 lines (122 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// IP.cpp
// WebHelper
//
// Created by Исаак Калинкин on 27.02.16.
// Copyright © 2016 EduAppHelpeer. All rights reserved.
//
#include "IP.hpp"
BitComponent::BitComponent()
{
this->bit = new bool[32];
for(int i = 0; i < 32; i++)
this->bit[i] = false;
}
BitComponent::BitComponent(bool* new_bit)
{
this->bit = new bool[32];
for(int i = 0; i < 32; i++)
this->bit[i] = new_bit[i];
}
BitComponent BitComponent::operator*=(BitComponent& rhs)
{
*this = *this * rhs;
return *this;
}
BitComponent operator*(BitComponent& lhs, BitComponent& rhs)
{
BitComponent result;
for(int i = 0; i < 32; i++)
{
result[i] = lhs[i] && rhs[i];
}
return result;
}
bool &BitComponent::operator[](int index)
{
return this->bit[index];
}
/*bool* BitComponent::GetBit()
{
return bit;
}*/
//==-Mask-====================================
Mask::Mask()
{
SetReservedBits(32);
}
Mask::Mask(int reservedBits)
{
SetReservedBits(reservedBits);
}
void Mask::SetReservedBits(int new_reservedBits)
{
for(int i = 0; i < new_reservedBits; i++)
this->bit[i] = true;
this->reservedBits = new_reservedBits;
}
IP::IP(string input)
{
SetBitIP(Parse(input));
}
void IP::SetBitIP(BitComponent new_bit_ip)
{
this->bit = new bool[32];
for(int i = 0; i < 32; i++)
this->bit[i] = new_bit_ip[i];
}
void IP::Manager(string h_input)
{
string ip, r_bits;
for(int i = 0, a = 0; i < h_input.length(); i++)
{
if(h_input[i] == '/')
{
a = 1;
continue;
}
if(!a)
ip += h_input[i];
else
r_bits += h_input[i];
}
SetBitIP(Parse(ip));
mask = *new Mask(r_bits);
}
BitComponent Parse(string input)
{
BitComponent result;
int buffer = 0;
for(int i = 0, c = 0; i < 4; i++, c++)
{
while((input[c] != '.') && (c < input.length())){
buffer = 10*buffer + (int)input[c] - '0';
c++;
}
bool *quad_result = Convert10to2(buffer);
for(int a = 0; a < 8; a++)
result[8*i + a] = quad_result[a];
buffer = 0;
}
return result;
}
//==-
bool *Convert10to2(int input)
{
bool* result = new bool[8];
string Bin = "";
while(input > 0)
{
if(input%2)
Bin+='1';
else
Bin+='0';
input/=2;
}
std::reverse(Bin.begin(), Bin.end());
for(int i = 0; i < 8 - Bin.length(); i++)
result[i] = false;
for(int i = 0; i < Bin.length(); i++)
result[i + 8 - Bin.length()] = Bin[i] - '0';
return result;
}