From: Niels Provos Date: Wed, 22 Nov 2006 01:21:10 +0000 (+0000) Subject: mingw fixes from Nick X-Git-Tag: release-2.0.1-alpha~688 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=868f10e7c9abe1cb38cd3cc84e6dd4a9b1d966cc;p=libevent mingw fixes from Nick svn:r271 --- diff --git a/WIN32-Code/misc.c b/WIN32-Code/misc.c index 6f63ddfe..bdebc3a4 100644 --- a/WIN32-Code/misc.c +++ b/WIN32-Code/misc.c @@ -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 +} diff --git a/WIN32-Code/win32.c b/WIN32-Code/win32.c index 701d1c3c..867d1f22 100644 --- a/WIN32-Code/win32.c +++ b/WIN32-Code/win32.c @@ -25,11 +25,15 @@ * (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 +#else +/* Avoid the windows/msvc thing. */ +#include "../config.h" +#endif #include +#include #include #include #include diff --git a/configure.in b/configure.in index fa43a9cc..ddcde2fb 100644 --- a/configure.in +++ b/configure.in @@ -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 6c16bbd2..1ddad821 100644 --- a/evdns.c +++ b/evdns.c @@ -39,6 +39,10 @@ #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 bd370f84..78f44bbc 100644 --- a/event.h +++ b/event.h @@ -38,6 +38,7 @@ extern "C" { #include #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 diff --git a/event_tagging.c b/event_tagging.c index ac4e3475..b77494d7 100644 --- a/event_tagging.c +++ b/event_tagging.c @@ -32,7 +32,14 @@ #include "config.h" #endif +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef WIN32_LEAN_AND_MEAN +#else #include +#endif + #include #include #ifdef HAVE_SYS_TIME_H @@ -43,7 +50,9 @@ #include #include #include +#ifndef WIN32 #include +#endif #include #include "event.h" diff --git a/evhttp.h b/evhttp.h index 9f7dafe7..f31a013c 100644 --- a/evhttp.h +++ b/evhttp.h @@ -34,8 +34,8 @@ extern "C" { #ifdef WIN32 #define WIN32_LEAN_AND_MEAN #include +#include #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 e422ccb6..bf66c181 100644 --- a/evrpc.c +++ b/evrpc.c @@ -31,15 +31,19 @@ #ifdef WIN32 #define WIN32_LEAN_AND_MEAN #include +#include #undef WIN32_LEAN_AND_MEAN #include "misc.h" #endif + #include #include +#ifndef WIN32 #include +#endif #ifdef HAVE_SYS_TIME_H #include -#else +#else #include #endif #include diff --git a/http.c b/http.c index 32895fc5..8213d7e2 100644 --- a/http.c +++ b/http.c @@ -38,22 +38,30 @@ #ifdef HAVE_SYS_IOCCOM_H #include #endif + +#ifndef WIN32 #include #include #include -#include #include +#endif + +#include #include +#ifndef WIN32 #include #include +#endif #include #include #include #include #include +#ifndef WIN32 #include +#endif #include #ifdef HAVE_TIME_H #include @@ -69,18 +77,66 @@ #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); }