|
PatchworkOS
|
Sockets. More...
Modules | |
| Socket Types | |
| Socket types. | |
Data Structures | |
| struct | socket_t |
| Socket structure. More... | |
Enumerations | |
| enum | socket_state_t { SOCKET_NEW , SOCKET_BOUND , SOCKET_LISTENING , SOCKET_CONNECTING , SOCKET_CONNECTED , SOCKET_CLOSING , SOCKET_CLOSED , SOCKET_STATE_AMOUNT } |
| Socket states. More... | |
Functions | |
| socket_t * | socket_new (socket_family_t *family, socket_type_t type, path_flags_t flags) |
| Create a new socket. | |
| uint64_t | socket_start_transition (socket_t *sock, socket_state_t state) |
| Starts a socket state 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. | |
| void | socket_end_transition (socket_t *sock, uint64_t result) |
| Ends a socket state transition. | |
Sockets.
Sockets are exposed in the /net directory. Sockets provide communication endpoints for networking.
Sockets are created by opening a factory located in each socket families directory. For example, to create a local seqpacket socket, open the /net/local/seqpacket/ file which gives you a handle that when read returns the socket's ID, which corresponds to the path /net/<family_name>/<socket_id>/, for example /net/local/1234/, which stores the files used to interact with the socket.
Sockets are interacted with using the following files located in their directory.
The /net/<family_name>/<socket_id>/accept file can be opened on a listening socket to accept incoming connections. Working in an similiar way to the POSIX accept() function, the returned file descriptor represents the new connection.
If opened with :nonblock and there are no incoming connections, the open will fail with EAGAIN, otherwise it will block until a connection is available.
The /net/<family_name>/<socket_id>/ctl file is used to send "commands" to the socket. Here is a list of supported commands:
bind <address>: Binds the socket to the specified address. (POSIX bind() function)listen <backlog>: Puts the socket into listening mode with the specified backlog length. (POSIX listen() function)connect <address>: Connects the socket to the specified address. (POSIX connect() function)The /net/<family_name>/<socket_id>/data file is used to send and retrieve data using the socket. Writing to this file sends data, reading from it receives data. (POSIX send() and recv() functions)
If opened with :nonblock, read and write operations will fail with EAGAIN if no data is available or there is no buffer space available, respectively, otherwise they will block, waiting for data or buffer space.
| enum socket_state_t |
| void socket_continue_transition | ( | socket_t * | sock, |
| socket_state_t | state | ||
| ) |
Without releasing the socket mutex, start a transition to a new target state.
| sock | Pointer to the socket. |
| state | Target state. |
Definition at line 519 of file socket.c.
References assert, socket_t::currentState, socket_t::nextState, and socket_can_transition().
Referenced by socket_accept_open(), and socket_ctl_connect().
Ends a socket state transition.
| sock | Pointer to the socket. |
| result | Result of the transition, if ERR the transition failed. |
Definition at line 528 of file socket.c.
References socket_t::currentState, ERR, socket_t::mutex, socket_t::nextState, and rwmutex_write_release().
Referenced by socket_accept_open(), socket_ctl_bind(), socket_ctl_connect(), and socket_ctl_listen().
| socket_t * socket_new | ( | socket_family_t * | family, |
| socket_type_t | type, | ||
| path_flags_t | flags | ||
| ) |
Create a new socket.
There is no socket_free() function, instead use DEREF() to free the socket.
| family | Pointer to the socket family. |
| type | Socket type. |
| flags | Path flags. |
NULL and errno is set. Definition at line 359 of file socket.c.
References socket_t::acceptFile, acceptOps, atomic_fetch_add, calloc(), socket_t::ctlFile, socket_t::currentState, socket_t::dataFile, dataOps, socket_family_ops_t::deinit, DEREF, EINVAL, ENOMEM, ERR, errno, socket_t::family, socket_t::flags, free(), socket_t::id, socket_family_ops_t::init, inodeOps, mount, socket_t::mutex, socket_family_t::newId, socket_t::nextState, NULL, socket_family_t::ops, PATH_EMPTY, path_put(), superblock_t::private, socket_t::private, socket_t::ref, REF, ref_init(), mount_t::root, rwmutex_deinit(), rwmutex_init(), sched_process(), snprintf(), socket_family_get_dir(), socket_free(), SOCKET_NEW, mount_t::superblock, superblockOps, sysfs_file_new(), sysfs_mount_new(), and socket_t::type.
Referenced by socket_accept_open(), and socket_factory_open().
| uint64_t socket_start_transition | ( | socket_t * | sock, |
| socket_state_t | state | ||
| ) |
Starts a socket state transition.
| sock | Pointer to the socket. |
| state | Target state. |
0. On failure, ERR and errno is set. Definition at line 491 of file socket.c.
References socket_t::currentState, EINVAL, ERR, errno, socket_t::mutex, socket_t::nextState, NULL, rwmutex_write_acquire(), rwmutex_write_release(), and socket_can_transition().
Referenced by socket_accept_open(), socket_ctl_bind(), socket_ctl_connect(), and socket_ctl_listen().