You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netbase.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2011 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  5. #include "netbase.h"
  6. #include "util.h"
  7. #ifndef WIN32
  8. #include <sys/fcntl.h>
  9. #endif
  10. #include "strlcpy.h"
  11. using namespace std;
  12. // Settings
  13. int fUseProxy = false;
  14. CService addrProxy("127.0.0.1",9050);
  15. int nConnectTimeout = 5000;
  16. static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
  17. bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, int nMaxSolutions, bool fAllowLookup)
  18. {
  19. vIP.clear();
  20. struct addrinfo aiHint;
  21. memset(&aiHint, 0, sizeof(struct addrinfo));
  22. aiHint.ai_socktype = SOCK_STREAM;
  23. aiHint.ai_protocol = IPPROTO_TCP;
  24. #ifdef WIN32
  25. # ifdef USE_IPV6
  26. aiHint.ai_family = AF_UNSPEC;
  27. aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST;
  28. # else
  29. aiHint.ai_family = AF_INET;
  30. aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST;
  31. # endif
  32. #else
  33. # ifdef USE_IPV6
  34. aiHint.ai_family = AF_UNSPEC;
  35. aiHint.ai_flags = AI_ADDRCONFIG | (fAllowLookup ? 0 : AI_NUMERICHOST);
  36. # else
  37. aiHint.ai_family = AF_INET;
  38. aiHint.ai_flags = AI_ADDRCONFIG | (fAllowLookup ? 0 : AI_NUMERICHOST);
  39. # endif
  40. #endif
  41. struct addrinfo *aiRes = NULL;
  42. int nErr = getaddrinfo(pszName, NULL, &aiHint, &aiRes);
  43. if (nErr)
  44. return false;
  45. struct addrinfo *aiTrav = aiRes;
  46. while (aiTrav != NULL && (nMaxSolutions == 0 || vIP.size() < nMaxSolutions))
  47. {
  48. if (aiTrav->ai_family == AF_INET)
  49. {
  50. assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in));
  51. vIP.push_back(CNetAddr(((struct sockaddr_in*)(aiTrav->ai_addr))->sin_addr));
  52. }
  53. #ifdef USE_IPV6
  54. if (aiTrav->ai_family == AF_INET6)
  55. {
  56. assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in6));
  57. vIP.push_back(CNetAddr(((struct sockaddr_in6*)(aiTrav->ai_addr))->sin6_addr));
  58. }
  59. #endif
  60. aiTrav = aiTrav->ai_next;
  61. }
  62. freeaddrinfo(aiRes);
  63. return (vIP.size() > 0);
  64. }
  65. bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, int nMaxSolutions, bool fAllowLookup)
  66. {
  67. if (pszName[0] == 0)
  68. return false;
  69. char psz[256];
  70. char *pszHost = psz;
  71. strlcpy(psz, pszName, sizeof(psz));
  72. if (psz[0] == '[' && psz[strlen(psz)-1] == ']')
  73. {
  74. pszHost = psz+1;
  75. psz[strlen(psz)-1] = 0;
  76. }
  77. return LookupIntern(pszHost, vIP, nMaxSolutions, fAllowLookup);
  78. }
  79. bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, int nMaxSolutions)
  80. {
  81. return LookupHost(pszName, vIP, nMaxSolutions, false);
  82. }
  83. bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup)
  84. {
  85. if (pszName[0] == 0)
  86. return false;
  87. int port = portDefault;
  88. char psz[256];
  89. char *pszHost = psz;
  90. strlcpy(psz, pszName, sizeof(psz));
  91. char* pszColon = strrchr(psz+1,':');
  92. char *pszPortEnd = NULL;
  93. int portParsed = pszColon ? strtoul(pszColon+1, &pszPortEnd, 10) : 0;
  94. if (pszColon && pszPortEnd && pszPortEnd[0] == 0)
  95. {
  96. if (psz[0] == '[' && pszColon[-1] == ']')
  97. {
  98. pszHost = psz+1;
  99. pszColon[-1] = 0;
  100. }
  101. else
  102. pszColon[0] = 0;
  103. if (port >= 0 && port <= USHRT_MAX)
  104. port = portParsed;
  105. }
  106. else
  107. {
  108. if (psz[0] == '[' && psz[strlen(psz)-1] == ']')
  109. {
  110. pszHost = psz+1;
  111. psz[strlen(psz)-1] = 0;
  112. }
  113. }
  114. std::vector<CNetAddr> vIP;
  115. bool fRet = LookupIntern(pszHost, vIP, 1, fAllowLookup);
  116. if (!fRet)
  117. return false;
  118. addr = CService(vIP[0], port);
  119. return true;
  120. }
  121. bool LookupNumeric(const char *pszName, CService& addr, int portDefault)
  122. {
  123. return Lookup(pszName, addr, portDefault, false);
  124. }
  125. bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
  126. {
  127. hSocketRet = INVALID_SOCKET;
  128. SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  129. if (hSocket == INVALID_SOCKET)
  130. return false;
  131. #ifdef SO_NOSIGPIPE
  132. int set = 1;
  133. setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
  134. #endif
  135. bool fProxy = (fUseProxy && addrDest.IsRoutable());
  136. struct sockaddr_in sockaddr;
  137. if (fProxy)
  138. addrProxy.GetSockAddr(&sockaddr);
  139. else
  140. addrDest.GetSockAddr(&sockaddr);
  141. #ifdef WIN32
  142. u_long fNonblock = 1;
  143. if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
  144. #else
  145. int fFlags = fcntl(hSocket, F_GETFL, 0);
  146. if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
  147. #endif
  148. {
  149. closesocket(hSocket);
  150. return false;
  151. }
  152. if (connect(hSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
  153. {
  154. // WSAEINVAL is here because some legacy version of winsock uses it
  155. if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAEINVAL)
  156. {
  157. struct timeval timeout;
  158. timeout.tv_sec = nTimeout / 1000;
  159. timeout.tv_usec = (nTimeout % 1000) * 1000;
  160. fd_set fdset;
  161. FD_ZERO(&fdset);
  162. FD_SET(hSocket, &fdset);
  163. int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout);
  164. if (nRet == 0)
  165. {
  166. printf("connection timeout\n");
  167. closesocket(hSocket);
  168. return false;
  169. }
  170. if (nRet == SOCKET_ERROR)
  171. {
  172. printf("select() for connection failed: %i\n",WSAGetLastError());
  173. closesocket(hSocket);
  174. return false;
  175. }
  176. socklen_t nRetSize = sizeof(nRet);
  177. #ifdef WIN32
  178. if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
  179. #else
  180. if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
  181. #endif
  182. {
  183. printf("getsockopt() for connection failed: %i\n",WSAGetLastError());
  184. closesocket(hSocket);
  185. return false;
  186. }
  187. if (nRet != 0)
  188. {
  189. printf("connect() failed after select(): %s\n",strerror(nRet));
  190. closesocket(hSocket);
  191. return false;
  192. }
  193. }
  194. #ifdef WIN32
  195. else if (WSAGetLastError() != WSAEISCONN)
  196. #else
  197. else
  198. #endif
  199. {
  200. printf("connect() failed: %i\n",WSAGetLastError());
  201. closesocket(hSocket);
  202. return false;
  203. }
  204. }
  205. // this isn't even strictly necessary
  206. // CNode::ConnectNode immediately turns the socket back to non-blocking
  207. // but we'll turn it back to blocking just in case
  208. #ifdef WIN32
  209. fNonblock = 0;
  210. if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
  211. #else
  212. fFlags = fcntl(hSocket, F_GETFL, 0);
  213. if (fcntl(hSocket, F_SETFL, fFlags & !O_NONBLOCK) == SOCKET_ERROR)
  214. #endif
  215. {
  216. closesocket(hSocket);
  217. return false;
  218. }
  219. if (fProxy)
  220. {
  221. printf("proxy connecting %s\n", addrDest.ToString().c_str());
  222. char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user";
  223. struct sockaddr_in addr;
  224. addrDest.GetSockAddr(&addr);
  225. memcpy(pszSocks4IP + 2, &addr.sin_port, 2);
  226. memcpy(pszSocks4IP + 4, &addr.sin_addr, 4);
  227. char* pszSocks4 = pszSocks4IP;
  228. int nSize = sizeof(pszSocks4IP);
  229. int ret = send(hSocket, pszSocks4, nSize, MSG_NOSIGNAL);
  230. if (ret != nSize)
  231. {
  232. closesocket(hSocket);
  233. return error("Error sending to proxy");
  234. }
  235. char pchRet[8];
  236. if (recv(hSocket, pchRet, 8, 0) != 8)
  237. {
  238. closesocket(hSocket);
  239. return error("Error reading proxy response");
  240. }
  241. if (pchRet[1] != 0x5a)
  242. {
  243. closesocket(hSocket);
  244. if (pchRet[1] != 0x5b)
  245. printf("ERROR: Proxy returned error %d\n", pchRet[1]);
  246. return false;
  247. }
  248. printf("proxy connected %s\n", addrDest.ToString().c_str());
  249. }
  250. hSocketRet = hSocket;
  251. return true;
  252. }
  253. void CNetAddr::Init()
  254. {
  255. memset(ip, 0, 16);
  256. }
  257. void CNetAddr::SetIP(const CNetAddr& ipIn)
  258. {
  259. memcpy(ip, ipIn.ip, sizeof(ip));
  260. }
  261. CNetAddr::CNetAddr()
  262. {
  263. Init();
  264. }
  265. CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
  266. {
  267. memcpy(ip, pchIPv4, 12);
  268. memcpy(ip+12, &ipv4Addr, 4);
  269. }
  270. #ifdef USE_IPV6
  271. CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr)
  272. {
  273. memcpy(ip, &ipv6Addr, 16);
  274. }
  275. #endif
  276. CNetAddr::CNetAddr(const char *pszIp, bool fAllowLookup)
  277. {
  278. Init();
  279. std::vector<CNetAddr> vIP;
  280. if (LookupHost(pszIp, vIP, 1, fAllowLookup))
  281. *this = vIP[0];
  282. }
  283. CNetAddr::CNetAddr(const std::string &strIp, bool fAllowLookup)
  284. {
  285. Init();
  286. std::vector<CNetAddr> vIP;
  287. if (LookupHost(strIp.c_str(), vIP, 1, fAllowLookup))
  288. *this = vIP[0];
  289. }
  290. int CNetAddr::GetByte(int n) const
  291. {
  292. return ip[15-n];
  293. }
  294. bool CNetAddr::IsIPv4() const
  295. {
  296. return (memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0);
  297. }
  298. bool CNetAddr::IsRFC1918() const
  299. {
  300. return IsIPv4() && (
  301. GetByte(3) == 10 ||
  302. (GetByte(3) == 192 && GetByte(2) == 168) ||
  303. (GetByte(3) == 172 && (GetByte(2) >= 16 && GetByte(2) <= 31)));
  304. }
  305. bool CNetAddr::IsRFC3927() const
  306. {
  307. return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
  308. }
  309. bool CNetAddr::IsRFC3849() const
  310. {
  311. return GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x0D && GetByte(12) == 0xB8;
  312. }
  313. bool CNetAddr::IsRFC3964() const
  314. {
  315. return (GetByte(15) == 0x20 && GetByte(14) == 0x02);
  316. }
  317. bool CNetAddr::IsRFC6052() const
  318. {
  319. static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
  320. return (memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0);
  321. }
  322. bool CNetAddr::IsRFC4380() const
  323. {
  324. return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0 && GetByte(12) == 0);
  325. }
  326. bool CNetAddr::IsRFC4862() const
  327. {
  328. static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
  329. return (memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0);
  330. }
  331. bool CNetAddr::IsRFC4193() const
  332. {
  333. return ((GetByte(15) & 0xFE) == 0xFC);
  334. }
  335. bool CNetAddr::IsRFC6145() const
  336. {
  337. static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
  338. return (memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0);
  339. }
  340. bool CNetAddr::IsRFC4843() const
  341. {
  342. return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x00 && GetByte(12) & 0xF0 == 0x10);
  343. }
  344. bool CNetAddr::IsLocal() const
  345. {
  346. // IPv4 loopback
  347. if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0))
  348. return true;
  349. // IPv6 loopback (::1/128)
  350. static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
  351. if (memcmp(ip, pchLocal, 16) == 0)
  352. return true;
  353. return false;
  354. }
  355. bool CNetAddr::IsMulticast() const
  356. {
  357. return (IsIPv4() && (GetByte(3) & 0xF0) == 0xE0)
  358. || (GetByte(15) == 0xFF);
  359. }
  360. bool CNetAddr::IsValid() const
  361. {
  362. // Clean up 3-byte shifted addresses caused by garbage in size field
  363. // of addr messages from versions before 0.2.9 checksum.
  364. // Two consecutive addr messages look like this:
  365. // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
  366. // so if the first length field is garbled, it reads the second batch
  367. // of addr misaligned by 3 bytes.
  368. if (memcmp(ip, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
  369. return false;
  370. // unspecified IPv6 address (::/128)
  371. unsigned char ipNone[16] = {};
  372. if (memcmp(ip, ipNone, 16) == 0)
  373. return false;
  374. // documentation IPv6 address
  375. if (IsRFC3849())
  376. return false;
  377. if (IsIPv4())
  378. {
  379. // INADDR_NONE
  380. uint32_t ipNone = INADDR_NONE;
  381. if (memcmp(ip+12, &ipNone, 4) == 0)
  382. return false;
  383. // 0
  384. ipNone = 0;
  385. if (memcmp(ip+12, &ipNone, 4) == 0)
  386. return false;
  387. }
  388. return true;
  389. }
  390. bool CNetAddr::IsRoutable() const
  391. {
  392. return IsValid() && !(IsRFC1918() || IsRFC3927() || IsRFC4862() || IsRFC4193() || IsRFC4843() || IsLocal());
  393. }
  394. std::string CNetAddr::ToStringIP() const
  395. {
  396. if (IsIPv4())
  397. return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
  398. else
  399. return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
  400. GetByte(15) << 8 | GetByte(14), GetByte(13) << 8 | GetByte(12),
  401. GetByte(11) << 8 | GetByte(10), GetByte(9) << 8 | GetByte(8),
  402. GetByte(7) << 8 | GetByte(6), GetByte(5) << 8 | GetByte(4),
  403. GetByte(3) << 8 | GetByte(2), GetByte(1) << 8 | GetByte(0));
  404. }
  405. std::string CNetAddr::ToString() const
  406. {
  407. return ToStringIP();
  408. }
  409. bool operator==(const CNetAddr& a, const CNetAddr& b)
  410. {
  411. return (memcmp(a.ip, b.ip, 16) == 0);
  412. }
  413. bool operator!=(const CNetAddr& a, const CNetAddr& b)
  414. {
  415. return (memcmp(a.ip, b.ip, 16) != 0);
  416. }
  417. bool operator<(const CNetAddr& a, const CNetAddr& b)
  418. {
  419. return (memcmp(a.ip, b.ip, 16) < 0);
  420. }
  421. bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
  422. {
  423. if (!IsIPv4())
  424. return false;
  425. memcpy(pipv4Addr, ip+12, 4);
  426. return true;
  427. }
  428. #ifdef USE_IPV6
  429. bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
  430. {
  431. memcpy(pipv6Addr, ip, 16);
  432. return true;
  433. }
  434. #endif
  435. // get canonical identifier of an address' group
  436. // no two connections will be attempted to addresses with the same group
  437. std::vector<unsigned char> CNetAddr::GetGroup() const
  438. {
  439. std::vector<unsigned char> vchRet;
  440. int nClass = 0; // 0=IPv6, 1=IPv4, 255=unroutable
  441. int nStartByte = 0;
  442. int nBits = 16;
  443. // for unroutable addresses, each address is considered different
  444. if (!IsRoutable())
  445. {
  446. nClass = 255;
  447. nBits = 128;
  448. }
  449. // for IPv4 addresses, '1' + the 16 higher-order bits of the IP
  450. // includes mapped IPv4, SIIT translated IPv4, and the well-known prefix
  451. else if (IsIPv4() || IsRFC6145() || IsRFC6052())
  452. {
  453. nClass = 1;
  454. nStartByte = 12;
  455. }
  456. // for 6to4 tunneled addresses, use the encapsulated IPv4 address
  457. else if (IsRFC3964())
  458. {
  459. nClass = 1;
  460. nStartByte = 2;
  461. }
  462. // for Teredo-tunneled IPv6 addresses, use the encapsulated IPv4 address
  463. else if (IsRFC4380())
  464. {
  465. vchRet.push_back(1);
  466. vchRet.push_back(GetByte(3) ^ 0xFF);
  467. vchRet.push_back(GetByte(2) ^ 0xFF);
  468. return vchRet;
  469. }
  470. // for he.net, use /36 groups
  471. else if (GetByte(15) == 0x20 && GetByte(14) == 0x11 && GetByte(13) == 0x04 && GetByte(12) == 0x70)
  472. nBits = 36;
  473. // for the rest of the IPv6 network, use /32 groups
  474. else
  475. nBits = 32;
  476. vchRet.push_back(nClass);
  477. while (nBits >= 8)
  478. {
  479. vchRet.push_back(GetByte(15 - nStartByte));
  480. nStartByte++;
  481. nBits -= 8;
  482. }
  483. if (nBits > 0)
  484. vchRet.push_back(GetByte(15 - nStartByte) | ((1 << nBits) - 1));
  485. return vchRet;
  486. }
  487. int64 CNetAddr::GetHash() const
  488. {
  489. uint256 hash = Hash(&ip[0], &ip[16]);
  490. int64 nRet;
  491. memcpy(&nRet, &hash, sizeof(nRet));
  492. return nRet;
  493. }
  494. void CNetAddr::print() const
  495. {
  496. printf("CNetAddr(%s)\n", ToString().c_str());
  497. }
  498. void CService::Init()
  499. {
  500. port = 0;
  501. }
  502. CService::CService()
  503. {
  504. Init();
  505. }
  506. CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
  507. {
  508. }
  509. CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn)
  510. {
  511. }
  512. #ifdef USE_IPV6
  513. CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn)
  514. {
  515. }
  516. #endif
  517. CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
  518. {
  519. assert(addr.sin_family == AF_INET);
  520. }
  521. #ifdef USE_IPV6
  522. CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr), port(ntohs(addr.sin6_port))
  523. {
  524. assert(addr.sin6_family == AF_INET6);
  525. }
  526. #endif
  527. CService::CService(const char *pszIpPort, bool fAllowLookup)
  528. {
  529. Init();
  530. CService ip;
  531. if (Lookup(pszIpPort, ip, 0, fAllowLookup))
  532. *this = ip;
  533. }
  534. CService::CService(const char *pszIp, int portIn, bool fAllowLookup)
  535. {
  536. std::vector<CNetAddr> ip;
  537. if (LookupHost(pszIp, ip, 1, fAllowLookup))
  538. *this = CService(ip[0], portIn);
  539. }
  540. CService::CService(const std::string &strIpPort, bool fAllowLookup)
  541. {
  542. Init();
  543. CService ip;
  544. if (Lookup(strIpPort.c_str(), ip, 0, fAllowLookup))
  545. *this = ip;
  546. }
  547. CService::CService(const std::string &strIp, int portIn, bool fAllowLookup)
  548. {
  549. std::vector<CNetAddr> ip;
  550. if (LookupHost(strIp.c_str(), ip, 1, fAllowLookup))
  551. *this = CService(ip[0], portIn);
  552. }
  553. unsigned short CService::GetPort() const
  554. {
  555. return port;
  556. }
  557. bool operator==(const CService& a, const CService& b)
  558. {
  559. return (CNetAddr)a == (CNetAddr)b && a.port == b.port;
  560. }
  561. bool operator!=(const CService& a, const CService& b)
  562. {
  563. return (CNetAddr)a != (CNetAddr)b || a.port != b.port;
  564. }
  565. bool operator<(const CService& a, const CService& b)
  566. {
  567. return (CNetAddr)a < (CNetAddr)b || ((CNetAddr)a == (CNetAddr)b && a.port < b.port);
  568. }
  569. bool CService::GetSockAddr(struct sockaddr_in* paddr) const
  570. {
  571. if (!IsIPv4())
  572. return false;
  573. memset(paddr, 0, sizeof(struct sockaddr_in));
  574. if (!GetInAddr(&paddr->sin_addr))
  575. return false;
  576. paddr->sin_family = AF_INET;
  577. paddr->sin_port = htons(port);
  578. return true;
  579. }
  580. #ifdef USE_IPV6
  581. bool CService::GetSockAddr6(struct sockaddr_in6* paddr) const
  582. {
  583. memset(paddr, 0, sizeof(struct sockaddr_in6));
  584. if (!GetIn6Addr(&paddr->sin6_addr))
  585. return false;
  586. paddr->sin6_family = AF_INET6;
  587. paddr->sin6_port = htons(port);
  588. return true;
  589. }
  590. #endif
  591. std::vector<unsigned char> CService::GetKey() const
  592. {
  593. std::vector<unsigned char> vKey;
  594. vKey.resize(18);
  595. memcpy(&vKey[0], ip, 16);
  596. vKey[16] = port / 0x100;
  597. vKey[17] = port & 0x0FF;
  598. return vKey;
  599. }
  600. std::string CService::ToStringPort() const
  601. {
  602. return strprintf(":%i", port);
  603. }
  604. std::string CService::ToStringIPPort() const
  605. {
  606. return ToStringIP() + ToStringPort();
  607. }
  608. std::string CService::ToString() const
  609. {
  610. return ToStringIPPort();
  611. }
  612. void CService::print() const
  613. {
  614. printf("CService(%s)\n", ToString().c_str());
  615. }
  616. void CService::SetPort(unsigned short portIn)
  617. {
  618. port = portIn;
  619. }