UDPConnection
A network connection implementation over UDP
Loading...
Searching...
No Matches
UDPC.h
Go to the documentation of this file.
1
26#ifndef UDPC_CONNECTION_H
27#define UDPC_CONNECTION_H
28
29#ifndef DOXYGEN_SHOULD_SKIP_THIS
30
31// Determine platform macros
32# define UDPC_PLATFORM_WINDOWS 1
33# define UDPC_PLATFORM_MAC 2
34# define UDPC_PLATFORM_LINUX 3
35# define UDPC_PLATFORM_UNKNOWN 0
36
37# if defined _WIN32
38# define UDPC_PLATFORM UDPC_PLATFORM_WINDOWS
39# elif defined __APPLE__
40# define UDPC_PLATFORM UDPC_PLATFORM_MAC
41# elif defined __linux__
42# define UDPC_PLATFORM UDPC_PLATFORM_LINUX
43# else
44# define UDPC_PLATFORM UDPC_PLATFORM_UNKNOWN
45# endif
46
47#endif // DOXYGEN_SHOULD_SKIP_THIS
48
49// OS-based networking macros
50#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
51# include <winsock2.h>
52# ifdef UDPC_PLATFORM_MINGW
53# include <ws2ipdef.h>
54# include <in6addr.h>
55# else
56# include <Ws2ipdef.h>
57# include <In6addr.h>
58# endif
59
60# ifndef DOXYGEN_SHOULD_SKIP_THIS
61
62# define UDPC_CLEANUPSOCKET(x) closesocket(x)
63# define UDPC_SOCKETTYPE SOCKET
64# define UDPC_IPV6_SOCKADDR_TYPE SOCKADDR_IN6
65# define UDPC_IPV6_ADDR_TYPE IN6_ADDR
66# define UDPC_IPV6_ADDR_SUB(addr) addr.u.Byte
67# define UDPC_SOCKET_RETURN_ERROR(socket) (socket == INVALID_SOCKET)
68
69# endif // DOXYGEN_SHOULD_SKIP_THIS
70
71#elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
72# include <fcntl.h>
73# include <netinet/in.h>
74# include <sys/socket.h>
75# include <unistd.h>
76
77# ifndef DOXYGEN_SHOULD_SKIP_THIS
78
79# define UDPC_CLEANUPSOCKET(x) close(x)
80# define UDPC_SOCKETTYPE int
81# define UDPC_IPV6_SOCKADDR_TYPE struct sockaddr_in6
82# define UDPC_IPV6_ADDR_TYPE struct in6_addr
83# define UDPC_IPV6_ADDR_SUB(addr) addr.s6_addr
84# define UDPC_SOCKET_RETURN_ERROR(socket) (socket <= 0)
85
86# endif // DOXYGEN_SHOULD_SKIP_THIS
87
88#else
89# ifndef DOXYGEN_SHOULD_SKIP_THIS
90# define UDPC_CLEANUPSOCKET(x) ((void)0)
91# endif // DOXYGEN_SHOULD_SKIP_THIS
92#endif
93
94// other defines
96#define UDPC_PACKET_MAX_SIZE 8192
97#define UDPC_DEFAULT_PROTOCOL_ID 1357924680 // 0x50f04948
98
99#ifndef DOXYGEN_SHOULD_SKIP_THIS
100
101// other defines continued
102
103# ifndef UDPC_LIBSODIUM_ENABLED
104# ifndef crypto_sign_PUBLICKEYBYTES
105# define crypto_sign_PUBLICKEYBYTES 1
106# endif
107# ifndef crypto_sign_SECRETKEYBYTES
108# define crypto_sign_SECRETKEYBYTES 1
109# endif
110# ifndef crypto_sign_BYTES
111# define crypto_sign_BYTES 1
112# endif
113# endif
114
115# if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
116# define UDPC_EXPORT __declspec(dllexport)
117# else
118# define UDPC_EXPORT
119# endif
120
121#endif // DOXYGEN_SHOULD_SKIP_THIS
122
123#ifdef __cplusplus
124# include <cstdint>
125extern "C" {
126#else
127# include <stdint.h>
128#endif
129
131struct UDPC_Context;
132typedef struct UDPC_Context *UDPC_HContext;
133
134typedef enum UDPC_EXPORT UDPC_LoggingType {
146 UDPC_DEBUG
147} UDPC_LoggingType;
148
151typedef enum UDPC_EXPORT UDPC_AuthPolicy {
156 // Used internally to get max size of enum
157 UDPC_AUTH_POLICY_SIZE
159
168typedef struct UDPC_EXPORT UDPC_ConnectionId {
169 UDPC_IPV6_ADDR_TYPE addr;
170 uint32_t scope_id;
171 uint16_t port;
173
183typedef struct UDPC_EXPORT UDPC_PacketInfo {
188 // id is stored at offset 8, size 4 (uint32_t) even for "empty" PktInfos
189 char *data;
200 uint32_t flags;
208 uint32_t id;
218 uint16_t dataSize;
219 uint16_t rtt;
225
245typedef enum UDPC_EXPORT UDPC_EventType {
246 UDPC_ET_NONE,
247 UDPC_ET_REQUEST_CONNECT,
248 UDPC_ET_REQUEST_DISCONNECT,
249 UDPC_ET_CONNECTED,
250 UDPC_ET_DISCONNECTED,
251 UDPC_ET_FAIL_CONNECT,
252 UDPC_ET_GOOD_MODE,
253 UDPC_ET_BAD_MODE
255
266typedef struct UDPC_EXPORT UDPC_Event {
267 UDPC_EventType type;
268 UDPC_ConnectionId conId;
269 union Value {
270 int dropAllWithAddr;
271 int enableLibSodium;
272 } v;
274
285UDPC_EXPORT UDPC_ConnectionId UDPC_create_id(UDPC_IPV6_ADDR_TYPE addr, uint16_t port);
286
294UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_full(UDPC_IPV6_ADDR_TYPE addr, uint32_t scope_id, uint16_t port);
295
305UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_anyaddr(uint16_t port);
306
317UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_easy(const char *addrString, uint16_t port);
318
319UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_hostname(const char *hostname, uint16_t port);
320
343UDPC_EXPORT UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient, int isUsingLibsodium);
363UDPC_EXPORT UDPC_HContext UDPC_init_threaded_update(
364 UDPC_ConnectionId listenId,
365 int isClient,
366 int isUsingLibsodium);
386UDPC_EXPORT UDPC_HContext UDPC_init_threaded_update_ms(
387 UDPC_ConnectionId listenId,
388 int isClient,
389 int updateMS,
390 int isUsingLibsodium);
391
401UDPC_EXPORT int UDPC_enable_threaded_update(UDPC_HContext ctx);
412UDPC_EXPORT int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS);
421UDPC_EXPORT int UDPC_disable_threaded_update(UDPC_HContext ctx);
422
428UDPC_EXPORT int UDPC_is_valid_context(UDPC_HContext ctx);
429
439UDPC_EXPORT void UDPC_destroy(UDPC_HContext ctx);
440
460UDPC_EXPORT void UDPC_update(UDPC_HContext ctx);
461
473 UDPC_HContext ctx,
474 UDPC_ConnectionId connectionId,
475 int enableLibSodium);
476
495UDPC_EXPORT void UDPC_queue_send(UDPC_HContext ctx, UDPC_ConnectionId destinationId,
496 int isChecked, const void *data, uint32_t size);
497
514UDPC_EXPORT unsigned long UDPC_get_queue_send_current_size(UDPC_HContext ctx);
515
532UDPC_EXPORT unsigned long UDPC_get_queued_size(UDPC_HContext ctx, UDPC_ConnectionId id, int *exists);
533
542UDPC_EXPORT unsigned long UDPC_get_max_queued_size();
543
550UDPC_EXPORT int UDPC_set_accept_new_connections(UDPC_HContext ctx, int isAccepting);
551
563UDPC_EXPORT void UDPC_drop_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId, int dropAllWithAddr);
564
574UDPC_EXPORT int UDPC_has_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId);
575
590UDPC_EXPORT UDPC_ConnectionId* UDPC_get_list_connected(UDPC_HContext ctx, unsigned int *size);
591
597
609UDPC_EXPORT uint32_t UDPC_get_protocol_id(UDPC_HContext ctx);
610
621UDPC_EXPORT uint32_t UDPC_set_protocol_id(UDPC_HContext ctx, uint32_t id);
622
631UDPC_EXPORT UDPC_LoggingType UDPC_get_logging_type(UDPC_HContext ctx);
632
642UDPC_EXPORT UDPC_LoggingType UDPC_set_logging_type(UDPC_HContext ctx, UDPC_LoggingType loggingType);
643
652UDPC_EXPORT int UDPC_get_receiving_events(UDPC_HContext ctx);
653
663UDPC_EXPORT int UDPC_set_receiving_events(UDPC_HContext ctx, int isReceivingEvents);
664
676UDPC_EXPORT UDPC_Event UDPC_get_event(UDPC_HContext ctx, unsigned long *remaining);
677
685UDPC_EXPORT UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx, unsigned long *remaining);
686
694UDPC_EXPORT void UDPC_free_PacketInfo(UDPC_PacketInfo pInfo);
695
711UDPC_EXPORT void UDPC_free_PacketInfo_ptr(UDPC_PacketInfo *pInfoPtr);
712
730UDPC_EXPORT int UDPC_set_libsodium_keys(UDPC_HContext ctx, const unsigned char *sk, const unsigned char *pk);
731
745UDPC_EXPORT int UDPC_set_libsodium_key_easy(UDPC_HContext ctx, const unsigned char *sk);
746
755UDPC_EXPORT int UDPC_unset_libsodium_keys(UDPC_HContext ctx);
756
772UDPC_EXPORT int UDPC_add_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk);
773
782UDPC_EXPORT int UDPC_has_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk);
783
792UDPC_EXPORT int UDPC_remove_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk);
793
808UDPC_EXPORT int UDPC_clear_whitelist(UDPC_HContext ctx);
809
824UDPC_EXPORT int UDPC_get_auth_policy(UDPC_HContext ctx);
825
840UDPC_EXPORT int UDPC_set_auth_policy(UDPC_HContext ctx, int value);
841
846UDPC_EXPORT const char *UDPC_atostr_cid(UDPC_HContext ctx, UDPC_ConnectionId connectionId);
847
877UDPC_EXPORT const char *UDPC_atostr(UDPC_HContext ctx, UDPC_IPV6_ADDR_TYPE addr);
878
893UDPC_EXPORT const char *UDPC_atostr_unsafe(UDPC_IPV6_ADDR_TYPE addr);
894
907UDPC_EXPORT const char *UDPC_atostr_unsafe_cid(UDPC_ConnectionId cid);
908
912UDPC_EXPORT void UDPC_atostr_unsafe_free(const char *addrBuf);
913
925UDPC_EXPORT void UDPC_atostr_unsafe_free_ptr(const char **addrBuf);
926
927// There used to be a `UDPC_set_heartbeat_millis(...)` function here, but it
928// was determined that it would be better not to implement it. Details here:
929// https://github.com/Stephen-Seo/UDPConnection/issues/2
930
931// =============================================================================
932// Helpers
933
935UDPC_EXPORT UDPC_IPV6_ADDR_TYPE UDPC_strtoa(const char *addrStr);
936
937UDPC_EXPORT UDPC_IPV6_ADDR_TYPE UDPC_strtoa_link(const char *addrStr, uint32_t *linkId_out);
938
939UDPC_EXPORT UDPC_IPV6_ADDR_TYPE UDPC_a4toa6(uint32_t a4_be);
940
941UDPC_EXPORT int UDPC_is_big_endian();
942
983UDPC_EXPORT uint16_t UDPC_no16i(uint16_t i);
984
1025UDPC_EXPORT uint32_t UDPC_no32i(uint32_t i);
1026
1067UDPC_EXPORT uint64_t UDPC_no64i(uint64_t i);
1068
1100UDPC_EXPORT float UDPC_no32f(float f);
1101
1133UDPC_EXPORT double UDPC_no64f(double f);
1134
1135#ifdef __cplusplus
1136}
1137#endif
1138#endif
UDPC_EXPORT uint32_t UDPC_set_protocol_id(UDPC_HContext ctx, uint32_t id)
Sets the protocol id of the UDPC context.
UDPC_EXPORT uint16_t UDPC_no16i(uint16_t i)
Converts a 16-bit int into/from network byte order (big endian).
UDPC_EXPORT uint64_t UDPC_no64i(uint64_t i)
Converts a 64-bit int into/from network byte order (big endian).
UDPC_EXPORT int UDPC_clear_whitelist(UDPC_HContext ctx)
Clears the public key whitelist.
UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_anyaddr(uint16_t port)
Creates an UDPC_ConnectionId with the given port.
UDPC_EXPORT int UDPC_enable_threaded_update(UDPC_HContext ctx)
Enables auto updating on a separate thread for the given UDPC_HContext.
UDPC_EXPORT void UDPC_client_initiate_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId, int enableLibSodium)
Initiate a connection to a server peer.
UDPC_EXPORT const char * UDPC_atostr_unsafe_cid(UDPC_ConnectionId cid)
Similar to UPDC_atostr(), but the returned ptr must be free'd.
UDPC_EXPORT int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS)
Enables auto updating on a separate thread for the given UDPC_HContext with the specified update inte...
UDPC_EXPORT unsigned long UDPC_get_queued_size(UDPC_HContext ctx, UDPC_ConnectionId id, int *exists)
Gets the size of a connection's queue of queued packets.
enum UDPC_EXPORT UDPC_AuthPolicy UDPC_AuthPolicy
Definition UDPC.h:151
UDPC_INFO
Log errors, warnings, and info.
Definition UDPC.h:142
UDPC_EXPORT UDPC_IPV6_ADDR_TYPE UDPC_strtoa(const char *addrStr)
addrStr must be a valid ipv6 address or a valid ipv4 address
UDPC_EXPORT UDPC_Event UDPC_get_event(UDPC_HContext ctx, unsigned long *remaining)
Gets a recorded event.
UDPC_EXPORT void UDPC_atostr_unsafe_free_ptr(const char **addrBuf)
Free an addr string created with UDPC_atostr_unsafe() and zeroes the pointer.
UDPC_EXPORT int UDPC_set_accept_new_connections(UDPC_HContext ctx, int isAccepting)
Set whether or not the UDPC context will accept new connections.
UDPC_EXPORT int UDPC_set_receiving_events(UDPC_HContext ctx, int isReceivingEvents)
Sets whether or not UDPC will record events.
UDPC_EXPORT uint32_t UDPC_no32i(uint32_t i)
Converts a 32-bit int into/from network byte order (big endian).
UDPC_EXPORT int UDPC_disable_threaded_update(UDPC_HContext ctx)
Disables auto updating on a separate thread for the given UDPC_HContext.
UDPC_EXPORT void UDPC_drop_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId, int dropAllWithAddr)
Drops an existing connection to a peer.
UDPC_EXPORT int UDPC_has_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId)
Checks if a connection exists to the peer identified by the given connectionId.
UDPC_EXPORT const char * UDPC_atostr_cid(UDPC_HContext ctx, UDPC_ConnectionId connectionId)
Returns the result of UDPC_atostr() with the addr data inside the given UDPC_ConnectionId instance.
UDPC_EXPORT void UDPC_atostr_unsafe_free(const char *addrBuf)
Free an addr string created with UDPC_atostr_unsafe().
UDPC_EXPORT UDPC_HContext UDPC_init_threaded_update(UDPC_ConnectionId listenId, int isClient, int isUsingLibsodium)
Creates an UDPC_HContext that holds state for connections that auto-updates via a thread.
UDPC_EXPORT float UDPC_no32f(float f)
Converts a 32-bit float into/from network byte order (big endian).
UDPC_EXPORT int UDPC_unset_libsodium_keys(UDPC_HContext ctx)
Removes set keys if any used for packet verification.
UDPC_EXPORT void UDPC_free_PacketInfo(UDPC_PacketInfo pInfo)
Frees a UDPC_PacketInfo.
UDPC_EXPORT void UDPC_update(UDPC_HContext ctx)
Updates the context.
UDPC_AUTH_POLICY_STRICT
Only peers with public key verification will be allowed.
Definition UDPC.h:155
UDPC_EXPORT UDPC_ConnectionId * UDPC_get_list_connected(UDPC_HContext ctx, unsigned int *size)
Gets a dynamically allocated array of connected peers' identifiers.
UDPC_EXPORT UDPC_LoggingType UDPC_set_logging_type(UDPC_HContext ctx, UDPC_LoggingType loggingType)
Sets the logging type of the UDPC context.
UDPC_EXPORT int UDPC_add_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk)
Adds a public key to the whitelist.
UDPC_EXPORT void UDPC_queue_send(UDPC_HContext ctx, UDPC_ConnectionId destinationId, int isChecked, const void *data, uint32_t size)
Queues a packet to be sent to the specified peer.
UDPC_EXPORT unsigned long UDPC_get_max_queued_size()
Gets the size limit of a connection's queue of queued packets.
struct UDPC_EXPORT UDPC_Event UDPC_Event
A struct containing information related to the type of event.
UDPC_EXPORT void UDPC_free_list_connected(UDPC_ConnectionId *list)
Cleans up a dynamically allocated array of connected peers' identifiers.
UDPC_EXPORT UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient, int isUsingLibsodium)
Creates an UDPC_HContext that holds state for connections.
UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_easy(const char *addrString, uint16_t port)
Creates an UDPC_ConnectionId with the given addr string and port.
UDPC_EXPORT int UDPC_get_receiving_events(UDPC_HContext ctx)
Returns non-zero if the UDPC context will record events.
UDPC_EXPORT int UDPC_set_auth_policy(UDPC_HContext ctx, int value)
Sets how peers are handled regarding public key verification.
UDPC_EXPORT const char * UDPC_atostr_unsafe(UDPC_IPV6_ADDR_TYPE addr)
Similar to UPDC_atostr(), but the returned ptr must be free'd.
UDPC_EXPORT double UDPC_no64f(double f)
Converts a 64-bit float into/from network byte order (big endian).
UDPC_EXPORT int UDPC_has_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk)
Checks if a public key is in the whitelist.
UDPC_AUTH_POLICY_FALLBACK
All peers will not be denied regardless of use of public key verification.
Definition UDPC.h:153
UDPC_WARNING
Log errors and warnings.
Definition UDPC.h:140
UDPC_EXPORT const char * UDPC_atostr(UDPC_HContext ctx, UDPC_IPV6_ADDR_TYPE addr)
Returns a pointer to a null-terminated address string derived from the given address.
UDPC_EXPORT unsigned long UDPC_get_queue_send_current_size(UDPC_HContext ctx)
Gets the size of the data structure holding queued packets.
UDPC_SILENT
Does not log anything.
Definition UDPC.h:136
UDPC_EXPORT uint32_t UDPC_get_protocol_id(UDPC_HContext ctx)
Gets the protocol id of the UDPC context.
UDPC_EXPORT void UDPC_destroy(UDPC_HContext ctx)
Cleans up the UDPC_HContext.
UDPC_EXPORT int UDPC_get_auth_policy(UDPC_HContext ctx)
Gets how peers are handled regarding public key verification.
UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_full(UDPC_IPV6_ADDR_TYPE addr, uint32_t scope_id, uint16_t port)
Creates an UDPC_ConnectionId with the given addr, scope_id, and port.
UDPC_EXPORT UDPC_HContext UDPC_init_threaded_update_ms(UDPC_ConnectionId listenId, int isClient, int updateMS, int isUsingLibsodium)
Creates an UDPC_HContext that holds state for connections that auto-updates via a thread at a specifi...
UDPC_EXPORT int UDPC_remove_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk)
Removes a public key from the whitelist.
UDPC_EXPORT int UDPC_set_libsodium_keys(UDPC_HContext ctx, const unsigned char *sk, const unsigned char *pk)
Sets public/private keys used for packet verification.
UDPC_EXPORT int UDPC_is_valid_context(UDPC_HContext ctx)
Checks if the given UDPC_HContext is valid (successfully initialized)
UDPC_ERROR
Only log errors.
Definition UDPC.h:138
UDPC_EXPORT UDPC_LoggingType UDPC_get_logging_type(UDPC_HContext ctx)
Gets the logging type of the UDPC context.
UDPC_VERBOSE
Log errors, warning, info, and verbose.
Definition UDPC.h:144
UDPC_EXPORT void UDPC_free_PacketInfo_ptr(UDPC_PacketInfo *pInfoPtr)
Frees a UDPC_PacketInfo.
UDPC_EXPORT int UDPC_set_libsodium_key_easy(UDPC_HContext ctx, const unsigned char *sk)
Sets the public/private keys used for packet verification.
enum UDPC_EXPORT UDPC_EventType UDPC_EventType
An enum describing the type of event.
Definition UDPC.h:245
struct UDPC_EXPORT UDPC_PacketInfo UDPC_PacketInfo
Data representing a received/sent packet.
struct UDPC_EXPORT UDPC_ConnectionId UDPC_ConnectionId
Data identifying a peer via addr, port, and scope_id.
UDPC_EXPORT UDPC_ConnectionId UDPC_create_id(UDPC_IPV6_ADDR_TYPE addr, uint16_t port)
Creates an UDPC_ConnectionId with the given addr and port.
UDPC_EXPORT UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx, unsigned long *remaining)
Get a received packet from a given UDPC context.
Data identifying a peer via addr, port, and scope_id.
Definition UDPC.h:168
A struct containing information related to the type of event.
Definition UDPC.h:266
Data representing a received/sent packet.
Definition UDPC.h:183
char * data
Definition UDPC.h:189
UDPC_ConnectionId sender
The UDPC_ConnectionId of the sender.
Definition UDPC.h:221
uint16_t dataSize
The size in bytes of the received packet's data inside the data pointer member variable.
Definition UDPC.h:218
UDPC_ConnectionId receiver
The UDPC_ConnectionId of the receiver.
Definition UDPC.h:223
uint32_t id
The packet's id.
Definition UDPC.h:208
uint32_t flags
Flags indication some additional information about the received packet.
Definition UDPC.h:200
Definition UDPC.h:269