-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
701f8d0
commit 3d65710
Showing
2 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |