]> granicus.if.org Git - libevent/commitdiff
mingw fixes from Nick
authorNiels Provos <provos@gmail.com>
Wed, 22 Nov 2006 01:21:10 +0000 (01:21 +0000)
committerNiels Provos <provos@gmail.com>
Wed, 22 Nov 2006 01:21:10 +0000 (01:21 +0000)
svn:r271

WIN32-Code/misc.c
WIN32-Code/win32.c
configure.in
evdns.c
event.h
event_tagging.c
evhttp.h
evrpc.c
http.c

index 6f63ddfe63481e2f654c5b9f323282dd673afdc7..bdebc3a4f4948cb4bcfd3ec179cf478ddbf0362f 100644 (file)
@@ -88,4 +88,4 @@ socketpair(int d, int type, int protocol, int *sv)
        sv[1] = (int)fd;
 
        return (0);
-}
\ No newline at end of file
+}
index 701d1c3cd3fa1a8f055795bb803963b4cbb112aa..867d1f22fd4382b59b5d3cdb248649f1d0c7c507 100644 (file)
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+#ifdef _MSC_VER
 #include "config.h"
-
-#include <winsock2.h>
+#else
+/* Avoid the windows/msvc thing. */
+#include "../config.h"
+#endif
 
 #include <windows.h>
+#include <winsock2.h>
 #include <sys/types.h>
 #include <sys/queue.h>
 #include <sys/tree.h>
index fa43a9cc0eeaaf5be23352da0fa53eff1bde9f81..ddcde2fb2410a0baf4adfa501f6b274d04a9a5de 100644 (file)
@@ -132,7 +132,7 @@ AC_C_INLINE
 AC_HEADER_TIME
 
 dnl Checks for library functions.
-AC_CHECK_FUNCS(gettimeofday vasprintf fcntl clock_gettime strtok_r)
+AC_CHECK_FUNCS(gettimeofday vasprintf fcntl clock_gettime strtok_r strsep getaddrinfo getnameinfo)
 AC_REPLACE_FUNCS(strlcpy)
 
 if test "x$ac_cv_func_clock_gettime" = "xyes"; then
diff --git a/evdns.c b/evdns.c
index 6c16bbd27cc6271cb03651092d7e0a8e64a253a7..1ddad821ea52dac9044f3ef952cdd4b6b9419178 100644 (file)
--- a/evdns.c
+++ b/evdns.c
 #include "config.h"
 #endif
 
+#ifdef WIN32
+#include "misc.h"
+#endif
+
 //#define NDEBUG
 
 #ifndef DNS_USE_CPU_CLOCK_FOR_ID
@@ -235,7 +239,7 @@ static u16 transaction_id_pick(void);
 static struct request *request_new(int type, const char *name, int flags, evdns_callback_type callback, void *ptr);
 static void request_submit(struct request *req);
 
-#ifdef MS_WINDOWS
+#if defined(MS_WINDOWS) || defined(WIN32)
 static int
 last_error(int sock)
 {
@@ -1275,7 +1279,7 @@ evdns_nameserver_add(unsigned long int address) {
 
        ns->socket = socket(PF_INET, SOCK_DGRAM, 0);
        if (ns->socket < 0) { err = 1; goto out1; }
-#ifdef MS_WINDOWS
+#if defined(MS_WINDOWS) || defined(WIN32)
         {
                u_long nonblocking = 1;
                ioctlsocket(ns->socket, FIONBIO, &nonblocking);
@@ -1330,7 +1334,8 @@ out1:
 int
 evdns_nameserver_ip_add(const char *ip_as_string) {
        struct in_addr ina;
-       if (!inet_aton(ip_as_string, &ina)) return 4;
+       if (!inet_aton(ip_as_string, &ina))
+          return 4;
        return evdns_nameserver_add(ina.s_addr);
 }
 
@@ -1847,7 +1852,7 @@ out1:
        return err;
 }
 
-#ifdef MS_WINDOWS
+#if defined(MS_WINDOWS) || defined(WIN32)
 // Add multiple nameservers from a space-or-comma-separated list.
 static int
 evdns_nameserver_ip_add_line(const char *ips) {
@@ -2043,8 +2048,8 @@ int
 evdns_init()
 {
        int res = 0;
-#ifdef MS_WINDOWS
-       evdns_config_windows_nameservers(void);
+#if defined(MS_WINDOWS) || defined(WIN32)
+       evdns_config_windows_nameservers();
 #else
        res = evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
 #endif
diff --git a/event.h b/event.h
index bd370f84b34419b87b3ea11730ee6c8947ff7ea0..78f44bbc9bb1661b6557ebb1b831deccff73cbd1 100644 (file)
--- a/event.h
+++ b/event.h
@@ -38,6 +38,7 @@ extern "C" {
 #include <windows.h>
 #undef WIN32_LEAN_AND_MEAN
 typedef unsigned char u_char;
+typedef unsigned short u_short;
 #endif
 
 #define EVLIST_TIMEOUT 0x01
@@ -191,7 +192,7 @@ void event_active(struct event *, int, short);
 int event_pending(struct event *, short, struct timeval *);
 
 #ifdef WIN32
-#define event_initialized(ev)          ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != INVALID_HANDLE_VALUE)
+#define event_initialized(ev)          ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE)
 #else
 #define event_initialized(ev)          ((ev)->ev_flags & EVLIST_INIT)
 #endif
index ac4e347500f1ffc4caad8c5bbcadc31bc7a02615..b77494d7f45693e6e17c41dcddeb97a0a1f8c3b5 100644 (file)
 #include "config.h"
 #endif
 
+#ifdef WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#undef WIN32_LEAN_AND_MEAN
+#else
 #include <sys/ioctl.h>
+#endif
+
 #include <sys/tree.h>
 #include <sys/queue.h>
 #ifdef HAVE_SYS_TIME_H
@@ -43,7 +50,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#ifndef WIN32
 #include <syslog.h>
+#endif
 #include <unistd.h>
 
 #include "event.h"
index 9f7dafe755bc45534ca03b7f464a6b396520154d..f31a013c17bbcf53ec84106c9619243cb5788306 100644 (file)
--- a/evhttp.h
+++ b/evhttp.h
@@ -34,8 +34,8 @@ extern "C" {
 #ifdef WIN32
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
+#include <winsock2.h>
 #undef WIN32_LEAN_AND_MEAN
-typedef unsigned char u_char;
 #endif
 
 /*
@@ -48,7 +48,7 @@ typedef unsigned char u_char;
  * creating a very simple HTTP server.
  */
 
-/* Response codes */   
+/* Response codes */
 #define HTTP_OK                        200
 #define HTTP_MOVEPERM          301
 #define HTTP_MOVETEMP          302
diff --git a/evrpc.c b/evrpc.c
index e422ccb67f652ef93fe04f51cf9706639f33eb86..bf66c181d3d5f7388073dae840a1a4b34614b5eb 100644 (file)
--- a/evrpc.c
+++ b/evrpc.c
 #ifdef WIN32
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
+#include <winsock2.h>
 #undef WIN32_LEAN_AND_MEAN
 #include "misc.h"
 #endif
+
 #include <sys/types.h>
 #include <sys/tree.h>
+#ifndef WIN32
 #include <sys/socket.h>
+#endif
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
-#else 
+#else
 #include <sys/_time.h>
 #endif
 #include <sys/queue.h>
diff --git a/http.c b/http.c
index 32895fc57894d0d535524f7ef772352304d71e2d..8213d7e24d1d3c396ac2a21c3a0ecdc192afbd01 100644 (file)
--- a/http.c
+++ b/http.c
 #ifdef HAVE_SYS_IOCCOM_H
 #include <sys/ioccom.h>
 #endif
+
+#ifndef WIN32
 #include <sys/resource.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
-#include <sys/tree.h>
 #include <sys/wait.h>
+#endif
+
+#include <sys/tree.h>
 #include <sys/queue.h>
 
+#ifndef WIN32
 #include <netinet/in.h>
 #include <netdb.h>
+#endif
 
 #include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#ifndef WIN32
 #include <syslog.h>
+#endif
 #include <signal.h>
 #ifdef HAVE_TIME_H
 #include <time.h>
 #include "log.h"
 #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);
+}
+#endif
+
 extern int debug;
 
-static int make_socket_ai(int (*f)(int, const struct sockaddr *, socklen_t),
-    struct addrinfo *);
-static int make_socket(int (*)(int, const struct sockaddr *, socklen_t),
-    const char *, short);
+static int make_socket_ai(int should_bind, struct addrinfo *);
+static int make_socket(int should_bind, const char *, short);
 static void name_from_addr(struct sockaddr *, socklen_t, char **, char **);
 static int evhttp_associate_new_request_with_connection(
        struct evhttp_connection *evcon);
 
 void evhttp_write(int, short, void *);
 
+#ifndef HAVE_STRSEP
+static char *
+strsep(char **s, const char *del)
+{
+       char *d, *tok;
+       if (!s || !*s)
+               return NULL;
+       tok = *s;
+       d = strstr(tok, del);
+       if (d)
+               *s = d + strlen(del);
+       else
+               *s = NULL;
+       return tok;
+}
+#endif
+
 static const char *
 html_replace(char ch)
 {
@@ -206,7 +262,7 @@ evhttp_make_header_request(struct evhttp_connection *evcon,
            evhttp_find_header(req->output_headers, "Content-Length") == NULL){
                char size[12];
                snprintf(size, sizeof(size), "%ld",
-                   EVBUFFER_LENGTH(req->output_buffer));
+                        (long)EVBUFFER_LENGTH(req->output_buffer));
                evhttp_add_header(req->output_headers, "Content-Length", size);
        }
 }
@@ -244,7 +300,7 @@ evhttp_make_header_response(struct evhttp_connection *evcon,
        if (evhttp_find_header(req->output_headers, "Content-Length") == NULL){
                static char len[12];
                snprintf(len, sizeof(len), "%ld",
-                   EVBUFFER_LENGTH(req->output_buffer));
+                        (long)EVBUFFER_LENGTH(req->output_buffer));
                evhttp_add_header(req->output_headers, "Content-Length", len);
        }
 
@@ -593,7 +649,7 @@ evhttp_connectioncb(int fd, short what, void *arg)
        }
 
        /* Check if the connection completed */
-       if (getsockopt(evcon->fd, SOL_SOCKET, SO_ERROR, &error,
+       if (getsockopt(evcon->fd, SOL_SOCKET, SO_ERROR, (void*)&error,
                       &errsz) == -1) {
                event_warn("%s: getsockopt for \"%s:%d\" on %d",
                    __func__, evcon->address, evcon->port, evcon->fd);
@@ -1066,7 +1122,7 @@ evhttp_connection_connect(struct evhttp_connection *evcon)
        
        /* Do async connection to HTTP server */
        if ((evcon->fd = make_socket(
-                    connect, evcon->address, evcon->port)) == -1) {
+                    0, evcon->address, evcon->port)) == -1) {
                event_warn("%s: failed to connect to \"%s:%d\"",
                    __func__, evcon->address, evcon->port);
                return (-1);
@@ -1378,7 +1434,7 @@ bind_socket(struct evhttp *http, const char *address, u_short port)
        struct event *ev = &http->bind_ev;
        int fd;
 
-       if ((fd = make_socket(bind, address, port)) == -1)
+       if ((fd = make_socket(1, address, port)) == -1)
                return (-1);
 
        if (listen(fd, 10) == -1) {
@@ -1637,8 +1693,9 @@ evhttp_get_request(struct evhttp *http, int fd,
 static struct addrinfo *
 addr_from_name(char *address)
 {
+#ifdef HAVE_GETADDRINFO
         struct addrinfo ai, *aitop;
-       
+
         memset(&ai, 0, sizeof (ai));
         ai.ai_family = AF_INET;
         ai.ai_socktype = SOCK_RAW;
@@ -1649,12 +1706,17 @@ addr_from_name(char *address)
         }
 
        return (aitop);
+#else
+       assert(0);
+       return NULL; // XXXXX Use gethostbyname, if this function is ever used.
+#endif
 }
 
 static void
 name_from_addr(struct sockaddr *sa, socklen_t salen,
     char **phost, char **pport)
 {
+#ifdef HAVE_GETNAMEINFO
        static char ntop[NI_MAXHOST];
        static char strport[NI_MAXSERV];
 
@@ -1665,16 +1727,18 @@ name_from_addr(struct sockaddr *sa, socklen_t salen,
 
        *phost = ntop;
        *pport = strport;
+#else
+       // XXXX
+#endif
 }
 
 /* Either connect or bind */
 
 static int
-make_socket_ai(int (*f)(int, const struct sockaddr *, socklen_t),
-    struct addrinfo *ai)
+make_socket_ai(int should_bind, struct addrinfo *ai)
 {
         struct linger linger;
-        int fd, on = 1;
+        int fd, on = 1, r;
        int serrno;
 
         /* Create listen socket */
@@ -1684,6 +1748,12 @@ make_socket_ai(int (*f)(int, const struct sockaddr *, socklen_t),
                 return (-1);
         }
 
+#ifdef WIN32
+       {
+               unsigned long nonblocking = 1;
+               ioctlsocket(fd, FIONBIO, (unsigned long*) &nonblocking);
+       }
+#else
         if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
                 event_warn("fcntl(O_NONBLOCK)");
                 goto out;
@@ -1693,18 +1763,31 @@ make_socket_ai(int (*f)(int, const struct sockaddr *, socklen_t),
                 event_warn("fcntl(F_SETFD)");
                 goto out;
         }
+#endif
 
         setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
-        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof(on));
+        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
         linger.l_onoff = 1;
         linger.l_linger = 5;
-        setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
+        setsockopt(fd, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
 
-        if ((f)(fd, ai->ai_addr, ai->ai_addrlen) == -1) {
+       if (should_bind)
+               r = bind(fd, ai->ai_addr, ai->ai_addrlen);
+       else
+               r = connect(fd, ai->ai_addr, ai->ai_addrlen);
+       if (r == -1) {
+#ifdef WIN32
+               int tmp_error = WSAGetLastError();
+               if (tmp_error != WSAEWOULDBLOCK && tmp_error != WSAEINVAL &&
+                   tmp_error != WSAEINPROGRESS) {
+                       goto out;
+               }
+#else
                if (errno != EINPROGRESS) {
                        goto out;
                }
-        }
+#endif
+       }
 
        return (fd);
 
@@ -1716,26 +1799,36 @@ make_socket_ai(int (*f)(int, const struct sockaddr *, socklen_t),
 }
 
 static int
-make_socket(int (*f)(int, const struct sockaddr *, socklen_t),
-    const char *address, short port)
+make_socket(int should_bind, const char *address, short port)
 {
+       int fd;
         struct addrinfo ai, *aitop;
+#ifdef HAVE_GETADDRINFO
         char strport[NI_MAXSERV];
-       int fd;
-       
         memset(&ai, 0, sizeof (ai));
         ai.ai_family = AF_INET;
         ai.ai_socktype = SOCK_STREAM;
-        ai.ai_flags = f != connect ? AI_PASSIVE : 0;
+        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);
         }
-        
-       fd = make_socket_ai(f, aitop);
+#else
+       if (fake_getaddrinfo(address, &ai) < 0) {
+               event_warn("fake_getaddrinfo");
+               return (-1);
+       }
+       aitop = &ai;
+#endif
 
+       fd = make_socket_ai(should_bind, aitop);
+
+#ifdef HAVE_GETADDRINFO
        freeaddrinfo(aitop);
+#else
+       fake_freeaddrinfo(aitop);
+#endif
 
        return (fd);
 }