BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
netaddress.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <netaddress.h>
7 #include <hash.h>
8 #include <utilstrencodings.h>
9 #include <tinyformat.h>
10 
11 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
12 static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};
13 
14 // 0xFD + sha3("bitcoin")[0:5]
15 static const unsigned char g_internal_prefix[] = { 0xFD, 0xF8, 0x2F, 0x54, 0xFE, 0x3A };
16 
18 {
19  memset(ip, 0, sizeof(ip));
20  scopeId = 0;
21 }
22 
23 void CNetAddr::SetIP(const CNetAddr& ipIn)
24 {
25  memcpy(ip, ipIn.ip, sizeof(ip));
26 }
27 
28 void CNetAddr::SetRaw(Network network, const uint8_t *ip_in)
29 {
30  switch(network)
31  {
32  case NET_IPV4:
33  memcpy(ip, pchIPv4, 12);
34  memcpy(ip+12, ip_in, 4);
35  break;
36  case NET_IPV6:
37  memcpy(ip, ip_in, 16);
38  break;
39  default:
40  assert(!"invalid network");
41  }
42 }
43 
44 bool CNetAddr::SetInternal(const std::string &name)
45 {
46  if (name.empty()) {
47  return false;
48  }
49  unsigned char hash[32] = {};
50  CSHA3().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
51  memcpy(ip, g_internal_prefix, sizeof(g_internal_prefix));
52  memcpy(ip + sizeof(g_internal_prefix), hash, sizeof(ip) - sizeof(g_internal_prefix));
53  return true;
54 }
55 
56 bool CNetAddr::SetSpecial(const std::string &strName)
57 {
58  if (strName.size()>6 && strName.substr(strName.size() - 6, 6) == ".onion") {
59  std::vector<unsigned char> vchAddr = DecodeBase32(strName.substr(0, strName.size() - 6).c_str());
60  if (vchAddr.size() != 16-sizeof(pchOnionCat))
61  return false;
62  memcpy(ip, pchOnionCat, sizeof(pchOnionCat));
63  for (unsigned int i=0; i<16-sizeof(pchOnionCat); i++)
64  ip[i + sizeof(pchOnionCat)] = vchAddr[i];
65  return true;
66  }
67  return false;
68 }
69 
70 CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
71 {
72  SetRaw(NET_IPV4, (const uint8_t*)&ipv4Addr);
73 }
74 
75 CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
76 {
77  SetRaw(NET_IPV6, (const uint8_t*)&ipv6Addr);
78  scopeId = scope;
79 }
80 
81 unsigned int CNetAddr::GetByte(int n) const
82 {
83  return ip[15-n];
84 }
85 
86 bool CNetAddr::IsIPv4() const
87 {
88  return (memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0);
89 }
90 
91 bool CNetAddr::IsIPv6() const
92 {
93  return (!IsIPv4() && !IsTor() && !IsInternal());
94 }
95 
96 bool CNetAddr::IsRFC1918() const
97 {
98  return IsIPv4() && (
99  GetByte(3) == 10 ||
100  (GetByte(3) == 192 && GetByte(2) == 168) ||
101  (GetByte(3) == 172 && (GetByte(2) >= 16 && GetByte(2) <= 31)));
102 }
103 
105 {
106  return IsIPv4() && GetByte(3) == 198 && (GetByte(2) == 18 || GetByte(2) == 19);
107 }
108 
110 {
111  return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
112 }
113 
115 {
116  return IsIPv4() && GetByte(3) == 100 && GetByte(2) >= 64 && GetByte(2) <= 127;
117 }
118 
120 {
121  return IsIPv4() && ((GetByte(3) == 192 && GetByte(2) == 0 && GetByte(1) == 2) ||
122  (GetByte(3) == 198 && GetByte(2) == 51 && GetByte(1) == 100) ||
123  (GetByte(3) == 203 && GetByte(2) == 0 && GetByte(1) == 113));
124 }
125 
127 {
128  return GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x0D && GetByte(12) == 0xB8;
129 }
130 
132 {
133  return (GetByte(15) == 0x20 && GetByte(14) == 0x02);
134 }
135 
137 {
138  static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
139  return (memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0);
140 }
141 
143 {
144  return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0 && GetByte(12) == 0);
145 }
146 
148 {
149  static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
150  return (memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0);
151 }
152 
154 {
155  return ((GetByte(15) & 0xFE) == 0xFC);
156 }
157 
159 {
160  static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
161  return (memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0);
162 }
163 
165 {
166  return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 && (GetByte(12) & 0xF0) == 0x10);
167 }
168 
169 bool CNetAddr::IsTor() const
170 {
171  return (memcmp(ip, pchOnionCat, sizeof(pchOnionCat)) == 0);
172 }
173 
174 bool CNetAddr::IsLocal() const
175 {
176  // IPv4 loopback
177  if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0))
178  return true;
179 
180  // IPv6 loopback (::1/128)
181  static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
182  if (memcmp(ip, pchLocal, 16) == 0)
183  return true;
184 
185  return false;
186 }
187 
188 bool CNetAddr::IsValid() const
189 {
190  // Cleanup 3-byte shifted addresses caused by garbage in size field
191  // of addr messages from versions before 0.2.9 checksum.
192  // Two consecutive addr messages look like this:
193  // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
194  // so if the first length field is garbled, it reads the second batch
195  // of addr misaligned by 3 bytes.
196  if (memcmp(ip, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
197  return false;
198 
199  // unspecified IPv6 address (::/128)
200  unsigned char ipNone6[16] = {};
201  if (memcmp(ip, ipNone6, 16) == 0)
202  return false;
203 
204  // documentation IPv6 address
205  if (IsRFC3849())
206  return false;
207 
208  if (IsInternal())
209  return false;
210 
211  if (IsIPv4())
212  {
213  // INADDR_NONE
214  uint32_t ipNone = INADDR_NONE;
215  if (memcmp(ip+12, &ipNone, 4) == 0)
216  return false;
217 
218  // 0
219  ipNone = 0;
220  if (memcmp(ip+12, &ipNone, 4) == 0)
221  return false;
222  }
223 
224  return true;
225 }
226 
228 {
229  return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) || IsRFC4843() || IsLocal() || IsInternal());
230 }
231 
233 {
234  return memcmp(ip, g_internal_prefix, sizeof(g_internal_prefix)) == 0;
235 }
236 
238 {
239  if (IsInternal())
240  return NET_INTERNAL;
241 
242  if (!IsRoutable())
243  return NET_UNROUTABLE;
244 
245  if (IsIPv4())
246  return NET_IPV4;
247 
248  if (IsTor())
249  return NET_ONION;
250 
251  return NET_IPV6;
252 }
253 
254 std::string CNetAddr::ToStringIP() const
255 {
256  if (IsTor())
257  return EncodeBase32(&ip[6], 10) + ".onion";
258  if (IsInternal())
259  return EncodeBase32(ip + sizeof(g_internal_prefix), sizeof(ip) - sizeof(g_internal_prefix)) + ".internal";
260  CService serv(*this, 0);
261  struct sockaddr_storage sockaddr;
262  socklen_t socklen = sizeof(sockaddr);
263  if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
264  char name[1025] = "";
265  if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), nullptr, 0, NI_NUMERICHOST))
266  return std::string(name);
267  }
268  if (IsIPv4())
269  return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
270  else
271  return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
272  GetByte(15) << 8 | GetByte(14), GetByte(13) << 8 | GetByte(12),
273  GetByte(11) << 8 | GetByte(10), GetByte(9) << 8 | GetByte(8),
274  GetByte(7) << 8 | GetByte(6), GetByte(5) << 8 | GetByte(4),
275  GetByte(3) << 8 | GetByte(2), GetByte(1) << 8 | GetByte(0));
276 }
277 
278 std::string CNetAddr::ToString() const
279 {
280  return ToStringIP();
281 }
282 
283 bool operator==(const CNetAddr& a, const CNetAddr& b)
284 {
285  return (memcmp(a.ip, b.ip, 16) == 0);
286 }
287 
288 bool operator<(const CNetAddr& a, const CNetAddr& b)
289 {
290  return (memcmp(a.ip, b.ip, 16) < 0);
291 }
292 
293 bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
294 {
295  if (!IsIPv4())
296  return false;
297  memcpy(pipv4Addr, ip+12, 4);
298  return true;
299 }
300 
301 bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
302 {
303  if (!IsIPv6()) {
304  return false;
305  }
306  memcpy(pipv6Addr, ip, 16);
307  return true;
308 }
309 
310 // get canonical identifier of an address' group
311 // no two connections will be attempted to addresses with the same group
312 std::vector<unsigned char> CNetAddr::GetGroup() const
313 {
314  std::vector<unsigned char> vchRet;
315  int nClass = NET_IPV6;
316  int nStartByte = 0;
317  int nBits = 16;
318 
319  // all local addresses belong to the same group
320  if (IsLocal())
321  {
322  nClass = 255;
323  nBits = 0;
324  }
325  // all internal-usage addresses get their own group
326  if (IsInternal())
327  {
328  nClass = NET_INTERNAL;
329  nStartByte = sizeof(g_internal_prefix);
330  nBits = (sizeof(ip) - sizeof(g_internal_prefix)) * 8;
331  }
332  // all other unroutable addresses belong to the same group
333  else if (!IsRoutable())
334  {
335  nClass = NET_UNROUTABLE;
336  nBits = 0;
337  }
338  // for IPv4 addresses, '1' + the 16 higher-order bits of the IP
339  // includes mapped IPv4, SIIT translated IPv4, and the well-known prefix
340  else if (IsIPv4() || IsRFC6145() || IsRFC6052())
341  {
342  nClass = NET_IPV4;
343  nStartByte = 12;
344  }
345  // for 6to4 tunnelled addresses, use the encapsulated IPv4 address
346  else if (IsRFC3964())
347  {
348  nClass = NET_IPV4;
349  nStartByte = 2;
350  }
351  // for Teredo-tunnelled IPv6 addresses, use the encapsulated IPv4 address
352  else if (IsRFC4380())
353  {
354  vchRet.push_back(NET_IPV4);
355  vchRet.push_back(GetByte(3) ^ 0xFF);
356  vchRet.push_back(GetByte(2) ^ 0xFF);
357  return vchRet;
358  }
359  else if (IsTor())
360  {
361  nClass = NET_ONION;
362  nStartByte = 6;
363  nBits = 4;
364  }
365  // for he.net, use /36 groups
366  else if (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x04 && GetByte(12) == 0x70)
367  nBits = 36;
368  // for the rest of the IPv6 network, use /32 groups
369  else
370  nBits = 32;
371 
372  vchRet.push_back(nClass);
373  while (nBits >= 8)
374  {
375  vchRet.push_back(GetByte(15 - nStartByte));
376  nStartByte++;
377  nBits -= 8;
378  }
379  if (nBits > 0)
380  vchRet.push_back(GetByte(15 - nStartByte) | ((1 << (8 - nBits)) - 1));
381 
382  return vchRet;
383 }
384 
385 uint64_t CNetAddr::GetHash() const
386 {
387  uint256 hash = Hash(&ip[0], &ip[16]);
388  uint64_t nRet;
389  memcpy(&nRet, &hash, sizeof(nRet));
390  return nRet;
391 }
392 
393 // private extensions to enum Network, only returned by GetExtNetwork,
394 // and only used in GetReachabilityFrom
395 static const int NET_UNKNOWN = NET_MAX + 0;
396 static const int NET_TEREDO = NET_MAX + 1;
397 int static GetExtNetwork(const CNetAddr *addr)
398 {
399  if (addr == nullptr)
400  return NET_UNKNOWN;
401  if (addr->IsRFC4380())
402  return NET_TEREDO;
403  return addr->GetNetwork();
404 }
405 
407 int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
408 {
409  enum Reachability {
410  REACH_UNREACHABLE,
411  REACH_DEFAULT,
412  REACH_TEREDO,
413  REACH_IPV6_WEAK,
414  REACH_IPV4,
415  REACH_IPV6_STRONG,
416  REACH_PRIVATE
417  };
418 
419  if (!IsRoutable() || IsInternal())
420  return REACH_UNREACHABLE;
421 
422  int ourNet = GetExtNetwork(this);
423  int theirNet = GetExtNetwork(paddrPartner);
424  bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
425 
426  switch(theirNet) {
427  case NET_IPV4:
428  switch(ourNet) {
429  default: return REACH_DEFAULT;
430  case NET_IPV4: return REACH_IPV4;
431  }
432  case NET_IPV6:
433  switch(ourNet) {
434  default: return REACH_DEFAULT;
435  case NET_TEREDO: return REACH_TEREDO;
436  case NET_IPV4: return REACH_IPV4;
437  case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
438  }
439  case NET_ONION:
440  switch(ourNet) {
441  default: return REACH_DEFAULT;
442  case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
443  case NET_ONION: return REACH_PRIVATE;
444  }
445  case NET_TEREDO:
446  switch(ourNet) {
447  default: return REACH_DEFAULT;
448  case NET_TEREDO: return REACH_TEREDO;
449  case NET_IPV6: return REACH_IPV6_WEAK;
450  case NET_IPV4: return REACH_IPV4;
451  }
452  case NET_UNKNOWN:
453  case NET_UNROUTABLE:
454  default:
455  switch(ourNet) {
456  default: return REACH_DEFAULT;
457  case NET_TEREDO: return REACH_TEREDO;
458  case NET_IPV6: return REACH_IPV6_WEAK;
459  case NET_IPV4: return REACH_IPV4;
460  case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
461  }
462  }
463 }
464 
466 {
467 }
468 
469 CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
470 {
471 }
472 
473 CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn)
474 {
475 }
476 
477 CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn)
478 {
479 }
480 
481 CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
482 {
483  assert(addr.sin_family == AF_INET);
484 }
485 
486 CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
487 {
488  assert(addr.sin6_family == AF_INET6);
489 }
490 
491 bool CService::SetSockAddr(const struct sockaddr *paddr)
492 {
493  switch (paddr->sa_family) {
494  case AF_INET:
495  *this = CService(*(const struct sockaddr_in*)paddr);
496  return true;
497  case AF_INET6:
498  *this = CService(*(const struct sockaddr_in6*)paddr);
499  return true;
500  default:
501  return false;
502  }
503 }
504 
505 unsigned short CService::GetPort() const
506 {
507  return port;
508 }
509 
510 bool operator==(const CService& a, const CService& b)
511 {
512  return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
513 }
514 
515 bool operator<(const CService& a, const CService& b)
516 {
517  return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
518 }
519 
520 bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
521 {
522  if (IsIPv4()) {
523  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
524  return false;
525  *addrlen = sizeof(struct sockaddr_in);
526  struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
527  memset(paddrin, 0, *addrlen);
528  if (!GetInAddr(&paddrin->sin_addr))
529  return false;
530  paddrin->sin_family = AF_INET;
531  paddrin->sin_port = htons(port);
532  return true;
533  }
534  if (IsIPv6()) {
535  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
536  return false;
537  *addrlen = sizeof(struct sockaddr_in6);
538  struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
539  memset(paddrin6, 0, *addrlen);
540  if (!GetIn6Addr(&paddrin6->sin6_addr))
541  return false;
542  paddrin6->sin6_scope_id = scopeId;
543  paddrin6->sin6_family = AF_INET6;
544  paddrin6->sin6_port = htons(port);
545  return true;
546  }
547  return false;
548 }
549 
550 std::vector<unsigned char> CService::GetKey() const
551 {
552  std::vector<unsigned char> vKey;
553  vKey.resize(18);
554  memcpy(vKey.data(), ip, 16);
555  vKey[16] = port / 0x100;
556  vKey[17] = port & 0x0FF;
557  return vKey;
558 }
559 
560 std::string CService::ToStringPort() const
561 {
562  return strprintf("%u", port);
563 }
564 
565 std::string CService::ToStringIPPort() const
566 {
567  if (IsIPv4() || IsTor() || IsInternal()) {
568  return ToStringIP() + ":" + ToStringPort();
569  } else {
570  return "[" + ToStringIP() + "]:" + ToStringPort();
571  }
572 }
573 
574 std::string CService::ToString() const
575 {
576  return ToStringIPPort();
577 }
578 
580  valid(false)
581 {
582  memset(netmask, 0, sizeof(netmask));
583 }
584 
585 CSubNet::CSubNet(const CNetAddr &addr, int32_t mask)
586 {
587  valid = true;
588  network = addr;
589  // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
590  memset(netmask, 255, sizeof(netmask));
591 
592  // IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
593  const int astartofs = network.IsIPv4() ? 12 : 0;
594 
595  int32_t n = mask;
596  if(n >= 0 && n <= (128 - astartofs*8)) // Only valid if in range of bits of address
597  {
598  n += astartofs*8;
599  // Clear bits [n..127]
600  for (; n < 128; ++n)
601  netmask[n>>3] &= ~(1<<(7-(n&7)));
602  } else
603  valid = false;
604 
605  // Normalize network according to netmask
606  for(int x=0; x<16; ++x)
607  network.ip[x] &= netmask[x];
608 }
609 
610 CSubNet::CSubNet(const CNetAddr &addr, const CNetAddr &mask)
611 {
612  valid = true;
613  network = addr;
614  // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
615  memset(netmask, 255, sizeof(netmask));
616 
617  // IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
618  const int astartofs = network.IsIPv4() ? 12 : 0;
619 
620  for(int x=astartofs; x<16; ++x)
621  netmask[x] = mask.ip[x];
622 
623  // Normalize network according to netmask
624  for(int x=0; x<16; ++x)
625  network.ip[x] &= netmask[x];
626 }
627 
629  valid(addr.IsValid())
630 {
631  memset(netmask, 255, sizeof(netmask));
632  network = addr;
633 }
634 
635 bool CSubNet::Match(const CNetAddr &addr) const
636 {
637  if (!valid || !addr.IsValid())
638  return false;
639  for(int x=0; x<16; ++x)
640  if ((addr.ip[x] & netmask[x]) != network.ip[x])
641  return false;
642  return true;
643 }
644 
645 static inline int NetmaskBits(uint8_t x)
646 {
647  switch(x) {
648  case 0x00: return 0;
649  case 0x80: return 1;
650  case 0xc0: return 2;
651  case 0xe0: return 3;
652  case 0xf0: return 4;
653  case 0xf8: return 5;
654  case 0xfc: return 6;
655  case 0xfe: return 7;
656  case 0xff: return 8;
657  default: return -1;
658  }
659 }
660 
661 std::string CSubNet::ToString() const
662 {
663  /* Parse binary 1{n}0{N-n} to see if mask can be represented as /n */
664  int cidr = 0;
665  bool valid_cidr = true;
666  int n = network.IsIPv4() ? 12 : 0;
667  for (; n < 16 && netmask[n] == 0xff; ++n)
668  cidr += 8;
669  if (n < 16) {
670  int bits = NetmaskBits(netmask[n]);
671  if (bits < 0)
672  valid_cidr = false;
673  else
674  cidr += bits;
675  ++n;
676  }
677  for (; n < 16 && valid_cidr; ++n)
678  if (netmask[n] != 0x00)
679  valid_cidr = false;
680 
681  /* Format output */
682  std::string strNetmask;
683  if (valid_cidr) {
684  strNetmask = strprintf("%u", cidr);
685  } else {
686  if (network.IsIPv4())
687  strNetmask = strprintf("%u.%u.%u.%u", netmask[12], netmask[13], netmask[14], netmask[15]);
688  else
689  strNetmask = strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
690  netmask[0] << 8 | netmask[1], netmask[2] << 8 | netmask[3],
691  netmask[4] << 8 | netmask[5], netmask[6] << 8 | netmask[7],
692  netmask[8] << 8 | netmask[9], netmask[10] << 8 | netmask[11],
693  netmask[12] << 8 | netmask[13], netmask[14] << 8 | netmask[15]);
694  }
695 
696  return network.ToString() + "/" + strNetmask;
697 }
698 
699 bool CSubNet::IsValid() const
700 {
701  return valid;
702 }
703 
704 bool operator==(const CSubNet& a, const CSubNet& b)
705 {
706  return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
707 }
708 
709 bool operator<(const CSubNet& a, const CSubNet& b)
710 {
711  return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
712 }
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:407
std::string ToStringPort() const
Definition: netaddress.cpp:560
unsigned short GetPort() const
Definition: netaddress.cpp:505
bool IsLocal() const
Definition: netaddress.cpp:174
bool IsRFC4380() const
Definition: netaddress.cpp:142
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:23
bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:283
#define strprintf
Definition: tinyformat.h:1066
bool IsIPv6() const
Definition: netaddress.cpp:91
uint16_t port
Definition: netaddress.h:145
bool IsInternal() const
Definition: netaddress.cpp:232
CNetAddr network
Network (base) address.
Definition: netaddress.h:108
bool GetInAddr(struct in_addr *pipv4Addr) const
Definition: netaddress.cpp:293
std::string ToString() const
Definition: netaddress.cpp:278
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Definition: netaddress.cpp:301
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:119
enum Network GetNetwork() const
Definition: netaddress.cpp:237
bool IsRFC2544() const
Definition: netaddress.cpp:104
bool IsRFC4862() const
Definition: netaddress.cpp:147
bool IsValid() const
Definition: netaddress.cpp:188
std::vector< unsigned char > GetGroup() const
Definition: netaddress.cpp:312
bool IsIPv4() const
Definition: netaddress.cpp:86
bool IsRFC5737() const
Definition: netaddress.cpp:119
uint32_t scopeId
Definition: netaddress.h:36
bool IsRFC6145() const
Definition: netaddress.cpp:158
bool IsRFC6052() const
Definition: netaddress.cpp:136
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Definition: netaddress.cpp:520
std::string ToStringIP() const
Definition: netaddress.cpp:254
const char * name
Definition: rest.cpp:37
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid)
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:142
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:550
Network
Definition: netaddress.h:20
unsigned int GetByte(int n) const
Definition: netaddress.cpp:81
bool IsRFC3849() const
Definition: netaddress.cpp:126
bool IsRoutable() const
Definition: netaddress.cpp:227
uint64_t GetHash() const
Definition: netaddress.cpp:385
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:112
A hasher class for SHA3-256.
Definition: sha3.h:14
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:635
bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:288
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:110
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:32
bool IsValid() const
Definition: netaddress.cpp:699
bool IsRFC1918() const
Definition: netaddress.cpp:96
256-bit opaque blob.
Definition: uint256.h:122
bool IsRFC6598() const
Definition: netaddress.cpp:114
std::string ToString() const
Definition: netaddress.cpp:661
bool IsRFC3927() const
Definition: netaddress.cpp:109
void * memcpy(void *a, const void *b, size_t c)
std::string ToStringIPPort() const
Definition: netaddress.cpp:565
unsigned char ip[16]
Definition: netaddress.h:35
CSHA3 & Write(const unsigned char *data, size_t len)
Definition: sha3.cpp:30
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
Definition: netaddress.cpp:28
std::string EncodeBase32(const unsigned char *pch, size_t len)
bool SetSpecial(const std::string &strName)
Definition: netaddress.cpp:56
std::string ToString() const
Definition: netaddress.cpp:574
bool IsRFC4843() const
Definition: netaddress.cpp:164
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:491
bool SetInternal(const std::string &name)
Transform an arbitrary string into a non-routable ipv6 address.
Definition: netaddress.cpp:44
bool IsRFC4193() const
Definition: netaddress.cpp:153
bool IsTor() const
Definition: netaddress.cpp:169
bool IsRFC3964() const
Definition: netaddress.cpp:131