Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/netbase.cpp
| // Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
| // Copyright (c) 2009-2012 The Bitcoin developers | // Copyright (c) 2009-2012 The Bitcoin developers | ||||
| // Distributed under the MIT/X11 software license, see the accompanying | // Distributed under the MIT/X11 software license, see the accompanying | ||||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
| #include "netbase.h" | #include "netbase.h" | ||||
| #include "strlcpy.h" | |||||
| #include "util.h" | #include "util.h" | ||||
| #ifndef WIN32 | #ifndef WIN32 | ||||
| #include <sys/fcntl.h> | #include <sys/fcntl.h> | ||||
| #endif | #endif | ||||
| #include "strlcpy.h" | |||||
| #include <boost/algorithm/string/case_conv.hpp> // for to_lower() | #include <boost/algorithm/string/case_conv.hpp> // for to_lower() | ||||
| #define printf my_printf | #define printf my_printf | ||||
| using namespace std; | |||||
| // Settings | // Settings | ||||
| typedef std::pair<CService, int> proxyType; | typedef std::pair<CService, int> proxyType; | ||||
| static proxyType proxyInfo[NET_MAX]; | static proxyType proxyInfo[NET_MAX]; | ||||
| static proxyType nameproxyInfo; | static proxyType nameproxyInfo; | ||||
| int nConnectTimeout = 5000; | int nConnectTimeout = 5000; | ||||
| bool fNameLookup = false; | bool fNameLookup = false; | ||||
| static const unsigned char pchIPv4[12] = {0, 0, 0, 0, 0, 0, | static const uint8_t pchIPv4[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}; | ||||
| 0, 0, 0, 0, 0xff, 0xff}; | |||||
| enum Network ParseNetwork(std::string net) { | enum Network ParseNetwork(std::string net) { | ||||
| boost::to_lower(net); | boost::to_lower(net); | ||||
| if (net == "ipv4") return NET_IPV4; | if (net == "ipv4") return NET_IPV4; | ||||
| if (net == "ipv6") return NET_IPV6; | if (net == "ipv6") return NET_IPV6; | ||||
| if (net == "tor") return NET_TOR; | if (net == "tor") return NET_TOR; | ||||
| if (net == "i2p") return NET_I2P; | if (net == "i2p") return NET_I2P; | ||||
| return NET_UNROUTABLE; | return NET_UNROUTABLE; | ||||
| } | } | ||||
| void SplitHostPort(std::string in, int &portOut, std::string &hostOut) { | void SplitHostPort(std::string in, int &portOut, std::string &hostOut) { | ||||
| size_t colon = in.find_last_of(':'); | size_t colon = in.find_last_of(':'); | ||||
| // if a : is found, and it either follows a [...], or no other : is in the | // if a : is found, and it either follows a [...], or no other : is in the | ||||
| // string, treat it as port separator | // string, treat it as port separator | ||||
| bool fHaveColon = colon != in.npos; | bool fHaveColon = colon != in.npos; | ||||
| bool fBracketed = | // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is | ||||
| fHaveColon && | // safe | ||||
| (in[0] == '[' && in[colon - 1] == ']'); // if there is a colon, and | bool fBracketed = fHaveColon && (in[0] == '[' && in[colon - 1] == ']'); | ||||
| // in[0]=='[', colon is not 0, | |||||
| // so in[colon-1] is safe | |||||
| bool fMultiColon = | bool fMultiColon = | ||||
| fHaveColon && (in.find_last_of(':', colon - 1) != in.npos); | fHaveColon && (in.find_last_of(':', colon - 1) != in.npos); | ||||
| if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) { | if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) { | ||||
| char *endp = NULL; | char *endp = nullptr; | ||||
| int n = strtol(in.c_str() + colon + 1, &endp, 10); | int n = strtol(in.c_str() + colon + 1, &endp, 10); | ||||
| if (endp && *endp == 0 && n >= 0) { | if (endp && *endp == 0 && n >= 0) { | ||||
| in = in.substr(0, colon); | in = in.substr(0, colon); | ||||
| if (n > 0 && n < 0x10000) portOut = n; | if (n > 0 && n < 0x10000) portOut = n; | ||||
| } | } | ||||
| } | } | ||||
| if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') | if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') | ||||
| hostOut = in.substr(1, in.size() - 2); | hostOut = in.substr(1, in.size() - 2); | ||||
| else | else | ||||
| hostOut = in; | hostOut = in; | ||||
| } | } | ||||
| bool static LookupIntern(const char *pszName, std::vector<CNetAddr> &vIP, | static bool LookupIntern(const char *pszName, std::vector<CNetAddr> &vIP, | ||||
| unsigned int nMaxSolutions, bool fAllowLookup) { | unsigned int nMaxSolutions, bool fAllowLookup) { | ||||
| vIP.clear(); | vIP.clear(); | ||||
| { | { | ||||
| CNetAddr addr; | CNetAddr addr; | ||||
| if (addr.SetSpecial(std::string(pszName))) { | if (addr.SetSpecial(std::string(pszName))) { | ||||
| vIP.push_back(addr); | vIP.push_back(addr); | ||||
| return true; | return true; | ||||
| } | } | ||||
| } | } | ||||
| struct addrinfo aiHint; | struct addrinfo aiHint; | ||||
| memset(&aiHint, 0, sizeof(struct addrinfo)); | memset(&aiHint, 0, sizeof(struct addrinfo)); | ||||
| aiHint.ai_socktype = SOCK_STREAM; | aiHint.ai_socktype = SOCK_STREAM; | ||||
| aiHint.ai_protocol = IPPROTO_TCP; | aiHint.ai_protocol = IPPROTO_TCP; | ||||
| #ifdef WIN32 | #ifdef WIN32 | ||||
| aiHint.ai_family = AF_UNSPEC; | aiHint.ai_family = AF_UNSPEC; | ||||
| aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST; | aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST; | ||||
| #else | #else | ||||
| aiHint.ai_family = AF_UNSPEC; | aiHint.ai_family = AF_UNSPEC; | ||||
| aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST; | aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST; | ||||
| #endif | #endif | ||||
| struct addrinfo *aiRes = NULL; | struct addrinfo *aiRes = nullptr; | ||||
| int nErr = getaddrinfo(pszName, NULL, &aiHint, &aiRes); | int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes); | ||||
| if (nErr) return false; | if (nErr) return false; | ||||
| struct addrinfo *aiTrav = aiRes; | struct addrinfo *aiTrav = aiRes; | ||||
| while (aiTrav != NULL && | while (aiTrav != nullptr && | ||||
| (nMaxSolutions == 0 || vIP.size() < nMaxSolutions)) { | (nMaxSolutions == 0 || vIP.size() < nMaxSolutions)) { | ||||
| if (aiTrav->ai_family == AF_INET) { | if (aiTrav->ai_family == AF_INET) { | ||||
| assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in)); | assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in)); | ||||
| vIP.push_back( | vIP.push_back( | ||||
| CNetAddr(((struct sockaddr_in *)(aiTrav->ai_addr))->sin_addr)); | CNetAddr(((struct sockaddr_in *)(aiTrav->ai_addr))->sin_addr)); | ||||
| } | } | ||||
| if (aiTrav->ai_family == AF_INET6) { | if (aiTrav->ai_family == AF_INET6) { | ||||
| ▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | bool Lookup(const char *pszName, CService &addr, int portDefault, | ||||
| addr = vService[0]; | addr = vService[0]; | ||||
| return true; | return true; | ||||
| } | } | ||||
| bool LookupNumeric(const char *pszName, CService &addr, int portDefault) { | bool LookupNumeric(const char *pszName, CService &addr, int portDefault) { | ||||
| return Lookup(pszName, addr, portDefault, false); | return Lookup(pszName, addr, portDefault, false); | ||||
| } | } | ||||
| bool static Socks4(const CService &addrDest, SOCKET &hSocket) { | static bool Socks4(const CService &addrDest, SOCKET &hSocket) { | ||||
| printf("SOCKS4 connecting %s\n", addrDest.ToString().c_str()); | printf("SOCKS4 connecting %s\n", addrDest.ToString().c_str()); | ||||
| if (!addrDest.IsIPv4()) { | if (!addrDest.IsIPv4()) { | ||||
| closesocket(hSocket); | closesocket(hSocket); | ||||
| return error("Proxy destination is not IPv4"); | return error("Proxy destination is not IPv4"); | ||||
| } | } | ||||
| char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user"; | char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user"; | ||||
| struct sockaddr_in addr; | struct sockaddr_in addr; | ||||
| socklen_t len = sizeof(addr); | socklen_t len = sizeof(addr); | ||||
| Show All 22 Lines | if (pchRet[1] != 0x5a) { | ||||
| if (pchRet[1] != 0x5b) | if (pchRet[1] != 0x5b) | ||||
| printf("ERROR: Proxy returned error %d\n", pchRet[1]); | printf("ERROR: Proxy returned error %d\n", pchRet[1]); | ||||
| return false; | return false; | ||||
| } | } | ||||
| printf("SOCKS4 connected %s\n", addrDest.ToString().c_str()); | printf("SOCKS4 connected %s\n", addrDest.ToString().c_str()); | ||||
| return true; | return true; | ||||
| } | } | ||||
| bool static Socks5(string strDest, int port, SOCKET &hSocket) { | static bool Socks5(std::string strDest, int port, SOCKET &hSocket) { | ||||
| printf("SOCKS5 connecting %s\n", strDest.c_str()); | printf("SOCKS5 connecting %s\n", strDest.c_str()); | ||||
| if (strDest.size() > 255) { | if (strDest.size() > 255) { | ||||
| closesocket(hSocket); | closesocket(hSocket); | ||||
| return error("Hostname too long"); | return error("Hostname too long"); | ||||
| } | } | ||||
| char pszSocks5Init[] = "\5\1\0"; | char pszSocks5Init[] = "\5\1\0"; | ||||
| char *pszSocks5 = pszSocks5Init; | char *pszSocks5 = pszSocks5Init; | ||||
| ssize_t nSize = sizeof(pszSocks5Init) - 1; | ssize_t nSize = sizeof(pszSocks5Init) - 1; | ||||
| ssize_t ret = send(hSocket, pszSocks5, nSize, MSG_NOSIGNAL); | ssize_t ret = send(hSocket, pszSocks5, nSize, MSG_NOSIGNAL); | ||||
| if (ret != nSize) { | if (ret != nSize) { | ||||
| closesocket(hSocket); | closesocket(hSocket); | ||||
| return error("Error sending to proxy"); | return error("Error sending to proxy"); | ||||
| } | } | ||||
| char pchRet1[2]; | char pchRet1[2]; | ||||
| if (recv(hSocket, pchRet1, 2, 0) != 2) { | if (recv(hSocket, pchRet1, 2, 0) != 2) { | ||||
| closesocket(hSocket); | closesocket(hSocket); | ||||
| return error("Error reading proxy response"); | return error("Error reading proxy response"); | ||||
| } | } | ||||
| if (pchRet1[0] != 0x05 || pchRet1[1] != 0x00) { | if (pchRet1[0] != 0x05 || pchRet1[1] != 0x00) { | ||||
| closesocket(hSocket); | closesocket(hSocket); | ||||
| return error("Proxy failed to initialize"); | return error("Proxy failed to initialize"); | ||||
| } | } | ||||
| string strSocks5("\5\1"); | std::string strSocks5("\5\1"); | ||||
| strSocks5 += '\000'; | strSocks5 += '\000'; | ||||
| strSocks5 += '\003'; | strSocks5 += '\003'; | ||||
| strSocks5 += static_cast<char>(std::min((int)strDest.size(), 255)); | strSocks5 += static_cast<char>(std::min((int)strDest.size(), 255)); | ||||
| strSocks5 += strDest; | strSocks5 += strDest; | ||||
| strSocks5 += static_cast<char>((port >> 8) & 0xFF); | strSocks5 += static_cast<char>((port >> 8) & 0xFF); | ||||
| strSocks5 += static_cast<char>((port >> 0) & 0xFF); | strSocks5 += static_cast<char>((port >> 0) & 0xFF); | ||||
| ret = send(hSocket, strSocks5.c_str(), strSocks5.size(), MSG_NOSIGNAL); | ret = send(hSocket, strSocks5.c_str(), strSocks5.size(), MSG_NOSIGNAL); | ||||
| if (ret != (ssize_t)strSocks5.size()) { | if (ret != (ssize_t)strSocks5.size()) { | ||||
| ▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | static bool Socks5(std::string strDest, int port, SOCKET &hSocket) { | ||||
| if (recv(hSocket, pchRet3, 2, 0) != 2) { | if (recv(hSocket, pchRet3, 2, 0) != 2) { | ||||
| closesocket(hSocket); | closesocket(hSocket); | ||||
| return error("Error reading from proxy"); | return error("Error reading from proxy"); | ||||
| } | } | ||||
| printf("SOCKS5 connected %s\n", strDest.c_str()); | printf("SOCKS5 connected %s\n", strDest.c_str()); | ||||
| return true; | return true; | ||||
| } | } | ||||
| bool static ConnectSocketDirectly(const CService &addrConnect, | static bool ConnectSocketDirectly(const CService &addrConnect, | ||||
| SOCKET &hSocketRet, int nTimeout) { | SOCKET &hSocketRet, int nTimeout) { | ||||
| hSocketRet = INVALID_SOCKET; | hSocketRet = INVALID_SOCKET; | ||||
| struct sockaddr_storage sockaddr; | struct sockaddr_storage sockaddr; | ||||
| socklen_t len = sizeof(sockaddr); | socklen_t len = sizeof(sockaddr); | ||||
| if (!addrConnect.GetSockAddr((struct sockaddr *)&sockaddr, &len)) { | if (!addrConnect.GetSockAddr((struct sockaddr *)&sockaddr, &len)) { | ||||
| printf("Cannot connect to %s: unsupported network\n", | printf("Cannot connect to %s: unsupported network\n", | ||||
| addrConnect.ToString().c_str()); | addrConnect.ToString().c_str()); | ||||
| Show All 27 Lines | if (connect(hSocket, (struct sockaddr *)&sockaddr, len) == SOCKET_ERROR) { | ||||
| WSAGetLastError() == WSAEINVAL) { | WSAGetLastError() == WSAEINVAL) { | ||||
| struct timeval timeout; | struct timeval timeout; | ||||
| timeout.tv_sec = nTimeout / 1000; | timeout.tv_sec = nTimeout / 1000; | ||||
| timeout.tv_usec = (nTimeout % 1000) * 1000; | timeout.tv_usec = (nTimeout % 1000) * 1000; | ||||
| fd_set fdset; | fd_set fdset; | ||||
| FD_ZERO(&fdset); | FD_ZERO(&fdset); | ||||
| FD_SET(hSocket, &fdset); | FD_SET(hSocket, &fdset); | ||||
| int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout); | int nRet = select(hSocket + 1, nullptr, &fdset, nullptr, &timeout); | ||||
| if (nRet == 0) { | if (nRet == 0) { | ||||
| printf("connection timeout\n"); | printf("connection timeout\n"); | ||||
| closesocket(hSocket); | closesocket(hSocket); | ||||
| return false; | return false; | ||||
| } | } | ||||
| if (nRet == SOCKET_ERROR) { | if (nRet == SOCKET_ERROR) { | ||||
| printf("select() for connection failed: %i\n", | printf("select() for connection failed: %i\n", | ||||
| WSAGetLastError()); | WSAGetLastError()); | ||||
| ▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | bool ConnectSocket(const CService &addrDest, SOCKET &hSocketRet, int nTimeout) { | ||||
| } | } | ||||
| hSocketRet = hSocket; | hSocketRet = hSocket; | ||||
| return true; | return true; | ||||
| } | } | ||||
| bool ConnectSocketByName(CService &addr, SOCKET &hSocketRet, | bool ConnectSocketByName(CService &addr, SOCKET &hSocketRet, | ||||
| const char *pszDest, int portDefault, int nTimeout) { | const char *pszDest, int portDefault, int nTimeout) { | ||||
| string strDest; | std::string strDest; | ||||
| int port = portDefault; | int port = portDefault; | ||||
| SplitHostPort(string(pszDest), port, strDest); | SplitHostPort(std::string(pszDest), port, strDest); | ||||
| SOCKET hSocket = INVALID_SOCKET; | SOCKET hSocket = INVALID_SOCKET; | ||||
| CService addrResolved( | CService addrResolved( | ||||
| CNetAddr(strDest, fNameLookup && !nameproxyInfo.second), port); | CNetAddr(strDest, fNameLookup && !nameproxyInfo.second), port); | ||||
| if (addrResolved.IsValid()) { | if (addrResolved.IsValid()) { | ||||
| addr = addrResolved; | addr = addrResolved; | ||||
| return ConnectSocket(addr, hSocketRet, nTimeout); | return ConnectSocket(addr, hSocketRet, nTimeout); | ||||
| } | } | ||||
| Show All 18 Lines | |||||
| void CNetAddr::Init() { | void CNetAddr::Init() { | ||||
| memset(ip, 0, 16); | memset(ip, 0, 16); | ||||
| } | } | ||||
| void CNetAddr::SetIP(const CNetAddr &ipIn) { | void CNetAddr::SetIP(const CNetAddr &ipIn) { | ||||
| memcpy(ip, ipIn.ip, sizeof(ip)); | memcpy(ip, ipIn.ip, sizeof(ip)); | ||||
| } | } | ||||
| static const unsigned char pchOnionCat[] = {0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43}; | static const uint8_t pchOnionCat[] = {0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43}; | ||||
| static const unsigned char pchGarliCat[] = {0xFD, 0x60, 0xDB, 0x4D, 0xDD, 0xB5}; | static const uint8_t pchGarliCat[] = {0xFD, 0x60, 0xDB, 0x4D, 0xDD, 0xB5}; | ||||
| bool CNetAddr::SetSpecial(const std::string &strName) { | bool CNetAddr::SetSpecial(const std::string &strName) { | ||||
| if (strName.size() > 6 && | if (strName.size() > 6 && | ||||
| strName.substr(strName.size() - 6, 6) == ".onion") { | strName.substr(strName.size() - 6, 6) == ".onion") { | ||||
| std::vector<unsigned char> vchAddr = | std::vector<uint8_t> vchAddr = | ||||
| DecodeBase32(strName.substr(0, strName.size() - 6).c_str()); | DecodeBase32(strName.substr(0, strName.size() - 6).c_str()); | ||||
| if (vchAddr.size() != 16 - sizeof(pchOnionCat)) return false; | if (vchAddr.size() != 16 - sizeof(pchOnionCat)) return false; | ||||
| memcpy(ip, pchOnionCat, sizeof(pchOnionCat)); | memcpy(ip, pchOnionCat, sizeof(pchOnionCat)); | ||||
| 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; | ||||
| } | } | ||||
| if (strName.size() > 11 && | if (strName.size() > 11 && | ||||
| strName.substr(strName.size() - 11, 11) == ".oc.b32.i2p") { | strName.substr(strName.size() - 11, 11) == ".oc.b32.i2p") { | ||||
| std::vector<unsigned char> vchAddr = | std::vector<uint8_t> vchAddr = | ||||
| DecodeBase32(strName.substr(0, strName.size() - 11).c_str()); | DecodeBase32(strName.substr(0, strName.size() - 11).c_str()); | ||||
| if (vchAddr.size() != 16 - sizeof(pchGarliCat)) return false; | if (vchAddr.size() != 16 - sizeof(pchGarliCat)) return false; | ||||
| memcpy(ip, pchOnionCat, sizeof(pchGarliCat)); | memcpy(ip, pchOnionCat, sizeof(pchGarliCat)); | ||||
| for (unsigned int i = 0; i < 16 - sizeof(pchGarliCat); i++) | for (unsigned int i = 0; i < 16 - sizeof(pchGarliCat); i++) | ||||
| ip[i + sizeof(pchGarliCat)] = vchAddr[i]; | ip[i + sizeof(pchGarliCat)] = vchAddr[i]; | ||||
| return true; | return true; | ||||
| } | } | ||||
| return false; | return false; | ||||
| ▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | return GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x0D && | ||||
| GetByte(12) == 0xB8; | GetByte(12) == 0xB8; | ||||
| } | } | ||||
| bool CNetAddr::IsRFC3964() const { | bool CNetAddr::IsRFC3964() const { | ||||
| return (GetByte(15) == 0x20 && GetByte(14) == 0x02); | return (GetByte(15) == 0x20 && GetByte(14) == 0x02); | ||||
| } | } | ||||
| bool CNetAddr::IsRFC6052() const { | bool CNetAddr::IsRFC6052() const { | ||||
| static const unsigned char pchRFC6052[] = {0, 0x64, 0xFF, 0x9B, 0, 0, | static const uint8_t pchRFC6052[] = {0, 0x64, 0xFF, 0x9B, 0, 0, | ||||
| 0, 0, 0, 0, 0, 0}; | 0, 0, 0, 0, 0, 0}; | ||||
| return (memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0); | return (memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0); | ||||
| } | } | ||||
| bool CNetAddr::IsRFC4380() const { | bool CNetAddr::IsRFC4380() const { | ||||
| return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0 && | return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0 && | ||||
| GetByte(12) == 0); | GetByte(12) == 0); | ||||
| } | } | ||||
| bool CNetAddr::IsRFC4862() const { | bool CNetAddr::IsRFC4862() const { | ||||
| static const unsigned char pchRFC4862[] = {0xFE, 0x80, 0, 0, 0, 0, 0, 0}; | static const uint8_t pchRFC4862[] = {0xFE, 0x80, 0, 0, 0, 0, 0, 0}; | ||||
| return (memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0); | return (memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0); | ||||
| } | } | ||||
| bool CNetAddr::IsRFC4193() const { | bool CNetAddr::IsRFC4193() const { | ||||
| return ((GetByte(15) & 0xFE) == 0xFC); | return ((GetByte(15) & 0xFE) == 0xFC); | ||||
| } | } | ||||
| bool CNetAddr::IsRFC6145() const { | bool CNetAddr::IsRFC6145() const { | ||||
| static const unsigned char pchRFC6145[] = {0, 0, 0, 0, 0, 0, | static const uint8_t pchRFC6145[] = {0, 0, 0, 0, 0, 0, | ||||
| 0, 0, 0xFF, 0xFF, 0, 0}; | 0, 0, 0xFF, 0xFF, 0, 0}; | ||||
| return (memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0); | return (memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0); | ||||
| } | } | ||||
| bool CNetAddr::IsRFC4843() const { | bool CNetAddr::IsRFC4843() const { | ||||
| return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 && | return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 && | ||||
| (GetByte(12) & 0xF0) == 0x10); | (GetByte(12) & 0xF0) == 0x10); | ||||
| } | } | ||||
| bool CNetAddr::IsTor() const { | bool CNetAddr::IsTor() const { | ||||
| return (memcmp(ip, pchOnionCat, sizeof(pchOnionCat)) == 0); | return (memcmp(ip, pchOnionCat, sizeof(pchOnionCat)) == 0); | ||||
| } | } | ||||
| bool CNetAddr::IsI2P() const { | bool CNetAddr::IsI2P() const { | ||||
| return (memcmp(ip, pchGarliCat, sizeof(pchGarliCat)) == 0); | return (memcmp(ip, pchGarliCat, sizeof(pchGarliCat)) == 0); | ||||
| } | } | ||||
| bool CNetAddr::IsLocal() const { | bool CNetAddr::IsLocal() const { | ||||
| // IPv4 loopback | // IPv4 loopback | ||||
| if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0)) return true; | if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0)) return true; | ||||
| // IPv6 loopback (::1/128) | // IPv6 loopback (::1/128) | ||||
| static const unsigned char pchLocal[16] = {0, 0, 0, 0, 0, 0, 0, 0, | static const uint8_t pchLocal[16] = {0, 0, 0, 0, 0, 0, 0, 0, | ||||
| 0, 0, 0, 0, 0, 0, 0, 1}; | 0, 0, 0, 0, 0, 0, 0, 1}; | ||||
| if (memcmp(ip, pchLocal, 16) == 0) return true; | if (memcmp(ip, pchLocal, 16) == 0) return true; | ||||
| return false; | return false; | ||||
| } | } | ||||
| bool CNetAddr::IsMulticast() const { | bool CNetAddr::IsMulticast() const { | ||||
| return (IsIPv4() && (GetByte(3) & 0xF0) == 0xE0) || (GetByte(15) == 0xFF); | return (IsIPv4() && (GetByte(3) & 0xF0) == 0xE0) || (GetByte(15) == 0xFF); | ||||
| } | } | ||||
| bool CNetAddr::IsValid() const { | bool CNetAddr::IsValid() const { | ||||
| // Cleanup 3-byte shifted addresses caused by garbage in size field | // Cleanup 3-byte shifted addresses caused by garbage in size field | ||||
| // of addr messages from versions before 0.2.9 checksum. | // of addr messages from versions before 0.2.9 checksum. | ||||
| // Two consecutive addr messages look like this: | // Two consecutive addr messages look like this: | ||||
| // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 | // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 | ||||
| // addr26 addr26... | // addr26 addr26... | ||||
| // so if the first length field is garbled, it reads the second batch | // so if the first length field is garbled, it reads the second batch | ||||
| // of addr misaligned by 3 bytes. | // of addr misaligned by 3 bytes. | ||||
| if (memcmp(ip, pchIPv4 + 3, sizeof(pchIPv4) - 3) == 0) return false; | if (memcmp(ip, pchIPv4 + 3, sizeof(pchIPv4) - 3) == 0) return false; | ||||
| // unspecified IPv6 address (::/128) | // unspecified IPv6 address (::/128) | ||||
| unsigned char ipNone[16] = {}; | uint8_t ipNone[16] = {}; | ||||
| if (memcmp(ip, ipNone, 16) == 0) return false; | if (memcmp(ip, ipNone, 16) == 0) return false; | ||||
| // documentation IPv6 address | // documentation IPv6 address | ||||
| if (IsRFC3849()) return false; | if (IsRFC3849()) return false; | ||||
| if (IsIPv4()) { | if (IsIPv4()) { | ||||
| // INADDR_NONE | // INADDR_NONE | ||||
| uint32_t ipNone = INADDR_NONE; | uint32_t ipNone = INADDR_NONE; | ||||
| Show All 29 Lines | std::string CNetAddr::ToStringIP() const { | ||||
| if (IsTor()) return EncodeBase32(&ip[6], 10) + ".onion"; | if (IsTor()) return EncodeBase32(&ip[6], 10) + ".onion"; | ||||
| if (IsI2P()) return EncodeBase32(&ip[6], 10) + ".oc.b32.i2p"; | if (IsI2P()) return EncodeBase32(&ip[6], 10) + ".oc.b32.i2p"; | ||||
| CService serv(*this, 0); | CService serv(*this, 0); | ||||
| struct sockaddr_storage sockaddr; | struct sockaddr_storage sockaddr; | ||||
| socklen_t socklen = sizeof(sockaddr); | socklen_t socklen = sizeof(sockaddr); | ||||
| if (serv.GetSockAddr((struct sockaddr *)&sockaddr, &socklen)) { | if (serv.GetSockAddr((struct sockaddr *)&sockaddr, &socklen)) { | ||||
| char name[1025] = ""; | char name[1025] = ""; | ||||
| if (!getnameinfo((const struct sockaddr *)&sockaddr, socklen, name, | if (!getnameinfo((const struct sockaddr *)&sockaddr, socklen, name, | ||||
| sizeof(name), NULL, 0, NI_NUMERICHOST)) | sizeof(name), nullptr, 0, NI_NUMERICHOST)) | ||||
| return std::string(name); | return std::string(name); | ||||
| } | } | ||||
| if (IsIPv4()) | |||||
| if (IsIPv4()) { | |||||
| return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), | return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), | ||||
| GetByte(0)); | GetByte(0)); | ||||
| else | } | ||||
| return strprintf( | |||||
| "%x:%x:%x:%x:%x:%x:%x:%x", GetByte(15) << 8 | GetByte(14), | return strprintf("%x:%x:%x:%x:%x:%x:%x:%x", GetByte(15) << 8 | GetByte(14), | ||||
| GetByte(13) << 8 | GetByte(12), GetByte(11) << 8 | GetByte(10), | GetByte(13) << 8 | GetByte(12), | ||||
| GetByte(11) << 8 | GetByte(10), | |||||
| GetByte(9) << 8 | GetByte(8), GetByte(7) << 8 | GetByte(6), | GetByte(9) << 8 | GetByte(8), GetByte(7) << 8 | GetByte(6), | ||||
| GetByte(5) << 8 | GetByte(4), GetByte(3) << 8 | GetByte(2), | GetByte(5) << 8 | GetByte(4), GetByte(3) << 8 | GetByte(2), | ||||
| GetByte(1) << 8 | GetByte(0)); | GetByte(1) << 8 | GetByte(0)); | ||||
| } | } | ||||
| 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); | ||||
| Show All 15 Lines | |||||
| bool CNetAddr::GetIn6Addr(struct in6_addr *pipv6Addr) const { | bool CNetAddr::GetIn6Addr(struct in6_addr *pipv6Addr) const { | ||||
| memcpy(pipv6Addr, ip, 16); | memcpy(pipv6Addr, ip, 16); | ||||
| return true; | return true; | ||||
| } | } | ||||
| // get canonical identifier of an address' group | // get canonical identifier of an address' group | ||||
| // no two connections will be attempted to addresses with the same group | // no two connections will be attempted to addresses with the same group | ||||
| std::vector<unsigned char> CNetAddr::GetGroup() const { | std::vector<uint8_t> CNetAddr::GetGroup() const { | ||||
| std::vector<unsigned char> vchRet; | std::vector<uint8_t> vchRet; | ||||
| int nClass = NET_IPV6; | int nClass = NET_IPV6; | ||||
| int nStartByte = 0; | int nStartByte = 0; | ||||
| int nBits = 16; | int nBits = 16; | ||||
| // all local addresses belong to the same group | // all local addresses belong to the same group | ||||
| if (IsLocal()) { | if (IsLocal()) { | ||||
| nClass = 255; | nClass = 255; | ||||
| nBits = 0; | nBits = 0; | ||||
| Show All 24 Lines | std::vector<uint8_t> CNetAddr::GetGroup() const { | ||||
| } else if (IsTor()) { | } else if (IsTor()) { | ||||
| nClass = NET_TOR; | nClass = NET_TOR; | ||||
| nStartByte = 6; | nStartByte = 6; | ||||
| nBits = 4; | nBits = 4; | ||||
| } else if (IsI2P()) { | } else if (IsI2P()) { | ||||
| nClass = NET_I2P; | nClass = NET_I2P; | ||||
| nStartByte = 6; | nStartByte = 6; | ||||
| nBits = 4; | nBits = 4; | ||||
| } | } else if (GetByte(15) == 0x20 && GetByte(14) == 0x11 && | ||||
| GetByte(13) == 0x04 && GetByte(12) == 0x70) { | |||||
| // for he.net, use /36 groups | // for he.net, use /36 groups | ||||
| else if (GetByte(15) == 0x20 && GetByte(14) == 0x11 && | |||||
| GetByte(13) == 0x04 && GetByte(12) == 0x70) | |||||
| nBits = 36; | nBits = 36; | ||||
| } else { | |||||
| // for the rest of the IPv6 network, use /32 groups | // for the rest of the IPv6 network, use /32 groups | ||||
| else | |||||
| nBits = 32; | nBits = 32; | ||||
| } | |||||
| vchRet.push_back(nClass); | vchRet.push_back(nClass); | ||||
| while (nBits >= 8) { | while (nBits >= 8) { | ||||
| vchRet.push_back(GetByte(15 - nStartByte)); | vchRet.push_back(GetByte(15 - nStartByte)); | ||||
| nStartByte++; | nStartByte++; | ||||
| nBits -= 8; | nBits -= 8; | ||||
| } | } | ||||
| if (nBits > 0) | if (nBits > 0) | ||||
| Show All 12 Lines | |||||
| void CNetAddr::print() const { | void CNetAddr::print() const { | ||||
| printf("CNetAddr(%s)\n", ToString().c_str()); | printf("CNetAddr(%s)\n", ToString().c_str()); | ||||
| } | } | ||||
| // private extensions to enum Network, only returned by GetExtNetwork, | // private extensions to enum Network, only returned by GetExtNetwork, | ||||
| // and only used in GetReachabilityFrom | // and only used in GetReachabilityFrom | ||||
| static const int NET_UNKNOWN = NET_MAX + 0; | static const int NET_UNKNOWN = NET_MAX + 0; | ||||
| static const int NET_TEREDO = NET_MAX + 1; | static const int NET_TEREDO = NET_MAX + 1; | ||||
| int static GetExtNetwork(const CNetAddr *addr) { | static int GetExtNetwork(const CNetAddr *addr) { | ||||
| if (addr == NULL) return NET_UNKNOWN; | if (addr == nullptr) return NET_UNKNOWN; | ||||
| if (addr->IsRFC4380()) return NET_TEREDO; | if (addr->IsRFC4380()) return NET_TEREDO; | ||||
| return addr->GetNetwork(); | return addr->GetNetwork(); | ||||
| } | } | ||||
| /** Calculates a metric for how reachable (*this) is from a given partner */ | /** Calculates a metric for how reachable (*this) is from a given partner */ | ||||
| int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const { | int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const { | ||||
| enum Reachability { | enum Reachability { | ||||
| REACH_UNREACHABLE, | REACH_UNREACHABLE, | ||||
| Show All 23 Lines | switch (theirNet) { | ||||
| switch (ourNet) { | switch (ourNet) { | ||||
| default: | default: | ||||
| return REACH_DEFAULT; | return REACH_DEFAULT; | ||||
| case NET_TEREDO: | case NET_TEREDO: | ||||
| return REACH_TEREDO; | return REACH_TEREDO; | ||||
| case NET_IPV4: | case NET_IPV4: | ||||
| return REACH_IPV4; | return REACH_IPV4; | ||||
| case NET_IPV6: | case NET_IPV6: | ||||
| return fTunnel ? REACH_IPV6_WEAK | // only prefer giving our IPv6 address if it's not tunnelled | ||||
| : REACH_IPV6_STRONG; // only prefer giving | return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; | ||||
| // our IPv6 address if | |||||
| // it's not tunnelled | |||||
| } | } | ||||
| case NET_TOR: | case NET_TOR: | ||||
| switch (ourNet) { | switch (ourNet) { | ||||
| default: | default: | ||||
| return REACH_DEFAULT; | return REACH_DEFAULT; | ||||
| case NET_IPV4: | case NET_IPV4: | ||||
| return REACH_IPV4; // Tor users can connect to IPv4 as well | // Tor users can connect to IPv4 as well | ||||
| return REACH_IPV4; | |||||
| case NET_TOR: | case NET_TOR: | ||||
| return REACH_PRIVATE; | return REACH_PRIVATE; | ||||
| } | } | ||||
| case NET_I2P: | case NET_I2P: | ||||
| switch (ourNet) { | switch (ourNet) { | ||||
| default: | default: | ||||
| return REACH_DEFAULT; | return REACH_DEFAULT; | ||||
| case NET_I2P: | case NET_I2P: | ||||
| Show All 18 Lines | switch (theirNet) { | ||||
| return REACH_DEFAULT; | return REACH_DEFAULT; | ||||
| case NET_TEREDO: | case NET_TEREDO: | ||||
| return REACH_TEREDO; | return REACH_TEREDO; | ||||
| case NET_IPV6: | case NET_IPV6: | ||||
| return REACH_IPV6_WEAK; | return REACH_IPV6_WEAK; | ||||
| case NET_IPV4: | case NET_IPV4: | ||||
| return REACH_IPV4; | return REACH_IPV4; | ||||
| case NET_I2P: | case NET_I2P: | ||||
| return REACH_PRIVATE; // assume connections from unroutable | // assume connections from unroutable addresses are | ||||
| // addresses are | return REACH_PRIVATE; | ||||
| case NET_TOR: | case NET_TOR: | ||||
| return REACH_PRIVATE; // either from Tor/I2P, or don't care | // either from Tor/I2P, or don't care about our address | ||||
| // about our address | return REACH_PRIVATE; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| void CService::Init() { | void CService::Init() { | ||||
| port = 0; | port = 0; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | if (IsIPv6()) { | ||||
| if (!GetIn6Addr(&paddrin6->sin6_addr)) return false; | if (!GetIn6Addr(&paddrin6->sin6_addr)) return false; | ||||
| paddrin6->sin6_family = AF_INET6; | paddrin6->sin6_family = AF_INET6; | ||||
| paddrin6->sin6_port = htons(port); | paddrin6->sin6_port = htons(port); | ||||
| return true; | return true; | ||||
| } | } | ||||
| return false; | return false; | ||||
| } | } | ||||
| std::vector<unsigned char> CService::GetKey() const { | std::vector<uint8_t> CService::GetKey() const { | ||||
| std::vector<unsigned char> vKey; | std::vector<uint8_t> vKey; | ||||
| vKey.resize(18); | vKey.resize(18); | ||||
| memcpy(&vKey[0], ip, 16); | memcpy(&vKey[0], ip, 16); | ||||
| vKey[16] = port / 0x100; | vKey[16] = port / 0x100; | ||||
| vKey[17] = port & 0x0FF; | vKey[17] = port & 0x0FF; | ||||
| return vKey; | return vKey; | ||||
| } | } | ||||
| std::string CService::ToStringPort() const { | std::string CService::ToStringPort() const { | ||||
| Show All 22 Lines | |||||