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

Shared endpoint normalization for TCP and WebSocket connections. More...

Go to the source code of this file.

Functions

const char * connection_endpoint_protocol_name (connection_endpoint_protocol_t protocol)
asciichat_error_t connection_endpoint_resolve (const char *input, uint16_t default_port, connection_endpoint_t *endpoint_out)
 For connection endpoint, this is the live record that ties protocol, port, has_explicit_port together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record. Resolve an input string into a normalized TCP or WebSocket endpoint.
asciichat_error_t connection_endpoint_override_port (const char *input, uint16_t port, char *output, size_t output_size)

Detailed Description

Shared endpoint normalization for TCP and WebSocket connections.

Definition in file connection_endpoint.c.

Function Documentation

◆ connection_endpoint_override_port()

asciichat_error_t connection_endpoint_override_port ( const char * input,
uint16_t port,
char * output,
size_t output_size )

Build an equivalent endpoint URL with an explicitly overridden port.

Definition at line 124 of file connection_endpoint.c.

126 {
127 if (!input || !*input || !output || output_size == 0 || port == 0)
128 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid endpoint port override");
129 url_parts_t parts = {0};
130 asciichat_error_t result = url_parse(input, &parts);
131 if (result != ASCIICHAT_OK)
132 return result;
133 const char *host = parts.ipv6 ? parts.ipv6 : parts.host;
134 const char *path = parts.path ? parts.path : "";
135 result = (safe_snprintf(output, output_size, "%s://%s%s%s:%u%s", parts.scheme,
136 parts.ipv6 ? "[" : "", host, parts.ipv6 ? "]" : "",
137 ":", (unsigned)port, path) < 0)
139 : ASCIICHAT_OK;
140 url_parts_destroy(&parts);
141 return result;
142}
#define SET_ERRNO(code,...)
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
void url_parts_destroy(url_parts_t *parts)
Free parsed URL components.
Definition url.c:354
asciichat_error_t url_parse(const char *url, url_parts_t *parts_out)
Parse HTTP(S), WebSocket, and TCP URL into components.
Definition url.c:188
Structure containing parsed URL components.
Definition url.h:73
char * path
Definition url.h:81
char * ipv6
Definition url.h:78
char * scheme
Extracted scheme: "http" or "https" (allocated).
Definition url.h:74
char * host
Canonical ASCII hostname or IP address (allocated).
Definition url.h:77

References ASCIICHAT_OK, ERROR_INVALID_PARAM, url_parts_t::host, url_parts_t::ipv6, url_parts_t::path, url_parts_t::scheme, SET_ERRNO, url_parse(), and url_parts_destroy().

Referenced by client_crypto_init(), and discovery_main().

◆ connection_endpoint_protocol_name()

const char * connection_endpoint_protocol_name ( connection_endpoint_protocol_t protocol)

Definition at line 13 of file connection_endpoint.c.

13 {
14 switch (protocol) {
16 return "TCP";
18 return "WebSocket";
20 return "QUIC";
21 default:
22 return "Unknown";
23 }
24}
@ CONNECTION_ENDPOINT_TCP
@ CONNECTION_ENDPOINT_QUIC
@ CONNECTION_ENDPOINT_WEBSOCKET

References CONNECTION_ENDPOINT_QUIC, CONNECTION_ENDPOINT_TCP, and CONNECTION_ENDPOINT_WEBSOCKET.

◆ connection_endpoint_resolve()

asciichat_error_t connection_endpoint_resolve ( const char * input,
uint16_t default_port,
connection_endpoint_t * endpoint_out )

For connection endpoint, this is the live record that ties protocol, port, has_explicit_port together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record. Resolve an input string into a normalized TCP or WebSocket endpoint.

Supported inputs:

  • ws://host[:port]
  • wss://host[:port]
  • tcp://host[:port]
  • quic://host[:port]
  • bare hostnames or host:port pairs

Bare hostnames are treated as TCP endpoints and use default_port when the input does not specify a port.

Definition at line 52 of file connection_endpoint.c.

53 {
54 if (!input || !*input || !endpoint_out) {
55 return SET_ERRNO(ERROR_INVALID_PARAM, "input or endpoint_out is NULL");
56 }
57
58 url_parts_t parts = {0};
59 asciichat_error_t result;
60
61 if (strncasecmp(input, "quic://", sizeof("quic://") - 1) == 0) {
62 char rewritten[URL_BUFFER_SIZE];
63 if (strlen(input) - 1 >= sizeof(rewritten))
64 return SET_ERRNO(ERROR_INVALID_PARAM, "QUIC endpoint is too long");
65 SAFE_SNPRINTF(rewritten, sizeof(rewritten), "tcp://%s", input + 7);
66 result = url_parse(rewritten, &parts);
67 if (result != ASCIICHAT_OK)
68 return result;
69 if (parts.userinfo || (parts.path && *parts.path)) {
70 url_parts_destroy(&parts);
71 return SET_ERRNO(
73 "QUIC endpoint must not include user information or a path");
74 }
75 result = connection_endpoint_from_parts(
76 endpoint_out, &parts, CONNECTION_ENDPOINT_QUIC, default_port, input);
77 if (result == ASCIICHAT_OK && parts.ipv6)
78 SAFE_STRNCPY(endpoint_out->host, parts.ipv6, sizeof(endpoint_out->host));
79 url_parts_destroy(&parts);
80 return result;
81 }
82
83 if (url_is_websocket(input)) {
84 result = url_parse(input, &parts);
85 if (result != ASCIICHAT_OK) {
86 return result;
87 }
88 result = connection_endpoint_from_parts(endpoint_out, &parts,
90 default_port, input);
91 url_parts_destroy(&parts);
92 return result;
93 }
94
95 if (url_is_tcp(input)) {
96 result = url_parse(input, &parts);
97 if (result != ASCIICHAT_OK) {
98 return result;
99 }
100 result = connection_endpoint_from_parts(
101 endpoint_out, &parts, CONNECTION_ENDPOINT_TCP, default_port, input);
102 url_parts_destroy(&parts);
103 return result;
104 }
105
106 /* Unsupported explicit scheme. Bare hostnames are allowed and have no "://".
107 */
108 if (strstr(input, "://") != NULL) {
109 return SET_ERRNO(ERROR_INVALID_PARAM, "Unsupported connection scheme: %s",
110 input);
111 }
112
113 result = url_parse(input, &parts);
114 if (result != ASCIICHAT_OK) {
115 return result;
116 }
117
118 result = connection_endpoint_from_parts(
119 endpoint_out, &parts, CONNECTION_ENDPOINT_TCP, default_port, input);
120 url_parts_destroy(&parts);
121 return result;
122}
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_SNPRINTF(buffer, buffer_size,...)
#define URL_BUFFER_SIZE
bool url_is_websocket(const char *url)
Check if a URL string is a valid WebSocket URL.
Definition url.c:380
bool url_is_tcp(const char *url)
Check if a URL string is a valid TCP URL.
Definition url.c:417
char * userinfo
Definition url.h:75

References ASCIICHAT_OK, CONNECTION_ENDPOINT_QUIC, CONNECTION_ENDPOINT_TCP, CONNECTION_ENDPOINT_WEBSOCKET, ERROR_INVALID_PARAM, connection_endpoint_t::host, url_parts_t::ipv6, url_parts_t::path, SAFE_SNPRINTF, SAFE_STRNCPY, SET_ERRNO, URL_BUFFER_SIZE, url_is_tcp(), url_is_websocket(), url_parse(), url_parts_destroy(), and url_parts_t::userinfo.

Referenced by client_crypto_init(), network_connection_open(), and session_connection_establish().