-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.hpp
354 lines (304 loc) · 7.6 KB
/
net.hpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* @file net.hpp
* @author Jayson Sho Toma
* @brief stores data structures and API for configuring network
* @version 0.1
* @date 2022-05-03
*/
#pragma once
#include <cstdint>
#include <cstring>
#include <string>
#include "printer.hpp"
// forward declaration
class ARPTable;
class MACTable;
/**
* @class IPAddress
* @brief represents an IP address.
*
*/
class IPAddress {
public:
/**
* @brief Construct a new IPAddress object
*
* @param addr 32-bit integer which represents an IP address
*/
explicit IPAddress(uint32_t addr);
/**
* @brief Construct a new IPAddress object
*
* @param addr string of form ddd.ddd.ddd.ddd which represents an IP address
*/
explicit IPAddress(const std::string &addr);
/**
* @brief cast operator which returns IP address as a 32-bit integer.
*
* @return uint32_t
*/
operator uint32_t() const;
/**
* @brief cast operator which returns IP address as a string of the form ddd.ddd.ddd.ddd
*
* @return std::string
*/
operator std::string() const;
/**
* @brief returns IP address masked with ~((1 << mask_size) - 1)
*
* @param mask_size
* @return IPAddress
*/
IPAddress applyMask(char mask_size) const;
/**
* @brief compares whether two IP addresses are same
*
* @param rhs IP address to be compared
* @return true if 32-bit integer representation of the IP address are equal
* @return false otherwise
*/
bool operator==(const IPAddress &rhs) const
{
return static_cast<uint32_t>(*this) == static_cast<uint32_t>(rhs);
}
bool operator!=(const IPAddress &rhs) const
{
return !(*this == rhs);
}
private:
uint32_t ip_addr;
};
/**
* @class MACAddress
* @brief represents a MAC address.
*
*/
class MACAddress {
public:
/**
* @brief Construct a new MACAddress object. All the bits of the MAC address are initialized with zero.
*
*/
MACAddress();
/**
* @brief Construct a new MACAddress object.
* 47th-bit to 0th-bit (counted from lsb) are recognized as a MAC address.
*
* @param val
*/
explicit MACAddress(uint64_t val);
/**
* @brief cast operator which returns MAC address as a string of the form XX:XX:XX:XX:XX:XX.
*
* @return std::string
*/
operator std::string() const;
uint64_t getBitRepresentation() const
{
uint64_t result = 0;
for (const auto &byte : mac) {
result = (result << 8) | byte;
}
return result;
}
/**
* @brief compares whether two MAC addresses are same
*
* @param rhs MAC address to be compared
* @return true if bit representation of the MAC address are equal
* @return false otherwise
*/
bool operator==(const MACAddress &rhs) const
{
return getBitRepresentation() == rhs.getBitRepresentation();
}
/**
* @brief broadcast MAC address (FF:FF:FF:FF:FF:FF).
*
*/
static const MACAddress BROADCAST_MAC_ADDRESS;
private:
static constexpr uint8_t MAC_ADDRESS_BYTE_LENGTH = 6;
char mac[MAC_ADDRESS_BYTE_LENGTH];
};
/**
* @class NodeNetworkProperty
* @brief stores network property used by network nodes.
*
*/
class NodeNetworkProperty : public IPrinter {
public:
/**
* @brief Construct a new Node Network Property object
*
*/
NodeNetworkProperty();
~NodeNetworkProperty();
/**
* @brief Get the Loopback Address object
*
* @return const IPAddress&
*/
const IPAddress &getLoopbackAddress() const
{
return loopback_addr;
}
/**
* @brief Set the Loopback Address object
*
* @param addr loopback IP address.
*/
void setLoopbackAddress(const IPAddress &addr)
{
loopback_addr = addr;
is_loopback_addr_configured = true;
}
const ARPTable *getARPTable() const
{
return arp_table;
}
const MACTable *getMACTable() const
{
return mac_table;
}
/**
* @brief outputs a detail of this node property on the standard output
*
*/
virtual void dump() const override;
private:
/* L2 properties */
ARPTable *arp_table;
MACTable *mac_table;
/* L3 properties */
bool is_loopback_addr_configured;
IPAddress loopback_addr;
};
/**
* @class InterfaceNetworkProperty
* @brief stores network property used by interfaces.
*
*/
class InterfaceNetworkProperty : public IPrinter {
public:
enum class L2Mode {
ACCESS,
TRUNK,
L2_MODE_UNKOWN,
};
/**
* @brief Construct a new Interface Network Property object
*
*/
InterfaceNetworkProperty();
/**
* @brief returns MAC address associated to the interface
*
* @return const MACAddress&
*/
const MACAddress &getMACAddress() const
{
return mac_addr;
}
/**
* @brief sets MAC address for the interface
*
* @param addr MAC address
*/
void setMACAddress(const MACAddress &addr)
{
mac_addr = addr;
}
/**
* @brief get IP address associated to the interface
*
* @return const IPAddress&
*/
const IPAddress &getIPAddress() const
{
return ip_addr;
}
/**
* @brief Get bit length of the subnet mask
*
* @return char
*/
char getMask() const
{
return mask;
}
/**
* @brief sets IP address and subnet mask for the interface
*
* @param addr IP address
* @param subnet_mask bit length of the subnet mask
*/
void setIPAddress(const IPAddress &addr, const char &subnet_mask)
{
ip_addr = addr;
mask = subnet_mask;
is_ip_addr_configured = true;
l2mode = L2Mode::L2_MODE_UNKOWN;
resetVLANSetting();
}
/**
* @brief unsets IP address associated to the interface
*
*/
void unsetIPAddress()
{
is_ip_addr_configured = false;
// TODO: not clearing `ip_addr` and `mask` might cause a security incident
}
/**
* @brief checks wheter this interface is acting as a L3 component.
*
* @return true if IP address is configured.
* @return false otherwise.
*/
bool isL3Mode() const
{
return is_ip_addr_configured;
}
const L2Mode &getL2Mode() const
{
return l2mode;
}
const std::string &getL2ModeStr() const
{
return L2ModeStr[static_cast<int>(l2mode)];
}
void setL2Mode(const L2Mode &mode)
{
l2mode = mode;
}
void resetVLANSetting();
bool isVLANMember(uint32_t vlan_id) const;
bool isVLANOccupied() const;
void updateVLANMemberShips(uint32_t vlan_id);
void addVLANMemberships(uint32_t vlan_id);
const uint32_t getVLANID() const;
/**
* @brief outputs a detail of this interface property on the standard output
*
*/
virtual void dump() const override;
private:
inline static const std::string L2ModeStr[] = {
"access",
"trunk",
"L2_MODE_UNKNOWN",
};
/* L2 properties */
inline static constexpr uint32_t MAX_VLAN_MEMBERSHIP = 10;
MACAddress mac_addr; // hard burnt in interface NIC
L2Mode l2mode;
std::array<uint16_t, MAX_VLAN_MEMBERSHIP> vlans;
/* L3 properties */
bool is_ip_addr_configured; /* set to true if IP address is configured
interface operates in L3 mode if IP address is configured on it */
IPAddress ip_addr;
char mask;
};
char *packetBufferShiftRight(char *packet, uint32_t packet_size, uint32_t total_buffer_size);