Define evhttp_{bind,accept}_socket_with_handle

[Patch from David Reiss]

svn:r1422
This commit is contained in:
Nick Mathewson 2009-08-16 19:22:04 +00:00
parent 4bcd5646d8
commit 6c53334c65
2 changed files with 57 additions and 8 deletions

40
http.c
View File

@ -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)

View File

@ -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.
*