PatchworkOS
621ae6b
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
netfs.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <
kernel/fs/path.h
>
4
#include <
kernel/sched/wait.h
>
5
#include <
kernel/sync/mutex.h
>
6
#include <
kernel/sync/rwmutex.h
>
7
#include <
kernel/utils/ref.h
>
8
9
#include <
stdint.h
>
10
#include <
sys/io.h
>
11
#include <
sys/list.h
>
12
13
typedef
struct
netfs_family
netfs_family_t
;
14
15
/**
16
* @brief Networking and Sockets.
17
* @defgroup kernel_fs_netfs Networking Filesystem
18
* @ingroup kernel_fs
19
*
20
* The networking filesystem provides networking and socket IPC functionality to the operating system. It exposes a
21
* common interface for various networking protocols and inter-process communication (IPC) mechanisms.
22
*
23
* ## Network Families
24
*
25
* Network families represent different networking protocols or IPC mechanisms. Each family has its own directory in the
26
* filesystem, named after the family.
27
*
28
* Each family directory contains factory files for creating sockets of different types, including `stream`, `dgram`,
29
* `seqpacket`, `raw`, and `rdm`.
30
*
31
* Additionally, there is an `addrs` file that lists the addresses of all listening sockets within that family in the
32
* format:
33
*
34
* ```
35
* <address>\n<address>\n...
36
* ```
37
*
38
* ## Sockets
39
*
40
* Sockets are created by opening a factory file, named after the socket type it will create, located in each socket
41
* family's directory. Once a socket is created, it will persist until the namespace that created it is destroyed and
42
* there are no more references to it.
43
*
44
* For example, to create a local seqpacket socket, open the `/local/seqpacket` file. This returns a handle that when
45
* read returns the socket's ID, which corresponds to the path `/<family_name>/<socket_id>/`, for example
46
* `/local/1234/`, which stores the files used to interact with the socket.
47
*
48
* The socket directory will only be visible in the namespace that created it.
49
*
50
* The files used to interact with sockets are listed below.
51
*
52
* ### accept
53
*
54
* The `/<family_name>/<socket_id>/accept` file can be opened on a listening socket to accept incoming connections.
55
* Working in an similar way to the POSIX `accept()` function, the returned file descriptor represents the new
56
* connection.
57
*
58
* If opened with `:nonblock` and there are no incoming connections, the open will fail with `EAGAIN`, otherwise it will
59
* block until a connection is available.
60
*
61
* ### ctl
62
*
63
* The `/<family_name>/<socket_id>/ctl` file is used to send "commands" to the socket by writing to it. Here is a
64
* list of supported commands:
65
* - `bind <address>`: Binds the socket to the specified address. (POSIX `bind()` function)
66
* - `listen <backlog>`: Puts the socket into listening mode with the specified backlog length. (POSIX `listen()`
67
* function)
68
* - `connect <address>`: Connects the socket to the specified address. (POSIX `connect()` function)
69
*
70
* ### data
71
*
72
* The `/<family_name>/<socket_id>/data` file is used to send and receive data using the socket. Writing to this
73
* file sends data, reading from it receives data. (POSIX `send()` and `recv()` functions)
74
*
75
* If opened with `:nonblock`, read and write operations will fail with `EAGAIN` if no data is available or there is no
76
* buffer space available, respectively. If not opened with `:nonblock` they will block, waiting for data or buffer
77
* space.
78
*
79
* @{
80
*/
81
82
/**
83
* @brief The name of the networking filesystem.
84
*/
85
#define NETFS_NAME "netfs"
86
87
/**
88
* @brief The default backlog size for listening sockets.
89
*/
90
#define NETFS_BACKLOG_DEFAULT 128
91
92
/**
93
* @brief Socket types.
94
* @enum socket_type_t
95
*/
96
typedef
enum
97
{
98
SOCKET_STREAM
= 1 << 0,
///< A sequenced, reliable, two-way connection-based byte stream.
99
SOCKET_DGRAM
= 1 << 1,
///< A connectionless, unreliable datagram service.
100
SOCKET_SEQPACKET
= 1 << 2,
///< A sequenced, reliable, two-way connection-based packet stream.
101
SOCKET_RAW
= 1 << 3,
///< Provides raw network protocol access.
102
SOCKET_RDM
= 1 << 4,
///< A reliable datagram layer that does not guarantee ordering.
103
SOCKET_TYPE_AMOUNT
= 5,
104
}
socket_type_t
;
105
106
/**
107
* @brief Socket states.
108
* @enum socket_state_t
109
*/
110
typedef
enum
111
{
112
SOCKET_NEW
,
113
SOCKET_BOUND
,
114
SOCKET_LISTENING
,
115
SOCKET_CONNECTING
,
116
SOCKET_CONNECTED
,
117
SOCKET_CLOSING
,
118
SOCKET_CLOSED
119
}
socket_state_t
;
120
121
/**
122
* @brief Socket structure.
123
* @struct socket_t
124
*/
125
typedef
struct
socket
126
{
127
ref_t
ref
;
128
list_entry_t
listEntry
;
129
char
id
[
MAX_NAME
];
130
char
address
[
MAX_PATH
];
131
netfs_family_t
*
family
;
132
socket_type_t
type
;
133
socket_state_t
state
;
134
weak_ptr_t
ownerNs
;
///< A weak pointer to the namespace that created the socket.
135
void
*
private
;
136
mutex_t
mutex
;
137
}
socket_t
;
138
139
/**
140
* @brief Socket Family structure.
141
* @struct netfs_family_t
142
*/
143
typedef
struct
netfs_family
144
{
145
const
char
*
name
;
146
/**
147
* @brief Initialize a socket.
148
*
149
* @param sock Pointer to the socket to initialize.
150
* @return On success, `0`. On failure, `ERR` and `errno` is set.
151
*/
152
uint64_t
(*init)(
socket_t
* sock);
153
/**
154
* @brief Deinitialize a socket.
155
*
156
* @param sock Pointer to the socket to deinitialize.
157
*/
158
void (*deinit)(
socket_t
* sock);
159
/**
160
* @brief Bind a socket to its address.
161
*
162
* The address is stored in `socket_t::address`.
163
*
164
* @param sock Pointer to the socket to bind.
165
* @return On success, `0`. On failure, `ERR` and `errno` is set.
166
*/
167
uint64_t
(*
bind
)(
socket_t
* sock);
168
/**
169
* @brief Listen for incoming connections on a socket.
170
*
171
* @param sock Pointer to the socket to listen on.
172
* @param backlog Maximum number of pending connections.
173
* @return On success, `0`. On failure, `ERR` and `errno` is set.
174
*/
175
uint64_t
(*listen)(
socket_t
* sock,
uint32_t
backlog);
176
/**
177
* @brief Connect a socket to its address.
178
*
179
* The address is stored in `socket_t::address`.
180
*
181
* @param sock Pointer to the socket to connect.
182
* @return On success, `0`. On failure, `ERR` and `errno` is set.
183
*/
184
uint64_t
(*connect)(
socket_t
* sock);
185
/**
186
* @brief Accept an incoming connection on a listening socket.
187
*
188
* @param sock Pointer to the listening socket.
189
* @param newSock Pointer to the socket to initialize for the new connection.
190
* @param mode Mode flags for the new socket.
191
* @return On success, `0`. On failure, `ERR` and `errno` is set.
192
*/
193
uint64_t
(*accept)(
socket_t
* sock,
socket_t
* newSock,
mode_t
mode);
194
/**
195
* @brief Send data on a socket.
196
*
197
* @param sock Pointer to the socket to send data on.
198
* @param buffer Pointer to the data to send.
199
* @param count Number of bytes to send.
200
* @param offset Pointer to the position in the file, families may ignore this.
201
* @param mode Mode flags for sending.
202
* @return On success, number of bytes sent. On failure, `ERR` and `errno` is set.
203
*/
204
size_t
(*send)(
socket_t
* sock,
const
void
*
buffer
,
size_t
count
,
size_t
*
offset
,
mode_t
mode);
205
/**
206
* @brief Receive data on a socket.
207
*
208
* @param sock Pointer to the socket to receive data on.
209
* @param buffer Pointer to the buffer to store received data.
210
* @param count Maximum number of bytes to receive.
211
* @param offset Pointer to the position in the file, families may ignore this.
212
* @param mode Mode flags for receiving.
213
* @return On success, number of bytes received. On failure, `ERR` and `errno` is set.
214
*/
215
size_t
(*recv)(
socket_t
* sock,
void
*
buffer
,
size_t
count
,
size_t
*
offset
,
mode_t
mode);
216
/**
217
* @brief Poll a socket for events.
218
*
219
* @param sock Pointer to the socket to poll.
220
* @param revents Pointer to store the events that occurred.
221
* @return On success, a pointer to the wait queue to block on. On failure, `NULL` and `errno` is set.
222
*/
223
wait_queue_t
* (*poll)(
socket_t
* sock,
poll_events_t
* revents);
224
list_entry_t
listEntry
;
225
list_t
sockets
;
226
rwmutex_t
mutex
;
227
}
netfs_family_t
;
228
229
/**
230
* @brief Initialize the networking filesystem.
231
*/
232
void
netfs_init
(
void
);
233
234
/**
235
* @brief Register a network family.
236
*
237
* @param family Pointer to the network family structure.
238
* @return On success, `0`. On failure, `ERR` and `errno` is set.
239
*/
240
uint64_t
netfs_family_register
(
netfs_family_t
* family);
241
242
/**
243
* @brief Unregister a network family.
244
*
245
* @param family Pointer to the network family structure.
246
*/
247
void
netfs_family_unregister
(
netfs_family_t
* family);
248
249
/** @} */
MAX_NAME
#define MAX_NAME
Maximum length of names.
Definition
MAX_NAME.h:11
MAX_PATH
#define MAX_PATH
Maximum length of filepaths.
Definition
MAX_PATH.h:11
socket_state_t
socket_state_t
Socket states.
Definition
netfs.h:111
socket_type_t
socket_type_t
Socket types.
Definition
netfs.h:97
netfs_family_register
uint64_t netfs_family_register(netfs_family_t *family)
Register a network family.
Definition
netfs.c:813
netfs_init
void netfs_init(void)
Initialize the networking filesystem.
Definition
netfs.c:805
netfs_family_unregister
void netfs_family_unregister(netfs_family_t *family)
Unregister a network family.
Definition
netfs.c:832
SOCKET_CLOSED
@ SOCKET_CLOSED
Definition
netfs.h:118
SOCKET_LISTENING
@ SOCKET_LISTENING
Definition
netfs.h:114
SOCKET_CONNECTED
@ SOCKET_CONNECTED
Definition
netfs.h:116
SOCKET_NEW
@ SOCKET_NEW
Definition
netfs.h:112
SOCKET_CLOSING
@ SOCKET_CLOSING
Definition
netfs.h:117
SOCKET_BOUND
@ SOCKET_BOUND
Definition
netfs.h:113
SOCKET_CONNECTING
@ SOCKET_CONNECTING
Definition
netfs.h:115
SOCKET_RDM
@ SOCKET_RDM
A reliable datagram layer that does not guarantee ordering.
Definition
netfs.h:102
SOCKET_STREAM
@ SOCKET_STREAM
A sequenced, reliable, two-way connection-based byte stream.
Definition
netfs.h:98
SOCKET_SEQPACKET
@ SOCKET_SEQPACKET
A sequenced, reliable, two-way connection-based packet stream.
Definition
netfs.h:100
SOCKET_DGRAM
@ SOCKET_DGRAM
A connectionless, unreliable datagram service.
Definition
netfs.h:99
SOCKET_TYPE_AMOUNT
@ SOCKET_TYPE_AMOUNT
Definition
netfs.h:103
SOCKET_RAW
@ SOCKET_RAW
Provides raw network protocol access.
Definition
netfs.h:101
mode_t
mode_t
Path flags and permissions.
Definition
path.h:78
bind
uint64_t bind(const char *mountpoint, fd_t source)
System call for binding a file descriptor to a mountpoint.
Definition
bind.c:5
poll_events_t
poll_events_t
Poll events type.
Definition
io.h:285
address
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition
hpet.c:95
offset
static uint64_t offset
Definition
screen.c:19
mutex.h
io.h
list.h
buffer
EFI_PHYSICAL_ADDRESS buffer
Definition
mem.c:15
path.h
count
static atomic_long count
Definition
main.c:11
ref.h
rwmutex.h
size_t
__SIZE_TYPE__ size_t
Definition
size_t.h:4
stdint.h
uint32_t
__UINT32_TYPE__ uint32_t
Definition
stdint.h:15
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
list_entry_t
A entry in a doubly linked list.
Definition
list.h:35
list_t
A doubly linked list.
Definition
list.h:47
mutex_t
Mutex structure.
Definition
mutex.h:41
netfs_family_t
Socket Family structure.
Definition
netfs.h:144
netfs_family_t::listEntry
list_entry_t listEntry
Definition
netfs.h:224
netfs_family_t::mutex
rwmutex_t mutex
Definition
netfs.h:226
netfs_family_t::name
const char * name
Definition
netfs.h:145
netfs_family_t::sockets
list_t sockets
Definition
netfs.h:225
ref_t
Reference counting structure.
Definition
ref.h:52
rwmutex_t
Read-Write Mutex structure.
Definition
rwmutex.h:42
socket_t
Socket structure.
Definition
netfs.h:126
socket_t::family
netfs_family_t * family
Definition
netfs.h:131
socket_t::ref
ref_t ref
Definition
netfs.h:127
socket_t::type
socket_type_t type
Definition
netfs.h:132
socket_t::state
socket_state_t state
Definition
netfs.h:133
socket_t::listEntry
list_entry_t listEntry
Definition
netfs.h:128
socket_t::ownerNs
weak_ptr_t ownerNs
A weak pointer to the namespace that created the socket.
Definition
netfs.h:134
socket_t::mutex
mutex_t mutex
Definition
netfs.h:136
wait_queue_t
The primitive that threads block on.
Definition
wait.h:182
weak_ptr_t
Weak pointer structure.
Definition
ref.h:35
wait.h
include
kernel
fs
netfs.h
Generated on Thu Jan 8 2026 08:21:24 for PatchworkOS by
1.9.8