miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
net_socket.h
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2026 Christian Spoo
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#ifndef _MINIOS_NET_NET_SOCKET_H_
24#define _MINIOS_NET_NET_SOCKET_H_
25
26#include <stddef.h>
27#include <stdint.h>
28
29#define SOCK_PROTO_UDP 0
30#define SOCK_PROTO_TCP 1
31#define SOCK_PROTO_RAW 2
32
33#define NET_SOCK_RX_BUF_SIZE 1500
34#define NET_SOCK_POOL_SIZE 4
35#define NET_SOCK_RX_RING_SLOTS 2
36
49
69
73static inline int net_sock_has_data(const net_sock_t *s) {
74 return s && ((uint8_t)(s->rx_tail - s->rx_head) > 0);
75}
76
80static inline int net_sock_ring_full(const net_sock_t *s) {
81 return s && ((uint8_t)(s->rx_tail - s->rx_head) >= NET_SOCK_RX_RING_SLOTS);
82}
83
89net_sock_t *net_sock_alloc(int proto);
90
97void net_sock_free(net_sock_t *sock);
98
103int net_sock_recv(net_sock_t *sock, void *buf, size_t len, int flags);
104
109int net_sock_send(net_sock_t *sock, const void *buf, size_t len, int flags);
110
115int net_sock_close(net_sock_t *sock);
116
117#endif /* _MINIOS_NET_NET_SOCKET_H_ */
net_sock_t * net_sock_alloc(int proto)
Allocate a socket from the static pool.
Definition net_socket.c:49
struct net_sock_rx_entry net_sock_rx_entry_t
One slot in the socket rx ring.
#define NET_SOCK_RX_RING_SLOTS
Definition net_socket.h:35
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.
Definition net_socket.h:73
struct net_sock net_sock_t
Kernel socket state.
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 befor...
Definition net_socket.c:69
int net_sock_close(net_sock_t *sock)
Close socket: remove lwIP PCB and free pool slot.
Definition net_socket.c:162
static int net_sock_ring_full(const net_sock_t *s)
Returns non-zero when the socket rx ring is full (all slots occupied).
Definition net_socket.h:80
int net_sock_send(net_sock_t *sock, const void *buf, size_t len, int flags)
Send data through socket.
Definition net_socket.c:123
int net_sock_recv(net_sock_t *sock, void *buf, size_t len, int flags)
Receive data from socket into buf.
Definition net_socket.c:101
#define NET_SOCK_RX_BUF_SIZE
Definition net_socket.h:33
One slot in the socket rx ring.
Definition net_socket.h:42
uint32_t addr_be
Definition net_socket.h:45
uint16_t _pad
Definition net_socket.h:47
uint8_t buf[NET_SOCK_RX_BUF_SIZE]
Definition net_socket.h:43
size_t len
Definition net_socket.h:44
uint16_t port_be
Definition net_socket.h:46
Kernel socket state.
Definition net_socket.h:57
uint8_t rx_head
Definition net_socket.h:61
uint32_t rcvtimeo_ticks
Definition net_socket.h:64
uint32_t poll_waiter_tid
Definition net_socket.h:67
int in_use
Definition net_socket.h:66
void * pcb
Definition net_socket.h:59
uint8_t _pad2[2]
Definition net_socket.h:63
int connected
Definition net_socket.h:65
net_sock_rx_entry_t rx_ring[NET_SOCK_RX_RING_SLOTS]
Definition net_socket.h:60
uint8_t rx_tail
Definition net_socket.h:62
int proto
Definition net_socket.h:58
unsigned short uint16_t
Definition types.h:31
unsigned int uint32_t
Definition types.h:34
unsigned char uint8_t
Definition types.h:28
uint16_t flags
Definition virtio_net.c:2
uint32_t len
Definition virtio_net.c:1