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};
15 static const unsigned char g_internal_prefix[] = { 0xFD, 0xF8, 0x2F, 0x54, 0xFE, 0x3A };
19 memset(
ip, 0,
sizeof(
ip));
40 assert(!
"invalid network");
49 unsigned char hash[32] = {};
51 memcpy(
ip, g_internal_prefix,
sizeof(g_internal_prefix));
52 memcpy(
ip +
sizeof(g_internal_prefix), hash,
sizeof(
ip) -
sizeof(g_internal_prefix));
58 if (strName.size()>6 && strName.substr(strName.size() - 6, 6) ==
".onion") {
59 std::vector<unsigned char> vchAddr =
DecodeBase32(strName.substr(0, strName.size() - 6).c_str());
60 if (vchAddr.size() != 16-
sizeof(pchOnionCat))
62 memcpy(
ip, pchOnionCat,
sizeof(pchOnionCat));
63 for (
unsigned int i=0; i<16-
sizeof(pchOnionCat); i++)
64 ip[i +
sizeof(pchOnionCat)] = vchAddr[i];
88 return (memcmp(
ip, pchIPv4,
sizeof(pchIPv4)) == 0);
138 static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
139 return (memcmp(
ip, pchRFC6052,
sizeof(pchRFC6052)) == 0);
149 static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
150 return (memcmp(
ip, pchRFC4862,
sizeof(pchRFC4862)) == 0);
155 return ((
GetByte(15) & 0xFE) == 0xFC);
160 static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
161 return (memcmp(
ip, pchRFC6145,
sizeof(pchRFC6145)) == 0);
171 return (memcmp(
ip, pchOnionCat,
sizeof(pchOnionCat)) == 0);
181 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
182 if (memcmp(
ip, pchLocal, 16) == 0)
196 if (memcmp(
ip, pchIPv4+3,
sizeof(pchIPv4)-3) == 0)
200 unsigned char ipNone6[16] = {};
201 if (memcmp(
ip, ipNone6, 16) == 0)
214 uint32_t ipNone = INADDR_NONE;
215 if (memcmp(
ip+12, &ipNone, 4) == 0)
220 if (memcmp(
ip+12, &ipNone, 4) == 0)
234 return memcmp(
ip, g_internal_prefix,
sizeof(g_internal_prefix)) == 0;
259 return EncodeBase32(
ip +
sizeof(g_internal_prefix),
sizeof(
ip) -
sizeof(g_internal_prefix)) +
".internal";
261 struct sockaddr_storage sockaddr;
262 socklen_t socklen =
sizeof(sockaddr);
263 if (serv.
GetSockAddr((
struct sockaddr*)&sockaddr, &socklen)) {
264 char name[1025] =
"";
265 if (!getnameinfo((
const struct sockaddr*)&sockaddr, socklen,
name,
sizeof(
name),
nullptr, 0, NI_NUMERICHOST))
266 return std::string(
name);
271 return strprintf(
"%x:%x:%x:%x:%x:%x:%x:%x",
285 return (memcmp(a.
ip, b.
ip, 16) == 0);
290 return (memcmp(a.
ip, b.
ip, 16) < 0);
314 std::vector<unsigned char> vchRet;
329 nStartByte =
sizeof(g_internal_prefix);
330 nBits = (
sizeof(
ip) -
sizeof(g_internal_prefix)) * 8;
355 vchRet.push_back(
GetByte(3) ^ 0xFF);
356 vchRet.push_back(
GetByte(2) ^ 0xFF);
372 vchRet.push_back(nClass);
375 vchRet.push_back(
GetByte(15 - nStartByte));
380 vchRet.push_back(
GetByte(15 - nStartByte) | ((1 << (8 - nBits)) - 1));
389 memcpy(&nRet, &hash,
sizeof(nRet));
395 static const int NET_UNKNOWN =
NET_MAX + 0;
396 static const int NET_TEREDO =
NET_MAX + 1;
397 int static GetExtNetwork(
const CNetAddr *addr)
420 return REACH_UNREACHABLE;
422 int ourNet = GetExtNetwork(
this);
423 int theirNet = GetExtNetwork(paddrPartner);
429 default:
return REACH_DEFAULT;
434 default:
return REACH_DEFAULT;
435 case NET_TEREDO:
return REACH_TEREDO;
437 case NET_IPV6:
return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
441 default:
return REACH_DEFAULT;
447 default:
return REACH_DEFAULT;
448 case NET_TEREDO:
return REACH_TEREDO;
449 case NET_IPV6:
return REACH_IPV6_WEAK;
456 default:
return REACH_DEFAULT;
457 case NET_TEREDO:
return REACH_TEREDO;
458 case NET_IPV6:
return REACH_IPV6_WEAK;
483 assert(addr.sin_family == AF_INET);
488 assert(addr.sin6_family == AF_INET6);
493 switch (paddr->sa_family) {
495 *
this =
CService(*(
const struct sockaddr_in*)paddr);
498 *
this =
CService(*(
const struct sockaddr_in6*)paddr);
512 return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.
port == b.
port;
517 return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (
static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.
port < b.
port);
523 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in))
525 *addrlen =
sizeof(
struct sockaddr_in);
526 struct sockaddr_in *paddrin = (
struct sockaddr_in*)paddr;
527 memset(paddrin, 0, *addrlen);
530 paddrin->sin_family = AF_INET;
531 paddrin->sin_port = htons(
port);
535 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in6))
537 *addrlen =
sizeof(
struct sockaddr_in6);
538 struct sockaddr_in6 *paddrin6 = (
struct sockaddr_in6*)paddr;
539 memset(paddrin6, 0, *addrlen);
542 paddrin6->sin6_scope_id =
scopeId;
543 paddrin6->sin6_family = AF_INET6;
544 paddrin6->sin6_port = htons(
port);
552 std::vector<unsigned char> vKey;
555 vKey[16] =
port / 0x100;
556 vKey[17] =
port & 0x0FF;
596 if(n >= 0 && n <= (128 - astartofs*8))
601 netmask[n>>3] &= ~(1<<(7-(n&7)));
606 for(
int x=0; x<16; ++x)
620 for(
int x=astartofs; x<16; ++x)
624 for(
int x=0; x<16; ++x)
629 valid(addr.IsValid())
639 for(
int x=0; x<16; ++x)
645 static inline int NetmaskBits(uint8_t x)
665 bool valid_cidr =
true;
667 for (; n < 16 &&
netmask[n] == 0xff; ++n)
670 int bits = NetmaskBits(
netmask[n]);
677 for (; n < 16 && valid_cidr; ++n)
682 std::string strNetmask;
689 strNetmask =
strprintf(
"%x:%x:%x:%x:%x:%x:%x:%x",
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
std::string ToStringPort() const
unsigned short GetPort() const
void SetIP(const CNetAddr &ip)
bool operator==(const CNetAddr &a, const CNetAddr &b)
CNetAddr network
Network (base) address.
bool GetInAddr(struct in_addr *pipv4Addr) const
std::string ToString() const
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
enum Network GetNetwork() const
std::vector< unsigned char > GetGroup() const
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
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.
std::vector< unsigned char > GetKey() const
unsigned int GetByte(int n) const
bool valid
Is this value valid? (only used to signal parse errors)
A hasher class for SHA3-256.
bool Match(const CNetAddr &addr) const
bool operator<(const CNetAddr &a, const CNetAddr &b)
uint8_t netmask[16]
Netmask, in network byte order.
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
std::string ToString() const
void * memcpy(void *a, const void *b, size_t c)
std::string ToStringIPPort() const
CSHA3 & Write(const unsigned char *data, size_t len)
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.