From 10d0bfdab42e8e97cee3f2e84503a29a24c20139 Mon Sep 17 00:00:00 2001 From: CurlyMoo Date: Tue, 27 Jan 2015 22:45:34 +0100 Subject: [PATCH 1/3] Replace obsolete gethostbyname by getaddrinfo This also protect against the latest glibc bug --- mongoose.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/mongoose.c b/mongoose.c index 0cacdfed..a560ca0a 100644 --- a/mongoose.c +++ b/mongoose.c @@ -593,14 +593,28 @@ int ns_socketpair(sock_t sp[2]) { // TODO(lsm): use non-blocking resolver static int ns_resolve2(const char *host, struct in_addr *ina) { - struct hostent *he; - if ((he = gethostbyname(host)) == NULL) { - DBG(("gethostbyname(%s) failed: %s", host, strerror(errno))); - } else { - memcpy(ina, he->h_addr_list[0], sizeof(*ina)); - return 1; - } - return 0; + int rv = 0; + struct addrinfo hints, *servinfo, *p; + struct sockaddr_in *h = NULL; + char *ip = malloc(17); + memset(ip, '\0', 17); + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + + if((rv = getaddrinfo(host, NULL , NULL, &servinfo)) != 0) { + + return 0; + } + + for(p = servinfo; p != NULL; p = p->ai_next) { + memcpy(&h, &p->ai_addr, sizeof(struct sockaddr_in *)); + memcpy(ina, &h->sin_addr, sizeof(ina)); + } + + freeaddrinfo(servinfo); + return 1; } // Resolve FDQN "host", store IP address in the "ip". From 10ee430bfddef836a4d7da0cac309c3ef84c7e08 Mon Sep 17 00:00:00 2001 From: CurlyMoo Date: Tue, 27 Jan 2015 22:55:13 +0100 Subject: [PATCH 2/3] Add debug line and adapt to styling guide Replaced tabs with spaces. --- mongoose.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/mongoose.c b/mongoose.c index a560ca0a..2530640a 100644 --- a/mongoose.c +++ b/mongoose.c @@ -593,28 +593,28 @@ int ns_socketpair(sock_t sp[2]) { // TODO(lsm): use non-blocking resolver static int ns_resolve2(const char *host, struct in_addr *ina) { - int rv = 0; - struct addrinfo hints, *servinfo, *p; - struct sockaddr_in *h = NULL; - char *ip = malloc(17); - memset(ip, '\0', 17); + int rv = 0; + struct addrinfo hints, *servinfo, *p; + struct sockaddr_in *h = NULL; + char *ip = NS_MALLOC(17); + memset(ip, '\0', 17); + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; - memset(&hints, 0, sizeof hints); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; + if((rv = getaddrinfo(host, NULL , NULL, &servinfo)) != 0) { + DBG(("getaddrinfo(%s) failed: %s", host, strerror(errno))); + return 0; + } - if((rv = getaddrinfo(host, NULL , NULL, &servinfo)) != 0) { - - return 0; - } + for(p = servinfo; p != NULL; p = p->ai_next) { + memcpy(&h, &p->ai_addr, sizeof(struct sockaddr_in *)); + memcpy(ina, &h->sin_addr, sizeof(ina)); + } - for(p = servinfo; p != NULL; p = p->ai_next) { - memcpy(&h, &p->ai_addr, sizeof(struct sockaddr_in *)); - memcpy(ina, &h->sin_addr, sizeof(ina)); - } - - freeaddrinfo(servinfo); - return 1; + freeaddrinfo(servinfo); + return 1; } // Resolve FDQN "host", store IP address in the "ip". From 4238370ae1085edae3892b298998e6019c4db1e6 Mon Sep 17 00:00:00 2001 From: CurlyMoo Date: Thu, 19 Feb 2015 12:09:34 +0100 Subject: [PATCH 3/3] Check for getaddrinfo availability. --- mongoose.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mongoose.c b/mongoose.c index 2530640a..f25de77f 100644 --- a/mongoose.c +++ b/mongoose.c @@ -593,6 +593,7 @@ int ns_socketpair(sock_t sp[2]) { // TODO(lsm): use non-blocking resolver static int ns_resolve2(const char *host, struct in_addr *ina) { +#ifdef NS_ENABLE_GETADDRINFO int rv = 0; struct addrinfo hints, *servinfo, *p; struct sockaddr_in *h = NULL; @@ -615,6 +616,16 @@ static int ns_resolve2(const char *host, struct in_addr *ina) { freeaddrinfo(servinfo); return 1; +#else + struct hostent *he; + if ((he = gethostbyname(host)) == NULL) { + DBG(("gethostbyname(%s) failed: %s", host, strerror(errno))); + } else { + memcpy(ina, he->h_addr_list[0], sizeof(*ina)); + return 1; + } + return 0; +#endif } // Resolve FDQN "host", store IP address in the "ip".