Make socket port binding a separate step with ipc_netsockbindport() syscall
This commit is contained in:
@ -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;
|
||||
|
||||
|
||||
@ -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_
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -5,9 +5,10 @@
|
||||
#include <stdint.h>
|
||||
#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_
|
||||
|
||||
@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user