Skip to content

Commit

Permalink
ipc: socket listen()
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 7, 2024
1 parent 3d65710 commit b09a353
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/include/kernel/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
#include <kernel/sched.h>
#include <sys/types.h>

/* 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
Expand Down
34 changes: 34 additions & 0 deletions src/ipc/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit b09a353

Please sign in to comment.