aboutsummaryrefslogtreecommitdiff
path: root/server.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-10-06 21:03:59 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-10-06 21:03:59 +0200
commitdb9cb051c4ee05c07ab32dfed5bae8b7dc0916bd (patch)
tree44e1efae86af612923ec5486c00eb2c56019c865 /server.c
parent4918cf87d3cf5d3dd8425fe10d97a06282472df6 (diff)
Allow custom backlog connections
libweb calls listen(2) when setting up the HTTP server, and its backlog argument was hardcoded to 10. While probably not an issue for some applications, it can be too limiting for some others. Therefore, it is desirable to allow library users to set up their own limits. Otherwise, 10 is still chosen as a sane default.
Diffstat (limited to 'server.c')
-rw-r--r--server.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/server.c b/server.c
index a77d7d2..3f7eb04 100644
--- a/server.c
+++ b/server.c
@@ -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;