PatchworkOS  621ae6b
A non-POSIX operating system.
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>
7#include <kernel/utils/ref.h>
8
9#include <stdint.h>
10#include <sys/io.h>
11#include <sys/list.h>
12
13typedef 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 */
96typedef 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.
105
106/**
107 * @brief Socket states.
108 * @enum socket_state_t
109 */
120
121/**
122 * @brief Socket structure.
123 * @struct socket_t
124 */
125typedef struct socket
126{
129 char id[MAX_NAME];
134 weak_ptr_t ownerNs; ///< A weak pointer to the namespace that created the socket.
135 void* private;
137} socket_t;
138
139/**
140 * @brief Socket Family structure.
141 * @struct netfs_family_t
142 */
143typedef 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 */
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);
228
229/**
230 * @brief Initialize the networking filesystem.
231 */
232void 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 */
241
242/**
243 * @brief Unregister a network family.
244 *
245 * @param family Pointer to the network family structure.
246 */
248
249/** @} */
#define MAX_NAME
Maximum length of names.
Definition MAX_NAME.h:11
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
socket_state_t
Socket states.
Definition netfs.h:111
socket_type_t
Socket types.
Definition netfs.h:97
uint64_t netfs_family_register(netfs_family_t *family)
Register a network family.
Definition netfs.c:813
void netfs_init(void)
Initialize the networking filesystem.
Definition netfs.c:805
void netfs_family_unregister(netfs_family_t *family)
Unregister a network family.
Definition netfs.c:832
@ SOCKET_CLOSED
Definition netfs.h:118
@ SOCKET_LISTENING
Definition netfs.h:114
@ SOCKET_CONNECTED
Definition netfs.h:116
@ SOCKET_NEW
Definition netfs.h:112
@ SOCKET_CLOSING
Definition netfs.h:117
@ SOCKET_BOUND
Definition netfs.h:113
@ SOCKET_CONNECTING
Definition netfs.h:115
@ SOCKET_RDM
A reliable datagram layer that does not guarantee ordering.
Definition netfs.h:102
@ SOCKET_STREAM
A sequenced, reliable, two-way connection-based byte stream.
Definition netfs.h:98
@ SOCKET_SEQPACKET
A sequenced, reliable, two-way connection-based packet stream.
Definition netfs.h:100
@ SOCKET_DGRAM
A connectionless, unreliable datagram service.
Definition netfs.h:99
@ SOCKET_TYPE_AMOUNT
Definition netfs.h:103
@ SOCKET_RAW
Provides raw network protocol access.
Definition netfs.h:101
mode_t
Path flags and permissions.
Definition path.h:78
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 type.
Definition io.h:285
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition hpet.c:95
static uint64_t offset
Definition screen.c:19
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static atomic_long count
Definition main.c:11
__SIZE_TYPE__ size_t
Definition size_t.h:4
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
A entry in a doubly linked list.
Definition list.h:35
A doubly linked list.
Definition list.h:47
Mutex structure.
Definition mutex.h:41
Socket Family structure.
Definition netfs.h:144
list_entry_t listEntry
Definition netfs.h:224
rwmutex_t mutex
Definition netfs.h:226
const char * name
Definition netfs.h:145
list_t sockets
Definition netfs.h:225
Reference counting structure.
Definition ref.h:52
Read-Write Mutex structure.
Definition rwmutex.h:42
Socket structure.
Definition netfs.h:126
netfs_family_t * family
Definition netfs.h:131
ref_t ref
Definition netfs.h:127
socket_type_t type
Definition netfs.h:132
socket_state_t state
Definition netfs.h:133
list_entry_t listEntry
Definition netfs.h:128
weak_ptr_t ownerNs
A weak pointer to the namespace that created the socket.
Definition netfs.h:134
mutex_t mutex
Definition netfs.h:136
The primitive that threads block on.
Definition wait.h:182
Weak pointer structure.
Definition ref.h:35