From: Nick Mathewson Date: Sun, 16 Aug 2009 19:22:04 +0000 (+0000) Subject: Define evhttp_{bind,accept}_socket_with_handle X-Git-Tag: release-2.0.3-alpha~116 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6c53334c65769acd79b423cb89bbd456c7c83977;p=libevent Define evhttp_{bind,accept}_socket_with_handle [Patch from David Reiss] svn:r1422 --- diff --git a/http.c b/http.c index 39528fc7..2c94edb4 100644 --- a/http.c +++ b/http.c @@ -2382,30 +2382,54 @@ accept_socket(evutil_socket_t fd, short what, void *arg) int evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t port) +{ + struct evhttp_bound_socket *bound = + evhttp_bind_socket_with_handle(http, address, port); + if (bound == NULL) + return (-1); + return (0); +} + +struct evhttp_bound_socket * +evhttp_bind_socket_with_handle(struct evhttp *http, const char *address, ev_uint16_t port) { evutil_socket_t fd; int res; if ((fd = bind_socket(address, port, 1 /*reuse*/)) == -1) - return (-1); + return (NULL); if (listen(fd, 128) == -1) { event_sock_warn(fd, "%s: listen", __func__); EVUTIL_CLOSESOCKET(fd); - return (-1); + return (NULL); } - res = evhttp_accept_socket(http, fd); + struct evhttp_bound_socket *bound = + evhttp_accept_socket_with_handle(http, fd); - if (res != -1) + if (bound != NULL) { event_debug(("Bound to port %d - Awaiting connections ... ", port)); + return (bound); + } - return (res); + return (NULL); } int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd) +{ + struct evhttp_bound_socket *bound = + evhttp_accept_socket_with_handle(http, fd); + if (bound == NULL) + return (-1); + return (0); +} + + +struct evhttp_bound_socket * +evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd) { struct evhttp_bound_socket *bound; struct event *ev; @@ -2413,7 +2437,7 @@ evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd) bound = mm_malloc(sizeof(struct evhttp_bound_socket)); if (bound == NULL) - return (-1); + return (NULL); ev = &bound->bind_ev; @@ -2425,12 +2449,12 @@ evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd) if (res == -1) { mm_free(bound); - return (-1); + return (NULL); } TAILQ_INSERT_TAIL(&http->sockets, bound, next); - return (0); + return (bound); } evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound) diff --git a/include/event2/http.h b/include/event2/http.h index 93c805f8..c65b7f29 100644 --- a/include/event2/http.h +++ b/include/event2/http.h @@ -94,6 +94,19 @@ struct evhttp *evhttp_new(struct event_base *base); */ int evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t port); +/** + * Like evhttp_bind_socket(), but returns a handle for referencing the socket. + * + * The returned pointer is not valid after \a http is freed. + * + * @param http a pointer to an evhttp object + * @param address a string containing the IP address to listen(2) on + * @param port the port number to listen on + * @return Handle for the socket on success, NULL on failure. + * @see evhttp_bind_socket(), evhttp_del_accept_socket() + */ +struct evhttp_bound_socket *evhttp_bind_socket_with_handle(struct evhttp *http, const char *address, ev_uint16_t port); + /** * Makes an HTTP server accept connections on the specified socket. * @@ -112,6 +125,18 @@ int evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t por */ int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd); +/** + * Like evhttp_accept_socket(), but returns a handle for referencing the socket. + * + * The returned pointer is not valid after \a http is freed. + * + * @param http a pointer to an evhttp object + * @param fd a socket fd that is ready for accepting connections + * @return Handle for the socket on success, NULL on failure. + * @see evhttp_accept_socket(), evhttp_del_accept_socket() + */ +struct evhttp_bound_socket *evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd); + /** * Get the raw file descriptor referenced by an evhttp_bound_socket. *