]> granicus.if.org Git - libevent/commitdiff
fix http server so it can accept on high ports;
authorNiels Provos <provos@gmail.com>
Thu, 18 Jan 2007 06:28:42 +0000 (06:28 +0000)
committerNiels Provos <provos@gmail.com>
Thu, 18 Jan 2007 06:28:42 +0000 (06:28 +0000)
better warning messages for getnameinfo;
from Philip Lewis

svn:r310

http.c
test/regress_http.c

diff --git a/http.c b/http.c
index 818d01a71bb7603721c7b12fce335d08387af184..fdb5e628c0786eee151dc69d76ca5ad32e2c3057 100644 (file)
--- 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) {
index 42f6b7e52d2f297710311d3bef81fc5d98822868..3fdaacb4350522972e03e017a42975bab6d0adab 100644 (file)
@@ -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();
 }