sock: pass protocol to sockets

This commit is contained in:
Matheus
2026-09-10 16:41:21 -03:00
parent 894d9bdaf9
commit 79752fb5e0
9 changed files with 36 additions and 26 deletions
+10 -2
View File
@@ -1,7 +1,15 @@
#ifndef _RAW_H #ifndef _RAW_H
#define _RAW_H #define _RAW_H
void raw_process(netdev_t *netdev, void *buffer, uint32_t peer); typedef struct socket socket_t;
socket_t *raw_create_socket(void);
typedef struct {
uint16_t netdev; // 1-based index that gets transformed into a minor number
uint16_t proto;
uint8_t mac[6];
} raw_addr_t;
void raw_process(netdev_t *netdev, void *buffer, mac_t *sender, uint16_t proto, size_t size);
socket_t *raw_create_socket(int protocol);
#endif #endif
+8 -7
View File
@@ -28,6 +28,7 @@ typedef struct {
netdev_t *netdev; netdev_t *netdev;
pollheader_t pollheader; pollheader_t pollheader;
int type; int type;
int protocol;
int shutdown; int shutdown;
int error; int error;
bool nonblocking; bool nonblocking;
@@ -363,13 +364,13 @@ static inline size_t sock_countctrl(sockctrl_t *ctrl, size_t len) {
} }
void localsock_leavebinding(vnode_t *vnode); void localsock_leavebinding(vnode_t *vnode);
int localsock_pair(socket_t **ret1, socket_t **ret2); int localsock_pair(socket_t **ret1, socket_t **ret2, int protocol);
int localsock_pair_seqpacket(socket_t **ret1, socket_t **ret2); int localsock_pair_seqpacket(socket_t **ret1, socket_t **ret2, int protocol);
socket_t *localsock_createsocket(void); socket_t *localsock_createsocket(int protocol);
socket_t *localsock_create_seqpacket_socket(void); socket_t *localsock_create_seqpacket_socket(int protocol);
socket_t *udp_createsocket(); socket_t *udp_createsocket(int protocol);
socket_t *tcp_createsocket(); socket_t *tcp_createsocket(int protocol);
socket_t *socket_create(int type); socket_t *socket_create(int type, int protocol);
int sockfs_newsocket(vnode_t **vnodep, socket_t *socket); int sockfs_newsocket(vnode_t **vnodep, socket_t *socket);
void sockfs_init(); void sockfs_init();
+8 -8
View File
@@ -1132,12 +1132,12 @@ void localsock_leavebinding(vnode_t *vnode) {
vnode->socketbinding = NULL; vnode->socketbinding = NULL;
} }
int localsock_pair(socket_t **ret1, socket_t **ret2) { int localsock_pair(socket_t **ret1, socket_t **ret2, int protocol) {
socket_t *socket1 = socket_create(SOCKET_TYPE_LOCAL); socket_t *socket1 = socket_create(SOCKET_TYPE_LOCAL, protocol);
if (socket1 == NULL) if (socket1 == NULL)
return ENOMEM; return ENOMEM;
socket_t *socket2 = socket_create(SOCKET_TYPE_LOCAL); socket_t *socket2 = socket_create(SOCKET_TYPE_LOCAL, protocol);
if (socket2 == NULL) { if (socket2 == NULL) {
localsock_destroy(socket1); localsock_destroy(socket1);
return ENOMEM; return ENOMEM;
@@ -1168,8 +1168,8 @@ int localsock_pair(socket_t **ret1, socket_t **ret2) {
return 0; return 0;
} }
int localsock_pair_seqpacket(socket_t **ret1, socket_t **ret2) { int localsock_pair_seqpacket(socket_t **ret1, socket_t **ret2, int protocol) {
int error = localsock_pair(ret1, ret2); int error = localsock_pair(ret1, ret2, protocol);
if (error) if (error)
return error; return error;
@@ -1421,7 +1421,7 @@ static socketops_t socketops = {
.setopt = localsock_setopt .setopt = localsock_setopt
}; };
socket_t *localsock_createsocket(void) { socket_t *localsock_createsocket(int protocol) {
// XXX possibly move this to a slab? // XXX possibly move this to a slab?
localsocket_t *socket = alloc(sizeof(localsocket_t)); localsocket_t *socket = alloc(sizeof(localsocket_t));
if (socket == NULL) if (socket == NULL)
@@ -1444,8 +1444,8 @@ socket_t *localsock_createsocket(void) {
return (socket_t *)socket; return (socket_t *)socket;
} }
socket_t *localsock_create_seqpacket_socket(void) { socket_t *localsock_create_seqpacket_socket(int protocol) {
localsocket_t *localsocket = (localsocket_t *)localsock_createsocket(); localsocket_t *localsocket = (localsocket_t *)localsock_createsocket(protocol);
if (localsocket == NULL) if (localsocket == NULL)
return NULL; return NULL;
+4 -3
View File
@@ -2,7 +2,7 @@
#include <kernel/raw.h> #include <kernel/raw.h>
#include <logging.h> #include <logging.h>
static socket_t *(*createsocket[])() = { static socket_t *(*createsocket[])(int) = {
udp_createsocket, udp_createsocket,
localsock_createsocket, localsock_createsocket,
tcp_createsocket, tcp_createsocket,
@@ -10,9 +10,9 @@ static socket_t *(*createsocket[])() = {
raw_create_socket raw_create_socket
}; };
socket_t *socket_create(int type) { socket_t *socket_create(int type, int protocol) {
__assert(type < sizeof(createsocket) / sizeof(createsocket[0])); __assert(type < sizeof(createsocket) / sizeof(createsocket[0]));
socket_t *socket = createsocket[type](); socket_t *socket = createsocket[type](protocol);
if (socket == NULL) if (socket == NULL)
return NULL; return NULL;
@@ -20,6 +20,7 @@ socket_t *socket_create(int type) {
socket->state = SOCKET_STATE_UNBOUND; socket->state = SOCKET_STATE_UNBOUND;
MUTEX_INIT(&socket->mutex); MUTEX_INIT(&socket->mutex);
socket->type = type; socket->type = type;
socket->protocol = protocol;
socket->nonblocking = false; socket->nonblocking = false;
return socket; return socket;
+1 -1
View File
@@ -1973,7 +1973,7 @@ static socketops_t socketops = {
.getopt = tcp_getopt .getopt = tcp_getopt
}; };
socket_t *tcp_createsocket() { socket_t *tcp_createsocket(int protocol) {
tcpsocket_t *socket = alloc(sizeof(tcpsocket_t)); tcpsocket_t *socket = alloc(sizeof(tcpsocket_t));
if (socket == NULL) if (socket == NULL)
return NULL; return NULL;
+1 -1
View File
@@ -382,7 +382,7 @@ static socketops_t socketops = {
.connect = udp_connect .connect = udp_connect
}; };
socket_t *udp_createsocket() { socket_t *udp_createsocket(int protocol) {
// XXX possibly move this to a slab? // XXX possibly move this to a slab?
udpsocket_t *socket = alloc(sizeof(udpsocket_t)); udpsocket_t *socket = alloc(sizeof(udpsocket_t));
if (socket == NULL) if (socket == NULL)
+1 -1
View File
@@ -28,7 +28,7 @@ syscallret_t syscall_accept(context_t *, int oldfd, abisockaddr_t *abisockaddr,
goto cleanup; goto cleanup;
} }
socket_t *client = socket_create(server->type); socket_t *client = socket_create(server->type, server->protocol);
if (client == NULL) { if (client == NULL) {
ret.errno = ENOMEM; ret.errno = ENOMEM;
goto cleanup; goto cleanup;
+1 -1
View File
@@ -43,7 +43,7 @@ syscallret_t syscall_socket(context_t *, int domain, int type, int protocol) {
return ret; return ret;
} }
socket_t *socket = socket_create(socktype); socket_t *socket = socket_create(socktype, protocol);
if (socket == NULL) { if (socket == NULL) {
ret.errno = ENOMEM; ret.errno = ENOMEM;
return ret; return ret;
+2 -2
View File
@@ -37,9 +37,9 @@ syscallret_t syscall_socketpair(context_t *, int domain, int type, int protocol)
// create pair // create pair
if (type == SOCK_SEQPACKET) if (type == SOCK_SEQPACKET)
ret.errno = localsock_pair_seqpacket(&sock1, &sock2); ret.errno = localsock_pair_seqpacket(&sock1, &sock2, protocol);
else else
ret.errno = localsock_pair(&sock1, &sock2); ret.errno = localsock_pair(&sock1, &sock2, protocol);
if (ret.errno) if (ret.errno)
return ret; return ret;