From b09a35307a48e2665c8c98e8a1c7c088b24f4c00 Mon Sep 17 00:00:00 2001 From: jewelcodes Date: Fri, 6 Sep 2024 20:50:56 -0400 Subject: [PATCH] ipc: socket listen() --- src/include/kernel/socket.h | 3 ++- src/ipc/connection.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/include/kernel/socket.h b/src/include/kernel/socket.h index 693fb7a..2cab57d 100644 --- a/src/include/kernel/socket.h +++ b/src/include/kernel/socket.h @@ -13,8 +13,9 @@ #include #include -/* system-wide limit */ +/* system-wide limits */ #define MAX_SOCKETS (1 << 18) // 262k +#define SOCKET_DEFAULT_BACKLOG 1024 // default socket backlog size /* socket family/domain - only Unix sockets will be implemented in the kernel */ #define AF_UNIX 1 diff --git a/src/ipc/connection.c b/src/ipc/connection.c index d15785f..4159534 100644 --- a/src/ipc/connection.c +++ b/src/ipc/connection.c @@ -48,3 +48,37 @@ int connect(Thread *t, int sd, const struct sockaddr *addr, socklen_t len) { socketRelease(); return 0; } + +/* listen(): listens for incoming connections on a socket + * params: t - calling thread, NULL for kernel threads + * params: sd - socket descriptor + * params: backlog - maximum number of queued connections, zero for default + * returns: zero on success, negative error code on fail + */ + +int listen(Thread *t, int sd, int backlog) { + Process *p; + if(t) p = getProcess(t->pid); + else p = getProcess(getPid()); + if(!p) return -ESRCH; + + if(!p->io[sd].valid || !p->io[sd].data || (p->io[sd].type != IO_SOCKET)) + return -ENOTSOCK; + + socketLock(); + SocketDescriptor *sock = (SocketDescriptor *) p->io[sd].data; + sock->backlogCount = 0; + + if(backlog > 0) sock->backlogMax = backlog; + else sock->backlogMax = SOCKET_DEFAULT_BACKLOG; + + sock->backlog = calloc(backlog, sizeof(SocketDescriptor *)); + if(!sock->backlog) { + socketRelease(); + return -ENOBUFS; + } + + sock->listener = true; + socketRelease(); + return 0; +}