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;
bound = mm_malloc(sizeof(struct evhttp_bound_socket));
if (bound == NULL)
- return (-1);
+ return (NULL);
ev = &bound->bind_ev;
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)
*/
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.
*
*/
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.
*