Bitcoin ABC  0.22.13
P2P Digital Currency
netaddress.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 
8 #include <hash.h>
9 #include <tinyformat.h>
10 #include <util/asmap.h>
11 #include <util/strencodings.h>
12 
13 static const uint8_t pchIPv4[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
14 static const uint8_t pchOnionCat[] = {0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43};
15 
16 // 0xFD + sha256("bitcoin")[0:5]
17 static const uint8_t g_internal_prefix[] = {0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24};
18 
25  memset(ip, 0, sizeof(ip));
26 }
27 
28 void CNetAddr::SetIP(const CNetAddr &ipIn) {
29  memcpy(ip, ipIn.ip, sizeof(ip));
30 }
31 
32 void CNetAddr::SetRaw(Network network, const uint8_t *ip_in) {
33  switch (network) {
34  case NET_IPV4:
35  memcpy(ip, pchIPv4, 12);
36  memcpy(ip + 12, ip_in, 4);
37  break;
38  case NET_IPV6:
39  memcpy(ip, ip_in, 16);
40  break;
41  default:
42  assert(!"invalid network");
43  }
44 }
45 
60 bool CNetAddr::SetInternal(const std::string &name) {
61  if (name.empty()) {
62  return false;
63  }
64  uint8_t hash[32] = {};
65  CSHA256().Write((const uint8_t *)name.data(), name.size()).Finalize(hash);
66  memcpy(ip, g_internal_prefix, sizeof(g_internal_prefix));
67  memcpy(ip + sizeof(g_internal_prefix), hash,
68  sizeof(ip) - sizeof(g_internal_prefix));
69  return true;
70 }
71 
82 bool CNetAddr::SetSpecial(const std::string &strName) {
83  if (strName.size() > 6 &&
84  strName.substr(strName.size() - 6, 6) == ".onion") {
85  std::vector<uint8_t> vchAddr =
86  DecodeBase32(strName.substr(0, strName.size() - 6).c_str());
87  if (vchAddr.size() != 16 - sizeof(pchOnionCat)) {
88  return false;
89  }
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  }
94  return true;
95  }
96  return false;
97 }
98 
99 CNetAddr::CNetAddr(const struct in_addr &ipv4Addr) {
100  SetRaw(NET_IPV4, (const uint8_t *)&ipv4Addr);
101 }
102 
103 CNetAddr::CNetAddr(const struct in6_addr &ipv6Addr, const uint32_t scope) {
104  SetRaw(NET_IPV6, (const uint8_t *)&ipv6Addr);
105  scopeId = scope;
106 }
107 
108 unsigned int CNetAddr::GetByte(int n) const {
109  return ip[15 - n];
110 }
111 
112 bool CNetAddr::IsBindAny() const {
113  const int cmplen = IsIPv4() ? 4 : 16;
114  for (int i = 0; i < cmplen; ++i) {
115  if (GetByte(i)) {
116  return false;
117  }
118  }
119 
120  return true;
121 }
122 
123 bool CNetAddr::IsIPv4() const {
124  return (memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0);
125 }
126 
127 bool CNetAddr::IsIPv6() const {
128  return !IsIPv4() && !IsTor() && !IsInternal();
129 }
130 
131 bool CNetAddr::IsRFC1918() const {
132  return IsIPv4() &&
133  (GetByte(3) == 10 || (GetByte(3) == 192 && GetByte(2) == 168) ||
134  (GetByte(3) == 172 && (GetByte(2) >= 16 && GetByte(2) <= 31)));
135 }
136 
137 bool CNetAddr::IsRFC2544() const {
138  return IsIPv4() && GetByte(3) == 198 &&
139  (GetByte(2) == 18 || GetByte(2) == 19);
140 }
141 
142 bool CNetAddr::IsRFC3927() const {
143  return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
144 }
145 
146 bool CNetAddr::IsRFC6598() const {
147  return IsIPv4() && GetByte(3) == 100 && GetByte(2) >= 64 &&
148  GetByte(2) <= 127;
149 }
150 
151 bool CNetAddr::IsRFC5737() const {
152  return IsIPv4() &&
153  ((GetByte(3) == 192 && GetByte(2) == 0 && GetByte(1) == 2) ||
154  (GetByte(3) == 198 && GetByte(2) == 51 && GetByte(1) == 100) ||
155  (GetByte(3) == 203 && GetByte(2) == 0 && GetByte(1) == 113));
156 }
157 
158 bool CNetAddr::IsRFC3849() const {
159  return GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x0D &&
160  GetByte(12) == 0xB8;
161 }
162 
163 bool CNetAddr::IsRFC3964() const {
164  return (GetByte(15) == 0x20 && GetByte(14) == 0x02);
165 }
166 
167 bool CNetAddr::IsRFC6052() const {
168  static const uint8_t pchRFC6052[] = {0, 0x64, 0xFF, 0x9B, 0, 0,
169  0, 0, 0, 0, 0, 0};
170  return (memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0);
171 }
172 
173 bool CNetAddr::IsRFC4380() const {
174  return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0 &&
175  GetByte(12) == 0);
176 }
177 
178 bool CNetAddr::IsRFC4862() const {
179  static const uint8_t pchRFC4862[] = {0xFE, 0x80, 0, 0, 0, 0, 0, 0};
180  return (memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0);
181 }
182 
183 bool CNetAddr::IsRFC4193() const {
184  return ((GetByte(15) & 0xFE) == 0xFC);
185 }
186 
187 bool CNetAddr::IsRFC6145() const {
188  static const uint8_t pchRFC6145[] = {0, 0, 0, 0, 0, 0,
189  0, 0, 0xFF, 0xFF, 0, 0};
190  return (memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0);
191 }
192 
193 bool CNetAddr::IsRFC4843() const {
194  return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 &&
195  (GetByte(12) & 0xF0) == 0x10);
196 }
197 
198 bool CNetAddr::IsRFC7343() const {
199  return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 &&
200  (GetByte(12) & 0xF0) == 0x20);
201 }
202 
203 bool CNetAddr::IsHeNet() const {
204  return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x04 &&
205  GetByte(12) == 0x70);
206 }
207 
214 bool CNetAddr::IsTor() const {
215  return (memcmp(ip, pchOnionCat, sizeof(pchOnionCat)) == 0);
216 }
217 
218 bool CNetAddr::IsLocal() const {
219  // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
220  if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0)) {
221  return true;
222  }
223 
224  // IPv6 loopback (::1/128)
225  static const uint8_t pchLocal[16] = {0, 0, 0, 0, 0, 0, 0, 0,
226  0, 0, 0, 0, 0, 0, 0, 1};
227  if (memcmp(ip, pchLocal, 16) == 0) {
228  return true;
229  }
230 
231  return false;
232 }
233 
244 bool CNetAddr::IsValid() const {
245  // Cleanup 3-byte shifted addresses caused by garbage in size field of addr
246  // messages from versions before 0.2.9 checksum.
247  // Two consecutive addr messages look like this:
248  // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26
249  // addr26 addr26... so if the first length field is garbled, it reads the
250  // second batch of addr misaligned by 3 bytes.
251  if (memcmp(ip, pchIPv4 + 3, sizeof(pchIPv4) - 3) == 0) {
252  return false;
253  }
254 
255  // unspecified IPv6 address (::/128)
256  uint8_t ipNone6[16] = {};
257  if (memcmp(ip, ipNone6, 16) == 0) {
258  return false;
259  }
260 
261  // documentation IPv6 address
262  if (IsRFC3849()) {
263  return false;
264  }
265 
266  if (IsInternal()) {
267  return false;
268  }
269 
270  if (IsIPv4()) {
271  // INADDR_NONE
272  uint32_t ipNone = INADDR_NONE;
273  if (memcmp(ip + 12, &ipNone, 4) == 0) {
274  return false;
275  }
276 
277  // 0
278  ipNone = 0;
279  if (memcmp(ip + 12, &ipNone, 4) == 0) {
280  return false;
281  }
282  }
283 
284  return true;
285 }
286 
296 bool CNetAddr::IsRoutable() const {
297  return IsValid() &&
298  !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() ||
299  IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) ||
300  IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
301 }
302 
308 bool CNetAddr::IsInternal() const {
309  return memcmp(ip, g_internal_prefix, sizeof(g_internal_prefix)) == 0;
310 }
311 
313  if (IsInternal()) {
314  return NET_INTERNAL;
315  }
316 
317  if (!IsRoutable()) {
318  return NET_UNROUTABLE;
319  }
320 
321  if (IsIPv4()) {
322  return NET_IPV4;
323  }
324 
325  if (IsTor()) {
326  return NET_ONION;
327  }
328 
329  return NET_IPV6;
330 }
331 
332 std::string CNetAddr::ToStringIP() const {
333  if (IsTor()) {
334  return EncodeBase32(&ip[6], 10) + ".onion";
335  }
336  if (IsInternal()) {
337  return EncodeBase32(ip + sizeof(g_internal_prefix),
338  sizeof(ip) - sizeof(g_internal_prefix)) +
339  ".internal";
340  }
341  CService serv(*this, 0);
342  struct sockaddr_storage sockaddr;
343  socklen_t socklen = sizeof(sockaddr);
344  if (serv.GetSockAddr((struct sockaddr *)&sockaddr, &socklen)) {
345  char name[1025] = "";
346  if (!getnameinfo((const struct sockaddr *)&sockaddr, socklen, name,
347  sizeof(name), nullptr, 0, NI_NUMERICHOST)) {
348  return std::string(name);
349  }
350  }
351  if (IsIPv4()) {
352  return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1),
353  GetByte(0));
354  }
355 
356  return strprintf("%x:%x:%x:%x:%x:%x:%x:%x", GetByte(15) << 8 | GetByte(14),
357  GetByte(13) << 8 | GetByte(12),
358  GetByte(11) << 8 | GetByte(10),
359  GetByte(9) << 8 | GetByte(8), GetByte(7) << 8 | GetByte(6),
360  GetByte(5) << 8 | GetByte(4), GetByte(3) << 8 | GetByte(2),
361  GetByte(1) << 8 | GetByte(0));
362 }
363 
364 std::string CNetAddr::ToString() const {
365  return ToStringIP();
366 }
367 
368 bool operator==(const CNetAddr &a, const CNetAddr &b) {
369  return (memcmp(a.ip, b.ip, 16) == 0);
370 }
371 
372 bool operator<(const CNetAddr &a, const CNetAddr &b) {
373  return (memcmp(a.ip, b.ip, 16) < 0);
374 }
375 
386 bool CNetAddr::GetInAddr(struct in_addr *pipv4Addr) const {
387  if (!IsIPv4()) {
388  return false;
389  }
390  memcpy(pipv4Addr, ip + 12, 4);
391  return true;
392 }
393 
404 bool CNetAddr::GetIn6Addr(struct in6_addr *pipv6Addr) const {
405  if (!IsIPv6()) {
406  return false;
407  }
408  memcpy(pipv6Addr, ip, 16);
409  return true;
410 }
411 
413  return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() ||
414  IsRFC3964() || IsRFC4380());
415 }
416 
417 uint32_t CNetAddr::GetLinkedIPv4() const {
418  if (IsIPv4() || IsRFC6145() || IsRFC6052()) {
419  // IPv4, mapped IPv4, SIIT translated IPv4: the IPv4 address is the last
420  // 4 bytes of the address
421  return ReadBE32(ip + 12);
422  } else if (IsRFC3964()) {
423  // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
424  return ReadBE32(ip + 2);
425  } else if (IsRFC4380()) {
426  // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the
427  // address, but bitflipped
428  return ~ReadBE32(ip + 12);
429  }
430  assert(false);
431 }
432 
433 uint32_t CNetAddr::GetNetClass() const {
434  uint32_t net_class = NET_IPV6;
435  if (IsLocal()) {
436  net_class = 255;
437  }
438  if (IsInternal()) {
439  net_class = NET_INTERNAL;
440  } else if (!IsRoutable()) {
441  net_class = NET_UNROUTABLE;
442  } else if (HasLinkedIPv4()) {
443  net_class = NET_IPV4;
444  } else if (IsTor()) {
445  net_class = NET_ONION;
446  }
447  return net_class;
448 }
449 
450 uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
451  uint32_t net_class = GetNetClass();
452  if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
453  return 0; // Indicates not found, safe because AS0 is reserved per
454  // RFC7607.
455  }
456  std::vector<bool> ip_bits(128);
457  if (HasLinkedIPv4()) {
458  // For lookup, treat as if it was just an IPv4 address (pchIPv4 prefix +
459  // IPv4 bits)
460  for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
461  for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
462  ip_bits[byte_i * 8 + bit_i] =
463  (pchIPv4[byte_i] >> (7 - bit_i)) & 1;
464  }
465  }
466  uint32_t ipv4 = GetLinkedIPv4();
467  for (int i = 0; i < 32; ++i) {
468  ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
469  }
470  } else {
471  // Use all 128 bits of the IPv6 address otherwise
472  for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
473  uint8_t cur_byte = GetByte(15 - byte_i);
474  for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
475  ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
476  }
477  }
478  }
479  uint32_t mapped_as = Interpret(asmap, ip_bits);
480  return mapped_as;
481 }
482 
493 std::vector<uint8_t> CNetAddr::GetGroup(const std::vector<bool> &asmap) const {
494  std::vector<uint8_t> vchRet;
495  uint32_t net_class = GetNetClass();
496  // If non-empty asmap is supplied and the address is IPv4/IPv6,
497  // return ASN to be used for bucketing.
498  uint32_t asn = GetMappedAS(asmap);
499  if (asn != 0) { // Either asmap was empty, or address has non-asmappable net
500  // class (e.g. TOR).
501  vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in
502  // the same bucket
503  for (int i = 0; i < 4; i++) {
504  vchRet.push_back((asn >> (8 * i)) & 0xFF);
505  }
506  return vchRet;
507  }
508 
509  vchRet.push_back(net_class);
510  int nStartByte = 0;
511  int nBits = 16;
512 
513  if (IsLocal()) {
514  // all local addresses belong to the same group
515  nBits = 0;
516  } else if (IsInternal()) {
517  // all internal-usage addresses get their own group
518  nStartByte = sizeof(g_internal_prefix);
519  nBits = (sizeof(ip) - sizeof(g_internal_prefix)) * 8;
520  } else if (!IsRoutable()) {
521  // all other unroutable addresses belong to the same group
522  nBits = 0;
523  } else if (HasLinkedIPv4()) {
524  // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
525  uint32_t ipv4 = GetLinkedIPv4();
526  vchRet.push_back((ipv4 >> 24) & 0xFF);
527  vchRet.push_back((ipv4 >> 16) & 0xFF);
528  return vchRet;
529  } else if (IsTor()) {
530  nStartByte = 6;
531  nBits = 4;
532  } else if (IsHeNet()) {
533  // for he.net, use /36 groups
534  nBits = 36;
535  } else {
536  // for the rest of the IPv6 network, use /32 groups
537  nBits = 32;
538  }
539 
540  // push our ip onto vchRet byte by byte...
541  while (nBits >= 8) {
542  vchRet.push_back(GetByte(15 - nStartByte));
543  nStartByte++;
544  nBits -= 8;
545  }
546  // ...for the last byte, push nBits and for the rest of the byte push 1's
547  if (nBits > 0) {
548  vchRet.push_back(GetByte(15 - nStartByte) | ((1 << (8 - nBits)) - 1));
549  }
550 
551  return vchRet;
552 }
553 
554 uint64_t CNetAddr::GetHash() const {
555  uint256 hash = Hash(&ip[0], &ip[16]);
556  uint64_t nRet;
557  memcpy(&nRet, &hash, sizeof(nRet));
558  return nRet;
559 }
560 
561 // private extensions to enum Network, only returned by GetExtNetwork, and only
562 // used in GetReachabilityFrom
563 static const int NET_UNKNOWN = NET_MAX + 0;
564 static const int NET_TEREDO = NET_MAX + 1;
565 static int GetExtNetwork(const CNetAddr *addr) {
566  if (addr == nullptr) {
567  return NET_UNKNOWN;
568  }
569  if (addr->IsRFC4380()) {
570  return NET_TEREDO;
571  }
572  return addr->GetNetwork();
573 }
574 
576 int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const {
577  enum Reachability {
578  REACH_UNREACHABLE,
579  REACH_DEFAULT,
580  REACH_TEREDO,
581  REACH_IPV6_WEAK,
582  REACH_IPV4,
583  REACH_IPV6_STRONG,
584  REACH_PRIVATE
585  };
586 
587  if (!IsRoutable() || IsInternal()) {
588  return REACH_UNREACHABLE;
589  }
590 
591  int ourNet = GetExtNetwork(this);
592  int theirNet = GetExtNetwork(paddrPartner);
593  bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
594 
595  switch (theirNet) {
596  case NET_IPV4:
597  switch (ourNet) {
598  default:
599  return REACH_DEFAULT;
600  case NET_IPV4:
601  return REACH_IPV4;
602  }
603  case NET_IPV6:
604  switch (ourNet) {
605  default:
606  return REACH_DEFAULT;
607  case NET_TEREDO:
608  return REACH_TEREDO;
609  case NET_IPV4:
610  return REACH_IPV4;
611  // only prefer giving our IPv6 address if it's not tunnelled
612  case NET_IPV6:
613  return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
614  }
615  case NET_ONION:
616  switch (ourNet) {
617  default:
618  return REACH_DEFAULT;
619  // Tor users can connect to IPv4 as well
620  case NET_IPV4:
621  return REACH_IPV4;
622  case NET_ONION:
623  return REACH_PRIVATE;
624  }
625  case NET_TEREDO:
626  switch (ourNet) {
627  default:
628  return REACH_DEFAULT;
629  case NET_TEREDO:
630  return REACH_TEREDO;
631  case NET_IPV6:
632  return REACH_IPV6_WEAK;
633  case NET_IPV4:
634  return REACH_IPV4;
635  }
636  case NET_UNKNOWN:
637  case NET_UNROUTABLE:
638  default:
639  switch (ourNet) {
640  default:
641  return REACH_DEFAULT;
642  case NET_TEREDO:
643  return REACH_TEREDO;
644  case NET_IPV6:
645  return REACH_IPV6_WEAK;
646  case NET_IPV4:
647  return REACH_IPV4;
648  // either from Tor, or don't care about our address
649  case NET_ONION:
650  return REACH_PRIVATE;
651  }
652  }
653 }
654 
655 CService::CService() : port(0) {}
656 
657 CService::CService(const CNetAddr &cip, unsigned short portIn)
658  : CNetAddr(cip), port(portIn) {}
659 
660 CService::CService(const struct in_addr &ipv4Addr, unsigned short portIn)
661  : CNetAddr(ipv4Addr), port(portIn) {}
662 
663 CService::CService(const struct in6_addr &ipv6Addr, unsigned short portIn)
664  : CNetAddr(ipv6Addr), port(portIn) {}
665 
666 CService::CService(const struct sockaddr_in &addr)
667  : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port)) {
668  assert(addr.sin_family == AF_INET);
669 }
670 
671 CService::CService(const struct sockaddr_in6 &addr)
672  : CNetAddr(addr.sin6_addr, addr.sin6_scope_id),
673  port(ntohs(addr.sin6_port)) {
674  assert(addr.sin6_family == AF_INET6);
675 }
676 
677 bool CService::SetSockAddr(const struct sockaddr *paddr) {
678  switch (paddr->sa_family) {
679  case AF_INET:
680  *this =
681  CService(*reinterpret_cast<const struct sockaddr_in *>(paddr));
682  return true;
683  case AF_INET6:
684  *this =
685  CService(*reinterpret_cast<const struct sockaddr_in6 *>(paddr));
686  return true;
687  default:
688  return false;
689  }
690 }
691 
692 unsigned short CService::GetPort() const {
693  return port;
694 }
695 
696 bool operator==(const CService &a, const CService &b) {
697  return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) &&
698  a.port == b.port;
699 }
700 
701 bool operator<(const CService &a, const CService &b) {
702  return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) ||
703  (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) &&
704  a.port < b.port);
705 }
706 
719 bool CService::GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const {
720  if (IsIPv4()) {
721  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in)) {
722  return false;
723  }
724  *addrlen = sizeof(struct sockaddr_in);
725  struct sockaddr_in *paddrin =
726  reinterpret_cast<struct sockaddr_in *>(paddr);
727  memset(paddrin, 0, *addrlen);
728  if (!GetInAddr(&paddrin->sin_addr)) {
729  return false;
730  }
731  paddrin->sin_family = AF_INET;
732  paddrin->sin_port = htons(port);
733  return true;
734  }
735  if (IsIPv6()) {
736  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6)) {
737  return false;
738  }
739  *addrlen = sizeof(struct sockaddr_in6);
740  struct sockaddr_in6 *paddrin6 =
741  reinterpret_cast<struct sockaddr_in6 *>(paddr);
742  memset(paddrin6, 0, *addrlen);
743  if (!GetIn6Addr(&paddrin6->sin6_addr)) {
744  return false;
745  }
746  paddrin6->sin6_scope_id = scopeId;
747  paddrin6->sin6_family = AF_INET6;
748  paddrin6->sin6_port = htons(port);
749  return true;
750  }
751  return false;
752 }
753 
757 std::vector<uint8_t> CService::GetKey() const {
758  std::vector<uint8_t> vKey;
759  vKey.resize(18);
760  memcpy(vKey.data(), ip, 16);
761  // most significant byte of our port
762  vKey[16] = port / 0x100;
763  // least significant byte of our port
764  vKey[17] = port & 0x0FF;
765  return vKey;
766 }
767 
768 std::string CService::ToStringPort() const {
769  return strprintf("%u", port);
770 }
771 
772 std::string CService::ToStringIPPort() const {
773  if (IsIPv4() || IsTor() || IsInternal()) {
774  return ToStringIP() + ":" + ToStringPort();
775  } else {
776  return "[" + ToStringIP() + "]:" + ToStringPort();
777  }
778 }
779 
780 std::string CService::ToString() const {
781  return ToStringIPPort();
782 }
783 
784 CSubNet::CSubNet() : valid(false) {
785  memset(netmask, 0, sizeof(netmask));
786 }
787 
788 CSubNet::CSubNet(const CNetAddr &addr, int32_t mask) {
789  valid = true;
790  network = addr;
791  // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
792  memset(netmask, 255, sizeof(netmask));
793 
794  // IPv4 addresses start at offset 12, and first 12 bytes must match, so just
795  // offset n
796  const int astartofs = network.IsIPv4() ? 12 : 0;
797 
798  // Only valid if in range of bits of address
799  int32_t n = mask;
800  if (n >= 0 && n <= (128 - astartofs * 8)) {
801  n += astartofs * 8;
802  // Clear bits [n..127]
803  for (; n < 128; ++n) {
804  netmask[n >> 3] &= ~(1 << (7 - (n & 7)));
805  }
806  } else {
807  valid = false;
808  }
809 
810  // Normalize network according to netmask
811  for (int x = 0; x < 16; ++x) {
812  network.ip[x] &= netmask[x];
813  }
814 }
815 
816 CSubNet::CSubNet(const CNetAddr &addr, const CNetAddr &mask) {
817  valid = true;
818  network = addr;
819  // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
820  memset(netmask, 255, sizeof(netmask));
821 
822  // IPv4 addresses start at offset 12, and first 12 bytes must match, so just
823  // offset n
824  const int astartofs = network.IsIPv4() ? 12 : 0;
825 
826  for (int x = astartofs; x < 16; ++x) {
827  netmask[x] = mask.ip[x];
828  }
829 
830  // Normalize network according to netmask
831  for (int x = 0; x < 16; ++x) {
832  network.ip[x] &= netmask[x];
833  }
834 }
835 
836 CSubNet::CSubNet(const CNetAddr &addr) : valid(addr.IsValid()) {
837  memset(netmask, 255, sizeof(netmask));
838  network = addr;
839 }
840 
845 bool CSubNet::Match(const CNetAddr &addr) const {
846  if (!valid || !addr.IsValid()) {
847  return false;
848  }
849  for (int x = 0; x < 16; ++x) {
850  if ((addr.ip[x] & netmask[x]) != network.ip[x]) {
851  return false;
852  }
853  }
854  return true;
855 }
856 
861 static inline int NetmaskBits(uint8_t x) {
862  switch (x) {
863  case 0x00:
864  return 0;
865  case 0x80:
866  return 1;
867  case 0xc0:
868  return 2;
869  case 0xe0:
870  return 3;
871  case 0xf0:
872  return 4;
873  case 0xf8:
874  return 5;
875  case 0xfc:
876  return 6;
877  case 0xfe:
878  return 7;
879  case 0xff:
880  return 8;
881  default:
882  return -1;
883  }
884 }
885 
886 std::string CSubNet::ToString() const {
887  /* Parse binary 1{n}0{N-n} to see if mask can be represented as /n */
888  int cidr = 0;
889  bool valid_cidr = true;
890  int n = network.IsIPv4() ? 12 : 0;
891  for (; n < 16 && netmask[n] == 0xff; ++n) {
892  cidr += 8;
893  }
894  if (n < 16) {
895  int bits = NetmaskBits(netmask[n]);
896  if (bits < 0) {
897  valid_cidr = false;
898  } else {
899  cidr += bits;
900  }
901  ++n;
902  }
903  for (; n < 16 && valid_cidr; ++n) {
904  if (netmask[n] != 0x00) {
905  valid_cidr = false;
906  }
907  }
908 
909  /* Format output */
910  std::string strNetmask;
911  if (valid_cidr) {
912  strNetmask = strprintf("%u", cidr);
913  } else {
914  if (network.IsIPv4()) {
915  strNetmask = strprintf("%u.%u.%u.%u", netmask[12], netmask[13],
916  netmask[14], netmask[15]);
917  } else {
918  strNetmask = strprintf(
919  "%x:%x:%x:%x:%x:%x:%x:%x", netmask[0] << 8 | netmask[1],
920  netmask[2] << 8 | netmask[3], netmask[4] << 8 | netmask[5],
921  netmask[6] << 8 | netmask[7], netmask[8] << 8 | netmask[9],
922  netmask[10] << 8 | netmask[11], netmask[12] << 8 | netmask[13],
923  netmask[14] << 8 | netmask[15]);
924  }
925  }
926 
927  return network.ToString() + "/" + strNetmask;
928 }
929 
930 bool CSubNet::IsValid() const {
931  return valid;
932 }
933 
934 bool operator==(const CSubNet &a, const CSubNet &b) {
935  return a.valid == b.valid && a.network == b.network &&
936  !memcmp(a.netmask, b.netmask, 16);
937 }
938 
939 bool operator<(const CSubNet &a, const CSubNet &b) {
940  return (a.network < b.network ||
941  (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
942 }
943 
944 bool SanityCheckASMap(const std::vector<bool> &asmap) {
945  // For IP address lookups, the input is 128 bits
946  return SanityCheckASMap(asmap, 128);
947 }
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:576
std::string ToStringPort() const
Definition: netaddress.cpp:768
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Definition: netaddress.cpp:412
unsigned short GetPort() const
Definition: netaddress.cpp:692
static const uint8_t g_internal_prefix[]
Definition: netaddress.cpp:17
bool IsLocal() const
Definition: netaddress.cpp:218
uint8_t ip[16]
Definition: netaddress.h:33
bool IsRFC4380() const
Definition: netaddress.cpp:173
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:28
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
bool IsIPv6() const
Definition: netaddress.cpp:127
static const int NET_UNKNOWN
Definition: netaddress.cpp:563
std::vector< uint8_t > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:493
uint16_t port
Definition: netaddress.h:182
friend bool operator==(const CService &a, const CService &b)
Definition: netaddress.cpp:696
bool IsInternal() const
Definition: netaddress.cpp:308
CNetAddr network
Network (base) address.
Definition: netaddress.h:143
bool IsBindAny() const
Definition: netaddress.cpp:112
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:386
std::string ToString() const
Definition: netaddress.cpp:364
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:404
enum Network GetNetwork() const
Definition: netaddress.cpp:312
bool IsRFC2544() const
Definition: netaddress.cpp:137
bool IsRFC4862() const
Definition: netaddress.cpp:178
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
Definition: netaddress.cpp:450
bool IsValid() const
Definition: netaddress.cpp:244
bool IsIPv4() const
Definition: netaddress.cpp:123
bool IsRFC5737() const
Definition: netaddress.cpp:151
uint32_t scopeId
Definition: netaddress.h:35
static int GetExtNetwork(const CNetAddr *addr)
Definition: netaddress.cpp:565
static const uint8_t pchIPv4[12]
Definition: netaddress.cpp:13
bool IsRFC6145() const
Definition: netaddress.cpp:187
bool IsRFC6052() const
Definition: netaddress.cpp:167
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
Definition: netaddress.cpp:719
std::string ToStringIP() const
Definition: netaddress.cpp:332
const char * name
Definition: rest.cpp:43
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:179
friend bool operator<(const CService &a, const CService &b)
Definition: netaddress.cpp:701
Network
Definition: netaddress.h:19
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:72
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:86
unsigned int GetByte(int n) const
Definition: netaddress.cpp:108
friend bool operator==(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:934
bool IsRFC3849() const
Definition: netaddress.cpp:158
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid)
bool IsRoutable() const
Definition: netaddress.cpp:296
uint64_t GetHash() const
Definition: netaddress.cpp:554
static const uint8_t pchOnionCat[]
Definition: netaddress.cpp:14
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:147
static uint32_t ReadBE32(const uint8_t *ptr)
Definition: common.h:50
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:845
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:145
std::string EncodeBase32(const uint8_t *pch, size_t len)
static int NetmaskBits(uint8_t x)
Definition: netaddress.cpp:861
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:30
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:372
bool IsValid() const
Definition: netaddress.cpp:930
bool IsRFC1918() const
Definition: netaddress.cpp:131
256-bit opaque blob.
Definition: uint256.h:120
bool IsRFC6598() const
Definition: netaddress.cpp:146
bool IsHeNet() const
Definition: netaddress.cpp:203
std::string ToString() const
Definition: netaddress.cpp:886
bool IsRFC3927() const
Definition: netaddress.cpp:142
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:368
std::string ToStringIPPort() const
Definition: netaddress.cpp:772
static const int NET_TEREDO
Definition: netaddress.cpp:564
friend bool operator<(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:939
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
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:82
bool IsRFC7343() const
Definition: netaddress.cpp:198
std::string ToString() const
Definition: netaddress.cpp:780
bool IsRFC4843() const
Definition: netaddress.cpp:193
bool SanityCheckASMap(const std::vector< bool > &asmap)
Definition: netaddress.cpp:944
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Definition: netaddress.cpp:417
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:677
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:60
A hasher class for SHA-256.
Definition: sha256.h:13
bool IsRFC4193() const
Definition: netaddress.cpp:183
std::vector< uint8_t > GetKey() const
Definition: netaddress.cpp:757
uint32_t GetNetClass() const
Definition: netaddress.cpp:433
bool IsTor() const
Definition: netaddress.cpp:214
bool IsRFC3964() const
Definition: netaddress.cpp:163
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:24