[Libevent-users] Patches for Cygwin / getaddrinfo

colm-libevent at blackstar.co.uk colm-libevent at blackstar.co.uk
Sat Jun 16 09:34:16 EDT 2007


Hi,

Attahced is a patch against libevent-1.3b for Cygwin - or cases where
getaddrinfo() and/or getnameinfo() are not present.

The patch addresses 3 issue :

1] compile error in test/regress_http.c when getaddrinfo() is not
present.  Changed this to use a similar workaround to the one already in
http.c.  Since this workaround is now used twice I dumped it in a
seperate header called fake_getaddrinfo.h

2] a runtime problem with fake_getaddrinfo - worked around by adding an
extra port argument to fake_getaddrinfo (not sure if this is the right
solution but it worked for me).

3] a runtime problem in cases where getnameinfo() in which we were
dropping out of name_from_addr() without doing anything and this was
casuing a segfault.

Colm
-------------- next part --------------
diff -Naur --exclude-from exclude libevent-1.3b_base/fake_getaddrinfo.h libevent-1.3b/fake_getaddrinfo.h
--- libevent-1.3b_base/fake_getaddrinfo.h	1970-01-01 00:00:00.000000000 +0000
+++ libevent-1.3b/fake_getaddrinfo.h	2007-06-16 03:32:38.515625000 +0100
@@ -0,0 +1,42 @@
+
+struct addrinfo {
+	int ai_family;
+	int ai_socktype;
+	int ai_protocol;
+	size_t ai_addrlen;
+	struct sockaddr *ai_addr;
+	struct addrinfo *ai_next;
+};
+static int
+fake_getaddrinfo(const char *hostname, struct addrinfo *ai, in_port_t port)
+{
+	struct hostent *he;
+        struct in_addr **ap;
+        struct sockaddr_in *sa_in;
+
+	he = gethostbyname(hostname);
+	if (!he)
+		return (-1);
+	ai->ai_family = he->h_addrtype;
+	ai->ai_socktype = SOCK_STREAM;
+	ai->ai_protocol = 0;
+	//ai->ai_addrlen = he->h_length;
+	ai->ai_addrlen = sizeof(struct sockaddr_in);
+	if (NULL == (ai->ai_addr = malloc(ai->ai_addrlen)))
+		return (-1);
+
+        sa_in = (struct sockaddr_in *)ai->ai_addr;
+        memset(sa_in, 0, sizeof(struct sockaddr_in));
+        sa_in->sin_family = PF_INET;
+        sa_in->sin_port = htons(port);
+        ap = (struct in_addr **)he->h_addr_list;
+        memcpy(&sa_in->sin_addr, *ap, sizeof(struct in_addr));
+
+	ai->ai_next = NULL;
+	return (0);
+}
+static void
+fake_freeaddrinfo(struct addrinfo *ai)
+{
+	free(ai->ai_addr);
+}
diff -Naur --exclude-from exclude libevent-1.3b_base/http.c libevent-1.3b/http.c
--- libevent-1.3b_base/http.c	2007-03-04 04:05:04.000000000 +0000
+++ libevent-1.3b/http.c	2007-06-16 03:45:02.437500000 +0100
@@ -80,36 +80,7 @@
 #include "http-internal.h"
 
 #ifndef HAVE_GETADDRINFO
-struct addrinfo {
-	int ai_family;
-	int ai_socktype;
-	int ai_protocol;
-	size_t ai_addrlen;
-	struct sockaddr *ai_addr;
-	struct addrinfo *ai_next;
-};
-static int
-fake_getaddrinfo(const char *hostname, struct addrinfo *ai)
-{
-	struct hostent *he;
-	he = gethostbyname(hostname);
-	if (!he)
-		return (-1);
-	ai->ai_family = he->h_addrtype;
-	ai->ai_socktype = SOCK_STREAM;
-	ai->ai_protocol = 0;
-	ai->ai_addrlen = he->h_length;
-	if (NULL == (ai->ai_addr = malloc(ai->ai_addrlen)))
-		return (-1);
-	memcpy(ai->ai_addr, &he->h_addr_list[0], ai->ai_addrlen);
-	ai->ai_next = NULL;
-	return (0);
-}
-static void
-fake_freeaddrinfo(struct addrinfo *ai)
-{
-	free(ai->ai_addr);
-}
+#include "fake_getaddrinfo.h"
 #endif
 
 #ifndef MIN
@@ -2190,12 +2161,17 @@
 		else
 			event_errx(1, "getnameinfo failed: %s", gai_strerror(ni_result));
 	}
- 
-	*phost = ntop;
-	*pport = strport;
 #else
-	// XXXX
+	char ntop[1025];
+	char strport[32];
+	struct sockaddr_in *sin = (struct sockaddr_in *)sa;
+
+	snprintf(strport, sizeof(strport), "%d", ntohs(sin->sin_port));
+	strcpy(ntop, inet_ntoa(sin->sin_addr));
 #endif
+
+	*phost = ntop;
+	*pport = strport;
 }
 
 /* Either connect or bind */
@@ -2279,7 +2255,8 @@
 		return (-1);
         }
 #else
-	if (fake_getaddrinfo(address, &ai) < 0) {
+        memset(&ai, 0, sizeof (ai));
+	if (fake_getaddrinfo(address, &ai, port) < 0) {
 		event_warn("fake_getaddrinfo");
 		return (-1);
 	}
diff -Naur --exclude-from exclude libevent-1.3b_base/test/regress_http.c libevent-1.3b/test/regress_http.c
--- libevent-1.3b_base/test/regress_http.c	2007-02-16 00:49:03.000000000 +0000
+++ libevent-1.3b/test/regress_http.c	2007-06-16 03:32:33.765625000 +0100
@@ -44,6 +44,7 @@
 #include <sys/socket.h>
 #include <sys/signal.h>
 #include <unistd.h>
+#include <netinet/in.h>
 #endif
 #include <netdb.h>
 #include <fcntl.h>
@@ -57,6 +58,10 @@
 #include "log.h"
 #include "http-internal.h"
 
+#ifndef HAVE_GETADDRINFO
+#include "fake_getaddrinfo.h"
+#endif
+
 extern int pair[];
 extern int test_ok;
 
@@ -97,9 +102,10 @@
 {
 	/* Stupid code for connecting */
 	struct addrinfo ai, *aitop;
-	char strport[NI_MAXSERV];
 	int fd;
-	
+
+#ifdef HAVE_GETADDRINFO
+	char strport[NI_MAXSERV];
 	memset(&ai, 0, sizeof (ai));
 	ai.ai_family = AF_INET;
 	ai.ai_socktype = SOCK_STREAM;
@@ -108,6 +114,14 @@
 		event_warn("getaddrinfo");
 		return (-1);
 	}
+#else
+        memset(&ai, 0, sizeof (ai));
+        if (fake_getaddrinfo(address, &ai, port) < 0) {
+                event_warn("fake_getaddrinfo");
+                return (-1);
+        }
+        aitop = &ai;
+#endif
         
 	fd = socket(AF_INET, SOCK_STREAM, 0);
 	if (fd == -1)
@@ -116,7 +130,11 @@
 	if (connect(fd, aitop->ai_addr, aitop->ai_addrlen) == -1)
 		event_err(1, "connect failed");
 
+#ifdef HAVE_GETADDRINFO
 	freeaddrinfo(aitop);
+#else
+	fake_freeaddrinfo(aitop);
+#endif
 
 	return (fd);
 }


More information about the Libevent-users mailing list