Skip to content

Commit

Permalink
ipc: socket connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 7, 2024
1 parent 701f8d0 commit 3d65710
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/include/kernel/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ typedef struct SocketDescriptor {
Process *process;
struct sockaddr address;
bool listener;
int type, protocol, backlog;
int type, protocol, backlogMax, backlogCount;
int inboundCount, outboundCount;
void **inbound, **outbound;
struct SocketDescriptor *peer;
struct SocketDescriptor **backlog; // for incoming connections via connect()
struct SocketDescriptor *peer; // for peer-to-peer connections
} SocketDescriptor;

void socketInit();
Expand Down
50 changes: 50 additions & 0 deletions src/ipc/connection.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* lux - a lightweight unix-like operating system
* Omar Elghoul, 2024
*
* Core Microkernel
*/

/* Socket Connection Functions */
/* connect(), listen(), and accept() are implemented here */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <platform/lock.h>
#include <kernel/logger.h>
#include <kernel/socket.h>
#include <kernel/io.h>
#include <kernel/sched.h>

/* connect(): creates a socket connection
* params: t - calling thread, NULL for kernel threads
* params: sd - socket descriptor
* params: addr - peer address
* params: len - length of peer address
* returns: zero on success, negative error code on fail
*/

int connect(Thread *t, int sd, const struct sockaddr *addr, socklen_t len) {
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;

SocketDescriptor *self = (SocketDescriptor *) p->io[sd].data;
SocketDescriptor *peer = getLocalSocket(addr, len);

if(!peer) return -EADDRNOTAVAIL;
if(!peer->listener || !peer->backlogMax || !peer->backlog) return -ECONNREFUSED;
if(peer->backlogCount >= peer->backlogMax) return -ETIMEDOUT;

// at this point we're sure it's safe to create a connection
socketLock();
peer->backlog[peer->backlogCount] = self;
peer->backlogCount++;
socketRelease();
return 0;
}

0 comments on commit 3d65710

Please sign in to comment.