Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Added evhtp_accept_socket
Browse files Browse the repository at this point in the history
Based on https://github.com/ellzey/libevhtp/pull/126 @simonas added a function
to setup the listener on an already bound socket.

The only problem I had was the duplicate code; so I fixed it in this manner
(and it flows quite well)

evhtp_bind_socket parses the data to which the server will listen on, then
creates a struct sockaddr.

evhtp_bind_socket then calls evhtp_bind_sockaddr, which creates the socket,
sets some options, and calls bind.

evhtp_bind_sockaddr will then call evhtp_accept_socket with the newly created
socket. evhtp_accept_socket will then create the listener context using only
that file descriptor (and the evhtp_t structure).

The result is clean and exactly what @simonas wanted without the code
duplication.
  • Loading branch information
Mark Ellzey committed Nov 26, 2015
1 parent bd70b5e commit a497a14
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions evhtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3179,53 +3179,40 @@ evhtp_unbind_socket(evhtp_t * htp) {
}

int
evhtp_bind_sockaddr(evhtp_t * htp, struct sockaddr * sa, size_t sin_len, int backlog) {
#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#endif
evutil_socket_t fd;
int on = 1;

fd = socket(sa->sa_family, SOCK_STREAM, 0);
evhtp_errno_assert(fd != -1);

evutil_make_socket_closeonexec(fd);
evutil_make_socket_nonblocking(fd);

setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
evhtp_accept_socket(evhtp_t * htp, evutil_socket_t sock, int backlog) {
int on = 1;

if (sa->sa_family == AF_INET6) {
int rc;
evhtp_assert(htp != NULL);

rc = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
evhtp_errno_assert(rc != -1);
if (sock == -1) {
return -1;
}

#if defined SO_REUSEPORT
if (htp->enable_reuseport) {
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on));
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on));
}
#endif

#if defined TCP_NODELAY
if (htp->enable_nodelay == 1) {
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on));
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on));
}
#endif

#if defined TCP_DEFER_ACCEPT
if (htp->enable_defer_accept == 1) {
setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, (void *)&on, sizeof(on));
setsockopt(sock, IPPROTO_TCP, TCP_DEFER_ACCEPT, (void *)&on, sizeof(on));
}
#endif

evhtp_errno_assert(bind(fd, sa, sin_len) != -1);

htp->server = evconnlistener_new(htp->evbase, _evhtp_accept_cb, htp,
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE,
backlog, fd);
evhtp_errno_assert(htp->server != NULL);
backlog, sock);

if (htp->server == NULL) {
return -1;
}

#ifndef EVHTP_DISABLE_SSL
if (htp->ssl_ctx != NULL) {
Expand All @@ -3240,8 +3227,38 @@ evhtp_bind_sockaddr(evhtp_t * htp, struct sockaddr * sa, size_t sin_len, int bac
}
}
#endif

return 0;
} /* evhtp_accept_socket */

int
evhtp_bind_sockaddr(evhtp_t * htp, struct sockaddr * sa, size_t sin_len, int backlog) {
#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#endif
evutil_socket_t fd;
int on = 1;

fd = socket(sa->sa_family, SOCK_STREAM, 0);
evhtp_errno_assert(fd != -1);

evutil_make_socket_closeonexec(fd);
evutil_make_socket_nonblocking(fd);

setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));

if (sa->sa_family == AF_INET6) {
int rc;

rc = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
evhtp_errno_assert(rc != -1);
}

if (bind(fd, sa, sin_len) == -1) {
return -1;
}

return evhtp_accept_socket(htp, fd, backlog);
} /* evhtp_bind_sockaddr */

int
Expand Down Expand Up @@ -4378,4 +4395,5 @@ evhtp_make_request(evhtp_connection_t * c, evhtp_request_t * r,
unsigned int
evhtp_request_status(evhtp_request_t * r) {
return htparser_get_status(r->conn->parser);
}
}

0 comments on commit a497a14

Please sign in to comment.