diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2018-04-15 19:30:28 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2018-04-15 19:30:28 +0200 |
commit | caded52a6c9f1ae86ed05dbe48074ef8a61a4027 (patch) | |
tree | 369d835a5be9aeb72d4779a447490626e411a7f5 | |
parent | 793909637762f62b793f8223b60d933bda77e620 (diff) |
POTD skeleton #6.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | src/main.c | 4 | ||||
-rw-r--r-- | src/server.c | 44 | ||||
-rw-r--r-- | src/server.h | 3 | ||||
-rw-r--r-- | src/socket.c | 50 | ||||
-rw-r--r-- | src/socket.h | 13 |
5 files changed, 62 insertions, 52 deletions
@@ -26,13 +26,13 @@ int main(int argc, char *argv[]) ssh_ports[1] = "2223"; ssh_ports[2] = "22050"; for (size_t i = 0; i < srv_siz; ++i) { - N("Initialising SSH server on port %s", ssh_ports[i]); + D("Initialising redirector service on port %s", ssh_ports[i]); ABORT_ON_FATAL( server_init_ctx(&srv[i], ssh_init_cb), "Server initialisation" ); server_validate_ctx(&srv[i]); - GAI_ABORT_ON_FATAL( socket_init_in(&srv[i].sock, NULL, ssh_ports[i], &netifs), + GAI_ABORT_ON_FATAL( socket_init_in(NULL, ssh_ports[i], &netifs), "Socket initialisation" ); ABORT_ON_FATAL( socket_bind_in(&srv[i].sock, netifs), 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; diff --git a/src/server.h b/src/server.h index 93343b9..2851c50 100644 --- a/src/server.h +++ b/src/server.h @@ -34,6 +34,7 @@ typedef struct server_ctx { server_callbacks server_cbs; server_data server_dat; psocket sock; + char host_buf[NI_MAXHOST], service_buf[NI_MAXSERV]; } server_ctx; typedef int (*init_cb) (struct server_ctx *ctx); @@ -44,7 +45,7 @@ server_init_ctx(server_ctx *ctx, init_cb init_fn); int server_validate_ctx(const server_ctx *ctx); -int server_setup_epoll(const server_ctx ctx[], size_t siz); +int server_setup_epoll(server_ctx ctx[], size_t siz); int server_mainloop_epoll(int epoll_fd, const server_ctx ctx[], size_t siz); diff --git a/src/socket.c b/src/socket.c index cf48661..4fe5e3a 100644 --- a/src/socket.c +++ b/src/socket.c @@ -10,25 +10,24 @@ #include "socket.h" -static int socket_nonblock(const psocket *psocket) +static int socket_nonblock(const psocket *psock) { int flags; - flags = fcntl(psocket->fd, F_GETFL, 0); + flags = fcntl(psock->fd, F_GETFL, 0); if (flags < 0) return 1; flags |= O_NONBLOCK; - if (fcntl(psocket->fd, F_SETFL, flags) == -1) + if (fcntl(psock->fd, F_SETFL, flags) == -1) return 1; return 0; } -int socket_init_in(psocket *psocket, const char *listen_addr, +int socket_init_in(const char *listen_addr, const char *listen_port, struct addrinfo **results) { struct addrinfo hints; - assert(psocket); assert(listen_addr || listen_port); /* getaddrinfo wants either node or service */ memset(&hints, 0, sizeof(hints)); @@ -39,12 +38,12 @@ int socket_init_in(psocket *psocket, const char *listen_addr, return getaddrinfo(listen_addr, listen_port, &hints, results); } -int socket_bind_in(psocket *psocket, struct addrinfo *results) +int socket_bind_in(psocket *psock, struct addrinfo *results) { int fd = -1, rv, reuse_enable = 1; struct addrinfo *rp = NULL; - assert(psocket && results); + assert(psock && results); for (rp = results; rp != NULL; rp = rp->ai_next) { fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); @@ -61,45 +60,48 @@ int socket_bind_in(psocket *psocket, struct addrinfo *results) if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_enable, sizeof(int)) < 0) return -2; - psocket->fd = fd; - psocket->addr = *rp; + psock->fd = fd; + psock->addr_len = rp->ai_addrlen; + psock->addr = *rp->ai_addr; freeaddrinfo(results); - return socket_nonblock(psocket); + return socket_nonblock(psock); } -int socket_listen_in(psocket *psocket) +int socket_listen_in(psocket *psock) { - assert(psocket); + assert(psock); - return listen(psocket->fd, POTD_BACKLOG); + return listen(psock->fd, POTD_BACKLOG); } -int socket_accept_in(const psocket *psocket, struct sockaddr_in *clientaddr) +int socket_accept_in(const psocket *psock, psocket *client_psock) { int fd; - socklen_t alen = {0}; - assert(psocket && clientaddr); + assert(psock && client_psock); - fd = accept(psocket->fd, (struct sockaddr*) clientaddr, &alen); + client_psock->addr_len = psock->addr_len; + fd = accept(psock->fd, &client_psock->addr, + &client_psock->addr_len); if (fd < 0) return -1; - if (socket_nonblock(psocket)) + if (socket_nonblock(psock)) return -2; + client_psock->fd = fd; - return fd; + return 0; } -int socket_addrtostr_in(const struct sockaddr_in *in_addr, +int socket_addrtostr_in(const psocket *psock, char hbuf[NI_MAXHOST], char sbuf[NI_MAXSERV]) { int s; - assert(in_addr); - s = getnameinfo((const struct sockaddr *) in_addr, - sizeof(struct sockaddr_in), + assert(psock); + s = getnameinfo(&psock->addr, + psock->addr_len, &hbuf[0], NI_MAXHOST, - &sbuf[0],NI_MAXSERV, + &sbuf[0], NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV); return s; } diff --git a/src/socket.h b/src/socket.h index c0ef15b..69c2690 100644 --- a/src/socket.h +++ b/src/socket.h @@ -7,20 +7,21 @@ typedef struct psocket { int fd; - struct addrinfo addr; + socklen_t addr_len; + struct sockaddr addr; } psocket; -int socket_init_in(psocket *psocket, const char *listen_addr, +int socket_init_in(const char *listen_addr, const char *listen_port, struct addrinfo **results); -int socket_bind_in(psocket *psocket, struct addrinfo *results); +int socket_bind_in(psocket *psock, struct addrinfo *results); -int socket_listen_in(psocket *psocket); +int socket_listen_in(psocket *psock); -int socket_accept_in(const psocket *psocket, struct sockaddr_in *clientaddr); +int socket_accept_in(const psocket *psock, psocket *client_psock); -int socket_addrtostr_in(const struct sockaddr_in *in_addr, +int socket_addrtostr_in(const psocket *psock, char hbuf[NI_MAXHOST], char sbuf[NI_MAXSERV]); #endif |