2019-06-06 07:02:48 +00:00
|
|
|
#ifndef UDPC_CONNECTION_H
|
|
|
|
#define UDPC_CONNECTION_H
|
|
|
|
|
|
|
|
// Determine platform macros
|
|
|
|
#define UDPC_PLATFORM_WINDOWS 1
|
|
|
|
#define UDPC_PLATFORM_MAC 2
|
|
|
|
#define UDPC_PLATFORM_LINUX 3
|
|
|
|
#define UDPC_PLATFORM_UNKNOWN 0
|
|
|
|
|
|
|
|
#if defined _WIN32
|
2019-06-06 07:42:07 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_WINDOWS
|
2019-06-06 07:02:48 +00:00
|
|
|
#elif defined __APPLE__
|
2019-06-06 07:42:07 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_MAC
|
2019-06-06 07:02:48 +00:00
|
|
|
#elif defined __linux__
|
2019-06-06 07:42:07 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_LINUX
|
2019-06-06 07:02:48 +00:00
|
|
|
#else
|
2019-06-06 07:42:07 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_UNKNOWN
|
2019-06-06 07:02:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// OS-based networking macros
|
|
|
|
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
|
2019-06-06 07:42:07 +00:00
|
|
|
#include <winsock2.h>
|
2019-09-21 03:15:49 +00:00
|
|
|
# ifdef UDPC_PLATFORM_MINGW
|
|
|
|
# include <ws2ipdef.h>
|
|
|
|
# include <in6addr.h>
|
|
|
|
# else
|
|
|
|
# include <Ws2ipdef.h>
|
|
|
|
# include <In6addr.h>
|
|
|
|
# endif
|
2019-09-19 03:23:15 +00:00
|
|
|
|
|
|
|
#define UDPC_CLEANUPSOCKET(x) closesocket(x)
|
|
|
|
#define UDPC_SOCKETTYPE SOCKET
|
|
|
|
#define UDPC_IPV6_SOCKADDR_TYPE SOCKADDR_IN6
|
|
|
|
#define UDPC_IPV6_ADDR_TYPE IN6_ADDR
|
|
|
|
#define UDPC_IPV6_ADDR_SUB(addr) addr.u.Byte
|
|
|
|
#define UDPC_SOCKET_RETURN_ERROR(socket) (socket == INVALID_SOCKET)
|
2019-06-06 07:02:48 +00:00
|
|
|
#elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
|
2019-06-06 07:42:07 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-09-19 03:23:15 +00:00
|
|
|
#define UDPC_CLEANUPSOCKET(x) close(x)
|
|
|
|
#define UDPC_SOCKETTYPE int
|
|
|
|
#define UDPC_IPV6_SOCKADDR_TYPE struct sockaddr_in6
|
|
|
|
#define UDPC_IPV6_ADDR_TYPE struct in6_addr
|
|
|
|
#define UDPC_IPV6_ADDR_SUB(addr) addr.s6_addr
|
|
|
|
#define UDPC_SOCKET_RETURN_ERROR(socket) (socket <= 0)
|
2019-06-06 07:02:48 +00:00
|
|
|
#else
|
2019-09-19 03:23:15 +00:00
|
|
|
#define UDPC_CLEANUPSOCKET(x) ((void)0)
|
2019-06-06 07:02:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// other defines
|
|
|
|
#define UDPC_PACKET_MAX_SIZE 8192
|
2019-09-17 11:33:47 +00:00
|
|
|
#define UDPC_DEFAULT_PROTOCOL_ID 1357924680 // 0x50f04948
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-11-18 08:37:03 +00:00
|
|
|
#ifndef UDPC_LIBSODIUM_ENABLED
|
|
|
|
# define crypto_sign_PUBLICKEYBYTES 1
|
|
|
|
# define crypto_sign_SECRETKEYBYTES 1
|
|
|
|
# define crypto_sign_BYTES 1
|
|
|
|
#endif
|
|
|
|
|
2019-06-06 07:02:48 +00:00
|
|
|
#ifdef __cplusplus
|
2019-06-06 07:42:07 +00:00
|
|
|
#include <cstdint>
|
2019-06-06 07:02:48 +00:00
|
|
|
extern "C" {
|
|
|
|
#else
|
2019-06-06 07:42:07 +00:00
|
|
|
#include <stdint.h>
|
2019-06-06 07:02:48 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
// Opaque struct handle to Context
|
|
|
|
struct UDPC_Context;
|
2019-08-30 03:11:07 +00:00
|
|
|
typedef struct UDPC_Context *UDPC_HContext;
|
2019-08-30 03:03:26 +00:00
|
|
|
|
2019-11-06 05:35:16 +00:00
|
|
|
typedef enum { UDPC_SILENT, UDPC_ERROR, UDPC_WARNING, UDPC_INFO, UDPC_VERBOSE, UDPC_DEBUG } UDPC_LoggingType;
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-09-03 03:06:46 +00:00
|
|
|
typedef struct {
|
2019-09-19 03:23:15 +00:00
|
|
|
UDPC_IPV6_ADDR_TYPE addr;
|
2019-09-18 08:35:14 +00:00
|
|
|
uint32_t scope_id;
|
2019-09-03 03:06:46 +00:00
|
|
|
uint16_t port;
|
|
|
|
} UDPC_ConnectionId;
|
|
|
|
|
2019-06-06 07:02:48 +00:00
|
|
|
typedef struct {
|
2019-08-22 11:16:07 +00:00
|
|
|
// id is stored at offset 8, size 4 (uint32_t) even for "empty" PktInfos
|
2019-06-06 07:02:48 +00:00
|
|
|
char data[UDPC_PACKET_MAX_SIZE];
|
2019-07-25 11:51:08 +00:00
|
|
|
/*
|
|
|
|
* 0x1 - connect
|
|
|
|
* 0x2 - ping
|
|
|
|
* 0x4 - no_rec_chk
|
|
|
|
* 0x8 - resending
|
|
|
|
*/
|
|
|
|
uint32_t flags;
|
2019-06-06 07:02:48 +00:00
|
|
|
uint16_t dataSize; // zero if invalid
|
2019-09-03 03:06:46 +00:00
|
|
|
UDPC_ConnectionId sender;
|
|
|
|
UDPC_ConnectionId receiver;
|
2019-07-25 11:51:08 +00:00
|
|
|
} UDPC_PacketInfo;
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-11-11 07:08:51 +00:00
|
|
|
typedef enum {
|
|
|
|
UDPC_ET_NONE,
|
|
|
|
UDPC_ET_REQUEST_CONNECT,
|
|
|
|
UDPC_ET_REQUEST_DISCONNECT,
|
|
|
|
UDPC_ET_CONNECTED,
|
|
|
|
UDPC_ET_DISCONNECTED,
|
|
|
|
UDPC_ET_GOOD_MODE,
|
2019-11-19 11:55:20 +00:00
|
|
|
UDPC_ET_BAD_MODE,
|
|
|
|
UDPC_ET_REQUEST_CONNECT_PK
|
2019-11-11 07:08:51 +00:00
|
|
|
} UDPC_EventType;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
UDPC_EventType type;
|
|
|
|
UDPC_ConnectionId conId;
|
2019-11-18 08:37:03 +00:00
|
|
|
union Value {
|
|
|
|
int dropAllWithAddr;
|
|
|
|
int enableLibSodium;
|
2019-11-19 11:55:20 +00:00
|
|
|
unsigned char *pk;
|
2019-11-18 08:37:03 +00:00
|
|
|
} v;
|
2019-11-11 07:08:51 +00:00
|
|
|
} UDPC_Event;
|
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
/// port should be in native byte order (not network/big-endian)
|
2019-09-19 03:23:15 +00:00
|
|
|
UDPC_ConnectionId UDPC_create_id(UDPC_IPV6_ADDR_TYPE addr, uint16_t port);
|
2019-09-03 03:06:46 +00:00
|
|
|
|
2019-09-19 03:23:15 +00:00
|
|
|
UDPC_ConnectionId UDPC_create_id_full(UDPC_IPV6_ADDR_TYPE addr, uint32_t scope_id, uint16_t port);
|
2019-09-18 08:35:14 +00:00
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
UDPC_ConnectionId UDPC_create_id_anyaddr(uint16_t port);
|
|
|
|
|
2019-11-13 03:15:12 +00:00
|
|
|
UDPC_ConnectionId UDPC_create_id_easy(const char *addrString, uint16_t port);
|
|
|
|
|
2019-11-18 08:37:03 +00:00
|
|
|
UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient, int isUsingLibsodium);
|
2019-11-13 05:06:48 +00:00
|
|
|
UDPC_HContext UDPC_init_threaded_update(
|
|
|
|
UDPC_ConnectionId listenId,
|
2019-11-18 08:37:03 +00:00
|
|
|
int isClient,
|
|
|
|
int isUsingLibsodium);
|
2019-11-13 05:06:48 +00:00
|
|
|
UDPC_HContext UDPC_init_threaded_update_ms(
|
|
|
|
UDPC_ConnectionId listenId,
|
|
|
|
int isClient,
|
2019-11-18 08:37:03 +00:00
|
|
|
int updateMS,
|
|
|
|
int isUsingLibsodium);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-11-27 10:47:51 +00:00
|
|
|
int UDPC_enable_threaded_update(UDPC_HContext ctx);
|
|
|
|
int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS);
|
|
|
|
int UDPC_disable_threaded_update(UDPC_HContext ctx);
|
|
|
|
|
|
|
|
int UDPC_is_valid_context(UDPC_HContext ctx);
|
2019-11-19 11:55:20 +00:00
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
void UDPC_destroy(UDPC_HContext ctx);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
void UDPC_update(UDPC_HContext ctx);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-11-19 11:55:20 +00:00
|
|
|
void UDPC_client_initiate_connection(
|
|
|
|
UDPC_HContext ctx,
|
|
|
|
UDPC_ConnectionId connectionId,
|
|
|
|
int enableLibSodium);
|
|
|
|
|
|
|
|
void UDPC_client_initiate_connection_pk(
|
|
|
|
UDPC_HContext ctx,
|
|
|
|
UDPC_ConnectionId connectionId,
|
|
|
|
unsigned char *serverPK);
|
2019-08-29 02:07:24 +00:00
|
|
|
|
2019-09-07 07:36:11 +00:00
|
|
|
void UDPC_queue_send(UDPC_HContext ctx, UDPC_ConnectionId destinationId,
|
2019-09-20 05:01:26 +00:00
|
|
|
int isChecked, void *data, uint32_t size);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-11-06 05:35:16 +00:00
|
|
|
unsigned long UDPC_get_queue_send_current_size(UDPC_HContext ctx);
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
int UDPC_set_accept_new_connections(UDPC_HContext ctx, int isAccepting);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-11-11 07:08:51 +00:00
|
|
|
void UDPC_drop_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId, int dropAllWithAddr);
|
2019-08-28 07:38:14 +00:00
|
|
|
|
2019-09-17 11:33:47 +00:00
|
|
|
int UDPC_has_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId);
|
|
|
|
|
2019-09-20 05:01:26 +00:00
|
|
|
UDPC_ConnectionId* UDPC_get_list_connected(UDPC_HContext ctx, unsigned int *size);
|
|
|
|
|
|
|
|
void UDPC_free_list_connected(UDPC_ConnectionId *list);
|
|
|
|
|
2019-11-11 07:08:51 +00:00
|
|
|
uint32_t UDPC_get_protocol_id(UDPC_HContext ctx);
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
uint32_t UDPC_set_protocol_id(UDPC_HContext ctx, uint32_t id);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-11-11 07:08:51 +00:00
|
|
|
UDPC_LoggingType UDPC_get_logging_type(UDPC_HContext ctx);
|
|
|
|
|
2019-09-17 11:33:47 +00:00
|
|
|
UDPC_LoggingType UDPC_set_logging_type(UDPC_HContext ctx, UDPC_LoggingType loggingType);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-11-11 07:08:51 +00:00
|
|
|
int UPDC_get_receiving_events(UDPC_HContext ctx);
|
|
|
|
|
|
|
|
int UDPC_set_receiving_events(UDPC_HContext ctx, int isReceivingEvents);
|
|
|
|
|
|
|
|
UDPC_Event UDPC_get_event(UDPC_HContext ctx, unsigned long *remaining);
|
|
|
|
|
2019-11-06 05:35:16 +00:00
|
|
|
UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx, unsigned long *remaining);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-11-27 10:41:38 +00:00
|
|
|
int UDPC_set_libsodium_keys(UDPC_HContext ctx, unsigned char *sk, unsigned char *pk);
|
|
|
|
|
|
|
|
int UDPC_set_libsodium_key_easy(UDPC_HContext ctx, unsigned char *sk);
|
2019-11-19 11:55:20 +00:00
|
|
|
|
2019-11-27 11:12:57 +00:00
|
|
|
int UDPC_unset_libsodium_keys(UDPC_HContext ctx);
|
2019-11-21 03:23:40 +00:00
|
|
|
|
2019-09-18 08:35:14 +00:00
|
|
|
const char *UDPC_atostr_cid(UDPC_HContext ctx, UDPC_ConnectionId connectionId);
|
|
|
|
|
2019-09-19 03:23:15 +00:00
|
|
|
const char *UDPC_atostr(UDPC_HContext ctx, UDPC_IPV6_ADDR_TYPE addr);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-09-17 08:17:16 +00:00
|
|
|
/// addrStr must be a valid ipv6 address or a valid ipv4 address
|
2019-09-19 03:23:15 +00:00
|
|
|
UDPC_IPV6_ADDR_TYPE UDPC_strtoa(const char *addrStr);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-09-19 03:23:15 +00:00
|
|
|
UDPC_IPV6_ADDR_TYPE UDPC_strtoa_link(const char *addrStr, uint32_t *linkId_out);
|
2019-09-18 08:35:14 +00:00
|
|
|
|
2019-06-06 07:02:48 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|