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

Go to the source code of this file.

Typedefs

typedef struct quic_udp_endpoint quic_udp_endpoint_t
typedef asciichat_error_t(* quic_udp_accept_fn) (quic_connection_t *, void *)

Functions

asciichat_error_t quic_udp_endpoint_create (const struct sockaddr *bind_address, socklen_t bind_length, quic_tls_t *server_tls, quic_udp_accept_fn accept, void *context, quic_udp_endpoint_t **out)
asciichat_error_t quic_udp_endpoint_address (const quic_udp_endpoint_t *endpoint, struct sockaddr_storage *address, socklen_t *length)
asciichat_error_t quic_udp_connect (quic_udp_endpoint_t *endpoint, quic_tls_t *client_tls, const char *server_name, const struct sockaddr *remote, socklen_t remote_length, uint64_t now_ns, quic_connection_t **out)
asciichat_error_t quic_udp_pump (quic_udp_endpoint_t *endpoint, uint64_t now_ns, quic_connection_t **failed)
asciichat_error_t quic_udp_wait_parameters (const quic_udp_endpoint_t *endpoint, uint64_t now_ns, uint64_t maximum_wait_ns, uint64_t *wait_ns, bool *want_write)
asciichat_error_t quic_udp_wait (quic_udp_endpoint_t *endpoint, uint64_t wait_ns, bool want_write, bool *ready)
asciichat_error_t quic_udp_wake (quic_udp_endpoint_t *endpoint)
bool quic_udp_contains (const quic_udp_endpoint_t *endpoint, const quic_connection_t *connection)
asciichat_error_t quic_udp_close_peer (quic_udp_endpoint_t *endpoint, quic_connection_t *connection)
asciichat_error_t quic_udp_remove (quic_udp_endpoint_t *endpoint, quic_connection_t *connection)
void quic_udp_endpoint_destroy (quic_udp_endpoint_t **endpoint)
asciichat_error_t quic_udp_migrate (quic_udp_endpoint_t *endpoint, const struct sockaddr *address, socklen_t length, uint64_t now_ns)

Typedef Documentation

◆ quic_udp_accept_fn

typedef asciichat_error_t(* quic_udp_accept_fn) (quic_connection_t *, void *)

Definition at line 15 of file udp.h.

◆ quic_udp_endpoint_t

Definition at line 14 of file udp.h.

Function Documentation

◆ quic_udp_close_peer()

asciichat_error_t quic_udp_close_peer ( quic_udp_endpoint_t * endpoint,
quic_connection_t * connection )

Definition at line 320 of file udp.c.

320 {
321 if (!quic_udp_contains(endpoint, connection))
322 return SET_ERRNO(ERROR_INVALID_PARAM, "QUIC UDP connection is not owned by endpoint");
324 ngtcp2_path_storage path;
325 size_t length = 0;
326 asciichat_error_t result = quic_connection_write_close(connection, &path, packet, sizeof(packet), &length, time_get_ns());
327 if (result != ASCIICHAT_OK || !length)
328 return result;
329 ngtcp2_addr *remote = &path.path.remote;
330 ssize_t sent = socket_sendto(endpoint->socket, packet, length, 0, remote->addr, (socklen_t)remote->addrlen);
331 if (sent < 0 || (size_t)sent != length)
332 return SET_ERRNO(ERROR_NETWORK, "Cannot send QUIC close packet");
333 return ASCIICHAT_OK;
334}
#define SET_ERRNO(code,...)
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NETWORK
Definition error_codes.h:77
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
asciichat_error_t quic_connection_write_close(quic_connection_t *connection, ngtcp2_path_storage *path, void *packet, size_t capacity, size_t *length, uint64_t now_ns)
socket_t socket
Definition udp.c:34
bool quic_udp_contains(const quic_udp_endpoint_t *endpoint, const quic_connection_t *connection)
Definition udp.c:311
#define QUIC_UDP_PACKET_CAPACITY
Definition udp.c:10

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_NETWORK, quic_connection_write_close(), quic_udp_contains(), QUIC_UDP_PACKET_CAPACITY, SET_ERRNO, and quic_udp_endpoint::socket.

◆ quic_udp_connect()

asciichat_error_t quic_udp_connect ( quic_udp_endpoint_t * endpoint,
quic_tls_t * client_tls,
const char * server_name,
const struct sockaddr * remote,
socklen_t remote_length,
uint64_t now_ns,
quic_connection_t ** out )

Definition at line 137 of file udp.c.

138 {
139 if (!out)
140 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing QUIC UDP connection output");
141 *out = NULL;
142 if (!endpoint || !client_tls || client_tls->server || !valid_sockaddr(remote, remote_length) || remote->sa_family != ((struct sockaddr *)&endpoint->local)->sa_family)
143 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC UDP connection configuration");
144 udp_peer_t *peer = new_peer(endpoint);
145 if (!peer)
146 return SET_ERRNO(ERROR_BUFFER_FULL, "QUIC UDP connection limit reached or allocation failed");
147 ngtcp2_path path = make_path(endpoint, remote, remote_length);
148 asciichat_error_t result = quic_connection_client_create(client_tls, server_name, &path, now_ns, &peer->connection);
149 if (result != ASCIICHAT_OK) {
150 SAFE_FREE(peer);
151 return result;
152 }
153 add_peer(endpoint, peer);
154 *out = peer->connection;
155 return ASCIICHAT_OK;
156}
#define SAFE_FREE(ptr)
@ ERROR_BUFFER_FULL
asciichat_error_t quic_connection_client_create(quic_tls_t *tls, const char *server_name, const ngtcp2_path *path, uint64_t now_ns, quic_connection_t **out)
static bool size_t gotel_query_t * out
Definition queries.h:10
bool server
Definition tls.h:15
struct sockaddr_storage local
Definition udp.c:35
quic_connection_t * connection
Definition udp.c:27
struct udp_peer udp_peer_t
acip quic udp endpoint is intentionally a small, concrete record. Fields such as socket,...

References ASCIICHAT_OK, udp_peer::connection, ERROR_BUFFER_FULL, ERROR_INVALID_PARAM, quic_udp_endpoint::local, out, quic_connection_client_create(), SAFE_FREE, quic_tls_t::server, and SET_ERRNO.

Referenced by quic_connect().

◆ quic_udp_contains()

bool quic_udp_contains ( const quic_udp_endpoint_t * endpoint,
const quic_connection_t * connection )

Definition at line 311 of file udp.c.

311 {
312 if (!endpoint || !connection)
313 return false;
314 for (const udp_peer_t *peer = endpoint->peers; peer; peer = peer->next)
315 if (peer->connection == connection)
316 return true;
317 return false;
318}
udp_peer_t * peers
Definition udp.c:41
struct udp_peer * next
Definition udp.c:26

References udp_peer::connection, udp_peer::next, and quic_udp_endpoint::peers.

Referenced by quic_udp_close_peer().

◆ quic_udp_endpoint_address()

asciichat_error_t quic_udp_endpoint_address ( const quic_udp_endpoint_t * endpoint,
struct sockaddr_storage * address,
socklen_t * length )

Definition at line 103 of file udp.c.

103 {
104 if (!endpoint || !address || !length)
105 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC UDP address output");
106 *address = endpoint->local;
107 *length = endpoint->local_length;
108 return ASCIICHAT_OK;
109}
socklen_t local_length
Definition udp.c:36

References ASCIICHAT_OK, ERROR_INVALID_PARAM, quic_udp_endpoint::local, quic_udp_endpoint::local_length, and SET_ERRNO.

Referenced by quic_endpoint_address().

◆ quic_udp_endpoint_create()

asciichat_error_t quic_udp_endpoint_create ( const struct sockaddr * bind_address,
socklen_t bind_length,
quic_tls_t * server_tls,
quic_udp_accept_fn accept,
void * context,
quic_udp_endpoint_t ** out )

Definition at line 66 of file udp.c.

66 {
67 if (!out)
68 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing QUIC UDP endpoint output");
69 *out = NULL;
70 if (!valid_sockaddr(address, length) || (!!server_tls != !!accept) || (server_tls && !server_tls->server))
71 return SET_ERRNO(ERROR_INVALID_PARAM, "QUIC UDP requires a concrete bind address and valid listener");
72 quic_udp_endpoint_t *endpoint = SAFE_CALLOC(1, sizeof(*endpoint), quic_udp_endpoint_t *);
73 if (!endpoint)
74 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate QUIC UDP endpoint");
75 endpoint->socket = socket_create("quic_udp", address->sa_family, SOCK_DGRAM, 0);
76 if (endpoint->socket == INVALID_SOCKET_VALUE || socket_bind(endpoint->socket, address, length) != 0 || socket_set_nonblocking(endpoint->socket, true) != 0) {
77 if (endpoint->socket != INVALID_SOCKET_VALUE)
78 socket_close(endpoint->socket);
79 SAFE_FREE(endpoint);
80 return SET_ERRNO(ERROR_NETWORK, "Cannot bind QUIC UDP endpoint");
81 }
82 endpoint->local_length = sizeof(endpoint->local);
83 if (socket_getsockname(endpoint->socket, (struct sockaddr *)&endpoint->local, &endpoint->local_length) != 0) {
84 socket_close(endpoint->socket);
85 SAFE_FREE(endpoint);
86 return SET_ERRNO(ERROR_NETWORK, "Cannot obtain QUIC UDP endpoint address");
87 }
88 endpoint->server_tls = server_tls;
89 if (server_tls) {
90 if (sodium_init() < 0) {
91 socket_close(endpoint->socket);
92 SAFE_FREE(endpoint);
93 return SET_ERRNO(ERROR_CRYPTO, "Cannot initialize QUIC Retry token secret");
94 }
95 randombytes_buf(endpoint->retry_secret, sizeof(endpoint->retry_secret));
96 }
97 endpoint->accept = accept;
98 endpoint->context = context;
99 *out = endpoint;
100 return ASCIICHAT_OK;
101}
#define SAFE_CALLOC(count, size, cast)
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_CRYPTO
int socket_close(socket_t)
quic_tls_t * server_tls
Definition udp.c:37
uint8_t retry_secret[32]
Definition udp.c:38
void * context
Definition udp.c:40
quic_udp_accept_fn accept
Definition udp.c:39
struct quic_udp_endpoint quic_udp_endpoint_t
Definition udp.h:14

References quic_udp_endpoint::accept, ASCIICHAT_OK, quic_udp_endpoint::context, ERROR_CRYPTO, ERROR_INVALID_PARAM, ERROR_MEMORY, ERROR_NETWORK, quic_udp_endpoint::local, quic_udp_endpoint::local_length, out, quic_udp_endpoint::retry_secret, SAFE_CALLOC, SAFE_FREE, quic_tls_t::server, quic_udp_endpoint::server_tls, SET_ERRNO, quic_udp_endpoint::socket, and socket_close().

Referenced by quic_endpoint_open(), and quic_udp_migrate().

◆ quic_udp_endpoint_destroy()

void quic_udp_endpoint_destroy ( quic_udp_endpoint_t ** endpoint)

Definition at line 352 of file udp.c.

352 {
353 if (!endpoint || !*endpoint)
354 return;
355 while ((*endpoint)->peers)
356 (void)quic_udp_remove(*endpoint, (*endpoint)->peers->connection);
357 socket_close((*endpoint)->socket);
358 sodium_memzero((*endpoint)->retry_secret, sizeof((*endpoint)->retry_secret));
359 SAFE_FREE(*endpoint);
360 *endpoint = NULL;
361}
asciichat_error_t quic_udp_remove(quic_udp_endpoint_t *endpoint, quic_connection_t *connection)
Definition udp.c:336

References udp_peer::connection, quic_udp_endpoint::peers, quic_udp_remove(), SAFE_FREE, and socket_close().

Referenced by quic_endpoint_destroy(), quic_endpoint_open(), and quic_udp_migrate().

◆ quic_udp_migrate()

asciichat_error_t quic_udp_migrate ( quic_udp_endpoint_t * endpoint,
const struct sockaddr * address,
socklen_t length,
uint64_t now_ns )

Definition at line 363 of file udp.c.

363 {
364 if (!endpoint || endpoint->server_tls || endpoint->count != 1 || !valid_sockaddr(address, length))
365 return SET_ERRNO(ERROR_INVALID_PARAM, "QUIC migration requires a single client connection and concrete address");
366 if (endpoint->peers->pending_length)
367 return SET_ERRNO(ERROR_BUFFER_FULL, "Flush the pending UDP packet before migrating");
368 quic_udp_endpoint_t *candidate = NULL;
369 asciichat_error_t result = quic_udp_endpoint_create(address, length, NULL, NULL, NULL, &candidate);
370 if (result != ASCIICHAT_OK)
371 return result;
372 result = quic_connection_migrate(endpoint->peers->connection, (struct sockaddr *)&candidate->local, candidate->local_length, now_ns);
373 if (result == ASCIICHAT_OK) {
374 socket_t old_socket = endpoint->socket;
375 endpoint->socket = candidate->socket;
376 candidate->socket = old_socket;
377 endpoint->local = candidate->local;
378 endpoint->local_length = candidate->local_length;
379 }
380 quic_udp_endpoint_destroy(&candidate);
381 return result;
382}
int socket_t
asciichat_error_t quic_connection_migrate(quic_connection_t *connection, const struct sockaddr *local, socklen_t length, uint64_t now_ns)
size_t count
Definition udp.c:42
size_t pending_length
Definition udp.c:30
void quic_udp_endpoint_destroy(quic_udp_endpoint_t **endpoint)
Definition udp.c:352
asciichat_error_t quic_udp_endpoint_create(const struct sockaddr *address, socklen_t length, quic_tls_t *server_tls, quic_udp_accept_fn accept, void *context, quic_udp_endpoint_t **out)
Definition udp.c:66

References ASCIICHAT_OK, udp_peer::connection, quic_udp_endpoint::count, ERROR_BUFFER_FULL, ERROR_INVALID_PARAM, quic_udp_endpoint::local, quic_udp_endpoint::local_length, quic_udp_endpoint::peers, udp_peer::pending_length, quic_connection_migrate(), quic_udp_endpoint_create(), quic_udp_endpoint_destroy(), quic_udp_endpoint::server_tls, SET_ERRNO, and quic_udp_endpoint::socket.

Referenced by quic_endpoint_migrate().

◆ quic_udp_pump()

asciichat_error_t quic_udp_pump ( quic_udp_endpoint_t * endpoint,
uint64_t now_ns,
quic_connection_t ** failed )

Definition at line 209 of file udp.c.

209 {
210 if (!endpoint || !failed)
211 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC UDP pump arguments");
212 *failed = NULL;
213 for (unsigned i = 0; i < QUIC_UDP_READ_BATCH; ++i) {
214 uint8_t data[65536];
215 struct sockaddr_storage remote = {0};
216 socklen_t remote_length = sizeof(remote);
217 ssize_t received = socket_recvfrom(endpoint->socket, data, sizeof(data), 0, (struct sockaddr *)&remote, &remote_length);
218 if (received < 0) {
219 int error = socket_get_last_error();
221 break;
222 /* An ICMP error for an earlier datagram does not identify a QUIC
223 * connection and must not terminate a shared listener. */
224 if (socket_is_connection_reset_error(error))
225 continue;
226 return SET_ERRNO(ERROR_NETWORK, "QUIC UDP receive failed");
227 }
228 if (!received || !valid_sockaddr((struct sockaddr *)&remote, remote_length))
229 continue;
230 asciichat_error_t result = receive_one(endpoint, data, (size_t)received, (struct sockaddr *)&remote, remote_length, now, failed);
231 if (result != ASCIICHAT_OK)
232 return result;
233 }
234 for (udp_peer_t *peer = endpoint->peers; peer; peer = peer->next) {
235 if (quic_connection_expiry(peer->connection) <= now) {
236 asciichat_error_t result = quic_connection_handle_expiry(peer->connection, now);
237 if (result != ASCIICHAT_OK) {
238 *failed = peer->connection;
239 return result;
240 }
241 }
242 for (unsigned i = 0; i < QUIC_UDP_WRITE_BATCH; ++i) {
243 if (!peer->pending_length) {
244 asciichat_error_t result = quic_connection_write(peer->connection, &peer->pending_path, peer->pending, sizeof(peer->pending), &peer->pending_length, now);
245 if (result != ASCIICHAT_OK) {
246 *failed = peer->connection;
247 return result;
248 }
249 if (!peer->pending_length)
250 break;
251 }
252 ngtcp2_addr *remote = &peer->pending_path.path.remote;
253 ssize_t sent = socket_sendto(endpoint->socket, peer->pending, peer->pending_length, 0, remote->addr, (socklen_t)remote->addrlen);
255 break;
256 if (sent < 0 || (size_t)sent != peer->pending_length)
257 return SET_ERRNO(ERROR_NETWORK, "QUIC UDP send failed");
258 peer->pending_length = 0;
259 asciichat_error_t result = quic_connection_packet_sent(peer->connection, now);
260 if (result != ASCIICHAT_OK) {
261 *failed = peer->connection;
262 return result;
263 }
264 }
265 }
266 return ASCIICHAT_OK;
267}
asciichat_error_t quic_connection_handle_expiry(quic_connection_t *connection, uint64_t now_ns)
asciichat_error_t quic_connection_write(quic_connection_t *connection, ngtcp2_path_storage *path, void *packet, size_t capacity, size_t *length, uint64_t now_ns)
asciichat_error_t quic_connection_packet_sent(quic_connection_t *connection, uint64_t now_ns)
uint64_t quic_connection_expiry(const quic_connection_t *connection)
bool socket_is_would_block_error(int)
int socket_get_last_error(void)
#define QUIC_UDP_READ_BATCH
Definition udp.c:12
#define QUIC_UDP_WRITE_BATCH
Definition udp.c:13

References ASCIICHAT_OK, udp_peer::connection, ERROR_INVALID_PARAM, ERROR_NETWORK, udp_peer::next, quic_udp_endpoint::peers, udp_peer::pending, udp_peer::pending_length, udp_peer::pending_path, quic_connection_expiry(), quic_connection_handle_expiry(), quic_connection_packet_sent(), quic_connection_write(), QUIC_UDP_READ_BATCH, QUIC_UDP_WRITE_BATCH, SET_ERRNO, quic_udp_endpoint::socket, socket_get_last_error(), and socket_is_would_block_error().

◆ quic_udp_remove()

asciichat_error_t quic_udp_remove ( quic_udp_endpoint_t * endpoint,
quic_connection_t * connection )

Definition at line 336 of file udp.c.

336 {
337 if (!endpoint || !connection)
338 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC UDP removal");
339 udp_peer_t **next = &endpoint->peers;
340 while (*next && (*next)->connection != connection)
341 next = &(*next)->next;
342 if (!*next)
343 return SET_ERRNO(ERROR_INVALID_PARAM, "QUIC UDP connection is not owned by endpoint");
344 udp_peer_t *peer = *next;
345 *next = peer->next;
346 --endpoint->count;
348 SAFE_FREE(peer);
349 return ASCIICHAT_OK;
350}
void quic_connection_destroy(quic_connection_t **connection)

References ASCIICHAT_OK, udp_peer::connection, quic_udp_endpoint::count, ERROR_INVALID_PARAM, udp_peer::next, quic_udp_endpoint::peers, quic_connection_destroy(), SAFE_FREE, and SET_ERRNO.

Referenced by quic_connect(), quic_endpoint_stop(), quic_release(), and quic_udp_endpoint_destroy().

◆ quic_udp_wait()

asciichat_error_t quic_udp_wait ( quic_udp_endpoint_t * endpoint,
uint64_t wait_ns,
bool want_write,
bool * ready )

Definition at line 286 of file udp.c.

286 {
287 if (!endpoint || !ready)
288 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC UDP wait");
289 *ready = false;
290 struct pollfd fd = {.fd = endpoint->socket, .events = (short)(POLLIN | (want_write ? POLLOUT : 0))};
291 int64_t timeout = wait_ns > INT64_MAX ? INT64_MAX : (int64_t)wait_ns;
292 int result = socket_poll(&fd, 1, timeout);
293 if (result < 0)
294 return SET_ERRNO(ERROR_NETWORK, "QUIC UDP poll failed");
295 if (result > 0 && (fd.revents & (POLLERR | POLLNVAL)))
296 return SET_ERRNO(ERROR_NETWORK, "QUIC UDP socket poll reported an error");
297 *ready = result > 0;
298 return ASCIICHAT_OK;
299}
int socket_poll(struct pollfd *, nfds_t, int64_t)

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_NETWORK, SET_ERRNO, quic_udp_endpoint::socket, and socket_poll().

◆ quic_udp_wait_parameters()

asciichat_error_t quic_udp_wait_parameters ( const quic_udp_endpoint_t * endpoint,
uint64_t now_ns,
uint64_t maximum_wait_ns,
uint64_t * wait_ns,
bool * want_write )

Definition at line 269 of file udp.c.

269 {
270 if (!endpoint || !wait_ns || !want_write || now_ns == UINT64_MAX)
271 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC UDP wait parameters");
272 *wait_ns = maximum_wait_ns;
273 *want_write = false;
274 for (const udp_peer_t *peer = endpoint->peers; peer; peer = peer->next) {
275 uint64_t expiry = quic_connection_expiry(peer->connection);
276 if (expiry <= now_ns)
277 *wait_ns = 0;
278 else if (expiry != UINT64_MAX && expiry - now_ns < *wait_ns)
279 *wait_ns = expiry - now_ns;
280 if (peer->pending_length)
281 *want_write = true;
282 }
283 return ASCIICHAT_OK;
284}
unsigned long long uint64_t

References ASCIICHAT_OK, udp_peer::connection, ERROR_INVALID_PARAM, udp_peer::next, quic_udp_endpoint::peers, udp_peer::pending_length, quic_connection_expiry(), and SET_ERRNO.

◆ quic_udp_wake()

asciichat_error_t quic_udp_wake ( quic_udp_endpoint_t * endpoint)

Definition at line 301 of file udp.c.

301 {
302 if (!endpoint)
303 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid QUIC UDP wake");
304 const uint8_t wake = 0;
305 ssize_t sent = socket_sendto(endpoint->socket, &wake, sizeof(wake), 0, (struct sockaddr *)&endpoint->local, endpoint->local_length);
306 if (sent != (ssize_t)sizeof(wake))
307 return SET_ERRNO(ERROR_NETWORK, "Cannot wake QUIC UDP endpoint");
308 return ASCIICHAT_OK;
309}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_NETWORK, quic_udp_endpoint::local, quic_udp_endpoint::local_length, SET_ERRNO, and quic_udp_endpoint::socket.

Referenced by quic_worker_wake().