miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
net_socket.h File Reference
#include <stddef.h>
#include <stdint.h>
Include dependency graph for net_socket.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  net_sock_rx_entry
 One slot in the socket rx ring. More...
struct  net_sock
 Kernel socket state. More...

Macros

#define SOCK_PROTO_UDP   0
#define SOCK_PROTO_TCP   1
#define SOCK_PROTO_RAW   2
#define NET_SOCK_RX_BUF_SIZE   1500
#define NET_SOCK_POOL_SIZE   4
#define NET_SOCK_RX_RING_SLOTS   2

Typedefs

typedef struct net_sock_rx_entry net_sock_rx_entry_t
 One slot in the socket rx ring.
typedef struct net_sock net_sock_t
 Kernel socket state.

Functions

static int net_sock_has_data (const net_sock_t *s)
 Returns non-zero when the socket rx ring has at least one entry ready.
static int net_sock_ring_full (const net_sock_t *s)
 Returns non-zero when the socket rx ring is full (all slots occupied).
net_sock_tnet_sock_alloc (int proto)
 Allocate a socket from the static pool.
void net_sock_free (net_sock_t *sock)
 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).
int net_sock_recv (net_sock_t *sock, void *buf, size_t len, int flags)
 Receive data from socket into buf.
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: remove lwIP PCB and free pool slot.

Macro Definition Documentation

◆ NET_SOCK_POOL_SIZE

#define NET_SOCK_POOL_SIZE   4

Max concurrent sockets (matches MEMP_NUM_*_PCB)

◆ NET_SOCK_RX_BUF_SIZE

#define NET_SOCK_RX_BUF_SIZE   1500

◆ NET_SOCK_RX_RING_SLOTS

#define NET_SOCK_RX_RING_SLOTS   2

Slots in the per-socket RAW rx ring

◆ SOCK_PROTO_RAW

#define SOCK_PROTO_RAW   2

Raw IP protocol selector for net_sock_alloc()

◆ SOCK_PROTO_TCP

#define SOCK_PROTO_TCP   1

TCP protocol selector for net_sock_alloc()

◆ SOCK_PROTO_UDP

#define SOCK_PROTO_UDP   0

UDP protocol selector for net_sock_alloc()

Typedef Documentation

◆ net_sock_rx_entry_t

One slot in the socket rx ring.

Used for RAW sockets (2 slots) and by UDP/TCP (single-slot at index 0).

◆ net_sock_t

typedef struct net_sock net_sock_t

Kernel socket state.

One net_sock_t per open socket fd. Owns the lwIP PCB pointer and a 2-slot receive ring for RAW sockets (UDP/TCP use slot 0 only). No dynamic allocation beyond the static pool.

Function Documentation

◆ net_sock_alloc()

net_sock_t * net_sock_alloc ( int proto)

Allocate a socket from the static pool.

Parameters
protoSOCK_PROTO_UDP, SOCK_PROTO_TCP, or SOCK_PROTO_RAW
Returns
Pointer to net_sock_t, or NULL if pool exhausted.

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: remove lwIP PCB and free pool slot.

Returns
0 on success.

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)

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).

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_has_data()

int net_sock_has_data ( const net_sock_t * s)
inlinestatic

Returns non-zero when the socket rx ring has at least one entry ready.

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 into buf.

Returns
Bytes received, -1 if no data ready or PCB not set.

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_ring_full()

int net_sock_ring_full ( const net_sock_t * s)
inlinestatic

Returns non-zero when the socket rx ring is full (all slots occupied).

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
Bytes sent, -1 on error (PCB not set, or lwIP error).

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: