From: Niels Provos Date: Thu, 18 Jan 2007 06:28:42 +0000 (+0000) Subject: fix http server so it can accept on high ports; X-Git-Tag: release-2.0.1-alpha~664 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d5d04949e3550823c2935562f5e07b423de46871;p=libevent fix http server so it can accept on high ports; better warning messages for getnameinfo; from Philip Lewis svn:r310 --- diff --git a/http.c b/http.c index 818d01a7..fdb5e628 100644 --- a/http.c +++ b/http.c @@ -115,7 +115,7 @@ fake_freeaddrinfo(struct addrinfo *ai) extern int debug; static int make_socket_ai(int should_bind, struct addrinfo *); -static int make_socket(int should_bind, const char *, short); +static int make_socket(int should_bind, const char *, u_short); static void name_from_addr(struct sockaddr *, socklen_t, char **, char **); static int evhttp_associate_new_request_with_connection( struct evhttp_connection *evcon); @@ -2089,14 +2089,17 @@ addr_from_name(char *address) { #ifdef HAVE_GETADDRINFO struct addrinfo ai, *aitop; + int ai_result; memset(&ai, 0, sizeof (ai)); ai.ai_family = AF_INET; ai.ai_socktype = SOCK_RAW; ai.ai_flags = 0; - if (getaddrinfo(address, NULL, &ai, &aitop) != 0) { - event_warn("getaddrinfo"); - return (NULL); + if ((ai_result = getaddrinfo(address, NULL, &ai, &aitop)) != 0) { + if ( ai_result == EAI_SYSTEM ) + event_warn("getaddrinfo"); + else + event_warnx("getaddrinfo: %s", gai_strerror(ai_result)); } return (aitop); @@ -2113,12 +2116,17 @@ name_from_addr(struct sockaddr *sa, socklen_t salen, #ifdef HAVE_GETNAMEINFO static char ntop[NI_MAXHOST]; static char strport[NI_MAXSERV]; + int ni_result; - if (getnameinfo(sa, salen, + if ((ni_result = getnameinfo(sa, salen, ntop, sizeof(ntop), strport, sizeof(strport), - NI_NUMERICHOST|NI_NUMERICSERV) != 0) - event_err(1, "getnameinfo failed"); - + NI_NUMERICHOST|NI_NUMERICSERV)) != 0) { + if (ni_result == EAI_SYSTEM) + event_err(1, "getnameinfo failed"); + else + event_errx(1, "getnameinfo failed: %s", gai_strerror(ni_result)); + } + *phost = ntop; *pport = strport; #else @@ -2193,20 +2201,24 @@ make_socket_ai(int should_bind, struct addrinfo *ai) } static int -make_socket(int should_bind, const char *address, short port) +make_socket(int should_bind, const char *address, u_short port) { int fd; struct addrinfo ai, *aitop; #ifdef HAVE_GETADDRINFO char strport[NI_MAXSERV]; + int ai_result; + memset(&ai, 0, sizeof (ai)); ai.ai_family = AF_INET; ai.ai_socktype = SOCK_STREAM; ai.ai_flags = should_bind ? AI_PASSIVE : 0; snprintf(strport, sizeof (strport), "%d", port); - if (getaddrinfo(address, strport, &ai, &aitop) != 0) { - event_warn("getaddrinfo"); - return (-1); + if ((ai_result = getaddrinfo(address, strport, &ai, &aitop)) != 0) { + if ( ai_result == EAI_SYSTEM ) + event_warn("getaddrinfo"); + else + event_warnx("getaddrinfo: %s", gai_strerror(ai_result)); } #else if (fake_getaddrinfo(address, &ai) < 0) { diff --git a/test/regress_http.c b/test/regress_http.c index 42f6b7e5..3fdaacb4 100644 --- a/test/regress_http.c +++ b/test/regress_http.c @@ -594,6 +594,28 @@ http_close_detection() fprintf(stdout, "OK\n"); } +void +http_highport_test(void) +{ + int i = -1; + struct evhttp *myhttp = NULL; + + fprintf(stdout, "Testing HTTP Server with high port: "); + + /* Try a few different ports */ + for (i = 0; i < 50; ++i) { + myhttp = evhttp_start("127.0.0.1", 65535 - i); + if (myhttp != NULL) { + fprintf(stdout, "OK\n"); + evhttp_free(myhttp); + return; + } + } + + fprintf(stdout, "FAILED\n"); + exit(1); +} + void http_suite(void) { @@ -603,4 +625,5 @@ http_suite(void) http_close_detection(); http_post_test(); http_failure_test(); + http_highport_test(); }