Replace all fds on non-unix-specific APIs with evutil_socket_t, which is int on unix and intptr_t on win32.
svn:r552
o The kqueue implementation now restores original signal handlers correctly when its signal events are removed.
o Check return value of event_add in signal.c
o Add a more powerful evbuffer_readln as a replacement for evbuffer_readline. The new function handles more newline styles, and is more useful with buffers that may contain a nul characters.
+ o Do not mangle socket handles on 64-bit windows.
+
Changes in 1.4.0:
#define EVBUFFER_MAX_READ 4096
int
-evbuffer_read(struct evbuffer *buf, int fd, int howmuch)
+evbuffer_read(struct evbuffer *buf, evutil_socket_t fd, int howmuch)
{
u_char *p;
size_t oldoff = buf->off;
}
int
-evbuffer_write(struct evbuffer *buffer, int fd)
+evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd)
{
int n;
}
static void
-bufferevent_readcb(int fd, short event, void *arg)
+bufferevent_readcb(evutil_socket_t fd, short event, void *arg)
{
struct bufferevent *bufev = arg;
int res = 0;
}
static void
-bufferevent_writecb(int fd, short event, void *arg)
+bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
{
struct bufferevent *bufev = arg;
int res = 0;
*/
struct bufferevent *
-bufferevent_new(int fd, evbuffercb readcb, evbuffercb writecb,
+bufferevent_new(evutil_socket_t fd, evbuffercb readcb, evbuffercb writecb,
everrorcb errorcb, void *cbarg)
{
struct bufferevent *bufev;
};
struct nameserver {
- int socket; /* a connected UDP socket */
+ evutil_socket_t socket; /* a connected UDP socket */
u32 address;
int failed_times; /* number of times which we have given this server a chance */
int timedout; /* number of times in a row a request has timed out */
/* Represents a local port where we're listening for DNS requests. Right now, */
/* only UDP is supported. */
struct evdns_server_port {
- int socket; /* socket we use to read queries and write replies. */
+ evutil_socket_t socket; /* socket we use to read queries and write replies. */
int refcnt; /* reference count. */
char choked; /* Are we currently blocked from writing? */
char closing; /* Are we trying to close this port, pending writes? */
static struct nameserver *nameserver_pick(void);
static void evdns_request_insert(struct request *req, struct request **head);
-static void nameserver_ready_callback(int fd, short events, void *arg);
+static void nameserver_ready_callback(evutil_socket_t fd, short events, void *arg);
static int evdns_transmit(void);
static int evdns_request_transmit(struct request *req);
static void nameserver_send_probe(struct nameserver *const ns);
static int server_request_free(struct server_request *req);
static void server_request_free_answers(struct server_request *req);
static void server_port_free(struct evdns_server_port *port);
-static void server_port_ready_callback(int fd, short events, void *arg);
+static void server_port_ready_callback(evutil_socket_t fd, short events, void *arg);
static int strtoint(const char *const str);
#ifdef WIN32
static int
-last_error(int sock)
+last_error(evutil_socket_t sock)
{
int optval, optvallen=sizeof(optval);
int err = WSAGetLastError();
/* a libevent callback function which is called when a nameserver */
/* has gone down and we want to test if it has came back to life yet */
static void
-nameserver_prod_callback(int fd, short events, void *arg) {
+nameserver_prod_callback(evutil_socket_t fd, short events, void *arg) {
struct nameserver *const ns = (struct nameserver *) arg;
(void)fd;
(void)events;
/* a callback function. Called by libevent when the kernel says that */
/* a nameserver socket is ready for writing or reading */
static void
-nameserver_ready_callback(int fd, short events, void *arg) {
+nameserver_ready_callback(evutil_socket_t fd, short events, void *arg) {
struct nameserver *ns = (struct nameserver *) arg;
(void)fd;
/* a callback function. Called by libevent when the kernel says that */
/* a server socket is ready for writing or reading. */
static void
-server_port_ready_callback(int fd, short events, void *arg) {
+server_port_ready_callback(evutil_socket_t fd, short events, void *arg) {
struct evdns_server_port *port = (struct evdns_server_port *) arg;
(void) fd;
/* this is a libevent callback function which is called when a request */
/* has timed out. */
static void
-evdns_request_timeout_callback(int fd, short events, void *arg) {
+evdns_request_timeout_callback(evutil_socket_t fd, short events, void *arg) {
struct request *const req = (struct request *) arg;
(void) fd;
(void) events;
}
static void
-event_loopexit_cb(int fd, short what, void *arg)
+event_loopexit_cb(evutil_socket_t fd, short what, void *arg)
{
struct event_base *base = arg;
base->event_gotterm = 1;
struct event_once {
struct event ev;
- void (*cb)(int, short, void *);
+ void (*cb)(evutil_socket_t, short, void *);
void *arg;
};
/* One-time callback, it deletes itself */
static void
-event_once_cb(int fd, short events, void *arg)
+event_once_cb(evutil_socket_t fd, short events, void *arg)
{
struct event_once *eonce = arg;
/* not threadsafe, event scheduled once. */
int
-event_once(int fd, short events,
- void (*callback)(int, short, void *), void *arg, struct timeval *tv)
+event_once(evutil_socket_t fd, short events,
+ void (*callback)(evutil_socket_t, short, void *),
+ void *arg, struct timeval *tv)
{
return event_base_once(current_base, fd, events, callback, arg, tv);
}
/* Schedules an event once */
int
-event_base_once(struct event_base *base, int fd, short events,
- void (*callback)(int, short, void *), void *arg, struct timeval *tv)
+event_base_once(struct event_base *base, evutil_socket_t fd, short events,
+ void (*callback)(evutil_socket_t, short, void *),
+ void *arg, struct timeval *tv)
{
struct event_once *eonce;
struct timeval etv;
}
void
-event_set(struct event *ev, int fd, short events,
- void (*callback)(int, short, void *), void *arg)
+event_set(struct event *ev, evutil_socket_t fd, short events,
+ void (*callback)(evutil_socket_t, short, void *), void *arg)
{
/* Take the current base - caller needs to set the real base later */
ev->ev_base = current_base;
struct event_base *ev_base;
- int ev_fd;
+ evutil_socket_t ev_fd;
short ev_events;
short ev_ncalls;
short *ev_pncalls; /* Allows deletes in callback */
int ev_pri; /* smaller numbers are higher priority */
- void (*ev_callback)(int, short, void *arg);
+ void (*ev_callback)(evutil_socket_t, short, void *arg);
void *ev_arg;
int ev_res; /* result passed to event callback */
@see event_add(), event_del(), event_once()
*/
-void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
+void event_set(struct event *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *);
/**
Schedule a one-time event to occur.
@see event_set()
*/
-int event_once(int, short, void (*)(int, short, void *), void *, struct timeval *);
+int event_once(evutil_socket_t , short, void (*)(evutil_socket_t, short, void *), void *, struct timeval *);
/**
@return 0 if successful, or -1 if an error occurred
@see event_once()
*/
-int event_base_once(struct event_base *, int, short, void (*)(int, short, void *), void *, struct timeval *);
+int event_base_once(struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *, struct timeval *);
/**
*/
int event_del(struct event *);
+/**
+ Make an event active.
+
+ @param ev an event to make active.
+ @param res a set of flags to pass to the event's callback.
+ @param ncalls
+ **/
void event_active(struct event *, int, short);
error occurred
@see bufferevent_base_set(), bufferevent_free()
*/
-struct bufferevent *bufferevent_new(int fd,
+struct bufferevent *bufferevent_new(evutil_socket_t fd,
evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
@return the number of bytes written, or -1 if an error occurred
@see evbuffer_read()
*/
-int evbuffer_write(struct evbuffer *, int);
+int evbuffer_write(struct evbuffer *, evutil_socket_t);
/**
@return the number of bytes read, or -1 if an error occurred
@see evbuffer_write()
*/
-int evbuffer_read(struct evbuffer *, int, int);
+int evbuffer_read(struct evbuffer *, evutil_socket_t, int);
/**
*
* @param address a string containing the IP address to listen(2) on
* @param port the port number to listen on
- * @return a newly allocated evhttp struct
+ * @return 0 on success, -1 on failure.
* @see evhttp_free()
*/
int evhttp_bind_socket(struct evhttp *http, const char *address, u_short port);
static void evrpc_reply_done(struct evhttp_request *, void *);
-static void evrpc_request_timeout(int, short, void *);
+static void evrpc_request_timeout(evutil_socket_t, short, void *);
/*
* Finds a connection object associated with the pool that is currently
}
static void
-evrpc_request_timeout(int fd, short what, void *arg)
+evrpc_request_timeout(evutil_socket_t fd, short what, void *arg)
{
struct evrpc_request_wrapper *ctx = arg;
struct evhttp_connection *evcon = ctx->evcon;
#ifndef _EVSIGNAL_H_
#define _EVSIGNAL_H_
+#ifndef evutil_socket_t
+#include "evutil.h"
+#endif
+
typedef void (*ev_sighandler_t)(int);
struct evsignal_info {
struct event_list signalqueue;
struct event ev_signal;
- int ev_signal_pair[2];
+ evutil_socket_t ev_signal_pair[2];
int ev_signal_added;
volatile sig_atomic_t evsignal_caught;
sig_atomic_t evsigcaught[NSIG];
#include "log.h"
int
-evutil_socketpair(int family, int type, int protocol, int fd[2])
+evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2])
{
#ifndef WIN32
return socketpair(family, type, protocol, fd);
* for now, and really, when localhost is down sometimes, we
* have other problems too.
*/
- int listener = -1;
- int connector = -1;
- int acceptor = -1;
+ evutil_socket_t listener = -1;
+ evutil_socket_t connector = -1;
+ evutil_socket_t acceptor = -1;
struct sockaddr_in listen_addr;
struct sockaddr_in connect_addr;
int size;
}
int
-evutil_make_socket_nonblocking(int fd)
+evutil_make_socket_nonblocking(evutil_socket_t fd)
{
#ifdef WIN32
{
#include <sys/time.h>
#endif
-int evutil_socketpair(int d, int type, int protocol, int sv[2]);
-int evutil_make_socket_nonblocking(int sock);
+#ifdef _EVENT_HAVE_STDINT_H
+#include <stdint.h>
+#define ev_uint64_t uint64_t
+#define ev_uint32_t uint32_t
+#define ev_uint16_t uint16_t
+#define ev_uint8_t uint8_t
+#elif defined(WIN32)
+#define ev_uint64_t __uint64_t
+#define ev_uint32_t unsigned int
+#define ev_uint16_t unsigned short
+#define ev_uint8_t unsigned char
+#endif
+
+#ifdef WIN32
+/** Type to hold the output of "socket()" or "accept()". On Windows, this is
+ * an intptr_t; elsewhere, it is an int. */
+#define evutil_socket_t intptr_t
+#else
+#define evutil_socket_t int
+#endif
+
+int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
+int evutil_make_socket_nonblocking(evutil_socket_t sock);
#ifdef WIN32
#define EVUTIL_CLOSESOCKET(s) closesocket(s)
#else
#define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
#endif
-#ifdef _EVENT_HAVE_STDINT_H
-#include <stdint.h>
-#define ev_uint64_t uint64_t
-#define ev_uint32_t uint32_t
-#define ev_uint16_t uint16_t
-#define ev_uint8_t uint8_t
-#elif defined(WIN32)
-#define ev_uint64_t __uint64_t
-#define ev_uint32_t unsigned int
-#define ev_uint16_t unsigned short
-#define ev_uint8_t unsigned char
-#endif
-
#ifdef __cplusplus
}
#endif
/* we use tailq only if they were created for an http server */
TAILQ_ENTRY(evhttp_connection) (next);
- int fd;
+ evutil_socket_t fd;
struct event ev;
struct event close_ev;
struct evbuffer *input_buffer;
void evhttp_connection_fail(struct evhttp_connection *,
enum evhttp_connection_error error);
-void evhttp_get_request(struct evhttp *, int, struct sockaddr *, socklen_t);
+void evhttp_get_request(struct evhttp *, evutil_socket_t, struct sockaddr *, socklen_t);
int evhttp_hostportfile(char *, char **, u_short *, char **);
int evhttp_parse_lines(struct evhttp_request *, struct evbuffer*);
void evhttp_start_read(struct evhttp_connection *);
-void evhttp_read_header(int, short, void *);
+void evhttp_read_header(evutil_socket_t, short, void *);
void evhttp_make_header(struct evhttp_connection *, struct evhttp_request *);
void evhttp_write_buffer(struct evhttp_connection *,
extern int debug;
-static int socket_connect(int fd, const char *address, unsigned short port);
-static int bind_socket_ai(struct addrinfo *);
-static int bind_socket(const char *, u_short);
+static int socket_connect(evutil_socket_t kefd, const char *address, unsigned short port);
+static evutil_socket_t bind_socket_ai(struct addrinfo *);
+static evutil_socket_t bind_socket(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);
struct evhttp_connection *evcon);
static void evhttp_request_dispatch(struct evhttp_connection* evcon);
-void evhttp_read(int, short, void *);
-void evhttp_write(int, short, void *);
+void evhttp_read(evutil_socket_t, short, void *);
+void evhttp_write(evutil_socket_t, short, void *);
#ifndef HAVE_STRSEP
/* strsep replacement for platforms that lack it. Only works if
}
void
-evhttp_write(int fd, short what, void *arg)
+evhttp_write(evutil_socket_t fd, short what, void *arg)
{
struct evhttp_connection *evcon = arg;
int n;
*/
void
-evhttp_read(int fd, short what, void *arg)
+evhttp_read(evutil_socket_t fd, short what, void *arg)
{
struct evhttp_connection *evcon = arg;
struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
}
static void
-evhttp_detect_close_cb(int fd, short what, void *arg)
+evhttp_detect_close_cb(evutil_socket_t fd, short what, void *arg)
{
struct evhttp_connection *evcon = arg;
evhttp_connection_reset(evcon);
}
static void
-evhttp_connection_retry(int fd, short what, void *arg)
+evhttp_connection_retry(evutil_socket_t fd, short what, void *arg)
{
struct evhttp_connection *evcon = arg;
*/
static void
-evhttp_connectioncb(int fd, short what, void *arg)
+evhttp_connectioncb(evutil_socket_t fd, short what, void *arg)
{
struct evhttp_connection *evcon = arg;
int error;
}
void
-evhttp_read_header(int fd, short what, void *arg)
+evhttp_read_header(evutil_socket_t fd, short what, void *arg)
{
struct evhttp_connection *evcon = arg;
struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
}
static void
-accept_socket(int fd, short what, void *arg)
+accept_socket(evutil_socket_t fd, short what, void *arg)
{
struct evhttp *http = arg;
struct sockaddr_storage ss;
socklen_t addrlen = sizeof(ss);
- int nfd;
+ evutil_socket_t nfd;
if ((nfd = accept(fd, (struct sockaddr *)&ss, &addrlen)) == -1) {
event_warn("%s: bad accept", __func__);
evhttp_bind_socket(struct evhttp *http, const char *address, u_short port)
{
struct event *ev = &http->bind_ev;
- int fd;
+ evutil_socket_t fd;
if ((fd = bind_socket(address, port)) == -1)
return (-1);
{
struct evhttp_cb *http_cb;
struct evhttp_connection *evcon;
- int fd = http->bind_ev.ev_fd;
+ evutil_socket_t fd = http->bind_ev.ev_fd;
/* Remove the accepting part */
event_del(&http->bind_ev);
static struct evhttp_connection*
evhttp_get_request_connection(
struct evhttp* http,
- int fd, struct sockaddr *sa, socklen_t salen)
+ evutil_socket_t fd, struct sockaddr *sa, socklen_t salen)
{
struct evhttp_connection *evcon;
char *hostname, *portname;
}
void
-evhttp_get_request(struct evhttp *http, int fd,
+evhttp_get_request(struct evhttp *http, evutil_socket_t fd,
struct sockaddr *sa, socklen_t salen)
{
struct evhttp_connection *evcon;
/* Either connect or bind */
-static int
+static evutil_socket_t
bind_socket_ai(struct addrinfo *ai)
{
struct linger linger;
- int fd, on = 1, r;
+ evutil_socket_t fd;
+
+ int on = 1, r;
int serrno;
/* Create listen socket */
return (aitop);
}
-static int
+static evutil_socket_t
bind_socket(const char *address, u_short port)
{
- int fd;
+ evutil_socket_t fd;
struct addrinfo *aitop = make_addrinfo(address, port);
if (aitop == NULL)
}
static int
-socket_connect(int fd, const char *address, unsigned short port)
+socket_connect(evutil_socket_t fd, const char *address, unsigned short port)
{
struct addrinfo *ai = make_addrinfo(address, port);
int res = -1;
#include "event.h"
#include "event-internal.h"
-#include "evsignal.h"
#include "evutil.h"
+#include "evsignal.h"
#include "log.h"
struct event_base *evsignal_base = NULL;
/* Callback for when the signal handler write a byte to our signaling socket */
static void
-evsignal_cb(int fd, short what, void *arg)
+evsignal_cb(evutil_socket_t fd, short what, void *arg)
{
static char signals[100];
#ifdef WIN32