-
Notifications
You must be signed in to change notification settings - Fork 390
/
address.cc
336 lines (303 loc) · 10.4 KB
/
address.cc
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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <map>
#include "net/address.h"
#include <boost/assign/list_of.hpp>
using namespace std;
Address::Address() {
}
static const std::map<string, Address::Family>
fromString = boost::assign::map_list_of
("unspecified", Address::UNSPEC)
("inet", Address::INET)
("inet6", Address::INET6)
("inet-vpn", Address::INETVPN)
("inet6-vpn", Address::INET6VPN)
("route-target", Address::RTARGET)
("inet-flow", Address::INETFLOW)
("inet-vpn-flow", Address::INETVPNFLOW)
("inetmcast", Address::INETMCAST)
("inet6mcast", Address::INET6MCAST)
("enet", Address::ENET)
("e-vpn", Address::EVPN)
("erm-vpn", Address::ERMVPN);
static const std::map<Address::Family, string>
toString = boost::assign::map_list_of
(Address::UNSPEC, "unspecified")
(Address::INET, "inet")
(Address::INET6, "inet6")
(Address::INETVPN, "inet-vpn")
(Address::INET6VPN, "inet6-vpn")
(Address::RTARGET, "route-target")
(Address::INETFLOW, "inet-flow")
(Address::INETVPNFLOW, "inet-vpn-flow")
(Address::INETMCAST, "inetmcast")
(Address::INET6MCAST, "inet6mcast")
(Address::ENET, "enet")
(Address::EVPN, "e-vpn")
(Address::ERMVPN, "erm-vpn");
static const std::map<string, Address::Family>
fromTableName = boost::assign::map_list_of
("unspecified", Address::UNSPEC)
("inet", Address::INET)
("inet6", Address::INET6)
("l3vpn", Address::INETVPN)
("l3vpn-inet6", Address::INET6VPN)
("rtarget", Address::RTARGET)
("inetflow", Address::INETFLOW)
("invpnflow", Address::INETVPNFLOW)
("inetmcast", Address::INETMCAST)
("inet6mcast", Address::INET6MCAST)
("enet", Address::ENET)
("evpn", Address::EVPN)
("ermvpn", Address::ERMVPN);
static const std::map<Address::Family, string>
toTableName = boost::assign::map_list_of
(Address::UNSPEC, "unspecified")
(Address::INET, "inet")
(Address::INET6, "inet6")
(Address::INETVPN, "l3vpn")
(Address::INET6VPN, "l3vpn-inet6")
(Address::RTARGET, "rtarget")
(Address::INETFLOW, "inetflow")
(Address::INETVPNFLOW, "invpnflow")
(Address::INETMCAST, "inetmcast")
(Address::INET6MCAST, "inet6mcast")
(Address::ENET, "enet")
(Address::EVPN, "evpn")
(Address::ERMVPN, "ermvpn");
Address::Family Address::FamilyFromString(const std::string &family) {
std::map<string, Address::Family>::const_iterator loc =
fromString.find(family);
if (loc != fromString.end()) {
return loc->second;
}
return Address::UNSPEC;
}
std::string Address::FamilyToString(Address::Family family) {
return toString.find(family)->second;
}
Address::Family Address::FamilyFromRoutingTableName(const std::string &name) {
size_t pos1 = name.rfind('.');
if (pos1 == string::npos) return Address::UNSPEC;
size_t pos2 = name.rfind('.', pos1);
if (pos2 == string::npos) pos2 = 0;
std::map<string, Address::Family>::const_iterator loc =
fromTableName.find(name.substr(pos2, pos1 - pos2));
if (loc != fromTableName.end()) {
return loc->second;
}
return Address::UNSPEC;
}
std::string Address::FamilyToTableString(Address::Family family) {
return toTableName.find(family)->second;
}
Address::Family Address::VpnFamilyFromFamily(Address::Family family) {
switch (family) {
case Address::INET:
case Address::INETVPN:
return Address::INETVPN;
case Address::INET6:
case Address::INET6VPN:
return Address::INET6VPN;
case Address::EVPN:
return Address::EVPN;
case Address::ERMVPN:
return Address::ERMVPN;
default:
return Address::UNSPEC;
}
return Address::UNSPEC;
}
static int CountDots(const string &str) {
int count = 0;
size_t pos = 0;
while (pos < str.size()) {
pos = str.find('.', pos);
if (pos == string::npos) {
break;
}
count++;
pos++;
}
return count;
}
// Return the IP address part as it is i.e. 1.2.3.4/24 will return 1.2.3.4 and
// 24.
boost::system::error_code Ip4PrefixParse(const string &str, Ip4Address *addr,
int *plen) {
size_t pos = str.find('/');
if (pos == string::npos) {
return make_error_code(boost::system::errc::invalid_argument);
}
*plen = atoi(str.c_str() + pos + 1);
if ((*plen < 0) || (*plen > Address::kMaxV4PrefixLen)) {
return make_error_code(boost::system::errc::invalid_argument);
}
string addrstr = str.substr(0, pos);
int dots = CountDots(addrstr);
while (dots < 3) {
addrstr.append(".0");
dots++;
}
boost::system::error_code err;
*addr = Ip4Address::from_string(addrstr, err);
return err;
}
// Return the bitwise and of IP and mask.
boost::system::error_code Ip4SubnetParse(const string &str, Ip4Address *addr,
int *plen) {
Ip4Address address;
boost::system::error_code err;
err = Ip4PrefixParse(str, &address, plen);
if (!err) {
*addr = Address::GetIp4SubnetAddress(address, *plen);
}
return err;
}
// Return the IP address part as it is i.e. 2001:db8:85a3:aaaa::b:c:d/64 will
// return 2001:db8:85a3:aaaa::b:c:d and 64.
boost::system::error_code Inet6PrefixParse(const string &str, Ip6Address *addr,
int *plen) {
size_t pos = str.find('/');
if (pos == string::npos) {
return make_error_code(boost::system::errc::invalid_argument);
}
*plen = atoi(str.c_str() + pos + 1);
if ((*plen < 0) || (*plen > Address::kMaxV6PrefixLen)) {
return make_error_code(boost::system::errc::invalid_argument);
}
string addrstr = str.substr(0, pos);
boost::system::error_code err;
*addr = Ip6Address::from_string(addrstr, err);
return err;
}
// Return the bitwise and of IP and mask.
boost::system::error_code Inet6SubnetParse(const string &str, Ip6Address *addr,
int *plen) {
Ip6Address address;
boost::system::error_code err;
err = Inet6PrefixParse(str, &address, plen);
if (!err) {
*addr = Address::GetIp6SubnetAddress(address, *plen);
}
return err;
}
/* Returns IPv4 subnet address for a given IPv4 address and prefix length. The
* IPv4 address and prefix lengths are converted to u32 numbers and then bitwise
* AND operation is performed between those 2 numbers and the resulting u32
* number is converted to IPv4Address object and returned. If prefix length is 0
* then 0 is converted to IPv4Address object and returned.
*/
Ip4Address Address::GetIp4SubnetAddress(const Ip4Address &prefix, uint16_t plen) {
if (plen == 0) {
return boost::asio::ip::address_v4(0);
}
Ip4Address subnet(prefix.to_ulong() & (0xFFFFFFFF << (32 - plen)));
return subnet;
}
/* Returns IPv6 subnet address for a given IPv6 address and prefix length. The
* input IPv6 address is first converted into an array of 8 two byte integers.
* Based on prefix length we first figure out the array index position from
* where the masking of bits needs to start. Once the array index is found we
* have 16 possible bit positions from where masking has to start. For each of
* the 16 positions we have a unique 16 byte constant that we use to mask the
* two byte integer. Once the array position is appropriately masked we mask
* the rest of two byte integers till the array ends. The resulting array is
* converted to IPv6 object and returned. If the prefix length is 0 then we
* return an IPv6 object constructed using its default constructors which
* assumes all bits as 0s.
*/
Ip6Address Address::GetIp6SubnetAddress(const Ip6Address &prefix, uint16_t plen) {
if (plen == 0) {
return boost::asio::ip::address_v6();
}
if (plen == 128) {
return prefix;
}
uint16_t ip6[8], in_ip6[8];
unsigned char bytes[16];
inet_pton(AF_INET6, prefix.to_string().c_str(), ip6);
for (int i = 0; i < 8; ++i) {
in_ip6[i] = ntohs(ip6[i]);
}
int index = (int) (plen / 16);
int remain_mask = plen % 16;
switch (remain_mask) {
case 0:
in_ip6[index++] = 0;
break;
case 1:
in_ip6[index++] &= 0x8000;
break;
case 2:
in_ip6[index++] &= 0xc000;
break;
case 3:
in_ip6[index++] &= 0xe000;
break;
case 4:
in_ip6[index++] &= 0xf000;
break;
case 5:
in_ip6[index++] &= 0xf800;
break;
case 6:
in_ip6[index++] &= 0xfc00;
break;
case 7:
in_ip6[index++] &= 0xfe00;
break;
case 8:
in_ip6[index++] &= 0xff00;
break;
case 9:
in_ip6[index++] &= 0xff80;
break;
case 10:
in_ip6[index++] &= 0xffc0;
break;
case 11:
in_ip6[index++] &= 0xffe0;
break;
case 12:
in_ip6[index++] &= 0xfff0;
break;
case 13:
in_ip6[index++] &= 0xfff8;
break;
case 14:
in_ip6[index++] &= 0xfffc;
break;
case 15:
in_ip6[index++] &= 0xfffe;
break;
}
for (int i = index; i < 8; ++i) {
in_ip6[i] = 0;
}
for (int i = 0; i < 8; ++i) {
ip6[i] = htons(in_ip6[i]);
}
memcpy(bytes, ip6, sizeof(ip6));
boost::array<uint8_t, 16> to_bytes;
for (int i = 0; i < 16; ++i) {
to_bytes.at(i) = bytes[i];
}
return boost::asio::ip::address_v6(to_bytes);
}
// Ip6Address.to_v4() has exceptions. Plus, we dont have a to_v4() version
// without exceptions that takes an boost::error_code.
// If the v6-address is v4_mapped, return the ipv4 equivalent address. Else
// return a 'zero' ipv4 address.
Ip4Address Address::V4FromV4MappedV6(const Ip6Address &v6_address) {
Ip4Address v4_address;
if (v6_address.is_v4_mapped()) {
Ip6Address::bytes_type v6_bt = v6_address.to_bytes();
Ip4Address::bytes_type v4_bt =
{ { v6_bt[12], v6_bt[13], v6_bt[14], v6_bt[15] } };
v4_address = Ip4Address(v4_bt);
}
return v4_address;
}