16 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
17 static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};
20 static const unsigned char g_internal_prefix[] = { 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 };
24 memset(
ip, 0,
sizeof(
ip));
45 assert(!
"invalid network");
54 unsigned char hash[32] = {};
55 CSHA256().
Write((
const unsigned char*)name.data(), name.size()).Finalize(hash);
56 memcpy(
ip, g_internal_prefix,
sizeof(g_internal_prefix));
57 memcpy(
ip +
sizeof(g_internal_prefix), hash,
sizeof(
ip) -
sizeof(g_internal_prefix));
63 if (strName.size()>6 && strName.substr(strName.size() - 6, 6) ==
".onion") {
64 std::vector<unsigned char> vchAddr =
DecodeBase32(strName.substr(0, strName.size() - 6).c_str());
65 if (vchAddr.size() != 16-
sizeof(pchOnionCat))
67 memcpy(
ip, pchOnionCat,
sizeof(pchOnionCat));
68 for (
unsigned int i=0; i<16-
sizeof(pchOnionCat); i++)
69 ip[i +
sizeof(pchOnionCat)] = vchAddr[i];
98 return (memcmp(
ip, pchIPv4,
sizeof(pchIPv4)) == 0);
148 static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
149 return (memcmp(
ip, pchRFC6052,
sizeof(pchRFC6052)) == 0);
159 static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
160 return (memcmp(
ip, pchRFC4862,
sizeof(pchRFC4862)) == 0);
165 return ((
GetByte(15) & 0xFE) == 0xFC);
170 static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
171 return (memcmp(
ip, pchRFC6145,
sizeof(pchRFC6145)) == 0);
181 return (memcmp(
ip, pchOnionCat,
sizeof(pchOnionCat)) == 0);
191 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
192 if (memcmp(
ip, pchLocal, 16) == 0)
206 if (memcmp(
ip, pchIPv4+3,
sizeof(pchIPv4)-3) == 0)
210 unsigned char ipNone6[16] = {};
211 if (memcmp(
ip, ipNone6, 16) == 0)
224 uint32_t ipNone = INADDR_NONE;
225 if (memcmp(
ip+12, &ipNone, 4) == 0)
230 if (memcmp(
ip+12, &ipNone, 4) == 0)
244 return memcmp(
ip, g_internal_prefix,
sizeof(g_internal_prefix)) == 0;
269 return EncodeBase32(
ip +
sizeof(g_internal_prefix),
sizeof(
ip) -
sizeof(g_internal_prefix)) +
".internal";
271 struct sockaddr_storage sockaddr;
272 socklen_t socklen =
sizeof(sockaddr);
273 if (serv.
GetSockAddr((
struct sockaddr*)&sockaddr, &socklen)) {
274 char name[1025] =
"";
275 if (!getnameinfo((
const struct sockaddr*)&sockaddr, socklen, name,
sizeof(name),
nullptr, 0, NI_NUMERICHOST))
276 return std::string(name);
281 return strprintf(
"%x:%x:%x:%x:%x:%x:%x:%x",
295 return (memcmp(a.
ip, b.
ip, 16) == 0);
300 return (memcmp(a.
ip, b.
ip, 16) != 0);
305 return (memcmp(a.
ip, b.
ip, 16) < 0);
326 std::vector<unsigned char> vchRet;
341 nStartByte =
sizeof(g_internal_prefix);
342 nBits = (
sizeof(
ip) -
sizeof(g_internal_prefix)) * 8;
367 vchRet.push_back(
GetByte(3) ^ 0xFF);
368 vchRet.push_back(
GetByte(2) ^ 0xFF);
384 vchRet.push_back(nClass);
387 vchRet.push_back(
GetByte(15 - nStartByte));
392 vchRet.push_back(
GetByte(15 - nStartByte) | ((1 << (8 - nBits)) - 1));
401 memcpy(&nRet, &hash,
sizeof(nRet));
407 static const int NET_UNKNOWN =
NET_MAX + 0;
408 static const int NET_TEREDO =
NET_MAX + 1;
409 int static GetExtNetwork(
const CNetAddr *addr)
432 return REACH_UNREACHABLE;
434 int ourNet = GetExtNetwork(
this);
435 int theirNet = GetExtNetwork(paddrPartner);
441 default:
return REACH_DEFAULT;
446 default:
return REACH_DEFAULT;
447 case NET_TEREDO:
return REACH_TEREDO;
449 case NET_IPV6:
return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
453 default:
return REACH_DEFAULT;
455 case NET_TOR:
return REACH_PRIVATE;
459 default:
return REACH_DEFAULT;
460 case NET_TEREDO:
return REACH_TEREDO;
461 case NET_IPV6:
return REACH_IPV6_WEAK;
468 default:
return REACH_DEFAULT;
469 case NET_TEREDO:
return REACH_TEREDO;
470 case NET_IPV6:
return REACH_IPV6_WEAK;
472 case NET_TOR:
return REACH_PRIVATE;
501 assert(addr.sin_family == AF_INET);
506 assert(addr.sin6_family == AF_INET6);
511 switch (paddr->sa_family) {
513 *
this =
CService(*(
const struct sockaddr_in*)paddr);
516 *
this =
CService(*(
const struct sockaddr_in6*)paddr);
546 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in))
548 *addrlen =
sizeof(
struct sockaddr_in);
549 struct sockaddr_in *paddrin = (
struct sockaddr_in*)paddr;
550 memset(paddrin, 0, *addrlen);
553 paddrin->sin_family = AF_INET;
554 paddrin->sin_port = htons(
port);
558 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in6))
560 *addrlen =
sizeof(
struct sockaddr_in6);
561 struct sockaddr_in6 *paddrin6 = (
struct sockaddr_in6*)paddr;
562 memset(paddrin6, 0, *addrlen);
565 paddrin6->sin6_scope_id =
scopeId;
566 paddrin6->sin6_family = AF_INET6;
567 paddrin6->sin6_port = htons(
port);
575 std::vector<unsigned char> vKey;
578 vKey[16] =
port / 0x100;
579 vKey[17] =
port & 0x0FF;
619 if(n >= 0 && n <= (128 - astartofs*8))
624 netmask[n>>3] &= ~(1<<(7-(n&7)));
629 for(
int x=0; x<16; ++x)
643 for(
int x=astartofs; x<16; ++x)
647 for(
int x=0; x<16; ++x)
662 for(
int x=0; x<16; ++x)
668 static inline int NetmaskBits(uint8_t x)
671 case 0x00:
return 0;
break;
672 case 0x80:
return 1;
break;
673 case 0xc0:
return 2;
break;
674 case 0xe0:
return 3;
break;
675 case 0xf0:
return 4;
break;
676 case 0xf8:
return 5;
break;
677 case 0xfc:
return 6;
break;
678 case 0xfe:
return 7;
break;
679 case 0xff:
return 8;
break;
680 default:
return -1;
break;
688 bool valid_cidr =
true;
690 for (; n < 16 &&
netmask[n] == 0xff; ++n)
693 int bits = NetmaskBits(netmask[n]);
700 for (; n < 16 && valid_cidr; ++n)
701 if (netmask[n] != 0x00)
705 std::string strNetmask;
710 strNetmask =
strprintf(
"%u.%u.%u.%u", netmask[12], netmask[13], netmask[14], netmask[15]);
712 strNetmask =
strprintf(
"%x:%x:%x:%x:%x:%x:%x:%x",
713 netmask[0] << 8 | netmask[1], netmask[2] << 8 | netmask[3],
714 netmask[4] << 8 | netmask[5], netmask[6] << 8 | netmask[7],
715 netmask[8] << 8 | netmask[9], netmask[10] << 8 | netmask[11],
716 netmask[12] << 8 | netmask[13], netmask[14] << 8 | netmask[15]);
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
CSHA256 & Write(const unsigned char *data, size_t len)
std::string ToStringPort() const
unsigned short GetPort() const
void SetIP(const CNetAddr &ip)
friend bool operator==(const CService &a, const CService &b)
CNetAddr network
Network (base) address.
bool GetInAddr(struct in_addr *pipv4Addr) const
std::string ToString() const
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
enum Network GetNetwork() const
std::vector< unsigned char > GetGroup() const
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
friend bool operator!=(const CSubNet &a, const CSubNet &b)
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
friend bool operator!=(const CService &a, const CService &b)
std::string ToStringIP() const
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid)
A combination of a network address (CNetAddr) and a (TCP) port.
friend bool operator<(const CService &a, const CService &b)
std::vector< unsigned char > GetKey() const
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
unsigned int GetByte(int n) const
friend bool operator==(const CSubNet &a, const CSubNet &b)
bool valid
Is this value valid? (only used to signal parse errors)
bool Match(const CNetAddr &addr) const
uint8_t netmask[16]
Netmask, in network byte order.
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
std::string ToString() const
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
void * memcpy(void *a, const void *b, size_t c)
std::string ToStringIPPort() const
friend bool operator<(const CSubNet &a, const CSubNet &b)
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
std::string EncodeBase32(const unsigned char *pch, size_t len)
bool SetSpecial(const std::string &strName)
std::string ToString() const
bool SetSockAddr(const struct sockaddr *paddr)
bool SetInternal(const std::string &name)
Transform an arbitrary string into a non-routable ipv6 address.
A hasher class for SHA-256.