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.h File Reference

Go to the source code of this file.

Functions

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.
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.
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.

Function Documentation

◆ signaling_broadcast()

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.

Sends packet to all participants in a session (optionally excluding one). Used internally by SDP/ICE relay when recipient_id is all zeros.

Parameters
dbSQLite database handle (for session validation)
serverShared connection registry
session_idSession UUID
packet_typePacket type to send
packetPacket data to broadcast
packet_lenPacket length
exclude_participant_idParticipant to exclude (NULL = no exclusion, typically sender)
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 213 of file signaling.c.

214 {
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}
#define SET_ERRNO(code,...)
session_entry_t * database_session_find_by_id(sqlite3 *db, const uint8_t session_id[16])
Find session by session_id.
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:80
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
struct session_entry session_entry_t
Persisted discovery-session record used to admit and route participants.
void session_entry_destroy(session_entry_t *entry)
Free a session entry and all its resources.
void network_server_foreach(network_server_t *s, network_connection_visit_fn visit, void *context)
uint8_t session_id[SESSION_ID_SIZE]
Context for broadcasting to session participants.
Definition signaling.c:75
size_t sent_count
Number of successful sends.
Definition signaling.c:81

References ASCIICHAT_OK, database_session_find_by_id(), ERROR_INVALID_PARAM, ERROR_NETWORK_PROTOCOL, log_debug, log_warn, network_server_foreach(), broadcast_context_t::sent_count, session_entry_destroy(), session_id, SESSION_ID_SIZE, and SET_ERRNO.

Referenced by signaling_relay_ice(), and signaling_relay_sdp().

◆ signaling_relay_ice()

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.

Relays ICE packet to specific participant or broadcasts to all participants in the session if recipient_id is all zeros.

Parameters
dbSQLite database handle (for session validation)
serverShared connection registry
iceICE packet to relay (with variable-length candidate data following struct)
total_packet_lenTotal packet length including variable data
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 174 of file signaling.c.

174 {
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}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NETWORK
Definition error_codes.h:76
@ PACKET_TYPE_ACIP_WEBRTC_ICE
WebRTC ICE candidate (bidirectional).
Definition packet.h:466
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
Context for finding client by participant_id.
Definition signaling.c:39
bool found
Whether client was found.
Definition signaling.c:45
asciichat_error_t result
Definition signaling.c:44

References acip_webrtc_ice_t, ASCIICHAT_OK, database_session_find_by_id(), ERROR_INVALID_PARAM, ERROR_NETWORK, ERROR_NETWORK_PROTOCOL, find_client_context_t::found, log_debug, network_server_foreach(), PACKET_TYPE_ACIP_WEBRTC_ICE, find_client_context_t::result, session_entry_destroy(), SET_ERRNO, and signaling_broadcast().

◆ signaling_relay_sdp()

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.

Relays SDP packet to specific participant or broadcasts to all participants in the session if recipient_id is all zeros.

Parameters
dbSQLite database handle (for session validation)
serverShared connection registry
sdpSDP packet to relay (with variable-length SDP data following struct)
total_packet_lenTotal packet length including variable data
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 132 of file signaling.c.

132 {
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}
@ PACKET_TYPE_ACIP_WEBRTC_SDP
WebRTC SDP offer/answer (bidirectional).
Definition packet.h:464

References acip_webrtc_sdp_t, ASCIICHAT_OK, database_session_find_by_id(), ERROR_INVALID_PARAM, ERROR_NETWORK, ERROR_NETWORK_PROTOCOL, find_client_context_t::found, log_debug, network_server_foreach(), PACKET_TYPE_ACIP_WEBRTC_SDP, find_client_context_t::result, session_entry_destroy(), SET_ERRNO, and signaling_broadcast().