diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2018-04-16 17:25:09 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2018-04-16 17:25:09 +0200 |
commit | c9b3ea785346546948afcf685c80c460f3b1317a (patch) | |
tree | e5e43c24167f8e0a4d66e776ea71219e6e3d4adc /src/server.c | |
parent | 914f8f335d73c4dd69b72f6f2d8c53257bce497d (diff) |
POTD skeleton #10.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/server.c')
-rw-r--r-- | src/server.c | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/src/server.c b/src/server.c index 884ec5b..01ad6c9 100644 --- a/src/server.c +++ b/src/server.c @@ -24,24 +24,51 @@ client_mainloop_epoll(void *arg); static int client_io_epoll(struct epoll_event *ev); -int server_init_ctx(server_ctx **ctx, init_cb init_fn) +int server_init_ctx(server_ctx **ctx, forward_ctx *fwd_ctx) { - assert(ctx); + assert(ctx && fwd_ctx); if (!*ctx) *ctx = (server_ctx *) malloc(sizeof(**ctx)); - assert(*ctx || init_fn); + assert(*ctx); memset(*ctx, 0, sizeof(**ctx)); - if (!init_fn(*ctx)) + (*ctx)->fwd_ctx = fwd_ctx; + + return 0; +} + +int server_setup(server_ctx *ctx, + const char *listen_addr, const char *listen_port) +{ + int s; + struct addrinfo *srv_addr = NULL; + + assert(ctx); + assert(listen_addr || listen_port); + + D2("Try to listen on %s:%s", listen_addr, listen_port); + s = socket_init_in(listen_addr, listen_port, &srv_addr); + if (s) { + E_GAIERR(s, "Could not initialise server socket"); return 1; + } + if (socket_bind_in(&ctx->sock, srv_addr)) { + E_STRERR("Could not bind server socket"); + return 1; + } + if (socket_listen_in(&ctx->sock)) { + E_STRERR("Could not listen on server socket"); + return 1; + } return 0; } int server_validate_ctx(const server_ctx *ctx) { - assert(ctx); - assert(ctx->server_cbs.on_listen && ctx->server_cbs.on_shutdown); + assert(ctx && ctx->fwd_ctx); + assert(ctx->sock.fd >= 0 && ctx->sock.addr_len > 0); + return 0; } |