PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1#pragma once
2
3#include "socket_type.h"
4#include <kernel/fs/path.h>
5#include <kernel/fs/sysfs.h>
7#include <kernel/utils/ref.h>
8
9#include <sys/io.h>
10
11typedef struct socket_family socket_family_t;
12
13/**
14 * @brief Sockets.
15 * @defgroup module_net_socket Sockets
16 * @ingroup module_net
17 *
18 * Sockets provide communication endpoints for networking and local client-server communication. They are exposed in the
19 * `/net` directory.
20 *
21 * ## Creating Sockets
22 *
23 * Sockets are created by opening a factory file, named after the socket type it will create, located in each socket
24 * family's directory. For example, to create a local seqpacket socket, open the `/net/local/seqpacket` file. This
25 * returns a handle that when read returns the socket's ID, which corresponds to the path
26 * `/net/<family_name>/<socket_id>/`, for example `/net/local/1234/`, which stores the files used to interact with the
27 * socket.
28 *
29 * The files used to interact with sockets are listed below.
30 *
31 * ### accept
32 *
33 * The `/net/<family_name>/<socket_id>/accept` file can be opened on a listening socket to accept incoming connections.
34 * Working in an similiar way to the POSIX `accept()` function, the returned file descriptor represents the new
35 * connection.
36 *
37 * If opened with `:nonblock` and there are no incoming connections, the open will fail with `EAGAIN`, otherwise it will
38 * block until a connection is available.
39 *
40 * ### ctl
41 *
42 * The `/net/<family_name>/<socket_id>/ctl` file is used to send "commands" to the socket by writing to it. Here is a
43 * list of supported commands:
44 * - `bind <address>`: Binds the socket to the specified address. (POSIX `bind()` function)
45 * - `listen <backlog>`: Puts the socket into listening mode with the specified backlog length. (POSIX `listen()`
46 * function)
47 * - `connect <address>`: Connects the socket to the specified address. (POSIX `connect()` function)
48 *
49 * ### data
50 *
51 * The `/net/<family_name>/<socket_id>/data` file is used to send and receive data using the socket. Writing to this
52 * file sends data, reading from it receives data. (POSIX `send()` and `recv()` functions)
53 *
54 * If opened with `:nonblock`, read and write operations will fail with `EAGAIN` if no data is available or there is no
55 * buffer space available, respectively. If not opened with `:nonblock` they will block, waiting for data or buffer
56 * space.
57 *
58 * @{
59 */
60
61/**
62 * @brief Socket states.
63 * @enum socket_state_t
64 */
76
77/**
78 * @brief Socket structure.
79 * @struct socket_t
80 */
96
97/**
98 * @brief Create a new socket.
99 *
100 * There is no `socket_free()` function, instead use `UNREF()` to free the socket.
101 *
102 * @param family Pointer to the socket family.
103 * @param type Socket type.
104 * @return On success, pointer to the new socket. On failure, `NULL` and `errno` is set.
105 */
107
108/**
109 * @brief Starts a socket state transition.
110 *
111 * @param sock Pointer to the socket.
112 * @param state Target state.
113 * @return On success, `0`. On failure, `ERR` and `errno` is set.
114 */
116
117/**
118 * @brief Without releasing the socket mutex, start a transition to a new target state.
119 *
120 * @param sock Pointer to the socket.
121 * @param state Target state.
122 */
124
125/**
126 * @brief Ends a socket state transition.
127 *
128 * @param sock Pointer to the socket.
129 * @param result Result of the transition, if `ERR` the transition failed.
130 */
131void socket_end_transition(socket_t* sock, uint64_t result);
132
133/** @} */
#define MAX_NAME
Maximum length of names.
Definition MAX_NAME.h:11
socket_type_t
Socket type enumeration.
Definition socket_type.h:18
socket_state_t
Socket states.
Definition socket.h:66
socket_t * socket_new(socket_family_t *family, socket_type_t type)
Create a new socket.
Definition socket.c:357
void socket_continue_transition(socket_t *sock, socket_state_t state)
Without releasing the socket mutex, start a transition to a new target state.
Definition socket.c:517
uint64_t socket_start_transition(socket_t *sock, socket_state_t state)
Starts a socket state transition.
Definition socket.c:489
void socket_end_transition(socket_t *sock, uint64_t result)
Ends a socket state transition.
Definition socket.c:526
@ SOCKET_CLOSED
Definition socket.h:73
@ SOCKET_LISTENING
Definition socket.h:69
@ SOCKET_STATE_AMOUNT
Definition socket.h:74
@ SOCKET_CONNECTED
Definition socket.h:71
@ SOCKET_NEW
Definition socket.h:67
@ SOCKET_CLOSING
Definition socket.h:72
@ SOCKET_BOUND
Definition socket.h:68
@ SOCKET_CONNECTING
Definition socket.h:70
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition hpet.c:95
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Directory entry structure.
Definition dentry.h:84
Reference counting structure.
Definition ref.h:30
Read-Write Mutex structure.
Definition rwmutex.h:42
Socket Family structure.
Socket structure.
Definition socket.h:82
dentry_t * dataFile
Definition socket.h:93
socket_state_t nextState
Definition socket.h:90
dentry_t * ctlFile
Definition socket.h:92
ref_t ref
Definition socket.h:83
socket_type_t type
Definition socket.h:87
socket_state_t currentState
Definition socket.h:89
socket_family_t * family
Definition socket.h:86
dentry_t * acceptFile
Definition socket.h:94
rwmutex_t mutex
Definition socket.h:91