Bitcoin Core  0.18.99
P2P Digital Currency
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 <util/strencodings.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 + sha256("bitcoin")[0:5]
15 static const unsigned char g_internal_prefix[] = { 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 };
16 
23 {
24  memset(ip, 0, sizeof(ip));
25 }
26 
27 void CNetAddr::SetIP(const CNetAddr& ipIn)
28 {
29  memcpy(ip, ipIn.ip, sizeof(ip));
30 }
31 
32 void CNetAddr::SetRaw(Network network, const uint8_t *ip_in)
33 {
34  switch(network)
35  {
36  case NET_IPV4:
37  memcpy(ip, pchIPv4, 12);
38  memcpy(ip+12, ip_in, 4);
39  break;
40  case NET_IPV6:
41  memcpy(ip, ip_in, 16);
42  break;
43  default:
44  assert(!"invalid network");
45  }
46 }
47 
62 bool CNetAddr::SetInternal(const std::string &name)
63 {
64  if (name.empty()) {
65  return false;
66  }
67  unsigned char hash[32] = {};
68  CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
69  memcpy(ip, g_internal_prefix, sizeof(g_internal_prefix));
70  memcpy(ip + sizeof(g_internal_prefix), hash, sizeof(ip) - sizeof(g_internal_prefix));
71  return true;
72 }
73 
84 bool CNetAddr::SetSpecial(const std::string &strName)
85 {
86  if (strName.size()>6 && strName.substr(strName.size() - 6, 6) == ".onion") {
87  std::vector<unsigned char> vchAddr = DecodeBase32(strName.substr(0, strName.size() - 6).c_str());
88  if (vchAddr.size() != 16-sizeof(pchOnionCat))
89  return false;
90  memcpy(ip, pchOnionCat, sizeof(pchOnionCat));
91  for (unsigned int i=0; i<16-sizeof(pchOnionCat); i++)
92  ip[i + sizeof(pchOnionCat)] = vchAddr[i];
93  return true;
94  }
95  return false;
96 }
97 
98 CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
99 {
100  SetRaw(NET_IPV4, (const uint8_t*)&ipv4Addr);
101 }
102 
103 CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
104 {
105  SetRaw(NET_IPV6, (const uint8_t*)&ipv6Addr);
106  scopeId = scope;
107 }
108 
109 unsigned int CNetAddr::GetByte(int n) const
110 {
111  return ip[15-n];
112 }
113 
115 {
116  const int cmplen = IsIPv4() ? 4 : 16;
117  for (int i = 0; i < cmplen; ++i) {
118  if (GetByte(i)) return false;
119  }
120 
121  return true;
122 }
123 
124 bool CNetAddr::IsIPv4() const
125 {
126  return (memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0);
127 }
128 
129 bool CNetAddr::IsIPv6() const
130 {
131  return (!IsIPv4() && !IsTor() && !IsInternal());
132 }
133 
135 {
136  return IsIPv4() && (
137  GetByte(3) == 10 ||
138  (GetByte(3) == 192 && GetByte(2) == 168) ||
139  (GetByte(3) == 172 && (GetByte(2) >= 16 && GetByte(2) <= 31)));
140 }
141 
143 {
144  return IsIPv4() && GetByte(3) == 198 && (GetByte(2) == 18 || GetByte(2) == 19);
145 }
146 
148 {
149  return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
150 }
151 
153 {
154  return IsIPv4() && GetByte(3) == 100 && GetByte(2) >= 64 && GetByte(2) <= 127;
155 }
156 
158 {
159  return IsIPv4() && ((GetByte(3) == 192 && GetByte(2) == 0 && GetByte(1) == 2) ||
160  (GetByte(3) == 198 && GetByte(2) == 51 && GetByte(1) == 100) ||
161  (GetByte(3) == 203 && GetByte(2) == 0 && GetByte(1) == 113));
162 }
163 
165 {
166  return GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x0D && GetByte(12) == 0xB8;
167 }
168 
170 {
171  return (GetByte(15) == 0x20 && GetByte(14) == 0x02);
172 }
173 
175 {
176  static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
177  return (memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0);
178 }
179 
181 {
182  return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0 && GetByte(12) == 0);
183 }
184 
186 {
187  static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
188  return (memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0);
189 }
190 
192 {
193  return ((GetByte(15) & 0xFE) == 0xFC);
194 }
195 
197 {
198  static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
199  return (memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0);
200 }
201 
203 {
204  return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 && (GetByte(12) & 0xF0) == 0x10);
205 }
206 
213 bool CNetAddr::IsTor() const
214 {
215  return (memcmp(ip, pchOnionCat, sizeof(pchOnionCat)) == 0);
216 }
217 
218 bool CNetAddr::IsLocal() const
219 {
220  // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
221  if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0))
222  return true;
223 
224  // IPv6 loopback (::1/128)
225  static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
226  if (memcmp(ip, pchLocal, 16) == 0)
227  return true;
228 
229  return false;
230 }
231 
242 bool CNetAddr::IsValid() const
243 {
244  // Cleanup 3-byte shifted addresses caused by garbage in size field
245  // of addr messages from versions before 0.2.9 checksum.
246  // Two consecutive addr messages look like this:
247  // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
248  // so if the first length field is garbled, it reads the second batch
249  // of addr misaligned by 3 bytes.
250  if (memcmp(ip, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
251  return false;
252 
253  // unspecified IPv6 address (::/128)
254  unsigned char ipNone6[16] = {};
255  if (memcmp(ip, ipNone6, 16) == 0)
256  return false;
257 
258  // documentation IPv6 address
259  if (IsRFC3849())
260  return false;
261 
262  if (IsInternal())
263  return false;
264 
265  if (IsIPv4())
266  {
267  // INADDR_NONE
268  uint32_t ipNone = INADDR_NONE;
269  if (memcmp(ip+12, &ipNone, 4) == 0)
270  return false;
271 
272  // 0
273  ipNone = 0;
274  if (memcmp(ip+12, &ipNone, 4) == 0)
275  return false;
276  }
277 
278  return true;
279 }
280 
291 {
292  return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) || IsRFC4843() || IsLocal() || IsInternal());
293 }
294 
301 {
302  return memcmp(ip, g_internal_prefix, sizeof(g_internal_prefix)) == 0;
303 }
304 
306 {
307  if (IsInternal())
308  return NET_INTERNAL;
309 
310  if (!IsRoutable())
311  return NET_UNROUTABLE;
312 
313  if (IsIPv4())
314  return NET_IPV4;
315 
316  if (IsTor())
317  return NET_ONION;
318 
319  return NET_IPV6;
320 }
321 
322 std::string CNetAddr::ToStringIP() const
323 {
324  if (IsTor())
325  return EncodeBase32(&ip[6], 10) + ".onion";
326  if (IsInternal())
327  return EncodeBase32(ip + sizeof(g_internal_prefix), sizeof(ip) - sizeof(g_internal_prefix)) + ".internal";
328  CService serv(*this, 0);
329  struct sockaddr_storage sockaddr;
330  socklen_t socklen = sizeof(sockaddr);
331  if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
332  char name[1025] = "";
333  if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), nullptr, 0, NI_NUMERICHOST))
334  return std::string(name);
335  }
336  if (IsIPv4())
337  return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
338  else
339  return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
340  GetByte(15) << 8 | GetByte(14), GetByte(13) << 8 | GetByte(12),
341  GetByte(11) << 8 | GetByte(10), GetByte(9) << 8 | GetByte(8),
342  GetByte(7) << 8 | GetByte(6), GetByte(5) << 8 | GetByte(4),
343  GetByte(3) << 8 | GetByte(2), GetByte(1) << 8 | GetByte(0));
344 }
345 
346 std::string CNetAddr::ToString() const
347 {
348  return ToStringIP();
349 }
350 
351 bool operator==(const CNetAddr& a, const CNetAddr& b)
352 {
353  return (memcmp(a.ip, b.ip, 16) == 0);
354 }
355 
356 bool operator<(const CNetAddr& a, const CNetAddr& b)
357 {
358  return (memcmp(a.ip, b.ip, 16) < 0);
359 }
360 
371 bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
372 {
373  if (!IsIPv4())
374  return false;
375  memcpy(pipv4Addr, ip+12, 4);
376  return true;
377 }
378 
389 bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
390 {
391  if (!IsIPv6()) {
392  return false;
393  }
394  memcpy(pipv6Addr, ip, 16);
395  return true;
396 }
397 
408 std::vector<unsigned char> CNetAddr::GetGroup() const
409 {
410  std::vector<unsigned char> vchRet;
411  int nClass = NET_IPV6;
412  int nStartByte = 0;
413  int nBits = 16;
414 
415  // all local addresses belong to the same group
416  if (IsLocal())
417  {
418  nClass = 255;
419  nBits = 0;
420  }
421  // all internal-usage addresses get their own group
422  if (IsInternal())
423  {
424  nClass = NET_INTERNAL;
425  nStartByte = sizeof(g_internal_prefix);
426  nBits = (sizeof(ip) - sizeof(g_internal_prefix)) * 8;
427  }
428  // all other unroutable addresses belong to the same group
429  else if (!IsRoutable())
430  {
431  nClass = NET_UNROUTABLE;
432  nBits = 0;
433  }
434  // for IPv4 addresses, '1' + the 16 higher-order bits of the IP
435  // includes mapped IPv4, SIIT translated IPv4, and the well-known prefix
436  else if (IsIPv4() || IsRFC6145() || IsRFC6052())
437  {
438  nClass = NET_IPV4;
439  nStartByte = 12;
440  }
441  // for 6to4 tunnelled addresses, use the encapsulated IPv4 address
442  else if (IsRFC3964())
443  {
444  nClass = NET_IPV4;
445  nStartByte = 2;
446  }
447  // for Teredo-tunnelled IPv6 addresses, use the encapsulated IPv4 address
448  else if (IsRFC4380())
449  {
450  vchRet.push_back(NET_IPV4);
451  vchRet.push_back(GetByte(3) ^ 0xFF);
452  vchRet.push_back(GetByte(2) ^ 0xFF);
453  return vchRet;
454  }
455  else if (IsTor())
456  {
457  nClass = NET_ONION;
458  nStartByte = 6;
459  nBits = 4;
460  }
461  // for he.net, use /36 groups
462  else if (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x04 && GetByte(12) == 0x70)
463  nBits = 36;
464  // for the rest of the IPv6 network, use /32 groups
465  else
466  nBits = 32;
467 
468  vchRet.push_back(nClass);
469 
470  // push our ip onto vchRet byte by byte...
471  while (nBits >= 8)
472  {
473  vchRet.push_back(GetByte(15 - nStartByte));
474  nStartByte++;
475  nBits -= 8;
476  }
477  // ...for the last byte, push nBits and for the rest of the byte push 1's
478  if (nBits > 0)
479  vchRet.push_back(GetByte(15 - nStartByte) | ((1 << (8 - nBits)) - 1));
480 
481  return vchRet;
482 }
483 
484 uint64_t CNetAddr::GetHash() const
485 {
486  uint256 hash = Hash(&ip[0], &ip[16]);
487  uint64_t nRet;
488  memcpy(&nRet, &hash, sizeof(nRet));
489  return nRet;
490 }
491 
492 // private extensions to enum Network, only returned by GetExtNetwork,
493 // and only used in GetReachabilityFrom
494 static const int NET_UNKNOWN = NET_MAX + 0;
495 static const int NET_TEREDO = NET_MAX + 1;
496 int static GetExtNetwork(const CNetAddr *addr)
497 {
498  if (addr == nullptr)
499  return NET_UNKNOWN;
500  if (addr->IsRFC4380())
501  return NET_TEREDO;
502  return addr->GetNetwork();
503 }
504 
506 int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
507 {
508  enum Reachability {
509  REACH_UNREACHABLE,
510  REACH_DEFAULT,
511  REACH_TEREDO,
512  REACH_IPV6_WEAK,
513  REACH_IPV4,
514  REACH_IPV6_STRONG,
515  REACH_PRIVATE
516  };
517 
518  if (!IsRoutable() || IsInternal())
519  return REACH_UNREACHABLE;
520 
521  int ourNet = GetExtNetwork(this);
522  int theirNet = GetExtNetwork(paddrPartner);
523  bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
524 
525  switch(theirNet) {
526  case NET_IPV4:
527  switch(ourNet) {
528  default: return REACH_DEFAULT;
529  case NET_IPV4: return REACH_IPV4;
530  }
531  case NET_IPV6:
532  switch(ourNet) {
533  default: return REACH_DEFAULT;
534  case NET_TEREDO: return REACH_TEREDO;
535  case NET_IPV4: return REACH_IPV4;
536  case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
537  }
538  case NET_ONION:
539  switch(ourNet) {
540  default: return REACH_DEFAULT;
541  case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
542  case NET_ONION: return REACH_PRIVATE;
543  }
544  case NET_TEREDO:
545  switch(ourNet) {
546  default: return REACH_DEFAULT;
547  case NET_TEREDO: return REACH_TEREDO;
548  case NET_IPV6: return REACH_IPV6_WEAK;
549  case NET_IPV4: return REACH_IPV4;
550  }
551  case NET_UNKNOWN:
552  case NET_UNROUTABLE:
553  default:
554  switch(ourNet) {
555  default: return REACH_DEFAULT;
556  case NET_TEREDO: return REACH_TEREDO;
557  case NET_IPV6: return REACH_IPV6_WEAK;
558  case NET_IPV4: return REACH_IPV4;
559  case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
560  }
561  }
562 }
563 
565 {
566 }
567 
568 CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
569 {
570 }
571 
572 CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn)
573 {
574 }
575 
576 CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn)
577 {
578 }
579 
580 CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
581 {
582  assert(addr.sin_family == AF_INET);
583 }
584 
585 CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
586 {
587  assert(addr.sin6_family == AF_INET6);
588 }
589 
590 bool CService::SetSockAddr(const struct sockaddr *paddr)
591 {
592  switch (paddr->sa_family) {
593  case AF_INET:
594  *this = CService(*(const struct sockaddr_in*)paddr);
595  return true;
596  case AF_INET6:
597  *this = CService(*(const struct sockaddr_in6*)paddr);
598  return true;
599  default:
600  return false;
601  }
602 }
603 
604 unsigned short CService::GetPort() const
605 {
606  return port;
607 }
608 
609 bool operator==(const CService& a, const CService& b)
610 {
611  return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
612 }
613 
614 bool operator<(const CService& a, const CService& b)
615 {
616  return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
617 }
618 
631 bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
632 {
633  if (IsIPv4()) {
634  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
635  return false;
636  *addrlen = sizeof(struct sockaddr_in);
637  struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
638  memset(paddrin, 0, *addrlen);
639  if (!GetInAddr(&paddrin->sin_addr))
640  return false;
641  paddrin->sin_family = AF_INET;
642  paddrin->sin_port = htons(port);
643  return true;
644  }
645  if (IsIPv6()) {
646  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
647  return false;
648  *addrlen = sizeof(struct sockaddr_in6);
649  struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
650  memset(paddrin6, 0, *addrlen);
651  if (!GetIn6Addr(&paddrin6->sin6_addr))
652  return false;
653  paddrin6->sin6_scope_id = scopeId;
654  paddrin6->sin6_family = AF_INET6;
655  paddrin6->sin6_port = htons(port);
656  return true;
657  }
658  return false;
659 }
660 
664 std::vector<unsigned char> CService::GetKey() const
665 {
666  std::vector<unsigned char> vKey;
667  vKey.resize(18);
668  memcpy(vKey.data(), ip, 16);
669  vKey[16] = port / 0x100; // most significant byte of our port
670  vKey[17] = port & 0x0FF; // least significant byte of our port
671  return vKey;
672 }
673 
674 std::string CService::ToStringPort() const
675 {
676  return strprintf("%u", port);
677 }
678 
679 std::string CService::ToStringIPPort() const
680 {
681  if (IsIPv4() || IsTor() || IsInternal()) {
682  return ToStringIP() + ":" + ToStringPort();
683  } else {
684  return "[" + ToStringIP() + "]:" + ToStringPort();
685  }
686 }
687 
688 std::string CService::ToString() const
689 {
690  return ToStringIPPort();
691 }
692 
694  valid(false)
695 {
696  memset(netmask, 0, sizeof(netmask));
697 }
698 
699 CSubNet::CSubNet(const CNetAddr &addr, int32_t mask)
700 {
701  valid = true;
702  network = addr;
703  // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
704  memset(netmask, 255, sizeof(netmask));
705 
706  // IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
707  const int astartofs = network.IsIPv4() ? 12 : 0;
708 
709  int32_t n = mask;
710  if(n >= 0 && n <= (128 - astartofs*8)) // Only valid if in range of bits of address
711  {
712  n += astartofs*8;
713  // Clear bits [n..127]
714  for (; n < 128; ++n)
715  netmask[n>>3] &= ~(1<<(7-(n&7)));
716  } else
717  valid = false;
718 
719  // Normalize network according to netmask
720  for(int x=0; x<16; ++x)
721  network.ip[x] &= netmask[x];
722 }
723 
724 CSubNet::CSubNet(const CNetAddr &addr, const CNetAddr &mask)
725 {
726  valid = true;
727  network = addr;
728  // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
729  memset(netmask, 255, sizeof(netmask));
730 
731  // IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
732  const int astartofs = network.IsIPv4() ? 12 : 0;
733 
734  for(int x=astartofs; x<16; ++x)
735  netmask[x] = mask.ip[x];
736 
737  // Normalize network according to netmask
738  for(int x=0; x<16; ++x)
739  network.ip[x] &= netmask[x];
740 }
741 
743  valid(addr.IsValid())
744 {
745  memset(netmask, 255, sizeof(netmask));
746  network = addr;
747 }
748 
753 bool CSubNet::Match(const CNetAddr &addr) const
754 {
755  if (!valid || !addr.IsValid())
756  return false;
757  for(int x=0; x<16; ++x)
758  if ((addr.ip[x] & netmask[x]) != network.ip[x])
759  return false;
760  return true;
761 }
762 
767 static inline int NetmaskBits(uint8_t x)
768 {
769  switch(x) {
770  case 0x00: return 0;
771  case 0x80: return 1;
772  case 0xc0: return 2;
773  case 0xe0: return 3;
774  case 0xf0: return 4;
775  case 0xf8: return 5;
776  case 0xfc: return 6;
777  case 0xfe: return 7;
778  case 0xff: return 8;
779  default: return -1;
780  }
781 }
782 
783 std::string CSubNet::ToString() const
784 {
785  /* Parse binary 1{n}0{N-n} to see if mask can be represented as /n */
786  int cidr = 0;
787  bool valid_cidr = true;
788  int n = network.IsIPv4() ? 12 : 0;
789  for (; n < 16 && netmask[n] == 0xff; ++n)
790  cidr += 8;
791  if (n < 16) {
792  int bits = NetmaskBits(netmask[n]);
793  if (bits < 0)
794  valid_cidr = false;
795  else
796  cidr += bits;
797  ++n;
798  }
799  for (; n < 16 && valid_cidr; ++n)
800  if (netmask[n] != 0x00)
801  valid_cidr = false;
802 
803  /* Format output */
804  std::string strNetmask;
805  if (valid_cidr) {
806  strNetmask = strprintf("%u", cidr);
807  } else {
808  if (network.IsIPv4())
809  strNetmask = strprintf("%u.%u.%u.%u", netmask[12], netmask[13], netmask[14], netmask[15]);
810  else
811  strNetmask = strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
812  netmask[0] << 8 | netmask[1], netmask[2] << 8 | netmask[3],
813  netmask[4] << 8 | netmask[5], netmask[6] << 8 | netmask[7],
814  netmask[8] << 8 | netmask[9], netmask[10] << 8 | netmask[11],
815  netmask[12] << 8 | netmask[13], netmask[14] << 8 | netmask[15]);
816  }
817 
818  return network.ToString() + "/" + strNetmask;
819 }
820 
821 bool CSubNet::IsValid() const
822 {
823  return valid;
824 }
825 
826 bool operator==(const CSubNet& a, const CSubNet& b)
827 {
828  return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
829 }
830 
831 bool operator<(const CSubNet& a, const CSubNet& b)
832 {
833  return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
834 }
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:506
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:648
std::string ToStringPort() const
Definition: netaddress.cpp:674
std::string EncodeBase32(const unsigned char *pch, size_t len)
unsigned short GetPort() const
Definition: netaddress.cpp:604
bool IsLocal() const
Definition: netaddress.cpp:218
bool IsRFC4380() const
Definition: netaddress.cpp:180
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:27
#define strprintf
Definition: tinyformat.h:1066
bool IsIPv6() const
Definition: netaddress.cpp:129
uint16_t port
Definition: netaddress.h:142
friend bool operator==(const CService &a, const CService &b)
Definition: netaddress.cpp:609
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid)
bool IsInternal() const
Definition: netaddress.cpp:300
CNetAddr network
Network (base) address.
Definition: netaddress.h:105
bool IsBindAny() const
Definition: netaddress.cpp:114
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:371
std::string ToString() const
Definition: netaddress.cpp:346
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:389
enum Network GetNetwork() const
Definition: netaddress.cpp:305
bool IsRFC2544() const
Definition: netaddress.cpp:142
bool IsRFC4862() const
Definition: netaddress.cpp:185
bool IsValid() const
Definition: netaddress.cpp:242
std::vector< unsigned char > GetGroup() const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:408
bool IsIPv4() const
Definition: netaddress.cpp:124
bool IsRFC5737() const
Definition: netaddress.cpp:157
uint32_t scopeId
Definition: netaddress.h:36
bool IsRFC6145() const
Definition: netaddress.cpp:196
bool IsRFC6052() const
Definition: netaddress.cpp:174
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
Definition: netaddress.cpp:631
std::string ToStringIP() const
Definition: netaddress.cpp:322
const char * name
Definition: rest.cpp:38
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:139
friend bool operator<(const CService &a, const CService &b)
Definition: netaddress.cpp:614
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:664
Network
Definition: netaddress.h:20
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:71
unsigned int GetByte(int n) const
Definition: netaddress.cpp:109
friend bool operator==(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:826
bool IsRFC3849() const
Definition: netaddress.cpp:164
bool IsRoutable() const
Definition: netaddress.cpp:290
uint64_t GetHash() const
Definition: netaddress.cpp:484
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:109
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:753
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:107
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:32
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:356
bool IsValid() const
Definition: netaddress.cpp:821
bool IsRFC1918() const
Definition: netaddress.cpp:134
256-bit opaque blob.
Definition: uint256.h:121
bool IsRFC6598() const
Definition: netaddress.cpp:152
std::string ToString() const
Definition: netaddress.cpp:783
bool IsRFC3927() const
Definition: netaddress.cpp:147
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:351
void * memcpy(void *a, const void *b, size_t c)
std::string ToStringIPPort() const
Definition: netaddress.cpp:679
unsigned char ip[16]
Definition: netaddress.h:35
friend bool operator<(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:831
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
Definition: netaddress.cpp:32
bool SetSpecial(const std::string &strName)
Try to make this a dummy address that maps the specified onion address into IPv6 using OnionCat&#39;s ran...
Definition: netaddress.cpp:84
std::string ToString() const
Definition: netaddress.cpp:688
bool IsRFC4843() const
Definition: netaddress.cpp:202
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:590
bool SetInternal(const std::string &name)
Try to make this a dummy address that maps the specified name into IPv6 like so: (0xFD + sha256("bitc...
Definition: netaddress.cpp:62
A hasher class for SHA-256.
Definition: sha256.h:13
bool IsRFC4193() const
Definition: netaddress.cpp:191
bool IsTor() const
Definition: netaddress.cpp:213
bool IsRFC3964() const
Definition: netaddress.cpp:169
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:22