From 572ddd9ae2687ea0c6c0058c18e841aa1099cf40 Mon Sep 17 00:00:00 2001 From: Marc Parisi Date: Wed, 8 Mar 2017 20:28:48 -0500 Subject: [PATCH] MINIFI-235: Resolve hostname ident --- libminifi/include/io/ClientSocket.h | 2 +- libminifi/src/io/ClientSocket.cpp | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/libminifi/include/io/ClientSocket.h b/libminifi/include/io/ClientSocket.h index 7d3dbe2f8b..3f8aae198e 100644 --- a/libminifi/include/io/ClientSocket.h +++ b/libminifi/include/io/ClientSocket.h @@ -207,7 +207,7 @@ class Socket: public BaseStream { * @param p addrinfo structure. * @returns fd. */ - virtual int8_t createConnection(const addrinfo *p); + virtual int8_t createConnection(const addrinfo *p,in_addr_t &addr); /** * Sets socket options depending on the instance. diff --git a/libminifi/src/io/ClientSocket.cpp b/libminifi/src/io/ClientSocket.cpp index ab391a441a..ebc2e444d4 100644 --- a/libminifi/src/io/ClientSocket.cpp +++ b/libminifi/src/io/ClientSocket.cpp @@ -78,7 +78,7 @@ void Socket::closeStream() } } -int8_t Socket::createConnection(const addrinfo *p) { +int8_t Socket::createConnection(const addrinfo *p,in_addr_t &addr) { if ((socket_file_descriptor_ = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { logger_->log_error("error while connecting to server socket"); @@ -114,7 +114,7 @@ int8_t Socket::createConnection(const addrinfo *p) { } else { - sa_loc->sin_addr.s_addr = inet_addr(requested_hostname_.c_str()); + sa_loc->sin_addr.s_addr = addr; } if (connect(socket_file_descriptor_, p->ai_addr, p->ai_addrlen) == -1) { close(socket_file_descriptor_); @@ -163,6 +163,25 @@ short Socket::initialize() { socket_file_descriptor_ = -1; + in_addr_t addr; + struct hostent *h; + #ifdef __MACH__ + h = gethostbyname(requested_hostname_.c_str()); + #else + const char *host; + uint16_t port; + + host = requested_hostname_.c_str(); + port = port_; + char buf[1024]; + struct hostent he; + int hh_errno; + gethostbyname_r(host, &he, buf, sizeof(buf), &h, &hh_errno); + #endif + + memcpy((char *) &addr, h->h_addr_list[0], h->h_length); + + auto p = addr_info_; for (; p != NULL; p = p->ai_next) { if (IsNullOrEmpty(canonical_hostname_)) { @@ -170,8 +189,9 @@ short Socket::initialize() { canonical_hostname_ = p->ai_canonname; } + //we've successfully connected - if (port_ > 0 && createConnection(p) >= 0) + if (port_ > 0 && createConnection(p,addr) >= 0) { return 0; break;