Add dlibc, start context work, updated public API
Added vector.h from dlibc (MIT license). context.h/.c only implemented one thing for now, which was a pain for some reason because I'm tired and couldn't figure out a basic damn pointer. Will have to test for stupidity later.
This commit is contained in:
+125
-69
@@ -19,10 +19,14 @@ extern "C" {
|
||||
#define BRAVAIS_VERSION ((BRAVAIS_VERSION_MAJOR << 16) | (BRAVAIS_VERSION_MINOR << 8) | BRAVAIS_VERSION_PATCH)
|
||||
|
||||
#define BRAVAIS_NO_TIMEOUT UINT64_MAX
|
||||
#define BRAVAIS_NO_CONN UINT64_C(0) // Never a valid connection ID
|
||||
|
||||
#define BRAVAIS_MAX_DATAGRAM_SIZE 1500
|
||||
#define BRAVAIS_MAX_PLAINTEXT_SIZE 65535
|
||||
|
||||
// The main header has this, everything else is in the internal one
|
||||
#define BRAVAIS_X25519_KEY_LENGTH 32 // For X25519 private and public
|
||||
|
||||
typedef enum {
|
||||
// Error codes; Generally <0 = error, 0 = success
|
||||
BRAVAIS_EMPTY = 1, // Nothing to emit right now
|
||||
@@ -34,16 +38,14 @@ typedef enum {
|
||||
BRAVAIS_ERR_UNSUPPORTED = -5,
|
||||
BRAVAIS_ERR_AUTH = -6,
|
||||
BRAVAIS_ERR_STATE = -7,
|
||||
BRAVAIS_ERR_CONN_NOT_FOUND = -8,
|
||||
BRAVAIS_ERR_CONN_LIMIT = -9,
|
||||
|
||||
BRAVAIS_ERR_UNKNOWN = -1000
|
||||
} bravais_result_t;
|
||||
|
||||
typedef enum {
|
||||
BRAVAIS_ROLE_INITIATOR,
|
||||
BRAVAIS_ROLE_RESPONDER
|
||||
} bravais_role_t;
|
||||
|
||||
typedef enum {
|
||||
BRAVAIS_TRANSPORT_MODE_UNSET = 0, // Rejected value
|
||||
BRAVAIS_TRANSPORT_MODE_RAW,
|
||||
BRAVAIS_TRANSPORT_MODE_QUIC
|
||||
} bravais_transport_mode_t;
|
||||
@@ -63,16 +65,37 @@ typedef enum {
|
||||
*/
|
||||
typedef void (*bravais_log_fn_t)(void* userData, bravais_log_level_t level, const char* message);
|
||||
|
||||
typedef struct bravais_endpoint {
|
||||
uint8_t address[16]; // IPv6 address (or IPv4-mapped IPv6) - network byte order
|
||||
uint16_t port; // Port number - host byte order
|
||||
} bravais_endpoint_t;
|
||||
|
||||
typedef enum {
|
||||
BRAVAIS_EVENT_CONN_ESTABLISHED = 1,
|
||||
BRAVAIS_EVENT_CONN_CLOSED,
|
||||
BRAVAIS_EVENT_CONN_FAILED
|
||||
} bravais_event_type_t;
|
||||
|
||||
typedef struct {
|
||||
bravais_role_t role;
|
||||
bravais_transport_mode_t transportMode;
|
||||
bravais_log_fn_t logFn;
|
||||
bravais_event_type_t type;
|
||||
uint64_t connId;
|
||||
} bravais_event_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t staticPrivateKey[BRAVAIS_X25519_KEY_LENGTH]; // Copied in, wipe afterwards
|
||||
uint32_t maxConns; // 0 selects default
|
||||
bravais_log_fn_t logFn; // NULL disables logging
|
||||
void* logUserData;
|
||||
} bravais_context_config_t;
|
||||
|
||||
typedef struct {
|
||||
bravais_transport_mode_t transportMode;
|
||||
bravais_endpoint_t remoteEndpoint;
|
||||
uint8_t remoteStaticKey[BRAVAIS_X25519_KEY_LENGTH]; // Responder identity
|
||||
// TODO: Add more configuration options here as needed
|
||||
} bravais_config_t;
|
||||
} bravais_connect_config_t;
|
||||
|
||||
typedef struct bravais_conn bravais_conn_t; // Opaque connection type
|
||||
typedef struct bravais_context bravais_context_t; // Opaque
|
||||
|
||||
// =============================================================================
|
||||
// Public API functions
|
||||
@@ -85,89 +108,122 @@ typedef struct bravais_conn bravais_conn_t; // Opaque connection type
|
||||
BRAVAIS_API uint32_t bravais_version(void);
|
||||
|
||||
/**
|
||||
* @brief Initialize the config struct with defaults for all optional fields.
|
||||
* @param config Pointer to a bravais_config_t struct to initialize.
|
||||
* @return BRAVAIS_OK on success, or BRAVAIS_ERR_INVALID_ARG if the config pointer is NULL.
|
||||
* @attention The caller must still set role and transportMode manually.
|
||||
* @brief Human-readable name of a result code.
|
||||
* @return A static string; never NULL.
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_config_init(const bravais_config_t* config);
|
||||
BRAVAIS_API const char* bravais_result_str(bravais_result_t result);
|
||||
|
||||
/**
|
||||
* @brief Create a new connection.
|
||||
* @param config The configuration to use for the new connection.
|
||||
* @param outConn A pointer to a bravais_conn_t pointer that will be set to the new connection. Set to NULL on failure.
|
||||
* @return BRAVAIS_OK on success, BRAVAIS_ERR_INVALID_ARG, or BRAVAIS_ERR_OUT_OF_MEMORY.
|
||||
* @attention The config is copied. The caller may reuse or discard it afterwards.
|
||||
* @brief Initialize a context config with defaults for all optional fields.
|
||||
* @return BRAVAIS_OK, or BRAVAIS_ERR_INVALID_ARG if config is NULL.
|
||||
* @attention The caller must still set staticPrivateKey.
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_conn_create(const bravais_config_t* config, bravais_conn_t** outConn);
|
||||
BRAVAIS_API bravais_result_t bravais_context_config_init(bravais_context_config_t* config);
|
||||
|
||||
/**
|
||||
* @brief Destroy a connection.
|
||||
* @param conn The connection to destroy.
|
||||
* @brief Create a context.
|
||||
* @param config The configuration. Copied; the caller may reuse or discard it afterwards.
|
||||
* @param outCtx Receives the new context. Set to NULL on failure.
|
||||
* @return BRAVAIS_OK, BRAVAIS_ERR_INVALID_ARG, or BRAVAIS_ERR_OUT_OF_MEMORY.
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_context_create(const bravais_context_config_t* config, bravais_context_t** outCtx);
|
||||
|
||||
/**
|
||||
* @brief Destroy a context and every connection it owns, wiping key material.
|
||||
* @attention NULL is a no-op.
|
||||
*/
|
||||
BRAVAIS_API void bravais_conn_destroy(bravais_conn_t* conn);
|
||||
|
||||
BRAVAIS_API void bravais_context_destroy(bravais_context_t* ctx);
|
||||
|
||||
/**
|
||||
* @brief Initialize a connect config with defaults for all optional fields.
|
||||
* @return BRAVAIS_OK, or BRAVAIS_ERR_INVALID_ARG if config is NULL.
|
||||
* @attention The caller must still set transportMode, remoteEndpoint and remoteStaticKey.
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_connect_config_init(bravais_connect_config_t* config);
|
||||
|
||||
/**
|
||||
* @brief Start a connection to a remote peer, as the initiator.
|
||||
* @param ctx The owning context.
|
||||
* @param config Where and how to connect. Copied.
|
||||
* @param nowMs Current time (see the note at the top of this header).
|
||||
* @param outConnId Receives the new connection id. Set to BRAVAIS_NO_CONN on failure.
|
||||
* @return BRAVAIS_OK, BRAVAIS_ERR_INVALID_ARG, BRAVAIS_ERR_OUT_OF_MEMORY, or BRAVAIS_ERR_LIMIT.
|
||||
* @attention The handshake's first flight is queued; drain it with bravais_poll_datagram().
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_connect(bravais_context_t* ctx, const bravais_connect_config_t* config, uint64_t nowMs, uint64_t* outConnId);
|
||||
|
||||
/**
|
||||
* @brief Close a connection.
|
||||
* @return BRAVAIS_OK, BRAVAIS_ERR_INVALID_ARG, or BRAVAIS_ERR_NOT_FOUND.
|
||||
* @attention The id is invalid as soon as this returns. No CLOSED event is reported for it.
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_close(bravais_context_t* ctx, uint64_t connId, uint64_t nowMs);
|
||||
|
||||
/**
|
||||
* @brief Feed one datagram received from the network.
|
||||
* @param conn The connection to feed the datagram to.
|
||||
* @param data The datagram data received from the network.
|
||||
* @param dataLen The length of the datagram data.
|
||||
* @param nowMs The current time in milliseconds (unix epoch).
|
||||
* @return BRAVAIS_OK on success, BRAVAIS_ERR_INVALID_ARG, or BRAVAIS_ERR_MALFORMED (datagram is malformed).
|
||||
* @param ctx The context. It finds the owning connection, or starts a responder connection for a new handshake.
|
||||
* @param transportMode The transport of the socket the datagram arrived on.
|
||||
* @param from The datagram's source address.
|
||||
* @param data The datagram.
|
||||
* @param dataLen The length of the datagram.
|
||||
* @param nowMs Current time.
|
||||
* @return BRAVAIS_OK, BRAVAIS_ERR_INVALID_ARG, or BRAVAIS_ERR_MALFORMED. A malformed datagram is dropped and affects no connection.
|
||||
* @attention Any resulting output is queued. Drain it with the poll functions.
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_recv_datagram(bravais_conn_t* conn, const uint8_t* data, size_t dataLen, uint64_t nowMs);
|
||||
|
||||
BRAVAIS_API bravais_result_t bravais_recv_datagram(bravais_context_t* ctx, bravais_transport_mode_t transportMode, const bravais_endpoint_t* from, const uint8_t* data, size_t dataLen, uint64_t nowMs);
|
||||
|
||||
/**
|
||||
* @brief Submit one plaintext packet read from the TUN device.
|
||||
* @param conn The connection to submit the packet to.
|
||||
* @param data The packet data read from the TUN device.
|
||||
* @param dataLen The length of the packet data.
|
||||
* @param nowMs The current time in milliseconds (unix epoch).
|
||||
* @return BRAVAIS_OK on success, or BRAVAIS_ERR_INVALID_ARG.
|
||||
* @attention The packet is encrypted and queued for sending. Drain it with bravais_poll_datagram().
|
||||
* @brief Submit one plaintext packet read from the TUN device, to be sent on a connection.
|
||||
* @return BRAVAIS_OK, BRAVAIS_ERR_INVALID_ARG, BRAVAIS_ERR_NOT_FOUND, or BRAVAIS_ERR_STATE.
|
||||
* @attention The packet is encrypted and queued. Drain it with bravais_poll_datagram().
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_send_packet(bravais_conn_t* conn, const uint8_t* data, size_t dataLen, uint64_t nowMs);
|
||||
|
||||
BRAVAIS_API bravais_result_t bravais_send_packet(bravais_context_t* ctx, uint64_t connId, const uint8_t* data, size_t dataLen, uint64_t nowMs);
|
||||
|
||||
/**
|
||||
* @brief Take the next datagram to send to the network, if any.
|
||||
* @param conn The connection to poll for datagrams.
|
||||
* @param out A buffer to write the datagram to.
|
||||
* @param outCap The capacity of the output buffer.
|
||||
* @param outLen A pointer to a size_t that will be set to the length of the datagram written to the output buffer.
|
||||
* @return BRAVAIS_OK on success, BRAVAIS_EMPTY if no datagram is available (this is NOT an error), or BRAVAIS_ERR_BUFFER_TOO_SMALL.
|
||||
* @attention The caller must provide a buffer large enough to hold the datagram.
|
||||
* @brief Take the next datagram to send to the network, from any connection.
|
||||
* @param out Destination buffer.
|
||||
* @param outCap Capacity of out.
|
||||
* @param outLen Receives the datagram length. On BRAVAIS_ERR_BUFFER_TOO_SMALL, receives the required size and the datagram stays queued.
|
||||
* @param outTo Receives the destination address.
|
||||
* @param outTransportMode Receives the transport, i.e. which socket to send it from.
|
||||
* @return BRAVAIS_OK, BRAVAIS_EMPTY if nothing is queued (not an error), or BRAVAIS_ERR_BUFFER_TOO_SMALL.
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_poll_datagram(bravais_conn_t* conn, uint8_t* out, size_t outCap, size_t* outLen);
|
||||
|
||||
BRAVAIS_API bravais_result_t bravais_poll_datagram(bravais_context_t* ctx, uint8_t* out, size_t outCap, size_t* outLen, bravais_endpoint_t* outTo, bravais_transport_mode_t* outTransportMode);
|
||||
|
||||
/**
|
||||
* @brief Take the next decrypted packet to write to the TUN device, if any.
|
||||
* @param conn The connection to poll for packets.
|
||||
* @param out A buffer to write the packet to.
|
||||
* @param outCap The capacity of the output buffer.
|
||||
* @param outLen A pointer to a size_t that will be set to the length of the packet written to the output buffer.
|
||||
* @return BRAVAIS_OK on success, BRAVAIS_EMPTY if no packet is available (this is NOT an error), or BRAVAIS_ERR_BUFFER_TOO_SMALL.
|
||||
* @attention The caller must provide a buffer large enough to hold the packet.
|
||||
* @brief Take the next decrypted packet to write to the TUN device, from any connection.
|
||||
* @param outConnId Receives the id of the connection it arrived on, for the inner source address check.
|
||||
* @return BRAVAIS_OK, BRAVAIS_EMPTY if nothing is queued (not an error), or BRAVAIS_ERR_BUFFER_TOO_SMALL.
|
||||
* @attention Same buffer semantics as bravais_poll_datagram().
|
||||
*
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_poll_plaintext(bravais_conn_t* conn, uint8_t* out, size_t outCap, size_t* outLen);
|
||||
|
||||
BRAVAIS_API bravais_result_t bravais_poll_plaintext(bravais_context_t* ctx, uint8_t* out, size_t outCap, size_t* outLen, uint64_t* outConnId);
|
||||
|
||||
/**
|
||||
* @brief Earliest time the connection needs bravais_on_timeout() called.
|
||||
* @return Absolute deadline in milliseconds (unix epoch) for the next timeout, or BRAVAIS_NO_TIMEOUT if no timeout is needed.
|
||||
* @brief Take the next connection lifecycle event.
|
||||
* @return BRAVAIS_OK, BRAVAIS_EMPTY if there is none (not an error), or BRAVAIS_ERR_INVALID_ARG.
|
||||
* @attention Poll this every loop iteration: connections that ended are only freed once their last event is taken.
|
||||
*/
|
||||
BRAVAIS_API uint64_t bravais_get_next_timeout(bravais_conn_t* conn);
|
||||
|
||||
BRAVAIS_API bravais_result_t bravais_poll_event(bravais_context_t* ctx, bravais_event_t* outEvent);
|
||||
|
||||
/**
|
||||
* @brief Run any timers due at nowMs.
|
||||
* @param conn The connection to run timers for.
|
||||
* @param nowMs The current time in milliseconds (unix epoch).
|
||||
* @return BRAVAIS_OK on success, or BRAVAIS_ERR_INVALID_ARG.
|
||||
* @brief The remote's authenticated static public key.
|
||||
* @param outKey Receives BRAVAIS_X25519_KEY_LENGTH bytes.
|
||||
* @return BRAVAIS_OK, BRAVAIS_ERR_INVALID_ARG, BRAVAIS_ERR_NOT_FOUND, or BRAVAIS_ERR_STATE before the handshake has completed.
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_conn_remote_key(const bravais_context_t* ctx, uint64_t connId, uint8_t* outKey);
|
||||
|
||||
/**
|
||||
* @brief Earliest time any connection needs bravais_on_timeout() called.
|
||||
* @return Absolute deadline on the caller's clock, or BRAVAIS_NO_TIMEOUT.
|
||||
*/
|
||||
BRAVAIS_API uint64_t bravais_get_next_timeout(const bravais_context_t* ctx);
|
||||
|
||||
/**
|
||||
* @brief Run any timers due at nowMs, across all connections.
|
||||
* @return BRAVAIS_OK, or BRAVAIS_ERR_INVALID_ARG.
|
||||
* @attention Safe to call early. No-op if no timers are due yet.
|
||||
*/
|
||||
BRAVAIS_API bravais_result_t bravais_on_timeout(bravais_conn_t* conn, uint64_t nowMs);
|
||||
BRAVAIS_API bravais_result_t bravais_on_timeout(bravais_context_t* ctx, uint64_t nowMs);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user