PatchworkOS
966e257
A non-POSIX operating system.
Theme:
Default
Round
Robot
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
>
6
#include <
kernel/sync/rwmutex.h
>
7
#include <
kernel/utils/ref.h
>
8
9
#include <
sys/io.h
>
10
11
typedef
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
*/
65
typedef
enum
66
{
67
SOCKET_NEW
,
68
SOCKET_BOUND
,
69
SOCKET_LISTENING
,
70
SOCKET_CONNECTING
,
71
SOCKET_CONNECTED
,
72
SOCKET_CLOSING
,
73
SOCKET_CLOSED
,
74
SOCKET_STATE_AMOUNT
75
}
socket_state_t
;
76
77
/**
78
* @brief Socket structure.
79
* @struct socket_t
80
*/
81
typedef
struct
socket
82
{
83
ref_t
ref
;
84
char
id
[
MAX_NAME
];
85
char
address
[
MAX_NAME
];
86
socket_family_t
*
family
;
87
socket_type_t
type
;
88
void
*
private
;
89
socket_state_t
currentState
;
90
socket_state_t
nextState
;
91
rwmutex_t
mutex
;
92
dentry_t
*
ctlFile
;
93
dentry_t
*
dataFile
;
94
dentry_t
*
acceptFile
;
95
}
socket_t
;
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
*/
106
socket_t
*
socket_new
(
socket_family_t
* family,
socket_type_t
type);
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
*/
115
uint64_t
socket_start_transition
(
socket_t
* sock,
socket_state_t
state);
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
*/
123
void
socket_continue_transition
(
socket_t
* sock,
socket_state_t
state);
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
*/
131
void
socket_end_transition
(
socket_t
* sock,
uint64_t
result);
132
133
/** @} */
MAX_NAME
#define MAX_NAME
Maximum length of names.
Definition
MAX_NAME.h:11
socket_type_t
socket_type_t
Socket type enumeration.
Definition
socket_type.h:18
socket_state_t
socket_state_t
Socket states.
Definition
socket.h:66
socket_new
socket_t * socket_new(socket_family_t *family, socket_type_t type)
Create a new socket.
Definition
socket.c:357
socket_continue_transition
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
socket_start_transition
uint64_t socket_start_transition(socket_t *sock, socket_state_t state)
Starts a socket state transition.
Definition
socket.c:489
socket_end_transition
void socket_end_transition(socket_t *sock, uint64_t result)
Ends a socket state transition.
Definition
socket.c:526
SOCKET_CLOSED
@ SOCKET_CLOSED
Definition
socket.h:73
SOCKET_LISTENING
@ SOCKET_LISTENING
Definition
socket.h:69
SOCKET_STATE_AMOUNT
@ SOCKET_STATE_AMOUNT
Definition
socket.h:74
SOCKET_CONNECTED
@ SOCKET_CONNECTED
Definition
socket.h:71
SOCKET_NEW
@ SOCKET_NEW
Definition
socket.h:67
SOCKET_CLOSING
@ SOCKET_CLOSING
Definition
socket.h:72
SOCKET_BOUND
@ SOCKET_BOUND
Definition
socket.h:68
SOCKET_CONNECTING
@ SOCKET_CONNECTING
Definition
socket.h:70
address
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition
hpet.c:95
io.h
path.h
ref.h
rwmutex.h
socket_type.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
dentry_t
Directory entry structure.
Definition
dentry.h:84
ref_t
Reference counting structure.
Definition
ref.h:30
rwmutex_t
Read-Write Mutex structure.
Definition
rwmutex.h:42
socket_family_t
Socket Family structure.
Definition
socket_family.h:66
socket_t
Socket structure.
Definition
socket.h:82
socket_t::dataFile
dentry_t * dataFile
Definition
socket.h:93
socket_t::nextState
socket_state_t nextState
Definition
socket.h:90
socket_t::ctlFile
dentry_t * ctlFile
Definition
socket.h:92
socket_t::ref
ref_t ref
Definition
socket.h:83
socket_t::type
socket_type_t type
Definition
socket.h:87
socket_t::currentState
socket_state_t currentState
Definition
socket.h:89
socket_t::family
socket_family_t * family
Definition
socket.h:86
socket_t::acceptFile
dentry_t * acceptFile
Definition
socket.h:94
socket_t::mutex
rwmutex_t mutex
Definition
socket.h:91
sysfs.h
src
modules
net
socket.h
Generated on Mon Dec 15 2025 21:55:53 for PatchworkOS by
1.9.8