[ISSUE #42] Fixed this issue that cann't set namesrv with hostname (#98)
* Fixed this issue that cann't set namesrv with hostname
* Use evutil_getaddrinfo resolve domain name
* Extract getInetAddr to deal domain name and IP
diff --git a/src/transport/TcpTransport.cpp b/src/transport/TcpTransport.cpp
index 2c148e7..bbebc37 100644
--- a/src/transport/TcpTransport.cpp
+++ b/src/transport/TcpTransport.cpp
@@ -64,7 +64,8 @@
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = inet_addr(hostName.c_str());
+ sin.sin_addr.s_addr = getInetAddr(hostName);
+
sin.sin_port = htons(portNumber);
m_eventBase = event_base_new();
@@ -129,6 +130,48 @@
}
}
+u_long TcpTransport::getInetAddr(string &hostname)
+{
+ u_long addr = inet_addr(hostname.c_str());
+
+ if (INADDR_NONE == addr) {
+ constexpr size_t length = 128;
+ struct evutil_addrinfo hints;
+ struct evutil_addrinfo *answer = NULL;
+ /* Build the hints to tell getaddrinfo how to act. */
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC; /* v4 or v6 is fine. */
+ //Look up the hostname.
+ int err = evutil_getaddrinfo(hostname.c_str(), NULL, &hints, &answer);
+ if (err != 0) {
+ string info = "Failed to resolve host name(" + hostname + "): " + evutil_gai_strerror(err);
+ THROW_MQEXCEPTION(MQClientException, info, -1);
+ }
+
+ struct evutil_addrinfo *addressInfo;
+ for (addressInfo = answer; addressInfo; addressInfo = addressInfo->ai_next) {
+ char buf[length];
+ const char *address = NULL;
+ if (addressInfo->ai_family == AF_INET) {
+ struct sockaddr_in *sin = (struct sockaddr_in*)addressInfo->ai_addr;
+ address = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, length);
+ }
+ else if (addressInfo->ai_family == AF_INET6) {
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)addressInfo->ai_addr;
+ address = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, length);
+ }
+ if (address) {
+ addr = inet_addr(address);
+ if (addr != INADDR_NONE) {
+ break;
+ }
+ }
+ }
+ }
+
+ return addr;
+}
+
void TcpTransport::disconnect(const string &addr) {
boost::lock_guard<boost::mutex> lock(m_socketLock);
if (getTcpConnectStatus() != e_connectInit) {
diff --git a/src/transport/TcpTransport.h b/src/transport/TcpTransport.h
index 544e14a..fa4da85 100755
--- a/src/transport/TcpTransport.h
+++ b/src/transport/TcpTransport.h
@@ -68,6 +68,7 @@
void freeBufferEvent();
void exitBaseDispatch();
void setTcpConnectEvent(tcpConnectStatus connectStatus);
+ u_long getInetAddr(std::string &hostname);
private:
uint64_t m_startTime;