Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include <base/address.h>
7 :
8 : #include <map>
9 : #include <boost/assign/list_of.hpp>
10 :
11 : using namespace std;
12 :
13 0 : Address::Address() {
14 0 : }
15 :
16 : static const std::map<string, Address::Family>
17 : fromString = boost::assign::map_list_of
18 : ("unspecified", Address::UNSPEC)
19 : ("inet", Address::INET)
20 : ("inet-labeled", Address::INETMPLS)
21 : ("inet6", Address::INET6)
22 : ("inet-vpn", Address::INETVPN)
23 : ("inet6-vpn", Address::INET6VPN)
24 : ("route-target", Address::RTARGET)
25 : ("e-vpn", Address::EVPN)
26 : ("inet-mvpn", Address::MVPN)
27 : ("erm-vpn", Address::ERMVPN);
28 :
29 : static const std::map<Address::Family, string>
30 : toString = boost::assign::map_list_of
31 : (Address::UNSPEC, "unspecified")
32 : (Address::INET, "inet")
33 : (Address::INETMPLS, "inet-labeled")
34 : (Address::INET6, "inet6")
35 : (Address::INETVPN, "inet-vpn")
36 : (Address::INET6VPN, "inet6-vpn")
37 : (Address::RTARGET, "route-target")
38 : (Address::EVPN, "e-vpn")
39 : (Address::MVPN, "inet-mvpn")
40 : (Address::ERMVPN, "erm-vpn");
41 :
42 : static const std::map<Address::Family, string>
43 : toTableName = boost::assign::map_list_of
44 : (Address::UNSPEC, "unspecified")
45 : (Address::INET, "inet")
46 : (Address::INETMPLS, "inet")
47 : (Address::INET6, "inet6")
48 : (Address::INETVPN, "l3vpn")
49 : (Address::INET6VPN, "l3vpn-inet6")
50 : (Address::RTARGET, "rtarget")
51 : (Address::EVPN, "evpn")
52 : (Address::MVPN, "mvpn")
53 : (Address::ERMVPN, "ermvpn");
54 :
55 34799 : Address::Family Address::FamilyFromString(const std::string &family) {
56 : std::map<string, Address::Family>::const_iterator loc =
57 34799 : fromString.find(family);
58 34799 : if (loc != fromString.end()) {
59 34799 : return loc->second;
60 : }
61 0 : return Address::UNSPEC;
62 : }
63 :
64 3501543 : std::string Address::FamilyToString(Address::Family family) {
65 7002980 : return toString.find(family)->second;
66 : }
67 :
68 257489 : std::string Address::FamilyToTableString(Address::Family family) {
69 514859 : return toTableName.find(family)->second;
70 : }
71 :
72 5543 : Address::Family Address::VpnFamilyFromFamily(Address::Family family) {
73 5543 : switch (family) {
74 5265 : case Address::INET:
75 : case Address::INETVPN:
76 5265 : return Address::INETVPN;
77 85 : case Address::INET6:
78 : case Address::INET6VPN:
79 85 : return Address::INET6VPN;
80 97 : case Address::EVPN:
81 97 : return Address::EVPN;
82 8 : case Address::MVPN:
83 8 : return Address::MVPN;
84 17 : case Address::ERMVPN:
85 17 : return Address::ERMVPN;
86 71 : default:
87 71 : return Address::UNSPEC;
88 : }
89 : return Address::UNSPEC;
90 : }
91 :
92 157768 : static int CountDots(const string &str) {
93 157768 : int count = 0;
94 157768 : size_t pos = 0;
95 618478 : while (pos < str.size()) {
96 618471 : pos = str.find('.', pos);
97 618475 : if (pos == string::npos) {
98 157765 : break;
99 : }
100 460710 : count++;
101 460710 : pos++;
102 : }
103 157767 : return count;
104 : }
105 :
106 : // Return the IP address part as it is i.e. 1.2.3.4/24 will return 1.2.3.4 and
107 : // 24.
108 178402 : boost::system::error_code Ip4PrefixParse(const string &str, Ip4Address *addr,
109 : int *plen) {
110 178402 : size_t pos = str.find('/');
111 178403 : if (pos == string::npos) {
112 35 : return make_error_code(boost::system::errc::invalid_argument);
113 : }
114 178368 : *plen = atoi(str.c_str() + pos + 1);
115 178368 : if ((*plen < 0) || (*plen > Address::kMaxV4PrefixLen)) {
116 20601 : return make_error_code(boost::system::errc::invalid_argument);
117 : }
118 :
119 157767 : string addrstr = str.substr(0, pos);
120 157767 : int dots = CountDots(addrstr);
121 170366 : while (dots < 3) {
122 12599 : addrstr.append(".0");
123 12599 : dots++;
124 : }
125 :
126 157767 : boost::system::error_code err;
127 157767 : *addr = Ip4Address::from_string(addrstr, err);
128 157769 : return err;
129 157769 : }
130 :
131 : // Return the bitwise and of IP and mask.
132 174603 : boost::system::error_code Ip4SubnetParse(const string &str, Ip4Address *addr,
133 : int *plen) {
134 174603 : Ip4Address address;
135 174603 : boost::system::error_code err;
136 174603 : err = Ip4PrefixParse(str, &address, plen);
137 174606 : if (!err) {
138 152487 : *addr = Address::GetIp4SubnetAddress(address, *plen);
139 : }
140 174605 : return err;
141 : }
142 :
143 : // Return the IP address part as it is i.e. 2001:db8:85a3:aaaa::b:c:d/64 will
144 : // return 2001:db8:85a3:aaaa::b:c:d and 64.
145 75438 : boost::system::error_code Inet6PrefixParse(const string &str, Ip6Address *addr,
146 : int *plen) {
147 75438 : size_t pos = str.find('/');
148 75438 : if (pos == string::npos) {
149 24 : return make_error_code(boost::system::errc::invalid_argument);
150 : }
151 75414 : *plen = atoi(str.c_str() + pos + 1);
152 75414 : if ((*plen < 0) || (*plen > Address::kMaxV6PrefixLen)) {
153 7 : return make_error_code(boost::system::errc::invalid_argument);
154 : }
155 :
156 75407 : string addrstr = str.substr(0, pos);
157 75407 : boost::system::error_code err;
158 75407 : *addr = Ip6Address::from_string(addrstr, err);
159 75407 : return err;
160 75407 : }
161 :
162 : // Return the bitwise and of IP and mask.
163 75002 : boost::system::error_code Inet6SubnetParse(const string &str, Ip6Address *addr,
164 : int *plen) {
165 75002 : Ip6Address address;
166 75002 : boost::system::error_code err;
167 75002 : err = Inet6PrefixParse(str, &address, plen);
168 75002 : if (!err) {
169 74968 : *addr = Address::GetIp6SubnetAddress(address, *plen);
170 : }
171 75002 : return err;
172 : }
173 :
174 : /* Returns IPv4 subnet address for a given IPv4 address and prefix length. The
175 : * IPv4 address and prefix lengths are converted to u32 numbers and then bitwise
176 : * AND operation is performed between those 2 numbers and the resulting u32
177 : * number is converted to IPv4Address object and returned. If prefix length is 0
178 : * then 0 is converted to IPv4Address object and returned.
179 : */
180 983280 : Ip4Address Address::GetIp4SubnetAddress(const Ip4Address &prefix, uint16_t plen) {
181 983280 : if (plen == 0) {
182 25453 : return boost::asio::ip::address_v4(0);
183 : }
184 :
185 957827 : Ip4Address subnet(prefix.to_ulong() & (0xFFFFFFFF << (32 - plen)));
186 957827 : return subnet;
187 : }
188 :
189 : /* Returns IPv6 subnet address for a given IPv6 address and prefix length. The
190 : * input IPv6 address is first converted into an array of 8 two byte integers.
191 : * Based on prefix length we first figure out the array index position from
192 : * where the masking of bits needs to start. Once the array index is found we
193 : * have 16 possible bit positions from where masking has to start. For each of
194 : * the 16 positions we have a unique 16 byte constant that we use to mask the
195 : * two byte integer. Once the array position is appropriately masked we mask
196 : * the rest of two byte integers till the array ends. The resulting array is
197 : * converted to IPv6 object and returned. If the prefix length is 0 then we
198 : * return an IPv6 object constructed using its default constructors which
199 : * assumes all bits as 0s.
200 : */
201 1458928 : Ip6Address Address::GetIp6SubnetAddress(const Ip6Address &prefix, uint16_t plen) {
202 1458928 : if (plen == 0) {
203 9893 : return boost::asio::ip::address_v6();
204 : }
205 :
206 1449035 : if (plen == 128) {
207 175690 : return prefix;
208 : }
209 :
210 : uint16_t ip6[8], in_ip6[8];
211 : unsigned char bytes[16];
212 :
213 1273345 : inet_pton(AF_INET6, prefix.to_string().c_str(), ip6);
214 :
215 11460105 : for (int i = 0; i < 8; ++i) {
216 10186760 : in_ip6[i] = ntohs(ip6[i]);
217 : }
218 :
219 1273345 : int index = (int) (plen / 16);
220 1273345 : int remain_mask = plen % 16;
221 :
222 1273345 : switch (remain_mask) {
223 81906 : case 0:
224 81906 : in_ip6[index++] = 0;
225 81906 : break;
226 76646 : case 1:
227 76646 : in_ip6[index++] &= 0x8000;
228 76646 : break;
229 76646 : case 2:
230 76646 : in_ip6[index++] &= 0xc000;
231 76646 : break;
232 76646 : case 3:
233 76646 : in_ip6[index++] &= 0xe000;
234 76646 : break;
235 76661 : case 4:
236 76661 : in_ip6[index++] &= 0xf000;
237 76661 : break;
238 76646 : case 5:
239 76646 : in_ip6[index++] &= 0xf800;
240 76646 : break;
241 76647 : case 6:
242 76647 : in_ip6[index++] &= 0xfc00;
243 76647 : break;
244 76649 : case 7:
245 76649 : in_ip6[index++] &= 0xfe00;
246 76649 : break;
247 109686 : case 8:
248 109686 : in_ip6[index++] &= 0xff00;
249 109686 : break;
250 76681 : case 9:
251 76681 : in_ip6[index++] &= 0xff80;
252 76681 : break;
253 76682 : case 10:
254 76682 : in_ip6[index++] &= 0xffc0;
255 76682 : break;
256 76893 : case 11:
257 76893 : in_ip6[index++] &= 0xffe0;
258 76893 : break;
259 76765 : case 12:
260 76765 : in_ip6[index++] &= 0xfff0;
261 76765 : break;
262 76681 : case 13:
263 76681 : in_ip6[index++] &= 0xfff8;
264 76681 : break;
265 76817 : case 14:
266 76817 : in_ip6[index++] &= 0xfffc;
267 76817 : break;
268 84693 : case 15:
269 84693 : in_ip6[index++] &= 0xfffe;
270 84693 : break;
271 : }
272 :
273 5564257 : for (int i = index; i < 8; ++i) {
274 4290912 : in_ip6[i] = 0;
275 : }
276 :
277 11460105 : for (int i = 0; i < 8; ++i) {
278 10186760 : ip6[i] = htons(in_ip6[i]);
279 : }
280 1273345 : memcpy(bytes, ip6, sizeof(ip6));
281 : Ip6Address::bytes_type to_bytes;
282 21646865 : for (int i = 0; i < 16; ++i) {
283 20373520 : to_bytes.at(i) = bytes[i];
284 : }
285 1273345 : return boost::asio::ip::address_v6(to_bytes);
286 : }
287 :
288 : // Ip6Address.to_v4() has exceptions. Plus, we dont have a to_v4() version
289 : // without exceptions that takes an boost::error_code.
290 : // If the v6-address is v4_mapped, return the ipv4 equivalent address. Else
291 : // return a 'zero' ipv4 address.
292 31448 : Ip4Address Address::V4FromV4MappedV6(const Ip6Address &v6_address) {
293 31448 : Ip4Address v4_address;
294 31448 : if (v6_address.is_v4_mapped()) {
295 31448 : Ip6Address::bytes_type v6_bt = v6_address.to_bytes();
296 : Ip4Address::bytes_type v4_bt =
297 31448 : { { v6_bt[12], v6_bt[13], v6_bt[14], v6_bt[15] } };
298 31448 : v4_address = Ip4Address(v4_bt);
299 : }
300 31448 : return v4_address;
301 : }
|