Introduced getIP and initialization wrappers for sockaddr_in and addrinfo Replaced obsolete functions gethostbyname2_r and gethostbyname2 with getaddrinfo and introduced getIP. Created initialization wrappers for sockaddr_in and addrinfo.
Review: https://reviews.apache.org/r/28719 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/0503b676 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/0503b676 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/0503b676 Branch: refs/heads/master Commit: 0503b676293e23d54fdb8e68604b5dd03b4895cf Parents: 50c3a85 Author: Evelina Dumitrescu <[email protected]> Authored: Fri Dec 5 13:27:59 2014 -0800 Committer: Dominic Hamon <[email protected]> Committed: Fri Dec 5 13:28:23 2014 -0800 ---------------------------------------------------------------------- 3rdparty/libprocess/include/process/socket.hpp | 21 +++--------- 3rdparty/libprocess/src/pid.cpp | 37 ++++----------------- 3rdparty/libprocess/src/process.cpp | 9 +++-- 3 files changed, 15 insertions(+), 52 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/0503b676/3rdparty/libprocess/include/process/socket.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/include/process/socket.hpp b/3rdparty/libprocess/include/process/socket.hpp index e237658..9188e3d 100644 --- a/3rdparty/libprocess/include/process/socket.hpp +++ b/3rdparty/libprocess/include/process/socket.hpp @@ -10,6 +10,7 @@ #include <stout/abort.hpp> #include <stout/nothing.hpp> +#include <stout/net.hpp> #include <stout/os.hpp> #include <stout/try.hpp> @@ -42,9 +43,7 @@ inline Try<int> accept(int s, sa_family_t family) { switch (family) { case AF_INET: { - sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - + sockaddr_in addr = net::createSockaddrIn(0, 0); socklen_t addrlen = sizeof(addr); int rc = ::accept(s, (sockaddr*) &addr, &addrlen); @@ -60,11 +59,7 @@ inline Try<int> accept(int s, sa_family_t family) inline Try<int> bind(int s, const Node& node) { - sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = node.ip; - addr.sin_port = htons(node.port); + sockaddr_in addr = net::createSockaddrIn(node.ip, node.port); int rc = ::bind(s, (sockaddr*) &addr, sizeof(addr)); if (rc < 0) @@ -75,11 +70,7 @@ inline Try<int> bind(int s, const Node& node) inline Try<int> connect(int s, const Node& node) { - sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(node.port); - addr.sin_addr.s_addr = node.ip; + sockaddr_in addr = net::createSockaddrIn(node.ip, node.port); int rc = ::connect(s, (sockaddr*) &addr, sizeof(addr)); if (rc < 0) @@ -92,9 +83,7 @@ inline Try<Node> getsockname(int s, sa_family_t family) { switch (family) { case AF_INET: { - sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - + sockaddr_in addr = net::createSockaddrIn(0, 0); socklen_t addrlen = sizeof(addr); if(::getsockname(s, (sockaddr*) &addr, &addrlen) < 0) http://git-wip-us.apache.org/repos/asf/mesos/blob/0503b676/3rdparty/libprocess/src/pid.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/src/pid.cpp b/3rdparty/libprocess/src/pid.cpp index a2c620e..085e0b9 100644 --- a/3rdparty/libprocess/src/pid.cpp +++ b/3rdparty/libprocess/src/pid.cpp @@ -16,6 +16,7 @@ #include <process/pid.hpp> #include <process/process.hpp> +#include <stout/net.hpp> #include <stout/os.hpp> #include "config.hpp" @@ -109,42 +110,16 @@ istream& operator >> (istream& stream, UPID& pid) return stream; } - hostent he, *hep; - char* temp; - size_t length; - int result; - int herrno; - - // Allocate temporary buffer for gethostbyname2_r. - length = 1024; - temp = new char[length]; - - while ((result = gethostbyname2_r( - host.c_str(), AF_INET, &he, temp, length, &hep, &herrno)) == ERANGE) { - // Enlarge the buffer. - delete[] temp; - length *= 2; - temp = new char[length]; - } - - if (result != 0 || hep == NULL) { - VLOG(2) << "Failed to parse host '" << host - << "' because " << hstrerror(herrno); - delete[] temp; - stream.setstate(std::ios_base::badbit); - return stream; - } + //TODO(evelinad): Extend this to support IPv6 + Try<uint32_t> ip = net::getIP(host, AF_INET); - if (hep->h_addr_list[0] == NULL) { - VLOG(2) << "Got no addresses for '" << host << "'"; - delete[] temp; + if (ip.isError()) { + VLOG(2) << ip.error(); stream.setstate(std::ios_base::badbit); return stream; } - node.ip = *((uint32_t*) hep->h_addr_list[0]); - - delete[] temp; + node.ip = ip.get(); str = str.substr(index + 1); http://git-wip-us.apache.org/repos/asf/mesos/blob/0503b676/3rdparty/libprocess/src/process.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/src/process.cpp b/3rdparty/libprocess/src/process.cpp index 611889b..c0d80bf 100644 --- a/3rdparty/libprocess/src/process.cpp +++ b/3rdparty/libprocess/src/process.cpp @@ -871,14 +871,13 @@ void initialize(const string& delegate) } // Lookup IP address of local hostname. - hostent* he; + Try<uint32_t> ip = net::getIP(hostname, AF_INET); - if ((he = gethostbyname2(hostname, AF_INET)) == NULL) { - LOG(FATAL) << "Failed to initialize, gethostbyname2: " - << hstrerror(h_errno); + if (ip.isError()) { + LOG(FATAL) << ip.error(); } - __node__.ip = *((uint32_t *) he->h_addr_list[0]); + __node__.ip = ip.get(); } Try<Nothing> listen = __s__.listen(LISTEN_BACKLOG);
