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