Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/transport/TcpTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebase two commits to only one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format this file with clang-format.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to rebase commits since we use suqash and merge button to merge PRs.

sin.sin_port = htons(portNumber);

m_eventBase = event_base_new();
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/transport/TcpTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down