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
Go to the documentation of this file.
1
5
6#include <ascii-chat/common.h>
9
10#include <string.h>
11
12const char *
14 switch (protocol) {
16 return "TCP";
18 return "WebSocket";
20 return "QUIC";
21 default:
22 return "Unknown";
23 }
24}
25
27connection_endpoint_from_parts(connection_endpoint_t *endpoint_out,
28 const url_parts_t *parts,
30 uint16_t default_port, const char *input) {
31 if (!endpoint_out || !parts || !parts->host) {
32 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid endpoint parse state");
33 }
34
35 if (strlen(input) >= sizeof(endpoint_out->input) ||
36 strlen(parts->host) >= sizeof(endpoint_out->host)) {
37 return SET_ERRNO(
39 "Connection endpoint exceeds URL or host storage capacity");
40 }
41
42 memset(endpoint_out, 0, sizeof(*endpoint_out));
43 endpoint_out->protocol = protocol;
44 SAFE_STRNCPY(endpoint_out->input, input, sizeof(endpoint_out->input));
45 SAFE_STRNCPY(endpoint_out->host, parts->host, sizeof(endpoint_out->host));
46 endpoint_out->port = parts->port > 0 ? (uint16_t)parts->port : default_port;
47 endpoint_out->has_explicit_port = parts->port > 0;
48 return ASCIICHAT_OK;
49}
50
52connection_endpoint_resolve(const char *input, uint16_t default_port,
53 connection_endpoint_t *endpoint_out) {
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}
123
125 uint16_t port, char *output,
126 size_t output_size) {
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 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...
asciichat_error_t connection_endpoint_override_port(const char *input, uint16_t port, char *output, size_t output_size)
const char * connection_endpoint_protocol_name(connection_endpoint_protocol_t protocol)
Shared endpoint normalization for TCP and WebSocket connections.
connection_endpoint_protocol_t
@ CONNECTION_ENDPOINT_TCP
@ CONNECTION_ENDPOINT_QUIC
@ CONNECTION_ENDPOINT_WEBSOCKET
unsigned short uint16_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_SNPRINTF(buffer, buffer_size,...)
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
#define URL_BUFFER_SIZE
void url_parts_destroy(url_parts_t *parts)
Free parsed URL components.
Definition url.c:354
bool url_is_websocket(const char *url)
Check if a URL string is a valid WebSocket URL.
Definition url.c:380
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
bool url_is_tcp(const char *url)
Check if a URL string is a valid TCP URL.
Definition url.c:417
⚙️ Common definitions, error codes, macros, and types shared throughout the application
🔤 String Manipulation and Shell Escaping Utilities
connection_endpoint_protocol_t protocol
Structure containing parsed URL components.
Definition url.h:73
char * userinfo
Definition url.h:75
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
int port
Extracted port number (0 if not specified).
Definition url.h:80
🌐 Production-Grade URL Validation and Parsing