UDPConnection
A network connection implementation over UDP
Loading...
Searching...
No Matches
UDPC.h
Go to the documentation of this file.
1
17#ifndef UDPC_CONNECTION_H
18#define UDPC_CONNECTION_H
19
20#ifndef DOXYGEN_SHOULD_SKIP_THIS
21
22// Determine platform macros
23# define UDPC_PLATFORM_WINDOWS 1
24# define UDPC_PLATFORM_MAC 2
25# define UDPC_PLATFORM_LINUX 3
26# define UDPC_PLATFORM_UNKNOWN 0
27
28# if defined _WIN32
29# define UDPC_PLATFORM UDPC_PLATFORM_WINDOWS
30# elif defined __APPLE__
31# define UDPC_PLATFORM UDPC_PLATFORM_MAC
32# elif defined __linux__
33# define UDPC_PLATFORM UDPC_PLATFORM_LINUX
34# else
35# define UDPC_PLATFORM UDPC_PLATFORM_UNKNOWN
36# endif
37
38#endif // DOXYGEN_SHOULD_SKIP_THIS
39
40// OS-based networking macros
41#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
42# include <winsock2.h>
43# ifdef UDPC_PLATFORM_MINGW
44# include <ws2ipdef.h>
45# include <in6addr.h>
46# else
47# include <Ws2ipdef.h>
48# include <In6addr.h>
49# endif
50
51# ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53# define UDPC_CLEANUPSOCKET(x) closesocket(x)
54# define UDPC_SOCKETTYPE SOCKET
55# define UDPC_IPV6_SOCKADDR_TYPE SOCKADDR_IN6
56# define UDPC_IPV6_ADDR_TYPE IN6_ADDR
57# define UDPC_IPV6_ADDR_SUB(addr) addr.u.Byte
58# define UDPC_SOCKET_RETURN_ERROR(socket) (socket == INVALID_SOCKET)
59
60# endif // DOXYGEN_SHOULD_SKIP_THIS
61
62#elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
63# include <fcntl.h>
64# include <netinet/in.h>
65# include <sys/socket.h>
66# include <unistd.h>
67
68# ifndef DOXYGEN_SHOULD_SKIP_THIS
69
70# define UDPC_CLEANUPSOCKET(x) close(x)
71# define UDPC_SOCKETTYPE int
72# define UDPC_IPV6_SOCKADDR_TYPE struct sockaddr_in6
73# define UDPC_IPV6_ADDR_TYPE struct in6_addr
74# define UDPC_IPV6_ADDR_SUB(addr) addr.s6_addr
75# define UDPC_SOCKET_RETURN_ERROR(socket) (socket <= 0)
76
77# endif // DOXYGEN_SHOULD_SKIP_THIS
78
79#else
80# ifndef DOXYGEN_SHOULD_SKIP_THIS
81# define UDPC_CLEANUPSOCKET(x) ((void)0)
82# endif // DOXYGEN_SHOULD_SKIP_THIS
83#endif
84
85// other defines
87#define UDPC_PACKET_MAX_SIZE 8192
88#define UDPC_DEFAULT_PROTOCOL_ID 1357924680 // 0x50f04948
89
90#ifndef DOXYGEN_SHOULD_SKIP_THIS
91
92// other defines continued
93
94# ifndef UDPC_LIBSODIUM_ENABLED
95# ifndef crypto_sign_PUBLICKEYBYTES
96# define crypto_sign_PUBLICKEYBYTES 1
97# endif
98# ifndef crypto_sign_SECRETKEYBYTES
99# define crypto_sign_SECRETKEYBYTES 1
100# endif
101# ifndef crypto_sign_BYTES
102# define crypto_sign_BYTES 1
103# endif
104# endif
105
106# if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
107# define UDPC_EXPORT __declspec(dllexport)
108# else
109# define UDPC_EXPORT
110# endif
111
112#endif // DOXYGEN_SHOULD_SKIP_THIS
113
114#ifdef __cplusplus
115# include <cstdint>
116extern "C" {
117#else
118# include <stdint.h>
119#endif
120
122struct UDPC_Context;
123typedef struct UDPC_Context *UDPC_HContext;
124
125typedef enum UDPC_EXPORT UDPC_LoggingType {
137 UDPC_DEBUG
138} UDPC_LoggingType;
139
142typedef enum UDPC_EXPORT UDPC_AuthPolicy {
147 // Used internally to get max size of enum
148 UDPC_AUTH_POLICY_SIZE
150
159typedef struct UDPC_EXPORT UDPC_ConnectionId {
160 UDPC_IPV6_ADDR_TYPE addr;
161 uint32_t scope_id;
162 uint16_t port;
164
174typedef struct UDPC_EXPORT UDPC_PacketInfo {
179 // id is stored at offset 8, size 4 (uint32_t) even for "empty" PktInfos
180 char *data;
191 uint32_t flags;
199 uint32_t id;
209 uint16_t dataSize;
210 uint16_t rtt;
216
236typedef enum UDPC_EXPORT UDPC_EventType {
237 UDPC_ET_NONE,
238 UDPC_ET_REQUEST_CONNECT,
239 UDPC_ET_REQUEST_DISCONNECT,
240 UDPC_ET_CONNECTED,
241 UDPC_ET_DISCONNECTED,
242 UDPC_ET_FAIL_CONNECT,
243 UDPC_ET_GOOD_MODE,
244 UDPC_ET_BAD_MODE
246
257typedef struct UDPC_EXPORT UDPC_Event {
258 UDPC_EventType type;
259 UDPC_ConnectionId conId;
260 union Value {
261 int dropAllWithAddr;
262 int enableLibSodium;
263 } v;
265
276UDPC_EXPORT UDPC_ConnectionId UDPC_create_id(UDPC_IPV6_ADDR_TYPE addr, uint16_t port);
277
285UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_full(UDPC_IPV6_ADDR_TYPE addr, uint32_t scope_id, uint16_t port);
286
296UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_anyaddr(uint16_t port);
297
308UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_easy(const char *addrString, uint16_t port);
309
310UDPC_EXPORT UDPC_ConnectionId UDPC_create_id_hostname(const char *hostname, uint16_t port);
311
334UDPC_EXPORT UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient, int isUsingLibsodium);
354UDPC_EXPORT UDPC_HContext UDPC_init_threaded_update(
355 UDPC_ConnectionId listenId,
356 int isClient,
357 int isUsingLibsodium);
377UDPC_EXPORT UDPC_HContext UDPC_init_threaded_update_ms(
378 UDPC_ConnectionId listenId,
379 int isClient,
380 int updateMS,
381 int isUsingLibsodium);
382
392UDPC_EXPORT int UDPC_enable_threaded_update(UDPC_HContext ctx);
403UDPC_EXPORT int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS);
412UDPC_EXPORT int UDPC_disable_threaded_update(UDPC_HContext ctx);
413
419UDPC_EXPORT int UDPC_is_valid_context(UDPC_HContext ctx);
420
430UDPC_EXPORT void UDPC_destroy(UDPC_HContext ctx);
431
451UDPC_EXPORT void UDPC_update(UDPC_HContext ctx);
452
464 UDPC_HContext ctx,
465 UDPC_ConnectionId connectionId,
466 int enableLibSodium);
467
486UDPC_EXPORT void UDPC_queue_send(UDPC_HContext ctx, UDPC_ConnectionId destinationId,
487 int isChecked, const void *data, uint32_t size);
488
505UDPC_EXPORT unsigned long UDPC_get_queue_send_current_size(UDPC_HContext ctx);
506
523UDPC_EXPORT unsigned long UDPC_get_queued_size(UDPC_HContext ctx, UDPC_ConnectionId id, int *exists);
524
533UDPC_EXPORT unsigned long UDPC_get_max_queued_size();
534
541UDPC_EXPORT int UDPC_set_accept_new_connections(UDPC_HContext ctx, int isAccepting);
542
554UDPC_EXPORT void UDPC_drop_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId, int dropAllWithAddr);
555
565UDPC_EXPORT int UDPC_has_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId);
566
581UDPC_EXPORT UDPC_ConnectionId* UDPC_get_list_connected(UDPC_HContext ctx, unsigned int *size);
582
588
600UDPC_EXPORT uint32_t UDPC_get_protocol_id(UDPC_HContext ctx);
601
612UDPC_EXPORT uint32_t UDPC_set_protocol_id(UDPC_HContext ctx, uint32_t id);
613
622UDPC_EXPORT UDPC_LoggingType UDPC_get_logging_type(UDPC_HContext ctx);
623
633UDPC_EXPORT UDPC_LoggingType UDPC_set_logging_type(UDPC_HContext ctx, UDPC_LoggingType loggingType);
634
643UDPC_EXPORT int UDPC_get_receiving_events(UDPC_HContext ctx);
644
654UDPC_EXPORT int UDPC_set_receiving_events(UDPC_HContext ctx, int isReceivingEvents);
655
667UDPC_EXPORT UDPC_Event UDPC_get_event(UDPC_HContext ctx, unsigned long *remaining);
668
676UDPC_EXPORT UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx, unsigned long *remaining);
677
685UDPC_EXPORT void UDPC_free_PacketInfo(UDPC_PacketInfo pInfo);
686
702UDPC_EXPORT void UDPC_free_PacketInfo_ptr(UDPC_PacketInfo *pInfoPtr);
703
721UDPC_EXPORT int UDPC_set_libsodium_keys(UDPC_HContext ctx, const unsigned char *sk, const unsigned char *pk);
722
736UDPC_EXPORT int UDPC_set_libsodium_key_easy(UDPC_HContext ctx, const unsigned char *sk);
737
746UDPC_EXPORT int UDPC_unset_libsodium_keys(UDPC_HContext ctx);
747
763UDPC_EXPORT int UDPC_add_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk);
764
773UDPC_EXPORT int UDPC_has_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk);
774
783UDPC_EXPORT int UDPC_remove_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk);
784
799UDPC_EXPORT int UDPC_clear_whitelist(UDPC_HContext ctx);
800
815UDPC_EXPORT int UDPC_get_auth_policy(UDPC_HContext ctx);
816
831UDPC_EXPORT int UDPC_set_auth_policy(UDPC_HContext ctx, int value);
832
837UDPC_EXPORT const char *UDPC_atostr_cid(UDPC_HContext ctx, UDPC_ConnectionId connectionId);
838
868UDPC_EXPORT const char *UDPC_atostr(UDPC_HContext ctx, UDPC_IPV6_ADDR_TYPE addr);
869
884UDPC_EXPORT const char *UDPC_atostr_unsafe(UDPC_IPV6_ADDR_TYPE addr);
885
898UDPC_EXPORT const char *UDPC_atostr_unsafe_cid(UDPC_ConnectionId cid);
899
903UDPC_EXPORT void UDPC_atostr_unsafe_free(const char *addrBuf);
904
916UDPC_EXPORT void UDPC_atostr_unsafe_free_ptr(const char **addrBuf);
917
918// =============================================================================
919// Helpers
920
922UDPC_EXPORT UDPC_IPV6_ADDR_TYPE UDPC_strtoa(const char *addrStr);
923
924UDPC_EXPORT UDPC_IPV6_ADDR_TYPE UDPC_strtoa_link(const char *addrStr, uint32_t *linkId_out);
925
926UDPC_EXPORT UDPC_IPV6_ADDR_TYPE UDPC_a4toa6(uint32_t a4_be);
927
928UDPC_EXPORT int UDPC_is_big_endian();
929
970UDPC_EXPORT uint16_t UDPC_no16i(uint16_t i);
971
1012UDPC_EXPORT uint32_t UDPC_no32i(uint32_t i);
1013
1054UDPC_EXPORT uint64_t UDPC_no64i(uint64_t i);
1055
1087UDPC_EXPORT float UDPC_no32f(float f);
1088
1120UDPC_EXPORT double UDPC_no64f(double f);
1121
1122#ifdef __cplusplus
1123}
1124#endif
1125#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:142
UDPC_INFO
Log errors, warnings, and info.
Definition: UDPC.h:133
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:146
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.
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:144
UDPC_WARNING
Log errors and warnings.
Definition: UDPC.h:131
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:127
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:129
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:135
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:236
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:159
A struct containing information related to the type of event.
Definition: UDPC.h:257
Data representing a received/sent packet.
Definition: UDPC.h:174
char * data
Definition: UDPC.h:180
UDPC_ConnectionId sender
The UDPC_ConnectionId of the sender.
Definition: UDPC.h:212
uint16_t dataSize
The size in bytes of the received packet's data inside the data pointer member variable.
Definition: UDPC.h:209
UDPC_ConnectionId receiver
The UDPC_ConnectionId of the receiver.
Definition: UDPC.h:214
uint32_t id
The packet's id.
Definition: UDPC.h:199
uint32_t flags
Flags indication some additional information about the received packet.
Definition: UDPC.h:191
Definition: UDPC.h:260