PatchworkOS
Loading...
Searching...
No Matches
Sockets

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_tsocket_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.
 

Detailed Description

Sockets.

Sockets are exposed in the /net directory. Sockets provide communication endpoints for networking.

Creating Sockets

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.

Using Sockets

Sockets are interacted with using the following files located in their directory.

accept

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.

ctl

The /net/<family_name>/<socket_id>/ctl file is used to send "commands" to the socket. Here is a list of supported commands:

data

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.

Enumeration Type Documentation

◆ socket_state_t

Socket states.

Enumerator
SOCKET_NEW 
SOCKET_BOUND 
SOCKET_LISTENING 
SOCKET_CONNECTING 
SOCKET_CONNECTED 
SOCKET_CLOSING 
SOCKET_CLOSED 
SOCKET_STATE_AMOUNT 

Definition at line 64 of file socket.h.

Function Documentation

◆ 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.

Parameters
sockPointer to the socket.
stateTarget 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().

◆ socket_end_transition()

void socket_end_transition ( socket_t sock,
uint64_t  result 
)

Ends a socket state transition.

Parameters
sockPointer to the socket.
resultResult 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_new()

◆ socket_start_transition()

uint64_t socket_start_transition ( socket_t sock,
socket_state_t  state 
)

Starts a socket state transition.

Parameters
sockPointer to the socket.
stateTarget state.
Returns
On success, 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().