From 006efa7dbbcaec4d412670107b893f04d38f0d83 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 25 Oct 2010 11:50:51 -0400 Subject: [PATCH] Functions to actually use evhttp_bound_socket with/as evconnlistener. --- http.c | 38 ++++++++++++++++++++++++++++---------- include/event2/http.h | 13 +++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/http.c b/http.c index c88522db..162f955a 100644 --- a/http.c +++ b/http.c @@ -2764,23 +2764,35 @@ evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd) const int flags = LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_CLOSE_ON_FREE; + listener = evconnlistener_new(http->base, NULL, NULL, + flags, + 0, /* Backlog is '0' because we already said 'listen' */ + fd); + if (!listener) + return (NULL); + + bound = evhttp_bind_listener(http, listener); + if (!bound) { + evconnlistener_free(listener); + return (NULL); + } + return (bound); +} + +struct evhttp_bound_socket * +evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener) +{ + struct evhttp_bound_socket *bound; + bound = mm_malloc(sizeof(struct evhttp_bound_socket)); if (bound == NULL) return (NULL); - listener = evconnlistener_new(http->base, accept_socket_cb, http, - flags, - 0, /* Backlog is '0' because we already said 'listen' */ - fd); - if (!listener) { - mm_free(bound); - return (NULL); - } bound->listener = listener; - TAILQ_INSERT_TAIL(&http->sockets, bound, next); - return (bound); + evconnlistener_set_cb(listener, accept_socket_cb, http); + return bound; } evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound) @@ -2788,6 +2800,12 @@ evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound) return evconnlistener_get_fd(bound->listener); } +struct evconnlistener * +evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound) +{ + return bound->listener; +} + void evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *bound) { diff --git a/include/event2/http.h b/include/event2/http.h index 5e96934d..66246f84 100644 --- a/include/event2/http.h +++ b/include/event2/http.h @@ -63,6 +63,7 @@ struct evhttp; struct evhttp_request; struct evkeyvalq; struct evhttp_bound_socket; +struct evconnlistener; /** * Create a new HTTP server. @@ -130,6 +131,18 @@ int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd); */ struct evhttp_bound_socket *evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd); +/** + * The most low-level evhttp_bind/accept method: takes an evconnlistener, and + * returns an evhttp_bound_socket. The listener will be freed when the bound + * socket is freed. + */ +struct evhttp_bound_socket *evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener); + +/** + * Return the listener used to implement a bound socket. + */ +struct evconnlistener *evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound); + /** * Makes an HTTP server stop accepting connections on the specified socket *