From 8178e6719fac6760333b1d9e9532e231d7060f10 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Thu, 15 Dec 2022 16:50:20 +0100 Subject: [PATCH] http,tcp: add http_listen_fd and tcp_sock_alloc_fd (#618) --- include/re_http.h | 2 ++ include/re_tcp.h | 2 ++ src/http/server.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/tcp/tcp.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/include/re_http.h b/include/re_http.h index b022093f2..9a13c0ddb 100644 --- a/include/re_http.h +++ b/include/re_http.h @@ -182,6 +182,8 @@ struct http_conn; typedef void (http_req_h)(struct http_conn *conn, const struct http_msg *msg, void *arg); +int http_listen_fd(struct http_sock **sockp, re_sock_t fd, http_req_h *reqh, + void *arg); int http_listen(struct http_sock **sockp, const struct sa *laddr, http_req_h *reqh, void *arg); int https_listen(struct http_sock **sockp, const struct sa *laddr, diff --git a/include/re_tcp.h b/include/re_tcp.h index 3a68fe50c..343d3bdea 100644 --- a/include/re_tcp.h +++ b/include/re_tcp.h @@ -62,6 +62,8 @@ int tcp_conn_settos(struct tcp_conn *tc, uint32_t tos); /* TCP Connection */ +int tcp_sock_alloc_fd(struct tcp_sock **tsp, re_sock_t fd, tcp_conn_h *ch, + void *arg); int tcp_conn_alloc(struct tcp_conn **tcp, const struct sa *peer, tcp_estab_h *eh, tcp_recv_h *rh, tcp_close_h *ch, void *arg); diff --git a/src/http/server.c b/src/http/server.c index 9b9d638d2..0d32bd287 100644 --- a/src/http/server.c +++ b/src/http/server.c @@ -241,6 +241,46 @@ static void connect_handler(const struct sa *peer, void *arg) } +/** + * Create an HTTP socket from file descriptor + * + * @param sockp Pointer to returned HTTP Socket + * @param fd File descriptor + * @param reqh Request handler + * @param arg Handler argument + * + * @return 0 if success, otherwise errorcode + */ +int http_listen_fd(struct http_sock **sockp, re_sock_t fd, http_req_h *reqh, + void *arg) +{ + struct http_sock *sock; + int err; + + if (!sockp || fd == RE_BAD_SOCK || !reqh) + return EINVAL; + + sock = mem_zalloc(sizeof(*sock), sock_destructor); + if (!sock) + return ENOMEM; + + err = tcp_sock_alloc_fd(&sock->ts, fd, connect_handler, sock); + if (err) + goto out; + + sock->reqh = reqh; + sock->arg = arg; + +out: + if (err) + mem_deref(sock); + else + *sockp = sock; + + return err; +} + + /** * Create an HTTP socket * diff --git a/src/tcp/tcp.c b/src/tcp/tcp.c index 789ea50af..eaa134612 100644 --- a/src/tcp/tcp.c +++ b/src/tcp/tcp.c @@ -551,6 +551,38 @@ static void tcp_conn_handler(int flags, void *arg) } +/** + * Create a TCP Socket with fd + * + * @param tsp Pointer to returned TCP Socket + * @param fd File descriptor + * @param ch Incoming connection handler + * @param arg Handler argument + * + * @return 0 if success, otherwise errorcode + */ +int tcp_sock_alloc_fd(struct tcp_sock **tsp, re_sock_t fd, tcp_conn_h *ch, + void *arg) +{ + struct tcp_sock *ts = NULL; + + if (!tsp || fd == RE_BAD_SOCK) + return EINVAL; + + ts = mem_zalloc(sizeof(*ts), sock_destructor); + if (!ts) + return ENOMEM; + + ts->fd = fd; + ts->connh = ch; + ts->arg = arg; + + *tsp = ts; + + return fd_listen(ts->fd, FD_READ, tcp_conn_handler, ts); +} + + /** * Create a TCP Socket *