Skip to content

Commit

Permalink
Merge pull request #28 from randomjohnnyh/master
Browse files Browse the repository at this point in the history
Use getaddrinfo instead of gethostbyname for thread safety
  • Loading branch information
tqchen committed Jul 27, 2016
2 parents 8f61535 + 9d235c3 commit 2dd7476
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ struct SockAddr {
* \param port the port of address
*/
inline void Set(const char *host, int port) {
hostent *hp = gethostbyname(host);
Check(hp != NULL, "cannot obtain address of %s", host);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_protocol = SOCK_STREAM;
addrinfo *res = NULL;
int sig = getaddrinfo(host, NULL, &hints, &res);
Check(sig == 0 && res != NULL, "cannot obtain address of %s", host);
Check(res->ai_family == AF_INET, "Does not support IPv6");
memcpy(&addr, res->ai_addr, res->ai_addrlen);
addr.sin_port = htons(port);
memcpy(&addr.sin_addr, hp->h_addr_list[0], hp->h_length);
freeaddrinfo(res);
}
/*! \brief return port of the address*/
inline int port(void) const {
Expand Down

0 comments on commit 2dd7476

Please sign in to comment.