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:
+2
-1
@@ -53,10 +53,11 @@ bravais_result_t bravais_conn_create(const bravais_config_t* config, bravais_con
|
||||
|
||||
void bravais_conn_destroy(bravais_conn_t* conn) {
|
||||
if (!conn) { return; }
|
||||
|
||||
conn_log(conn, BRAVAIS_LOG_LEVEL_DEBUG, "Connection destroyed");
|
||||
|
||||
bravais_hygiene_zero(conn, sizeof(*conn));
|
||||
free(conn);
|
||||
conn_log(conn, BRAVAIS_LOG_LEVEL_DEBUG, "Connection destroyed");
|
||||
}
|
||||
|
||||
bravais_result_t bravais_recv_datagram(bravais_conn_t* conn, const uint8_t* buf, size_t bufLen, [[maybe_unused]] uint64_t nowMs) {
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
// context.c
|
||||
// The context, connection table and public API surface
|
||||
// v0 - lifecycle, connection table and event delivery; data path is stubbed
|
||||
|
||||
#include "proto/context.h"
|
||||
#include "util/hygiene.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BRAVAIS_DEFAULT_MAX_CONNS 1024
|
||||
#define BRAVAIS_MAX_CONNS_LIMIT (UINT32_C(1) << 20)
|
||||
#define BRAVAIS_INITIAL_SLOT_COUNT 8
|
||||
|
||||
/* HELPERS */
|
||||
|
||||
void bravais_log(const bravais_context_t* ctx, bravais_log_level_t level, const char* message) {
|
||||
if (!ctx || !ctx->config.logFn) { return; }
|
||||
ctx->config.logFn(ctx->config.logUserData, level, message);
|
||||
}
|
||||
|
||||
// An all-zero key will almost never happen, but we'll have this just in case
|
||||
static bool key_is_zero(const uint8_t* key, size_t len) {
|
||||
uint8_t acc = 0;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
acc |= key[i]; // Actually a pretty cool method instead of an if statement every iteration
|
||||
}
|
||||
|
||||
return acc == 0;
|
||||
}
|
||||
|
||||
static bool transport_mode_is_valid(bravais_transport_mode_t transportMode) {
|
||||
// UNSET is defined as invalid
|
||||
return transportMode == BRAVAIS_TRANSPORT_MODE_RAW || transportMode == BRAVAIS_TRANSPORT_MODE_QUIC;
|
||||
}
|
||||
|
||||
/* CONNECTION TABLE */
|
||||
static uint64_t conn_id_make(uint32_t index, uint32_t generation) {
|
||||
return ((uint64_t)generation << 32) | index; // bottom/top rule - see the header comment
|
||||
}
|
||||
|
||||
static void conn_id_disassemble(uint64_t connId, uint32_t* index, uint32_t* generation) {
|
||||
if (!index || !generation) { return; }
|
||||
*index = (uint32_t)connId;
|
||||
*generation = (uint32_t)(connId >> 32);
|
||||
}
|
||||
|
||||
static bravais_conn_slot_t* slot_from_id(const bravais_context_t* ctx, uint64_t connId) {
|
||||
if (!ctx->slots) { return nullptr; }
|
||||
|
||||
uint32_t index;
|
||||
uint32_t generation;
|
||||
conn_id_disassemble(connId, &index, &generation);
|
||||
|
||||
if (generation == 0 || index >= vector_size(ctx->slots)) { return nullptr; }
|
||||
|
||||
bravis_conn_slot_t* slot = (bravais_conn_slot_t*)vector_get(ctx->slots, index);
|
||||
if (!slot || !slot->conn || slot->generation != generation) { return nullptr; }
|
||||
|
||||
return slot;
|
||||
}
|
||||
|
||||
// Takes ownership of conn on success only
|
||||
static bravais_result_t context_insert(bravais_context_t* ctx, bravais_conn_t* conn, uint64_t* outConnId) {
|
||||
if (!ctx->slots) { return; }
|
||||
size_t slotCount = vector_size(ctx->slots);
|
||||
|
||||
uint32_t index = (uint32_t)slotCount;
|
||||
for (uint32_t i = 0; i < slotCount; i++) {
|
||||
const bravais_conn_slot_t* temp = (const bravais_conn_slot_t*)vector_get_const(ctx->slots, i);
|
||||
if (!temp->conn) {
|
||||
index = i; // We do this for reuse of dead slots instead of just adding a new entry
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index == slotCount) {
|
||||
if (slotCount >= ctx->maxConns) { return BRAVAIS_ERR_CONN_LIMIT; }
|
||||
|
||||
bravais_conn_slot_t slot = {
|
||||
.conn = conn,
|
||||
.generation = 1
|
||||
};
|
||||
vector_push_back(ctx->slots, &slot);
|
||||
|
||||
*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;
|
||||
|
||||
/*uint32_t newCount = slotCount == 0 ? BRAVAIS_INITIAL_SLOT_COUNT : slotCount * 2;
|
||||
if (newCount > ctx->maxCoons) {
|
||||
newCount = ctx->maxConns;
|
||||
}
|
||||
|
||||
if (vector_reserve(ctx->slots, newCount) < 0) {
|
||||
return BRAVAIS_ERR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
// There's probably a better way to do this (i.e. put it in vector.h), but whatever
|
||||
bravais_conn_slot_t* ptr = (bravais_conn_slot_t*)vector_as_c_array_mutable(ctx->slots);
|
||||
memset(ptr + slotCount, 0x00, (size_t)(newCount - slotCount) * sizeof(*ptr));*/
|
||||
}
|
||||
|
||||
// Not the last element, change
|
||||
bravais_conn_slot_t* slot = (bravais_conn_slot_t*)vector_get(ctx->slots, index);
|
||||
slot->generation++; // Bump the generation
|
||||
if (slot->generation == 0) {
|
||||
// uint wraps to 0, 0 is invalid, skip
|
||||
slot->generation = 1;
|
||||
}
|
||||
slot->conn = conn;
|
||||
|
||||
// Copy-pasting code, oh well
|
||||
*outConnId = conn_id_make(index, slot->generation);
|
||||
return BRAVAIS_OK;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// context.h
|
||||
// Context internals - the connection table
|
||||
//
|
||||
// Connection IDs pack a slot index (low 32 bits) and that slot's generation (high 32 bits).
|
||||
// A slot's gen is bumped each occupation and is NEVER 0 (BRAVAIS_NO_CONN).
|
||||
// Never names a live connection, and an ID whose slot was free'd and reused fails lookup instead of reaching the new occupant.
|
||||
// IDs are internal bookkeeping and NEVER go on the wire.
|
||||
|
||||
#ifndef BRAVAIS_CONTEXT_H
|
||||
#define BRAVAIS_CONTEXT_H
|
||||
|
||||
#include "bravais.h"
|
||||
#include "proto/conn.h"
|
||||
#include "dlibc/vector.h"
|
||||
|
||||
typedef struct {
|
||||
bravais_conn_t* conn; // nullptr when free
|
||||
uint32_t generation;
|
||||
} bravais_conn_slot_t;
|
||||
|
||||
struct bravais_context {
|
||||
bravais_context_config_t config; // Holds the private key, beware! (wipe on destroy)
|
||||
uint32_t maxConns;
|
||||
vector_t* slots; // Slots of bravais_conn_slot_t
|
||||
}
|
||||
|
||||
// Safe to call with a context without a log callback
|
||||
void bravais_log(const bravais_context_t* ctx, bravais_log_level_t level, const char* message);
|
||||
|
||||
#endif // BRAVAIS_CONTEXT_H
|
||||
@@ -8,14 +8,11 @@
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "bravais.h"
|
||||
|
||||
|
||||
static const uint8_t bravaisEndpointMappedIPv4Prefix[12] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff };
|
||||
|
||||
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;
|
||||
|
||||
bool bravais_endpoint_is_ipv4(const bravais_endpoint_t* endpoint);
|
||||
|
||||
#endif // BRAVAIS_ENDPOINT_H
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
// This apparently stops the compiler from optimizing away the memset
|
||||
static void* (*volatile volatileMemset)(void*, int, size_t) = memset;
|
||||
|
||||
void bravais_hygiene_zero(void* ptr, size_t len) {
|
||||
|
||||
Reference in New Issue
Block a user