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 int nSocksVersion = 5;
20 int fUseProxy = false;
21 bool fProxyNameLookup = false;
22 bool fNameLookup = false;
23 CService addrProxy("127.0.0.1",9050);
24 int nConnectTimeout = 5000;
25 static bool vfNoProxy[NET_MAX] = {};
28 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
30 enum Network ParseNetwork(std::string net) {
32 if (net == "ipv4") return NET_IPV4;
33 if (net == "ipv6") return NET_IPV6;
34 if (net == "tor") return NET_TOR;
35 if (net == "i2p") return NET_I2P;
36 return NET_UNROUTABLE;
39 void SetNoProxy(enum Network net, bool fNoProxy) {
40 assert(net >= 0 && net < NET_MAX);
41 vfNoProxy[net] = fNoProxy;
44 bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
47 struct addrinfo aiHint;
48 memset(&aiHint, 0, sizeof(struct addrinfo));
50 aiHint.ai_socktype = SOCK_STREAM;
51 aiHint.ai_protocol = IPPROTO_TCP;
54 aiHint.ai_family = AF_UNSPEC;
55 aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST;
57 aiHint.ai_family = AF_INET;
58 aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST;
62 aiHint.ai_family = AF_UNSPEC;
63 aiHint.ai_flags = AI_ADDRCONFIG | (fAllowLookup ? 0 : AI_NUMERICHOST);
65 aiHint.ai_family = AF_INET;
66 aiHint.ai_flags = AI_ADDRCONFIG | (fAllowLookup ? 0 : AI_NUMERICHOST);
69 struct addrinfo *aiRes = NULL;
70 int nErr = getaddrinfo(pszName, NULL, &aiHint, &aiRes);
74 struct addrinfo *aiTrav = aiRes;
75 while (aiTrav != NULL && (nMaxSolutions == 0 || vIP.size() < nMaxSolutions))
77 if (aiTrav->ai_family == AF_INET)
79 assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in));
80 vIP.push_back(CNetAddr(((struct sockaddr_in*)(aiTrav->ai_addr))->sin_addr));
84 if (aiTrav->ai_family == AF_INET6)
86 assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in6));
87 vIP.push_back(CNetAddr(((struct sockaddr_in6*)(aiTrav->ai_addr))->sin6_addr));
91 aiTrav = aiTrav->ai_next;
96 return (vIP.size() > 0);
99 bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
105 strlcpy(psz, pszName, sizeof(psz));
106 if (psz[0] == '[' && psz[strlen(psz)-1] == ']')
109 psz[strlen(psz)-1] = 0;
112 return LookupIntern(pszHost, vIP, nMaxSolutions, fAllowLookup);
115 bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions)
117 return LookupHost(pszName, vIP, nMaxSolutions, false);
120 bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
124 int port = portDefault;
127 strlcpy(psz, pszName, sizeof(psz));
128 char* pszColon = strrchr(psz+1,':');
129 char *pszPortEnd = NULL;
130 int portParsed = pszColon ? strtoul(pszColon+1, &pszPortEnd, 10) : 0;
131 if (pszColon && pszPortEnd && pszPortEnd[0] == 0)
133 if (psz[0] == '[' && pszColon[-1] == ']')
140 if (port >= 0 && port <= USHRT_MAX)
145 if (psz[0] == '[' && psz[strlen(psz)-1] == ']')
148 psz[strlen(psz)-1] = 0;
153 std::vector<CNetAddr> vIP;
154 bool fRet = LookupIntern(pszHost, vIP, nMaxSolutions, fAllowLookup);
157 vAddr.resize(vIP.size());
158 for (unsigned int i = 0; i < vIP.size(); i++)
159 vAddr[i] = CService(vIP[i], port);
163 bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup)
165 std::vector<CService> vService;
166 bool fRet = Lookup(pszName, vService, portDefault, fAllowLookup, 1);
173 bool LookupNumeric(const char *pszName, CService& addr, int portDefault)
175 return Lookup(pszName, addr, portDefault, false);
178 bool static Socks4(const CService &addrDest, SOCKET& hSocket)
180 printf("SOCKS4 connecting %s\n", addrDest.ToString().c_str());
181 if (!addrDest.IsIPv4())
183 closesocket(hSocket);
184 return error("Proxy destination is not IPv4");
186 char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user";
187 struct sockaddr_in addr;
188 socklen_t len = sizeof(addr);
189 if (!addrDest.GetSockAddr((struct sockaddr*)&addr, &len) || addr.sin_family != AF_INET)
191 closesocket(hSocket);
192 return error("Cannot get proxy destination address");
194 memcpy(pszSocks4IP + 2, &addr.sin_port, 2);
195 memcpy(pszSocks4IP + 4, &addr.sin_addr, 4);
196 char* pszSocks4 = pszSocks4IP;
197 int nSize = sizeof(pszSocks4IP);
199 int ret = send(hSocket, pszSocks4, nSize, MSG_NOSIGNAL);
202 closesocket(hSocket);
203 return error("Error sending to proxy");
206 if (recv(hSocket, pchRet, 8, 0) != 8)
208 closesocket(hSocket);
209 return error("Error reading proxy response");
211 if (pchRet[1] != 0x5a)
213 closesocket(hSocket);
214 if (pchRet[1] != 0x5b)
215 printf("ERROR: Proxy returned error %d\n", pchRet[1]);
218 printf("SOCKS4 connected %s\n", addrDest.ToString().c_str());
222 bool static Socks5(string strDest, int port, SOCKET& hSocket)
224 printf("SOCKS5 connecting %s\n", strDest.c_str());
225 if (strDest.size() > 255)
227 closesocket(hSocket);
228 return error("Hostname too long");
230 char pszSocks5Init[] = "\5\1\0";
231 char *pszSocks5 = pszSocks5Init;
232 ssize_t nSize = sizeof(pszSocks5Init);
234 ssize_t ret = send(hSocket, pszSocks5, nSize, MSG_NOSIGNAL);
237 closesocket(hSocket);
238 return error("Error sending to proxy");
241 if (recv(hSocket, pchRet1, 2, 0) != 2)
243 closesocket(hSocket);
244 return error("Error reading proxy response");
246 if (pchRet1[0] != 0x05 || pchRet1[1] != 0x00)
248 closesocket(hSocket);
249 return error("Proxy failed to initialize");
251 string strSocks5("\5\1");
252 strSocks5 += '\000'; strSocks5 += '\003';
253 strSocks5 += static_cast<char>(std::min((int)strDest.size(), 255));
254 strSocks5 += strDest;
255 strSocks5 += static_cast<char>((port >> 8) & 0xFF);
256 strSocks5 += static_cast<char>((port >> 0) & 0xFF);
257 ret = send(hSocket, strSocks5.c_str(), strSocks5.size(), MSG_NOSIGNAL);
258 if (ret != (ssize_t)strSocks5.size())
260 closesocket(hSocket);
261 return error("Error sending to proxy");
264 if (recv(hSocket, pchRet2, 4, 0) != 4)
266 closesocket(hSocket);
267 return error("Error reading proxy response");
269 if (pchRet2[0] != 0x05)
271 closesocket(hSocket);
272 return error("Proxy failed to accept request");
274 if (pchRet2[1] != 0x00)
276 closesocket(hSocket);
279 case 0x01: return error("Proxy error: general failure");
280 case 0x02: return error("Proxy error: connection not allowed");
281 case 0x03: return error("Proxy error: network unreachable");
282 case 0x04: return error("Proxy error: host unreachable");
283 case 0x05: return error("Proxy error: connection refused");
284 case 0x06: return error("Proxy error: TTL expired");
285 case 0x07: return error("Proxy error: protocol error");
286 case 0x08: return error("Proxy error: address type not supported");
287 default: return error("Proxy error: unknown");
290 if (pchRet2[2] != 0x00)
292 closesocket(hSocket);
293 return error("Error: malformed proxy response");
298 case 0x01: ret = recv(hSocket, pchRet3, 4, 0) != 4; break;
299 case 0x04: ret = recv(hSocket, pchRet3, 16, 0) != 16; break;
302 ret = recv(hSocket, pchRet3, 1, 0) != 1;
304 return error("Error reading from proxy");
305 int nRecv = pchRet3[0];
306 ret = recv(hSocket, pchRet3, nRecv, 0) != nRecv;
309 default: closesocket(hSocket); return error("Error: malformed proxy response");
313 closesocket(hSocket);
314 return error("Error reading from proxy");
316 if (recv(hSocket, pchRet3, 2, 0) != 2)
318 closesocket(hSocket);
319 return error("Error reading from proxy");
321 printf("SOCKS5 connected %s\n", strDest.c_str());
325 bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRet, int nTimeout)
327 hSocketRet = INVALID_SOCKET;
330 struct sockaddr_storage sockaddr;
332 struct sockaddr sockaddr;
334 socklen_t len = sizeof(sockaddr);
335 if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
336 printf("Cannot connect to %s: unsupported network\n", addrConnect.ToString().c_str());
340 SOCKET hSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
341 if (hSocket == INVALID_SOCKET)
345 setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
349 u_long fNonblock = 1;
350 if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
352 int fFlags = fcntl(hSocket, F_GETFL, 0);
353 if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
356 closesocket(hSocket);
360 if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
362 // WSAEINVAL is here because some legacy version of winsock uses it
363 if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAEINVAL)
365 struct timeval timeout;
366 timeout.tv_sec = nTimeout / 1000;
367 timeout.tv_usec = (nTimeout % 1000) * 1000;
371 FD_SET(hSocket, &fdset);
372 int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout);
375 printf("connection timeout\n");
376 closesocket(hSocket);
379 if (nRet == SOCKET_ERROR)
381 printf("select() for connection failed: %i\n",WSAGetLastError());
382 closesocket(hSocket);
385 socklen_t nRetSize = sizeof(nRet);
387 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
389 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
392 printf("getsockopt() for connection failed: %i\n",WSAGetLastError());
393 closesocket(hSocket);
398 printf("connect() failed after select(): %s\n",strerror(nRet));
399 closesocket(hSocket);
404 else if (WSAGetLastError() != WSAEISCONN)
409 printf("connect() failed: %i\n",WSAGetLastError());
410 closesocket(hSocket);
415 // this isn't even strictly necessary
416 // CNode::ConnectNode immediately turns the socket back to non-blocking
417 // but we'll turn it back to blocking just in case
420 if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
422 fFlags = fcntl(hSocket, F_GETFL, 0);
423 if (fcntl(hSocket, F_SETFL, fFlags & !O_NONBLOCK) == SOCKET_ERROR)
426 closesocket(hSocket);
430 hSocketRet = hSocket;
434 bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
436 SOCKET hSocket = INVALID_SOCKET;
437 bool fProxy = (fUseProxy && addrDest.IsRoutable() && !vfNoProxy[addrDest.GetNetwork()]);
439 if (!ConnectSocketDirectly(fProxy ? addrProxy : addrDest, hSocket, nTimeout))
444 switch(nSocksVersion)
447 if (!Socks4(addrDest, hSocket))
453 if (!Socks5(addrDest.ToStringIP(), addrDest.GetPort(), hSocket))
459 hSocketRet = hSocket;
463 bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout)
465 string strDest(pszDest);
466 int port = portDefault;
468 size_t colon = strDest.find_last_of(':');
469 if (colon != strDest.npos) {
471 int n = strtol(pszDest + colon + 1, &endp, 10);
472 if (endp && *endp == 0 && n >= 0) {
473 strDest = strDest.substr(0, colon);
474 if (n > 0 && n < 0x10000)
478 if (strDest[0] == '[' && strDest[strDest.size()-1] == ']')
479 strDest = strDest.substr(1, strDest.size()-2);
481 SOCKET hSocket = INVALID_SOCKET;
482 CService addrResolved(CNetAddr(strDest, fNameLookup && !fProxyNameLookup), port);
483 if (addrResolved.IsValid()) {
485 return ConnectSocket(addr, hSocketRet, nTimeout);
487 addr = CService("0.0.0.0:0");
490 if (!ConnectSocketDirectly(addrProxy, hSocket, nTimeout))
493 switch(nSocksVersion)
495 case 4: return false;
498 if (!Socks5(strDest, port, hSocket))
503 hSocketRet = hSocket;
507 void CNetAddr::Init()
512 void CNetAddr::SetIP(const CNetAddr& ipIn)
514 memcpy(ip, ipIn.ip, sizeof(ip));
522 CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
524 memcpy(ip, pchIPv4, 12);
525 memcpy(ip+12, &ipv4Addr, 4);
529 CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr)
531 memcpy(ip, &ipv6Addr, 16);
535 CNetAddr::CNetAddr(const char *pszIp, bool fAllowLookup)
538 std::vector<CNetAddr> vIP;
539 if (LookupHost(pszIp, vIP, 1, fAllowLookup))
543 CNetAddr::CNetAddr(const std::string &strIp, bool fAllowLookup)
546 std::vector<CNetAddr> vIP;
547 if (LookupHost(strIp.c_str(), vIP, 1, fAllowLookup))
551 int CNetAddr::GetByte(int n) const
556 bool CNetAddr::IsIPv4() const
558 return (memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0);
561 bool CNetAddr::IsIPv6() const
566 bool CNetAddr::IsRFC1918() const
570 (GetByte(3) == 192 && GetByte(2) == 168) ||
571 (GetByte(3) == 172 && (GetByte(2) >= 16 && GetByte(2) <= 31)));
574 bool CNetAddr::IsRFC3927() const
576 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
579 bool CNetAddr::IsRFC3849() const
581 return GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x0D && GetByte(12) == 0xB8;
584 bool CNetAddr::IsRFC3964() const
586 return (GetByte(15) == 0x20 && GetByte(14) == 0x02);
589 bool CNetAddr::IsRFC6052() const
591 static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
592 return (memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0);
595 bool CNetAddr::IsRFC4380() const
597 return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0 && GetByte(12) == 0);
600 bool CNetAddr::IsRFC4862() const
602 static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
603 return (memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0);
606 bool CNetAddr::IsRFC4193() const
608 return ((GetByte(15) & 0xFE) == 0xFC);
611 bool CNetAddr::IsRFC6145() const
613 static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
614 return (memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0);
617 bool CNetAddr::IsRFC4843() const
619 return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 && (GetByte(12) & 0xF0) == 0x10);
622 bool CNetAddr::IsOnionCat() const
624 static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};
625 return (memcmp(ip, pchOnionCat, sizeof(pchOnionCat)) == 0);
628 bool CNetAddr::IsGarliCat() const
630 static const unsigned char pchGarliCat[] = {0xFD,0x60,0xDB,0x4D,0xDD,0xB5};
631 return (memcmp(ip, pchGarliCat, sizeof(pchGarliCat)) == 0);
634 bool CNetAddr::IsLocal() const
637 if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0))
640 // IPv6 loopback (::1/128)
641 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
642 if (memcmp(ip, pchLocal, 16) == 0)
648 bool CNetAddr::IsMulticast() const
650 return (IsIPv4() && (GetByte(3) & 0xF0) == 0xE0)
651 || (GetByte(15) == 0xFF);
654 bool CNetAddr::IsValid() const
656 // Clean up 3-byte shifted addresses caused by garbage in size field
657 // of addr messages from versions before 0.2.9 checksum.
658 // Two consecutive addr messages look like this:
659 // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
660 // so if the first length field is garbled, it reads the second batch
661 // of addr misaligned by 3 bytes.
662 if (memcmp(ip, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
665 // unspecified IPv6 address (::/128)
666 unsigned char ipNone[16] = {};
667 if (memcmp(ip, ipNone, 16) == 0)
670 // documentation IPv6 address
677 uint32_t ipNone = INADDR_NONE;
678 if (memcmp(ip+12, &ipNone, 4) == 0)
683 if (memcmp(ip+12, &ipNone, 4) == 0)
690 bool CNetAddr::IsRoutable() const
692 return IsValid() && !(IsRFC1918() || IsRFC3927() || IsRFC4862() || (IsRFC4193() && !IsOnionCat() && !IsGarliCat()) || IsRFC4843() || IsLocal());
695 enum Network CNetAddr::GetNetwork() const
698 return NET_UNROUTABLE;
712 std::string CNetAddr::ToStringIP() const
714 CService serv(*this, 0);
716 struct sockaddr_storage sockaddr;
718 struct sockaddr sockaddr;
720 socklen_t socklen = sizeof(sockaddr);
721 if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
722 char name[1025] = "";
723 if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), NULL, 0, NI_NUMERICHOST))
724 return std::string(name);
727 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
729 return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
730 GetByte(15) << 8 | GetByte(14), GetByte(13) << 8 | GetByte(12),
731 GetByte(11) << 8 | GetByte(10), GetByte(9) << 8 | GetByte(8),
732 GetByte(7) << 8 | GetByte(6), GetByte(5) << 8 | GetByte(4),
733 GetByte(3) << 8 | GetByte(2), GetByte(1) << 8 | GetByte(0));
736 std::string CNetAddr::ToString() const
741 bool operator==(const CNetAddr& a, const CNetAddr& b)
743 return (memcmp(a.ip, b.ip, 16) == 0);
746 bool operator!=(const CNetAddr& a, const CNetAddr& b)
748 return (memcmp(a.ip, b.ip, 16) != 0);
751 bool operator<(const CNetAddr& a, const CNetAddr& b)
753 return (memcmp(a.ip, b.ip, 16) < 0);
756 bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
760 memcpy(pipv4Addr, ip+12, 4);
765 bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
767 memcpy(pipv6Addr, ip, 16);
772 // get canonical identifier of an address' group
773 // no two connections will be attempted to addresses with the same group
774 std::vector<unsigned char> CNetAddr::GetGroup() const
776 std::vector<unsigned char> vchRet;
777 int nClass = NET_IPV6;
781 // all local addresses belong to the same group
788 // all unroutable addresses belong to the same group
791 nClass = NET_UNROUTABLE;
794 // for IPv4 addresses, '1' + the 16 higher-order bits of the IP
795 // includes mapped IPv4, SIIT translated IPv4, and the well-known prefix
796 else if (IsIPv4() || IsRFC6145() || IsRFC6052())
801 // for 6to4 tunneled addresses, use the encapsulated IPv4 address
802 else if (IsRFC3964())
807 // for Teredo-tunneled IPv6 addresses, use the encapsulated IPv4 address
808 else if (IsRFC4380())
810 vchRet.push_back(NET_IPV4);
811 vchRet.push_back(GetByte(3) ^ 0xFF);
812 vchRet.push_back(GetByte(2) ^ 0xFF);
815 // for he.net, use /36 groups
816 else if (GetByte(15) == 0x20 && GetByte(14) == 0x11 && GetByte(13) == 0x04 && GetByte(12) == 0x70)
818 // for the rest of the IPv6 network, use /32 groups
822 vchRet.push_back(nClass);
825 vchRet.push_back(GetByte(15 - nStartByte));
830 vchRet.push_back(GetByte(15 - nStartByte) | ((1 << nBits) - 1));
835 int64 CNetAddr::GetHash() const
837 uint256 hash = Hash(&ip[0], &ip[16]);
839 memcpy(&nRet, &hash, sizeof(nRet));
843 void CNetAddr::print() const
845 printf("CNetAddr(%s)\n", ToString().c_str());
848 // for IPv6 partners: for unknown/Teredo partners: for IPv4 partners:
849 // 0 - unroutable // 0 - unroutable // 0 - unroutable
850 // 1 - teredo // 1 - teredo // 1 - ipv4
851 // 2 - tunneled ipv6 // 2 - tunneled ipv6
852 // 3 - ipv4 // 3 - ipv6
853 // 4 - ipv6 // 4 - ipv4
854 int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
856 if (!IsValid() || !IsRoutable())
858 if (paddrPartner && paddrPartner->IsIPv4())
859 return IsIPv4() ? 1 : 0;
862 if (IsRFC3964() || IsRFC6052())
864 bool fRealIPv6 = paddrPartner && !paddrPartner->IsRFC4380() && paddrPartner->IsValid() && paddrPartner->IsRoutable();
866 return IsIPv4() ? 3 : 4;
868 return IsIPv4() ? 4 : 3;
871 void CService::Init()
881 CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
885 CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn)
890 CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn)
895 CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
897 assert(addr.sin_family == AF_INET);
901 CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr), port(ntohs(addr.sin6_port))
903 assert(addr.sin6_family == AF_INET6);
907 bool CService::SetSockAddr(const struct sockaddr *paddr)
909 switch (paddr->sa_family) {
911 *this = CService(*(const struct sockaddr_in*)paddr);
915 *this = CService(*(const struct sockaddr_in6*)paddr);
923 CService::CService(const char *pszIpPort, bool fAllowLookup)
927 if (Lookup(pszIpPort, ip, 0, fAllowLookup))
931 CService::CService(const char *pszIpPort, int portDefault, bool fAllowLookup)
935 if (Lookup(pszIpPort, ip, portDefault, fAllowLookup))
939 CService::CService(const std::string &strIpPort, bool fAllowLookup)
943 if (Lookup(strIpPort.c_str(), ip, 0, fAllowLookup))
947 CService::CService(const std::string &strIpPort, int portDefault, bool fAllowLookup)
951 if (Lookup(strIpPort.c_str(), ip, portDefault, fAllowLookup))
955 unsigned short CService::GetPort() const
960 bool operator==(const CService& a, const CService& b)
962 return (CNetAddr)a == (CNetAddr)b && a.port == b.port;
965 bool operator!=(const CService& a, const CService& b)
967 return (CNetAddr)a != (CNetAddr)b || a.port != b.port;
970 bool operator<(const CService& a, const CService& b)
972 return (CNetAddr)a < (CNetAddr)b || ((CNetAddr)a == (CNetAddr)b && a.port < b.port);
975 bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
978 if (*addrlen < sizeof(struct sockaddr_in))
980 *addrlen = sizeof(struct sockaddr_in);
981 struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
982 memset(paddrin, 0, *addrlen);
983 if (!GetInAddr(&paddrin->sin_addr))
985 paddrin->sin_family = AF_INET;
986 paddrin->sin_port = htons(port);
991 if (*addrlen < sizeof(struct sockaddr_in6))
993 *addrlen = sizeof(struct sockaddr_in6);
994 struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
995 memset(paddrin6, 0, *addrlen);
996 if (!GetIn6Addr(&paddrin6->sin6_addr))
998 paddrin6->sin6_family = AF_INET6;
999 paddrin6->sin6_port = htons(port);
1006 std::vector<unsigned char> CService::GetKey() const
1008 std::vector<unsigned char> vKey;
1010 memcpy(&vKey[0], ip, 16);
1011 vKey[16] = port / 0x100;
1012 vKey[17] = port & 0x0FF;
1016 std::string CService::ToStringPort() const
1018 return strprintf("%i", port);
1021 std::string CService::ToStringIPPort() const
1024 return ToStringIP() + ":" + ToStringPort();
1026 return "[" + ToStringIP() + "]:" + ToStringPort();
1030 std::string CService::ToString() const
1032 return ToStringIPPort();
1035 void CService::print() const
1037 printf("CService(%s)\n", ToString().c_str());
1040 void CService::SetPort(unsigned short portIn)