// SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR MIT // libbravais public api header file #ifndef BRAVAIS_H #define BRAVAIS_H #include #include #include "bravais/export.h" // Generated by cmake #ifdef __cplusplus extern "C" { #endif #define BRAVAIS_VERSION_MAJOR 0 #define BRAVAIS_VERSION_MINOR 1 #define BRAVAIS_VERSION_PATCH 0 #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 BRAVAIS_OK = 0, BRAVAIS_ERR_INVALID_ARG = -1, BRAVAIS_ERR_BUFFER_TOO_SMALL = -2, BRAVAIS_ERR_MALFORMED = -3, BRAVAIS_ERR_OUT_OF_MEMORY = -4, 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_LIMIT = -10, BRAVAIS_ERR_UNKNOWN = -1000 } bravais_result_t; typedef enum { BRAVAIS_TRANSPORT_MODE_UNSET = 0, // Rejected value BRAVAIS_TRANSPORT_MODE_RAW, BRAVAIS_TRANSPORT_MODE_QUIC } bravais_transport_mode_t; typedef enum { BRAVAIS_LOG_LEVEL_DEBUG, BRAVAIS_LOG_LEVEL_INFO, BRAVAIS_LOG_LEVEL_WARN, BRAVAIS_LOG_LEVEL_ERROR } bravais_log_level_t; /** * @brief Logging callback function type * @param userData User-defined data passed to the logging function * @param level The log level of the message * @param message The log message string */ 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_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_connect_config_t; typedef struct bravais_context bravais_context_t; // Opaque // ============================================================================= // Public API functions // ============================================================================= /** * @brief Runtime library version number, packed as (major << 16) | (minor << 8) | patch * @return The version number, packed as (major << 16) | (minor << 8) | patch */ BRAVAIS_API uint32_t bravais_version(void); /** * @brief Human-readable name of a result code. * @return A static string; never NULL. */ BRAVAIS_API const char* bravais_result_str(bravais_result_t result); /** * @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_context_config_init(bravais_context_config_t* config); /** * @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_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 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_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, 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_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, 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_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, 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_context_t* ctx, uint8_t* out, size_t outCap, size_t* outLen, uint64_t* outConnId); /** * @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 bravais_result_t bravais_poll_event(bravais_context_t* ctx, bravais_event_t* outEvent); /** * @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_context_t* ctx, uint64_t nowMs); #ifdef __cplusplus } #endif #endif // BRAVAIS_H