r15218@catbus: nickm | 2007-09-20 14:14:05 -0400
authorNick Mathewson <nickm@torproject.org>
Thu, 20 Sep 2007 18:26:51 +0000 (18:26 +0000)
committerNick Mathewson <nickm@torproject.org>
Thu, 20 Sep 2007 18:26:51 +0000 (18:26 +0000)
 More win32 fixes: Use evutil_make_socket_nonblocking and EVUTIL_CLOSESOCKET consistently throughout the code.

svn:r443

ChangeLog
evdns.c
http.c

index e660b6621c1f8bd8f3307315577281677cd12785..06e54ae53ff590b1977f1ad66dafe00d5cc4aa72 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -18,3 +18,5 @@ Changes in current version:
  o Add a evdns_set_transaction_id_fn() function to override the default
    transaction ID generation code.
  o Add an evutil module (with header evutil.h) to implement our standard cross-platform hacks, on the theory that somebody else would like to use them too.
+ o Fix signals implementation on windows.
+ o Fix http module on windows to close sockets properly.
diff --git a/evdns.c b/evdns.c
index eb73866b9fd8de0ab911461e3d2c696734193f67..560786249fb75ada955b6090e8e861ddc64e5627 100644 (file)
--- a/evdns.c
+++ b/evdns.c
@@ -94,6 +94,7 @@
 #include <stdarg.h>
 
 #include "evdns.h"
+#include "evutil.h"
 #include "log.h"
 #ifdef WIN32
 #include <windows.h>
@@ -359,12 +360,11 @@ inet_aton(const char *c, struct in_addr *addr)
        }
        return 1;
 }
-#define CLOSE_SOCKET(x) closesocket(x)
 #else
 #define last_error(sock) (errno)
 #define error_is_eagain(err) ((err) == EAGAIN)
-#define CLOSE_SOCKET(x) close(x)
 #endif
+#define CLOSE_SOCKET(s) EVUTIL_CLOSESOCKET(s)
 
 #define ISSPACE(c) isspace((int)(unsigned char)(c))
 #define ISDIGIT(c) isdigit((int)(unsigned char)(c))
@@ -2070,14 +2070,7 @@ _evdns_nameserver_add_impl(unsigned long int address, int port) {
 
        ns->socket = socket(PF_INET, SOCK_DGRAM, 0);
        if (ns->socket < 0) { err = 1; goto out1; }
-#ifdef WIN32
-        {
-               u_long nonblocking = 1;
-               ioctlsocket(ns->socket, FIONBIO, &nonblocking);
-       }
-#else
-        fcntl(ns->socket, F_SETFL, O_NONBLOCK);
-#endif
+        evutil_make_socket_nonblocking(ns->socket);
        sin.sin_addr.s_addr = address;
        sin.sin_port = htons(port);
        sin.sin_family = AF_INET;
@@ -3106,7 +3099,7 @@ main(int c, char **v) {
                int sock;
                struct sockaddr_in my_addr;
                sock = socket(PF_INET, SOCK_DGRAM, 0);
-               fcntl(sock, F_SETFL, O_NONBLOCK);
+                evutil_make_socket_nonblocking(sock);
                my_addr.sin_family = AF_INET;
                my_addr.sin_port = htons(10053);
                my_addr.sin_addr.s_addr = INADDR_ANY;
diff --git a/http.c b/http.c
index 65dafbc080723543e30677f24e4368dca8629a1f..e0f8ff5f7acfa487410c41d5c0a24a4b821f796d 100644 (file)
--- a/http.c
+++ b/http.c
@@ -74,6 +74,7 @@
 #include "strlcpy-internal.h"
 #include "event.h"
 #include "evhttp.h"
+#include "evutil.h"
 #include "log.h"
 #include "http-internal.h"
 
@@ -119,24 +120,6 @@ fake_freeaddrinfo(struct addrinfo *ai)
        if ((x)->base != NULL) event_base_set((x)->base, y);    \
 } while (0) 
 
-static int
-event_make_socket_nonblocking(int fd)
-{
-
-#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)");
-               return -1;
-       }
-#endif
-       return 0;
-}
-
 extern int debug;
 
 static int socket_connect(int fd, const char *address, unsigned short port);
@@ -822,7 +805,7 @@ evhttp_connection_free(struct evhttp_connection *evcon)
                event_del(&evcon->ev);
        
        if (evcon->fd != -1)
-               close(evcon->fd);
+               EVUTIL_CLOSESOCKET(evcon->fd);
 
        if (evcon->bind_address != NULL)
                free(evcon->bind_address);
@@ -884,7 +867,7 @@ evhttp_connection_reset(struct evhttp_connection *evcon)
                if (evcon->state == EVCON_CONNECTED && evcon->closecb != NULL)
                        (*evcon->closecb)(evcon, evcon->closecb_arg);
 
-               close(evcon->fd);
+               EVUTIL_CLOSESOCKET(evcon->fd);
                evcon->fd = -1;
        }
        evcon->state = EVCON_DISCONNECTED;
@@ -1504,7 +1487,7 @@ evhttp_connection_connect(struct evhttp_connection *evcon)
        }
 
        if (socket_connect(evcon->fd, evcon->address, evcon->port) == -1) {
-               close(evcon->fd); evcon->fd = -1;
+               EVUTIL_CLOSESOCKET(evcon->fd); evcon->fd = -1;
                return (-1);
        }
 
@@ -1949,7 +1932,7 @@ accept_socket(int fd, short what, void *arg)
                event_warn("%s: bad accept", __func__);
                return;
        }
-        if (event_make_socket_nonblocking(nfd) < 0)
+        if (evutil_make_socket_nonblocking(nfd) < 0)
                 return;
 
        evhttp_get_request(http, nfd, (struct sockaddr *)&ss, addrlen);
@@ -1966,7 +1949,7 @@ evhttp_bind_socket(struct evhttp *http, const char *address, u_short port)
 
        if (listen(fd, 10) == -1) {
                event_warn("%s: listen", __func__);
-               close(fd);
+               EVUTIL_CLOSESOCKET(fd);
                return (-1);
        }
 
@@ -2034,7 +2017,7 @@ evhttp_free(struct evhttp* http)
 
        /* Remove the accepting part */
        event_del(&http->bind_ev);
-       close(fd);
+       EVUTIL_CLOSESOCKET(fd);
 
        while ((evcon = TAILQ_FIRST(&http->connections)) != NULL) {
                /* evhttp_connection_free removes the connection */
@@ -2347,7 +2330,7 @@ bind_socket_ai(struct addrinfo *ai)
                 return (-1);
         }
 
-        if (event_make_socket_nonblocking(fd) < 0)
+        if (evutil_make_socket_nonblocking(fd) < 0)
                 goto out;
 
 #ifndef WIN32
@@ -2371,7 +2354,7 @@ bind_socket_ai(struct addrinfo *ai)
 
  out:
        serrno = errno;
-       close(fd);
+       EVUTIL_CLOSESOCKET(fd);
        errno = serrno;
        return (-1);
 }