Skip to content

Commit da49964

Browse files
wangjunengShannonDing
authored andcommitted
[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
1 parent dcba721 commit da49964

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/transport/TcpTransport.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ tcpConnectStatus TcpTransport::connect(const string &strServerURL,
6464
struct sockaddr_in sin;
6565
memset(&sin, 0, sizeof(sin));
6666
sin.sin_family = AF_INET;
67-
sin.sin_addr.s_addr = inet_addr(hostName.c_str());
67+
sin.sin_addr.s_addr = getInetAddr(hostName);
68+
6869
sin.sin_port = htons(portNumber);
6970

7071
m_eventBase = event_base_new();
@@ -129,6 +130,48 @@ void TcpTransport::setTcpConnectEvent(tcpConnectStatus connectStatus) {
129130
}
130131
}
131132

133+
u_long TcpTransport::getInetAddr(string &hostname)
134+
{
135+
u_long addr = inet_addr(hostname.c_str());
136+
137+
if (INADDR_NONE == addr) {
138+
constexpr size_t length = 128;
139+
struct evutil_addrinfo hints;
140+
struct evutil_addrinfo *answer = NULL;
141+
/* Build the hints to tell getaddrinfo how to act. */
142+
memset(&hints, 0, sizeof(hints));
143+
hints.ai_family = AF_UNSPEC; /* v4 or v6 is fine. */
144+
//Look up the hostname.
145+
int err = evutil_getaddrinfo(hostname.c_str(), NULL, &hints, &answer);
146+
if (err != 0) {
147+
string info = "Failed to resolve host name(" + hostname + "): " + evutil_gai_strerror(err);
148+
THROW_MQEXCEPTION(MQClientException, info, -1);
149+
}
150+
151+
struct evutil_addrinfo *addressInfo;
152+
for (addressInfo = answer; addressInfo; addressInfo = addressInfo->ai_next) {
153+
char buf[length];
154+
const char *address = NULL;
155+
if (addressInfo->ai_family == AF_INET) {
156+
struct sockaddr_in *sin = (struct sockaddr_in*)addressInfo->ai_addr;
157+
address = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, length);
158+
}
159+
else if (addressInfo->ai_family == AF_INET6) {
160+
struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)addressInfo->ai_addr;
161+
address = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, length);
162+
}
163+
if (address) {
164+
addr = inet_addr(address);
165+
if (addr != INADDR_NONE) {
166+
break;
167+
}
168+
}
169+
}
170+
}
171+
172+
return addr;
173+
}
174+
132175
void TcpTransport::disconnect(const string &addr) {
133176
boost::lock_guard<boost::mutex> lock(m_socketLock);
134177
if (getTcpConnectStatus() != e_connectInit) {

src/transport/TcpTransport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class TcpTransport {
6868
void freeBufferEvent();
6969
void exitBaseDispatch();
7070
void setTcpConnectEvent(tcpConnectStatus connectStatus);
71+
u_long getInetAddr(std::string &hostname);
7172

7273
private:
7374
uint64_t m_startTime;

0 commit comments

Comments
 (0)