From 2fc1ad8622ca85859eb817fa34cad2e042d9a19f Mon Sep 17 00:00:00 2001 From: Nicolas Pope <nwpope@utu.fi> Date: Wed, 5 Jun 2019 18:58:00 +0300 Subject: [PATCH] switch peer to getaddrinfo --- components/net/cpp/src/peer.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/components/net/cpp/src/peer.cpp b/components/net/cpp/src/peer.cpp index 370f413df..55c5990d8 100644 --- a/components/net/cpp/src/peer.cpp +++ b/components/net/cpp/src/peer.cpp @@ -59,7 +59,7 @@ static ctpl::thread_pool pool(5); // TODO(nick) Move to tcp_internal.cpp static SOCKET tcpConnect(URI &uri) { int rc; - sockaddr_in destAddr; + //sockaddr_in destAddr; #ifdef WIN32 WSAData wsaData; @@ -77,13 +77,19 @@ static SOCKET tcpConnect(URI &uri) { return INVALID_SOCKET; } - #ifdef WIN32 + /*#ifdef WIN32 HOSTENT *host = gethostbyname(uri.getHost().c_str()); #else hostent *host = gethostbyname(uri.getHost().c_str()); - #endif + #endif*/ + + addrinfo hints = {}, *addrs; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + rc = getaddrinfo(uri.getHost().c_str(), std::to_string(uri.getPort()).c_str(), &hints, &addrs); - if (host == NULL) { + if (rc != 0 || addrs == nullptr) { #ifndef WIN32 close(csocket); #else @@ -94,16 +100,18 @@ static SOCKET tcpConnect(URI &uri) { return INVALID_SOCKET; } - destAddr.sin_family = AF_INET; - destAddr.sin_addr.s_addr = ((in_addr *)(host->h_addr))->s_addr; - destAddr.sin_port = htons(uri.getPort()); + //destAddr.sin_family = AF_INET; + //destAddr.sin_addr.s_addr = ((in_addr *)(host->h_addr))->s_addr; + //destAddr.sin_port = htons(uri.getPort()); // Make nonblocking /*long arg = fcntl(csocket, F_GETFL, NULL)); arg |= O_NONBLOCK; fcntl(csocket, F_SETFL, arg) < 0)*/ - rc = ::connect(csocket, (struct sockaddr*)&destAddr, sizeof(destAddr)); + // TODO(Nick) - Check all returned addresses. + auto addr = addrs; + rc = ::connect(csocket, addr->ai_addr, addr->ai_addrlen); if (rc < 0) { if (errno == EINPROGRESS) { -- GitLab