miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
net_socket.c File Reference

Kernel socket state layer for miniOS. More...

#include <miniOS/net/net_socket.h>
#include <miniOS/sched/sched.h>
#include <miniOS/net/lwip_netif.h>
#include <string.h>
#include "lwip/udp.h"
#include "lwip/tcp.h"
#include "lwip/raw.h"
#include "lwip/pbuf.h"
Include dependency graph for net_socket.c:

Functions

net_sock_tnet_sock_alloc (int proto)
 Allocate a socket from the static pool.
void net_sock_free (net_sock_t *sock)
 Release a socket slot back to the pool.
int net_sock_recv (net_sock_t *sock, void *buf, size_t len, int flags)
 Receive data from socket ring head (non-blocking).
int net_sock_send (net_sock_t *sock, const void *buf, size_t len, int flags)
 Send data through socket.
int net_sock_close (net_sock_t *sock)
 Close socket: free pool slot (including lwIP PCB teardown).
void net_poll_thread_fn (void *arg)
 Kernel thread function that drives lwIP polling.

Variables

static net_sock_t g_sock_pool [NET_SOCK_POOL_SIZE]

Detailed Description

Kernel socket state layer for miniOS.

Provides a static pool of net_sock_t structs. The lwIP PCB pointer (pcb field) is set by the syscall layer when bind/connect is called. Until then, send/recv return -1.

Function Documentation

◆ net_poll_thread_fn()

void net_poll_thread_fn ( void * arg)

Kernel thread function that drives lwIP polling.

net_poll_thread_fn() - Dedicated kernel thread that drives lwIP polling.

Runs continuously, calling lwip_netif_poll() so that network receive callbacks fire even when userspace threads are sleeping in poll/select. Receive callbacks call sched_wake_tid() to unblock waiting threads.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ net_sock_alloc()

net_sock_t * net_sock_alloc ( int proto)

Allocate a socket from the static pool.

Scans the pool for a free slot, zeroes it, and sets proto and in_use. Returns NULL if all NET_SOCK_POOL_SIZE slots are occupied.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ net_sock_close()

int net_sock_close ( net_sock_t * sock)

Close socket: free pool slot (including lwIP PCB teardown).

Close socket: remove lwIP PCB and free pool slot.

Returns
0 on success.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ net_sock_free()

void net_sock_free ( net_sock_t * sock)

Release a socket slot back to the pool.

Free a socket slot back to the pool. Calls raw_remove/udp_remove/tcp_abort based on sock->proto before freeing the slot; sets sock->pcb=NULL before clearing in_use (UAF safety — prevents recv callbacks from firing on a freed slot).

D-04: call lwIP teardown BEFORE nulling pcb, so the raw/udp/tcp layer removes the pcb from its internal list. Then null pcb BEFORE clearing in_use so any recv callback that races cannot reach a freed slot.

Here is the caller graph for this function:

◆ net_sock_recv()

int net_sock_recv ( net_sock_t * sock,
void * buf,
size_t len,
int flags )

Receive data from socket ring head (non-blocking).

Receive data from socket into buf.

Returns -1 immediately when PCB is NULL (socket not connected) or when the rx ring is empty. Copies min(slot->len, len) bytes on success and advances rx_head.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ net_sock_send()

int net_sock_send ( net_sock_t * sock,
const void * buf,
size_t len,
int flags )

Send data through socket.

Returns -1 when PCB is NULL (socket not connected). Actual lwIP send (udp_send/tcp_write) is implemented here for each protocol type.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ g_sock_pool

net_sock_t g_sock_pool[NET_SOCK_POOL_SIZE]
static