frame definitions/implementation

This commit is contained in:
2026-12-02 10:26:34 +01:00
parent 35f902c6a2
commit 3e3940a8c2
2 changed files with 245 additions and 4 deletions
+213
View File
@@ -0,0 +1,213 @@
// frame.c
#include "wire/frame.h"
// Borrow len bytes at the cursor. Unlike bravais_rcur_slice, a zero-length span still gets a real pointer.
static const uint8_t* take_span(bravais_rcur_t* cur, size_t len) {
const uint8_t* start = cur->buf + cur->pos;
bravais_rcur_skip(cur, len);
if (!bravais_rcur_ok(cur)) { return nullptr; }
return start;
}
// A varint that must fit in a size_t, as lengths do. Fails the cursor if it doesn't.
static size_t read_length(bravais_rcur_t* cur) {
uint64_t val = bravais_rcur_varint(cur);
if (val > SIZE_MAX) {
cur->failed = true; // Only reachable where size_t is narrower than 62 bits
return 0;
}
return (size_t)val;
}
bravais_result_t bravais_frame_parse(bravais_rcur_t* cur, bravais_frame_t* outFrame) {
if (!outFrame) { return BRAVAIS_ERR_INVALID_ARG; }
*outFrame = (bravais_frame_t){};
if (!cur || !bravais_rcur_ok(cur)) { return BRAVAIS_ERR_INVALID_ARG; }
if (bravais_rcur_remaining(cur) < 1) { return BRAVAIS_EMPTY; }
// make a copy to work on
bravais_rcur_t work = *cur;
bravais_frame_t frame = {};
size_t before = bravais_rcur_remaining(&work);
uint64_t type = bravais_rcur_varint(&work);
if (!bravais_rcur_ok(&work)) { return BRAVAIS_ERR_MALFORMED; }
// RFC 9000 section 12.4 - frame types use the shortest encoding possible
if (before - bravais_rcur_remaining(&work) != bravais_varint_len(type)) { return BRAVAIS_ERR_MALFORMED; }
switch (type) {
case BRAVAIS_FRAME_TYPE_PADDING: {
size_t run = 1;
while (bravais_rcur_remaining(&work) > 0 && work.buf[work.pos] == BRAVAIS_FRAME_TYPE_PADDING) {
bravais_rcur_skip(&work, 1);
run++;
}
frame.type = BRAVAIS_FRAME_TYPE_PADDING;
frame.paddingLen = run;
break;
}
case BRAVAIS_FRAME_TYPE_PING: {
frame.type = BRAVAIS_FRAME_TYPE_PING;
break;
}
case BRAVAIS_FRAME_TYPE_CRYPTO: {
uint64_t offset = bravais_rcur_varint(&work);
size_t len = read_length(&work);
if (!bravais_rcur_ok(&work)) { return BRAVAIS_ERR_MALFORMED; }
// RFC 9000 section 19.6 - stream can't extend past 2^62 - 1 bytes
if ((uint64_t)len > BRAVAIS_VARINT_MAX - offset) { return BRAVAIS_ERR_MALFORMED; }
const uint8_t* data = take_span(&work, len);
if (!data) { return BRAVAIS_ERR_MALFORMED; }
frame.type = BRAVAIS_FRAME_TYPE_CRYPTO;
frame.crypto.offset = offset;
frame.crypto.data = data;
frame.crypto.dataLen = len;
break;
}
case BRAVAIS_FRAME_TYPE_DATAGRAM:
case BRAVAIS_FRAME_TYPE_DATAGRAM_WITH_LENGTH: {
size_t len = 0;
if (type == BRAVAIS_FRAME_TYPE_DATAGRAM_WITH_LENGTH) {
len = read_length(&work);
if (!bravais_rcur_ok(&work)) { return BRAVAIS_ERR_MALFORMED; }
} else {
len = bravais_rcur_remaining(&work);
}
const uint8_t* data = take_span(&work, len);
if (!data) { return BRAVAIS_ERR_MALFORMED; }
frame.type = BRAVAIS_FRAME_TYPE_DATAGRAM;
frame.datagram.data = data;
frame.datagram.dataLen = len;
break;
}
default:
return BRAVAIS_ERR_UNSUPPORTED;
}
*cur = work;
*outFrame = frame;
return BRAVAIS_OK;
}
size_t bravais_frame_crypto_size(uint64_t offset, size_t dataLen) {
if ((uint64_t)dataLen > BRAVAIS_VARINT_MAX || offset > BRAVAIS_VARINT_MAX - (uint64_t)dataLen) { return 0; }
size_t overhead = 1 + bravais_varint_len(offset) + bravais_varint_len(dataLen);
if (dataLen > SIZE_MAX - overhead) { return 0; }
return overhead + dataLen;
}
size_t bravais_frame_datagram_size(size_t dataLen, bool includeLengthField) {
if ((uint64_t)dataLen > BRAVAIS_VARINT_MAX) { return 0; }
size_t overhead = 1 + (includeLengthField ? bravais_varint_len(dataLen) : 0);
if (dataLen > SIZE_MAX - overhead) { return 0; }
return overhead + dataLen;
}
bravais_frame_crypto_max_data(uint64_t offset, size_t space) {
if (offset >= BRAVAIS_VARINT_MAX) { return 0; } // no space left in the stream
size_t fixed = 1 + bravais_varint_len(offset);
if (space <= fixed) { return 0; }
size_t available = space - fixed;
static const size_t lengthBytes[] = {1, 2, 3, 8};
static const uint64-t lengthLimits[] = {UINT64_C(0x3F), UINT64_C(0x3FFF), UINT64_C(0x3FFFFFFF), UINT64_C(BRAVAIS_VARINT_MAX)};
size_t best = 0;
for (size_t i = 0; i < sizeof(lengthBytes) / sizeof(lengthBytes[0]); i++) {
if (available <= lengthBytes[i]) { break; }
uint64_t candidate = available - lengthBytes[i];
if (candidate > lengthLimits[i]) {
candidate = lengthLimits[i];
}
if (candidate > (uint64_t)best) {
best = (size_t)candidate;
}
}
if ((uint64_t)best > BRAVAIS_VARINT_MAX - offset) {
best = (size_t)(BRAVAIS_VARINT_MAX - offset); // stream limit
}
return best;
}
static bool writer_usable(const bravais_wcur_t* cur) {
return cur && bravais_wcur_ok(cur);
}
bravais_result_t bravais_frame_write_padding(bravais_wcur_t* cur, size_t len) {
if (!writer_usable(cur)) { return BRAVAIS_ERR_INVALID_ARG; }
if (len > bravais_wcur_remaining(cur)) { return BRAVAIS_ERR_BUFFER_TOO_SMALL; }
for (size_t i = 0; i < len; i++) {
bravais_wcur_u8(cur, BRAVAIS_FRAME_TYPE_PADDING);
}
return BRAVAIS_OK;
}
bravais_result_t bravais_frame_write_ping(bravais_wcur_t* cur) {
if (!writer_usable(cur)) { return BRAVAIS_ERR_INVALID_ARG; }
if (bravais_wcur_remaining(cur) < 1) { return BRAVAIS_ERR_BUFFER_TOO_SMALL; }
bravais_wcur_u8(cur, BRAVAIS_FRAME_TYPE_PING);
return BRAVAIS_OK;
}
bravais_result_t bravais_frame_write_crypto(bravais_wcur_t* cur, uint64_t offset, const uint8_t* data, size_t dataLen) {
if (!writer_usable(cur) || (!data && dataLen != 0)) { return BRAVAIS_ERR_INVALID_ARG; }
size_t size = bravais_frame_crypto_size(offset, dataLen);
if (size == 0) { return BRAVAIS_ERR_INVALID_ARG; }
if (size > bravais_wcur_remaining(cur)) { return BRAVAIS_ERR_BUFFER_TOO_SMALL; }
bravais_wcur_u8(cur, BRAVAIS_FRAME_TYPE_CRYPTO);
bravais_wcur_varint(cur, offset);
bravais_wcur_varint(cur, dataLen);
bravais_wcur_bytes(cur, data, dataLen);
return bravais_wcur_ok(cur) ? BRAVAIS_OK : BRAVAIS_ERR_UNKNOWN; // can't fail rule
}
bravais_result_t bravais_frame_write_datagram(bravais_wcur_t* cur, const uint8_t* data, size_t dataLen, bool includeLengthField) {
if (!writer_usable(cur) || (!data && dataLen != 0)) { return BRAVAIS_ERR_INVALID_ARG; }
size_t size = bravais_frame_crypto_size(offset, dataLen);
if (size == 0) { return BRAVAIS_ERR_INVALID_ARG; }
if (size > bravais_wcur_remaining(cur)) { return BRAVAIS_ERR_BUFFER_TOO_SMALL; }
if (includeLengthField) {
bravais_wcur_u8(cur, BRAVAIS_FRAME_TYPE_DATAGRAM_WITH_LENGTH);
bravais_wcur_varint(cur, dataLen);
} else {
bravais_wcur_u8(cur, BRAVAIS_FRAME_TYPE_DATAGRAM);
}
bravais_wcur_bytes(cur, data, dataLen);
return bravais_wcur_ok(cur) ? BRAVAIS_OK : BRAVAIS_ERR_UNKNOWN; // can't fail rule
}
+32 -4
View File
@@ -9,10 +9,15 @@
#include <stddef.h>
#include <stdint.h>
#include "bravais.h"
#include "util/buf.h"
typedef enum {
BRAVAIS_FRAME_TYPE_PADDING = 0x00,
BRAVAIS_FRAME_TYPE_CRYPTO = 0x06, // QUIC crypto frame
BRAVAIS_FRAME_TYPE_DATAGRAM = 0x31, // QUIC datagram frame
BRAVAIS_FRAME_TYPE_PING = 0x01,
BRAVAIS_FRAME_TYPE_CRYPTO = 0x06, // QUIC CRYPTO frame
BRAVAIS_FRAME_TYPE_DATAGRAM = 0x30, // QUIC datagram frame, parsed frames use this for both encodings
BRAVAIS_FRAME_TYPE_DATAGRAM_WITH_LENGTH = 0x31 // Wire value only, never the type of a parsed frame
} bravais_frame_type_t;
// Handshake bytes at a byte offset in the handshake stream. Offset + Length makes the fragmentation across datagrams to happen naturally.
@@ -31,12 +36,35 @@ typedef struct {
typedef struct {
bravais_frame_type_t type;
union {
size_t paddingLen; // For PADDING frames, the length of the padding, a consequtive run of PAdDING bytes (parsed as a single frame)
size_t paddingLen; // For PADDING frames, the length of the padding, a consequtive run of PAdDING bytes (parsed as a single frame), type bytes incl.
bravais_crypto_frame_t crypto;
bravais_datagram_frame_t datagram;
};
} bravais_frame_t;
// TODO: Functions or something
// Parse the next frame from a decrypted payload
bravais_result_t bravais_frame_parse(bravais_rcur_t* cur, bravais_frame_t* outFrame); // zeroes on failure
/* WRITING */
// Rule - write the whole frame or nothing at all. If you hit BRAVAIS_ERR_BUFFER_TOO_SMALL, don't fail the cursor (packetizer can try again this way).
// Encoded sizes, 0 if unencodable
size_t bravais_frame_crypto_size(uint64_t offset, size_t dataLen);
size_t bravais_frame_datagram_size(size_t dataLen, bool includeLengthField);
// The absolute most bytes a CRYPTO frame can fit. 0 if (logically) no bytes fit.
size_t bravais_frame_crypto_max_data(uint64_t offset, size_t maxFrameLen);
// len zero bytes. Any len works, a run of padding is a run of one-byte PADDING frames.
bravais_result_t bravais_frame_write_padding(bravais_wcur_t* cur, size_t len);
bravais_result_t bravais_frame_write_ping(bravais_wcur_t* cur);
// BRAVAIS_ERR_INVALID_ARG if offset + dataLen > BRAVAIS_VARINT_MAX
bravais_result_t bravais_frame_write_crypto(bravais_wcur_t* cur, uint64 offset, const uint8_t* data, size_t dataLen);
// includeLengthField - false writes type 0x30 (DATAGRAM), which runs to the end of the payload (must be last frame).
// It saves the length varint (matters if an IP packet only just fits).
bravais_result_t bravais_frame_write_datagram(bravais_wcur_t* cur, const uint8_t* data, size_t dataLen, bool includeLengthField);
#endif // BRAVAIS_FRAME_H