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