Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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