26#ifndef UDPC_CONNECTION_H
27#define UDPC_CONNECTION_H
29#ifndef DOXYGEN_SHOULD_SKIP_THIS
32# define UDPC_PLATFORM_WINDOWS 1
33# define UDPC_PLATFORM_MAC 2
34# define UDPC_PLATFORM_LINUX 3
35# define UDPC_PLATFORM_UNKNOWN 0
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
44# define UDPC_PLATFORM UDPC_PLATFORM_UNKNOWN
50#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
52# ifdef UDPC_PLATFORM_MINGW
60# ifndef DOXYGEN_SHOULD_SKIP_THIS
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)
71#elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
73# include <netinet/in.h>
74# include <sys/socket.h>
77# ifndef DOXYGEN_SHOULD_SKIP_THIS
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)
89# ifndef DOXYGEN_SHOULD_SKIP_THIS
90# define UDPC_CLEANUPSOCKET(x) ((void)0)
96#define UDPC_PACKET_MAX_SIZE 8192
97#define UDPC_DEFAULT_PROTOCOL_ID 1357924680
99#ifndef DOXYGEN_SHOULD_SKIP_THIS
103# ifndef UDPC_LIBSODIUM_ENABLED
104# ifndef crypto_sign_PUBLICKEYBYTES
105# define crypto_sign_PUBLICKEYBYTES 1
107# ifndef crypto_sign_SECRETKEYBYTES
108# define crypto_sign_SECRETKEYBYTES 1
110# ifndef crypto_sign_BYTES
111# define crypto_sign_BYTES 1
115# if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
116# define UDPC_EXPORT __declspec(dllexport)
132typedef struct UDPC_Context *UDPC_HContext;
134typedef enum UDPC_EXPORT UDPC_LoggingType {
157 UDPC_AUTH_POLICY_SIZE
169 UDPC_IPV6_ADDR_TYPE addr;
247 UDPC_ET_REQUEST_CONNECT,
248 UDPC_ET_REQUEST_DISCONNECT,
250 UDPC_ET_DISCONNECTED,
251 UDPC_ET_FAIL_CONNECT,
319UDPC_EXPORT
UDPC_ConnectionId UDPC_create_id_hostname(
const char *hostname, uint16_t port);
366 int isUsingLibsodium);
390 int isUsingLibsodium);
475 int enableLibSodium);
496 int isChecked,
const void *data, uint32_t size);
877UDPC_EXPORT
const char *
UDPC_atostr(UDPC_HContext ctx, UDPC_IPV6_ADDR_TYPE addr);
937UDPC_EXPORT UDPC_IPV6_ADDR_TYPE UDPC_strtoa_link(
const char *addrStr, uint32_t *linkId_out);
939UDPC_EXPORT UDPC_IPV6_ADDR_TYPE UDPC_a4toa6(uint32_t a4_be);
941UDPC_EXPORT
int UDPC_is_big_endian();
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