implement conn.c lifecycle and bookkeeping, implement context.c lifecycle, conn table and event delivery (data path stubbed/missing)
This commit is contained in:
+53
-93
@@ -1,129 +1,89 @@
|
|||||||
// conn.c
|
// conn.c
|
||||||
// Top-level connection object and the public API surface
|
// Top-level connection object and the public API surface
|
||||||
//
|
//
|
||||||
// Lifecycle made but the data path is a stub for now.
|
// Lifecycle and bookkeeping made, rest are stubs
|
||||||
|
|
||||||
#include "proto/conn.h"
|
#include "proto/conn.h"
|
||||||
#include "util/hygiene.h"
|
#include "util/hygiene.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static void conn_log(const bravais_conn_t* conn, bravais_log_level_t level, const char* msg) {
|
bravais_result_t bravais_conn_create(bravais_role_t role, bravais_transport_mode_t transportMode, const bravais_endpoint_t* remoteEndpoint, const uint8_t* remoteStaticKey, bravais_conn_t** outConn) {
|
||||||
if (!conn->config.logFn) { return; }
|
|
||||||
conn->config.logFn(conn->config.logUserData, level, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool config_is_valid(const bravais_config_t* config) {
|
|
||||||
if (!config) { return false; }
|
|
||||||
if (config->role != BRAVAIS_ROLE_INITIATOR && config->role != BRAVAIS_ROLE_RESPONDER) { return false; }
|
|
||||||
if (config->transportMode != BRAVAIS_TRANSPORT_RAW && config->transportMode != BRAVAIS_TRANSPORT_QUIC) { return false; }
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bravais_config_init(bravais_config_t* config) {
|
|
||||||
if (!config) { return; }
|
|
||||||
|
|
||||||
config->role = BRAVAIS_ROLE_INITIATOR;
|
|
||||||
config->transportMode = BRAVAIS_TRANSPORT_RAW;
|
|
||||||
config->logFn = nullptr;
|
|
||||||
config->logUserData = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
bravais_result_t bravais_conn_create(const bravais_config_t* config, bravais_conn_t** outConn) {
|
|
||||||
if (!outConn) { return BRAVAIS_ERR_INVALID_ARG; }
|
if (!outConn) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
*outConn = nullptr;
|
*outConn = nullptr;
|
||||||
|
|
||||||
if (!config || !config_is_valid(config)) { return BRAVAIS_ERR_INVALID_ARG; }
|
if (!remoteEndpoint) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
|
||||||
bravais_conn_t* newConn = (bravais_conn_t*)calloc(1, sizeof(*newConn));
|
bravais_conn_t* conn = (bravais_conn_t*)calloc(1, sizeof(*conn));
|
||||||
if (!newConn) { return BRAVAIS_ERR_OUT_OF_MEMORY; }
|
if (!conn) { return BRAVAIS_ERR_OUT_OF_MEMORY; }
|
||||||
|
|
||||||
newConn->config = *config;
|
conn->role = role;
|
||||||
newConn->state = BRAVAIS_CONN_STATE_IDLE;
|
conn->transportMode = transportMode;
|
||||||
// calloc() already zeroes
|
conn->state = BRAVAIS_CONN_STATE_IDLE;
|
||||||
//memset(&newConn->remoteEndpoint, 0, sizeof(newConn->remoteEndpoint));
|
conn->remoteEndpoint = *remoteEndpoint;
|
||||||
//memset(&newConn->localEndpoint, 0, sizeof(newConn->localEndpoint));
|
|
||||||
|
|
||||||
conn_log(newConn, BRAVAIS_LOG_LEVEL_DEBUG, "Connection created");
|
if (remoteStaticKey) {
|
||||||
*outConn = newConn;
|
memcpy(conn->remoteStaticKey, remoteStaticKey, sizeof(conn->remoteStaticKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
*outConn = conn;
|
||||||
|
|
||||||
return BRAVAIS_OK;
|
return BRAVAIS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bravais_conn_destroy(bravais_conn_t* conn) {
|
void bravais_conn_destroy(void* conn) {
|
||||||
if (!conn) { return; }
|
if (!conn) { return; }
|
||||||
|
conn = (bravais_conn_t*)conn; // Cast from destructor format - this might be unsafe
|
||||||
conn_log(conn, BRAVAIS_LOG_LEVEL_DEBUG, "Connection destroyed");
|
|
||||||
|
|
||||||
bravais_hygiene_zero(conn, sizeof(*conn));
|
bravais_hygiene_zero(conn, sizeof(*conn));
|
||||||
free(conn);
|
free(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
bravais_result_t bravais_recv_datagram(bravais_conn_t* conn, const uint8_t* buf, size_t bufLen, [[maybe_unused]] uint64_t nowMs) {
|
void bravais_conn_mark_established(bravais_conn_t* conn) {
|
||||||
if (!conn || (!buf && bufLen != 0)) {
|
conn->state = BRAVAIS_CONN_STATE_ESTABLISHED;
|
||||||
return BRAVAIS_ERR_INVALID_ARG;
|
conn->remoteAuthenticated = true;
|
||||||
}
|
conn->pendingEvents = (uint8_t)(conn->pendingEvents | BRAVAIS_EVENT_BIT(BRAVAIS_EVENT_CONN_ESTABLISHED));
|
||||||
return BRAVAIS_ERR_UNSUPPORTED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bravais_result_t bravais_send_packet(bravais_conn_t* conn, const uint8_t* buf, size_t bufLen, [[maybe_unused]] uint64_t nowMs) {
|
void bravais_conn_finish(bravais_conn_t* conn) {
|
||||||
if (!conn || (!buf && bufLen != 0)) {
|
if (conn->state == BRAVAIS_CONN_STATE_CLOSED) { return; }
|
||||||
return BRAVAIS_ERR_INVALID_ARG;
|
|
||||||
|
bool wasEstablished = conn->state == BRAVAIS_CONN_STATE_ESTABLISHED;
|
||||||
|
conn->state = BRAVAIS_CONN_STATE_CLOSED;
|
||||||
|
|
||||||
|
if (wasEstablished) {
|
||||||
|
conn->pendingEvents = (uint8_t)(conn->pendingEvents | BRAVAIS_EVENT_BIT(BRAVAIS_EVENT_CONN_CLOSED));
|
||||||
|
} else if (conn->role == BRAVAIS_ROLE_INITIATOR) {
|
||||||
|
conn->pendingEvents = (uint8_t)(conn->pendingEvents | BRAVAIS_EVENT_BIT(BRAVAIS_EVENT_CONN_FAILED));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bufLen > BRAVAIS_MAX_PLAINTEXT_SIZE) {
|
// A responder that never finished its handshake was never visible to the caller, so there is nothing to report. poll_event() frees it on the next pass.
|
||||||
return BRAVAIS_ERR_INVALID_ARG;
|
}
|
||||||
|
|
||||||
|
bool bravais_conn_take_event(bravais_conn_t* conn, bravais_event_type_t* outType) {
|
||||||
|
static const bravais_event_type_t order[] = {
|
||||||
|
BRAVAIS_EVENT_CONN_ESTABLISHED,
|
||||||
|
BRAVAIS_EVENT_CONN_CLOSED,
|
||||||
|
BRAVAIS_EVENT_CONN_FAILED
|
||||||
|
};
|
||||||
|
|
||||||
|
for (size_t i = 0; i < sizeof(order) / sizeof(order[0]); i++) {
|
||||||
|
uint8_t bit = BRAVAIS_EVENT_BIT(order[i]);
|
||||||
|
if (conn->pendingEvents & bit) {
|
||||||
|
conn->pendingEvents = (uint8_t)(conn->pendingEvents & ~bit); // reset
|
||||||
|
*outType = order[i];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return BRAVAIS_ERR_UNSUPPORTED;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bravais_result_t bravais_poll_datagram(bravais_conn_t* conn, uint8_t* out, size_t* outLen, [[maybe_unused]] uint64_t nowMs) {
|
uint64_t bravais_conn_next_timeout([[maybe_unused]] const bravais_conn_t* conn) {
|
||||||
if (!conn || !out || !outLen) {
|
return BRAVAIS_NO_TIMEOUT;
|
||||||
return BRAVAIS_ERR_INVALID_ARG;
|
// We don't have timers yet. Handshake retransmit, keepalive, PMTU probes will go here
|
||||||
}
|
|
||||||
|
|
||||||
*outLen = 0;
|
|
||||||
|
|
||||||
return BRAVAIS_ERR_UNSUPPORTED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bravais_result_t bravais_poll_plaintext(bravais_conn_t* conn, uint8_t* out, size_t* outLen, [[maybe_unused]] uint64_t nowMs) {
|
void bravais_conn_on_timeout([[maybe_unused]] bravais_conn_t* conn, [[maybe_unused]] uint64_t nowMs) { }
|
||||||
if (!conn || !out || !outLen) {
|
|
||||||
return BRAVAIS_ERR_INVALID_ARG;
|
|
||||||
}
|
|
||||||
|
|
||||||
*outLen = 0;
|
|
||||||
|
|
||||||
return BRAVAIS_EMPTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t bravais_next_timeout([[maybe_unused]] const bravais_conn_t* conn) {
|
|
||||||
return UINT64_MAX;
|
|
||||||
}
|
|
||||||
|
|
||||||
bravais_result_t bravais_on_timeout(bravais_conn_t* conn, [[maybe_unused]] uint64_t nowMs) {
|
|
||||||
if (!conn) {
|
|
||||||
return BRAVAIS_ERR_INVALID_ARG;
|
|
||||||
}
|
|
||||||
|
|
||||||
return BRAVAIS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* bravais_result_str(bravais_result_t result) {
|
|
||||||
switch (result) {
|
|
||||||
case BRAVAIS_OK: return "OK";
|
|
||||||
case BRAVAIS_EMPTY: return "Nothing to emit right now";
|
|
||||||
case BRAVAIS_ERR_INVALID_ARG: return "Invalid argument";
|
|
||||||
case BRAVAIS_ERR_BUFFER_TOO_SMALL: return "Buffer too small";
|
|
||||||
case BRAVAIS_ERR_MALFORMED: return "Malformed input";
|
|
||||||
case BRAVAIS_ERR_OUT_OF_MEMORY: return "Out of memory";
|
|
||||||
case BRAVAIS_ERR_UNSUPPORTED: return "Unsupported or unimplemented feature";
|
|
||||||
case BRAVAIS_ERR_UNKNOWN: return "Unknown error";
|
|
||||||
case BRAVAIS_ERR_AUTH: return "Authentication failed";
|
|
||||||
case BRAVAIS_ERR_STATE: return "Invalid state for operation";
|
|
||||||
|
|
||||||
return "Unknown result";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+30
-4
@@ -9,6 +9,11 @@
|
|||||||
#include "bravais.h"
|
#include "bravais.h"
|
||||||
#include "proto/endpoint.h"
|
#include "proto/endpoint.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
BRAVAIS_ROLE_INITIATOR, // Created by bravais_connect()
|
||||||
|
BRAVAIS_ROLE_RESPONDER // Created by the context when a new handshake arrives
|
||||||
|
} bravais_role_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
BRAVAIS_CONN_STATE_IDLE,
|
BRAVAIS_CONN_STATE_IDLE,
|
||||||
BRAVAIS_CONN_STATE_HANDSHAKE,
|
BRAVAIS_CONN_STATE_HANDSHAKE,
|
||||||
@@ -16,11 +21,32 @@ typedef enum {
|
|||||||
BRAVAIS_CONN_STATE_CLOSED
|
BRAVAIS_CONN_STATE_CLOSED
|
||||||
} bravais_conn_state_t;
|
} bravais_conn_state_t;
|
||||||
|
|
||||||
struct bravais_conn {
|
#define BRAVAIS_EVENT_BIT(type) ((uint8_t)(1u << (type)))
|
||||||
bravais_config_t config;
|
|
||||||
|
typedef struct bravais_conn {
|
||||||
|
bravais_role_t role;
|
||||||
|
bravais_transport_mode_t transportMode;
|
||||||
bravais_conn_state_t state;
|
bravais_conn_state_t state;
|
||||||
bravais_endpoint_t remoteEndpoint;
|
bravais_endpoint_t remoteEndpoint;
|
||||||
bravais_endpoint_t localEndpoint; // This is here since the local endpoint, if we're initiating a connection, may not have a standard port number.
|
uint8_t remoteStaticKey[BRAVAIS_X25519_KEY_LENGTH]; // Initiator: from the connect config. Responder: learned in the handshake.
|
||||||
};
|
bool remoteAuthenticated; // The handshake has proven remoteStaticKey
|
||||||
|
uint8_t pendingEvents; // BRAVAIS_EVENT_BIT() flags, delivered by bravais_poll_event()
|
||||||
|
} bravais_conn_t;
|
||||||
|
|
||||||
|
// remoteStaticKey may be nullptr (responders don't know it yet)
|
||||||
|
bravais_result_t bravais_conn_create(bravais_role_t role, bravais_transport_mode_t transportMode, const bravais_endpoint_t* remoteEndpoint, const uint8_t* remoteStaticKey, bravais_conn_t** outConn);
|
||||||
|
void bravais_conn_destroy(void* conn); // void* to comply with vector_t's destructor - cast to bravais_conn_t*
|
||||||
|
|
||||||
|
// For the handshake code: the remote is authenticated; queues ESTABLISHED
|
||||||
|
void bravais_conn_mark_established(bravais_conn_t* conn);
|
||||||
|
|
||||||
|
// The connection is over, for whatever reason. Queues whichever event the caller needs, if any.
|
||||||
|
void bravais_conn_finish(bravais_conn_t* conn);
|
||||||
|
|
||||||
|
// Takes the next pending event, oldest kind first. False if there is none.
|
||||||
|
bool bravais_conn_take_event(bravais_conn_t* conn, bravais_event_type_t* outType);
|
||||||
|
|
||||||
|
uint64_t bravais_conn_next_timeout(const bravais_conn_t* conn);
|
||||||
|
void bravais_conn_on_timeout(bravais_conn_t* conn, uint64_t nowMs);
|
||||||
|
|
||||||
#endif // BRAVAIS_CONN_H
|
#endif // BRAVAIS_CONN_H
|
||||||
|
|||||||
+149
-2
@@ -62,7 +62,7 @@ static bravais_conn_slot_t* slot_from_id(const bravais_context_t* ctx, uint64_t
|
|||||||
|
|
||||||
// Takes ownership of conn on success only
|
// Takes ownership of conn on success only
|
||||||
static bravais_result_t context_insert(bravais_context_t* ctx, bravais_conn_t* conn, uint64_t* outConnId) {
|
static bravais_result_t context_insert(bravais_context_t* ctx, bravais_conn_t* conn, uint64_t* outConnId) {
|
||||||
if (!ctx->slots) { return; }
|
if (!ctx->slots) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
size_t slotCount = vector_size(ctx->slots);
|
size_t slotCount = vector_size(ctx->slots);
|
||||||
|
|
||||||
uint32_t index = (uint32_t)slotCount;
|
uint32_t index = (uint32_t)slotCount;
|
||||||
@@ -81,7 +81,10 @@ static bravais_result_t context_insert(bravais_context_t* ctx, bravais_conn_t* c
|
|||||||
.conn = conn,
|
.conn = conn,
|
||||||
.generation = 1
|
.generation = 1
|
||||||
};
|
};
|
||||||
vector_push_back(ctx->slots, &slot);
|
|
||||||
|
if (vector_push_back(ctx->slots, &slot) < 0) {
|
||||||
|
return BRAVAIS_ERR_INVALID_ARG;
|
||||||
|
}
|
||||||
|
|
||||||
*outConnId = conn_id_make(index, 1); // 1 is hardcoded here since it's a brand new element; TODO: figure out if bugs arise
|
*outConnId = conn_id_make(index, 1); // 1 is hardcoded here since it's a brand new element; TODO: figure out if bugs arise
|
||||||
return BRAVAIS_OK;
|
return BRAVAIS_OK;
|
||||||
@@ -102,6 +105,10 @@ static bravais_result_t context_insert(bravais_context_t* ctx, bravais_conn_t* c
|
|||||||
|
|
||||||
// Not the last element, change
|
// Not the last element, change
|
||||||
bravais_conn_slot_t* slot = (bravais_conn_slot_t*)vector_get(ctx->slots, index);
|
bravais_conn_slot_t* slot = (bravais_conn_slot_t*)vector_get(ctx->slots, index);
|
||||||
|
if (!slot) {
|
||||||
|
return BRAVAIS_ERR_INVALID_ARG;
|
||||||
|
}
|
||||||
|
|
||||||
slot->generation++; // Bump the generation
|
slot->generation++; // Bump the generation
|
||||||
if (slot->generation == 0) {
|
if (slot->generation == 0) {
|
||||||
// uint wraps to 0, 0 is invalid, skip
|
// uint wraps to 0, 0 is invalid, skip
|
||||||
@@ -113,3 +120,143 @@ static bravais_result_t context_insert(bravais_context_t* ctx, bravais_conn_t* c
|
|||||||
*outConnId = conn_id_make(index, slot->generation);
|
*outConnId = conn_id_make(index, slot->generation);
|
||||||
return BRAVAIS_OK;
|
return BRAVAIS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Frees the underlying connection - generation stays, so the above code actually... works
|
||||||
|
static void context_release(bravais_conn_slot_t* slot) {
|
||||||
|
if (!slot) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
bravais_conn_destroy(slot->conn);
|
||||||
|
slot->conn = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Public API: misc */
|
||||||
|
|
||||||
|
uint32_t bravais_version(void) {
|
||||||
|
return BRAVAIS_VERSION;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* bravais_result_str(bravais_result_t result) {
|
||||||
|
switch (result) {
|
||||||
|
case BRAVAIS_OK: return "OK";
|
||||||
|
case BRAVAIS_EMPTY: return "Nothing to emit right now";
|
||||||
|
case BRAVAIS_ERR_INVALID_ARG: return "Invalid argument";
|
||||||
|
case BRAVAIS_ERR_BUFFER_TOO_SMALL: return "Buffer too small";
|
||||||
|
case BRAVAIS_ERR_MALFORMED: return "Malformed input";
|
||||||
|
case BRAVAIS_ERR_OUT_OF_MEMORY: return "Out of memory";
|
||||||
|
case BRAVAIS_ERR_UNSUPPORTED: return "Unsupported or unimplemented feature";
|
||||||
|
case BRAVAIS_ERR_AUTH: return "Authentication failed";
|
||||||
|
case BRAVAIS_ERR_STATE: return "Invalid state for operation";
|
||||||
|
case BRAVAIS_ERR_CONN_NOT_FOUND: return "Connection not found";
|
||||||
|
case BRAVAIS_ERR_CONN_LIMIT: return "Connection limit reached";
|
||||||
|
case BRAVAIS_ERR_UNKNOWN: return "Unknown error";
|
||||||
|
|
||||||
|
return "Unrecognized result code";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Public API: context lifecycle */
|
||||||
|
|
||||||
|
bravais_result_t bravais_context_config_init(bravais_connect_config_t* config) {
|
||||||
|
if (!config) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
|
||||||
|
*config = (bravais_context_config_t){};
|
||||||
|
return BRAVAIS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bravais_result_t bravais_context_create(const bravais_context_config_t* config, bravais_context_t** outCtx) {
|
||||||
|
if (!outCtx) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
*outCtx = nullptr;
|
||||||
|
|
||||||
|
if (!config) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
if (key_is_zero(config->staticPrivateKey, sizeof(config->staticPrivateKey))) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
if (config->maxConns > BRAVAIS_MAX_CONNS_LIMIT) { return BRAVAIS_ERR_INVALID_ARG; } // You will literally never reach this
|
||||||
|
|
||||||
|
bravais_context_t* ctx = (bravais_context_t*)calloc(1, sizeof(*ctx));
|
||||||
|
if (!ctx) { return BRAVAIS_ERR_OUT_OF_MEMORY; }
|
||||||
|
|
||||||
|
ctx->config = *config;
|
||||||
|
ctx->maxConns = config->maxConns != 0 ? config->maxConns : BRAVAIS_DEFAULT_MAX_CONNS;
|
||||||
|
|
||||||
|
vector_t* slotsVec = vector_create(sizeof(bravais_conn_slot_t));
|
||||||
|
if (!slotsVec) {
|
||||||
|
free(ctx);
|
||||||
|
return BRAVAIS_ERR_OUT_OF_MEMORY;
|
||||||
|
}
|
||||||
|
vector_set_destructor(slotsVec, bravais_conn_destroy);
|
||||||
|
ctx->slots = slotsVec;
|
||||||
|
|
||||||
|
bravais_log(ctx, BRAVAIS_LOG_LEVEL_DEBUG, "Context created");
|
||||||
|
*outCtx = ctx;
|
||||||
|
|
||||||
|
return BRAVAIS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bravais_context_destroy(bravais_context_t* ctx) {
|
||||||
|
if (!ctx || !ctx->slots) { return; }
|
||||||
|
|
||||||
|
bravais_log(ctx, BRAVAIS_LOG_LEVEL_DEBUG, "Context destroyed");
|
||||||
|
vector_destroy(ctx->slots); // Calls bravais_conn_destroy destructors
|
||||||
|
|
||||||
|
bravais_hygiene_zero(ctx, sizeof(*ctx));
|
||||||
|
free(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Public API: connection lifecycle */
|
||||||
|
|
||||||
|
bravais_result_t bravais_connect_config_init(bravais_connect_config_t* config) {
|
||||||
|
if (!config) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
|
||||||
|
*config = (bravais_connect_config_t){}; // Note: transportMode is UNSET here, set it before bravais_connect()
|
||||||
|
return BRAVAIS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bravais_result_t bravais_connect(bravais_context_t* ctx, const bravais_connect_config_t* config, [[maybe_unused]] uint64_t nowMs, uint64_t* outConnId) {
|
||||||
|
if (!outConnId) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
*outConnId = BRAVAIS_NO_CONN;
|
||||||
|
|
||||||
|
if (!ctx || !config) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
if (!transport_mode_is_valid(config->transportMode)) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
if (config->remoteEndpoint.port == 0) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
if (key_is_zero(config->remoteStaticKey, sizeof(config->remoteStaticKey))) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
|
||||||
|
bravais_conn_t* conn = nullptr;
|
||||||
|
bravais_result_t result = bravais_conn_create(BRAVAIS_ROLE_INITIATOR, config->transportMode, &config->removeEndpoint, config->remoteStaticKey, &conn);
|
||||||
|
if (result != BRAVAIS_OK) { return result; }
|
||||||
|
|
||||||
|
result = context_insert(ctx, conn, outConnId);
|
||||||
|
if (result != BRAVAIS_OK) {
|
||||||
|
bravais_conn_destroy(conn);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: actually start the handshake here and queue first flight for poll_datagram()
|
||||||
|
bravais_log(ctx, BRAVAIS_LOG_LEVEL_DEBUG, "Connection created (initator)");
|
||||||
|
return BRAVAIS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bravais_result_t bravais_close(bravais_context_t* ctx, uint64_t connId, [[maybe_unused]] uint64_t nowMs) {
|
||||||
|
if (!ctx) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
|
||||||
|
bravais_conn_slot_t* slot = slot_from_id(ctx, connId);
|
||||||
|
if (!slot) { return BRAVAIS_ERR_CONN_NOT_FOUND; }
|
||||||
|
|
||||||
|
// TODO: once there's a close frame to send, the conn lingers internally to send it, but its id still dies here - the caller will never see it again either eay.
|
||||||
|
context_release(slot);
|
||||||
|
bravais_log(ctx, BRAVAIS_LOG_LEVEL_DEBUG, "Connection closed by caller");
|
||||||
|
|
||||||
|
return BRAVAIS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bravais_result_t bravais_conn_remote_key(const bravais_context_t* ctx, uint64_t connId, uint8_t* outKey) {
|
||||||
|
if (!ctx || !outKey) { return BRAVAIS_ERR_INVALID_ARG; }
|
||||||
|
|
||||||
|
bravais_conn_slot_t* slot = slot_from_id(ctx, connId);
|
||||||
|
if (!slot) { return BRAVAIS_ERR_CONN_NOT_FOUND; }
|
||||||
|
if (!slot->conn->remoteAuthenticated) { return BRAVAIS_ERR_STATE; }
|
||||||
|
|
||||||
|
memcpy(outKey, slot->conn->remoteStaticKey, BRAVAIS_X25519_KEY_LENGTH);
|
||||||
|
|
||||||
|
return BRAVAIS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Public API: data path */
|
||||||
|
// TODO: write them
|
||||||
|
|||||||
Reference in New Issue
Block a user