]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ip/IpAddress.cc
SourceFormat: enforcement
[thirdparty/squid.git] / src / ip / IpAddress.cc
1 /*
2 * DEBUG: section 14 IP Storage and Handling
3 * AUTHOR: Amos Jeffries
4 *
5 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from the
9 * Internet community. Development is led by Duane Wessels of the
10 * National Laboratory for Applied Network Research and funded by the
11 * National Science Foundation. Squid is Copyrighted (C) 1998 by
12 * the Regents of the University of California. Please see the
13 * COPYRIGHT file for full details. Squid incorporates software
14 * developed and/or copyrighted by other sources. Please see the
15 * CREDITS file for full details.
16 *
17 * This IpAddress code is copyright (C) 2007 by Treehouse Networks Ltd
18 * of New Zealand. It is published and Lisenced as an extension of
19 * squid under the same conditions as the main squid application.
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 *
35 */
36
37 #include "config.h"
38 #include "ip/IpAddress.h"
39 #include "util.h"
40
41
42 #if HAVE_ASSERT_H
43 #include <assert.h>
44 #endif
45 #if HAVE_STDLIB_H
46 #include <stdlib.h>
47 #endif
48 #if HAVE_STRING_H
49 #include <string.h>
50 #endif
51 #if HAVE_ARPA_INET_H
52 #include <arpa/inet.h> /* inet_ntoa() */
53 #endif
54
55 #ifdef INET6
56 #error "INET6 defined but has been deprecated! Try running bootstrap and configure again."
57 #endif
58
59 /* We want to use the debug routines when running as module of squid. */
60 /* otherwise fallback to printf if those are not available. */
61 #ifndef SQUID_DEBUG
62 # define debugs(a,b,c) // drop.
63 #else
64 #warning "IpAddress built with Debugs!!"
65 # include "../src/Debug.h"
66 #endif
67
68 #if !USE_IPV6
69 // So there are some places where I will drop to using Macros too.
70 // At least I can restrict them to this file so they don't corrupt the app with C code.
71 # define sin6_addr sin_addr
72 # define sin6_port sin_port
73 # define sin6_family sin_family
74 #undef s6_addr
75 # define s6_addr s_addr
76 #endif
77
78 static const unsigned int STRLEN_IP4A = 16; // aaa.bbb.ccc.ddd\0
79 static const unsigned int STRLEN_IP4R = 28; // ddd.ccc.bbb.aaa.in-addr.arpa.\0
80 static const unsigned int STRLEN_IP4S = 21; // ddd.ccc.bbb.aaa:ppppp\0
81 static const unsigned int MAX_IP4_STRLEN = STRLEN_IP4R;
82 static const unsigned int STRLEN_IP6A = 42; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]/0
83 static const unsigned int STRLEN_IP6R = 75; // f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f ipv6.arpa./0
84 static const unsigned int STRLEN_IP6S = 48; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:00000/0
85 static const unsigned int MAX_IP6_STRLEN = STRLEN_IP6R;
86
87
88 /* Debugging only. Dump the address content when a fatal assert is encountered. */
89 #if USE_IPV6
90 #define IASSERT(a,b) \
91 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
92 printf("IpAddress invalid? with IsIPv4()=%c, IsIPv6()=%c\n",(IsIPv4()?'T':'F'),(IsIPv6()?'T':'F')); \
93 printf("ADDRESS:"); \
94 for(unsigned int i = 0; i < sizeof(m_SocketAddr.sin6_addr); i++) { \
95 printf(" %x", m_SocketAddr.sin6_addr.s6_addr[i]); \
96 } printf("\n"); assert(b); \
97 }
98 #else
99 #define IASSERT(a,b) \
100 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
101 printf("IpAddress invalid? with IsIPv4()=%c, IsIPv6()=%c\n",(IsIPv4()?'T':'F'),(IsIPv6()?'T':'F')); \
102 printf("ADDRESS: %x\n", (unsigned int)m_SocketAddr.sin_addr.s_addr); \
103 assert(b); \
104 }
105 #endif
106
107 IpAddress::IpAddress()
108 {
109 SetEmpty();
110 }
111
112 IpAddress::~IpAddress()
113 {
114 memset(this,0,sizeof(IpAddress));
115 }
116
117 int
118 IpAddress::GetCIDR() const
119 {
120 uint8_t shift,byte;
121 uint8_t bit,caught;
122 int len = 0;
123 #if USE_IPV6
124 const uint8_t *ptr= m_SocketAddr.sin6_addr.s6_addr;
125 #else
126 const uint8_t *ptr= (uint8_t *)&m_SocketAddr.sin_addr.s_addr;
127 #endif
128
129 /* Let's scan all the bits from Most Significant to Least */
130 /* Until we find an "0" bit. Then, we return */
131 shift=0;
132
133 #if USE_IPV6
134 /* return IPv4 CIDR for any Mapped address */
135 /* Thus only check the mapped bit */
136
137 if ( !IsIPv6() ) {
138 shift = 12;
139 }
140
141 #endif
142
143 for (; shift<sizeof(m_SocketAddr.sin6_addr) ; shift++) {
144 byte= *(ptr+shift);
145
146 if (byte == 0xFF) {
147 len += 8;
148 continue ; /* A short-cut */
149 }
150
151 for (caught = 0 , bit= 7 ; !caught && (bit <= 7); bit--) {
152 caught = ((byte & 0x80) == 0x00); /* Found a '0' at 'bit' ? */
153
154 if (!caught)
155 len++;
156
157 byte <<= 1;
158 }
159
160 if (caught)
161 break; /* We have found the most significant "0" bit. */
162 }
163
164 return len;
165 }
166
167 const int IpAddress::ApplyMask(IpAddress const &mask_addr)
168 {
169 uint32_t *p1 = (uint32_t*)(&m_SocketAddr.sin6_addr);
170 uint32_t const *p2 = (uint32_t const *)(&mask_addr.m_SocketAddr.sin6_addr);
171 unsigned int blen = sizeof(m_SocketAddr.sin6_addr)/sizeof(uint32_t);
172 unsigned int changes = 0;
173
174 for (unsigned int i = 0; i < blen; i++) {
175 if ((p1[i] & p2[i]) != p1[i])
176 changes++;
177
178 p1[i] &= p2[i];
179 }
180
181 /* we have found a situation where mask forms or destroys a IPv4 map. */
182 check4Mapped();
183
184 return changes;
185 }
186
187 bool IpAddress::ApplyMask(const unsigned int cidr, int mtype)
188 {
189 uint8_t clearbits = 0;
190 uint8_t* p = NULL;
191
192 #if !USE_IPV6
193 IASSERT("mtype != AF_INET6", mtype != AF_INET6); /* using IPv6 in IPv4 is invalid. */
194
195 if (mtype == AF_UNSPEC)
196 mtype = AF_INET;
197
198 #else
199 if (mtype == AF_UNSPEC)
200 mtype = AF_INET6;
201
202 #endif
203
204 // validation and short-cuts.
205 if (cidr > 128)
206 return false;
207
208 if (cidr > 32 && mtype == AF_INET)
209 return false;
210
211 if (cidr == 0) {
212 /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */
213 SetNoAddr();
214 return true;
215 }
216
217 clearbits = (uint8_t)( (mtype==AF_INET6?128:32) -cidr);
218
219 // short-cut
220 if (clearbits == 0)
221 return true;
222
223 #if USE_IPV6
224
225 p = (uint8_t*)(&m_SocketAddr.sin6_addr) + 15;
226
227 #else
228
229 p = (uint8_t*)(&m_SocketAddr.sin_addr) + 3;
230
231 #endif
232
233 for (; clearbits>0 && p >= (uint8_t*)&m_SocketAddr.sin6_addr ; p-- ) {
234 if (clearbits < 8) {
235 *p &= ((0xFF << clearbits) & 0xFF);
236 clearbits = 0;
237 } else {
238 *p &= 0x00;
239 clearbits -= 8;
240 }
241 }
242
243 return true;
244 }
245
246 bool IpAddress::IsSockAddr() const
247 {
248 return (m_SocketAddr.sin6_port != 0);
249 }
250
251 bool IpAddress::IsIPv4() const
252 {
253 #if USE_IPV6
254
255 return IsAnyAddr() || IsNoAddr() ||
256 ( m_SocketAddr.sin6_addr.s6_addr32[0] == htonl(0x00000000) &&
257 m_SocketAddr.sin6_addr.s6_addr32[1] == htonl(0x00000000) &&
258 m_SocketAddr.sin6_addr.s6_addr32[2] == htonl(0x0000FFFF)
259 );
260
261 #else
262 return true; // enforce IPv4 in IPv4-only mode.
263 #endif
264 }
265
266 bool IpAddress::IsIPv6() const
267 {
268 #if USE_IPV6
269
270 return IsAnyAddr() || IsNoAddr() ||
271 !( m_SocketAddr.sin6_addr.s6_addr32[0] == htonl(0x00000000) &&
272 m_SocketAddr.sin6_addr.s6_addr32[1] == htonl(0x00000000) &&
273 m_SocketAddr.sin6_addr.s6_addr32[2] == htonl(0x0000FFFF)
274 );
275 #else
276 return false; // enforce IPv4 in IPv4-only mode.
277 #endif
278 }
279
280 bool IpAddress::IsAnyAddr() const
281 {
282 #if USE_IPV6
283 return m_SocketAddr.sin6_addr.s6_addr32[0] == 0
284 && m_SocketAddr.sin6_addr.s6_addr32[1] == 0
285 && m_SocketAddr.sin6_addr.s6_addr32[2] == 0
286 && m_SocketAddr.sin6_addr.s6_addr32[3] == 0
287 ;
288 #else
289
290 return (INADDR_ANY == m_SocketAddr.sin_addr.s_addr);
291 #endif
292 }
293
294 /// NOTE: Does NOT clear the Port stored. Ony the Address and Type.
295 void IpAddress::SetAnyAddr()
296 {
297 #if USE_IPV6
298 memset(&m_SocketAddr.sin6_addr, 0, sizeof(struct in6_addr) );
299 #else
300 memset(&m_SocketAddr.sin_addr, 0, sizeof(struct in_addr) );
301 #endif
302 }
303
304 /// NOTE: completely empties the IpAddress structure. Address, Port, Type, everything.
305 void IpAddress::SetEmpty()
306 {
307 memset(&m_SocketAddr, 0, sizeof(m_SocketAddr) );
308 }
309
310 bool IpAddress::SetIPv4()
311 {
312 #if USE_IPV6
313
314 if ( IsLocalhost() ) {
315 m_SocketAddr.sin6_addr.s6_addr32[2] = htonl(0xffff);
316 m_SocketAddr.sin6_addr.s6_addr32[3] = htonl(0x7F000001);
317 return true;
318 }
319
320 if ( IsAnyAddr() ) {
321 m_SocketAddr.sin6_addr.s6_addr32[2] = htonl(0xffff);
322 return true;
323 }
324
325 if ( IsIPv4())
326 return true;
327
328 // anything non-IPv4 and non-convertable is BAD.
329 return false;
330 #else
331 return true; // Always IPv4 in IPv4-only builds.
332 #endif
333 }
334
335 bool IpAddress::IsLocalhost() const
336 {
337 #if USE_IPV6
338 return ( m_SocketAddr.sin6_addr.s6_addr32[0] == 0
339 && m_SocketAddr.sin6_addr.s6_addr32[1] == 0
340 && m_SocketAddr.sin6_addr.s6_addr32[2] == 0
341 && m_SocketAddr.sin6_addr.s6_addr32[3] == htonl(0x1)
342 )
343 ||
344 ( m_SocketAddr.sin6_addr.s6_addr32[0] == 0
345 && m_SocketAddr.sin6_addr.s6_addr32[1] == 0
346 && m_SocketAddr.sin6_addr.s6_addr32[2] == htonl(0xffff)
347 && m_SocketAddr.sin6_addr.s6_addr32[3] == htonl(0x7F000001)
348 );
349 #else
350
351 return (htonl(0x7F000001) == m_SocketAddr.sin_addr.s_addr);
352 #endif
353 }
354
355 void IpAddress::SetLocalhost()
356 {
357 #if USE_IPV6
358 SetAnyAddr();
359 m_SocketAddr.sin6_addr.s6_addr[15] = 0x1;
360 m_SocketAddr.sin6_family = AF_INET6;
361
362 #else
363 m_SocketAddr.sin_addr.s_addr = htonl(0x7F000001);
364 m_SocketAddr.sin_family = AF_INET;
365 #endif
366 }
367
368 bool IpAddress::IsNoAddr() const
369 {
370 // IFF the address == 0xff..ff (all ones)
371 #if USE_IPV6
372 return m_SocketAddr.sin6_addr.s6_addr32[0] == 0xFFFFFFFF
373 && m_SocketAddr.sin6_addr.s6_addr32[1] == 0xFFFFFFFF
374 && m_SocketAddr.sin6_addr.s6_addr32[2] == 0xFFFFFFFF
375 && m_SocketAddr.sin6_addr.s6_addr32[3] == 0xFFFFFFFF
376 ;
377 #else
378
379 return 0xFFFFFFFF == m_SocketAddr.sin_addr.s_addr;
380 #endif
381 }
382
383 void IpAddress::SetNoAddr()
384 {
385 #if USE_IPV6
386 memset(&m_SocketAddr.sin6_addr, 0xFFFFFFFF, sizeof(struct in6_addr) );
387 m_SocketAddr.sin6_family = AF_INET6;
388 #else
389 memset(&m_SocketAddr.sin_addr, 0xFFFFFFFF, sizeof(struct in_addr) );
390 m_SocketAddr.sin_family = AF_INET;
391 #endif
392 }
393
394 #if USE_IPV6
395
396 bool IpAddress::GetReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const
397 {
398 char *p = buf;
399 unsigned char const *r = dat.s6_addr;
400
401 /* RFC1886 says: */
402 /* 4321:0:1:2:3:4:567:89ab */
403 /* must be sent */
404 /* 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. */
405
406 /* Work from the binary field. Anything else may have representation changes. */
407 /* The sin6_port and sin6_addr members shall be in network byte order. */
408
409 /* Compile Err: 'Too many arguments for format. */
410
411 for (int i = 15; i >= 0; i--, p+=4) {
412 snprintf(p, 5, "%x.%x.", ((r[i])&0xf), (((r[i])>>4)&0xf) );
413 }
414
415 /* RFC3152 says: */
416 /* ip6.int is now deprecated TLD, use ip6.arpa instead. */
417 snprintf(p,10,"ip6.arpa.");
418
419 return true;
420 }
421
422 #endif
423
424 bool IpAddress::GetReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const
425 {
426 unsigned int i = (unsigned int) ntohl(dat.s_addr);
427 snprintf(buf, 32, "%u.%u.%u.%u.in-addr.arpa.",
428 i & 255,
429 (i >> 8) & 255,
430 (i >> 16) & 255,
431 (i >> 24) & 255);
432 return true;
433 }
434
435 bool IpAddress::GetReverseString(char buf[MAX_IPSTRLEN], int show_type) const
436 {
437
438 if (show_type == AF_UNSPEC) {
439 #if USE_IPV6
440 show_type = IsIPv6() ? AF_INET6 : AF_INET ;
441 #else
442 show_type = AF_INET;
443 #endif
444 }
445
446 if (show_type == AF_INET && IsIPv4()) {
447 #if USE_IPV6
448
449 return GetReverseString4(buf, *(struct in_addr*)&m_SocketAddr.sin6_addr.s6_addr32[3] );
450 } else if ( show_type == AF_INET6 && IsIPv6() ) {
451 return GetReverseString6(buf, m_SocketAddr.sin6_addr);
452 #else
453 return GetReverseString4(buf, m_SocketAddr.sin_addr);
454 #endif
455 }
456
457 debugs(14,0, "Unable to convert '" << NtoA(buf,MAX_IPSTRLEN) << "' to the rDNS type requested.");
458
459 buf[0] = '\0';
460
461 return false;
462 }
463
464 IpAddress& IpAddress::operator =(const IpAddress &s)
465 {
466 memcpy(this, &s, sizeof(IpAddress));
467 return *this;
468 };
469
470 IpAddress::IpAddress(const char*s)
471 {
472 SetEmpty();
473 operator=(s);
474 }
475
476 bool IpAddress::operator =(const char* s)
477 {
478 return LookupHostIP(s, true);
479 }
480
481 bool IpAddress::GetHostByName(const char* s)
482 {
483 return LookupHostIP(s, false);
484 }
485
486 bool IpAddress::LookupHostIP(const char *s, bool nodns)
487 {
488 int err = 0;
489
490 short port = 0;
491
492 struct addrinfo *res = NULL;
493
494 struct addrinfo want;
495
496 memset(&want, 0, sizeof(struct addrinfo));
497 if (nodns) {
498 want.ai_flags = AI_NUMERICHOST; // prevent actual DNS lookups!
499 }
500 #if !USE_IPV6
501 want.ai_family = AF_INET;
502 #endif
503
504 if ( (err = xgetaddrinfo(s, NULL, &want, &res)) != 0) {
505 debugs(14,1, HERE << "Given Bad IP '" << s << "': " << xgai_strerror(err) );
506 /* free the memory xgetaddrinfo() dynamically allocated. */
507 if (res) {
508 xfreeaddrinfo(res);
509 res = NULL;
510 }
511 return false;
512 }
513
514 /*
515 * NP: =(sockaddr_*) may alter the port. we don't want that.
516 * all we have been given as input was an IPA.
517 */
518 port = GetPort();
519 operator=(*res);
520 SetPort(port);
521
522 /* free the memory xgetaddrinfo() dynamically allocated. */
523 xfreeaddrinfo(res);
524
525 res = NULL;
526
527 return true;
528 }
529
530 IpAddress::IpAddress(struct sockaddr_in const &s)
531 {
532 SetEmpty();
533 operator=(s);
534 };
535
536 IpAddress& IpAddress::operator =(struct sockaddr_in const &s)
537 {
538 #if USE_IPV6
539 Map4to6((const in_addr)s.sin_addr, m_SocketAddr.sin6_addr);
540 m_SocketAddr.sin6_port = s.sin_port;
541 m_SocketAddr.sin6_family = AF_INET6;
542 #else
543
544 memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in));
545 #endif
546
547 /* maintain stored family values properly */
548 check4Mapped();
549
550 return *this;
551 };
552
553 IpAddress& IpAddress::operator =(const struct sockaddr_storage &s)
554 {
555 #if USE_IPV6
556 /* some AF_* magic to tell socket types apart and what we need to do */
557 if (s.ss_family == AF_INET6) {
558 memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in));
559 } else { // convert it to our storage mapping.
560 struct sockaddr_in *sin = (struct sockaddr_in*)&s;
561 m_SocketAddr.sin6_port = sin->sin_port;
562 Map4to6( sin->sin_addr, m_SocketAddr.sin6_addr);
563 }
564 #else
565 memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in));
566 #endif
567 return *this;
568 };
569
570 void IpAddress::check4Mapped()
571 {
572 // obsolete.
573 // TODO use this NOW to set the sin6_family properly on exporting. not on import.
574 }
575
576 #if USE_IPV6
577 IpAddress::IpAddress(struct sockaddr_in6 const &s)
578 {
579 SetEmpty();
580 operator=(s);
581 };
582
583 IpAddress& IpAddress::operator =(struct sockaddr_in6 const &s)
584 {
585 memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in6));
586
587 /* maintain address family properly */
588 check4Mapped();
589 return *this;
590 };
591
592 #endif
593
594 IpAddress::IpAddress(struct in_addr const &s)
595 {
596 SetEmpty();
597 operator=(s);
598 };
599
600 IpAddress& IpAddress::operator =(struct in_addr const &s)
601 {
602 #if USE_IPV6
603 Map4to6((const in_addr)s, m_SocketAddr.sin6_addr);
604 m_SocketAddr.sin6_family = AF_INET6;
605
606 #else
607
608 memcpy(&m_SocketAddr.sin_addr, &s, sizeof(struct in_addr));
609
610 #endif
611
612 /* maintain stored family type properly */
613 check4Mapped();
614
615 return *this;
616 };
617
618 #if USE_IPV6
619
620 IpAddress::IpAddress(struct in6_addr const &s)
621 {
622 SetEmpty();
623 operator=(s);
624 };
625
626 IpAddress& IpAddress::operator =(struct in6_addr const &s)
627 {
628
629 memcpy(&m_SocketAddr.sin6_addr, &s, sizeof(struct in6_addr));
630 m_SocketAddr.sin6_family = AF_INET6;
631
632 /* maintain address family type properly */
633 check4Mapped();
634
635 return *this;
636 };
637
638 #endif
639
640 IpAddress::IpAddress(const IpAddress &s)
641 {
642 SetEmpty();
643 operator=(s);
644 }
645
646 IpAddress::IpAddress(IpAddress *s)
647 {
648 SetEmpty();
649 if (s)
650 memcpy(this, s, sizeof(IpAddress));
651 }
652
653 IpAddress::IpAddress(const struct hostent &s)
654 {
655 SetEmpty();
656 operator=(s);
657 }
658
659 bool IpAddress::operator =(const struct hostent &s)
660 {
661
662 struct in_addr* ipv4 = NULL;
663
664 struct in6_addr* ipv6 = NULL;
665
666 //struct hostent {
667 // char *h_name; /* official name of host */
668 // char **h_aliases; /* alias list */
669 // int h_addrtype; /* host address type */
670 // int h_length; /* length of address */
671 // char **h_addr_list; /* list of addresses */
672 //}
673
674 switch (s.h_addrtype) {
675
676 case AF_INET:
677 ipv4 = (in_addr*)(s.h_addr_list[0]);
678 /* this */
679 operator=(*ipv4);
680 break;
681
682 case AF_INET6:
683 ipv6 = (in6_addr*)(s.h_addr_list[0]);
684 #if USE_IPV6
685 /* this */
686 operator=(*ipv6);
687 #else
688
689 debugs(14,1, HERE << "Discarded IPv6 Address. Protocol disabled.");
690
691 // FIXME see if there is another address in the list that might be usable ??
692 return false;
693 #endif
694
695 break;
696
697 default:
698 IASSERT("false",false);
699 return false;
700 }
701
702 return true;
703 }
704
705 IpAddress::IpAddress(const struct addrinfo &s)
706 {
707 SetEmpty();
708 operator=(s);
709 }
710
711 bool IpAddress::operator =(const struct addrinfo &s)
712 {
713
714 struct sockaddr_in* ipv4 = NULL;
715
716 struct sockaddr_in6* ipv6 = NULL;
717
718 //struct addrinfo {
719 // int ai_flags; /* input flags */
720 // int ai_family; /* protocol family for socket */
721 // int ai_socktype; /* socket type */
722 // int ai_protocol; /* protocol for socket */
723 // socklen_t ai_addrlen; /* length of socket-address */
724 // struct sockaddr *ai_addr; /* socket-address for socket */
725 // char *ai_canonname; /* canonical name for service location */
726 // struct addrinfo *ai_next; /* pointer to next in list */
727 //}
728
729 switch (s.ai_family) {
730
731 case AF_INET:
732 ipv4 = (sockaddr_in*)(s.ai_addr);
733 /* this */
734 assert(ipv4);
735 operator=(*ipv4);
736 break;
737
738 case AF_INET6:
739 ipv6 = (sockaddr_in6*)(s.ai_addr);
740 #if USE_IPV6
741 /* this */
742 assert(ipv6);
743 operator=(*ipv6);
744 #else
745
746 debugs(14,1, HERE << "Discarded IPv6 Address. Protocol disabled.");
747
748 // see if there is another address in the list that might be usable ??
749
750 if (s.ai_next)
751 return operator=(*s.ai_next);
752 else
753 return false;
754
755 #endif
756 break;
757
758 case AF_UNSPEC:
759 default:
760 // attempt to handle partially initialised addrinfo.
761 // such as those where data only comes from getsockopt()
762 if (s.ai_addr != NULL) {
763 #if USE_IPV6
764 if (s.ai_addrlen == sizeof(struct sockaddr_in6)) {
765 operator=(*((struct sockaddr_in6*)s.ai_addr));
766 return true;
767 } else
768 #endif
769 if (s.ai_addrlen == sizeof(struct sockaddr_in)) {
770 operator=(*((struct sockaddr_in*)s.ai_addr));
771 return true;
772 }
773 }
774 return false;
775 }
776
777 return true;
778 }
779
780 void IpAddress::GetAddrInfo(struct addrinfo *&dst, int force) const
781 {
782 if (dst == NULL) {
783 dst = new addrinfo;
784 }
785
786 memset(dst, 0, sizeof(struct addrinfo));
787
788 // set defaults
789 dst->ai_flags = AI_NUMERICHOST;
790
791 if (dst->ai_socktype == 0)
792 dst->ai_socktype = SOCK_STREAM;
793
794 if (dst->ai_socktype == SOCK_STREAM // implies TCP
795 && dst->ai_protocol == 0)
796 dst->ai_protocol = IPPROTO_TCP;
797
798 if (dst->ai_socktype == SOCK_DGRAM // implies UDP
799 && dst->ai_protocol == 0)
800 dst->ai_protocol = IPPROTO_UDP;
801
802 #if USE_IPV6
803 if ( force == AF_INET6 || (force == AF_UNSPEC && IsIPv6()) ) {
804 dst->ai_addr = (struct sockaddr*)new sockaddr_in6;
805
806 memset(dst->ai_addr,0,sizeof(struct sockaddr_in6));
807
808 GetSockAddr(*((struct sockaddr_in6*)dst->ai_addr));
809
810 dst->ai_addrlen = sizeof(struct sockaddr_in6);
811
812 dst->ai_family = ((struct sockaddr_in6*)dst->ai_addr)->sin6_family;
813
814 #if 0
815 /**
816 * Enable only if you must and please report to squid-dev if you find a need for this.
817 *
818 * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY).
819 * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx
820 * Linux appears to only do some things when its present.
821 * (93) Bad Protocol
822 * FreeBSD dies horribly when using dual-stack with it set.
823 * (43) Protocol not supported
824 */
825 dst->ai_protocol = IPPROTO_IPV6;
826 #endif
827
828 } else
829 #endif
830 if ( force == AF_INET || (force == AF_UNSPEC && IsIPv4()) ) {
831
832 dst->ai_addr = (struct sockaddr*)new sockaddr_in;
833
834 memset(dst->ai_addr,0,sizeof(struct sockaddr_in));
835
836 GetSockAddr(*((struct sockaddr_in*)dst->ai_addr));
837
838 dst->ai_addrlen = sizeof(struct sockaddr_in);
839
840 dst->ai_family = ((struct sockaddr_in*)dst->ai_addr)->sin_family;
841 } else {
842 IASSERT("false",false);
843 }
844 }
845
846 void IpAddress::InitAddrInfo(struct addrinfo *&ai) const
847 {
848 if (ai == NULL) {
849 ai = new addrinfo;
850 memset(ai,0,sizeof(struct addrinfo));
851 }
852
853 // remove any existing data.
854 if (ai->ai_addr) delete ai->ai_addr;
855
856 ai->ai_addr = (struct sockaddr*)new sockaddr_in6;
857 memset(ai->ai_addr, 0, sizeof(struct sockaddr_in6));
858
859 ai->ai_addrlen = sizeof(struct sockaddr_in6);
860
861 }
862
863 void IpAddress::FreeAddrInfo(struct addrinfo *&ai) const
864 {
865 if (ai == NULL) return;
866
867 if (ai->ai_addr) delete ai->ai_addr;
868
869 ai->ai_addr = NULL;
870
871 ai->ai_addrlen = 0;
872
873 // NP: name fields are NOT allocated at present.
874 delete ai;
875
876 ai = NULL;
877 }
878
879 int IpAddress::matchIPAddr(const IpAddress &rhs) const
880 {
881 #if USE_IPV6
882 uint8_t *l = (uint8_t*)m_SocketAddr.sin6_addr.s6_addr;
883 uint8_t *r = (uint8_t*)rhs.m_SocketAddr.sin6_addr.s6_addr;
884 #else
885 uint8_t *l = (uint8_t*)&m_SocketAddr.sin_addr.s_addr;
886 uint8_t *r = (uint8_t*)&rhs.m_SocketAddr.sin_addr.s_addr;
887 #endif
888
889 // loop a byte-wise compare
890 // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR
891 // expected difference on CIDR is gt/eq or lt/eq ONLY.
892 for (unsigned int i = 0 ; i < sizeof(m_SocketAddr.sin6_addr) ; i++) {
893
894 if (l[i] < r[i])
895 return -1;
896
897 if (l[i] > r[i])
898 return 1;
899 }
900
901 return 0;
902 }
903
904 bool IpAddress::operator ==(const IpAddress &s) const
905 {
906 return (0 == matchIPAddr(s));
907 }
908
909 bool IpAddress::operator !=(const IpAddress &s) const
910 {
911 return ! ( operator==(s) );
912 }
913
914 bool IpAddress::operator <=(const IpAddress &rhs) const
915 {
916 if (IsAnyAddr() && !rhs.IsAnyAddr())
917 return true;
918
919 return (matchIPAddr(rhs) <= 0);
920 }
921
922 bool IpAddress::operator >=(const IpAddress &rhs) const
923 {
924 if (IsNoAddr() && !rhs.IsNoAddr())
925 return true;
926
927 return ( matchIPAddr(rhs) >= 0);
928 }
929
930 bool IpAddress::operator >(const IpAddress &rhs) const
931 {
932 if (IsNoAddr() && !rhs.IsNoAddr())
933 return true;
934
935 return ( matchIPAddr(rhs) > 0);
936 }
937
938 bool IpAddress::operator <(const IpAddress &rhs) const
939 {
940 if (IsNoAddr() && !rhs.IsNoAddr())
941 return true;
942
943 return ( matchIPAddr(rhs) < 0);
944 }
945
946 u_short IpAddress::GetPort() const
947 {
948 return ntohs( m_SocketAddr.sin6_port );
949 }
950
951 u_short IpAddress::SetPort(u_short prt)
952 {
953 m_SocketAddr.sin6_port = htons(prt);
954
955 return prt;
956 }
957
958 /**
959 * NtoA Given a buffer writes a readable ascii version of the IPA and/or port stored
960 *
961 * Buffer must be of a size large enough to hold the converted address.
962 * This size is provided in the form of a global defined variable MAX_IPSTRLEN
963 * Should a buffer shorter be provided the string result will be truncated
964 * at the length of the available buffer.
965 *
966 * A copy of the buffer is also returned for simple immediate display.
967 */
968 char* IpAddress::NtoA(char* buf, const unsigned int blen, int force) const
969 {
970 // Ensure we have a buffer.
971 if (buf == NULL) {
972 return NULL;
973 }
974
975 /* some external code may have blindly memset a parent. */
976 /* thats okay, our default is known */
977 if ( IsAnyAddr() ) {
978 #if USE_IPV6
979 memcpy(buf,"::\0", min((const unsigned int)3,blen));
980 #else
981 memcpy(buf,"0.0.0.0\0", min((const unsigned int)8,blen));
982 #endif
983 return buf;
984 }
985
986 memset(buf,0,blen); // clear buffer before write
987
988 /* Pure-IPv6 CANNOT be displayed in IPv4 format. */
989 /* However IPv4 CAN. */
990 if ( force == AF_INET && !IsIPv4() ) {
991 if ( IsIPv6() ) {
992 memcpy(buf, "{!IPv4}\0", min((const unsigned int)8,blen));
993 }
994 return buf;
995 }
996
997 #if USE_IPV6
998 if ( force == AF_INET6 || (force == AF_UNSPEC && IsIPv6()) ) {
999
1000 xinet_ntop(AF_INET6, &m_SocketAddr.sin6_addr, buf, blen);
1001
1002 } else if ( force == AF_INET || (force == AF_UNSPEC && IsIPv4()) ) {
1003
1004 struct in_addr tmp;
1005 GetInAddr(tmp);
1006 xinet_ntop(AF_INET, &tmp, buf, blen);
1007 #else
1008 if ( force == AF_UNSPEC || (force == AF_INET && IsIPv4()) ) {
1009 xinet_ntop(AF_INET, &m_SocketAddr.sin_addr, buf, blen);
1010 #endif
1011 } else {
1012 debugs(14,0,"WARNING: Corrupt IP Address details OR required to display in unknown format (" <<
1013 force << "). accepted={" << AF_UNSPEC << "," << AF_INET << "," << AF_INET6 << "}");
1014 fprintf(stderr,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ",
1015 force, AF_UNSPEC, AF_INET, AF_INET6);
1016 memcpy(buf,"dead:beef::\0", min((const unsigned int)13,blen));
1017 assert(false);
1018 }
1019
1020 return buf;
1021 }
1022
1023 unsigned int IpAddress::ToHostname(char *buf, const unsigned int blen) const
1024 {
1025 char *p = buf;
1026
1027 if (IsIPv6() && blen > 0) {
1028 *p = '[';
1029 p++;
1030 }
1031
1032 /* 7 being space for [,], and port */
1033 if ( IsIPv6() )
1034 NtoA(p, blen-7, AF_INET6);
1035 else
1036 NtoA(p, blen-7, AF_INET);
1037
1038 // find the end of the new string
1039 while (*p != '\0' && p < buf+blen)
1040 p++;
1041
1042 if (IsIPv6() && p < (buf+blen-1) ) {
1043 *p = ']';
1044 p++;
1045 }
1046
1047 /* terminate just in case. */
1048 *p = '\0';
1049
1050 /* return size of buffer now used */
1051 return (p - buf);
1052 }
1053
1054 char* IpAddress::ToURL(char* buf, unsigned int blen) const
1055 {
1056 char *p = buf;
1057
1058 // Ensure we have a buffer.
1059
1060 if (buf == NULL) {
1061 return NULL;
1062 }
1063
1064 p += ToHostname(p, blen);
1065
1066 if (m_SocketAddr.sin6_port > 0 && p < (buf+blen-6) ) {
1067 /* 6 is max length of expected ':port' (short int) */
1068 snprintf(p, 6,":%d", GetPort() );
1069 }
1070
1071 // force a null-terminated string
1072 buf[blen-1] = '\0';
1073
1074 return buf;
1075 }
1076
1077 void IpAddress::GetSockAddr(struct sockaddr_storage &addr, const int family) const
1078 {
1079 struct sockaddr_in *sin = NULL;
1080
1081 if ( family == AF_INET && !IsIPv4()) {
1082 // FIXME INET6: caller using the wrong socket type!
1083 debugs(14, DBG_CRITICAL, HERE << "IpAddress::GetSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this);
1084 assert(false);
1085 }
1086
1087 #if USE_IPV6
1088 if ( family == AF_INET6 || (family == AF_UNSPEC && IsIPv6()) ) {
1089 struct sockaddr_in6 *ss6 = (struct sockaddr_in6*)&addr;
1090 GetSockAddr(*ss6);
1091 } else if ( family == AF_INET || (family == AF_UNSPEC && IsIPv4()) ) {
1092 sin = (struct sockaddr_in*)&addr;
1093 GetSockAddr(*sin);
1094 } else {
1095 IASSERT("false",false);
1096 }
1097 #else /* not USE_IPV6 */
1098 sin = (struct sockaddr_in*)&addr;
1099 GetSockAddr(*sin);
1100 #endif /* USE_IPV6 */
1101 }
1102
1103 void IpAddress::GetSockAddr(struct sockaddr_in &buf) const
1104 {
1105 #if USE_IPV6
1106
1107 if ( IsIPv4() ) {
1108 buf.sin_family = AF_INET;
1109 buf.sin_port = m_SocketAddr.sin6_port;
1110 Map6to4( m_SocketAddr.sin6_addr, buf.sin_addr);
1111 } else {
1112 debugs(14, DBG_CRITICAL, HERE << "IpAddress::GetSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this );
1113
1114 memset(&buf,0xFFFFFFFF,sizeof(struct sockaddr_in));
1115 assert(false);
1116 }
1117
1118 #else
1119
1120 memcpy(&buf, &m_SocketAddr, sizeof(struct sockaddr_in));
1121
1122 if (buf.sin_family == 0) {
1123 buf.sin_family = AF_INET;
1124 }
1125
1126 #endif
1127
1128 #if HAVE_SIN_LEN_IN_SAI
1129 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
1130 buf.sin_len = sizeof(struct sockaddr_in);
1131 #endif
1132
1133 }
1134
1135 #if USE_IPV6
1136
1137 void IpAddress::GetSockAddr(struct sockaddr_in6 &buf) const
1138 {
1139 memcpy(&buf, &m_SocketAddr, sizeof(struct sockaddr_in6));
1140 /* maintain address family. It may have changed inside us. */
1141 buf.sin6_family = AF_INET6;
1142
1143 #if HAVE_SIN6_LEN_IN_SAI
1144 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
1145 buf.sin6_len = sizeof(struct sockaddr_in6);
1146 #endif
1147 }
1148
1149 #endif
1150
1151 #if USE_IPV6
1152
1153 void IpAddress::Map4to6(const struct in_addr &in, struct in6_addr &out) const
1154 {
1155 /* check for special cases */
1156
1157 if ( in.s_addr == 0x00000000) {
1158 /* ANYADDR */
1159
1160 memset(&out, 0, sizeof(struct in6_addr));
1161 } else if ( in.s_addr == 0xFFFFFFFF) {
1162 /* NOADDR */
1163
1164 out.s6_addr32[0] = 0xFFFFFFFF;
1165 out.s6_addr32[1] = 0xFFFFFFFF;
1166 out.s6_addr32[2] = 0xFFFFFFFF;
1167 out.s6_addr32[3] = 0xFFFFFFFF;
1168
1169 } else {
1170 /* general */
1171
1172 memset(&out, 0, sizeof(struct in6_addr));
1173 out.s6_addr32[2] = htonl(0xFFFF);
1174 out.s6_addr32[3] = in.s_addr;
1175 }
1176 }
1177
1178 void IpAddress::Map6to4(const struct in6_addr &in, struct in_addr &out) const
1179 {
1180 /* ANYADDR */
1181 /* NOADDR */
1182 /* general */
1183
1184 memset(&out, 0, sizeof(struct in_addr));
1185 out.s_addr = in.s6_addr32[3];
1186 }
1187
1188 #endif
1189
1190 #if USE_IPV6
1191 void IpAddress::GetInAddr(in6_addr &buf) const
1192 {
1193 memcpy(&buf, &m_SocketAddr.sin6_addr, sizeof(struct in6_addr));
1194 }
1195
1196 #endif
1197
1198 bool IpAddress::GetInAddr(struct in_addr &buf) const
1199 {
1200
1201 #if USE_IPV6
1202 if ( IsIPv4() ) {
1203 Map6to4((const in6_addr)m_SocketAddr.sin6_addr, buf);
1204 return true;
1205 }
1206 #else
1207
1208 if ( IsIPv4() ) {
1209 memcpy(&buf, &m_SocketAddr.sin_addr, sizeof(struct in_addr));
1210 return true;
1211 }
1212 #endif
1213
1214 // default:
1215 // non-compatible IPv6 Pure Address
1216
1217 debugs(14,1, HERE << "IpAddress::GetInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this);
1218 memset(&buf,0xFFFFFFFF,sizeof(struct in_addr));
1219 assert(false);
1220 return false;
1221 }