|
Veritable Lasagna
An Allocator & Data Structure Library for C.
|
#include <arpa/inet.h>#include <errno.h>#include <fcntl.h>#include <netinet/in.h>#include <netinet/tcp.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/socket.h>#include <unistd.h>
Include dependency graph for vl_socket_posix.c:Data Structures | |
| struct | vl_socket |
| Opaque socket handle. More... | |
Functions | |
| vl_socket_result | vlSocketStartup (void) |
| Initializes the process-wide socket subsystem. | |
| void | vlSocketShutdownLibrary (void) |
| Shuts down the process-wide socket subsystem. | |
| vl_socket | vlSocketNew (vl_socket_domain domain, vl_socket_type type) |
| Creates a new socket. | |
| void | vlSocketDelete (vl_socket socket) |
| Closes and deletes a socket handle. | |
| vl_socket_result | vlSocketBind (vl_socket socket, const vl_socket_address *address) |
| Binds a socket to a local address. | |
| vl_socket_result | vlSocketListen (vl_socket socket, vl_int_t backlog) |
| Marks a stream socket as a passive listening socket. | |
| vl_socket | vlSocketAccept (vl_socket socket, vl_socket_address *outAddress) |
| Accepts an incoming connection from a listening socket. | |
| vl_socket_result | vlSocketConnect (vl_socket socket, const vl_socket_address *address) |
| Connects a socket to a remote address. | |
| vl_int64_t | vlSocketSend (vl_socket socket, const void *buffer, vl_memsize_t length) |
| Sends bytes through a connected socket. | |
| vl_int64_t | vlSocketReceive (vl_socket socket, void *buffer, vl_memsize_t length) |
| Receives bytes from a connected socket. | |
| vl_socket_result | vlSocketShutdown (vl_socket socket, vl_socket_shutdown how) |
| Shuts down part or all of a full-duplex connection. | |
| vl_socket_result | vlSocketSetBlocking (vl_socket socket, vl_bool_t blocking) |
| Configures whether a socket operates in blocking mode. | |
| vl_bool_t | vlSocketAddressSetIPv4 (vl_socket_address *address, vl_uint8_t a, vl_uint8_t b, vl_uint8_t c, vl_uint8_t d, vl_uint16_t port) |
| Fills a socket address with an IPv4 address and port. | |
| vl_bool_t | vlSocketAddressSetIPv6 (vl_socket_address *address, const vl_uint8_t ipv6Bytes[16], vl_uint16_t port) |
| Fills a socket address with an IPv6 address and port. | |
| vl_socket_result | vlSocketSetReuseAddress (vl_socket socket, vl_bool_t enabled) |
| Configures whether local address reuse is enabled for a socket. | |
| vl_socket_result | vlSocketSetNoDelay (vl_socket socket, vl_bool_t enabled) |
| Configures whether the Nagle algorithm is disabled for a TCP socket. | |
| vl_socket_result | vlSocketSetKeepAlive (vl_socket socket, vl_bool_t enabled) |
| Configures whether TCP keep-alive probes are enabled. | |
| vl_socket_result | vlSocketGetReuseAddress (vl_socket socket, vl_bool_t *outEnabled) |
| Checks if local address reuse is enabled for a socket. | |
| vl_socket_result | vlSocketGetNoDelay (vl_socket socket, vl_bool_t *outEnabled) |
| Checks if the Nagle algorithm is disabled for a TCP socket. | |
| vl_socket_result | vlSocketGetKeepAlive (vl_socket socket, vl_bool_t *outEnabled) |
| Checks if TCP keep-alive probes are enabled. | |
| vl_socket_result | vlSocketIsBlocking (vl_socket socket, vl_bool_t *outBlocking) |
| Checks if a socket is in blocking mode. | |
| const char * | vlSocketError (void) |
| Returns a human-readable description of the most recent socket error. | |
| vl_socket_result | vlSocketGetRemoteAddress (vl_socket socket, vl_socket_address *outAddress) |
| Retrieves the remote address that the socket is connected to. | |
| vl_bool_t | vlSocketAddressToString (const vl_socket_address *address, char *buffer, vl_memsize_t bufferSize) |
| Converts a socket address to a human-readable string. | |
| vl_bool_t | vlSocketAddressFromString (vl_socket_address *address, const char *string) |
| Parses a human-readable string into a socket address. | |
| vl_socket_result | vlSocketGetLocalAddress (vl_socket socket, vl_socket_address *outAddress) |
| Retrieves the local address that the socket is bound to. | |
| struct vl_socket_ |
Opaque socket handle.
The underlying representation is platform-specific:
Collaboration diagram for vl_socket:| Data Fields | ||
|---|---|---|
| vl_bool_t | blocking | |
| vl_socket_domain | domain | |
| int | fd | |
| SOCKET | fd | |
| vl_socket_type | type | |
| vl_socket vlSocketAccept | ( | vl_socket | socket, |
| vl_socket_address * | outAddress | ||
| ) |
Accepts an incoming connection from a listening socket.
On success, returns a newly allocated socket handle representing the accepted connection.
If outAddress is non-NULL, it receives the remote peer address.
vlSocketDelete.socket must remain valid throughout the call. outAddress, if provided, must be writable.VL_SOCKET_NULL if socket is NULL or if the accept operation fails.VL_SOCKET_NULL on backend failure or if the operation would block in non-blocking mode.VL_SOCKET_NULL on failure.| socket | listening socket |
| outAddress | optional pointer receiving the remote address |
VL_SOCKET_NULL on failure | vl_bool_t vlSocketAddressFromString | ( | vl_socket_address * | address, |
| const char * | string | ||
| ) |
Parses a human-readable string into a socket address.
Supports IPv4 ("a.b.c.d:port") and IPv6 ("[addr]:port") formats.
address and string must remain valid for the duration of the call.VL_FALSE if address or string is NULL.VL_FALSE if the string format is invalid.VL_TRUE on success, VL_FALSE on failure.| address | destination address structure |
| string | source string to parse |
| vl_bool_t vlSocketAddressSetIPv4 | ( | vl_socket_address * | address, |
| vl_uint8_t | a, | ||
| vl_uint8_t | b, | ||
| vl_uint8_t | c, | ||
| vl_uint8_t | d, | ||
| vl_uint16_t | port | ||
| ) |
Fills a socket address with an IPv4 address and port.
The port argument is specified in host byte order.
address must remain valid for the duration of the call.VL_FALSE if address is NULL.VL_TRUE on success, VL_FALSE on invalid input.| address | destination address structure |
| a | first IPv4 octet |
| b | second IPv4 octet |
| c | third IPv4 octet |
| d | fourth IPv4 octet |
| port | port in host byte order |
| vl_bool_t vlSocketAddressSetIPv6 | ( | vl_socket_address * | address, |
| const vl_uint8_t | ipv6Bytes[16], | ||
| vl_uint16_t | port | ||
| ) |
Fills a socket address with an IPv6 address and port.
The sixteen-byte address is copied from ipv6Bytes. The port argument is specified in host byte order.
address and ipv6Bytes must remain valid for the duration of the call.VL_FALSE if address or ipv6Bytes is NULL.VL_TRUE on success, VL_FALSE on invalid input.| address | destination address structure |
| ipv6Bytes | pointer to 16 bytes of IPv6 address data |
| port | port in host byte order |
| vl_bool_t vlSocketAddressToString | ( | const vl_socket_address * | address, |
| char * | buffer, | ||
| vl_memsize_t | bufferSize | ||
| ) |
Converts a socket address to a human-readable string.
The output format is typically "a.b.c.d:port" for IPv4 and "[addr]:port" for IPv6.
address and buffer must remain valid for the duration of the call.VL_FALSE if address or buffer is NULL.VL_FALSE if bufferSize is too small.VL_TRUE on success, VL_FALSE on failure.| address | source address structure |
| buffer | destination string buffer |
| bufferSize | size of the destination buffer |
| vl_socket_result vlSocketBind | ( | vl_socket | socket, |
| const vl_socket_address * | address | ||
| ) |
Binds a socket to a local address.
socket and address must remain valid for the duration of the call.VL_SOCKET_ERROR_INVALID_ARGUMENT if socket or address is NULL.VL_SOCKET_ERROR_BIND on backend bind failure.VL_SOCKET_SUCCESS on success, otherwise an error code.| socket | socket handle |
| address | local address to bind |
| vl_socket_result vlSocketConnect | ( | vl_socket | socket, |
| const vl_socket_address * | address | ||
| ) |
Connects a socket to a remote address.
socket and address must remain valid for the duration of the call.VL_SOCKET_ERROR_INVALID_ARGUMENT if socket or address is NULL.VL_SOCKET_ERROR_CONNECT on backend connection failure.VL_SOCKET_SUCCESS on success, otherwise an error code.| socket | socket handle |
| address | remote address |
| void vlSocketDelete | ( | vl_socket | socket | ) |
Closes and deletes a socket handle.
VL_SOCKET_NULL (no-op).| socket | socket handle to destroy |
| const char * vlSocketError | ( | void | ) |
Returns a human-readable description of the most recent socket error.
The returned string is owned by the library and must not be modified or freed by the caller. The storage may be thread-local, static, or overwritten by subsequent socket API calls.
NULL if no additional error text is available.NULL.NULL | vl_socket_result vlSocketGetKeepAlive | ( | vl_socket | socket, |
| vl_bool_t * | outEnabled | ||
| ) |
Checks if TCP keep-alive probes are enabled.
| socket | socket handle |
| outEnabled | pointer receiving the enabled status |
| vl_socket_result vlSocketGetLocalAddress | ( | vl_socket | socket, |
| vl_socket_address * | outAddress | ||
| ) |
Retrieves the local address that the socket is bound to.
This function retrieves the local address information for the given socket. This is particularly useful after binding to an ephemeral port (port 0) to determine which port was actually assigned by the operating system.
socket and outAddress must remain valid for the duration of the call.VL_SOCKET_ERROR_INVALID_ARGUMENT if socket or outAddress is NULL.VL_SOCKET_ERROR_BIND or a similar code if the backend getsockname call fails.VL_SOCKET_SUCCESS on success, otherwise an error code.| socket | socket handle |
| outAddress | pointer to address structure where the local address will be stored |
| vl_socket_result vlSocketGetNoDelay | ( | vl_socket | socket, |
| vl_bool_t * | outEnabled | ||
| ) |
Checks if the Nagle algorithm is disabled for a TCP socket.
| socket | socket handle |
| outEnabled | pointer receiving the enabled status |
| vl_socket_result vlSocketGetRemoteAddress | ( | vl_socket | socket, |
| vl_socket_address * | outAddress | ||
| ) |
Retrieves the remote address that the socket is connected to.
socket and outAddress must remain valid for the duration of the call.VL_SOCKET_ERROR_INVALID_ARGUMENT if socket or outAddress is NULL.VL_SOCKET_ERROR_NOT_CONNECTED if the socket is not connected or a backend error occurs.VL_SOCKET_SUCCESS on success, otherwise an error code.| socket | socket handle |
| outAddress | pointer to address structure where the remote address will be stored |
| vl_socket_result vlSocketGetReuseAddress | ( | vl_socket | socket, |
| vl_bool_t * | outEnabled | ||
| ) |
Checks if local address reuse is enabled for a socket.
| socket | socket handle |
| outEnabled | pointer receiving the enabled status |
| vl_socket_result vlSocketIsBlocking | ( | vl_socket | socket, |
| vl_bool_t * | outBlocking | ||
| ) |
Checks if a socket is in blocking mode.
| socket | socket handle |
| outBlocking | pointer receiving the blocking status |
| vl_socket_result vlSocketListen | ( | vl_socket | socket, |
| vl_int_t | backlog | ||
| ) |
Marks a stream socket as a passive listening socket.
socket must remain valid for the duration of the call.VL_SOCKET_ERROR_INVALID_ARGUMENT if socket is NULL.VL_SOCKET_ERROR_LISTEN if the backend listen call fails.VL_SOCKET_SUCCESS on success, otherwise an error code.| socket | socket handle |
| backlog | maximum pending connection backlog |
| vl_socket vlSocketNew | ( | vl_socket_domain | domain, |
| vl_socket_type | type | ||
| ) |
Creates a new socket.
vl_socket handle and must release it with vlSocketDelete.VL_SOCKET_NULL on failure.VL_SOCKET_NULL if the subsystem is not initialized, allocation fails, or the underlying socket creation call fails.VL_SOCKET_NULL on error.| domain | socket address family |
| type | socket kind |
VL_SOCKET_NULL on failure | vl_int64_t vlSocketReceive | ( | vl_socket | socket, |
| void * | buffer, | ||
| vl_memsize_t | length | ||
| ) |
Receives bytes from a connected socket.
A return value of 0 typically indicates an orderly remote shutdown for stream sockets.
socket and buffer must remain valid for the duration of the call.-1 if socket or buffer is NULL.-1 if the backend receive operation fails.0 on end-of-stream, or -1 on error.| socket | socket handle |
| buffer | destination buffer |
| length | maximum number of bytes to receive |
0 on orderly close, or -1 on error | vl_int64_t vlSocketSend | ( | vl_socket | socket, |
| const void * | buffer, | ||
| vl_memsize_t | length | ||
| ) |
Sends bytes through a connected socket.
This function may perform a partial send. A successful return value smaller than length does not indicate an error.
socket and buffer must remain valid for the duration of the call.-1 if socket or buffer is NULL.-1 if the backend send operation fails.-1 on error.| socket | socket handle |
| buffer | source buffer |
| length | number of bytes requested to send |
-1 on error | vl_socket_result vlSocketSetBlocking | ( | vl_socket | socket, |
| vl_bool_t | blocking | ||
| ) |
Configures whether a socket operates in blocking mode.
socket must remain valid for the duration of the call.VL_SOCKET_ERROR_INVALID_ARGUMENT if socket is NULL.VL_SOCKET_ERROR_SET_OPTION if the backend mode change fails.VL_SOCKET_SUCCESS on success, otherwise an error code.| socket | socket handle |
| blocking | VL_TRUE for blocking mode, VL_FALSE for non-blocking mode |
| vl_socket_result vlSocketSetKeepAlive | ( | vl_socket | socket, |
| vl_bool_t | enabled | ||
| ) |
Configures whether TCP keep-alive probes are enabled.
When enabled, the underlying TCP implementation will periodically send keep-alive probes on an idle connection to verify the remote endpoint is still reachable.
This function typically corresponds to the SO_KEEPALIVE socket option.
socket must remain valid for the duration of the call.VL_SOCKET_ERROR_INVALID_ARGUMENT if socket is NULL.VL_SOCKET_ERROR_SET_OPTION if the backend socket option call fails.VL_SOCKET_SUCCESS on success, otherwise an error code.| socket | socket handle |
| enabled | VL_TRUE to enable keep-alive, VL_FALSE to disable it |
| vl_socket_result vlSocketSetNoDelay | ( | vl_socket | socket, |
| vl_bool_t | enabled | ||
| ) |
Configures whether the Nagle algorithm is disabled for a TCP socket.
When enabled (no-delay is VL_TRUE), small packets are sent as soon as possible without waiting for additional data to fill a segment. This reduces latency at the potential cost of network efficiency.
This function typically corresponds to the TCP_NODELAY socket option.
socket must remain valid for the duration of the call.VL_SOCKET_ERROR_INVALID_ARGUMENT if socket is NULL.VL_SOCKET_ERROR_SET_OPTION if the backend socket option call fails.VL_SOCKET_SUCCESS on success, otherwise an error code.| socket | socket handle |
| enabled | VL_TRUE to disable Nagle's algorithm, VL_FALSE to enable it |
| vl_socket_result vlSocketSetReuseAddress | ( | vl_socket | socket, |
| vl_bool_t | enabled | ||
| ) |
Configures whether local address reuse is enabled for a socket.
This function requests that the underlying socket enable or disable the platform's address reuse option, typically corresponding to SO_REUSEADDR. This is commonly used for server sockets that need to re-bind to a recently used local address without waiting for normal timeout expiration.
The exact backend behavior may vary slightly by platform. In particular, address reuse semantics are not perfectly identical between POSIX systems and Winsock.
For best portability, this option should generally be configured before calling vlSocketBind.
socket must remain valid for the duration of the call.VL_SOCKET_ERROR_INVALID_ARGUMENT if socket is NULL.VL_SOCKET_ERROR_SET_OPTION if the backend socket option call fails.VL_SOCKET_SUCCESS on success, otherwise an error code.| socket | socket handle |
| enabled | VL_TRUE to enable local address reuse, VL_FALSE to disable it |
| vl_socket_result vlSocketShutdown | ( | vl_socket | socket, |
| vl_socket_shutdown | how | ||
| ) |
Shuts down part or all of a full-duplex connection.
socket must remain valid for the duration of the call.VL_SOCKET_ERROR_INVALID_ARGUMENT if socket is NULL.VL_SOCKET_ERROR_SHUTDOWN if the backend shutdown call fails.VL_SOCKET_SUCCESS on success, otherwise an error code.| socket | socket handle |
| how | which direction(s) to shut down |
| void vlSocketShutdownLibrary | ( | void | ) |
Shuts down the process-wide socket subsystem.
On platforms where no explicit teardown is required, this function is a no-op.
| vl_socket_result vlSocketStartup | ( | void | ) |
Initializes the process-wide socket subsystem.
On platforms where no explicit socket startup is required, this function is a no-op that returns VL_SOCKET_SUCCESS.
On platforms that require initialization of the networking subsystem, this function must be called successfully before creating sockets.
VL_SOCKET_ERROR_SYSTEM or VL_SOCKET_ERROR_NOT_INITIALIZED if platform startup fails.VL_SOCKET_SUCCESS on success, otherwise an error code.