]> granicus.if.org Git - libevent/commitdiff
Define evhttp_{bind,accept}_socket_with_handle
authorNick Mathewson <nickm@torproject.org>
Sun, 16 Aug 2009 19:22:04 +0000 (19:22 +0000)
committerNick Mathewson <nickm@torproject.org>
Sun, 16 Aug 2009 19:22:04 +0000 (19:22 +0000)
[Patch from David Reiss]

svn:r1422

http.c
include/event2/http.h

diff --git a/http.c b/http.c
index 39528fc712199e302a307f88878005eb54887202..2c94edb46e7ae4f07b3e822b145544c81774f001 100644 (file)
--- 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)
index 93c805f801be41403860a9b5c51f0c9863bf2e53..c65b7f29490a548c5df7ea6eae9226ae18a2d6e4 100644 (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.
  *