]> granicus.if.org Git - libevent/commitdiff
Functions to actually use evhttp_bound_socket with/as evconnlistener.
authorNick Mathewson <nickm@torproject.org>
Mon, 25 Oct 2010 15:50:51 +0000 (11:50 -0400)
committerNick Mathewson <nickm@torproject.org>
Mon, 25 Oct 2010 15:50:51 +0000 (11:50 -0400)
http.c
include/event2/http.h

diff --git a/http.c b/http.c
index c88522db06a3787635d1b3056ca495d9fdddf430..162f955a26cd633949515eabb9d0c3e796d520e6 100644 (file)
--- 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;
 
-       bound = mm_malloc(sizeof(struct evhttp_bound_socket));
-       if (bound == NULL)
-               return (NULL);
-
-       listener = evconnlistener_new(http->base, accept_socket_cb, http,
+       listener = evconnlistener_new(http->base, NULL, NULL,
            flags,
            0, /* Backlog is '0' because we already said 'listen' */
            fd);
-       if (!listener) {
-               mm_free(bound);
+       if (!listener)
+               return (NULL);
+
+       bound = evhttp_bind_listener(http, listener);
+       if (!bound) {
+               evconnlistener_free(listener);
                return (NULL);
        }
-       bound->listener = listener;
+       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);
+
+       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)
 {
index 5e96934d9ff9d07945d25cd13f02dfa3803266bf..66246f841bf43102b03c14d5f545e4bc0834fb25 100644 (file)
@@ -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
  *