diff options
Diffstat (limited to 'server.c')
| -rw-r--r-- | server.c | 16 |
1 files changed, 12 insertions, 4 deletions
@@ -5,6 +5,7 @@ #include <poll.h> #include <unistd.h> #include <errno.h> +#include <limits.h> #include <stdbool.h> #include <stddef.h> #include <stdio.h> @@ -299,10 +300,13 @@ end: } struct server *server_init(const unsigned short port, - unsigned short *const outport) + unsigned short *const outport, const unsigned ubacklog) { struct server *const s = malloc(sizeof *s); int fds[2] = {-1, -1}, fd = -1; + /* Ensure default value if not set. */ + enum {QUEUE_LEN = 10}; + const unsigned backlog = ubacklog ? ubacklog : QUEUE_LEN; if (!s) { @@ -326,14 +330,18 @@ struct server *server_init(const unsigned short port, .sin_port = htons(port) }; - enum {QUEUE_LEN = 10}; - if (bind(fd, (const struct sockaddr *)&addr, sizeof addr)) { fprintf(stderr, "%s: bind(2): %s\n", __func__, strerror(errno)); goto failure; } - else if (listen(fd, QUEUE_LEN)) + else if (backlog > INT_MAX) + { + fprintf(stderr, "%s: backlog (%u) exceeds maximum (%d)\n", + __func__, backlog, INT_MAX); + goto failure; + } + else if (listen(fd, backlog)) { fprintf(stderr, "%s: listen(2): %s\n", __func__, strerror(errno)); goto failure; |
