15 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
16 static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};
19 static const unsigned char g_internal_prefix[] = { 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 };
23 memset(
ip, 0,
sizeof(
ip));
44 assert(!
"invalid network");
53 unsigned char hash[32] = {};
54 CSHA256().
Write((
const unsigned char*)name.data(), name.size()).Finalize(hash);
55 memcpy(
ip, g_internal_prefix,
sizeof(g_internal_prefix));
56 memcpy(
ip +
sizeof(g_internal_prefix), hash,
sizeof(
ip) -
sizeof(g_internal_prefix));
62 if (strName.size()>6 && strName.substr(strName.size() - 6, 6) ==
".onion") {
63 std::vector<unsigned char> vchAddr =
DecodeBase32(strName.substr(0, strName.size() - 6).c_str());
64 if (vchAddr.size() != 16-
sizeof(pchOnionCat))
66 memcpy(
ip, pchOnionCat,
sizeof(pchOnionCat));
67 for (
unsigned int i=0; i<16-
sizeof(pchOnionCat); i++)
68 ip[i +
sizeof(pchOnionCat)] = vchAddr[i];
97 return (memcmp(
ip, pchIPv4,
sizeof(pchIPv4)) == 0);
147 static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
148 return (memcmp(
ip, pchRFC6052,
sizeof(pchRFC6052)) == 0);
158 static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
159 return (memcmp(
ip, pchRFC4862,
sizeof(pchRFC4862)) == 0);
164 return ((
GetByte(15) & 0xFE) == 0xFC);
169 static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
170 return (memcmp(
ip, pchRFC6145,
sizeof(pchRFC6145)) == 0);
180 return (memcmp(
ip, pchOnionCat,
sizeof(pchOnionCat)) == 0);
190 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
191 if (memcmp(
ip, pchLocal, 16) == 0)
205 if (memcmp(
ip, pchIPv4+3,
sizeof(pchIPv4)-3) == 0)
209 unsigned char ipNone6[16] = {};
210 if (memcmp(
ip, ipNone6, 16) == 0)
223 uint32_t ipNone = INADDR_NONE;
224 if (memcmp(
ip+12, &ipNone, 4) == 0)
229 if (memcmp(
ip+12, &ipNone, 4) == 0)
243 return memcmp(
ip, g_internal_prefix,
sizeof(g_internal_prefix)) == 0;
268 return EncodeBase32(
ip +
sizeof(g_internal_prefix),
sizeof(
ip) -
sizeof(g_internal_prefix)) +
".internal";
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);
280 return strprintf(
"%x:%x:%x:%x:%x:%x:%x:%x",
294 return (memcmp(a.
ip, b.
ip, 16) == 0);
299 return (memcmp(a.
ip, b.
ip, 16) != 0);
304 return (memcmp(a.
ip, b.
ip, 16) < 0);
325 std::vector<unsigned char> vchRet;
340 nStartByte =
sizeof(g_internal_prefix);
341 nBits = (
sizeof(
ip) -
sizeof(g_internal_prefix)) * 8;
366 vchRet.push_back(
GetByte(3) ^ 0xFF);
367 vchRet.push_back(
GetByte(2) ^ 0xFF);
383 vchRet.push_back(nClass);
386 vchRet.push_back(
GetByte(15 - nStartByte));
391 vchRet.push_back(
GetByte(15 - nStartByte) | ((1 << (8 - nBits)) - 1));
400 memcpy(&nRet, &hash,
sizeof(nRet));
406 static const int NET_UNKNOWN =
NET_MAX + 0;
407 static const int NET_TEREDO =
NET_MAX + 1;
408 int static GetExtNetwork(
const CNetAddr *addr)
431 return REACH_UNREACHABLE;
433 int ourNet = GetExtNetwork(
this);
434 int theirNet = GetExtNetwork(paddrPartner);
440 default:
return REACH_DEFAULT;
445 default:
return REACH_DEFAULT;
446 case NET_TEREDO:
return REACH_TEREDO;
448 case NET_IPV6:
return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
452 default:
return REACH_DEFAULT;
454 case NET_TOR:
return REACH_PRIVATE;
458 default:
return REACH_DEFAULT;
459 case NET_TEREDO:
return REACH_TEREDO;
460 case NET_IPV6:
return REACH_IPV6_WEAK;
467 default:
return REACH_DEFAULT;
468 case NET_TEREDO:
return REACH_TEREDO;
469 case NET_IPV6:
return REACH_IPV6_WEAK;
471 case NET_TOR:
return REACH_PRIVATE;
500 assert(addr.sin_family == AF_INET);
505 assert(addr.sin6_family == AF_INET6);
510 switch (paddr->sa_family) {
512 *
this =
CService(*(
const struct sockaddr_in*)paddr);
515 *
this =
CService(*(
const struct sockaddr_in6*)paddr);
545 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in))
547 *addrlen =
sizeof(
struct sockaddr_in);
548 struct sockaddr_in *paddrin = (
struct sockaddr_in*)paddr;
549 memset(paddrin, 0, *addrlen);
552 paddrin->sin_family = AF_INET;
553 paddrin->sin_port = htons(
port);
557 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in6))
559 *addrlen =
sizeof(
struct sockaddr_in6);
560 struct sockaddr_in6 *paddrin6 = (
struct sockaddr_in6*)paddr;
561 memset(paddrin6, 0, *addrlen);
564 paddrin6->sin6_scope_id =
scopeId;
565 paddrin6->sin6_family = AF_INET6;
566 paddrin6->sin6_port = htons(
port);
574 std::vector<unsigned char> vKey;
577 vKey[16] =
port / 0x100;
578 vKey[17] =
port & 0x0FF;
618 if(n >= 0 && n <= (128 - astartofs*8))
623 netmask[n>>3] &= ~(1<<(7-(n&7)));
628 for(
int x=0;
x<16; ++
x)
642 for(
int x=astartofs;
x<16; ++
x)
646 for(
int x=0;
x<16; ++
x)
661 for(
int x=0;
x<16; ++
x)
667 static inline int NetmaskBits(uint8_t
x)
670 case 0x00:
return 0;
break;
671 case 0x80:
return 1;
break;
672 case 0xc0:
return 2;
break;
673 case 0xe0:
return 3;
break;
674 case 0xf0:
return 4;
break;
675 case 0xf8:
return 5;
break;
676 case 0xfc:
return 6;
break;
677 case 0xfe:
return 7;
break;
678 case 0xff:
return 8;
break;
679 default:
return -1;
break;
687 bool valid_cidr =
true;
689 for (; n < 16 &&
netmask[n] == 0xff; ++n)
692 int bits = NetmaskBits(netmask[n]);
699 for (; n < 16 && valid_cidr; ++n)
700 if (netmask[n] != 0x00)
704 std::string strNetmask;
709 strNetmask =
strprintf(
"%u.%u.%u.%u", netmask[12], netmask[13], netmask[14], netmask[15]);
711 strNetmask =
strprintf(
"%x:%x:%x:%x:%x:%x:%x:%x",
712 netmask[0] << 8 | netmask[1], netmask[2] << 8 | netmask[3],
713 netmask[4] << 8 | netmask[5], netmask[6] << 8 | netmask[7],
714 netmask[8] << 8 | netmask[9], netmask[10] << 8 | netmask[11],
715 netmask[12] << 8 | netmask[13], netmask[14] << 8 | netmask[15]);
CSHA256 & Write(const unsigned char *data, size_t len)
unsigned short GetPort() const
void SetIP(const CNetAddr &ip)
std::string ToStringIP() const
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
std::string ToStringIPPort() const
friend bool operator==(const CService &a, const CService &b)
std::string ToString() const
assert(len-trim+(2 *lenIndices)<=WIDTH)
CNetAddr network
Network (base) address.
bool Match(const CNetAddr &addr) const
unsigned int GetByte(int n) const
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
friend bool operator!=(const CSubNet &a, const CSubNet &b)
friend bool operator!=(const CService &a, const CService &b)
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::string ToString() const
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
friend bool operator==(const CSubNet &a, const CSubNet &b)
bool valid
Is this value valid? (only used to signal parse errors)
bool GetInAddr(struct in_addr *pipv4Addr) const
std::vector< unsigned char > GetGroup() 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)
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
void * memcpy(void *a, const void *b, size_t c)
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
std::string ToStringPort() 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.
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
std::vector< unsigned char > GetKey() const
enum Network GetNetwork() const