From d947192475d7910be1779a379c8a7434da1afdec Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Wed, 29 Oct 2025 22:33:41 +0100 Subject: [PATCH] Make socket port binding a separate step with ipc_netsockbindport() syscall --- kernel/ipc/netsock/netsock.c | 18 +++++++++++++----- kernel/ipc/netsock/netsock.h | 3 ++- kernel/syscall/ipcnetsock.c | 26 +++++++++++++++++++++++--- kernel/syscall/ipcnetsock.h | 3 ++- kernel/syscall/syscall.c | 1 + share/errors.h | 1 + share/sysdefs/syscall.h | 1 + ulib/system/system.c | 8 ++++++-- ulib/system/system.h | 3 ++- user/diagdummy/tcptest.c | 5 +++-- 10 files changed, 54 insertions(+), 15 deletions(-) diff --git a/kernel/ipc/netsock/netsock.c b/kernel/ipc/netsock/netsock.c index 8024840..684da47 100644 --- a/kernel/ipc/netsock/netsock.c +++ b/kernel/ipc/netsock/netsock.c @@ -72,7 +72,7 @@ void ipc_netsock_event(uint16_t ev, struct pico_socket *sock1) { spinlock_release(&netsock->spinlock); } -IpcNetSock *ipc_netsockmake(uint16_t net, uint16_t proto, uint16_t port, uint64_t pid) { +IpcNetSock *ipc_netsockmake(uint16_t net, uint16_t proto, uint64_t pid) { IpcNetSock *netsock = dlmalloc(sizeof(*netsock)); if (netsock == NULL) { return NULL; @@ -92,10 +92,6 @@ IpcNetSock *ipc_netsockmake(uint16_t net, uint16_t proto, uint16_t port, uint64_ netsock->ownerpid = pid; - uint16_t port_be = short_be(port); - struct pico_ip4 inaddr_any = {0}; - pico_socket_bind(netsock->picosock, &inaddr_any, &port_be); - spinlock_acquire(&IPC_NETSOCKS.spinlock); LL_APPEND(IPC_NETSOCKS.netsocks, netsock); spinlock_release(&IPC_NETSOCKS.spinlock); @@ -144,6 +140,18 @@ int32_t ipc_netsockdelete(IpcNetSock *netsock) { return E_OK; } +int32_t ipc_netsockbindport(IpcNetSock *netsock, uint16_t port) { + uint16_t port_be = short_be(port); + struct pico_ip4 inaddr_any = {0}; + spinlock_acquire(&netsock->spinlock); + int32_t r = pico_socket_bind(netsock->picosock, &inaddr_any, &port_be); + spinlock_release(&netsock->spinlock); + if (r != 0) { + return E_NETSOCKBIND; + } + return E_OK; +} + void ipc_netsock_cleanup_dangling(void) { IpcNetSock *ns, *nstmp; diff --git a/kernel/ipc/netsock/netsock.h b/kernel/ipc/netsock/netsock.h index efb701e..668758b 100644 --- a/kernel/ipc/netsock/netsock.h +++ b/kernel/ipc/netsock/netsock.h @@ -27,9 +27,10 @@ typedef struct { extern IpcNetSocks IPC_NETSOCKS; void ipc_netsockinit(void); -IpcNetSock *ipc_netsockmake(uint16_t net, uint16_t proto, uint16_t port, uint64_t pid); +IpcNetSock *ipc_netsockmake(uint16_t net, uint16_t proto, uint64_t pid); int32_t ipc_netsocklisten(IpcNetSock *netsock, size_t maxlisteners); int32_t ipc_netsockdelete(IpcNetSock *netsock); +int32_t ipc_netsockbindport(IpcNetSock *netsock, uint16_t port); void ipc_netsock_cleanup_dangling(void); #endif // NETSOCK_NETSOCK_H_ diff --git a/kernel/syscall/ipcnetsock.c b/kernel/syscall/ipcnetsock.c index e1d6850..7b00998 100644 --- a/kernel/syscall/ipcnetsock.c +++ b/kernel/syscall/ipcnetsock.c @@ -6,12 +6,11 @@ #include "util/util.h" #include "errors.h" -int32_t SYSCALL3(sys_ipc_netsockmake, net1, proto1, port1) { +int32_t SYSCALL2(sys_ipc_netsockmake, net1, proto1) { uint16_t net = net1; uint16_t proto = proto1; - uint16_t port = port1; - IpcNetSock *netsock = ipc_netsockmake(net, proto, port, _caller_pid); + IpcNetSock *netsock = ipc_netsockmake(net, proto, _caller_pid); if (netsock == NULL) { return E_NOMEMORY; @@ -102,3 +101,24 @@ int32_t SYSCALL1(sys_ipc_netsockdelete, socknum1) { return ipc_netsockdelete(ns); } + +int32_t SYSCALL2(sys_ipc_netsockbindport, socknum1, port1) { + size_t socknum = socknum1; + uint16_t port = port1; + + spinlock_acquire(&IPC_NETSOCKS.spinlock); + size_t idx = 0; + IpcNetSock *ns, *nstmp; + LL_FOREACH_SAFE_IDX(IPC_NETSOCKS.netsocks, ns, nstmp, idx) { + if (idx == socknum) { + break; + } + } + spinlock_release(&IPC_NETSOCKS.spinlock); + + if (ns == NULL) { + return E_NOENTRY; + } + + return ipc_netsockbindport(ns, port); +} diff --git a/kernel/syscall/ipcnetsock.h b/kernel/syscall/ipcnetsock.h index ed8239b..189b849 100644 --- a/kernel/syscall/ipcnetsock.h +++ b/kernel/syscall/ipcnetsock.h @@ -5,9 +5,10 @@ #include #include "syscall.h" -int32_t SYSCALL3(sys_ipc_netsockmake, net1, proto1, port1); +int32_t SYSCALL2(sys_ipc_netsockmake, net1, proto1); int32_t SYSCALL2(sys_ipc_netsocklisten, socknum1, maxlisteners1); int32_t SYSCALL1(sys_ipc_netsockpollev, socknum1); int32_t SYSCALL1(sys_ipc_netsockdelete, socknum1); +int32_t SYSCALL2(sys_ipc_netsockbindport, socknum1, port1); #endif // SYSCALL_IPC_NETSOCK_H_ diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 269ede4..158f5e1 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -69,4 +69,5 @@ SyscallFn SYSCALL_TABLE[SYSCALLS_MAX] = { [SYS_IPC_NETSOCKLISTEN] = &sys_ipc_netsocklisten, [SYS_IPC_NETSOCKPOLLEV] = &sys_ipc_netsockpollev, [SYS_IPC_NETSOCKDELETE] = &sys_ipc_netsockdelete, + [SYS_IPC_NETSOCKBINDPORT] = &sys_ipc_netsockbindport, }; diff --git a/share/errors.h b/share/errors.h index 7523899..588ceb5 100644 --- a/share/errors.h +++ b/share/errors.h @@ -17,6 +17,7 @@ #define E_SPAWNERROR -13 #define E_NOTYET -14 #define E_NETSOCKLISTEN -15 +#define E_NETSOCKBIND -16 #if !defined(__ASSEMBLER__) diff --git a/share/sysdefs/syscall.h b/share/sysdefs/syscall.h index 169f341..064e247 100644 --- a/share/sysdefs/syscall.h +++ b/share/sysdefs/syscall.h @@ -39,5 +39,6 @@ #define SYS_IPC_NETSOCKLISTEN 40 #define SYS_IPC_NETSOCKPOLLEV 41 #define SYS_IPC_NETSOCKDELETE 42 +#define SYS_IPC_NETSOCKBINDPORT 43 #endif // SHARE_HDRS_SYSCALL_H_ diff --git a/ulib/system/system.c b/ulib/system/system.c index f680c88..ad5c154 100644 --- a/ulib/system/system.c +++ b/ulib/system/system.c @@ -143,8 +143,8 @@ int32_t time(Time *time) { return syscall(SYS_TIME, (uint64_t)time, 0, 0, 0, 0, 0); } -int32_t ipc_netsockmake(uint16_t net, uint16_t proto, uint16_t port) { - return syscall(SYS_IPC_NETSOCKMAKE, (uint64_t)net, (uint64_t)proto, (uint64_t)port, 0, 0, 0); +int32_t ipc_netsockmake(uint16_t net, uint16_t proto) { + return syscall(SYS_IPC_NETSOCKMAKE, (uint64_t)net, (uint64_t)proto, 0, 0, 0, 0); } int32_t ipc_netsocklisten(uint64_t netsock, size_t maxlisteners) { @@ -158,3 +158,7 @@ int32_t ipc_netsockpollev(uint64_t netsock) { int32_t ipc_netsockdelete(uint64_t netsock) { return syscall(SYS_IPC_NETSOCKDELETE, (uint64_t)netsock, 0, 0, 0, 0, 0); } + +int32_t ipc_netsockbindport(uint64_t netsock, uint16_t port) { + return syscall(SYS_IPC_NETSOCKBINDPORT, (uint64_t)netsock, (uint64_t)port, 0, 0, 0, 0); +} diff --git a/ulib/system/system.h b/ulib/system/system.h index 0664dde..3d09850 100644 --- a/ulib/system/system.h +++ b/ulib/system/system.h @@ -44,9 +44,10 @@ int32_t dev_listsize(void); int32_t dev_stat(DevStat *devstatbuf, size_t idx); int32_t dev_cmd(Dev_t *dev, uint64_t cmd, void *buf, size_t len); int32_t time(Time *time); -int32_t ipc_netsockmake(uint16_t net, uint16_t proto, uint16_t port); +int32_t ipc_netsockmake(uint16_t net, uint16_t proto); int32_t ipc_netsocklisten(uint64_t netsock, size_t maxlisteners); int32_t ipc_netsockpollev(uint64_t netsock); int32_t ipc_netsockdelete(uint64_t netsock); +int32_t ipc_netsockbindport(uint64_t netsock, uint16_t port); #endif // ULIB_SYSTEM_SYSTEM_H_ diff --git a/user/diagdummy/tcptest.c b/user/diagdummy/tcptest.c index e2674b7..05f1979 100644 --- a/user/diagdummy/tcptest.c +++ b/user/diagdummy/tcptest.c @@ -5,7 +5,8 @@ #define MAX_CONNS 10 void diagdummy_tcptestserver(void) { - netsock_t netsock = ipc_netsockmake(NETSOCK_IPV4, NETSOCK_TCP, 1); + netsock_t netsock = ipc_netsockmake(NETSOCK_IPV4, NETSOCK_TCP); + ipc_netsockbindport(netsock, 1); ipc_netsocklisten(netsock, MAX_CONNS); for (;;) { @@ -21,5 +22,5 @@ void diagdummy_tcptestserver(void) { } void diagdummy_tcptestclient(void) { - + /* netsock_t netsock = ipc_netsockmake(NETSOCK_IPV4, NETSOCK_TCP, ); */ }