Changeset View
Changeset View
Standalone View
Standalone View
src/netaddress.cpp
Show All 12 Lines | |||||
#include "utilstrencodings.h" | #include "utilstrencodings.h" | ||||
static const uint8_t pchIPv4[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}; | static const uint8_t pchIPv4[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}; | ||||
static const uint8_t pchOnionCat[] = {0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43}; | static const uint8_t pchOnionCat[] = {0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43}; | ||||
// 0xFD + sha256("bitcoin")[0:5] | // 0xFD + sha256("bitcoin")[0:5] | ||||
static const uint8_t g_internal_prefix[] = {0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24}; | static const uint8_t g_internal_prefix[] = {0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24}; | ||||
void CNetAddr::Init() { | CNetAddr::CNetAddr() { | ||||
memset(ip, 0, sizeof(ip)); | memset(ip, 0, sizeof(ip)); | ||||
scopeId = 0; | scopeId = 0; | ||||
} | } | ||||
void CNetAddr::SetIP(const CNetAddr &ipIn) { | void CNetAddr::SetIP(const CNetAddr &ipIn) { | ||||
memcpy(ip, ipIn.ip, sizeof(ip)); | memcpy(ip, ipIn.ip, sizeof(ip)); | ||||
} | } | ||||
Show All 35 Lines | if (strName.size() > 6 && | ||||
for (unsigned int i = 0; i < 16 - sizeof(pchOnionCat); i++) { | for (unsigned int i = 0; i < 16 - sizeof(pchOnionCat); i++) { | ||||
ip[i + sizeof(pchOnionCat)] = vchAddr[i]; | ip[i + sizeof(pchOnionCat)] = vchAddr[i]; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
CNetAddr::CNetAddr() { | |||||
Init(); | |||||
} | |||||
CNetAddr::CNetAddr(const struct in_addr &ipv4Addr) { | CNetAddr::CNetAddr(const struct in_addr &ipv4Addr) { | ||||
SetRaw(NET_IPV4, (const uint8_t *)&ipv4Addr); | SetRaw(NET_IPV4, (const uint8_t *)&ipv4Addr); | ||||
} | } | ||||
CNetAddr::CNetAddr(const struct in6_addr &ipv6Addr, const uint32_t scope) { | CNetAddr::CNetAddr(const struct in6_addr &ipv6Addr, const uint32_t scope) { | ||||
SetRaw(NET_IPV6, (const uint8_t *)&ipv6Addr); | SetRaw(NET_IPV6, (const uint8_t *)&ipv6Addr); | ||||
scopeId = scope; | scopeId = scope; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 192 Lines • ▼ Show 20 Lines | |||||
std::string CNetAddr::ToString() const { | std::string CNetAddr::ToString() const { | ||||
return ToStringIP(); | return ToStringIP(); | ||||
} | } | ||||
bool operator==(const CNetAddr &a, const CNetAddr &b) { | bool operator==(const CNetAddr &a, const CNetAddr &b) { | ||||
return (memcmp(a.ip, b.ip, 16) == 0); | return (memcmp(a.ip, b.ip, 16) == 0); | ||||
} | } | ||||
bool operator!=(const CNetAddr &a, const CNetAddr &b) { | |||||
return (memcmp(a.ip, b.ip, 16) != 0); | |||||
} | |||||
bool operator<(const CNetAddr &a, const CNetAddr &b) { | bool operator<(const CNetAddr &a, const CNetAddr &b) { | ||||
return (memcmp(a.ip, b.ip, 16) < 0); | return (memcmp(a.ip, b.ip, 16) < 0); | ||||
} | } | ||||
bool CNetAddr::GetInAddr(struct in_addr *pipv4Addr) const { | bool CNetAddr::GetInAddr(struct in_addr *pipv4Addr) const { | ||||
if (!IsIPv4()) return false; | if (!IsIPv4()) return false; | ||||
memcpy(pipv4Addr, ip + 12, 4); | memcpy(pipv4Addr, ip + 12, 4); | ||||
return true; | return true; | ||||
▲ Show 20 Lines • Show All 161 Lines • ▼ Show 20 Lines | switch (theirNet) { | ||||
return REACH_IPV4; | return REACH_IPV4; | ||||
// either from Tor, or don't care about our address | // either from Tor, or don't care about our address | ||||
case NET_TOR: | case NET_TOR: | ||||
return REACH_PRIVATE; | return REACH_PRIVATE; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void CService::Init() { | CService::CService() : port(0) {} | ||||
port = 0; | |||||
} | |||||
CService::CService() { | |||||
Init(); | |||||
} | |||||
CService::CService(const CNetAddr &cip, unsigned short portIn) | CService::CService(const CNetAddr &cip, unsigned short portIn) | ||||
: CNetAddr(cip), port(portIn) {} | : CNetAddr(cip), port(portIn) {} | ||||
CService::CService(const struct in_addr &ipv4Addr, unsigned short portIn) | CService::CService(const struct in_addr &ipv4Addr, unsigned short portIn) | ||||
: CNetAddr(ipv4Addr), port(portIn) {} | : CNetAddr(ipv4Addr), port(portIn) {} | ||||
CService::CService(const struct in6_addr &ipv6Addr, unsigned short portIn) | CService::CService(const struct in6_addr &ipv6Addr, unsigned short portIn) | ||||
Show All 26 Lines | |||||
unsigned short CService::GetPort() const { | unsigned short CService::GetPort() const { | ||||
return port; | return port; | ||||
} | } | ||||
bool operator==(const CService &a, const CService &b) { | bool operator==(const CService &a, const CService &b) { | ||||
return (CNetAddr)a == (CNetAddr)b && a.port == b.port; | return (CNetAddr)a == (CNetAddr)b && a.port == b.port; | ||||
} | } | ||||
bool operator!=(const CService &a, const CService &b) { | |||||
return (CNetAddr)a != (CNetAddr)b || a.port != b.port; | |||||
} | |||||
bool operator<(const CService &a, const CService &b) { | bool operator<(const CService &a, const CService &b) { | ||||
return (CNetAddr)a < (CNetAddr)b || | return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || | ||||
((CNetAddr)a == (CNetAddr)b && a.port < b.port); | (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && | ||||
a.port < b.port); | |||||
} | } | ||||
bool CService::GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const { | bool CService::GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const { | ||||
if (IsIPv4()) { | if (IsIPv4()) { | ||||
if (*addrlen < (socklen_t)sizeof(struct sockaddr_in)) return false; | if (*addrlen < (socklen_t)sizeof(struct sockaddr_in)) return false; | ||||
*addrlen = sizeof(struct sockaddr_in); | *addrlen = sizeof(struct sockaddr_in); | ||||
struct sockaddr_in *paddrin = (struct sockaddr_in *)paddr; | struct sockaddr_in *paddrin = (struct sockaddr_in *)paddr; | ||||
memset(paddrin, 0, *addrlen); | memset(paddrin, 0, *addrlen); | ||||
▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | for (int x = 0; x < 16; ++x) | ||||
if ((addr.ip[x] & netmask[x]) != network.ip[x]) return false; | if ((addr.ip[x] & netmask[x]) != network.ip[x]) return false; | ||||
return true; | return true; | ||||
} | } | ||||
static inline int NetmaskBits(uint8_t x) { | static inline int NetmaskBits(uint8_t x) { | ||||
switch (x) { | switch (x) { | ||||
case 0x00: | case 0x00: | ||||
return 0; | return 0; | ||||
break; | |||||
case 0x80: | case 0x80: | ||||
return 1; | return 1; | ||||
break; | |||||
case 0xc0: | case 0xc0: | ||||
return 2; | return 2; | ||||
break; | |||||
case 0xe0: | case 0xe0: | ||||
return 3; | return 3; | ||||
break; | |||||
case 0xf0: | case 0xf0: | ||||
return 4; | return 4; | ||||
break; | |||||
case 0xf8: | case 0xf8: | ||||
return 5; | return 5; | ||||
break; | |||||
case 0xfc: | case 0xfc: | ||||
return 6; | return 6; | ||||
break; | |||||
case 0xfe: | case 0xfe: | ||||
return 7; | return 7; | ||||
break; | |||||
case 0xff: | case 0xff: | ||||
return 8; | return 8; | ||||
break; | |||||
default: | default: | ||||
return -1; | return -1; | ||||
break; | |||||
} | } | ||||
} | } | ||||
std::string CSubNet::ToString() const { | std::string CSubNet::ToString() const { | ||||
/* Parse binary 1{n}0{N-n} to see if mask can be represented as /n */ | /* Parse binary 1{n}0{N-n} to see if mask can be represented as /n */ | ||||
int cidr = 0; | int cidr = 0; | ||||
bool valid_cidr = true; | bool valid_cidr = true; | ||||
int n = network.IsIPv4() ? 12 : 0; | int n = network.IsIPv4() ? 12 : 0; | ||||
Show All 34 Lines | bool CSubNet::IsValid() const { | ||||
return valid; | return valid; | ||||
} | } | ||||
bool operator==(const CSubNet &a, const CSubNet &b) { | bool operator==(const CSubNet &a, const CSubNet &b) { | ||||
return a.valid == b.valid && a.network == b.network && | return a.valid == b.valid && a.network == b.network && | ||||
!memcmp(a.netmask, b.netmask, 16); | !memcmp(a.netmask, b.netmask, 16); | ||||
} | } | ||||
bool operator!=(const CSubNet &a, const CSubNet &b) { | |||||
return !(a == b); | |||||
} | |||||
bool operator<(const CSubNet &a, const CSubNet &b) { | bool operator<(const CSubNet &a, const CSubNet &b) { | ||||
return (a.network < b.network || | return (a.network < b.network || | ||||
(a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0)); | (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0)); | ||||
} | } |