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

Go to the source code of this file.

Functions

asciichat_error_t session_quic_listeners_open (int port, quic_endpoint_t **native, quic_endpoint_t **webtransport)
asciichat_error_t session_quic_listeners_close (quic_endpoint_t **native, quic_endpoint_t **webtransport)

Function Documentation

◆ session_quic_listeners_close()

asciichat_error_t session_quic_listeners_close ( quic_endpoint_t ** native,
quic_endpoint_t ** webtransport )

Definition at line 57 of file quic_listeners.c.

57 {
58 if (!native || !webtransport)
59 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing session QUIC listener outputs");
60 quic_endpoint_t **endpoints[] = {webtransport, native};
61 for (size_t i = 0; i < 2; ++i) {
62 if (!*endpoints[i])
63 continue;
64 asciichat_error_t result = quic_endpoint_stop(*endpoints[i]);
65 if (result == ASCIICHAT_OK)
66 result = quic_endpoint_destroy(endpoints[i]);
67 if (result != ASCIICHAT_OK)
68 return result;
69 }
70 return ASCIICHAT_OK;
71}
#define SET_ERRNO(code,...)
asciichat_error_t quic_endpoint_stop(quic_endpoint_t *endpoint)
Definition endpoint.c:289
asciichat_error_t quic_endpoint_destroy(quic_endpoint_t **endpoint)
Definition endpoint.c:334
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
struct quic_endpoint quic_endpoint_t

References ASCIICHAT_OK, ERROR_INVALID_PARAM, quic_endpoint_destroy(), quic_endpoint_stop(), and SET_ERRNO.

Referenced by session_host_stop(), and session_server_like_run().

◆ session_quic_listeners_open()

asciichat_error_t session_quic_listeners_open ( int port,
quic_endpoint_t ** native,
quic_endpoint_t ** webtransport )

Definition at line 7 of file quic_listeners.c.

7 {
8 if (!native || !webtransport || *native || *webtransport || port < 1 || port > UINT16_MAX)
9 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid session QUIC listener state");
10 const char *native_address = GET_OPTION(quic_bind_address);
11 const char *browser_address = GET_OPTION(webtransport_bind_address);
12 const char *origin = GET_OPTION(webtransport_origin);
13 const char *path = GET_OPTION(webtransport_path);
14 const char *certificate = GET_OPTION(tls_cert);
15 const char *key = GET_OPTION(tls_key);
16 int quic_port = GET_OPTION(quic_port);
17 bool browser = *browser_address || *origin;
18 bool requested = *native_address || browser || quic_port;
19 if (!requested) {
20 return ASCIICHAT_OK;
21 }
22 if (quic_port < 0 || quic_port > UINT16_MAX)
23 return SET_ERRNO(ERROR_USAGE, "QUIC port must be between 0 and 65535");
24 const char *address = *native_address ? native_address : browser_address;
25 if (!*address || !*certificate || !*key)
26 return SET_ERRNO(ERROR_USAGE, "QUIC requires a bind address, --tls-cert, and --tls-key");
27 if (browser && (!*origin || !*path))
28 return SET_ERRNO(ERROR_USAGE, "WebTransport requires --webtransport-origin and --webtransport-path");
29 if (*native_address && *browser_address && strcmp(native_address, browser_address))
30 return SET_ERRNO(ERROR_USAGE, "Native QUIC and WebTransport share --quic-port and must use the same bind address");
31 struct sockaddr_storage bind_address;
32 socklen_t bind_length = 0;
33 uint16_t selected_port = (uint16_t)(quic_port ? quic_port : port);
34 asciichat_error_t result = parse_numeric_socket_address(address, selected_port, &bind_address, &bind_length);
35 if (result != ASCIICHAT_OK)
36 return result;
37 quic_config_t config = {.certificate_file = certificate,
38 .private_key_file = key,
39 .webtransport_origin = browser ? origin : NULL,
40 .webtransport_path = browser ? path : NULL,
41 .enable_datagrams = true,
42 .accept_native = browser,
43 .prefer_audio_datagrams = GET_OPTION(quic_audio_datagrams),
44 .prefer_visual_datagrams = GET_OPTION(quic_visual_datagrams)};
45 // One endpoint owns the UDP socket and dispatches each connection by ALPN.
46 // Keep the second output empty so teardown and accept never alias ownership.
47 result = quic_endpoint_open((const struct sockaddr *)&bind_address, bind_length, true, &config, native);
48 if (result != ASCIICHAT_OK)
49 return result;
50 log_info("Native QUIC listener initialized on %s UDP port %u", address, selected_port);
51 if (browser)
52 log_info("WebTransport listener initialized on %s UDP port %u (shared QUIC listener)", address, selected_port);
53
54 return ASCIICHAT_OK;
55}
asciichat_error_t quic_endpoint_open(const struct sockaddr *address, socklen_t length, bool listener, const quic_config_t *config, quic_endpoint_t **out)
Definition endpoint.c:128
unsigned short uint16_t
@ ERROR_USAGE
Definition error_codes.h:52
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define GET_OPTION(field)
Definition options.h:1339
asciichat_error_t parse_numeric_socket_address(const char *host, uint16_t port, struct sockaddr_storage *address, socklen_t *length)
Definition ip.c:155

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_USAGE, GET_OPTION, log_info, parse_numeric_socket_address(), quic_endpoint_open(), and SET_ERRNO.

Referenced by session_host_start(), and session_server_like_run().