Merge pull request #28 from randomjohnnyh/master
Use getaddrinfo instead of gethostbyname for thread safety
This commit is contained in:
commit
2dd7476ad7
15
src/socket.h
15
src/socket.h
@ -57,12 +57,17 @@ struct SockAddr {
|
|||||||
* \param port the port of address
|
* \param port the port of address
|
||||||
*/
|
*/
|
||||||
inline void Set(const char *host, int port) {
|
inline void Set(const char *host, int port) {
|
||||||
hostent *hp = gethostbyname(host);
|
addrinfo hints;
|
||||||
Check(hp != NULL, "cannot obtain address of %s", host);
|
memset(&hints, 0, sizeof(hints));
|
||||||
memset(&addr, 0, sizeof(addr));
|
hints.ai_family = AF_INET;
|
||||||
addr.sin_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);
|
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*/
|
/*! \brief return port of the address*/
|
||||||
inline int port(void) const {
|
inline int port(void) const {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user