QSC Post Quantum Cryptographic Library 1.1.0.2 (B2)
A post quantum secure library written in Ansi C
Loading...
Searching...
No Matches
socketbase.h
Go to the documentation of this file.
1/* 2020-2026 Quantum Resistant Cryptographic Solutions Corporation
2 * All Rights Reserved.
3 *
4 * NOTICE:
5 * This software and all accompanying materials are the exclusive property of
6 * Quantum Resistant Cryptographic Solutions Corporation (QRCS). The intellectual
7 * and technical concepts contained herein are proprietary to QRCS and are
8 * protected under applicable Canadian, U.S., and international copyright,
9 * patent, and trade secret laws.
10 *
11 * CRYPTOGRAPHIC ALGORITHMS AND IMPLEMENTATIONS:
12 * - This software includes implementations of cryptographic primitives and
13 * algorithms that are standardized or in the public domain, such as AES
14 * and SHA-3, which are not proprietary to QRCS.
15 * - This software also includes cryptographic primitives, constructions, and
16 * algorithms designed by QRCS, including but not limited to RCS, SCB, CSX, QMAC, and
17 * related components, which are proprietary to QRCS.
18 * - All source code, implementations, protocol compositions, optimizations,
19 * parameter selections, and engineering work contained in this software are
20 * original works of QRCS and are protected under this license.
21 *
22 * LICENSE AND USE RESTRICTIONS:
23 * - This software is licensed under the Quantum Resistant Cryptographic Solutions
24 * Public Research and Evaluation License (QRCS-PREL), 2025-2026.
25 * - Permission is granted solely for non-commercial evaluation, academic research,
26 * cryptographic analysis, interoperability testing, and feasibility assessment.
27 * - Commercial use, production deployment, commercial redistribution, or
28 * integration into products or services is strictly prohibited without a
29 * separate written license agreement executed with QRCS.
30 * - Licensing and authorized distribution are solely at the discretion of QRCS.
31 *
32 * EXPERIMENTAL CRYPTOGRAPHY NOTICE:
33 * Portions of this software may include experimental, novel, or evolving
34 * cryptographic designs. Use of this software is entirely at the user's risk.
35 *
36 * DISCLAIMER:
37 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38 * IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS
39 * FOR A PARTICULAR PURPOSE, SECURITY, OR NON-INFRINGEMENT. QRCS DISCLAIMS ALL
40 * LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
41 * ARISING FROM THE USE OR MISUSE OF THIS SOFTWARE.
42 *
43 * FULL LICENSE:
44 * This software is subject to the Quantum Resistant Cryptographic Solutions
45 * Public Research and Evaluation License (QRCS-PREL), 2025-2026. The complete license terms
46 * are provided in the accompanying LICENSE file or at https://www.qrcscorp.ca.
47 *
48 * Written by: John G. Underhill
49 * Contact: contact@qrcscorp.ca
50 */
51
52#ifndef QSC_SOCKETBASE_H
53#define QSC_SOCKETBASE_H
54
55#include "qsccommon.h"
56#include "intutils.h"
57#include "ipinfo.h"
58#include "memutils.h"
59#include "socket.h"
60
61QSC_CPLUSPLUS_ENABLED_START
62
79
80/* bogus winbase.h error */
82
83#if defined(QSC_SYSTEM_OS_WINDOWS)
84# include <WinSock2.h>
85#elif defined(QSC_SYSTEM_OS_POSIX)
86# include <sys/select.h>
87#endif
88
90//\def QSC_SOCKET_DUAL_IPV6_STACK
91//* \brief Enables a dual stack ipv4 and ipv6 listener.
92//*/
93//#if !defined(QSC_SOCKET_DUAL_IPV6_STACK)
94//# define QSC_SOCKET_DUAL_IPV6_STACK
95//#endif
96
97/*** Function State ***/
98
103#define QSC_SOCKET_RECEIVE_BUFFER_SIZE 1600U
104
109#define QSC_SOCKET_DEFAULT_SNDBUF_SIZE 65536U
110
114typedef enum
115{
119#if defined(QSC_SYSTEM_OS_WINDOWS)
121 qsc_socket_exception_address_required = WSAEDESTADDRREQ,
128 qsc_socket_exception_circuit_reset = WSAECONNRESET,
136 qsc_socket_exception_host_is_down = WSAEHOSTDOWN,
137 qsc_socket_exception_host_unreachable = WSAEHOSTUNREACH,
139 qsc_socket_exception_invalid_address = WSAEADDRNOTAVAIL,
140 qsc_socket_exception_invalid_parameter = WSA_INVALID_PARAMETER,
143 qsc_socket_exception_invalid_provider = WSAEINVALIDPROVIDER,
146 qsc_socket_exception_name_too_long = WSAENAMETOOLONG,
151 qsc_socket_exception_no_memory = WSA_NOT_ENOUGH_MEMORY,
154 qsc_socket_exception_not_initialized = WSANOTINITIALISED,
156 qsc_socket_exception_protocol_unsupported = WSAEPROTONOSUPPORT,
157 qsc_socket_exception_shut_down = WSAESHUTDOWN,
158 qsc_socket_exception_socket_unsupported = WSAESOCKTNOSUPPORT,
160 qsc_socket_exception_too_many_processes = WSAEPROCLIM,
163 qsc_socket_exception_would_block = WSAEWOULDBLOCK,
164#else
185 //qsc_socket_exception_invalid_parameter = EOTHER, /*!< One or more parameters are invalid */
188 //qsc_socket_exception_invalid_provider = EINVALIDPROVIDER, /*!< The service provider is invalid */
203 //qsc_socket_exception_too_many_processes = EPROCLIM, /*!< The host is using too many processes */
207
208#endif
210
214extern const char QSC_SOCKET_ERROR_STRINGS[48][128];
215
221typedef struct
222{
223 void (*callback)(qsc_socket* sock, const uint8_t* message, size_t* msglen);
224 void (*error)(const qsc_socket* sock, qsc_socket_exceptions exception);
228
234typedef struct
235{
237 void (*callback)(qsc_socket* sock, size_t id);
238 void (*error)(qsc_socket* sock, qsc_socket_exceptions exception);
239 uint32_t count;
241
242/*** Function Prototypes ***/
243
245//* \brief The socket exception callback prototype
246//*
247//* \param source: [qsc_socket*] The socket source
248//* \param error: [qsc_socket_exceptions] The socket exception
249//*/
250//QSC_EXPORT_API void qsc_socket_exception_callback(qsc_socket* source, qsc_socket_exceptions error);
251
253//* \brief The socket receive asynchronous callback prototype
254//*
255//* \param source: [qsc_socket*] The socket source
256//* \param message: [const uint8_t*] The socket message buffer
257//* \param msglen: [size_t*] A pointer to the size of the message
258//*/
259//QSC_EXPORT_API void qsc_socket_receive_async_callback(qsc_socket* source, const uint8_t* message, size_t* msglen);
260
262//* \brief The receive polling callback prototype
263//*
264//* \param source: [const qsc_socket*] The socket source
265//* \param error: [size_t] The socket exception
266//*/
267//QSC_EXPORT_API void qsc_socket_receive_poll_callback(const qsc_socket* source, size_t error);
268
269/*** Accessors ***/
270
277QSC_EXPORT_API bool qsc_socket_ipv4_valid_address(const char* address);
278
285QSC_EXPORT_API bool qsc_socket_ipv6_valid_address(const char* address);
286
295
304
314
322
332QSC_EXPORT_API qsc_socket_exceptions qsc_socket_bind(qsc_socket* sock, const char* address, uint16_t port);
333
344
355
362
371
381QSC_EXPORT_API qsc_socket_exceptions qsc_socket_connect(qsc_socket* sock, const char* address, uint16_t port);
382
393
404
416
426
435
445QSC_EXPORT_API size_t qsc_socket_peek(const qsc_socket* sock, uint8_t* output, size_t otplen);
446
459QSC_EXPORT_API size_t qsc_socket_receive(const qsc_socket* sock, uint8_t* output, size_t otplen, qsc_socket_receive_flags flag);
460
469
480QSC_EXPORT_API size_t qsc_socket_receive_all(const qsc_socket* sock, uint8_t* output, size_t otplen, qsc_socket_receive_flags flag);
481
495QSC_EXPORT_API size_t qsc_socket_receive_from(qsc_socket* sock, char* dest, size_t destlen, uint16_t port, uint8_t* output, size_t otplen, qsc_socket_receive_flags flag);
496
506
519QSC_EXPORT_API size_t qsc_socket_send(const qsc_socket* sock, const uint8_t* input, size_t inplen, qsc_socket_send_flags flag);
520
531QSC_EXPORT_API size_t qsc_socket_send_to(const qsc_socket* sock, const uint8_t* input, size_t inplen, qsc_socket_send_flags flag);
532
543QSC_EXPORT_API size_t qsc_socket_send_all(const qsc_socket* sock, const uint8_t* input, size_t inplen, qsc_socket_send_flags flag);
544
554
555/*~~~ Helper Functions ~~~*/
556
564
571
581QSC_EXPORT_API qsc_socket_exceptions qsc_socket_ioctl(const qsc_socket* sock, int32_t command, uint32_t* arguments);
582
591QSC_EXPORT_API bool qsc_socket_receive_ready(const qsc_socket* sock, const struct timeval* timeout);
592
601QSC_EXPORT_API bool qsc_socket_send_ready(const qsc_socket* sock, const struct timeval* timeout);
602
609
616
629
636
637QSC_CPLUSPLUS_ENABLED_END
638
639#endif
This file contains common integer manipulation and conversion functions.
IP information function definitions.
Contains common memory-related functions implemented using SIMD instructions.
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 qsccommon.h:605
#define QSC_SYSTEM_CONDITION_IGNORE(x)
A macro to disable a specific warning condition.
Definition qsccommon.h:851
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:706
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:1288
QSC_EXPORT_API bool qsc_socket_start_sockets(void)
Start the sockets library.
Definition socketbase.c:1415
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:315
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:530
QSC_EXPORT_API void qsc_socket_clear_socket(qsc_socket *sock)
Erases the socket struture.
Definition socketbase.c:455
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:1343
qsc_socket_exceptions
Socket code enumeration names.
Definition socketbase.h:115
@ qsc_socket_exception_would_block
Definition socketbase.h:206
@ qsc_socket_exception_broadcast_address
Definition socketbase.h:171
@ qsc_socket_exception_socket_unsupported
Definition socketbase.h:200
@ qsc_socket_exception_circuit_terminated
Definition socketbase.h:173
@ qsc_socket_exception_buffer_fault
Definition socketbase.h:172
@ qsc_socket_exception_message_too_long
Definition socketbase.h:190
@ qsc_socket_exception_address_unsupported
Definition socketbase.h:167
@ qsc_socket_exception_invalid_protocol_option
Definition socketbase.h:187
@ qsc_socket_exception_address_required
Definition socketbase.h:166
@ qsc_socket_exception_already_in_use
Definition socketbase.h:168
@ qsc_socket_exception_blocking_in_progress
Definition socketbase.h:170
@ qsc_socket_exception_no_descriptors
Definition socketbase.h:195
@ qsc_socket_exception_host_is_down
Definition socketbase.h:181
@ qsc_socket_exception_connection_refused
Definition socketbase.h:176
@ qsc_socket_exception_operation_unsupported
Definition socketbase.h:198
@ qsc_socket_exception_shut_down
Definition socketbase.h:201
@ qsc_socket_exception_disk_quota_exceeded
Definition socketbase.h:178
@ qsc_socket_exception_item_is_remote
Definition socketbase.h:189
@ qsc_socket_exception_circuit_timeout
Definition socketbase.h:175
@ qsc_socket_invalid_input
Definition socketbase.h:118
@ qsc_socket_exception_name_too_long
Definition socketbase.h:191
@ qsc_socket_exception_host_unreachable
Definition socketbase.h:182
@ qsc_socket_exception_dropped_connection
Definition socketbase.h:179
@ qsc_socket_exception_invalid_address
Definition socketbase.h:184
@ qsc_socket_exception_system_not_ready
Definition socketbase.h:202
@ qsc_socket_exception_not_connected
Definition socketbase.h:197
@ qsc_socket_exception_error
Definition socketbase.h:117
@ qsc_socket_exception_not_bound
Definition socketbase.h:196
@ qsc_socket_exception_success
Definition socketbase.h:116
@ qsc_socket_exception_too_many_users
Definition socketbase.h:204
@ qsc_socket_exception_family_unsupported
Definition socketbase.h:180
@ qsc_socket_exception_blocking_cancelled
Definition socketbase.h:169
@ qsc_socket_exception_circuit_reset
Definition socketbase.h:174
@ qsc_socket_exception_protocol_unsupported
Definition socketbase.h:199
@ qsc_socket_exception_invalid_protocol
Definition socketbase.h:186
@ qsc_socket_exception_descriptor_not_socket
Definition socketbase.h:177
@ qsc_socket_exception_address_in_use
Definition socketbase.h:165
@ qsc_socket_exception_network_failure
Definition socketbase.h:192
@ qsc_socket_exception_no_buffer_space
Definition socketbase.h:194
@ qsc_socket_exception_in_progress
Definition socketbase.h:183
@ qsc_socket_exception_translation_failed
Definition socketbase.h:205
@ qsc_socket_exception_network_unreachable
Definition socketbase.h:193
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:421
#define QSC_SOCKET_RECEIVE_BUFFER_SIZE
The socket receive buffer size.
Definition socketbase.h:103
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:380
QSC_EXPORT_API void qsc_socket_attach(qsc_socket *source, qsc_socket *target)
Copy a socket to the target socket.
Definition socketbase.c:344
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:606
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:799
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:1372
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:637
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:769
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:500
QSC_EXPORT_API bool qsc_socket_is_blocking(const qsc_socket *sock)
Determines if the socket is in blocking mode.
Definition socketbase.c:219
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:568
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:658
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_close_socket(qsc_socket *sock)
Closes and disposes of the socket.
Definition socketbase.c:472
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:976
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:354
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:1008
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:1314
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:686
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_shut_down_sockets(void)
Shut down the sockets library.
Definition socketbase.c:1381
QSC_EXPORT_API bool qsc_socket_ipv4_valid_address(const char *address)
The socket exception callback prototype.
Definition socketbase.c:185
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:1034
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:750
QSC_EXPORT_API bool qsc_socket_is_connected(const qsc_socket *sock)
Determines if the socket is connected.
Definition socketbase.c:241
QSC_EXPORT_API size_t qsc_socket_receive_from(qsc_socket *sock, char *dest, size_t destlen, 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:831
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:917
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:937
QSC_EXPORT_API qsc_socket_exceptions qsc_socket_get_last_error(void)
The last error generated by the internal socket library.
Definition socketbase.c:1275
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:1394
QSC_EXPORT_API bool qsc_socket_ipv6_valid_address(const char *address)
Detects if the string contains a valid IPV6 address.
Definition socketbase.c:202
qsc_socket_options
TCP socket options.
Definition socketflags.h:112
qsc_socket_transports
The socket transmission type.
Definition socketflags.h:203
qsc_socket_shut_down_flags
The socket shutdown api flags.
Definition socketflags.h:193
qsc_socket_address_families
The socket address family type.
Definition socketflags.h:80
qsc_socket_receive_flags
The socket receive api flags.
Definition socketflags.h:165
qsc_socket_send_flags
The socket send api flags.
Definition socketflags.h:182
qsc_socket_protocols
The socket IP protocol type.
Definition socketflags.h:142
The IPv4 address structure.
Definition ipinfo.h:161
The IPv6 address structure.
Definition ipinfo.h:320
The socket async receive state structure. The structure contains pointers to the originating socket,...
Definition socketbase.h:222
void(* callback)(qsc_socket *sock, const uint8_t *message, size_t *msglen)
Definition socketbase.h:223
void(* error)(const qsc_socket *sock, qsc_socket_exceptions exception)
Definition socketbase.h:224
uint8_t buffer[QSC_SOCKET_RECEIVE_BUFFER_SIZE]
Definition socketbase.h:226
qsc_socket * source
Definition socketbase.h:225
The socket polling state structure. The structure contains an array of client sockets,...
Definition socketbase.h:235
uint32_t count
Definition socketbase.h:239
void(* error)(qsc_socket *sock, qsc_socket_exceptions exception)
Definition socketbase.h:238
qsc_socket ** sockarr
Definition socketbase.h:236
void(* callback)(qsc_socket *sock, size_t id)
Definition socketbase.h:237
The socket instance structure.
Definition socket.h:133