ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
datagram.c File Reference

Go to the source code of this file.

Macros

#define MAX_WIRE_PACKET   (sizeof(packet_header_t) + CRYPTO_MAX_PLAINTEXT_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE)
#define FLOW_BUDGET   (QUIC_DATAGRAM_BUDGET / 2)
#define FLOW_SLOTS   (QUIC_DATAGRAM_SLOTS / 2)

Functions

asciichat_error_t quic_datagram_fragment (acip_traffic_class_t flow, uint64_t id, const void *packet, size_t length, uint16_t unit, uint16_t index, void *out, size_t capacity, size_t *out_length)
void quic_datagram_expire (quic_datagram_reader_t *reader, uint64_t now_ns)
void quic_datagram_reset (quic_datagram_reader_t *reader)
asciichat_error_t quic_datagram_read (quic_datagram_reader_t *reader, const void *data, size_t length, uint64_t now_ns, acip_traffic_class_t *flow_out, uint8_t **out, size_t *out_length)

Macro Definition Documentation

◆ FLOW_BUDGET

#define FLOW_BUDGET   (QUIC_DATAGRAM_BUDGET / 2)

Definition at line 10 of file datagram.c.

Referenced by quic_datagram_read().

◆ FLOW_SLOTS

#define FLOW_SLOTS   (QUIC_DATAGRAM_SLOTS / 2)

Definition at line 11 of file datagram.c.

Referenced by quic_datagram_read().

◆ MAX_WIRE_PACKET

#define MAX_WIRE_PACKET   (sizeof(packet_header_t) + CRYPTO_MAX_PLAINTEXT_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE)

Definition at line 9 of file datagram.c.

Referenced by quic_datagram_fragment(), and quic_datagram_read().

Function Documentation

◆ quic_datagram_expire()

void quic_datagram_expire ( quic_datagram_reader_t * reader,
uint64_t now_ns )

Definition at line 83 of file datagram.c.

83 {
84 if (!reader || now_ns < reader->last_time_ns)
85 return;
86 reader->last_time_ns = now_ns;
87 for (size_t i = 0; i < QUIC_DATAGRAM_SLOTS; ++i) {
88 quic_datagram_slot_t *slot = &reader->slots[i];
89 if (slot->data && now_ns - slot->started_ns >= QUIC_DATAGRAM_EXPIRY_NS)
90 clear_slot(reader, slot);
91 }
92}
#define QUIC_DATAGRAM_EXPIRY_NS
Definition datagram.h:15
#define QUIC_DATAGRAM_SLOTS
Definition datagram.h:13
quic_datagram_slot_t slots[8U]
Definition datagram.h:35
uint64_t started_ns
Definition datagram.h:21

References quic_datagram_slot_t::data, quic_datagram_reader_t::last_time_ns, QUIC_DATAGRAM_EXPIRY_NS, QUIC_DATAGRAM_SLOTS, quic_datagram_reader_t::slots, and quic_datagram_slot_t::started_ns.

Referenced by quic_datagram_read().

◆ quic_datagram_fragment()

asciichat_error_t quic_datagram_fragment ( acip_traffic_class_t flow,
uint64_t id,
const void * packet,
size_t length,
uint16_t unit,
uint16_t index,
void * out,
size_t capacity,
size_t * out_length )

Definition at line 45 of file datagram.c.

45 {
46 if (!out_length)
47 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing QUIC datagram output length");
48 *out_length = 0;
49 if (!media_flow(flow) || !id || !packet || !length || length > MAX_WIRE_PACKET || !unit || !out)
50 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC datagram fragment parameters");
51 size_t count = (length + unit - 1) / unit;
52 if (count > QUIC_DATAGRAM_MAX_FRAGMENTS || index >= count)
53 return SET_ERRNO(ERROR_NETWORK_SIZE, "Invalid QUIC datagram fragment count or index");
54 size_t offset = (size_t)index * unit;
55 size_t part = length - offset < unit ? length - offset : unit;
56 if (capacity < QUIC_DATAGRAM_HEADER_SIZE + part)
57 return SET_ERRNO(ERROR_BUFFER_FULL, "QUIC datagram output buffer is too small");
58 uint8_t *wire = out;
59 memcpy(wire, "ACQD", sizeof("ACQD") - 1);
60 wire[4] = 1;
61 wire[5] = (uint8_t)flow;
62 wire[6] = wire[7] = 0;
63 put64(wire + 8, id);
64 put32(wire + 16, (uint32_t)length);
65 put16(wire + 20, unit);
66 put16(wire + 22, index);
67 put16(wire + 24, (uint16_t)count);
68 wire[26] = wire[27] = 0;
69 memcpy(wire + QUIC_DATAGRAM_HEADER_SIZE, (const uint8_t *)packet + offset, part);
70 *out_length = QUIC_DATAGRAM_HEADER_SIZE + part;
71 return ASCIICHAT_OK;
72}
#define SET_ERRNO(code,...)
#define MAX_WIRE_PACKET
Definition datagram.c:9
#define QUIC_DATAGRAM_HEADER_SIZE
Definition datagram.h:11
#define QUIC_DATAGRAM_MAX_FRAGMENTS
Definition datagram.h:12
unsigned short uint16_t
unsigned int uint32_t
unsigned char uint8_t
@ ERROR_BUFFER_FULL
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
@ ERROR_INVALID_PARAM
static bool size_t index
Definition queries.h:10
static bool size_t gotel_query_t * out
Definition queries.h:10

References ASCIICHAT_OK, ERROR_BUFFER_FULL, ERROR_INVALID_PARAM, ERROR_NETWORK_SIZE, index, MAX_WIRE_PACKET, out, QUIC_DATAGRAM_HEADER_SIZE, QUIC_DATAGRAM_MAX_FRAGMENTS, and SET_ERRNO.

◆ quic_datagram_read()

asciichat_error_t quic_datagram_read ( quic_datagram_reader_t * reader,
const void * data,
size_t length,
uint64_t now_ns,
acip_traffic_class_t * flow_out,
uint8_t ** out,
size_t * out_length )

Definition at line 102 of file datagram.c.

102 {
103 if (!out || !out_length || !flow_out)
104 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing QUIC datagram output");
105 *out = NULL;
106 *out_length = 0;
107 if (!reader || !data || length < QUIC_DATAGRAM_HEADER_SIZE || now_ns < reader->last_time_ns)
108 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC datagram input");
109 const uint8_t *wire = data;
111 uint64_t id = get64(wire + 8);
112 uint32_t total = get32(wire + 16);
113 uint16_t unit = get16(wire + 20), index = get16(wire + 22), count = get16(wire + 24);
114 if (memcmp(wire, "ACQD", sizeof("ACQD") - 1) || wire[4] != 1 || !media_flow(flow) || wire[6] || wire[7] || wire[26] || wire[27] || !id || !total || total > MAX_WIRE_PACKET || !unit || !count ||
115 count > QUIC_DATAGRAM_MAX_FRAGMENTS || count != ((size_t)total + unit - 1) / unit || index >= count)
116 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid QUIC datagram envelope");
117 size_t offset = (size_t)index * unit;
118 size_t part = total - offset < unit ? total - offset : unit;
119 if (length != QUIC_DATAGRAM_HEADER_SIZE + part)
120 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid QUIC datagram fragment size");
121 quic_datagram_expire(reader, now_ns);
122 size_t first = flow == ACIP_TRAFFIC_AUDIO ? 0 : FLOW_SLOTS;
123 quic_datagram_slot_t *slot = NULL, *empty = NULL;
124 size_t flow_bytes = 0;
125 for (size_t i = first; i < first + FLOW_SLOTS; ++i) {
126 quic_datagram_slot_t *candidate = &reader->slots[i];
127 flow_bytes += candidate->cost;
128 if (candidate->data && candidate->id == id)
129 slot = candidate;
130 else if (!candidate->data && !empty)
131 empty = candidate;
132 }
133 if (slot && (slot->total != total || slot->unit != unit || slot->count != count))
134 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Conflicting QUIC datagram fragments");
135 if (!slot) {
136 if (!empty || total > FLOW_BUDGET - flow_bytes)
137 return SET_ERRNO(ERROR_BUFFER_FULL, "QUIC datagram reassembly budget exhausted");
138 size_t bitmap_size = (count + 7U) / 8U;
139 uint8_t *packet = buffer_alloc(NULL, total);
140 uint8_t *bitmap = buffer_alloc(NULL, bitmap_size);
141 if (!packet || !bitmap) {
142 buffer_release(packet);
143 buffer_release(bitmap);
144 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate QUIC datagram reassembly");
145 }
146 size_t cost = buffer_capacity(packet) + buffer_capacity(bitmap);
147 if (cost > FLOW_BUDGET - flow_bytes) {
148 buffer_release(packet);
149 buffer_release(bitmap);
150 return SET_ERRNO(ERROR_BUFFER_FULL, "QUIC datagram reassembly budget exhausted");
151 }
152 memset(bitmap, 0, bitmap_size);
153 *empty = (quic_datagram_slot_t){.data = packet, .received = bitmap, .cost = cost, .id = id, .started_ns = now_ns, .total = total, .unit = unit, .count = count, .traffic_class = flow};
154 reader->bytes += cost;
155 slot = empty;
156 }
157 uint8_t mask = (uint8_t)(1U << (index % 8));
158 if (slot->received[index / 8] & mask) {
159 if (memcmp(slot->data + offset, wire + QUIC_DATAGRAM_HEADER_SIZE, part))
160 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Conflicting duplicate QUIC datagram fragment");
161 return ASCIICHAT_OK;
162 }
163 memcpy(slot->data + offset, wire + QUIC_DATAGRAM_HEADER_SIZE, part);
164 slot->received[index / 8] |= mask;
165 if (++slot->received_count == slot->count) {
166 *out = slot->data;
167 *out_length = slot->total;
168 *flow_out = flow;
169 slot->data = NULL;
170 reader->bytes -= slot->cost;
172 memset(slot, 0, sizeof(*slot));
173 }
174 return ASCIICHAT_OK;
175}
size_t buffer_capacity(const void *data)
Definition buffer.c:300
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:294
#define buffer_release(data)
Definition buffer.h:108
#define FLOW_BUDGET
Definition datagram.c:10
void quic_datagram_expire(quic_datagram_reader_t *reader, uint64_t now_ns)
Definition datagram.c:83
#define FLOW_SLOTS
Definition datagram.c:11
unsigned long long uint64_t
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ERROR_MEMORY
Definition error_codes.h:55
uint16_t received_count
Definition datagram.h:23
uint8_t * received
Definition datagram.h:19
acip_traffic_class_t
Definition traffic.h:9
@ ACIP_TRAFFIC_AUDIO
Definition traffic.h:11

References ACIP_TRAFFIC_AUDIO, ASCIICHAT_OK, buffer_alloc(), buffer_capacity(), buffer_release, quic_datagram_reader_t::bytes, quic_datagram_slot_t::cost, quic_datagram_slot_t::count, quic_datagram_slot_t::data, ERROR_BUFFER_FULL, ERROR_INVALID_PARAM, ERROR_MEMORY, ERROR_NETWORK_PROTOCOL, FLOW_BUDGET, FLOW_SLOTS, quic_datagram_slot_t::id, index, MAX_WIRE_PACKET, out, quic_datagram_expire(), QUIC_DATAGRAM_HEADER_SIZE, QUIC_DATAGRAM_MAX_FRAGMENTS, quic_datagram_slot_t::received, quic_datagram_slot_t::received_count, SET_ERRNO, quic_datagram_reader_t::slots, quic_datagram_slot_t::total, and quic_datagram_slot_t::unit.

◆ quic_datagram_reset()

void quic_datagram_reset ( quic_datagram_reader_t * reader)

Definition at line 94 of file datagram.c.

94 {
95 if (!reader)
96 return;
97 for (size_t i = 0; i < QUIC_DATAGRAM_SLOTS; ++i)
98 clear_slot(reader, &reader->slots[i]);
99 memset(reader, 0, sizeof(*reader));
100}

References QUIC_DATAGRAM_SLOTS, and quic_datagram_reader_t::slots.

Referenced by quic_connection_destroy().