7 #include "config/bitcoin-config.h"
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};
20 memset(
ip, 0,
sizeof(
ip));
41 assert(!
"invalid network");
47 if (strName.size()>6 && strName.substr(strName.size() - 6, 6) ==
".onion") {
48 std::vector<unsigned char> vchAddr =
DecodeBase32(strName.substr(0, strName.size() - 6).c_str());
49 if (vchAddr.size() != 16-
sizeof(pchOnionCat))
51 memcpy(
ip, pchOnionCat,
sizeof(pchOnionCat));
52 for (
unsigned int i=0; i<16-
sizeof(pchOnionCat); i++)
53 ip[i +
sizeof(pchOnionCat)] = vchAddr[i];
82 return (memcmp(
ip, pchIPv4,
sizeof(pchIPv4)) == 0);
132 static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
133 return (memcmp(
ip, pchRFC6052,
sizeof(pchRFC6052)) == 0);
143 static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
144 return (memcmp(
ip, pchRFC4862,
sizeof(pchRFC4862)) == 0);
149 return ((
GetByte(15) & 0xFE) == 0xFC);
154 static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
155 return (memcmp(
ip, pchRFC6145,
sizeof(pchRFC6145)) == 0);
165 return (memcmp(
ip, pchOnionCat,
sizeof(pchOnionCat)) == 0);
175 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
176 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)
211 uint32_t ipNone = INADDR_NONE;
212 if (memcmp(
ip+12, &ipNone, 4) == 0)
217 if (memcmp(
ip+12, &ipNone, 4) == 0)
248 struct sockaddr_storage sockaddr;
249 socklen_t socklen =
sizeof(sockaddr);
250 if (serv.
GetSockAddr((
struct sockaddr*)&sockaddr, &socklen)) {
251 char name[1025] =
"";
252 if (!getnameinfo((
const struct sockaddr*)&sockaddr, socklen, name,
sizeof(name), NULL, 0, NI_NUMERICHOST))
253 return std::string(name);
258 return strprintf(
"%x:%x:%x:%x:%x:%x:%x:%x",
272 return (memcmp(a.
ip, b.
ip, 16) == 0);
277 return (memcmp(a.
ip, b.
ip, 16) != 0);
282 return (memcmp(a.
ip, b.
ip, 16) < 0);
303 std::vector<unsigned char> vchRet;
338 vchRet.push_back(
GetByte(3) ^ 0xFF);
339 vchRet.push_back(
GetByte(2) ^ 0xFF);
355 vchRet.push_back(nClass);
358 vchRet.push_back(
GetByte(15 - nStartByte));
363 vchRet.push_back(
GetByte(15 - nStartByte) | ((1 << (8 - nBits)) - 1));
372 memcpy(&nRet, &hash,
sizeof(nRet));
378 static const int NET_UNKNOWN =
NET_MAX + 0;
379 static const int NET_TEREDO =
NET_MAX + 1;
380 int static GetExtNetwork(
const CNetAddr *addr)
403 return REACH_UNREACHABLE;
405 int ourNet = GetExtNetwork(
this);
406 int theirNet = GetExtNetwork(paddrPartner);
412 default:
return REACH_DEFAULT;
417 default:
return REACH_DEFAULT;
418 case NET_TEREDO:
return REACH_TEREDO;
420 case NET_IPV6:
return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
424 default:
return REACH_DEFAULT;
426 case NET_TOR:
return REACH_PRIVATE;
430 default:
return REACH_DEFAULT;
431 case NET_TEREDO:
return REACH_TEREDO;
432 case NET_IPV6:
return REACH_IPV6_WEAK;
439 default:
return REACH_DEFAULT;
440 case NET_TEREDO:
return REACH_TEREDO;
441 case NET_IPV6:
return REACH_IPV6_WEAK;
443 case NET_TOR:
return REACH_PRIVATE;
472 assert(addr.sin_family == AF_INET);
477 assert(addr.sin6_family == AF_INET6);
482 switch (paddr->sa_family) {
484 *
this =
CService(*(
const struct sockaddr_in*)paddr);
487 *
this =
CService(*(
const struct sockaddr_in6*)paddr);
517 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in))
519 *addrlen =
sizeof(
struct sockaddr_in);
520 struct sockaddr_in *paddrin = (
struct sockaddr_in*)paddr;
521 memset(paddrin, 0, *addrlen);
524 paddrin->sin_family = AF_INET;
525 paddrin->sin_port = htons(
port);
529 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in6))
531 *addrlen =
sizeof(
struct sockaddr_in6);
532 struct sockaddr_in6 *paddrin6 = (
struct sockaddr_in6*)paddr;
533 memset(paddrin6, 0, *addrlen);
536 paddrin6->sin6_scope_id =
scopeId;
537 paddrin6->sin6_family = AF_INET6;
538 paddrin6->sin6_port = htons(
port);
546 std::vector<unsigned char> vKey;
549 vKey[16] =
port / 0x100;
550 vKey[17] =
port & 0x0FF;
595 if(n >= 0 && n <= (128 - astartofs*8))
600 netmask[n>>3] &= ~(1<<(7-(n&7)));
605 for(
int x=0; x<16; ++x)
619 for(
int x=astartofs; x<16; ++x)
623 for(
int x=0; x<16; ++x)
628 valid(addr.IsValid())
638 for(
int x=0; x<16; ++x)
644 static inline int NetmaskBits(uint8_t x)
647 case 0x00:
return 0;
break;
648 case 0x80:
return 1;
break;
649 case 0xc0:
return 2;
break;
650 case 0xe0:
return 3;
break;
651 case 0xf0:
return 4;
break;
652 case 0xf8:
return 5;
break;
653 case 0xfc:
return 6;
break;
654 case 0xfe:
return 7;
break;
655 case 0xff:
return 8;
break;
656 default:
return -1;
break;
664 bool valid_cidr =
true;
666 for (; n < 16 &&
netmask[n] == 0xff; ++n)
669 int bits = NetmaskBits(
netmask[n]);
676 for (; n < 16 && valid_cidr; ++n)
681 std::string strNetmask;
688 strNetmask =
strprintf(
"%x:%x:%x:%x:%x:%x:%x:%x",
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
std::string ToStringIP() const
void SetIP(const CNetAddr &ip)
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
std::string ToString() const
bool GetInAddr(struct in_addr *pipv4Addr) const
std::vector< unsigned char > GetGroup() const
bool SetSpecial(const std::string &strName)
int GetReachabilityFrom(const CNetAddr *paddrPartner=NULL) const
Calculates a metric for how reachable (*this) is from a given partner.
unsigned int GetByte(int n) const
enum Network GetNetwork() const
A combination of a network address (CNetAddr) and a (TCP) port.
std::string ToStringIPPort() const
std::string ToString() const
void SetPort(unsigned short portIn)
unsigned short GetPort() const
bool SetSockAddr(const struct sockaddr *paddr)
std::string ToStringPort() const
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
std::vector< unsigned char > GetKey() const
bool valid
Is this value valid? (only used to signal parse errors)
CNetAddr network
Network (base) address.
uint8_t netmask[16]
Netmask, in network byte order.
std::string ToString() const
bool Match(const CNetAddr &addr) const
void * memcpy(void *a, const void *b, size_t c)
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
bool operator==(const CNetAddr &a, const CNetAddr &b)
bool operator!=(const CNetAddr &a, const CNetAddr &b)
bool operator<(const CNetAddr &a, const CNetAddr &b)
string EncodeBase32(const unsigned char *pch, size_t len)
vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid)