1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
10 #include <sys/fcntl.h>
14 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
19 typedef std::pair<CService, int> proxyType;
20 static proxyType proxyInfo[NET_MAX];
21 static proxyType nameproxyInfo;
22 int nConnectTimeout = 5000;
23 bool fNameLookup = false;
25 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
27 enum Network ParseNetwork(std::string net) {
29 if (net == "ipv4") return NET_IPV4;
30 if (net == "ipv6") return NET_IPV6;
31 if (net == "tor") return NET_TOR;
32 if (net == "i2p") return NET_I2P;
33 return NET_UNROUTABLE;
36 bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
39 struct addrinfo aiHint;
40 memset(&aiHint, 0, sizeof(struct addrinfo));
42 aiHint.ai_socktype = SOCK_STREAM;
43 aiHint.ai_protocol = IPPROTO_TCP;
46 aiHint.ai_family = AF_UNSPEC;
47 aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST;
49 aiHint.ai_family = AF_INET;
50 aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST;
54 aiHint.ai_family = AF_UNSPEC;
55 aiHint.ai_flags = AI_ADDRCONFIG | (fAllowLookup ? 0 : AI_NUMERICHOST);
57 aiHint.ai_family = AF_INET;
58 aiHint.ai_flags = AI_ADDRCONFIG | (fAllowLookup ? 0 : AI_NUMERICHOST);
61 struct addrinfo *aiRes = NULL;
62 int nErr = getaddrinfo(pszName, NULL, &aiHint, &aiRes);
66 struct addrinfo *aiTrav = aiRes;
67 while (aiTrav != NULL && (nMaxSolutions == 0 || vIP.size() < nMaxSolutions))
69 if (aiTrav->ai_family == AF_INET)
71 assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in));
72 vIP.push_back(CNetAddr(((struct sockaddr_in*)(aiTrav->ai_addr))->sin_addr));
76 if (aiTrav->ai_family == AF_INET6)
78 assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in6));
79 vIP.push_back(CNetAddr(((struct sockaddr_in6*)(aiTrav->ai_addr))->sin6_addr));
83 aiTrav = aiTrav->ai_next;
88 return (vIP.size() > 0);
91 bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
97 strlcpy(psz, pszName, sizeof(psz));
98 if (psz[0] == '[' && psz[strlen(psz)-1] == ']')
101 psz[strlen(psz)-1] = 0;
104 return LookupIntern(pszHost, vIP, nMaxSolutions, fAllowLookup);
107 bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions)
109 return LookupHost(pszName, vIP, nMaxSolutions, false);
112 bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
116 int port = portDefault;
119 strlcpy(psz, pszName, sizeof(psz));
120 char* pszColon = strrchr(psz+1,':');
121 char *pszPortEnd = NULL;
122 int portParsed = pszColon ? strtoul(pszColon+1, &pszPortEnd, 10) : 0;
123 if (pszColon && pszPortEnd && pszPortEnd[0] == 0)
125 if (psz[0] == '[' && pszColon[-1] == ']')
132 if (port >= 0 && port <= USHRT_MAX)
137 if (psz[0] == '[' && psz[strlen(psz)-1] == ']')
140 psz[strlen(psz)-1] = 0;
145 std::vector<CNetAddr> vIP;
146 bool fRet = LookupIntern(pszHost, vIP, nMaxSolutions, fAllowLookup);
149 vAddr.resize(vIP.size());
150 for (unsigned int i = 0; i < vIP.size(); i++)
151 vAddr[i] = CService(vIP[i], port);
155 bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup)
157 std::vector<CService> vService;
158 bool fRet = Lookup(pszName, vService, portDefault, fAllowLookup, 1);
165 bool LookupNumeric(const char *pszName, CService& addr, int portDefault)
167 return Lookup(pszName, addr, portDefault, false);
170 bool static Socks4(const CService &addrDest, SOCKET& hSocket)
172 printf("SOCKS4 connecting %s\n", addrDest.ToString().c_str());
173 if (!addrDest.IsIPv4())
175 closesocket(hSocket);
176 return error("Proxy destination is not IPv4");
178 char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user";
179 struct sockaddr_in addr;
180 socklen_t len = sizeof(addr);
181 if (!addrDest.GetSockAddr((struct sockaddr*)&addr, &len) || addr.sin_family != AF_INET)
183 closesocket(hSocket);
184 return error("Cannot get proxy destination address");
186 memcpy(pszSocks4IP + 2, &addr.sin_port, 2);
187 memcpy(pszSocks4IP + 4, &addr.sin_addr, 4);
188 char* pszSocks4 = pszSocks4IP;
189 int nSize = sizeof(pszSocks4IP);
191 int ret = send(hSocket, pszSocks4, nSize, MSG_NOSIGNAL);
194 closesocket(hSocket);
195 return error("Error sending to proxy");
198 if (recv(hSocket, pchRet, 8, 0) != 8)
200 closesocket(hSocket);
201 return error("Error reading proxy response");
203 if (pchRet[1] != 0x5a)
205 closesocket(hSocket);
206 if (pchRet[1] != 0x5b)
207 printf("ERROR: Proxy returned error %d\n", pchRet[1]);
210 printf("SOCKS4 connected %s\n", addrDest.ToString().c_str());
214 bool static Socks5(string strDest, int port, SOCKET& hSocket)
216 printf("SOCKS5 connecting %s\n", strDest.c_str());
217 if (strDest.size() > 255)
219 closesocket(hSocket);
220 return error("Hostname too long");
222 char pszSocks5Init[] = "\5\1\0";
223 char *pszSocks5 = pszSocks5Init;
224 ssize_t nSize = sizeof(pszSocks5Init);
226 ssize_t ret = send(hSocket, pszSocks5, nSize, MSG_NOSIGNAL);
229 closesocket(hSocket);
230 return error("Error sending to proxy");
233 if (recv(hSocket, pchRet1, 2, 0) != 2)
235 closesocket(hSocket);
236 return error("Error reading proxy response");
238 if (pchRet1[0] != 0x05 || pchRet1[1] != 0x00)
240 closesocket(hSocket);
241 return error("Proxy failed to initialize");
243 string strSocks5("\5\1");
244 strSocks5 += '\000'; strSocks5 += '\003';
245 strSocks5 += static_cast<char>(std::min((int)strDest.size(), 255));
246 strSocks5 += strDest;
247 strSocks5 += static_cast<char>((port >> 8) & 0xFF);
248 strSocks5 += static_cast<char>((port >> 0) & 0xFF);
249 ret = send(hSocket, strSocks5.c_str(), strSocks5.size(), MSG_NOSIGNAL);
250 if (ret != (ssize_t)strSocks5.size())
252 closesocket(hSocket);
253 return error("Error sending to proxy");
256 if (recv(hSocket, pchRet2, 4, 0) != 4)
258 closesocket(hSocket);
259 return error("Error reading proxy response");
261 if (pchRet2[0] != 0x05)
263 closesocket(hSocket);
264 return error("Proxy failed to accept request");
266 if (pchRet2[1] != 0x00)
268 closesocket(hSocket);
271 case 0x01: return error("Proxy error: general failure");
272 case 0x02: return error("Proxy error: connection not allowed");
273 case 0x03: return error("Proxy error: network unreachable");
274 case 0x04: return error("Proxy error: host unreachable");
275 case 0x05: return error("Proxy error: connection refused");
276 case 0x06: return error("Proxy error: TTL expired");
277 case 0x07: return error("Proxy error: protocol error");
278 case 0x08: return error("Proxy error: address type not supported");
279 default: return error("Proxy error: unknown");
282 if (pchRet2[2] != 0x00)
284 closesocket(hSocket);
285 return error("Error: malformed proxy response");
290 case 0x01: ret = recv(hSocket, pchRet3, 4, 0) != 4; break;
291 case 0x04: ret = recv(hSocket, pchRet3, 16, 0) != 16; break;
294 ret = recv(hSocket, pchRet3, 1, 0) != 1;
296 return error("Error reading from proxy");
297 int nRecv = pchRet3[0];
298 ret = recv(hSocket, pchRet3, nRecv, 0) != nRecv;
301 default: closesocket(hSocket); return error("Error: malformed proxy response");
305 closesocket(hSocket);
306 return error("Error reading from proxy");
308 if (recv(hSocket, pchRet3, 2, 0) != 2)
310 closesocket(hSocket);
311 return error("Error reading from proxy");
313 printf("SOCKS5 connected %s\n", strDest.c_str());
317 bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRet, int nTimeout)
319 hSocketRet = INVALID_SOCKET;
322 struct sockaddr_storage sockaddr;
324 struct sockaddr sockaddr;
326 socklen_t len = sizeof(sockaddr);
327 if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
328 printf("Cannot connect to %s: unsupported network\n", addrConnect.ToString().c_str());
332 SOCKET hSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
333 if (hSocket == INVALID_SOCKET)
337 setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
341 u_long fNonblock = 1;
342 if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
344 int fFlags = fcntl(hSocket, F_GETFL, 0);
345 if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
348 closesocket(hSocket);
352 if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
354 // WSAEINVAL is here because some legacy version of winsock uses it
355 if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAEINVAL)
357 struct timeval timeout;
358 timeout.tv_sec = nTimeout / 1000;
359 timeout.tv_usec = (nTimeout % 1000) * 1000;
363 FD_SET(hSocket, &fdset);
364 int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout);
367 printf("connection timeout\n");
368 closesocket(hSocket);
371 if (nRet == SOCKET_ERROR)
373 printf("select() for connection failed: %i\n",WSAGetLastError());
374 closesocket(hSocket);
377 socklen_t nRetSize = sizeof(nRet);
379 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
381 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
384 printf("getsockopt() for connection failed: %i\n",WSAGetLastError());
385 closesocket(hSocket);
390 printf("connect() failed after select(): %s\n",strerror(nRet));
391 closesocket(hSocket);
396 else if (WSAGetLastError() != WSAEISCONN)
401 printf("connect() failed: %i\n",WSAGetLastError());
402 closesocket(hSocket);
407 // this isn't even strictly necessary
408 // CNode::ConnectNode immediately turns the socket back to non-blocking
409 // but we'll turn it back to blocking just in case
412 if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
414 fFlags = fcntl(hSocket, F_GETFL, 0);
415 if (fcntl(hSocket, F_SETFL, fFlags & !O_NONBLOCK) == SOCKET_ERROR)
418 closesocket(hSocket);
422 hSocketRet = hSocket;
426 bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion) {
427 assert(net >= 0 && net < NET_MAX);
428 if (nSocksVersion != 0 && nSocksVersion != 4 && nSocksVersion != 5)
430 if (nSocksVersion != 0 && !addrProxy.IsValid())
432 proxyInfo[net] = std::make_pair(addrProxy, nSocksVersion);
436 bool GetProxy(enum Network net, CService &addrProxy) {
437 assert(net >= 0 && net < NET_MAX);
438 if (!proxyInfo[net].second)
440 addrProxy = proxyInfo[net].first;
444 bool SetNameProxy(CService addrProxy, int nSocksVersion) {
445 if (nSocksVersion != 0 && nSocksVersion != 5)
447 if (nSocksVersion != 0 && !addrProxy.IsValid())
449 nameproxyInfo = std::make_pair(addrProxy, nSocksVersion);
453 bool GetNameProxy() {
454 return nameproxyInfo.second != 0;
457 bool IsProxy(const CNetAddr &addr) {
458 for (int i=0; i<NET_MAX; i++) {
459 if (proxyInfo[i].second && (addr == (CNetAddr)proxyInfo[i].first))
465 bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
467 const proxyType &proxy = proxyInfo[addrDest.GetNetwork()];
471 return ConnectSocketDirectly(addrDest, hSocketRet, nTimeout);
473 SOCKET hSocket = INVALID_SOCKET;
475 // first connect to proxy server
476 if (!ConnectSocketDirectly(proxy.first, hSocket, nTimeout))
479 // do socks negotiation
480 switch (proxy.second) {
482 if (!Socks4(addrDest, hSocket))
486 if (!Socks5(addrDest.ToStringIP(), addrDest.GetPort(), hSocket))
493 hSocketRet = hSocket;
497 bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout)
499 string strDest(pszDest);
500 int port = portDefault;
502 // split hostname and port
503 size_t colon = strDest.find_last_of(':');
504 if (colon != strDest.npos) {
506 int n = strtol(pszDest + colon + 1, &endp, 10);
507 if (endp && *endp == 0 && n >= 0) {
508 strDest = strDest.substr(0, colon);
509 if (n > 0 && n < 0x10000)
513 if (strDest[0] == '[' && strDest[strDest.size()-1] == ']')
514 strDest = strDest.substr(1, strDest.size()-2);
516 SOCKET hSocket = INVALID_SOCKET;
517 CService addrResolved(CNetAddr(strDest, fNameLookup && !nameproxyInfo.second), port);
518 if (addrResolved.IsValid()) {
520 return ConnectSocket(addr, hSocketRet, nTimeout);
522 addr = CService("0.0.0.0:0");
523 if (!nameproxyInfo.second)
525 if (!ConnectSocketDirectly(nameproxyInfo.first, hSocket, nTimeout))
528 switch(nameproxyInfo.second)
531 case 4: return false;
533 if (!Socks5(strDest, port, hSocket))
538 hSocketRet = hSocket;
542 void CNetAddr::Init()
547 void CNetAddr::SetIP(const CNetAddr& ipIn)
549 memcpy(ip, ipIn.ip, sizeof(ip));
557 CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
559 memcpy(ip, pchIPv4, 12);
560 memcpy(ip+12, &ipv4Addr, 4);
564 CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr)
566 memcpy(ip, &ipv6Addr, 16);
570 CNetAddr::CNetAddr(const char *pszIp, bool fAllowLookup)
573 std::vector<CNetAddr> vIP;
574 if (LookupHost(pszIp, vIP, 1, fAllowLookup))
578 CNetAddr::CNetAddr(const std::string &strIp, bool fAllowLookup)
581 std::vector<CNetAddr> vIP;
582 if (LookupHost(strIp.c_str(), vIP, 1, fAllowLookup))
586 int CNetAddr::GetByte(int n) const
591 bool CNetAddr::IsIPv4() const
593 return (memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0);
596 bool CNetAddr::IsIPv6() const
601 bool CNetAddr::IsRFC1918() const
605 (GetByte(3) == 192 && GetByte(2) == 168) ||
606 (GetByte(3) == 172 && (GetByte(2) >= 16 && GetByte(2) <= 31)));
609 bool CNetAddr::IsRFC3927() const
611 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
614 bool CNetAddr::IsRFC3849() const
616 return GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x0D && GetByte(12) == 0xB8;
619 bool CNetAddr::IsRFC3964() const
621 return (GetByte(15) == 0x20 && GetByte(14) == 0x02);
624 bool CNetAddr::IsRFC6052() const
626 static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
627 return (memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0);
630 bool CNetAddr::IsRFC4380() const
632 return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0 && GetByte(12) == 0);
635 bool CNetAddr::IsRFC4862() const
637 static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
638 return (memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0);
641 bool CNetAddr::IsRFC4193() const
643 return ((GetByte(15) & 0xFE) == 0xFC);
646 bool CNetAddr::IsRFC6145() const
648 static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
649 return (memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0);
652 bool CNetAddr::IsRFC4843() const
654 return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 && (GetByte(12) & 0xF0) == 0x10);
657 bool CNetAddr::IsOnionCat() const
659 static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};
660 return (memcmp(ip, pchOnionCat, sizeof(pchOnionCat)) == 0);
663 bool CNetAddr::IsGarliCat() const
665 static const unsigned char pchGarliCat[] = {0xFD,0x60,0xDB,0x4D,0xDD,0xB5};
666 return (memcmp(ip, pchGarliCat, sizeof(pchGarliCat)) == 0);
669 bool CNetAddr::IsLocal() const
672 if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0))
675 // IPv6 loopback (::1/128)
676 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
677 if (memcmp(ip, pchLocal, 16) == 0)
683 bool CNetAddr::IsMulticast() const
685 return (IsIPv4() && (GetByte(3) & 0xF0) == 0xE0)
686 || (GetByte(15) == 0xFF);
689 bool CNetAddr::IsValid() const
691 // Clean up 3-byte shifted addresses caused by garbage in size field
692 // of addr messages from versions before 0.2.9 checksum.
693 // Two consecutive addr messages look like this:
694 // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
695 // so if the first length field is garbled, it reads the second batch
696 // of addr misaligned by 3 bytes.
697 if (memcmp(ip, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
700 // unspecified IPv6 address (::/128)
701 unsigned char ipNone[16] = {};
702 if (memcmp(ip, ipNone, 16) == 0)
705 // documentation IPv6 address
712 uint32_t ipNone = INADDR_NONE;
713 if (memcmp(ip+12, &ipNone, 4) == 0)
718 if (memcmp(ip+12, &ipNone, 4) == 0)
725 bool CNetAddr::IsRoutable() const
727 return IsValid() && !(IsRFC1918() || IsRFC3927() || IsRFC4862() || (IsRFC4193() && !IsOnionCat() && !IsGarliCat()) || IsRFC4843() || IsLocal());
730 enum Network CNetAddr::GetNetwork() const
733 return NET_UNROUTABLE;
747 std::string CNetAddr::ToStringIP() const
749 CService serv(*this, 0);
751 struct sockaddr_storage sockaddr;
753 struct sockaddr sockaddr;
755 socklen_t socklen = sizeof(sockaddr);
756 if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
757 char name[1025] = "";
758 if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), NULL, 0, NI_NUMERICHOST))
759 return std::string(name);
762 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
764 return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
765 GetByte(15) << 8 | GetByte(14), GetByte(13) << 8 | GetByte(12),
766 GetByte(11) << 8 | GetByte(10), GetByte(9) << 8 | GetByte(8),
767 GetByte(7) << 8 | GetByte(6), GetByte(5) << 8 | GetByte(4),
768 GetByte(3) << 8 | GetByte(2), GetByte(1) << 8 | GetByte(0));
771 std::string CNetAddr::ToString() const
776 bool operator==(const CNetAddr& a, const CNetAddr& b)
778 return (memcmp(a.ip, b.ip, 16) == 0);
781 bool operator!=(const CNetAddr& a, const CNetAddr& b)
783 return (memcmp(a.ip, b.ip, 16) != 0);
786 bool operator<(const CNetAddr& a, const CNetAddr& b)
788 return (memcmp(a.ip, b.ip, 16) < 0);
791 bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
795 memcpy(pipv4Addr, ip+12, 4);
800 bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
802 memcpy(pipv6Addr, ip, 16);
807 // get canonical identifier of an address' group
808 // no two connections will be attempted to addresses with the same group
809 std::vector<unsigned char> CNetAddr::GetGroup() const
811 std::vector<unsigned char> vchRet;
812 int nClass = NET_IPV6;
816 // all local addresses belong to the same group
823 // all unroutable addresses belong to the same group
826 nClass = NET_UNROUTABLE;
829 // for IPv4 addresses, '1' + the 16 higher-order bits of the IP
830 // includes mapped IPv4, SIIT translated IPv4, and the well-known prefix
831 else if (IsIPv4() || IsRFC6145() || IsRFC6052())
836 // for 6to4 tunneled addresses, use the encapsulated IPv4 address
837 else if (IsRFC3964())
842 // for Teredo-tunneled IPv6 addresses, use the encapsulated IPv4 address
843 else if (IsRFC4380())
845 vchRet.push_back(NET_IPV4);
846 vchRet.push_back(GetByte(3) ^ 0xFF);
847 vchRet.push_back(GetByte(2) ^ 0xFF);
850 // for he.net, use /36 groups
851 else if (GetByte(15) == 0x20 && GetByte(14) == 0x11 && GetByte(13) == 0x04 && GetByte(12) == 0x70)
853 // for the rest of the IPv6 network, use /32 groups
857 vchRet.push_back(nClass);
860 vchRet.push_back(GetByte(15 - nStartByte));
865 vchRet.push_back(GetByte(15 - nStartByte) | ((1 << nBits) - 1));
870 int64 CNetAddr::GetHash() const
872 uint256 hash = Hash(&ip[0], &ip[16]);
874 memcpy(&nRet, &hash, sizeof(nRet));
878 void CNetAddr::print() const
880 printf("CNetAddr(%s)\n", ToString().c_str());
883 // for IPv6 partners: for unknown/Teredo partners: for IPv4 partners:
884 // 0 - unroutable // 0 - unroutable // 0 - unroutable
885 // 1 - teredo // 1 - teredo // 1 - ipv4
886 // 2 - tunneled ipv6 // 2 - tunneled ipv6
887 // 3 - ipv4 // 3 - ipv6
888 // 4 - ipv6 // 4 - ipv4
889 int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
891 if (!IsValid() || !IsRoutable())
893 if (paddrPartner && paddrPartner->IsIPv4())
894 return IsIPv4() ? 1 : 0;
897 if (IsRFC3964() || IsRFC6052())
899 bool fRealIPv6 = paddrPartner && !paddrPartner->IsRFC4380() && paddrPartner->IsValid() && paddrPartner->IsRoutable();
901 return IsIPv4() ? 3 : 4;
903 return IsIPv4() ? 4 : 3;
906 void CService::Init()
916 CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
920 CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn)
925 CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn)
930 CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
932 assert(addr.sin_family == AF_INET);
936 CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr), port(ntohs(addr.sin6_port))
938 assert(addr.sin6_family == AF_INET6);
942 bool CService::SetSockAddr(const struct sockaddr *paddr)
944 switch (paddr->sa_family) {
946 *this = CService(*(const struct sockaddr_in*)paddr);
950 *this = CService(*(const struct sockaddr_in6*)paddr);
958 CService::CService(const char *pszIpPort, bool fAllowLookup)
962 if (Lookup(pszIpPort, ip, 0, fAllowLookup))
966 CService::CService(const char *pszIpPort, int portDefault, bool fAllowLookup)
970 if (Lookup(pszIpPort, ip, portDefault, fAllowLookup))
974 CService::CService(const std::string &strIpPort, bool fAllowLookup)
978 if (Lookup(strIpPort.c_str(), ip, 0, fAllowLookup))
982 CService::CService(const std::string &strIpPort, int portDefault, bool fAllowLookup)
986 if (Lookup(strIpPort.c_str(), ip, portDefault, fAllowLookup))
990 unsigned short CService::GetPort() const
995 bool operator==(const CService& a, const CService& b)
997 return (CNetAddr)a == (CNetAddr)b && a.port == b.port;
1000 bool operator!=(const CService& a, const CService& b)
1002 return (CNetAddr)a != (CNetAddr)b || a.port != b.port;
1005 bool operator<(const CService& a, const CService& b)
1007 return (CNetAddr)a < (CNetAddr)b || ((CNetAddr)a == (CNetAddr)b && a.port < b.port);
1010 bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
1013 if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
1015 *addrlen = sizeof(struct sockaddr_in);
1016 struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
1017 memset(paddrin, 0, *addrlen);
1018 if (!GetInAddr(&paddrin->sin_addr))
1020 paddrin->sin_family = AF_INET;
1021 paddrin->sin_port = htons(port);
1026 if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
1028 *addrlen = sizeof(struct sockaddr_in6);
1029 struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
1030 memset(paddrin6, 0, *addrlen);
1031 if (!GetIn6Addr(&paddrin6->sin6_addr))
1033 paddrin6->sin6_family = AF_INET6;
1034 paddrin6->sin6_port = htons(port);
1041 std::vector<unsigned char> CService::GetKey() const
1043 std::vector<unsigned char> vKey;
1045 memcpy(&vKey[0], ip, 16);
1046 vKey[16] = port / 0x100;
1047 vKey[17] = port & 0x0FF;
1051 std::string CService::ToStringPort() const
1053 return strprintf("%i", port);
1056 std::string CService::ToStringIPPort() const
1059 return ToStringIP() + ":" + ToStringPort();
1061 return "[" + ToStringIP() + "]:" + ToStringPort();
1065 std::string CService::ToString() const
1067 return ToStringIPPort();
1070 void CService::print() const
1072 printf("CService(%s)\n", ToString().c_str());
1075 void CService::SetPort(unsigned short portIn)