-
Notifications
You must be signed in to change notification settings - Fork 0
/
addressing.cpp
217 lines (170 loc) · 4.82 KB
/
addressing.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <stdlib.h>
#include <iostream>
#include <vector>
#include "addressing.h"
#include <time.h>
#include <cstdint>
#include <sstream>
using namespace std;
vector<uint8_t> create_mac(uint8_t b1,uint8_t b2,uint8_t b3,uint8_t b4,uint8_t b5,uint8_t b6) {
vector<uint8_t> result; // we'll add 6 bytes to this mac
result.reserve(6);
result.push_back(b1);
result.push_back(b2);
result.push_back(b3);
result.push_back(b4);
result.push_back(b5);
result.push_back(b6);
return result;
}
vector<uint8_t> create_random_mac() {
// as one might expect, this is a function
// that takes no argument and generates
// a random MAC address
int character_number;
vector<uint8_t> result; // we'll add 6 bytes to this mac
result.reserve(6);
for(int i = 0; i < 6; i++) {
result.push_back((uint8_t)(rand() % 256));
}
return result;
}
vector<uint8_t> create_uniform_mac(uint8_t mac_byte) {
vector<uint8_t> result; // we'll add 6 bytes to this mac
result.reserve(6);
for (int i = 0; i < 6; i++) {
result.push_back(mac_byte);
}
return result;
}
vector<uint8_t> create_broadcast_mac() {
vector<uint8_t> result; // we'll add 6 bytes to this mac
result.reserve(6);
for (int i = 0; i < 6; i++) {
result.push_back(0xFF);
}
return result;
}
uint32_t create_ip(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) {
// again, as one might expect, this takes no argument
// and returns a random ip
uint32_t result = 0x00000000; // this will only be four bytes
result |= b1;
result = result << 8;
result |= b2;
result = result << 8;
result |= b3;
result = result << 8;
result |= b4;
return result;
}
uint32_t create_random_ip() {
// again, as one might expect, this takes no argument
// and returns a random ip
uint32_t result = 0x00000000; // this will only be four bytes
for(int i = 0; i < 4; i++) {
result |= ((uint32_t) (rand() % 256)) << (i*8);
}
return result;
}
uint32_t create_broadcast_ip() {
return 0xFFFFFFFF;
}
bool compare_macs(vector<uint8_t> mac1, vector<uint8_t> mac2) {
bool result = true;
for (int i = 0; i < 6; i++){
if (mac1[i] != mac2[i]){
return false;
}
}
return result;
}
bool compare_ips(uint32_t ip1, uint32_t ip2) {
return ip1 == ip2;
}
bool is_broadcast(vector<uint8_t> address) {
bool result = true;
for(int i = 0; i < 6; i++) {
if (address[i] != 0xFF) {
return false;
}
}
return result;
}
bool is_broadcast(uint32_t address) {
return address == 0xFFFFFFFF;
}
bool in_subnet(uint32_t ip_1,uint32_t ip_2, uint32_t test_netmask) {
// roll through each of the bits of the netmask
for (int i = 0; i < 32; i++){
// check to see if this bit is 1 in the netmask, meaning
// that an ip in the same subnet should be equal to the subnet
// at this bit
if ((test_netmask >> i) & 0x01){
if (!(((ip_1 >> i) & 0x01) == ((ip_2 >> i) & 0x01))) {
// if this is not true, then the address isn't in the same subnet
return false;
}
}
}
return true;
}
string mac_to_string(vector<uint8_t> mac_addr){
ostringstream convert;
for (int i = 0; i < 6; i++) {
if (mac_addr[i] < 16) {
convert << "0";
}
convert << uppercase << hex << (int)mac_addr[i];
if (i < 5) {
convert << ":";
}
}
return convert.str();
}
string mac_to_string(vector<uint8_t>* mac_vector, int first_index){
ostringstream convert;
uint8_t current_byte;
for (int i = first_index; i < first_index+ 6; i++) {
current_byte = mac_vector->at(i);
if (current_byte < 16) {
convert << "0";
}
convert << uppercase << hex << (int)current_byte;
if (i < first_index+ 5) {
convert << ":";
}
}
return convert.str();
}
string ip_to_string(uint32_t ip_addr){
ostringstream convert;
for (int i = 3; i >= 0; i--) {
convert << (int)((ip_addr >> (i*8)) & 0x000000FF);
if (i > 0) {
convert << ".";
}
}
return convert.str();
}
string ip_to_string(vector<uint8_t> * ip_vector, int first_index) {
ostringstream convert;
for (int i = first_index; i < first_index + 4; i++) {
convert << (int)(ip_vector->at(i));
if (i < first_index+3) {
convert << ".";
}
}
return convert.str();
}
// int main() {
// vector<uint8_t> test;
// test.push_back(0x11);
// test.push_back(0x22);
// test.push_back(0x33);
// test.push_back(0x44);
// test.push_back(0x44);
// test.push_back(0x55);
// cout << mac_to_string(&test,0) << endl;
// return 0;
// }