Skip to content

Commit

Permalink
ipc: find socket by address
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 6, 2024
1 parent c606528 commit cdd2e1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/include/kernel/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ typedef struct {
} SocketDescriptor;

void socketInit();
SocketDescriptor *getLocalSocket(const struct sockaddr *, socklen_t);

/* socket system calls */
int socket(Thread *, int, int, int);
Expand Down
18 changes: 18 additions & 0 deletions src/ipc/sockinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ void socketInit() {
KDEBUG("max %d sockets, %d per process\n", MAX_SOCKETS, MAX_IO_DESCRIPTORS);
}

/* getLocalSocket(): finds a local socket by address
* params: addr - socket address
* params: len - length of socket address
* returns: pointer to socket descriptor, NULL on fail
*/

SocketDescriptor *getLocalSocket(const struct sockaddr *addr, socklen_t len) {
if(!socketCount) return NULL;
if(len > sizeof(struct sockaddr)) len = sizeof(struct sockaddr);
for(int i = 0; i < socketCount; i++) {
if((sockets[i]) && !memcmp(&sockets[i]->address, addr, len)) {
return sockets[i];
}
}

return NULL;
}

/* socket(): opens a communication socket
* params: t - calling thread, NULL for kernel threads
* params: domain - socket domain/family
Expand Down

0 comments on commit cdd2e1a

Please sign in to comment.