aboutsummaryrefslogtreecommitdiff
path: root/src/server.c
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2018-04-15 19:30:28 +0200
committerToni Uhlig <matzeton@googlemail.com>2018-04-15 19:30:28 +0200
commitcaded52a6c9f1ae86ed05dbe48074ef8a61a4027 (patch)
tree369d835a5be9aeb72d4779a447490626e411a7f5 /src/server.c
parent793909637762f62b793f8223b60d933bda77e620 (diff)
POTD skeleton #6.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/server.c')
-rw-r--r--src/server.c44
1 files changed, 25 insertions, 19 deletions
diff --git a/src/server.c b/src/server.c
index 60925b6..6a76f1b 100644
--- a/src/server.c
+++ b/src/server.c
@@ -11,8 +11,7 @@
typedef struct client_thread_args {
pthread_t self;
- int client_fd;
- struct sockaddr_in clientaddr;
+ psocket client_psock;
char host_buf[NI_MAXHOST], service_buf[NI_MAXSERV];
const server_ctx *server_ctx;
} client_thread_args;
@@ -47,7 +46,7 @@ int server_validate_ctx(const server_ctx *ctx)
return 0;
}
-int server_setup_epoll(const server_ctx ctx[], size_t siz)
+int server_setup_epoll(server_ctx ctx[], size_t siz)
{
int s, fd = epoll_create1(0); /* flags == 0 -> obsolete size arg is dropped */
struct epoll_event ev;
@@ -62,10 +61,19 @@ int server_setup_epoll(const server_ctx ctx[], size_t siz)
ev.data.fd = ctx[i].sock.fd;
ev.events = EPOLLIN | EPOLLET;
+ s = socket_addrtostr_in(&ctx[i].sock,
+ ctx[i].host_buf, ctx[i].service_buf);
+ if (s) {
+ E_GAIERR(s, "Convert socket address to string");
+ return -2;
+ }
+ N("Redirector service listening on %s:%s",
+ ctx[i].host_buf, ctx[i].service_buf);
+
s = epoll_ctl(fd, EPOLL_CTL_ADD, ctx[i].sock.fd, &ev);
if (s) {
close(fd);
- return -2;
+ return -3;
}
}
@@ -117,30 +125,28 @@ static int server_accept_client(const server_ctx ctx[],
size_t siz, struct epoll_event *event)
{
size_t i;
- int s, client_fd;
- struct sockaddr_in clientaddr;
+ int s;
+ client_thread_args *args;
for (i = 0; i < siz; ++i) {
if (ctx[i].sock.fd == event->data.fd) {
- client_fd = socket_accept_in(&ctx[i].sock, &clientaddr);
- if (client_fd < 0) {
+ args = (client_thread_args *) calloc(1, sizeof(client_thread_args));
+
+ if (socket_accept_in(&ctx[i].sock, &args->client_psock)) {
E_STRERR("Could not accept client connection");
return 0;
}
- client_thread_args *args =
- (client_thread_args *) malloc(sizeof(client_thread_args));
- args->client_fd = client_fd;
- args->clientaddr = clientaddr;
args->server_ctx = &ctx[i];
- s = socket_addrtostr_in(&clientaddr,
+ s = socket_addrtostr_in(&args->client_psock,
args->host_buf, args->service_buf);
if (s) {
E_GAIERR(s, "Convert socket address to string");
goto error;
}
- N("New client connection from %s:%s",
- args->host_buf, args->service_buf);
+ N("New connection from %s:%s to %s:%s",
+ args->host_buf, args->service_buf,
+ ctx[i].host_buf, ctx[i].service_buf);
if (pthread_create(&args->self, NULL,
client_mainloop_epoll, args))
@@ -151,7 +157,7 @@ static int server_accept_client(const server_ctx ctx[],
return 1;
error:
- close(client_fd);
+ close(args->client_psock.fd);
free(args);
return 0;
}
@@ -178,16 +184,16 @@ client_mainloop_epoll(void *arg)
if (epoll_fd < 0)
goto finish;
- event.data.fd = args->client_fd;
+ event.data.fd = args->client_psock.fd;
event.events = EPOLLIN | EPOLLOUT | EPOLLET;
memset(&event, 0, sizeof(event));
- s = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, args->client_fd, &event);
+ s = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, args->client_psock.fd, &event);
if (s)
goto finish;
finish:
close(epoll_fd);
- close(args->client_fd);
+ close(args->client_psock.fd);
free(events);
free(args);
return NULL;