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);
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 
208 {
209  return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 && (GetByte(12) & 0xF0) == 0x20);
210 }
211 
218 bool CNetAddr::IsTor() const
219 {
220  return (memcmp(ip, pchOnionCat, sizeof(pchOnionCat)) == 0);
221 }
222 
223 bool CNetAddr::IsLocal() const
224 {
225  // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
226  if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0))
227  return true;
228 
229  // IPv6 loopback (::1/128)
230  static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
231  if (memcmp(ip, pchLocal, 16) == 0)
232  return true;
233 
234  return false;
235 }
236 
247 bool CNetAddr::IsValid() const
248 {
249  // Cleanup 3-byte shifted addresses caused by garbage in size field
250  // of addr messages from versions before 0.2.9 checksum.
251  // Two consecutive addr messages look like this:
252  // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
253  // so if the first length field is garbled, it reads the second batch
254  // of addr misaligned by 3 bytes.
255  if (memcmp(ip, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
256  return false;
257 
258  // unspecified IPv6 address (::/128)
259  unsigned char ipNone6[16] = {};
260  if (memcmp(ip, ipNone6, 16) == 0)
261  return false;
262 
263  // documentation IPv6 address
264  if (IsRFC3849())
265  return false;
266 
267  if (IsInternal())
268  return false;
269 
270  if (IsIPv4())
271  {
272  // INADDR_NONE
273  uint32_t ipNone = INADDR_NONE;
274  if (memcmp(ip+12, &ipNone, 4) == 0)
275  return false;
276 
277  // 0
278  ipNone = 0;
279  if (memcmp(ip+12, &ipNone, 4) == 0)
280  return false;
281  }
282 
283  return true;
284 }
285 
296 {
297  return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
298 }
299 
306 {
307  return memcmp(ip, g_internal_prefix, sizeof(g_internal_prefix)) == 0;
308 }
309 
311 {
312  if (IsInternal())
313  return NET_INTERNAL;
314 
315  if (!IsRoutable())
316  return NET_UNROUTABLE;
317 
318  if (IsIPv4())
319  return NET_IPV4;
320 
321  if (IsTor())
322  return NET_ONION;
323 
324  return NET_IPV6;
325 }
326 
327 std::string CNetAddr::ToStringIP() const
328 {
329  if (IsTor())
330  return EncodeBase32(&ip[6], 10) + ".onion";
331  if (IsInternal())
332  return EncodeBase32(ip + sizeof(g_internal_prefix), sizeof(ip) - sizeof(g_internal_prefix)) + ".internal";
333  CService serv(*this, 0);
334  struct sockaddr_storage sockaddr;
335  socklen_t socklen = sizeof(sockaddr);
336  if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
337  char name[1025] = "";
338  if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), nullptr, 0, NI_NUMERICHOST))
339  return std::string(name);
340  }
341  if (IsIPv4())
342  return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
343  else
344  return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
345  GetByte(15) << 8 | GetByte(14), GetByte(13) << 8 | GetByte(12),
346  GetByte(11) << 8 | GetByte(10), GetByte(9) << 8 | GetByte(8),
347  GetByte(7) << 8 | GetByte(6), GetByte(5) << 8 | GetByte(4),
348  GetByte(3) << 8 | GetByte(2), GetByte(1) << 8 | GetByte(0));
349 }
350 
351 std::string CNetAddr::ToString() const
352 {
353  return ToStringIP();
354 }
355 
356 bool operator==(const CNetAddr& a, const CNetAddr& b)
357 {
358  return (memcmp(a.ip, b.ip, 16) == 0);
359 }
360 
361 bool operator<(const CNetAddr& a, const CNetAddr& b)
362 {
363  return (memcmp(a.ip, b.ip, 16) < 0);
364 }
365 
376 bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
377 {
378  if (!IsIPv4())
379  return false;
380  memcpy(pipv4Addr, ip+12, 4);
381  return true;
382 }
383 
394 bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
395 {
396  if (!IsIPv6()) {
397  return false;
398  }
399  memcpy(pipv6Addr, ip, 16);
400  return true;
401 }
402 
413 std::vector<unsigned char> CNetAddr::GetGroup() const
414 {
415  std::vector<unsigned char> vchRet;
416  int nClass = NET_IPV6;
417  int nStartByte = 0;
418  int nBits = 16;
419 
420  // all local addresses belong to the same group
421  if (IsLocal())
422  {
423  nClass = 255;
424  nBits = 0;
425  }
426  // all internal-usage addresses get their own group
427  if (IsInternal())
428  {
429  nClass = NET_INTERNAL;
430  nStartByte = sizeof(g_internal_prefix);
431  nBits = (sizeof(ip) - sizeof(g_internal_prefix)) * 8;
432  }
433  // all other unroutable addresses belong to the same group
434  else if (!IsRoutable())
435  {
436  nClass = NET_UNROUTABLE;
437  nBits = 0;
438  }
439  // for IPv4 addresses, '1' + the 16 higher-order bits of the IP
440  // includes mapped IPv4, SIIT translated IPv4, and the well-known prefix
441  else if (IsIPv4() || IsRFC6145() || IsRFC6052())
442  {
443  nClass = NET_IPV4;
444  nStartByte = 12;
445  }
446  // for 6to4 tunnelled addresses, use the encapsulated IPv4 address
447  else if (IsRFC3964())
448  {
449  nClass = NET_IPV4;
450  nStartByte = 2;
451  }
452  // for Teredo-tunnelled IPv6 addresses, use the encapsulated IPv4 address
453  else if (IsRFC4380())
454  {
455  vchRet.push_back(NET_IPV4);
456  vchRet.push_back(GetByte(3) ^ 0xFF);
457  vchRet.push_back(GetByte(2) ^ 0xFF);
458  return vchRet;
459  }
460  else if (IsTor())
461  {
462  nClass = NET_ONION;
463  nStartByte = 6;
464  nBits = 4;
465  }
466  // for he.net, use /36 groups
467  else if (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x04 && GetByte(12) == 0x70)
468  nBits = 36;
469  // for the rest of the IPv6 network, use /32 groups
470  else
471  nBits = 32;
472 
473  vchRet.push_back(nClass);
474 
475  // push our ip onto vchRet byte by byte...
476  while (nBits >= 8)
477  {
478  vchRet.push_back(GetByte(15 - nStartByte));
479  nStartByte++;
480  nBits -= 8;
481  }
482  // ...for the last byte, push nBits and for the rest of the byte push 1's
483  if (nBits > 0)
484  vchRet.push_back(GetByte(15 - nStartByte) | ((1 << (8 - nBits)) - 1));
485 
486  return vchRet;
487 }
488 
489 uint64_t CNetAddr::GetHash() const
490 {
491  uint256 hash = Hash(&ip[0], &ip[16]);
492  uint64_t nRet;
493  memcpy(&nRet, &hash, sizeof(nRet));
494  return nRet;
495 }
496 
497 // private extensions to enum Network, only returned by GetExtNetwork,
498 // and only used in GetReachabilityFrom
499 static const int NET_UNKNOWN = NET_MAX + 0;
500 static const int NET_TEREDO = NET_MAX + 1;
501 int static GetExtNetwork(const CNetAddr *addr)
502 {
503  if (addr == nullptr)
504  return NET_UNKNOWN;
505  if (addr->IsRFC4380())
506  return NET_TEREDO;
507  return addr->GetNetwork();
508 }
509 
511 int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
512 {
513  enum Reachability {
514  REACH_UNREACHABLE,
515  REACH_DEFAULT,
516  REACH_TEREDO,
517  REACH_IPV6_WEAK,
518  REACH_IPV4,
519  REACH_IPV6_STRONG,
520  REACH_PRIVATE
521  };
522 
523  if (!IsRoutable() || IsInternal())
524  return REACH_UNREACHABLE;
525 
526  int ourNet = GetExtNetwork(this);
527  int theirNet = GetExtNetwork(paddrPartner);
528  bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
529 
530  switch(theirNet) {
531  case NET_IPV4:
532  switch(ourNet) {
533  default: return REACH_DEFAULT;
534  case NET_IPV4: return REACH_IPV4;
535  }
536  case NET_IPV6:
537  switch(ourNet) {
538  default: return REACH_DEFAULT;
539  case NET_TEREDO: return REACH_TEREDO;
540  case NET_IPV4: return REACH_IPV4;
541  case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
542  }
543  case NET_ONION:
544  switch(ourNet) {
545  default: return REACH_DEFAULT;
546  case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
547  case NET_ONION: return REACH_PRIVATE;
548  }
549  case NET_TEREDO:
550  switch(ourNet) {
551  default: return REACH_DEFAULT;
552  case NET_TEREDO: return REACH_TEREDO;
553  case NET_IPV6: return REACH_IPV6_WEAK;
554  case NET_IPV4: return REACH_IPV4;
555  }
556  case NET_UNKNOWN:
557  case NET_UNROUTABLE:
558  default:
559  switch(ourNet) {
560  default: return REACH_DEFAULT;
561  case NET_TEREDO: return REACH_TEREDO;
562  case NET_IPV6: return REACH_IPV6_WEAK;
563  case NET_IPV4: return REACH_IPV4;
564  case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
565  }
566  }
567 }
568 
570 {
571 }
572 
573 CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
574 {
575 }
576 
577 CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn)
578 {
579 }
580 
581 CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn)
582 {
583 }
584 
585 CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
586 {
587  assert(addr.sin_family == AF_INET);
588 }
589 
590 CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
591 {
592  assert(addr.sin6_family == AF_INET6);
593 }
594 
595 bool CService::SetSockAddr(const struct sockaddr *paddr)
596 {
597  switch (paddr->sa_family) {
598  case AF_INET:
599  *this = CService(*(const struct sockaddr_in*)paddr);
600  return true;
601  case AF_INET6:
602  *this = CService(*(const struct sockaddr_in6*)paddr);
603  return true;
604  default:
605  return false;
606  }
607 }
608 
609 unsigned short CService::GetPort() const
610 {
611  return port;
612 }
613 
614 bool operator==(const CService& a, const CService& b)
615 {
616  return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
617 }
618 
619 bool operator<(const CService& a, const CService& b)
620 {
621  return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
622 }
623 
636 bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
637 {
638  if (IsIPv4()) {
639  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
640  return false;
641  *addrlen = sizeof(struct sockaddr_in);
642  struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
643  memset(paddrin, 0, *addrlen);
644  if (!GetInAddr(&paddrin->sin_addr))
645  return false;
646  paddrin->sin_family = AF_INET;
647  paddrin->sin_port = htons(port);
648  return true;
649  }
650  if (IsIPv6()) {
651  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
652  return false;
653  *addrlen = sizeof(struct sockaddr_in6);
654  struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
655  memset(paddrin6, 0, *addrlen);
656  if (!GetIn6Addr(&paddrin6->sin6_addr))
657  return false;
658  paddrin6->sin6_scope_id = scopeId;
659  paddrin6->sin6_family = AF_INET6;
660  paddrin6->sin6_port = htons(port);
661  return true;
662  }
663  return false;
664 }
665 
669 std::vector<unsigned char> CService::GetKey() const
670 {
671  std::vector<unsigned char> vKey;
672  vKey.resize(18);
673  memcpy(vKey.data(), ip, 16);
674  vKey[16] = port / 0x100; // most significant byte of our port
675  vKey[17] = port & 0x0FF; // least significant byte of our port
676  return vKey;
677 }
678 
679 std::string CService::ToStringPort() const
680 {
681  return strprintf("%u", port);
682 }
683 
684 std::string CService::ToStringIPPort() const
685 {
686  if (IsIPv4() || IsTor() || IsInternal()) {
687  return ToStringIP() + ":" + ToStringPort();
688  } else {
689  return "[" + ToStringIP() + "]:" + ToStringPort();
690  }
691 }
692 
693 std::string CService::ToString() const
694 {
695  return ToStringIPPort();
696 }
697 
699  valid(false)
700 {
701  memset(netmask, 0, sizeof(netmask));
702 }
703 
704 CSubNet::CSubNet(const CNetAddr &addr, int32_t mask)
705 {
706  valid = true;
707  network = addr;
708  // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
709  memset(netmask, 255, sizeof(netmask));
710 
711  // IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
712  const int astartofs = network.IsIPv4() ? 12 : 0;
713 
714  int32_t n = mask;
715  if(n >= 0 && n <= (128 - astartofs*8)) // Only valid if in range of bits of address
716  {
717  n += astartofs*8;
718  // Clear bits [n..127]
719  for (; n < 128; ++n)
720  netmask[n>>3] &= ~(1<<(7-(n&7)));
721  } else
722  valid = false;
723 
724  // Normalize network according to netmask
725  for(int x=0; x<16; ++x)
726  network.ip[x] &= netmask[x];
727 }
728 
729 CSubNet::CSubNet(const CNetAddr &addr, const CNetAddr &mask)
730 {
731  valid = true;
732  network = addr;
733  // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
734  memset(netmask, 255, sizeof(netmask));
735 
736  // IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
737  const int astartofs = network.IsIPv4() ? 12 : 0;
738 
739  for(int x=astartofs; x<16; ++x)
740  netmask[x] = mask.ip[x];
741 
742  // Normalize network according to netmask
743  for(int x=0; x<16; ++x)
744  network.ip[x] &= netmask[x];
745 }
746 
748  valid(addr.IsValid())
749 {
750  memset(netmask, 255, sizeof(netmask));
751  network = addr;
752 }
753 
758 bool CSubNet::Match(const CNetAddr &addr) const
759 {
760  if (!valid || !addr.IsValid())
761  return false;
762  for(int x=0; x<16; ++x)
763  if ((addr.ip[x] & netmask[x]) != network.ip[x])
764  return false;
765  return true;
766 }
767 
772 static inline int NetmaskBits(uint8_t x)
773 {
774  switch(x) {
775  case 0x00: return 0;
776  case 0x80: return 1;
777  case 0xc0: return 2;
778  case 0xe0: return 3;
779  case 0xf0: return 4;
780  case 0xf8: return 5;
781  case 0xfc: return 6;
782  case 0xfe: return 7;
783  case 0xff: return 8;
784  default: return -1;
785  }
786 }
787 
788 std::string CSubNet::ToString() const
789 {
790  /* Parse binary 1{n}0{N-n} to see if mask can be represented as /n */
791  int cidr = 0;
792  bool valid_cidr = true;
793  int n = network.IsIPv4() ? 12 : 0;
794  for (; n < 16 && netmask[n] == 0xff; ++n)
795  cidr += 8;
796  if (n < 16) {
797  int bits = NetmaskBits(netmask[n]);
798  if (bits < 0)
799  valid_cidr = false;
800  else
801  cidr += bits;
802  ++n;
803  }
804  for (; n < 16 && valid_cidr; ++n)
805  if (netmask[n] != 0x00)
806  valid_cidr = false;
807 
808  /* Format output */
809  std::string strNetmask;
810  if (valid_cidr) {
811  strNetmask = strprintf("%u", cidr);
812  } else {
813  if (network.IsIPv4())
814  strNetmask = strprintf("%u.%u.%u.%u", netmask[12], netmask[13], netmask[14], netmask[15]);
815  else
816  strNetmask = strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
817  netmask[0] << 8 | netmask[1], netmask[2] << 8 | netmask[3],
818  netmask[4] << 8 | netmask[5], netmask[6] << 8 | netmask[7],
819  netmask[8] << 8 | netmask[9], netmask[10] << 8 | netmask[11],
820  netmask[12] << 8 | netmask[13], netmask[14] << 8 | netmask[15]);
821  }
822 
823  return network.ToString() + "/" + strNetmask;
824 }
825 
826 bool CSubNet::IsValid() const
827 {
828  return valid;
829 }
830 
831 bool operator==(const CSubNet& a, const CSubNet& b)
832 {
833  return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
834 }
835 
836 bool operator<(const CSubNet& a, const CSubNet& b)
837 {
838  return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
839 }
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:511
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:648
std::string ToStringPort() const
Definition: netaddress.cpp:679
std::string EncodeBase32(const unsigned char *pch, size_t len)
unsigned short GetPort() const
Definition: netaddress.cpp:609
bool IsLocal() const
Definition: netaddress.cpp:223
bool IsRFC4380() const
Definition: netaddress.cpp:180
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:27
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1067
bool IsIPv6() const
Definition: netaddress.cpp:129
static const int NET_UNKNOWN
Definition: netaddress.cpp:499
uint16_t port
Definition: netaddress.h:143
friend bool operator==(const CService &a, const CService &b)
Definition: netaddress.cpp:614
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid)
bool IsInternal() const
Definition: netaddress.cpp:305
CNetAddr network
Network (base) address.
Definition: netaddress.h:106
bool IsBindAny() const
Definition: netaddress.cpp:114
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:376
std::string ToString() const
Definition: netaddress.cpp:351
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:394
enum Network GetNetwork() const
Definition: netaddress.cpp:310
bool IsRFC2544() const
Definition: netaddress.cpp:142
bool IsRFC4862() const
Definition: netaddress.cpp:185
bool IsValid() const
Definition: netaddress.cpp:247
std::vector< unsigned char > GetGroup() const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:413
bool IsIPv4() const
Definition: netaddress.cpp:124
bool IsRFC5737() const
Definition: netaddress.cpp:157
static const unsigned char pchOnionCat[]
Definition: netaddress.cpp:12
uint32_t scopeId
Definition: netaddress.h:36
static int GetExtNetwork(const CNetAddr *addr)
Definition: netaddress.cpp:501
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:636
std::string ToStringIP() const
Definition: netaddress.cpp:327
const char * name
Definition: rest.cpp:39
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:140
friend bool operator<(const CService &a, const CService &b)
Definition: netaddress.cpp:619
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:669
Network
Definition: netaddress.h:20
static const unsigned char g_internal_prefix[]
Definition: netaddress.cpp:15
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:831
bool IsRFC3849() const
Definition: netaddress.cpp:164
bool IsRoutable() const
Definition: netaddress.cpp:295
uint64_t GetHash() const
Definition: netaddress.cpp:489
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:110
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:758
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:108
static int NetmaskBits(uint8_t x)
Definition: netaddress.cpp:772
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:361
bool IsValid() const
Definition: netaddress.cpp:826
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:788
bool IsRFC3927() const
Definition: netaddress.cpp:147
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:356
void * memcpy(void *a, const void *b, size_t c)
std::string ToStringIPPort() const
Definition: netaddress.cpp:684
static const int NET_TEREDO
Definition: netaddress.cpp:500
unsigned char ip[16]
Definition: netaddress.h:35
friend bool operator<(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:836
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
bool IsRFC7343() const
Definition: netaddress.cpp:207
std::string ToString() const
Definition: netaddress.cpp:693
bool IsRFC4843() const
Definition: netaddress.cpp:202
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:595
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
static const unsigned char pchIPv4[12]
Definition: netaddress.cpp:11
bool IsTor() const
Definition: netaddress.cpp:218
bool IsRFC3964() const
Definition: netaddress.cpp:169
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:22