aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/tcp_server.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tcp_server.c')
-rw-r--r--src/tcp_server.c33
1 files changed, 27 insertions, 6 deletions
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 <stdlib.h>
#include <unistd.h>
-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 {