From 3ef3c65b4d19df39e020c1d5f778dafdf493a635 Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Wed, 2 May 2018 00:22:25 +0200 Subject: POTD skeleton #39. Signed-off-by: Toni Uhlig --- src/server.c | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) (limited to 'src/server.c') diff --git a/src/server.c b/src/server.c index afa2172..9bf41f7 100644 --- a/src/server.c +++ b/src/server.c @@ -62,11 +62,13 @@ int server_setup(server_ctx *ctx, return 1; } if (socket_bind_in(&ctx->sock, &srv_addr)) { - E_STRERR("Could not bind server socket"); + E_STRERR("Could not bind server socket to %s:%s", + listen_addr, listen_port); return 1; } if (socket_listen_in(&ctx->sock)) { - E_STRERR("Could not listen on server socket"); + E_STRERR("Could not listen on server socket on %s:%s", + listen_addr, listen_port); return 1; } @@ -137,7 +139,7 @@ pid_t server_daemonize(int epoll_fd, server_ctx *ctx[], size_t siz) p = fork(); switch (p) { case -1: - W_STRERR("Server daemonsize"); + W_STRERR("%s", "Server daemonize"); return -1; case 0: N("%s", "Server daemon mainloop"); @@ -177,8 +179,7 @@ static int server_mainloop_epoll(int epoll_fd, server_ctx *ctx[], size_t siz) (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { - E("Epoll for descriptor %d failed", events[i].data.fd); - E_STRERR("epoll_wait"); + E_STRERR("Epoll for descriptor %d failed", events[i].data.fd); close(events[i].data.fd); continue; } else { @@ -209,7 +210,8 @@ static int server_accept_client(server_ctx *ctx[], assert(args); if (socket_accept_in(&ctx[i]->sock, &args->client_psock)) { - E_STRERR("Could not accept client connection"); + E_STRERR("Could not accept client connection on fd %d", + ctx[i]->sock.fd); goto error; } @@ -228,7 +230,9 @@ static int server_accept_client(server_ctx *ctx[], if (pthread_create(&args->self, NULL, client_mainloop_epoll, args)) { - E_STRERR("Thread creation"); + E_STRERR("Thread creation for %s:%s on fd %d", + args->host_buf, args->service_buf, + args->client_psock.fd); goto error; } @@ -262,25 +266,29 @@ client_mainloop_epoll(void *arg) epoll_fd = epoll_create1(0); if (epoll_fd < 0) { - E_STRERR("Client epoll_create1"); + E_STRERR("Client Epoll descriptor creation for server fd %d", + args->server_ctx->sock.fd); goto finish; } if (fwd_connect(args->server_ctx->fwd_ctx, &fwd)) { - E("Forward connection to %s:%s failed", + E_STRERR("Forward connection to %s:%s server fd %d", args->server_ctx->fwd_ctx->host_buf, - args->server_ctx->fwd_ctx->service_buf); - E_STRERR("Forward connect"); + args->server_ctx->fwd_ctx->service_buf, + args->server_ctx->sock.fd); goto finish; } - N("Forwarding connection to %s:%s: %d", args->server_ctx->fwd_ctx->host_buf, + N("Forwarding connection to %s:%s forward fd %d", + args->server_ctx->fwd_ctx->host_buf, args->server_ctx->fwd_ctx->service_buf, fwd.fd); event.data.fd = fwd.fd; event.events = EPOLLIN | EPOLLET; s = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fwd.fd, &event); if (s) { - E_STRERR("Forward epoll_ctl"); + E_STRERR("Forward Epoll descriptor add to %s:%s forward fd %d", + args->server_ctx->fwd_ctx->host_buf, + args->server_ctx->fwd_ctx->service_buf, fwd.fd); goto finish; } @@ -290,14 +298,18 @@ client_mainloop_epoll(void *arg) */ s = socket_nonblock(&args->client_psock); if (s) { - E_STRERR("socket_nonblock"); + E_STRERR("Socket non blocking mode to %s:%s forward fd %d", + args->server_ctx->fwd_ctx->host_buf, + args->server_ctx->fwd_ctx->service_buf, fwd.fd); goto finish; } event.data.fd = args->client_psock.fd; event.events = EPOLLIN | EPOLLET; s = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, args->client_psock.fd, &event); if (s) { - E_STRERR("Client epoll_ctl"); + E_STRERR("Forward Epoll descriptor add to %s:%s forward fd %d", + args->server_ctx->fwd_ctx->host_buf, + args->server_ctx->fwd_ctx->service_buf, fwd.fd); goto finish; } @@ -314,8 +326,7 @@ client_mainloop_epoll(void *arg) (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { - E("Epoll for descriptor %d failed", events[i].data.fd); - E_STRERR("epoll_pwait"); + E_STRERR("Epoll for descriptor %d", events[i].data.fd); active = 0; break; } else { @@ -401,7 +412,7 @@ client_io_epoll(struct epoll_event *ev, int dest_fd) switch (siz) { case -1: - E_STRERR("Client read"); + E_STRERR("Client read from fd %d", ev->data.fd); rc = CON_IN_ERROR; break; case 0: -- cgit v1.2.3