1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include "config/bitcoin-config.h"
16 #include "utilstrencodings.h"
18 #ifdef HAVE_GETADDRINFO_A
24 #include <arpa/inet.h>
29 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
30 #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
31 #include <boost/thread.hpp>
33 #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
34 #define MSG_NOSIGNAL 0
40 static proxyType proxyInfo[NET_MAX];
41 static CService nameProxy;
42 static CCriticalSection cs_proxyInfos;
43 int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
44 bool fNameLookup = false;
46 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
48 // Need ample time for negotiation for very slow proxies such as Tor (milliseconds)
49 static const int SOCKS5_RECV_TIMEOUT = 20 * 1000;
51 enum Network ParseNetwork(std::string net) {
53 if (net == "ipv4") return NET_IPV4;
54 if (net == "ipv6") return NET_IPV6;
55 if (net == "tor" || net == "onion") return NET_TOR;
56 return NET_UNROUTABLE;
59 std::string GetNetworkName(enum Network net) {
62 case NET_IPV4: return "ipv4";
63 case NET_IPV6: return "ipv6";
64 case NET_TOR: return "onion";
69 void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
70 size_t colon = in.find_last_of(':');
71 // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
72 bool fHaveColon = colon != in.npos;
73 bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
74 bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
75 if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
77 if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
78 in = in.substr(0, colon);
82 if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
83 hostOut = in.substr(1, in.size()-2);
88 bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
94 if (addr.SetSpecial(std::string(pszName))) {
100 #ifdef HAVE_GETADDRINFO_A
101 struct in_addr ipv4_addr;
102 #ifdef HAVE_INET_PTON
103 if (inet_pton(AF_INET, pszName, &ipv4_addr) > 0) {
104 vIP.push_back(CNetAddr(ipv4_addr));
108 struct in6_addr ipv6_addr;
109 if (inet_pton(AF_INET6, pszName, &ipv6_addr) > 0) {
110 vIP.push_back(CNetAddr(ipv6_addr));
114 ipv4_addr.s_addr = inet_addr(pszName);
115 if (ipv4_addr.s_addr != INADDR_NONE) {
116 vIP.push_back(CNetAddr(ipv4_addr));
122 struct addrinfo aiHint;
123 memset(&aiHint, 0, sizeof(struct addrinfo));
124 aiHint.ai_socktype = SOCK_STREAM;
125 aiHint.ai_protocol = IPPROTO_TCP;
126 aiHint.ai_family = AF_UNSPEC;
128 aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST;
130 aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
133 struct addrinfo *aiRes = NULL;
134 #ifdef HAVE_GETADDRINFO_A
135 struct gaicb gcb, *query = &gcb;
136 memset(query, 0, sizeof(struct gaicb));
137 gcb.ar_name = pszName;
138 gcb.ar_request = &aiHint;
139 int nErr = getaddrinfo_a(GAI_NOWAIT, &query, 1, NULL);
144 // Should set the timeout limit to a resonable value to avoid
145 // generating unnecessary checking call during the polling loop,
146 // while it can still response to stop request quick enough.
147 // 2 seconds looks fine in our situation.
148 struct timespec ts = { 2, 0 };
149 gai_suspend(&query, 1, &ts);
150 boost::this_thread::interruption_point();
152 nErr = gai_error(query);
154 aiRes = query->ar_result;
155 } while (nErr == EAI_INPROGRESS);
157 int nErr = getaddrinfo(pszName, NULL, &aiHint, &aiRes);
162 struct addrinfo *aiTrav = aiRes;
163 while (aiTrav != NULL && (nMaxSolutions == 0 || vIP.size() < nMaxSolutions))
165 if (aiTrav->ai_family == AF_INET)
167 assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in));
168 vIP.push_back(CNetAddr(((struct sockaddr_in*)(aiTrav->ai_addr))->sin_addr));
171 if (aiTrav->ai_family == AF_INET6)
173 assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in6));
174 vIP.push_back(CNetAddr(((struct sockaddr_in6*)(aiTrav->ai_addr))->sin6_addr));
177 aiTrav = aiTrav->ai_next;
182 return (vIP.size() > 0);
185 bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
187 std::string strHost(pszName);
190 if (boost::algorithm::starts_with(strHost, "[") && boost::algorithm::ends_with(strHost, "]"))
192 strHost = strHost.substr(1, strHost.size() - 2);
195 return LookupIntern(strHost.c_str(), vIP, nMaxSolutions, fAllowLookup);
198 bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
202 int port = portDefault;
203 std::string hostname = "";
204 SplitHostPort(std::string(pszName), port, hostname);
206 std::vector<CNetAddr> vIP;
207 bool fRet = LookupIntern(hostname.c_str(), vIP, nMaxSolutions, fAllowLookup);
210 vAddr.resize(vIP.size());
211 for (unsigned int i = 0; i < vIP.size(); i++)
212 vAddr[i] = CService(vIP[i], port);
216 bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup)
218 std::vector<CService> vService;
219 bool fRet = Lookup(pszName, vService, portDefault, fAllowLookup, 1);
226 bool LookupNumeric(const char *pszName, CService& addr, int portDefault)
228 return Lookup(pszName, addr, portDefault, false);
232 * Convert milliseconds to a struct timeval for select.
234 struct timeval static MillisToTimeval(int64_t nTimeout)
236 struct timeval timeout;
237 timeout.tv_sec = nTimeout / 1000;
238 timeout.tv_usec = (nTimeout % 1000) * 1000;
243 * Read bytes from socket. This will either read the full number of bytes requested
244 * or return False on error or timeout.
245 * This function can be interrupted by boost thread interrupt.
247 * @param data Buffer to receive into
248 * @param len Length of data to receive
249 * @param timeout Timeout in milliseconds for receive operation
251 * @note This function requires that hSocket is in non-blocking mode.
253 bool static InterruptibleRecv(char* data, size_t len, int timeout, SOCKET& hSocket)
255 int64_t curTime = GetTimeMillis();
256 int64_t endTime = curTime + timeout;
257 // Maximum time to wait in one select call. It will take up until this time (in millis)
258 // to break off in case of an interruption.
259 const int64_t maxWait = 1000;
260 while (len > 0 && curTime < endTime) {
261 ssize_t ret = recv(hSocket, data, len, 0); // Optimistically try the recv first
265 } else if (ret == 0) { // Unexpected disconnection
267 } else { // Other error or blocking
268 int nErr = WSAGetLastError();
269 if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
270 struct timeval tval = MillisToTimeval(std::min(endTime - curTime, maxWait));
273 FD_SET(hSocket, &fdset);
274 int nRet = select(hSocket + 1, &fdset, NULL, NULL, &tval);
275 if (nRet == SOCKET_ERROR) {
282 boost::this_thread::interruption_point();
283 curTime = GetTimeMillis();
288 bool static Socks5(string strDest, int port, SOCKET& hSocket)
290 LogPrintf("SOCKS5 connecting %s\n", strDest);
291 if (strDest.size() > 255)
293 CloseSocket(hSocket);
294 return error("Hostname too long");
296 char pszSocks5Init[] = "\5\1\0";
297 ssize_t nSize = sizeof(pszSocks5Init) - 1;
299 ssize_t ret = send(hSocket, pszSocks5Init, nSize, MSG_NOSIGNAL);
302 CloseSocket(hSocket);
303 return error("Error sending to proxy");
306 if (!InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, hSocket))
308 CloseSocket(hSocket);
309 return error("Error reading proxy response");
311 if (pchRet1[0] != 0x05 || pchRet1[1] != 0x00)
313 CloseSocket(hSocket);
314 return error("Proxy failed to initialize");
316 string strSocks5("\5\1");
317 strSocks5 += '\000'; strSocks5 += '\003';
318 strSocks5 += static_cast<char>(std::min((int)strDest.size(), 255));
319 strSocks5 += strDest;
320 strSocks5 += static_cast<char>((port >> 8) & 0xFF);
321 strSocks5 += static_cast<char>((port >> 0) & 0xFF);
322 ret = send(hSocket, strSocks5.data(), strSocks5.size(), MSG_NOSIGNAL);
323 if (ret != (ssize_t)strSocks5.size())
325 CloseSocket(hSocket);
326 return error("Error sending to proxy");
329 if (!InterruptibleRecv(pchRet2, 4, SOCKS5_RECV_TIMEOUT, hSocket))
331 CloseSocket(hSocket);
332 return error("Error reading proxy response");
334 if (pchRet2[0] != 0x05)
336 CloseSocket(hSocket);
337 return error("Proxy failed to accept request");
339 if (pchRet2[1] != 0x00)
341 CloseSocket(hSocket);
344 case 0x01: return error("Proxy error: general failure");
345 case 0x02: return error("Proxy error: connection not allowed");
346 case 0x03: return error("Proxy error: network unreachable");
347 case 0x04: return error("Proxy error: host unreachable");
348 case 0x05: return error("Proxy error: connection refused");
349 case 0x06: return error("Proxy error: TTL expired");
350 case 0x07: return error("Proxy error: protocol error");
351 case 0x08: return error("Proxy error: address type not supported");
352 default: return error("Proxy error: unknown");
355 if (pchRet2[2] != 0x00)
357 CloseSocket(hSocket);
358 return error("Error: malformed proxy response");
363 case 0x01: ret = InterruptibleRecv(pchRet3, 4, SOCKS5_RECV_TIMEOUT, hSocket); break;
364 case 0x04: ret = InterruptibleRecv(pchRet3, 16, SOCKS5_RECV_TIMEOUT, hSocket); break;
367 ret = InterruptibleRecv(pchRet3, 1, SOCKS5_RECV_TIMEOUT, hSocket);
369 CloseSocket(hSocket);
370 return error("Error reading from proxy");
372 int nRecv = pchRet3[0];
373 ret = InterruptibleRecv(pchRet3, nRecv, SOCKS5_RECV_TIMEOUT, hSocket);
376 default: CloseSocket(hSocket); return error("Error: malformed proxy response");
380 CloseSocket(hSocket);
381 return error("Error reading from proxy");
383 if (!InterruptibleRecv(pchRet3, 2, SOCKS5_RECV_TIMEOUT, hSocket))
385 CloseSocket(hSocket);
386 return error("Error reading from proxy");
388 LogPrintf("SOCKS5 connected %s\n", strDest);
392 bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRet, int nTimeout)
394 hSocketRet = INVALID_SOCKET;
396 struct sockaddr_storage sockaddr;
397 socklen_t len = sizeof(sockaddr);
398 if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
399 LogPrintf("Cannot connect to %s: unsupported network\n", addrConnect.ToString());
403 SOCKET hSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
404 if (hSocket == INVALID_SOCKET)
409 // Different way of disabling SIGPIPE on BSD
410 setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
413 // Set to non-blocking
414 if (!SetSocketNonBlocking(hSocket, true))
415 return error("ConnectSocketDirectly: Setting socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
417 if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
419 int nErr = WSAGetLastError();
420 // WSAEINVAL is here because some legacy version of winsock uses it
421 if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL)
423 struct timeval timeout = MillisToTimeval(nTimeout);
426 FD_SET(hSocket, &fdset);
427 int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout);
430 LogPrint("net", "connection to %s timeout\n", addrConnect.ToString());
431 CloseSocket(hSocket);
434 if (nRet == SOCKET_ERROR)
436 LogPrintf("select() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
437 CloseSocket(hSocket);
440 socklen_t nRetSize = sizeof(nRet);
442 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
444 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
447 LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
448 CloseSocket(hSocket);
453 LogPrintf("connect() to %s failed after select(): %s\n", addrConnect.ToString(), NetworkErrorString(nRet));
454 CloseSocket(hSocket);
459 else if (WSAGetLastError() != WSAEISCONN)
464 LogPrintf("connect() to %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
465 CloseSocket(hSocket);
470 hSocketRet = hSocket;
474 bool SetProxy(enum Network net, CService addrProxy) {
475 assert(net >= 0 && net < NET_MAX);
476 if (!addrProxy.IsValid())
479 proxyInfo[net] = addrProxy;
483 bool GetProxy(enum Network net, proxyType &proxyInfoOut) {
484 assert(net >= 0 && net < NET_MAX);
486 if (!proxyInfo[net].IsValid())
488 proxyInfoOut = proxyInfo[net];
492 bool SetNameProxy(CService addrProxy) {
493 if (!addrProxy.IsValid())
496 nameProxy = addrProxy;
500 bool GetNameProxy(CService &nameProxyOut) {
502 if(!nameProxy.IsValid())
504 nameProxyOut = nameProxy;
508 bool HaveNameProxy() {
510 return nameProxy.IsValid();
513 bool IsProxy(const CNetAddr &addr) {
515 for (int i = 0; i < NET_MAX; i++) {
516 if (addr == (CNetAddr)proxyInfo[i])
522 bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed)
525 if (outProxyConnectionFailed)
526 *outProxyConnectionFailed = false;
527 // no proxy needed (none set for target network)
528 if (!GetProxy(addrDest.GetNetwork(), proxy))
529 return ConnectSocketDirectly(addrDest, hSocketRet, nTimeout);
531 SOCKET hSocket = INVALID_SOCKET;
533 // first connect to proxy server
534 if (!ConnectSocketDirectly(proxy, hSocket, nTimeout)) {
535 if (outProxyConnectionFailed)
536 *outProxyConnectionFailed = true;
539 // do socks negotiation
540 if (!Socks5(addrDest.ToStringIP(), addrDest.GetPort(), hSocket))
543 hSocketRet = hSocket;
547 bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed)
550 int port = portDefault;
552 if (outProxyConnectionFailed)
553 *outProxyConnectionFailed = false;
555 SplitHostPort(string(pszDest), port, strDest);
557 SOCKET hSocket = INVALID_SOCKET;
560 GetNameProxy(nameProxy);
562 CService addrResolved(CNetAddr(strDest, fNameLookup && !HaveNameProxy()), port);
563 if (addrResolved.IsValid()) {
565 return ConnectSocket(addr, hSocketRet, nTimeout);
568 addr = CService("0.0.0.0:0");
570 if (!HaveNameProxy())
572 // first connect to name proxy server
573 if (!ConnectSocketDirectly(nameProxy, hSocket, nTimeout)) {
574 if (outProxyConnectionFailed)
575 *outProxyConnectionFailed = true;
578 // do socks negotiation
579 if (!Socks5(strDest, (unsigned short)port, hSocket))
582 hSocketRet = hSocket;
586 void CNetAddr::Init()
588 memset(ip, 0, sizeof(ip));
591 void CNetAddr::SetIP(const CNetAddr& ipIn)
593 memcpy(ip, ipIn.ip, sizeof(ip));
596 void CNetAddr::SetRaw(Network network, const uint8_t *ip_in)
601 memcpy(ip, pchIPv4, 12);
602 memcpy(ip+12, ip_in, 4);
605 memcpy(ip, ip_in, 16);
608 assert(!"invalid network");
612 static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};
614 bool CNetAddr::SetSpecial(const std::string &strName)
616 if (strName.size()>6 && strName.substr(strName.size() - 6, 6) == ".onion") {
617 std::vector<unsigned char> vchAddr = DecodeBase32(strName.substr(0, strName.size() - 6).c_str());
618 if (vchAddr.size() != 16-sizeof(pchOnionCat))
620 memcpy(ip, pchOnionCat, sizeof(pchOnionCat));
621 for (unsigned int i=0; i<16-sizeof(pchOnionCat); i++)
622 ip[i + sizeof(pchOnionCat)] = vchAddr[i];
633 CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
635 SetRaw(NET_IPV4, (const uint8_t*)&ipv4Addr);
638 CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr)
640 SetRaw(NET_IPV6, (const uint8_t*)&ipv6Addr);
643 CNetAddr::CNetAddr(const char *pszIp, bool fAllowLookup)
646 std::vector<CNetAddr> vIP;
647 if (LookupHost(pszIp, vIP, 1, fAllowLookup))
651 CNetAddr::CNetAddr(const std::string &strIp, bool fAllowLookup)
654 std::vector<CNetAddr> vIP;
655 if (LookupHost(strIp.c_str(), vIP, 1, fAllowLookup))
659 unsigned int CNetAddr::GetByte(int n) const
664 bool CNetAddr::IsIPv4() const
666 return (memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0);
669 bool CNetAddr::IsIPv6() const
671 return (!IsIPv4() && !IsTor());
674 bool CNetAddr::IsRFC1918() const
678 (GetByte(3) == 192 && GetByte(2) == 168) ||
679 (GetByte(3) == 172 && (GetByte(2) >= 16 && GetByte(2) <= 31)));
682 bool CNetAddr::IsRFC2544() const
684 return IsIPv4() && GetByte(3) == 198 && (GetByte(2) == 18 || GetByte(2) == 19);
687 bool CNetAddr::IsRFC3927() const
689 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
692 bool CNetAddr::IsRFC6598() const
694 return IsIPv4() && GetByte(3) == 100 && GetByte(2) >= 64 && GetByte(2) <= 127;
697 bool CNetAddr::IsRFC5737() const
699 return IsIPv4() && ((GetByte(3) == 192 && GetByte(2) == 0 && GetByte(1) == 2) ||
700 (GetByte(3) == 198 && GetByte(2) == 51 && GetByte(1) == 100) ||
701 (GetByte(3) == 203 && GetByte(2) == 0 && GetByte(1) == 113));
704 bool CNetAddr::IsRFC3849() const
706 return GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x0D && GetByte(12) == 0xB8;
709 bool CNetAddr::IsRFC3964() const
711 return (GetByte(15) == 0x20 && GetByte(14) == 0x02);
714 bool CNetAddr::IsRFC6052() const
716 static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
717 return (memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0);
720 bool CNetAddr::IsRFC4380() const
722 return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0 && GetByte(12) == 0);
725 bool CNetAddr::IsRFC4862() const
727 static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
728 return (memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0);
731 bool CNetAddr::IsRFC4193() const
733 return ((GetByte(15) & 0xFE) == 0xFC);
736 bool CNetAddr::IsRFC6145() const
738 static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
739 return (memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0);
742 bool CNetAddr::IsRFC4843() const
744 return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 && (GetByte(12) & 0xF0) == 0x10);
747 bool CNetAddr::IsTor() const
749 return (memcmp(ip, pchOnionCat, sizeof(pchOnionCat)) == 0);
752 bool CNetAddr::IsLocal() const
755 if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0))
758 // IPv6 loopback (::1/128)
759 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
760 if (memcmp(ip, pchLocal, 16) == 0)
766 bool CNetAddr::IsMulticast() const
768 return (IsIPv4() && (GetByte(3) & 0xF0) == 0xE0)
769 || (GetByte(15) == 0xFF);
772 bool CNetAddr::IsValid() const
774 // Cleanup 3-byte shifted addresses caused by garbage in size field
775 // of addr messages from versions before 0.2.9 checksum.
776 // Two consecutive addr messages look like this:
777 // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
778 // so if the first length field is garbled, it reads the second batch
779 // of addr misaligned by 3 bytes.
780 if (memcmp(ip, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
783 // unspecified IPv6 address (::/128)
784 unsigned char ipNone[16] = {};
785 if (memcmp(ip, ipNone, 16) == 0)
788 // documentation IPv6 address
795 uint32_t ipNone = INADDR_NONE;
796 if (memcmp(ip+12, &ipNone, 4) == 0)
801 if (memcmp(ip+12, &ipNone, 4) == 0)
808 bool CNetAddr::IsRoutable() const
810 return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) || IsRFC4843() || IsLocal());
813 enum Network CNetAddr::GetNetwork() const
816 return NET_UNROUTABLE;
827 std::string CNetAddr::ToStringIP() const
830 return EncodeBase32(&ip[6], 10) + ".onion";
831 CService serv(*this, 0);
832 struct sockaddr_storage sockaddr;
833 socklen_t socklen = sizeof(sockaddr);
834 if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
835 char name[1025] = "";
836 if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), NULL, 0, NI_NUMERICHOST))
837 return std::string(name);
840 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
842 return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
843 GetByte(15) << 8 | GetByte(14), GetByte(13) << 8 | GetByte(12),
844 GetByte(11) << 8 | GetByte(10), GetByte(9) << 8 | GetByte(8),
845 GetByte(7) << 8 | GetByte(6), GetByte(5) << 8 | GetByte(4),
846 GetByte(3) << 8 | GetByte(2), GetByte(1) << 8 | GetByte(0));
849 std::string CNetAddr::ToString() const
854 bool operator==(const CNetAddr& a, const CNetAddr& b)
856 return (memcmp(a.ip, b.ip, 16) == 0);
859 bool operator!=(const CNetAddr& a, const CNetAddr& b)
861 return (memcmp(a.ip, b.ip, 16) != 0);
864 bool operator<(const CNetAddr& a, const CNetAddr& b)
866 return (memcmp(a.ip, b.ip, 16) < 0);
869 bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
873 memcpy(pipv4Addr, ip+12, 4);
877 bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
879 memcpy(pipv6Addr, ip, 16);
883 // get canonical identifier of an address' group
884 // no two connections will be attempted to addresses with the same group
885 std::vector<unsigned char> CNetAddr::GetGroup() const
887 std::vector<unsigned char> vchRet;
888 int nClass = NET_IPV6;
892 // all local addresses belong to the same group
899 // all unroutable addresses belong to the same group
902 nClass = NET_UNROUTABLE;
905 // for IPv4 addresses, '1' + the 16 higher-order bits of the IP
906 // includes mapped IPv4, SIIT translated IPv4, and the well-known prefix
907 else if (IsIPv4() || IsRFC6145() || IsRFC6052())
912 // for 6to4 tunnelled addresses, use the encapsulated IPv4 address
913 else if (IsRFC3964())
918 // for Teredo-tunnelled IPv6 addresses, use the encapsulated IPv4 address
919 else if (IsRFC4380())
921 vchRet.push_back(NET_IPV4);
922 vchRet.push_back(GetByte(3) ^ 0xFF);
923 vchRet.push_back(GetByte(2) ^ 0xFF);
932 // for he.net, use /36 groups
933 else if (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x04 && GetByte(12) == 0x70)
935 // for the rest of the IPv6 network, use /32 groups
939 vchRet.push_back(nClass);
942 vchRet.push_back(GetByte(15 - nStartByte));
947 vchRet.push_back(GetByte(15 - nStartByte) | ((1 << nBits) - 1));
952 uint64_t CNetAddr::GetHash() const
954 uint256 hash = Hash(&ip[0], &ip[16]);
956 memcpy(&nRet, &hash, sizeof(nRet));
960 // private extensions to enum Network, only returned by GetExtNetwork,
961 // and only used in GetReachabilityFrom
962 static const int NET_UNKNOWN = NET_MAX + 0;
963 static const int NET_TEREDO = NET_MAX + 1;
964 int static GetExtNetwork(const CNetAddr *addr)
968 if (addr->IsRFC4380())
970 return addr->GetNetwork();
973 /** Calculates a metric for how reachable (*this) is from a given partner */
974 int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
987 return REACH_UNREACHABLE;
989 int ourNet = GetExtNetwork(this);
990 int theirNet = GetExtNetwork(paddrPartner);
991 bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
996 default: return REACH_DEFAULT;
997 case NET_IPV4: return REACH_IPV4;
1001 default: return REACH_DEFAULT;
1002 case NET_TEREDO: return REACH_TEREDO;
1003 case NET_IPV4: return REACH_IPV4;
1004 case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
1008 default: return REACH_DEFAULT;
1009 case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
1010 case NET_TOR: return REACH_PRIVATE;
1014 default: return REACH_DEFAULT;
1015 case NET_TEREDO: return REACH_TEREDO;
1016 case NET_IPV6: return REACH_IPV6_WEAK;
1017 case NET_IPV4: return REACH_IPV4;
1020 case NET_UNROUTABLE:
1023 default: return REACH_DEFAULT;
1024 case NET_TEREDO: return REACH_TEREDO;
1025 case NET_IPV6: return REACH_IPV6_WEAK;
1026 case NET_IPV4: return REACH_IPV4;
1027 case NET_TOR: return REACH_PRIVATE; // either from Tor, or don't care about our address
1032 void CService::Init()
1037 CService::CService()
1042 CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
1046 CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn)
1050 CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn)
1054 CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
1056 assert(addr.sin_family == AF_INET);
1059 CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr), port(ntohs(addr.sin6_port))
1061 assert(addr.sin6_family == AF_INET6);
1064 bool CService::SetSockAddr(const struct sockaddr *paddr)
1066 switch (paddr->sa_family) {
1068 *this = CService(*(const struct sockaddr_in*)paddr);
1071 *this = CService(*(const struct sockaddr_in6*)paddr);
1078 CService::CService(const char *pszIpPort, bool fAllowLookup)
1082 if (Lookup(pszIpPort, ip, 0, fAllowLookup))
1086 CService::CService(const char *pszIpPort, int portDefault, bool fAllowLookup)
1090 if (Lookup(pszIpPort, ip, portDefault, fAllowLookup))
1094 CService::CService(const std::string &strIpPort, bool fAllowLookup)
1098 if (Lookup(strIpPort.c_str(), ip, 0, fAllowLookup))
1102 CService::CService(const std::string &strIpPort, int portDefault, bool fAllowLookup)
1106 if (Lookup(strIpPort.c_str(), ip, portDefault, fAllowLookup))
1110 unsigned short CService::GetPort() const
1115 bool operator==(const CService& a, const CService& b)
1117 return (CNetAddr)a == (CNetAddr)b && a.port == b.port;
1120 bool operator!=(const CService& a, const CService& b)
1122 return (CNetAddr)a != (CNetAddr)b || a.port != b.port;
1125 bool operator<(const CService& a, const CService& b)
1127 return (CNetAddr)a < (CNetAddr)b || ((CNetAddr)a == (CNetAddr)b && a.port < b.port);
1130 bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
1133 if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
1135 *addrlen = sizeof(struct sockaddr_in);
1136 struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
1137 memset(paddrin, 0, *addrlen);
1138 if (!GetInAddr(&paddrin->sin_addr))
1140 paddrin->sin_family = AF_INET;
1141 paddrin->sin_port = htons(port);
1145 if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
1147 *addrlen = sizeof(struct sockaddr_in6);
1148 struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
1149 memset(paddrin6, 0, *addrlen);
1150 if (!GetIn6Addr(&paddrin6->sin6_addr))
1152 paddrin6->sin6_family = AF_INET6;
1153 paddrin6->sin6_port = htons(port);
1159 std::vector<unsigned char> CService::GetKey() const
1161 std::vector<unsigned char> vKey;
1163 memcpy(&vKey[0], ip, 16);
1164 vKey[16] = port / 0x100;
1165 vKey[17] = port & 0x0FF;
1169 std::string CService::ToStringPort() const
1171 return strprintf("%u", port);
1174 std::string CService::ToStringIPPort() const
1176 if (IsIPv4() || IsTor()) {
1177 return ToStringIP() + ":" + ToStringPort();
1179 return "[" + ToStringIP() + "]:" + ToStringPort();
1183 std::string CService::ToString() const
1185 return ToStringIPPort();
1188 void CService::SetPort(unsigned short portIn)
1196 memset(netmask, 0, sizeof(netmask));
1199 CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup)
1201 size_t slash = strSubnet.find_last_of('/');
1202 std::vector<CNetAddr> vIP;
1205 // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
1206 memset(netmask, 255, sizeof(netmask));
1208 std::string strAddress = strSubnet.substr(0, slash);
1209 if (LookupHost(strAddress.c_str(), vIP, 1, fAllowLookup))
1212 if (slash != strSubnet.npos)
1214 std::string strNetmask = strSubnet.substr(slash + 1);
1216 // IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
1217 int noffset = network.IsIPv4() ? (12 * 8) : 0;
1218 if (ParseInt32(strNetmask, &n)) // If valid number, assume /24 symtex
1220 if(n >= 0 && n <= (128 - noffset)) // Only valid if in range of bits of address
1223 // Clear bits [n..127]
1224 for (; n < 128; ++n)
1225 netmask[n>>3] &= ~(1<<(n&7));
1232 else // If not a valid number, try full netmask syntax
1234 if (LookupHost(strNetmask.c_str(), vIP, 1, false)) // Never allow lookup for netmask
1236 // Remember: GetByte returns bytes in reversed order
1237 // Copy only the *last* four bytes in case of IPv4, the rest of the mask should stay 1's as
1238 // we don't want pchIPv4 to be part of the mask.
1239 int asize = network.IsIPv4() ? 4 : 16;
1240 for(int x=0; x<asize; ++x)
1241 netmask[15-x] = vIP[0].GetByte(x);
1256 bool CSubNet::Match(const CNetAddr &addr) const
1258 if (!valid || !addr.IsValid())
1260 for(int x=0; x<16; ++x)
1261 if ((addr.GetByte(x) & netmask[15-x]) != network.GetByte(x))
1266 std::string CSubNet::ToString() const
1268 std::string strNetmask;
1269 if (network.IsIPv4())
1270 strNetmask = strprintf("%u.%u.%u.%u", netmask[12], netmask[13], netmask[14], netmask[15]);
1272 strNetmask = strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
1273 netmask[0] << 8 | netmask[1], netmask[2] << 8 | netmask[3],
1274 netmask[4] << 8 | netmask[5], netmask[6] << 8 | netmask[7],
1275 netmask[8] << 8 | netmask[9], netmask[10] << 8 | netmask[11],
1276 netmask[12] << 8 | netmask[13], netmask[14] << 8 | netmask[15]);
1277 return network.ToString() + "/" + strNetmask;
1280 bool CSubNet::IsValid() const
1285 bool operator==(const CSubNet& a, const CSubNet& b)
1287 return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
1290 bool operator!=(const CSubNet& a, const CSubNet& b)
1296 std::string NetworkErrorString(int err)
1300 if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
1301 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1302 buf, sizeof(buf), NULL))
1304 return strprintf("%s (%d)", buf, err);
1308 return strprintf("Unknown error (%d)", err);
1312 std::string NetworkErrorString(int err)
1315 const char *s = buf;
1317 /* Too bad there are two incompatible implementations of the
1318 * thread-safe strerror. */
1319 #ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
1320 s = strerror_r(err, buf, sizeof(buf));
1321 #else /* POSIX variant always returns message in buffer */
1322 if (strerror_r(err, buf, sizeof(buf)))
1325 return strprintf("%s (%d)", s, err);
1329 bool CloseSocket(SOCKET& hSocket)
1331 if (hSocket == INVALID_SOCKET)
1334 int ret = closesocket(hSocket);
1336 int ret = close(hSocket);
1338 hSocket = INVALID_SOCKET;
1339 return ret != SOCKET_ERROR;
1342 bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking)
1347 if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR) {
1349 int fFlags = fcntl(hSocket, F_GETFL, 0);
1350 if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == SOCKET_ERROR) {
1352 CloseSocket(hSocket);
1358 if (ioctlsocket(hSocket, FIONBIO, &nZero) == SOCKET_ERROR) {
1360 int fFlags = fcntl(hSocket, F_GETFL, 0);
1361 if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR) {
1363 CloseSocket(hSocket);