40#ifndef QSC_SOCKETBASE_H 
   41#define QSC_SOCKETBASE_H 
   49QSC_CPLUSPLUS_ENABLED_START
 
   71#if defined(QSC_SYSTEM_OS_WINDOWS) 
   78#   if defined(QSC_SYSTEM_COMPILER_MSC) && defined(QSC_SYSTEM_MAX_PATH) 
   79#       pragma comment(lib, "iphlpapi.lib") 
   80#       pragma comment(lib, "ws2_32.lib") 
   82#elif defined(QSC_SYSTEM_OS_POSIX) 
   86#   include <netinet/in.h> 
   87#   include <arpa/inet.h> 
   88#   include <sys/select.h> 
   89#   include <sys/socket.h> 
   91#   include <sys/types.h> 
   94#   if defined(QSC_SYSTEM_OS_LINUX) 
   95#       include <netpacket/packet.h> 
   96#   elif defined(QSC_SYSTEM_OS_APPLE) 
   97#       include <net/if_dl.h> 
   98#       include <netinet/in.h> 
  100#       if !defined(AF_PACKET) 
  101#           define AF_PACKET PF_INET 
  103#   elif defined(QSC_SYSTEM_OS_UNIX) 
  124#define QSC_SOCKET_RECEIVE_BUFFER_SIZE 1600ULL 
  134#if defined(QSC_SYSTEM_OS_WINDOWS) 
  155    qsc_socket_exception_invalid_parameter = WSA_INVALID_PARAMETER, 
 
  158    qsc_socket_exception_invalid_provider = WSAEINVALIDPROVIDER,    
 
  166    qsc_socket_exception_no_memory = WSA_NOT_ENOUGH_MEMORY,         
 
  169    qsc_socket_exception_not_initialized = WSANOTINITIALISED,       
 
  175    qsc_socket_exception_too_many_processes = WSAEPROCLIM,          
 
 
  229static const char QSC_SOCKET_ERROR_STRINGS[48][128] =
 
  231    "SUCCESS: The operation completed successfully.",
 
  232    "ERROR: The operation has failed.",
 
  233    "INVALID: The input parameters are incorrect.",
 
  234    "EADDRINUSE: The socket's local address is in use and the socket was not marked to allow address reuse with SO_REUSEADDR.",
 
  235    "EDESTADDRREQ: A destination address is required.",
 
  236    "EAFNOSUPPORT: The address family is not supported.",
 
  237    "EISCONN: The socket is already connected.",
 
  238    "EINTR: A blocking sockets call was canceled.",
 
  239    "EINPROGRESS: A blocking sockets call is in progress, or the service provider is still processing a callback function.",
 
  240    "EACCES: The requested address is a broadcast address, but the appropriate flag was not set.",
 
  241    "EFAULT: The buffer parameter is not completely contained in a valid part of the user address space.",
 
  242    "ECONNRESET: The virtual circuit was reset by the remote side executing a hard or abortive close.",
 
  243    "ECONNABORTED: The virtual circuit was terminated due to a time-out or other failure.",
 
  244    "ETIMEDOUT: The connection has been dropped, because of a network failure.",
 
  245    "ECONNREFUSED: The connection was refused.",
 
  246    "ENOTSOCK: The descriptor is not a socket.",
 
  247    "EDQUOT: The disk quota is exceeded.",
 
  248    "ENETRESET: The connection has been broken due to the keep-alive activity detecting a failure.",
 
  249    "EPFNOSUPPORT: The protocol family is not supported.",
 
  250    "EHOSTDOWN: The destination host is down.",
 
  251    "EHOSTUNREACH: The remote host cannot be reached from this host at this time.",
 
  252    "EALREADY: Operation in progress.",
 
  253    "EADDRNOTAVAIL: The address is not available.",
 
  254    "INVALID_PARAMETER: One or more parameters are invalid.",
 
  255    "EPROTOTYPE: The protocol type is invalid for the socket.",
 
  256    "ENOPROTOOPT: The protocol option is invalid.",
 
  257    "EINVALIDPROVIDER: The service provider is invalid.",
 
  258    "EREMOTE: The item is not available locally.",
 
  259    "EMSGSIZE: The message size is too long.",
 
  260    "ENAMETOOLONG: The name is too long.",
 
  261    "ENETDOWN: The network subsystem has failed.",
 
  262    "ENETUNREACH: The network is unreachable.",
 
  263    "ENOBUFS: No buffer space is available.",
 
  264    "EMFILE: No more socket descriptors are available.",
 
  265    "_NOT_ENOUGH_MEMORY: The system does not have enough memory available.",
 
  266    "EINVAL: The socket has not been bound with bind, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled.",
 
  267    "ENOTCONN: The socket is not connected.",
 
  268    "NOTINITIALISED: A successful Startup call must occur before using this function.",
 
  269    "EOPNOTSUPP: The socket operation is not supported.",
 
  270    "EPROTONOSUPPORT: The protocol is not supported.",
 
  271    "ESHUTDOWN: The socket has been shut down.",
 
  272    "ESOCKTNOSUPPORT: The socket type is not supported.",
 
  273    "SYSNOTREADY: The subsystem is unavailable.",
 
  274    "EPROCLIM: The host is using too many processes.",
 
  275    "EUSERS: The user quota is exceeded.",
 
  276    "ELOOP: Can not translate name.",
 
  277    "EWOULDBLOCK: The socket is marked as nonblocking and the requested operation would block.",
 
  701QSC_CPLUSPLUS_ENABLED_END
 
Contains common definitions for the Quantum Secure Cryptographic (QSC) library.
 
#define QSC_EXPORT_API
API export macro for Microsoft compilers when importing from a DLL.
Definition common.h:520
 
#define QSC_SYSTEM_CONDITION_IGNORE(x)
MSVC-specific macro to disable a specific warning condition.
Definition common.h:784
 
This file contains common integer manipulation and conversion functions.
 
IP information function definitions.
 
Contains common memory-related functions implemented using SIMD instructions.
 
TCP/IP function constants and structures.
 
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....
Definition socketbase.c:564
 
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.
Definition socketbase.c:1098
 
QSC_EXPORT_API bool qsc_socket_start_sockets(void)
Start the sockets library.
Definition socketbase.c:1227
 
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.
Definition socketbase.c:173
 
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.
Definition socketbase.c:391
 
QSC_EXPORT_API void qsc_socket_clear_socket(qsc_socket *sock)
Erases the socket struture.
Definition socketbase.c:321
 
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.
Definition socketbase.c:1153
 
qsc_socket_exceptions
Socket code enumeration names.
Definition socketbase.h:130
 
@ qsc_socket_exception_would_block
Definition socketbase.h:221
 
@ qsc_socket_exception_broadcast_address
Definition socketbase.h:186
 
@ qsc_socket_exception_socket_unsupported
Definition socketbase.h:215
 
@ qsc_socket_exception_circuit_terminated
Definition socketbase.h:188
 
@ qsc_socket_exception_buffer_fault
Definition socketbase.h:187
 
@ qsc_socket_exception_message_too_long
Definition socketbase.h:205
 
@ qsc_socket_exception_address_unsupported
Definition socketbase.h:182
 
@ qsc_socket_exception_invalid_protocol_option
Definition socketbase.h:202
 
@ qsc_socket_exception_address_required
Definition socketbase.h:181
 
@ qsc_socket_exception_already_in_use
Definition socketbase.h:183
 
@ qsc_socket_exception_blocking_in_progress
Definition socketbase.h:185
 
@ qsc_socket_exception_no_descriptors
Definition socketbase.h:210
 
@ qsc_socket_exception_host_is_down
Definition socketbase.h:196
 
@ qsc_socket_exception_connection_refused
Definition socketbase.h:191
 
@ qsc_socket_exception_operation_unsupported
Definition socketbase.h:213
 
@ qsc_socket_exception_shut_down
Definition socketbase.h:216
 
@ qsc_socket_exception_disk_quota_exceeded
Definition socketbase.h:193
 
@ qsc_socket_exception_item_is_remote
Definition socketbase.h:204
 
@ qsc_socket_exception_circuit_timeout
Definition socketbase.h:190
 
@ qsc_socket_invalid_input
Definition socketbase.h:133
 
@ qsc_socket_exception_name_too_long
Definition socketbase.h:206
 
@ qsc_socket_exception_host_unreachable
Definition socketbase.h:197
 
@ qsc_socket_exception_dropped_connection
Definition socketbase.h:194
 
@ qsc_socket_exception_invalid_address
Definition socketbase.h:199
 
@ qsc_socket_exception_system_not_ready
Definition socketbase.h:217
 
@ qsc_socket_exception_not_connected
Definition socketbase.h:212
 
@ qsc_socket_exception_error
Definition socketbase.h:132
 
@ qsc_socket_exception_not_bound
Definition socketbase.h:211
 
@ qsc_socket_exception_success
Definition socketbase.h:131
 
@ qsc_socket_exception_too_many_users
Definition socketbase.h:219
 
@ qsc_socket_exception_family_unsupported
Definition socketbase.h:195
 
@ qsc_socket_exception_blocking_cancelled
Definition socketbase.h:184
 
@ qsc_socket_exception_circuit_reset
Definition socketbase.h:189
 
@ qsc_socket_exception_protocol_unsupported
Definition socketbase.h:214
 
@ qsc_socket_exception_invalid_protocol
Definition socketbase.h:201
 
@ qsc_socket_exception_descriptor_not_socket
Definition socketbase.h:192
 
@ qsc_socket_exception_address_in_use
Definition socketbase.h:180
 
@ qsc_socket_exception_network_failure
Definition socketbase.h:207
 
@ qsc_socket_exception_no_buffer_space
Definition socketbase.h:209
 
@ qsc_socket_exception_in_progress
Definition socketbase.h:198
 
@ qsc_socket_exception_translation_failed
Definition socketbase.h:220
 
@ qsc_socket_exception_network_unreachable
Definition socketbase.h:208
 
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.
Definition socketbase.c:279
 
#define QSC_SOCKET_RECEIVE_BUFFER_SIZE
The socket receive buffer size.
Definition socketbase.h:124
 
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.
Definition socketbase.c:238
 
QSC_EXPORT_API void qsc_socket_attach(qsc_socket *source, qsc_socket *target)
Copy a socket to the target socket.
Definition socketbase.c:202
 
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.
Definition socketbase.c:467
 
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,...
Definition socketbase.c:657
 
QSC_EXPORT_API void qsc_socket_set_last_error(qsc_socket_exceptions error)
Set the last error generated by the socket library.
Definition socketbase.c:1184
 
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.
Definition socketbase.c:498
 
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.
Definition socketbase.c:687
 
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 sock...
Definition socketbase.c:622
 
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.
Definition socketbase.c:363
 
QSC_EXPORT_API bool qsc_socket_is_blocking(const qsc_socket *sock)
Determines if the socket is in blocking mode.
Definition socketbase.c:136
 
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.
Definition socketbase.c:429
 
QSC_EXPORT_API size_t qsc_socket_max_send_buffer_size(const qsc_socket *sock)
Get the maximum send buffer size for a socket.
Definition socketbase.c:519
 
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_close_socket(qsc_socket *sock)
Closes and disposes of the socket.
Definition socketbase.c:335
 
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.
Definition socketbase.c:804
 
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.
Definition socketbase.c:212
 
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_shut_down(qsc_socket *sock, qsc_socket_shut_down_flags params)
Shuts down a socket.
Definition socketbase.c:834
 
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.
Definition socketbase.c:1124
 
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 downloadin...
Definition socketbase.c:546
 
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_shut_down_sockets(void)
Shut down the sockets library.
Definition socketbase.c:1193
 
QSC_EXPORT_API bool qsc_socket_ipv4_valid_address(const char *address)
The socket exception callback prototype.
Definition socketbase.c:98
 
QSC_EXPORT_API const char * qsc_socket_error_to_string(qsc_socket_exceptions code)
Returns the error string associated with the exception code.
Definition socketbase.c:860
 
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_receive_async(qsc_socket_receive_async_state *state)
Receive data from a connected socket asynchronously.
Definition socketbase.c:606
 
QSC_EXPORT_API bool qsc_socket_is_connected(const qsc_socket *sock)
Determines if the socket is connected.
Definition socketbase.c:153
 
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 ...
Definition socketbase.c:750
 
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.
Definition socketbase.c:768
 
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_get_last_error(void)
The last error generated by the internal socket library.
Definition socketbase.c:1085
 
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,...
Definition socketbase.c:1206
 
QSC_EXPORT_API bool qsc_socket_ipv6_valid_address(const char *address)
Detects if the string contains a valid IPV6 address.
Definition socketbase.c:117
 
qsc_socket_options
TCP socket options.
Definition socketflags.h:90
 
qsc_socket_transports
The socket transmission type.
Definition socketflags.h:165
 
qsc_socket_shut_down_flags
The socket shutdown api flags.
Definition socketflags.h:155
 
qsc_socket_address_families
The socket address family type.
Definition socketflags.h:68
 
qsc_socket_receive_flags
The socket receive api flags.
Definition socketflags.h:127
 
qsc_socket_send_flags
The socket send api flags.
Definition socketflags.h:144
 
qsc_socket_protocols
The socket IP protocol type.
Definition socketflags.h:108
 
The IPv4 address structure.
Definition ipinfo.h:150
 
The IPv6 address structure.
Definition ipinfo.h:309
 
The socket async receive state structure. The structure contains pointers to the originating socket,...
Definition socketbase.h:287
 
void(* callback)(qsc_socket *sock, const uint8_t *message, size_t *msglen)
Definition socketbase.h:288
 
void(* error)(const qsc_socket *sock, qsc_socket_exceptions exception)
Definition socketbase.h:289
 
uint8_t buffer[QSC_SOCKET_RECEIVE_BUFFER_SIZE]
Definition socketbase.h:291
 
qsc_socket * source
Definition socketbase.h:290
 
The socket polling state structure. The structure contains an array of client sockets,...
Definition socketbase.h:300
 
uint32_t count
Definition socketbase.h:304
 
void(* error)(qsc_socket *sock, qsc_socket_exceptions exception)
Definition socketbase.h:303
 
qsc_socket ** sockarr
Definition socketbase.h:301
 
void(* callback)(qsc_socket *sock, size_t id)
Definition socketbase.h:302
 
The socket instance structure.
Definition socket.h:121