rocketmq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ding...@apache.org
Subject [rocketmq-client-cpp] branch master updated: [ISSUE #42] Fixed this issue that cann't set namesrv with hostname (#98)
Date Mon, 18 Mar 2019 06:11:55 GMT
This is an automated email from the ASF dual-hosted git repository.

dinglei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-cpp.git


The following commit(s) were added to refs/heads/master by this push:
     new da49964  [ISSUE #42] Fixed this issue that cann't set namesrv with hostname (#98)
da49964 is described below

commit da49964b2c632a10ec74a0e9a66eb17378bfa1c0
Author: wangjuneng <wangjuneng@gmail.com>
AuthorDate: Mon Mar 18 14:11:51 2019 +0800

    [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
---
 src/transport/TcpTransport.cpp | 45 +++++++++++++++++++++++++++++++++++++++++-
 src/transport/TcpTransport.h   |  1 +
 2 files changed, 45 insertions(+), 1 deletion(-)

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 @@ tcpConnectStatus TcpTransport::connect(const string &strServerURL,
   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 @@ void TcpTransport::setTcpConnectEvent(tcpConnectStatus connectStatus)
{
   }
 }
 
+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 @@ class TcpTransport {
   void freeBufferEvent();
   void exitBaseDispatch();
   void setTcpConnectEvent(tcpConnectStatus connectStatus);
+  u_long getInetAddr(std::string &hostname);
 
  private:
   uint64_t m_startTime;


Mime
View raw message