Socket function definitions. More...
#include "common.h"
#include "intutils.h"
#include "ipinfo.h"
#include "memutils.h"
#include "socket.h"
Go to the source code of this file.
Data Structures | |
struct | qsc_socket_receive_async_state |
The socket async receive state structure. The structure contains pointers to the originating socket, message and error call-backs, and the message buffer. More... | |
struct | qsc_socket_receive_poll_state |
The socket polling state structure. The structure contains an array of client sockets, and a socket counter with sockets that are ready to receive data. More... | |
Macros | |
#define | QSC_SOCKET_RECEIVE_BUFFER_SIZE 1600ULL |
The socket receive buffer size. | |
Functions | |
QSC_EXPORT_API bool | qsc_socket_ipv4_valid_address (const char *address) |
The socket exception callback prototype. | |
QSC_EXPORT_API bool | qsc_socket_ipv6_valid_address (const char *address) |
Detects if the string contains a valid IPV6 address. | |
QSC_EXPORT_API bool | qsc_socket_is_blocking (const qsc_socket *sock) |
Determines if the socket is in blocking mode. | |
QSC_EXPORT_API bool | qsc_socket_is_connected (const qsc_socket *sock) |
Determines if the socket is connected. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_accept (const qsc_socket *source, qsc_socket *target) |
The Accept function handles an incoming connection attempt on the socket. | |
QSC_EXPORT_API void | qsc_socket_attach (qsc_socket *source, qsc_socket *target) |
Copy a socket to the target socket. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_bind (qsc_socket *sock, const char *address, uint16_t port) |
The Bind function associates an IP address with a socket. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_bind_ipv4 (qsc_socket *sock, const qsc_ipinfo_ipv4_address *address, uint16_t port) |
The Bind function associates an IPv4 address with a socket. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_bind_ipv6 (qsc_socket *sock, const qsc_ipinfo_ipv6_address *address, uint16_t port) |
The Bind function associates an IPv6 address with a socket. | |
QSC_EXPORT_API void | qsc_socket_clear_socket (qsc_socket *sock) |
Erases the socket struture. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_close_socket (qsc_socket *sock) |
Closes and disposes of the socket. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_connect (qsc_socket *sock, const char *address, uint16_t port) |
The Connect function establishes a connection to a remote host. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_connect_ipv4 (qsc_socket *sock, const qsc_ipinfo_ipv4_address *address, uint16_t port) |
The Connect function establishes a connection to a remote host using IPv4 addressing. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_connect_ipv6 (qsc_socket *sock, const qsc_ipinfo_ipv6_address *address, uint16_t port) |
The Connect function establishes a connection to a remote host using IPv6 addressing. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_create (qsc_socket *sock, qsc_socket_address_families family, qsc_socket_transports transport, qsc_socket_protocols protocol) |
The Create function creates a socket that is bound to a specific transport provider. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_listen (const qsc_socket *sock, int32_t backlog) |
Places the socket in the listening state, waiting for a connection. | |
QSC_EXPORT_API size_t | qsc_socket_max_send_buffer_size (const qsc_socket *sock) |
Get the maximum send buffer size for a socket. | |
QSC_EXPORT_API size_t | qsc_socket_peek (const qsc_socket *sock, uint8_t *output, size_t otplen) |
Receive data from a synchronous connected socket or a bound connection-less socket without downloading the entire message. | |
QSC_EXPORT_API size_t | qsc_socket_receive (const qsc_socket *sock, uint8_t *output, size_t otplen, qsc_socket_receive_flags flag) |
Receive data from a synchronous connected socket or a bound connection-less socket. Note: the receive buffer must be at least 1 byte larger than the expected size to accomodate a packet terminator. When calling receive with the wait-all flag, the receiver must include the terminator to empty the buffer. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_receive_async (qsc_socket_receive_async_state *state) |
Receive data from a connected socket asynchronously. | |
QSC_EXPORT_API size_t | qsc_socket_receive_all (const qsc_socket *sock, uint8_t *output, size_t otplen, qsc_socket_receive_flags flag) |
Receive a block of data from a synchronous connected socket or a bound connection-less socket, and returns when buffer is full. | |
QSC_EXPORT_API size_t | qsc_socket_receive_from (qsc_socket *sock, char *dest, uint16_t port, uint8_t *output, size_t otplen, qsc_socket_receive_flags flag) |
Receive data from a synchronous connected socket or a bound connection-less socket. | |
QSC_EXPORT_API uint32_t | qsc_socket_receive_poll (const qsc_socket_receive_poll_state *state) |
Polls an array of sockets. Fires a callback if a socket is ready to receive data, or an error if socket is disconnected. | |
QSC_EXPORT_API size_t | qsc_socket_send (const qsc_socket *sock, const uint8_t *input, size_t inplen, qsc_socket_send_flags flag) |
Sends data on a TCP connected socket. Note: The input buffer must be at least 1 byte longer than the input length. The send function terminates the packet with a null character for simplified string termination. | |
QSC_EXPORT_API size_t | qsc_socket_send_to (const qsc_socket *sock, const uint8_t *input, size_t inplen, qsc_socket_send_flags flag) |
Sends data on a UDP socket. | |
QSC_EXPORT_API size_t | qsc_socket_send_all (const qsc_socket *sock, const uint8_t *input, size_t inplen, qsc_socket_send_flags flag) |
Sends a block of data larger than a single packet size, on a TCP socket and returns when sent. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_shut_down (qsc_socket *sock, qsc_socket_shut_down_flags params) |
Shuts down a socket. | |
QSC_EXPORT_API const char * | qsc_socket_error_to_string (qsc_socket_exceptions code) |
Returns the error string associated with the exception code. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_get_last_error (void) |
The last error generated by the internal socket library. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_ioctl (const qsc_socket *sock, int32_t command, uint32_t *arguments) |
Sets the IO mode of the socket. | |
QSC_EXPORT_API bool | qsc_socket_receive_ready (const qsc_socket *sock, const struct timeval *timeout) |
Tests the socket to see if it is ready to receive data. | |
QSC_EXPORT_API bool | qsc_socket_send_ready (const qsc_socket *sock, const struct timeval *timeout) |
Tests the socket to see if it is ready to send data. | |
QSC_EXPORT_API void | qsc_socket_set_last_error (qsc_socket_exceptions error) |
Set the last error generated by the socket library. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_shut_down_sockets (void) |
Shut down the sockets library. | |
QSC_EXPORT_API qsc_socket_exceptions | qsc_socket_set_option (const qsc_socket *sock, qsc_socket_protocols level, qsc_socket_options option, int32_t optval) |
Send an option command to the socket. Options that use a boolean are format: 0=false, 1=true. | |
QSC_EXPORT_API bool | qsc_socket_start_sockets (void) |
Start the sockets library. | |
Socket function definitions.
This header provides the fundamental function definitions, enums, and structures for socket-based networking. It abstracts the underlying system-specific implementations for network socket operations, enabling a unified interface for both Windows and POSIX systems. The API supports the creation, configuration, and management of sockets for TCP/IP communication, including both IPv4 and IPv6. Functions provided in this module handle tasks such as opening, closing, binding, and listening on sockets, as well as setting various socket options and error handling.
Socket code enumeration names.
Enumerator | |
---|---|
qsc_socket_exception_success | The operation completed successfully |
qsc_socket_exception_error | The operation has failed |
qsc_socket_invalid_input | The input parameters are incorrect |
qsc_socket_exception_address_in_use | address already in use |
qsc_socket_exception_address_required | Destination address required |
qsc_socket_exception_address_unsupported | The address family is not supported |
qsc_socket_exception_already_in_use | qsc_socket is already connected |
qsc_socket_exception_blocking_cancelled | A blocking call was canceled |
qsc_socket_exception_blocking_in_progress | A blocking sockets call is in progress, or the service provider is still processing a callback function |
qsc_socket_exception_broadcast_address | The requested address is a broadcast address, but the appropriate flag was not set |
qsc_socket_exception_buffer_fault | The buffer parameter is not completely contained in a valid part of the user address space |
qsc_socket_exception_circuit_terminated | Software caused connection abort |
qsc_socket_exception_circuit_reset | connection reset by peer |
qsc_socket_exception_circuit_timeout | connection timed out |
qsc_socket_exception_connection_refused | connection refused |
qsc_socket_exception_descriptor_not_socket | qsc_socket operation on non-socket |
qsc_socket_exception_disk_quota_exceeded | The disk quota is exceeded |
qsc_socket_exception_dropped_connection | Network dropped connection on reset |
qsc_socket_exception_family_unsupported | Protocol family not supported |
qsc_socket_exception_host_is_down | The destination host is down |
qsc_socket_exception_host_unreachable | The remote host cannot be reached from this host at this time |
qsc_socket_exception_in_progress | Operation already in progress |
qsc_socket_exception_invalid_address | Can't assign requested address |
qsc_socket_exception_invalid_protocol | Protocol wrong type for socket |
qsc_socket_exception_invalid_protocol_option | Protocol not available |
qsc_socket_exception_item_is_remote | The item is not available locally |
qsc_socket_exception_message_too_long | The message size is too long |
qsc_socket_exception_name_too_long | The name is too long |
qsc_socket_exception_network_failure | Network is down |
qsc_socket_exception_network_unreachable | Network is unreachable |
qsc_socket_exception_no_buffer_space | No buffer space available |
qsc_socket_exception_no_descriptors | No more socket descriptors are available |
qsc_socket_exception_not_bound | The socket has not been bound with bind, or an unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled |
qsc_socket_exception_not_connected | qsc_socket is not connected |
qsc_socket_exception_operation_unsupported | The socket operation is not supported |
qsc_socket_exception_protocol_unsupported | Protocol not supported |
qsc_socket_exception_socket_unsupported | qsc_socket type not supported |
qsc_socket_exception_shut_down | Can't send after socket shutdown |
qsc_socket_exception_system_not_ready | The subsystem is unavailable |
qsc_socket_exception_too_many_users | The user quota is exceeded |
qsc_socket_exception_translation_failed | Can not translate name |
qsc_socket_exception_would_block | Operation would block |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_accept | ( | const qsc_socket * | source, |
qsc_socket * | target ) |
The Accept function handles an incoming connection attempt on the socket.
source | [const qsc_socket*] The source listening socket instance |
target | [const qsc_socket*] The socket receiving the new socket |
QSC_EXPORT_API void qsc_socket_attach | ( | qsc_socket * | source, |
qsc_socket * | target ) |
Copy a socket to the target socket.
source | [qsc_socket*] The source socket instance |
target | [qsc_socket*] The socket to attach |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_bind | ( | qsc_socket * | sock, |
const char * | address, | ||
uint16_t | port ) |
The Bind function associates an IP address with a socket.
sock | [qsc_socket*] The socket instance |
address | [const char*] The IP address to bind to the socket |
port | [uint16_t] The service port number |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_bind_ipv4 | ( | qsc_socket * | sock, |
const qsc_ipinfo_ipv4_address * | address, | ||
uint16_t | port ) |
The Bind function associates an IPv4 address with a socket.
sock | [qsc_socket*] The socket instance |
address | [const qsc_ipinfo_ipv4_address*] The IPv4 address to bind to the socket |
port | [uint16_t] The service port number |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_bind_ipv6 | ( | qsc_socket * | sock, |
const qsc_ipinfo_ipv6_address * | address, | ||
uint16_t | port ) |
The Bind function associates an IPv6 address with a socket.
sock | [qsc_socket*] The socket instance |
address | [const qsc_ipinfo_ipv6_address*] The IPv6 address to bind to the socket |
port | [uint16_t] The service port number |
QSC_EXPORT_API void qsc_socket_clear_socket | ( | qsc_socket * | sock | ) |
Erases the socket struture.
sock | [qsc_socket*] The socket instance |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_close_socket | ( | qsc_socket * | sock | ) |
Closes and disposes of the socket.
sock | [qsc_socket*] The socket instance |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_connect | ( | qsc_socket * | sock, |
const char * | address, | ||
uint16_t | port ) |
The Connect function establishes a connection to a remote host.
sock | [qsc_socket*] The socket instance |
address | [const char*] The remote hosts IP address |
port | [uint16_t] The remote hosts service port number |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_connect_ipv4 | ( | qsc_socket * | sock, |
const qsc_ipinfo_ipv4_address * | address, | ||
uint16_t | port ) |
The Connect function establishes a connection to a remote host using IPv4 addressing.
sock | [qsc_socket*] The socket instance |
address | [const qsc_ipinfo_ipv4_address*] The remote hosts IPv4 address |
port | [uint16_t] The remote hosts service port number |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_connect_ipv6 | ( | qsc_socket * | sock, |
const qsc_ipinfo_ipv6_address * | address, | ||
uint16_t | port ) |
The Connect function establishes a connection to a remote host using IPv6 addressing.
sock | [qsc_socket*] The socket instance |
address | [const] The remote hosts IPv6 address |
port | [const qsc_ipinfo_ipv6_address*] The remote hosts service port number |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_create | ( | qsc_socket * | sock, |
qsc_socket_address_families | family, | ||
qsc_socket_transports | transport, | ||
qsc_socket_protocols | protocol ) |
The Create function creates a socket that is bound to a specific transport provider.
sock | [qsc_socket*] The socket instance |
family | [qsc_socket_address_families] The address family |
transport | [qsc_socket_transports] The transport layer |
protocol | [qsc_socket_protocols] The socket protocol |
QSC_EXPORT_API const char * qsc_socket_error_to_string | ( | qsc_socket_exceptions | code | ) |
Returns the error string associated with the exception code.
code | [qsc_socket_exceptions] The exception code |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_get_last_error | ( | void | ) |
The last error generated by the internal socket library.
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_ioctl | ( | const qsc_socket * | sock, |
int32_t | command, | ||
uint32_t * | arguments ) |
Sets the IO mode of the socket.
sock | [const qsc_socket*] [const] The socket instance |
command | [int32_t] The command to pass to the socket |
arguments | [uint32_t*] The command arguments |
QSC_EXPORT_API bool qsc_socket_ipv4_valid_address | ( | const char * | address | ) |
The socket exception callback prototype.
source | [qsc_socket*] The socket source |
error | [qsc_socket_exceptions] The socket exception |
The socket receive asynchronous callback prototype
source | [qsc_socket*] The socket source |
message | [const uint8_t*] The socket message buffer |
msglen | [size_t*] A pointer to the size of the message |
The receive polling callback prototype
source | [const qsc_socket*] The socket source |
error | [size_t] The socket exception |
Detects if the string contains a valid IPV4 address
address | [const char*] The IP address string |
QSC_EXPORT_API bool qsc_socket_ipv6_valid_address | ( | const char * | address | ) |
Detects if the string contains a valid IPV6 address.
address | [const char*] The IP address string |
QSC_EXPORT_API bool qsc_socket_is_blocking | ( | const qsc_socket * | sock | ) |
Determines if the socket is in blocking mode.
sock | [const qsc_socket*] The socket instance |
QSC_EXPORT_API bool qsc_socket_is_connected | ( | const qsc_socket * | sock | ) |
Determines if the socket is connected.
sock | [const qsc_socket*] The socket instance |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_listen | ( | const qsc_socket * | sock, |
int32_t | backlog ) |
Places the socket in the listening state, waiting for a connection.
sock | [const qsc_socket*] The socket instance |
backlog | [int32_t] The maximum pending connections queue length |
QSC_EXPORT_API size_t qsc_socket_max_send_buffer_size | ( | const qsc_socket * | sock | ) |
Get the maximum send buffer size for a socket.
sock | [const qsc_socket*] The socket instance |
QSC_EXPORT_API size_t qsc_socket_peek | ( | const qsc_socket * | sock, |
uint8_t * | output, | ||
size_t | otplen ) |
Receive data from a synchronous connected socket or a bound connection-less socket without downloading the entire message.
sock | [const qsc_socket*] The socket instance |
output | [uint8_t*] The output buffer that receives data |
otplen | [size_t] The length of the output received |
QSC_EXPORT_API size_t qsc_socket_receive | ( | const qsc_socket * | sock, |
uint8_t * | output, | ||
size_t | otplen, | ||
qsc_socket_receive_flags | flag ) |
Receive data from a synchronous connected socket or a bound connection-less socket. Note: the receive buffer must be at least 1 byte larger than the expected size to accomodate a packet terminator. When calling receive with the wait-all flag, the receiver must include the terminator to empty the buffer.
sock | [const qsc_socket*] The socket instance |
output | [uint8_t*] The output buffer that receives data |
otplen | [size_t] The length of the output received |
flag | [qsc_socket_receive_flags] Flags that influence the behavior of the receive function |
QSC_EXPORT_API size_t qsc_socket_receive_all | ( | const qsc_socket * | sock, |
uint8_t * | output, | ||
size_t | otplen, | ||
qsc_socket_receive_flags | flag ) |
Receive a block of data from a synchronous connected socket or a bound connection-less socket, and returns when buffer is full.
sock | [const qsc_socket*] The socket instance |
output | [uint8_t*] The output buffer that receives data |
otplen | [size_t] The length of the output received |
flag | [qsc_socket_receive_flags] Flags that influence the behavior of the receive function |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_receive_async | ( | qsc_socket_receive_async_state * | state | ) |
Receive data from a connected socket asynchronously.
state | [qsc_socket_receive_async_state*] A pointer to the async receive data structure |
QSC_EXPORT_API size_t qsc_socket_receive_from | ( | qsc_socket * | sock, |
char * | dest, | ||
uint16_t | port, | ||
uint8_t * | output, | ||
size_t | otplen, | ||
qsc_socket_receive_flags | flag ) |
Receive data from a synchronous connected socket or a bound connection-less socket.
sock | [qsc_socket*] The local socket |
dest | [char*] The destination IP address string |
port | [uint16_t] The port receiving the data |
output | [uint8_t*] The output buffer |
otplen | [size_t] The length of the output buffer |
flag | [qsc_socket_receive_flags] Flags that influence the behavior of the receive from function |
QSC_EXPORT_API uint32_t qsc_socket_receive_poll | ( | const qsc_socket_receive_poll_state * | state | ) |
Polls an array of sockets. Fires a callback if a socket is ready to receive data, or an error if socket is disconnected.
state | [const qsc_socket_receive_poll_state*] The server state, containing a pointer to an array of sockets |
QSC_EXPORT_API bool qsc_socket_receive_ready | ( | const qsc_socket * | sock, |
const struct timeval * | timeout ) |
Tests the socket to see if it is ready to receive data.
sock | [const qsc_socket*] The socket instance |
timeout | [const struct timeval*] The receive wait timeout |
QSC_EXPORT_API size_t qsc_socket_send | ( | const qsc_socket * | sock, |
const uint8_t * | input, | ||
size_t | inplen, | ||
qsc_socket_send_flags | flag ) |
Sends data on a TCP connected socket. Note: The input buffer must be at least 1 byte longer than the input length. The send function terminates the packet with a null character for simplified string termination.
sock | [const qsc_socket*] The socket instance |
input | [const uint8_t*] The input buffer containing the data to be transmitted |
inplen | [size_t] The number of bytes to send |
flag | [qsc_socket_send_flags] Flags that influence the behavior of the send function |
QSC_EXPORT_API size_t qsc_socket_send_all | ( | const qsc_socket * | sock, |
const uint8_t * | input, | ||
size_t | inplen, | ||
qsc_socket_send_flags | flag ) |
Sends a block of data larger than a single packet size, on a TCP socket and returns when sent.
sock | [const qsc_socket*] The socket instance |
input | [const uint8_t*] The input buffer containing the data to be transmitted |
inplen | [size_t] The number of bytes to send |
flag | [qsc_socket_send_flags] Flags that influence the behavior of the send function |
QSC_EXPORT_API bool qsc_socket_send_ready | ( | const qsc_socket * | sock, |
const struct timeval * | timeout ) |
Tests the socket to see if it is ready to send data.
sock | [const qsc_socket*] The socket instance |
timeout | [const struct timeval*] The maximum time to wait for a response from the socket |
QSC_EXPORT_API size_t qsc_socket_send_to | ( | const qsc_socket * | sock, |
const uint8_t * | input, | ||
size_t | inplen, | ||
qsc_socket_send_flags | flag ) |
Sends data on a UDP socket.
sock | [const qsc_socket*] The socket instance |
input | [const uint8_t*] The input buffer containing the data to be transmitted |
inplen | [size_t] The number of bytes to send |
flag | [qsc_socket_send_flags] Flags that influence the behavior of the send function |
QSC_EXPORT_API void qsc_socket_set_last_error | ( | qsc_socket_exceptions | error | ) |
Set the last error generated by the socket library.
error | [qsc_socket_exceptions] The error code |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_set_option | ( | const qsc_socket * | sock, |
qsc_socket_protocols | level, | ||
qsc_socket_options | option, | ||
int32_t | optval ) |
Send an option command to the socket. Options that use a boolean are format: 0=false, 1=true.
sock | [const qsc_socket*] The socket instance |
level | [qsc_socket_protocols] The level at which the option is assigned |
option | [qsc_socket_options] The option command to send |
optval | [int32_t] The value of the option command |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_shut_down | ( | qsc_socket * | sock, |
qsc_socket_shut_down_flags | params ) |
Shuts down a socket.
sock | [qsc_socket*] The socket instance |
params | [qsc_socket_shut_down_flags] The shutdown parameters |
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_shut_down_sockets | ( | void | ) |
Shut down the sockets library.
QSC_EXPORT_API bool qsc_socket_start_sockets | ( | void | ) |
Start the sockets library.