From 37895358b03b805f1887749c8dc73d1e075af810 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 28 Apr 2023 12:58:42 +0200 Subject: make struct tcp_server opaque --- src/tcp_server.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'src/tcp_server.c') diff --git a/src/tcp_server.c b/src/tcp_server.c index 87613a5..acd3320 100644 --- a/src/tcp_server.c +++ b/src/tcp_server.c @@ -15,18 +15,39 @@ #include #include -int tcp_server_create(struct tcp_server *server, const char *port) +struct tcp_server { + int fd; +}; + +int tcp_server_create(struct tcp_server **_server, const char *port) { - server->fd = net_bind(port); - if (server->fd < 0) - return server->fd; + struct tcp_server *server; + int ret = 0; + + *_server = malloc(sizeof(struct tcp_server)); + if (!*_server) { + log_errno("malloc"); + return -1; + } + server = *_server; - return 0; + ret = net_bind(port); + if (ret < 0) + goto free; + server->fd = ret; + + return ret; + +free: + free(server); + + return ret; } -void tcp_server_destroy(const struct tcp_server *server) +void tcp_server_destroy(struct tcp_server *server) { log_errno_if(close(server->fd), "close"); + free(server); } struct child_context { -- cgit v1.2.3