ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
signaling.c
Go to the documentation of this file.
1/* Discovery signaling through pinned connection-registry visits.
2 * Sends run outside the registry mutex while participant state remains alive. */
3
10#include <ascii-chat/log/log.h>
13#include <string.h>
14
18static bool is_broadcast_uuid(const uint8_t uuid[SESSION_ID_SIZE]) {
19 for (int i = 0; i < 16; i++) {
20 if (uuid[i] != 0) {
21 return false;
22 }
23 }
24 return true;
25}
26
32static bool uuid_equals(const uint8_t a[SESSION_ID_SIZE], const uint8_t b[SESSION_ID_SIZE]) {
33 return memcmp(a, b, SESSION_ID_SIZE) == 0;
34}
35
47
51static void find_participant_callback(network_connection_t *connection, void *user_arg) {
52 void *client_data = network_connection_context(connection);
54
55 if (!client_data) {
56 return; // Client not yet joined a session
57 }
58
59 acds_client_data_t *acds_data = (acds_client_data_t *)client_data;
60 if (!acds_data->joined_session) {
61 return; // Client connected but hasn't joined
62 }
63
64 // Check if participant_id matches
65 if (uuid_equals(acds_data->participant_id, ctx->target_participant_id)) {
66 ctx->found = true;
67 // The registry visitor pins the connection through packet admission.
68 ctx->result = packet_send_via_transport(acds_data->transport, ctx->type, ctx->packet, ctx->length, 0);
69 }
70}
71
83
87static void broadcast_callback(network_connection_t *connection, void *user_arg) {
88 void *client_data = network_connection_context(connection);
90
91 if (!client_data) {
92 log_debug("Broadcast: socket has no client_data");
93 return; // Client not yet joined a session
94 }
95
96 acds_client_data_t *acds_data = (acds_client_data_t *)client_data;
97 if (!acds_data->joined_session) {
98 log_debug("Broadcast: client not joined (joined_session=false)");
99 return; // Client connected but hasn't joined
100 }
101
102 if (!acds_data->transport) {
103 log_warn("Broadcast: client has no transport");
104 return;
105 }
106
107 log_debug("Broadcast: checking participant=%02x%02x... (session=%02x%02x...)", acds_data->participant_id[0], acds_data->participant_id[1], acds_data->session_id[0], acds_data->session_id[1]);
108
109 // Check if client is in target session
110 if (!uuid_equals(acds_data->session_id, ctx->target_session_id)) {
111 log_debug("Broadcast: client in different session");
112 return; // Different session
113 }
114
115 // Skip excluded participant (e.g., sender)
116 if (ctx->exclude_participant_id && uuid_equals(acds_data->participant_id, ctx->exclude_participant_id)) {
117 log_debug("Broadcast: excluding sender (participant=%02x%02x...)", acds_data->participant_id[0], acds_data->participant_id[1]);
118 return; // Skip sender
119 }
120
121 // Send packet through transport (not raw socket)
122 log_debug("★ BROADCAST_CALLBACK: sending packet_type=%u to participant=%02x%02x...", ctx->packet_type, acds_data->participant_id[0], acds_data->participant_id[1]);
123 asciichat_error_t result = packet_send_via_transport(acds_data->transport, ctx->packet_type, ctx->packet, ctx->packet_len, 0);
124 if (result == ASCIICHAT_OK) {
125 ctx->sent_count++;
126 log_debug("★ BROADCAST_CALLBACK: successfully sent to participant=%02x%02x...", acds_data->participant_id[0], acds_data->participant_id[1]);
127 } else {
128 log_warn("★ BROADCAST_CALLBACK_FAILED: Failed to send to participant=%02x%02x...: %s", acds_data->participant_id[0], acds_data->participant_id[1], asciichat_error_format(NULL, result));
129 }
130}
131
132asciichat_error_t signaling_relay_sdp(sqlite3 *db, network_server_t *network_server, const acip_webrtc_sdp_t *sdp, size_t total_packet_len) {
133 if (!db || !network_server || !sdp) {
134 return SET_ERRNO(ERROR_INVALID_PARAM, "db, network server, or sdp is NULL");
135 }
136
137 /* Find session by UUID using database lookup */
138 log_debug("SDP relay: Looking up session_id=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", sdp->session_id[0], sdp->session_id[1], sdp->session_id[2], sdp->session_id[3],
139 sdp->session_id[4], sdp->session_id[5], sdp->session_id[6], sdp->session_id[7], sdp->session_id[8], sdp->session_id[9], sdp->session_id[10], sdp->session_id[11], sdp->session_id[12],
140 sdp->session_id[13], sdp->session_id[14], sdp->session_id[15]);
141 session_entry_t *session = database_session_find_by_id(db, sdp->session_id);
142 if (!session) {
143 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Session not found for SDP relay");
144 }
145 session_entry_destroy(session); // We only need to validate existence
146
147 // Check if broadcast (recipient_id all zeros) or unicast
148 if (is_broadcast_uuid(sdp->recipient_id)) {
149 // Broadcast to all participants in session (except sender)
150 log_debug("Broadcasting SDP to all participants in session (excluding sender)");
151 return signaling_broadcast(db, network_server, sdp->session_id, PACKET_TYPE_ACIP_WEBRTC_SDP, sdp, total_packet_len, sdp->sender_id);
152 } else {
153 // Unicast to specific recipient
154 find_client_context_t ctx = {.target_participant_id = sdp->recipient_id, .found = false, .type = PACKET_TYPE_ACIP_WEBRTC_SDP, .packet = sdp, .length = total_packet_len};
155
156 network_server_foreach(network_server, find_participant_callback, &ctx);
157
158 if (!ctx.found) {
159 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Recipient participant not found (may be offline)");
160 }
161
162 // Send SDP packet to recipient
163 asciichat_error_t result = ctx.result;
164 if (result != ASCIICHAT_OK) {
165 return SET_ERRNO(ERROR_NETWORK, "Failed to send SDP packet to recipient");
166 }
167
168 log_debug("Relayed SDP to participant");
169 }
170
171 return ASCIICHAT_OK;
172}
173
174asciichat_error_t signaling_relay_ice(sqlite3 *db, network_server_t *network_server, const acip_webrtc_ice_t *ice, size_t total_packet_len) {
175 if (!db || !network_server || !ice) {
176 return SET_ERRNO(ERROR_INVALID_PARAM, "db, network server, or ice is NULL");
177 }
178
179 /* Find session by UUID using database lookup */
180 session_entry_t *session = database_session_find_by_id(db, ice->session_id);
181 if (!session) {
182 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Session not found for ICE relay");
183 }
184 session_entry_destroy(session); // We only need to validate existence
185
186 // Check if broadcast (recipient_id all zeros) or unicast
187 if (is_broadcast_uuid(ice->recipient_id)) {
188 // Broadcast to all participants in session (except sender)
189 log_debug("Broadcasting ICE candidate to all participants in session (excluding sender)");
190 return signaling_broadcast(db, network_server, ice->session_id, PACKET_TYPE_ACIP_WEBRTC_ICE, ice, total_packet_len, ice->sender_id);
191 } else {
192 // Unicast to specific recipient
193 find_client_context_t ctx = {.target_participant_id = ice->recipient_id, .found = false, .type = PACKET_TYPE_ACIP_WEBRTC_ICE, .packet = ice, .length = total_packet_len};
194
195 network_server_foreach(network_server, find_participant_callback, &ctx);
196
197 if (!ctx.found) {
198 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Recipient participant not found (may be offline)");
199 }
200
201 // Send ICE packet to recipient
202 asciichat_error_t result = ctx.result;
203 if (result != ASCIICHAT_OK) {
204 return SET_ERRNO(ERROR_NETWORK, "Failed to send ICE packet to recipient");
205 }
206
207 log_debug("Relayed ICE candidate to participant");
208 }
209
210 return ASCIICHAT_OK;
211}
212
213asciichat_error_t signaling_broadcast(sqlite3 *db, network_server_t *network_server, const uint8_t session_id[SESSION_ID_SIZE], packet_type_t packet_type, const void *packet, size_t packet_len,
214 const uint8_t *exclude_participant_id) {
215 if (!db || !network_server || !session_id || !packet) {
216 return SET_ERRNO(ERROR_INVALID_PARAM, "db, network server, session_id, or packet is NULL");
217 }
218
219 log_debug("★ SIGNALING_BROADCAST_START: packet_type=%u, packet_len=%zu, session=%02x%02x...", packet_type, packet_len, session_id[0], session_id[1]);
220 if (exclude_participant_id) {
221 log_debug("★ SIGNALING_BROADCAST: excluding participant=%02x%02x...", exclude_participant_id[0], exclude_participant_id[1]);
222 }
223
224 /* Find session by UUID using database lookup */
226 if (!session) {
227 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Session not found for broadcast");
228 }
229 session_entry_destroy(session); // We only need to validate existence
230
231 // Broadcast to all participants in session (excluding sender if specified)
232 broadcast_context_t ctx = {
233 .target_session_id = session_id, .exclude_participant_id = exclude_participant_id, .packet_type = packet_type, .packet = packet, .packet_len = packet_len, .sent_count = 0};
234
235 network_server_foreach(network_server, broadcast_callback, &ctx);
236
237 if (ctx.sent_count == 0) {
238 log_warn("★ SIGNALING_BROADCAST_COMPLETE: sent to 0 participants (all offline or not joined yet)");
239 } else {
240 log_debug("★ SIGNALING_BROADCAST_COMPLETE: successfully sent to %zu participants", ctx.sent_count);
241 }
242
243 return ASCIICHAT_OK;
244}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
#define SET_ERRNO(code,...)
Application limits and constraints.
session_entry_t * database_session_find_by_id(sqlite3 *db, const uint8_t session_id[16])
Find session by session_id.
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:76
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:80
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define SESSION_ID_SIZE
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
acip_webrtc_ice_t
Definition acds.h:655
acip_webrtc_sdp_t
Definition acds.h:609
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
@ PACKET_TYPE_ACIP_WEBRTC_ICE
WebRTC ICE candidate (bidirectional).
Definition packet.h:466
@ PACKET_TYPE_ACIP_WEBRTC_SDP
WebRTC SDP offer/answer (bidirectional).
Definition packet.h:464
struct session_entry session_entry_t
Persisted discovery-session record used to admit and route participants.
struct network_server network_server_t
struct network_connection network_connection_t
void session_entry_destroy(session_entry_t *entry)
Free a session entry and all its resources.
void * network_connection_context(network_connection_t *c)
void network_server_foreach(network_server_t *s, network_connection_visit_fn visit, void *context)
📝 Logging API with multiple log levels and terminal output control
🌐 Core network I/O operations with timeout support
asciichat_error_t packet_send_via_transport(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, uint32_t client_id)
Definition send.c:183
ACIP shared/bidirectional packet sending functions.
asciichat_error_t signaling_relay_ice(sqlite3 *db, network_server_t *network_server, const acip_webrtc_ice_t *ice, size_t total_packet_len)
Relay ICE candidate to recipient.
Definition signaling.c:174
asciichat_error_t signaling_relay_sdp(sqlite3 *db, network_server_t *network_server, const acip_webrtc_sdp_t *sdp, size_t total_packet_len)
Relay SDP offer/answer to recipient.
Definition signaling.c:132
asciichat_error_t signaling_broadcast(sqlite3 *db, network_server_t *network_server, const uint8_t session_id[SESSION_ID_SIZE], packet_type_t packet_type, const void *packet, size_t packet_len, const uint8_t *exclude_participant_id)
Broadcast packet to all session participants.
Definition signaling.c:213
uint8_t session_id[SESSION_ID_SIZE]
🔤 String Manipulation and Shell Escaping Utilities
Network statistics for both TCP and WebSocket connections.
bool joined_session
Whether client has successfully joined a session.
uint8_t participant_id[SESSION_ID_SIZE]
Participant UUID (valid if joined_session).
acip_transport_t * transport
Persistent transport for this client connection.
uint8_t session_id[SESSION_ID_SIZE]
Session UUID (valid if joined_session).
Context for broadcasting to session participants.
Definition signaling.c:75
packet_type_t packet_type
Packet type to send.
Definition signaling.c:78
size_t packet_len
Packet length.
Definition signaling.c:80
const uint8_t * target_session_id
Session to broadcast to.
Definition signaling.c:76
const void * packet
Packet data.
Definition signaling.c:79
const uint8_t * exclude_participant_id
Participant to exclude (NULL = no exclusion).
Definition signaling.c:77
size_t sent_count
Number of successful sends.
Definition signaling.c:81
Context for finding client by participant_id.
Definition signaling.c:39
const void * packet
Definition signaling.c:42
const uint8_t * target_participant_id
Participant to find.
Definition signaling.c:40
bool found
Whether client was found.
Definition signaling.c:45
asciichat_error_t result
Definition signaling.c:44
packet_type_t type
Definition signaling.c:41