]>
Commit | Line | Data |
---|---|---|
41d93087 | 1 | /* |
b510f3a1 | 2 | * DEBUG: section 14 IP Storage and Handling |
41d93087 | 3 | */ |
f7f3304a | 4 | #include "squid.h" |
27bc2077 | 5 | #include "compat/getaddrinfo.h" |
602d9612 | 6 | #include "compat/inet_ntop.h" |
82b7abe3 | 7 | #include "Debug.h" |
96d89ea0 | 8 | #include "ip/Address.h" |
055421ee | 9 | #include "ip/tools.h" |
1ef0b9ce AJ |
10 | #include "util.h" |
11 | ||
074d6a40 AJ |
12 | #include <cassert> |
13 | #include <cstring> | |
41d93087 | 14 | #if HAVE_ARPA_INET_H |
489520a9 AJ |
15 | /* for inet_ntoa() */ |
16 | #include <arpa/inet.h> | |
41d93087 | 17 | #endif |
18 | ||
41d93087 | 19 | /* Debugging only. Dump the address content when a fatal assert is encountered. */ |
41d93087 | 20 | #define IASSERT(a,b) \ |
68b313c2 | 21 | if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \ |
4dd643d5 | 22 | printf("Ip::Address invalid? with isIPv4()=%c, isIPv6()=%c\n",(isIPv4()?'T':'F'),(isIPv6()?'T':'F')); \ |
41d93087 | 23 | printf("ADDRESS:"); \ |
4dd643d5 AJ |
24 | for(unsigned int i = 0; i < sizeof(mSocketAddr_.sin6_addr); ++i) { \ |
25 | printf(" %x", mSocketAddr_.sin6_addr.s6_addr[i]); \ | |
41d93087 | 26 | } printf("\n"); assert(b); \ |
27 | } | |
41d93087 | 28 | |
41d93087 | 29 | int |
4dd643d5 | 30 | Ip::Address::cidr() const |
41d93087 | 31 | { |
32 | uint8_t shift,byte; | |
33 | uint8_t bit,caught; | |
34 | int len = 0; | |
4dd643d5 | 35 | const uint8_t *ptr= mSocketAddr_.sin6_addr.s6_addr; |
41d93087 | 36 | |
37 | /* Let's scan all the bits from Most Significant to Least */ | |
38 | /* Until we find an "0" bit. Then, we return */ | |
39 | shift=0; | |
40 | ||
41d93087 | 41 | /* return IPv4 CIDR for any Mapped address */ |
42 | /* Thus only check the mapped bit */ | |
43 | ||
4dd643d5 | 44 | if ( !isIPv6() ) { |
41d93087 | 45 | shift = 12; |
46 | } | |
47 | ||
4dd643d5 | 48 | for (; shift<sizeof(mSocketAddr_.sin6_addr) ; ++shift) { |
41d93087 | 49 | byte= *(ptr+shift); |
50 | ||
51 | if (byte == 0xFF) { | |
52 | len += 8; | |
53 | continue ; /* A short-cut */ | |
54 | } | |
55 | ||
5e263176 | 56 | for (caught = 0 , bit= 7 ; !caught && (bit <= 7); --bit) { |
41d93087 | 57 | caught = ((byte & 0x80) == 0x00); /* Found a '0' at 'bit' ? */ |
58 | ||
59 | if (!caught) | |
d7ae3534 | 60 | ++len; |
41d93087 | 61 | |
62 | byte <<= 1; | |
63 | } | |
64 | ||
65 | if (caught) | |
66 | break; /* We have found the most significant "0" bit. */ | |
67 | } | |
68 | ||
69 | return len; | |
70 | } | |
71 | ||
881c4733 | 72 | int |
4dd643d5 | 73 | Ip::Address::applyMask(Ip::Address const &mask_addr) |
41d93087 | 74 | { |
4dd643d5 AJ |
75 | uint32_t *p1 = (uint32_t*)(&mSocketAddr_.sin6_addr); |
76 | uint32_t const *p2 = (uint32_t const *)(&mask_addr.mSocketAddr_.sin6_addr); | |
77 | unsigned int blen = sizeof(mSocketAddr_.sin6_addr)/sizeof(uint32_t); | |
41d93087 | 78 | unsigned int changes = 0; |
79 | ||
d7ae3534 | 80 | for (unsigned int i = 0; i < blen; ++i) { |
26ac0430 | 81 | if ((p1[i] & p2[i]) != p1[i]) |
d7ae3534 | 82 | ++changes; |
41d93087 | 83 | |
84 | p1[i] &= p2[i]; | |
85 | } | |
86 | ||
41d93087 | 87 | return changes; |
88 | } | |
89 | ||
b7ac5457 | 90 | bool |
4dd643d5 | 91 | Ip::Address::applyMask(const unsigned int cidrMask, int mtype) |
41d93087 | 92 | { |
93 | uint8_t clearbits = 0; | |
94 | uint8_t* p = NULL; | |
95 | ||
41d93087 | 96 | // validation and short-cuts. |
4dd643d5 | 97 | if (cidrMask > 128) |
41d93087 | 98 | return false; |
99 | ||
4dd643d5 | 100 | if (cidrMask > 32 && mtype == AF_INET) |
41d93087 | 101 | return false; |
102 | ||
4dd643d5 | 103 | if (cidrMask == 0) { |
2c1b9590 | 104 | /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */ |
4dd643d5 | 105 | setNoAddr(); |
2c1b9590 AJ |
106 | return true; |
107 | } | |
108 | ||
4dd643d5 | 109 | clearbits = (uint8_t)( (mtype==AF_INET6?128:32) - cidrMask); |
41d93087 | 110 | |
111 | // short-cut | |
26ac0430 | 112 | if (clearbits == 0) |
41d93087 | 113 | return true; |
114 | ||
4dd643d5 | 115 | p = (uint8_t*)(&mSocketAddr_.sin6_addr) + 15; |
41d93087 | 116 | |
4dd643d5 | 117 | for (; clearbits>0 && p >= (uint8_t*)&mSocketAddr_.sin6_addr ; --p ) { |
26ac0430 | 118 | if (clearbits < 8) { |
41d93087 | 119 | *p &= ((0xFF << clearbits) & 0xFF); |
120 | clearbits = 0; | |
121 | } else { | |
122 | *p &= 0x00; | |
123 | clearbits -= 8; | |
124 | } | |
125 | } | |
126 | ||
127 | return true; | |
128 | } | |
129 | ||
b7ac5457 | 130 | bool |
4dd643d5 | 131 | Ip::Address::isSockAddr() const |
41d93087 | 132 | { |
4dd643d5 | 133 | return (mSocketAddr_.sin6_port != 0); |
41d93087 | 134 | } |
135 | ||
b7ac5457 | 136 | bool |
4dd643d5 | 137 | Ip::Address::isIPv4() const |
41d93087 | 138 | { |
4dd643d5 | 139 | return IN6_IS_ADDR_V4MAPPED( &mSocketAddr_.sin6_addr ); |
41d93087 | 140 | } |
141 | ||
b7ac5457 | 142 | bool |
4dd643d5 | 143 | Ip::Address::isIPv6() const |
41d93087 | 144 | { |
4dd643d5 | 145 | return !isIPv4(); |
41d93087 | 146 | } |
147 | ||
b7ac5457 | 148 | bool |
4dd643d5 | 149 | Ip::Address::isAnyAddr() const |
41d93087 | 150 | { |
4dd643d5 | 151 | return IN6_IS_ADDR_UNSPECIFIED(&mSocketAddr_.sin6_addr) || IN6_ARE_ADDR_EQUAL(&mSocketAddr_.sin6_addr, &v4_anyaddr); |
41d93087 | 152 | } |
153 | ||
154 | /// NOTE: Does NOT clear the Port stored. Ony the Address and Type. | |
b7ac5457 | 155 | void |
4dd643d5 | 156 | Ip::Address::setAnyAddr() |
41d93087 | 157 | { |
4dd643d5 | 158 | memset(&mSocketAddr_.sin6_addr, 0, sizeof(struct in6_addr) ); |
41d93087 | 159 | } |
160 | ||
b7ac5457 AJ |
161 | /// NOTE: completely empties the Ip::Address structure. Address, Port, Type, everything. |
162 | void | |
4dd643d5 | 163 | Ip::Address::setEmpty() |
41d93087 | 164 | { |
4dd643d5 | 165 | memset(&mSocketAddr_, 0, sizeof(mSocketAddr_) ); |
41d93087 | 166 | } |
167 | ||
349a322f | 168 | #if _SQUID_AIX_ |
d48a52bf A |
169 | // Bug 2885 comment 78 explains. |
170 | // In short AIX has a different netinet/in.h union definition | |
349a322f AJ |
171 | const struct in6_addr Ip::Address::v4_localhost = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x7f000001 }}}; |
172 | const struct in6_addr Ip::Address::v4_anyaddr = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x00000000 }}}; | |
173 | const struct in6_addr Ip::Address::v4_noaddr = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0xffffffff }}}; | |
174 | const struct in6_addr Ip::Address::v6_noaddr = {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}; | |
175 | #else | |
b7ac5457 | 176 | const struct in6_addr Ip::Address::v4_localhost = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
d5b4b6c7 | 177 | 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }} |
c0248e14 | 178 | }; |
b7ac5457 | 179 | const struct in6_addr Ip::Address::v4_anyaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
d5b4b6c7 | 180 | 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }} |
c0248e14 | 181 | }; |
0eb08770 HN |
182 | const struct in6_addr Ip::Address::v4_noaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
183 | 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }} | |
184 | }; | |
b7ac5457 | 185 | const struct in6_addr Ip::Address::v6_noaddr = {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
d5b4b6c7 | 186 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }} |
c0248e14 | 187 | }; |
349a322f | 188 | #endif |
41d93087 | 189 | |
b7ac5457 | 190 | bool |
4dd643d5 | 191 | Ip::Address::setIPv4() |
c0248e14 | 192 | { |
4dd643d5 AJ |
193 | if ( isLocalhost() ) { |
194 | mSocketAddr_.sin6_addr = v4_localhost; | |
41d93087 | 195 | return true; |
196 | } | |
41d93087 | 197 | |
4dd643d5 AJ |
198 | if ( isAnyAddr() ) { |
199 | mSocketAddr_.sin6_addr = v4_anyaddr; | |
41d93087 | 200 | return true; |
201 | } | |
202 | ||
4dd643d5 AJ |
203 | if ( isNoAddr() ) { |
204 | mSocketAddr_.sin6_addr = v4_noaddr; | |
0906f01d CT |
205 | return true; |
206 | } | |
207 | ||
4dd643d5 | 208 | if ( isIPv4()) |
41d93087 | 209 | return true; |
210 | ||
211 | // anything non-IPv4 and non-convertable is BAD. | |
212 | return false; | |
41d93087 | 213 | } |
214 | ||
b7ac5457 | 215 | bool |
4dd643d5 | 216 | Ip::Address::isLocalhost() const |
41d93087 | 217 | { |
4dd643d5 | 218 | return IN6_IS_ADDR_LOOPBACK( &mSocketAddr_.sin6_addr ) || IN6_ARE_ADDR_EQUAL( &mSocketAddr_.sin6_addr, &v4_localhost ); |
41d93087 | 219 | } |
220 | ||
b7ac5457 | 221 | void |
4dd643d5 | 222 | Ip::Address::setLocalhost() |
41d93087 | 223 | { |
055421ee | 224 | if (Ip::EnableIpv6) { |
4dd643d5 AJ |
225 | mSocketAddr_.sin6_addr = in6addr_loopback; |
226 | mSocketAddr_.sin6_family = AF_INET6; | |
055421ee | 227 | } else { |
4dd643d5 AJ |
228 | mSocketAddr_.sin6_addr = v4_localhost; |
229 | mSocketAddr_.sin6_family = AF_INET; | |
055421ee | 230 | } |
41d93087 | 231 | } |
232 | ||
b7ac5457 | 233 | bool |
4dd643d5 | 234 | Ip::Address::isSiteLocal6() const |
a98c2da5 | 235 | { |
dd19b634 AJ |
236 | // RFC 4193 the site-local allocated range is fc00::/7 |
237 | // with fd00::/8 as the only currently allocated range (so we test it first). | |
238 | // BUG: as of 2010-02 Linux and BSD define IN6_IS_ADDR_SITELOCAL() to check for fec::/10 | |
4dd643d5 AJ |
239 | return mSocketAddr_.sin6_addr.s6_addr[0] == static_cast<uint8_t>(0xfd) || |
240 | mSocketAddr_.sin6_addr.s6_addr[0] == static_cast<uint8_t>(0xfc); | |
a98c2da5 AJ |
241 | } |
242 | ||
b7ac5457 | 243 | bool |
4dd643d5 | 244 | Ip::Address::isSiteLocalAuto() const |
a98c2da5 | 245 | { |
4dd643d5 AJ |
246 | return mSocketAddr_.sin6_addr.s6_addr[10] == static_cast<uint8_t>(0xff) && |
247 | mSocketAddr_.sin6_addr.s6_addr[11] == static_cast<uint8_t>(0xfe); | |
a98c2da5 AJ |
248 | } |
249 | ||
b7ac5457 | 250 | bool |
4dd643d5 | 251 | Ip::Address::isNoAddr() const |
41d93087 | 252 | { |
253 | // IFF the address == 0xff..ff (all ones) | |
4dd643d5 AJ |
254 | return IN6_ARE_ADDR_EQUAL( &mSocketAddr_.sin6_addr, &v6_noaddr ) |
255 | || IN6_ARE_ADDR_EQUAL( &mSocketAddr_.sin6_addr, &v4_noaddr ); | |
41d93087 | 256 | } |
257 | ||
b7ac5457 | 258 | void |
4dd643d5 | 259 | Ip::Address::setNoAddr() |
41d93087 | 260 | { |
4dd643d5 AJ |
261 | memset(&mSocketAddr_.sin6_addr, 0xFF, sizeof(struct in6_addr) ); |
262 | mSocketAddr_.sin6_family = AF_INET6; | |
41d93087 | 263 | } |
264 | ||
b7ac5457 | 265 | bool |
4dd643d5 | 266 | Ip::Address::getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const |
41d93087 | 267 | { |
268 | char *p = buf; | |
269 | unsigned char const *r = dat.s6_addr; | |
270 | ||
271 | /* RFC1886 says: */ | |
272 | /* 4321:0:1:2:3:4:567:89ab */ | |
273 | /* must be sent */ | |
274 | /* b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.0.0.0.0.1.2.3.4.ip6.int. */ | |
275 | ||
276 | /* Work from the binary field. Anything else may have representation changes. */ | |
277 | /* The sin6_port and sin6_addr members shall be in network byte order. */ | |
278 | ||
279 | /* Compile Err: 'Too many arguments for format. */ | |
280 | ||
5e263176 | 281 | for (int i = 15; i >= 0; --i, p+=4) { |
41d93087 | 282 | snprintf(p, 5, "%x.%x.", ((r[i])&0xf), (((r[i])>>4)&0xf) ); |
283 | } | |
284 | ||
285 | /* RFC3152 says: */ | |
286 | /* ip6.int is now deprecated TLD, use ip6.arpa instead. */ | |
287 | snprintf(p,10,"ip6.arpa."); | |
288 | ||
289 | return true; | |
290 | } | |
41d93087 | 291 | |
b7ac5457 | 292 | bool |
4dd643d5 | 293 | Ip::Address::getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const |
41d93087 | 294 | { |
295 | unsigned int i = (unsigned int) ntohl(dat.s_addr); | |
296 | snprintf(buf, 32, "%u.%u.%u.%u.in-addr.arpa.", | |
297 | i & 255, | |
298 | (i >> 8) & 255, | |
299 | (i >> 16) & 255, | |
300 | (i >> 24) & 255); | |
301 | return true; | |
302 | } | |
303 | ||
b7ac5457 | 304 | bool |
4dd643d5 | 305 | Ip::Address::getReverseString(char buf[MAX_IPSTRLEN], int show_type) const |
41d93087 | 306 | { |
307 | ||
26ac0430 | 308 | if (show_type == AF_UNSPEC) { |
4dd643d5 | 309 | show_type = isIPv6() ? AF_INET6 : AF_INET ; |
41d93087 | 310 | } |
311 | ||
4dd643d5 AJ |
312 | if (show_type == AF_INET && isIPv4()) { |
313 | struct in_addr* tmp = (struct in_addr*)&mSocketAddr_.sin6_addr.s6_addr[12]; | |
314 | return getReverseString4(buf, *tmp); | |
315 | } else if ( show_type == AF_INET6 && isIPv6() ) { | |
316 | return getReverseString6(buf, mSocketAddr_.sin6_addr); | |
41d93087 | 317 | } |
318 | ||
4dd643d5 | 319 | debugs(14, DBG_CRITICAL, "Unable to convert '" << toStr(buf,MAX_IPSTRLEN) << "' to the rDNS type requested."); |
41d93087 | 320 | |
321 | buf[0] = '\0'; | |
322 | ||
323 | return false; | |
324 | } | |
325 | ||
b7ac5457 AJ |
326 | Ip::Address& |
327 | Ip::Address::operator =(const Ip::Address &s) | |
41d93087 | 328 | { |
b7ac5457 | 329 | memcpy(this, &s, sizeof(Ip::Address)); |
41d93087 | 330 | return *this; |
331 | }; | |
332 | ||
b7ac5457 | 333 | Ip::Address::Address(const char*s) |
41d93087 | 334 | { |
4dd643d5 AJ |
335 | setEmpty(); |
336 | lookupHostIP(s, true); | |
41d93087 | 337 | } |
338 | ||
b7ac5457 AJ |
339 | bool |
340 | Ip::Address::operator =(const char* s) | |
41d93087 | 341 | { |
4dd643d5 | 342 | return lookupHostIP(s, true); |
41d93087 | 343 | } |
344 | ||
b7ac5457 AJ |
345 | bool |
346 | Ip::Address::GetHostByName(const char* s) | |
41d93087 | 347 | { |
4dd643d5 | 348 | return lookupHostIP(s, false); |
41d93087 | 349 | } |
350 | ||
b7ac5457 | 351 | bool |
4dd643d5 | 352 | Ip::Address::lookupHostIP(const char *s, bool nodns) |
41d93087 | 353 | { |
41d93087 | 354 | struct addrinfo want; |
41d93087 | 355 | memset(&want, 0, sizeof(struct addrinfo)); |
26ac0430 | 356 | if (nodns) { |
41d93087 | 357 | want.ai_flags = AI_NUMERICHOST; // prevent actual DNS lookups! |
358 | } | |
41d93087 | 359 | |
4dd643d5 AJ |
360 | int err = 0; |
361 | struct addrinfo *res = NULL; | |
27bc2077 | 362 | if ( (err = getaddrinfo(s, NULL, &want, &res)) != 0) { |
380de14e | 363 | debugs(14,3, HERE << "Given Non-IP '" << s << "': " << gai_strerror(err) ); |
27bc2077 | 364 | /* free the memory getaddrinfo() dynamically allocated. */ |
4dd643d5 | 365 | if (res) |
27bc2077 | 366 | freeaddrinfo(res); |
41d93087 | 367 | return false; |
368 | } | |
369 | ||
370 | /* | |
371 | * NP: =(sockaddr_*) may alter the port. we don't want that. | |
372 | * all we have been given as input was an IPA. | |
373 | */ | |
4dd643d5 | 374 | short portSaved = port(); |
41d93087 | 375 | operator=(*res); |
4dd643d5 | 376 | port(portSaved); |
41d93087 | 377 | |
27bc2077 AJ |
378 | /* free the memory getaddrinfo() dynamically allocated. */ |
379 | freeaddrinfo(res); | |
41d93087 | 380 | return true; |
381 | } | |
382 | ||
b7ac5457 | 383 | Ip::Address::Address(struct sockaddr_in const &s) |
41d93087 | 384 | { |
4dd643d5 | 385 | setEmpty(); |
41d93087 | 386 | operator=(s); |
387 | }; | |
388 | ||
b7ac5457 AJ |
389 | Ip::Address & |
390 | Ip::Address::operator =(struct sockaddr_in const &s) | |
41d93087 | 391 | { |
4dd643d5 AJ |
392 | map4to6((const in_addr)s.sin_addr, mSocketAddr_.sin6_addr); |
393 | mSocketAddr_.sin6_port = s.sin_port; | |
394 | mSocketAddr_.sin6_family = AF_INET6; | |
41d93087 | 395 | return *this; |
396 | }; | |
397 | ||
b7ac5457 AJ |
398 | Ip::Address & |
399 | Ip::Address::operator =(const struct sockaddr_storage &s) | |
0e1a47f0 | 400 | { |
0e1a47f0 | 401 | /* some AF_* magic to tell socket types apart and what we need to do */ |
26ac0430 | 402 | if (s.ss_family == AF_INET6) { |
7da22087 | 403 | memcpy(&mSocketAddr_, &s, sizeof(struct sockaddr_in6)); |
26ac0430 | 404 | } else { // convert it to our storage mapping. |
0e1a47f0 | 405 | struct sockaddr_in *sin = (struct sockaddr_in*)&s; |
4dd643d5 AJ |
406 | mSocketAddr_.sin6_port = sin->sin_port; |
407 | map4to6( sin->sin_addr, mSocketAddr_.sin6_addr); | |
0e1a47f0 | 408 | } |
0e1a47f0 AJ |
409 | return *this; |
410 | }; | |
411 | ||
b7ac5457 | 412 | Ip::Address::Address(struct sockaddr_in6 const &s) |
41d93087 | 413 | { |
4dd643d5 | 414 | setEmpty(); |
41d93087 | 415 | operator=(s); |
416 | }; | |
417 | ||
b7ac5457 AJ |
418 | Ip::Address & |
419 | Ip::Address::operator =(struct sockaddr_in6 const &s) | |
41d93087 | 420 | { |
4dd643d5 | 421 | memcpy(&mSocketAddr_, &s, sizeof(struct sockaddr_in6)); |
b5587d15 | 422 | |
41d93087 | 423 | return *this; |
424 | }; | |
41d93087 | 425 | |
b7ac5457 | 426 | Ip::Address::Address(struct in_addr const &s) |
41d93087 | 427 | { |
4dd643d5 | 428 | setEmpty(); |
41d93087 | 429 | operator=(s); |
430 | }; | |
431 | ||
b7ac5457 AJ |
432 | Ip::Address & |
433 | Ip::Address::operator =(struct in_addr const &s) | |
41d93087 | 434 | { |
4dd643d5 AJ |
435 | map4to6((const in_addr)s, mSocketAddr_.sin6_addr); |
436 | mSocketAddr_.sin6_family = AF_INET6; | |
41d93087 | 437 | return *this; |
438 | }; | |
439 | ||
b7ac5457 | 440 | Ip::Address::Address(struct in6_addr const &s) |
41d93087 | 441 | { |
4dd643d5 | 442 | setEmpty(); |
41d93087 | 443 | operator=(s); |
444 | }; | |
445 | ||
b7ac5457 AJ |
446 | Ip::Address & |
447 | Ip::Address::operator =(struct in6_addr const &s) | |
41d93087 | 448 | { |
b5587d15 | 449 | |
4dd643d5 AJ |
450 | memcpy(&mSocketAddr_.sin6_addr, &s, sizeof(struct in6_addr)); |
451 | mSocketAddr_.sin6_family = AF_INET6; | |
b5587d15 | 452 | |
41d93087 | 453 | return *this; |
454 | }; | |
41d93087 | 455 | |
b7ac5457 | 456 | Ip::Address::Address(const Ip::Address &s) |
41d93087 | 457 | { |
4dd643d5 | 458 | setEmpty(); |
41d93087 | 459 | operator=(s); |
460 | } | |
461 | ||
b7ac5457 | 462 | Ip::Address::Address(const struct hostent &s) |
41d93087 | 463 | { |
4dd643d5 | 464 | setEmpty(); |
41d93087 | 465 | operator=(s); |
466 | } | |
467 | ||
b7ac5457 AJ |
468 | bool |
469 | Ip::Address::operator =(const struct hostent &s) | |
41d93087 | 470 | { |
471 | ||
472 | struct in_addr* ipv4 = NULL; | |
473 | ||
474 | struct in6_addr* ipv6 = NULL; | |
475 | ||
476 | //struct hostent { | |
477 | // char *h_name; /* official name of host */ | |
478 | // char **h_aliases; /* alias list */ | |
479 | // int h_addrtype; /* host address type */ | |
480 | // int h_length; /* length of address */ | |
481 | // char **h_addr_list; /* list of addresses */ | |
482 | //} | |
483 | ||
26ac0430 | 484 | switch (s.h_addrtype) { |
41d93087 | 485 | |
486 | case AF_INET: | |
487 | ipv4 = (in_addr*)(s.h_addr_list[0]); | |
488 | /* this */ | |
489 | operator=(*ipv4); | |
490 | break; | |
491 | ||
492 | case AF_INET6: | |
493 | ipv6 = (in6_addr*)(s.h_addr_list[0]); | |
41d93087 | 494 | /* this */ |
495 | operator=(*ipv6); | |
41d93087 | 496 | break; |
497 | ||
498 | default: | |
26ac0430 AJ |
499 | IASSERT("false",false); |
500 | return false; | |
41d93087 | 501 | } |
502 | ||
503 | return true; | |
504 | } | |
505 | ||
b7ac5457 | 506 | Ip::Address::Address(const struct addrinfo &s) |
41d93087 | 507 | { |
4dd643d5 | 508 | setEmpty(); |
41d93087 | 509 | operator=(s); |
510 | } | |
511 | ||
b7ac5457 AJ |
512 | bool |
513 | Ip::Address::operator =(const struct addrinfo &s) | |
41d93087 | 514 | { |
515 | ||
516 | struct sockaddr_in* ipv4 = NULL; | |
517 | ||
518 | struct sockaddr_in6* ipv6 = NULL; | |
519 | ||
520 | //struct addrinfo { | |
521 | // int ai_flags; /* input flags */ | |
522 | // int ai_family; /* protocol family for socket */ | |
523 | // int ai_socktype; /* socket type */ | |
524 | // int ai_protocol; /* protocol for socket */ | |
525 | // socklen_t ai_addrlen; /* length of socket-address */ | |
526 | // struct sockaddr *ai_addr; /* socket-address for socket */ | |
527 | // char *ai_canonname; /* canonical name for service location */ | |
528 | // struct addrinfo *ai_next; /* pointer to next in list */ | |
529 | //} | |
530 | ||
26ac0430 | 531 | switch (s.ai_family) { |
41d93087 | 532 | |
533 | case AF_INET: | |
534 | ipv4 = (sockaddr_in*)(s.ai_addr); | |
535 | /* this */ | |
536 | assert(ipv4); | |
537 | operator=(*ipv4); | |
538 | break; | |
539 | ||
540 | case AF_INET6: | |
541 | ipv6 = (sockaddr_in6*)(s.ai_addr); | |
41d93087 | 542 | /* this */ |
543 | assert(ipv6); | |
544 | operator=(*ipv6); | |
41d93087 | 545 | break; |
546 | ||
547 | case AF_UNSPEC: | |
548 | default: | |
549 | // attempt to handle partially initialised addrinfo. | |
550 | // such as those where data only comes from getsockopt() | |
26ac0430 | 551 | if (s.ai_addr != NULL) { |
26ac0430 | 552 | if (s.ai_addrlen == sizeof(struct sockaddr_in6)) { |
41d93087 | 553 | operator=(*((struct sockaddr_in6*)s.ai_addr)); |
554 | return true; | |
055421ee AJ |
555 | } else if (s.ai_addrlen == sizeof(struct sockaddr_in)) { |
556 | operator=(*((struct sockaddr_in*)s.ai_addr)); | |
557 | return true; | |
558 | } | |
41d93087 | 559 | } |
560 | return false; | |
561 | } | |
562 | ||
563 | return true; | |
564 | } | |
565 | ||
b7ac5457 | 566 | void |
4dd643d5 | 567 | Ip::Address::getAddrInfo(struct addrinfo *&dst, int force) const |
41d93087 | 568 | { |
26ac0430 | 569 | if (dst == NULL) { |
41d93087 | 570 | dst = new addrinfo; |
571 | } | |
572 | ||
573 | memset(dst, 0, sizeof(struct addrinfo)); | |
574 | ||
575 | // set defaults | |
3db830d3 AJ |
576 | // Mac OS X does not emit a flag indicating the output is numeric (IP address) |
577 | #if _SQUID_APPLE_ | |
578 | dst->ai_flags = 0; | |
579 | #else | |
41d93087 | 580 | dst->ai_flags = AI_NUMERICHOST; |
3db830d3 | 581 | #endif |
41d93087 | 582 | |
26ac0430 | 583 | if (dst->ai_socktype == 0) |
41d93087 | 584 | dst->ai_socktype = SOCK_STREAM; |
585 | ||
26ac0430 | 586 | if (dst->ai_socktype == SOCK_STREAM // implies TCP |
41d93087 | 587 | && dst->ai_protocol == 0) |
588 | dst->ai_protocol = IPPROTO_TCP; | |
589 | ||
26ac0430 | 590 | if (dst->ai_socktype == SOCK_DGRAM // implies UDP |
41d93087 | 591 | && dst->ai_protocol == 0) |
592 | dst->ai_protocol = IPPROTO_UDP; | |
593 | ||
4dd643d5 | 594 | if (force == AF_INET6 || (force == AF_UNSPEC && Ip::EnableIpv6 && isIPv6()) ) { |
41d93087 | 595 | dst->ai_addr = (struct sockaddr*)new sockaddr_in6; |
596 | ||
597 | memset(dst->ai_addr,0,sizeof(struct sockaddr_in6)); | |
598 | ||
4dd643d5 | 599 | getSockAddr(*((struct sockaddr_in6*)dst->ai_addr)); |
41d93087 | 600 | |
601 | dst->ai_addrlen = sizeof(struct sockaddr_in6); | |
602 | ||
603 | dst->ai_family = ((struct sockaddr_in6*)dst->ai_addr)->sin6_family; | |
9d92af86 AJ |
604 | |
605 | #if 0 | |
606 | /** | |
607 | * Enable only if you must and please report to squid-dev if you find a need for this. | |
608 | * | |
609 | * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY). | |
610 | * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx | |
611 | * Linux appears to only do some things when its present. | |
612 | * (93) Bad Protocol | |
613 | * FreeBSD dies horribly when using dual-stack with it set. | |
614 | * (43) Protocol not supported | |
615 | */ | |
41d93087 | 616 | dst->ai_protocol = IPPROTO_IPV6; |
9d92af86 AJ |
617 | #endif |
618 | ||
4dd643d5 | 619 | } else if ( force == AF_INET || (force == AF_UNSPEC && isIPv4()) ) { |
41d93087 | 620 | |
055421ee | 621 | dst->ai_addr = (struct sockaddr*)new sockaddr_in; |
41d93087 | 622 | |
055421ee | 623 | memset(dst->ai_addr,0,sizeof(struct sockaddr_in)); |
41d93087 | 624 | |
4dd643d5 | 625 | getSockAddr(*((struct sockaddr_in*)dst->ai_addr)); |
41d93087 | 626 | |
055421ee | 627 | dst->ai_addrlen = sizeof(struct sockaddr_in); |
41d93087 | 628 | |
055421ee AJ |
629 | dst->ai_family = ((struct sockaddr_in*)dst->ai_addr)->sin_family; |
630 | } else { | |
631 | IASSERT("false",false); | |
632 | } | |
41d93087 | 633 | } |
634 | ||
b7ac5457 AJ |
635 | void |
636 | Ip::Address::InitAddrInfo(struct addrinfo *&ai) | |
41d93087 | 637 | { |
26ac0430 | 638 | if (ai == NULL) { |
41d93087 | 639 | ai = new addrinfo; |
640 | memset(ai,0,sizeof(struct addrinfo)); | |
641 | } | |
642 | ||
643 | // remove any existing data. | |
26ac0430 | 644 | if (ai->ai_addr) delete ai->ai_addr; |
41d93087 | 645 | |
646 | ai->ai_addr = (struct sockaddr*)new sockaddr_in6; | |
647 | memset(ai->ai_addr, 0, sizeof(struct sockaddr_in6)); | |
648 | ||
649 | ai->ai_addrlen = sizeof(struct sockaddr_in6); | |
650 | ||
651 | } | |
652 | ||
b7ac5457 AJ |
653 | void |
654 | Ip::Address::FreeAddrInfo(struct addrinfo *&ai) | |
41d93087 | 655 | { |
26ac0430 | 656 | if (ai == NULL) return; |
41d93087 | 657 | |
26ac0430 | 658 | if (ai->ai_addr) delete ai->ai_addr; |
41d93087 | 659 | |
660 | ai->ai_addr = NULL; | |
661 | ||
662 | ai->ai_addrlen = 0; | |
663 | ||
664 | // NP: name fields are NOT allocated at present. | |
665 | delete ai; | |
666 | ||
667 | ai = NULL; | |
668 | } | |
669 | ||
b7ac5457 AJ |
670 | int |
671 | Ip::Address::matchIPAddr(const Ip::Address &rhs) const | |
41d93087 | 672 | { |
4dd643d5 AJ |
673 | uint8_t *l = (uint8_t*)mSocketAddr_.sin6_addr.s6_addr; |
674 | uint8_t *r = (uint8_t*)rhs.mSocketAddr_.sin6_addr.s6_addr; | |
41d93087 | 675 | |
676 | // loop a byte-wise compare | |
677 | // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR | |
678 | // expected difference on CIDR is gt/eq or lt/eq ONLY. | |
4dd643d5 | 679 | for (unsigned int i = 0 ; i < sizeof(mSocketAddr_.sin6_addr) ; ++i) { |
41d93087 | 680 | |
26ac0430 | 681 | if (l[i] < r[i]) |
41d93087 | 682 | return -1; |
683 | ||
26ac0430 | 684 | if (l[i] > r[i]) |
41d93087 | 685 | return 1; |
686 | } | |
687 | ||
688 | return 0; | |
689 | } | |
690 | ||
a67d2b2e AR |
691 | int |
692 | Ip::Address::compareWhole(const Ip::Address &rhs) const | |
607a7bd4 AR |
693 | { |
694 | return memcmp(this, &rhs, sizeof(*this)); | |
695 | } | |
696 | ||
b7ac5457 AJ |
697 | bool |
698 | Ip::Address::operator ==(const Ip::Address &s) const | |
41d93087 | 699 | { |
700 | return (0 == matchIPAddr(s)); | |
701 | } | |
702 | ||
b7ac5457 AJ |
703 | bool |
704 | Ip::Address::operator !=(const Ip::Address &s) const | |
41d93087 | 705 | { |
706 | return ! ( operator==(s) ); | |
707 | } | |
708 | ||
b7ac5457 AJ |
709 | bool |
710 | Ip::Address::operator <=(const Ip::Address &rhs) const | |
41d93087 | 711 | { |
4dd643d5 | 712 | if (isAnyAddr() && !rhs.isAnyAddr()) |
41d93087 | 713 | return true; |
714 | ||
715 | return (matchIPAddr(rhs) <= 0); | |
716 | } | |
717 | ||
b7ac5457 AJ |
718 | bool |
719 | Ip::Address::operator >=(const Ip::Address &rhs) const | |
41d93087 | 720 | { |
4dd643d5 | 721 | if (isNoAddr() && !rhs.isNoAddr()) |
41d93087 | 722 | return true; |
723 | ||
724 | return ( matchIPAddr(rhs) >= 0); | |
725 | } | |
726 | ||
b7ac5457 AJ |
727 | bool |
728 | Ip::Address::operator >(const Ip::Address &rhs) const | |
41d93087 | 729 | { |
4dd643d5 | 730 | if (isNoAddr() && !rhs.isNoAddr()) |
41d93087 | 731 | return true; |
732 | ||
733 | return ( matchIPAddr(rhs) > 0); | |
734 | } | |
735 | ||
b7ac5457 AJ |
736 | bool |
737 | Ip::Address::operator <(const Ip::Address &rhs) const | |
41d93087 | 738 | { |
4dd643d5 | 739 | if (isAnyAddr() && !rhs.isAnyAddr()) |
41d93087 | 740 | return true; |
741 | ||
742 | return ( matchIPAddr(rhs) < 0); | |
743 | } | |
744 | ||
f45dd259 | 745 | unsigned short |
4dd643d5 | 746 | Ip::Address::port() const |
41d93087 | 747 | { |
4dd643d5 | 748 | return ntohs( mSocketAddr_.sin6_port ); |
41d93087 | 749 | } |
750 | ||
f45dd259 | 751 | unsigned short |
4dd643d5 | 752 | Ip::Address::port(unsigned short prt) |
41d93087 | 753 | { |
4dd643d5 | 754 | mSocketAddr_.sin6_port = htons(prt); |
41d93087 | 755 | |
756 | return prt; | |
757 | } | |
758 | ||
759 | /** | |
4dd643d5 | 760 | * toStr Given a buffer writes a readable ascii version of the IPA and/or port stored |
41d93087 | 761 | * |
762 | * Buffer must be of a size large enough to hold the converted address. | |
763 | * This size is provided in the form of a global defined variable MAX_IPSTRLEN | |
764 | * Should a buffer shorter be provided the string result will be truncated | |
765 | * at the length of the available buffer. | |
766 | * | |
767 | * A copy of the buffer is also returned for simple immediate display. | |
768 | */ | |
b7ac5457 | 769 | char * |
4dd643d5 | 770 | Ip::Address::toStr(char* buf, const unsigned int blen, int force) const |
41d93087 | 771 | { |
772 | // Ensure we have a buffer. | |
26ac0430 | 773 | if (buf == NULL) { |
41d93087 | 774 | return NULL; |
775 | } | |
776 | ||
777 | /* some external code may have blindly memset a parent. */ | |
778 | /* thats okay, our default is known */ | |
4dd643d5 AJ |
779 | if ( isAnyAddr() ) { |
780 | if (isIPv6()) | |
8ea75675 | 781 | memcpy(buf,"::\0", min(static_cast<unsigned int>(3),blen)); |
4dd643d5 | 782 | else if (isIPv4()) |
8ea75675 | 783 | memcpy(buf,"0.0.0.0\0", min(static_cast<unsigned int>(8),blen)); |
41d93087 | 784 | return buf; |
785 | } | |
786 | ||
787 | memset(buf,0,blen); // clear buffer before write | |
788 | ||
789 | /* Pure-IPv6 CANNOT be displayed in IPv4 format. */ | |
790 | /* However IPv4 CAN. */ | |
4dd643d5 AJ |
791 | if ( force == AF_INET && !isIPv4() ) { |
792 | if ( isIPv6() ) { | |
1dc746da | 793 | memcpy(buf, "{!IPv4}\0", min(static_cast<unsigned int>(8),blen)); |
41d93087 | 794 | } |
795 | return buf; | |
796 | } | |
797 | ||
4dd643d5 | 798 | if ( force == AF_INET6 || (force == AF_UNSPEC && isIPv6()) ) { |
41d93087 | 799 | |
4dd643d5 | 800 | inet_ntop(AF_INET6, &mSocketAddr_.sin6_addr, buf, blen); |
41d93087 | 801 | |
4dd643d5 | 802 | } else if ( force == AF_INET || (force == AF_UNSPEC && isIPv4()) ) { |
41d93087 | 803 | |
804 | struct in_addr tmp; | |
4dd643d5 | 805 | getInAddr(tmp); |
27bc2077 | 806 | inet_ntop(AF_INET, &tmp, buf, blen); |
41d93087 | 807 | } else { |
fa84c01d | 808 | debugs(14, DBG_CRITICAL, "WARNING: Corrupt IP Address details OR required to display in unknown format (" << |
26ac0430 | 809 | force << "). accepted={" << AF_UNSPEC << "," << AF_INET << "," << AF_INET6 << "}"); |
41d93087 | 810 | fprintf(stderr,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ", |
811 | force, AF_UNSPEC, AF_INET, AF_INET6); | |
1dc746da | 812 | memcpy(buf,"dead:beef::\0", min(static_cast<unsigned int>(13),blen)); |
41d93087 | 813 | assert(false); |
814 | } | |
815 | ||
816 | return buf; | |
817 | } | |
818 | ||
b7ac5457 | 819 | unsigned int |
4dd643d5 | 820 | Ip::Address::toHostStr(char *buf, const unsigned int blen) const |
e1381638 | 821 | { |
41d93087 | 822 | char *p = buf; |
823 | ||
4dd643d5 | 824 | if (isIPv6() && blen > 0) { |
41d93087 | 825 | *p = '['; |
d7ae3534 | 826 | ++p; |
41d93087 | 827 | } |
828 | ||
188a640f | 829 | /* 8 being space for [ ] : and port digits */ |
4dd643d5 AJ |
830 | if ( isIPv6() ) |
831 | toStr(p, blen-8, AF_INET6); | |
41d93087 | 832 | else |
4dd643d5 | 833 | toStr(p, blen-8, AF_INET); |
41d93087 | 834 | |
835 | // find the end of the new string | |
26ac0430 | 836 | while (*p != '\0' && p < buf+blen) |
d7ae3534 | 837 | ++p; |
41d93087 | 838 | |
4dd643d5 | 839 | if (isIPv6() && p < (buf+blen-1) ) { |
41d93087 | 840 | *p = ']'; |
d7ae3534 | 841 | ++p; |
41d93087 | 842 | } |
843 | ||
844 | /* terminate just in case. */ | |
845 | *p = '\0'; | |
846 | ||
847 | /* return size of buffer now used */ | |
848 | return (p - buf); | |
849 | } | |
850 | ||
b7ac5457 | 851 | char * |
4dd643d5 | 852 | Ip::Address::toUrl(char* buf, unsigned int blen) const |
e1381638 | 853 | { |
41d93087 | 854 | char *p = buf; |
855 | ||
856 | // Ensure we have a buffer. | |
857 | ||
26ac0430 | 858 | if (buf == NULL) { |
41d93087 | 859 | return NULL; |
860 | } | |
861 | ||
4dd643d5 | 862 | p += toHostStr(p, blen); |
41d93087 | 863 | |
4dd643d5 | 864 | if (mSocketAddr_.sin6_port > 0 && p <= (buf+blen-7) ) { |
842fe4cc | 865 | // ':port' (short int) needs at most 6 bytes plus 1 for 0-terminator |
4dd643d5 | 866 | snprintf(p, 7, ":%d", port() ); |
41d93087 | 867 | } |
868 | ||
869 | // force a null-terminated string | |
303bfd76 | 870 | buf[blen-1] = '\0'; |
41d93087 | 871 | |
872 | return buf; | |
873 | } | |
874 | ||
b7ac5457 | 875 | void |
4dd643d5 | 876 | Ip::Address::getSockAddr(struct sockaddr_storage &addr, const int family) const |
e1381638 | 877 | { |
52b694c2 AJ |
878 | struct sockaddr_in *sin = NULL; |
879 | ||
4dd643d5 | 880 | if ( family == AF_INET && !isIPv4()) { |
0e1a47f0 | 881 | // FIXME INET6: caller using the wrong socket type! |
4dd643d5 | 882 | debugs(14, DBG_CRITICAL, HERE << "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this); |
0e1a47f0 AJ |
883 | assert(false); |
884 | } | |
885 | ||
4dd643d5 | 886 | if ( family == AF_INET6 || (family == AF_UNSPEC && isIPv6()) ) { |
1ef0b9ce | 887 | struct sockaddr_in6 *ss6 = (struct sockaddr_in6*)&addr; |
4dd643d5 AJ |
888 | getSockAddr(*ss6); |
889 | } else if ( family == AF_INET || (family == AF_UNSPEC && isIPv4()) ) { | |
52b694c2 | 890 | sin = (struct sockaddr_in*)&addr; |
4dd643d5 | 891 | getSockAddr(*sin); |
52b694c2 AJ |
892 | } else { |
893 | IASSERT("false",false); | |
0e1a47f0 | 894 | } |
0e1a47f0 AJ |
895 | } |
896 | ||
b7ac5457 | 897 | void |
4dd643d5 | 898 | Ip::Address::getSockAddr(struct sockaddr_in &buf) const |
e1381638 | 899 | { |
4dd643d5 | 900 | if ( isIPv4() ) { |
41d93087 | 901 | buf.sin_family = AF_INET; |
4dd643d5 AJ |
902 | buf.sin_port = mSocketAddr_.sin6_port; |
903 | map6to4( mSocketAddr_.sin6_addr, buf.sin_addr); | |
26ac0430 | 904 | } else { |
4dd643d5 | 905 | debugs(14, DBG_CRITICAL, HERE << "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this ); |
41d93087 | 906 | |
907 | memset(&buf,0xFFFFFFFF,sizeof(struct sockaddr_in)); | |
908 | assert(false); | |
909 | } | |
910 | ||
12f45551 AJ |
911 | #if HAVE_SIN_LEN_IN_SAI |
912 | /* not all OS have this field, BUT when they do it can be a problem if set wrong */ | |
1ef0b9ce | 913 | buf.sin_len = sizeof(struct sockaddr_in); |
12f45551 | 914 | #endif |
41d93087 | 915 | } |
916 | ||
b7ac5457 | 917 | void |
4dd643d5 | 918 | Ip::Address::getSockAddr(struct sockaddr_in6 &buf) const |
e1381638 | 919 | { |
4dd643d5 | 920 | memcpy(&buf, &mSocketAddr_, sizeof(struct sockaddr_in6)); |
41d93087 | 921 | /* maintain address family. It may have changed inside us. */ |
922 | buf.sin6_family = AF_INET6; | |
12f45551 AJ |
923 | |
924 | #if HAVE_SIN6_LEN_IN_SAI | |
925 | /* not all OS have this field, BUT when they do it can be a problem if set wrong */ | |
1ef0b9ce | 926 | buf.sin6_len = sizeof(struct sockaddr_in6); |
12f45551 | 927 | #endif |
41d93087 | 928 | } |
41d93087 | 929 | |
b7ac5457 | 930 | void |
4dd643d5 | 931 | Ip::Address::map4to6(const struct in_addr &in, struct in6_addr &out) const |
e1381638 | 932 | { |
41d93087 | 933 | /* check for special cases */ |
934 | ||
26ac0430 | 935 | if ( in.s_addr == 0x00000000) { |
41d93087 | 936 | /* ANYADDR */ |
0eb08770 | 937 | out = v4_anyaddr; |
26ac0430 | 938 | } else if ( in.s_addr == 0xFFFFFFFF) { |
41d93087 | 939 | /* NOADDR */ |
0eb08770 | 940 | out = v4_noaddr; |
26ac0430 | 941 | } else { |
41d93087 | 942 | /* general */ |
0eb08770 | 943 | out = v4_anyaddr; |
2c8fff41 AJ |
944 | out.s6_addr[12] = ((uint8_t *)&in.s_addr)[0]; |
945 | out.s6_addr[13] = ((uint8_t *)&in.s_addr)[1]; | |
946 | out.s6_addr[14] = ((uint8_t *)&in.s_addr)[2]; | |
947 | out.s6_addr[15] = ((uint8_t *)&in.s_addr)[3]; | |
41d93087 | 948 | } |
949 | } | |
950 | ||
b7ac5457 | 951 | void |
4dd643d5 | 952 | Ip::Address::map6to4(const struct in6_addr &in, struct in_addr &out) const |
e1381638 | 953 | { |
41d93087 | 954 | /* ANYADDR */ |
955 | /* NOADDR */ | |
956 | /* general */ | |
957 | ||
958 | memset(&out, 0, sizeof(struct in_addr)); | |
2c8fff41 AJ |
959 | ((uint8_t *)&out.s_addr)[0] = in.s6_addr[12]; |
960 | ((uint8_t *)&out.s_addr)[1] = in.s6_addr[13]; | |
961 | ((uint8_t *)&out.s_addr)[2] = in.s6_addr[14]; | |
962 | ((uint8_t *)&out.s_addr)[3] = in.s6_addr[15]; | |
41d93087 | 963 | } |
964 | ||
b7ac5457 | 965 | void |
68fb74b0 | 966 | Ip::Address::getInAddr(struct in6_addr &buf) const |
e1381638 | 967 | { |
4dd643d5 | 968 | memcpy(&buf, &mSocketAddr_.sin6_addr, sizeof(struct in6_addr)); |
41d93087 | 969 | } |
41d93087 | 970 | |
b7ac5457 | 971 | bool |
4dd643d5 | 972 | Ip::Address::getInAddr(struct in_addr &buf) const |
e1381638 | 973 | { |
4dd643d5 | 974 | if ( isIPv4() ) { |
68fb74b0 | 975 | map6to4(mSocketAddr_.sin6_addr, buf); |
41d93087 | 976 | return true; |
977 | } | |
41d93087 | 978 | |
979 | // default: | |
980 | // non-compatible IPv6 Pure Address | |
981 | ||
4dd643d5 | 982 | debugs(14, DBG_IMPORTANT, HERE << "Ip::Address::getInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this); |
41d93087 | 983 | memset(&buf,0xFFFFFFFF,sizeof(struct in_addr)); |
984 | assert(false); | |
985 | return false; | |
986 | } |