cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
webrtc_internal.h File Reference

Internal types and cross-file helpers for the WebRTC DataChannel module (ICE-lite + DTLS + SCTP over UDP). More...

#include <cwist/net/webrtc.h>
#include <cwist/sys/io/reactor.h>
#include <netinet/in.h>
#include <openssl/ssl.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
Include dependency graph for webrtc_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  cwist_webrtc_msg
 A DataChannel message on its way into SCTP. More...
 
struct  cwist_webrtc_conn
 One peer: ICE state, DTLS session, SCTP association. More...
 
struct  cwist_webrtc_ctx
 A UDP endpoint bound to one reactor, serving many conns. More...
 
struct  cwist_sdp_info
 Fields the ICE-lite agent needs from an SDP description. More...
 

Macros

#define CWIST_WEBRTC_MAX_MESSAGE   262144
 
#define CWIST_WEBRTC_MAX_BUFFERED   (4u << 20)
 
#define CWIST_WEBRTC_DGRAM_MAX   2048
 
#define CWIST_WEBRTC_RX_BATCH   32
 
#define CWIST_WEBRTC_TX_BATCH   64
 
STUN message types (ice.c)
#define CWIST_STUN_BINDING_REQUEST   0x0001
 
#define CWIST_STUN_BINDING_RESPONSE   0x0101
 

Typedefs

typedef struct cwist_webrtc_msg cwist_webrtc_msg
 A DataChannel message on its way into SCTP.
 

Enumerations

enum  cwist_conn_state { CWIST_CONN_STUN = 0 , CWIST_CONN_DTLS = 1 , CWIST_CONN_ESTABLISHED = 2 , CWIST_CONN_CLOSED = 3 }
 

Functions

ICE-lite STUN (ice.c)
int cwist_ice_stun_is_message (const uint8_t *buf, size_t len)
 Non-zero if buf looks like a STUN message (magic cookie, length).
 
bool cwist_ice_stun_is_binding_request (const uint8_t *buf, size_t len)
 true if buf is a STUN Binding request.
 
bool cwist_ice_stun_has_use_candidate (const uint8_t *buf, size_t len)
 true if the request carries USE-CANDIDATE (nomination).
 
int cwist_ice_stun_get_remote_ufrag (const uint8_t *buf, size_t len, char *out, size_t cap)
 Extract the sender's ufrag from USERNAME ("recipient:sender", RFC 8445 section 7.2.2) – the peer's SDP ice-ufrag.
 
int cwist_ice_stun_validate_request (const uint8_t *buf, size_t len, const char *pwd)
 Check MESSAGE-INTEGRITY (HMAC-SHA1 keyed with pwd).
 
int cwist_ice_stun_build_response (uint8_t *out, size_t cap, const uint8_t *req, size_t req_len, const struct sockaddr_in *mapped, const char *pwd)
 Build a signed Binding success response with XOR-MAPPED-ADDRESS.
 
int cwist_ice_stun_build_request (uint8_t *out, size_t cap, const uint8_t txid[12], const char *username)
 Build an unsigned Binding request (USERNAME, USE-CANDIDATE, ...).
 
int cwist_ice_stun_sign_request (uint8_t *msg, size_t cap, size_t msg_len, const char *pwd)
 Append MESSAGE-INTEGRITY and FINGERPRINT to a request.
 
int cwist_ice_stun_parse_response (const uint8_t *buf, size_t len, const uint8_t txid[12], const char *pwd, struct sockaddr_in *mapped)
 Validate a response: cookie, txid match, MI with pwd, return mapped address.
 
void cwist_ice_random_creds (char *ufrag, size_t ufrag_cap, char *pwd, size_t pwd_cap)
 Generate random ICE credentials (ice-chars only).
 
SDP (sdp.c)
int cwist_sdp_parse (const char *sdp, size_t len, cwist_sdp_info *info)
 Parse the fields the ICE-lite agent needs from an SDP description.
 
int cwist_sdp_write_answer (char *out, size_t cap, const char *fingerprint, const char *ufrag, const char *pwd, const char *mid, const char *host, uint16_t port)
 Write an ICE-lite, DTLS-passive DataChannel answer.
 
int cwist_sdp_write_offer (char *out, size_t cap, const char *fingerprint, const char *ufrag, const char *pwd, const char *mid)
 Write a DTLS-active DataChannel offer (loopback tests).
 
DTLS (dtls.c)
int cwist_dtls_generate_cert (X509 **cert, EVP_PKEY **pkey)
 Generate an ephemeral self-signed P-256 certificate.
 
int cwist_dtls_fingerprint (X509 *cert, char *out, size_t cap)
 SHA-256 fingerprint of cert in SDP form ("AB:CD:...").
 
SSL_CTX * cwist_dtls_ctx_new (int is_server, X509 *cert, EVP_PKEY *pkey)
 DTLS 1.2 SSL_CTX for the server (with cert) or client role.
 
BIO * cwist_dtls_bio_new (struct cwist_webrtc_conn *conn)
 Datagram BIO for conn: reads return conn->dtls_in once, writes go to cwist_webrtc_conn_dtls_out() one datagram per call.
 
unsigned int cwist_dtls_link_mtu (void)
 DTLS link MTU (bytes per datagram) used for handshake fragmentation.
 
SCTP (sctp.c)
int cwist_sctp_global_init (void)
 One-time usrsctp initialisation (no threads, AF_CONN only).
 
void cwist_sctp_tick (void)
 Advance usrsctp's process-wide timer clock to now. Any thread.
 
int cwist_sctp_conn_open (struct cwist_webrtc_conn *conn)
 Create, bind and listen/connect the conn's SCTP socket.
 
void cwist_sctp_conn_close (struct cwist_webrtc_conn *conn)
 Abort the association and release SCTP state. Idempotent.
 
void cwist_sctp_conn_input (struct cwist_webrtc_conn *conn, const uint8_t *data, size_t len)
 Feed one decrypted SCTP packet to usrsctp.
 
int cwist_sctp_conn_drain (struct cwist_webrtc_conn *conn)
 Accept (server role), then deliver every complete inbound message.
 
bool cwist_sctp_assoc_established (struct cwist_webrtc_conn *conn)
 true once the SCTP association is up.
 
int cwist_sctp_send (struct cwist_webrtc_conn *conn, uint16_t channel, const uint8_t *data, size_t len, int is_string)
 Send one message, announcing the channel with DCEP OPEN first if we are opening it.
 
Cross-file helpers (webrtc.c)
bool cwist_webrtc_on_owner (const cwist_webrtc_ctx *ctx)
 true when the calling thread is servicing ctx right now.
 
void cwist_webrtc_conn_dtls_out (struct cwist_webrtc_conn *conn, const uint8_t *data, size_t len)
 Queue one datagram to the conn's peer (owner thread).
 
void cwist_webrtc_conn_sctp_out (struct cwist_webrtc_conn *conn, const void *buffer, size_t len)
 usrsctp output for conn; marshalled to the owner thread when needed.
 
void cwist_webrtc_conn_on_message (struct cwist_webrtc_conn *conn, uint16_t channel, const uint8_t *data, size_t len, int is_string)
 Deliver a complete inbound message to the message handler.
 
void cwist_webrtc_conn_on_channel_open (struct cwist_webrtc_conn *conn, uint16_t channel, const char *label)
 Report a channel the peer opened to the channel handler.
 
cwist_webrtc_conn * cwist_webrtc_connect (cwist_webrtc_ctx *ctx, const struct sockaddr_in *remote, const char *peer_ufrag, const char *peer_pwd)
 Offering-side dialer used by the loopback tests: registers a client conn to remote and starts ICE.
 

Detailed Description

Internal types and cross-file helpers for the WebRTC DataChannel module (ICE-lite + DTLS + SCTP over UDP).

Threading model: every ctx is bound to one cwist reactor, and all mutable ctx/conn state is touched only on that reactor's run thread (the "owner"). Other threads reach a ctx or conn through cwist_reactor_post() nodes: cwist_webrtc_conn_send(), cwist_webrtc_conn_close(), cwist_webrtc_handle_offer() and cwist_webrtc_ctx_free() all post when called off the owner thread and act inline when called on it.

Lifetimes are reference counted. A conn holds a reference on its ctx; the ctx's connection table holds a reference on each registered conn; every in-flight post holds a reference on the object it targets. Teardown (closing sockets, freeing SSL/SCTP state) happens on the owner thread and is separate from freeing the memory, which happens on the last release.

Not part of the public API; only the module's .c files and its tests include this header.

Definition in file webrtc_internal.h.

Macro Definition Documentation

◆ CWIST_STUN_BINDING_REQUEST

#define CWIST_STUN_BINDING_REQUEST   0x0001

STUN Binding request.

Definition at line 195 of file webrtc_internal.h.

◆ CWIST_STUN_BINDING_RESPONSE

#define CWIST_STUN_BINDING_RESPONSE   0x0101

STUN Binding success response.

Definition at line 196 of file webrtc_internal.h.

◆ CWIST_WEBRTC_DGRAM_MAX

#define CWIST_WEBRTC_DGRAM_MAX   2048

Largest UDP datagram handled; bigger ones are dropped.

Definition at line 48 of file webrtc_internal.h.

◆ CWIST_WEBRTC_MAX_BUFFERED

#define CWIST_WEBRTC_MAX_BUFFERED   (4u << 20)

Per-conn cap on bytes queued by cwist_webrtc_conn_send() and not yet taken by SCTP. Sends beyond it fail instead of growing the queue.

Definition at line 46 of file webrtc_internal.h.

◆ CWIST_WEBRTC_MAX_MESSAGE

#define CWIST_WEBRTC_MAX_MESSAGE   262144

Largest DataChannel message accepted either way; advertised in the SDP as a=max-message-size.

Definition at line 43 of file webrtc_internal.h.

◆ CWIST_WEBRTC_RX_BATCH

#define CWIST_WEBRTC_RX_BATCH   32

Datagrams received per recvmmsg() call.

Definition at line 50 of file webrtc_internal.h.

◆ CWIST_WEBRTC_TX_BATCH

#define CWIST_WEBRTC_TX_BATCH   64

Datagrams queued before a sendmmsg() flush.

Definition at line 52 of file webrtc_internal.h.

Typedef Documentation

◆ cwist_webrtc_msg

A DataChannel message on its way into SCTP.

Foreign-thread sends travel through the conn's inbox in the same node they wait in on the pending queue, so a queued send costs one allocation.

Enumeration Type Documentation

◆ cwist_conn_state

Connection lifecycle.

Enumerator
CWIST_CONN_STUN 

Waiting for ICE nomination (or a STUN response, client).

CWIST_CONN_DTLS 

DTLS handshake in flight.

CWIST_CONN_ESTABLISHED 

DTLS up; SCTP pending or up.

CWIST_CONN_CLOSED 

Torn down.

Definition at line 55 of file webrtc_internal.h.

Function Documentation

◆ cwist_dtls_bio_new()

BIO * cwist_dtls_bio_new ( struct cwist_webrtc_conn *  conn)

Datagram BIO for conn: reads return conn->dtls_in once, writes go to cwist_webrtc_conn_dtls_out() one datagram per call.

Definition at line 211 of file dtls.c.

References cap.

◆ cwist_dtls_ctx_new()

SSL_CTX * cwist_dtls_ctx_new ( int  is_server,
X509 *  cert,
EVP_PKEY *  pkey 
)

DTLS 1.2 SSL_CTX for the server (with cert) or client role.

DTLS 1.2 SSL_CTX for the server (with cert) or client role.

Parameters
is_serverNon-zero to build a DTLS server context, zero for a DTLS client context.
certServer certificate; used (with pkey) only when is_server is non-zero. May be NULL for a client.
pkeyPrivate key matching cert; only used for a server.
Returns
New SSL_CTX on success, NULL on allocation failure or when certificate/key loading or validation fails on the server side.
Note
The context is pinned to DTLS 1.2 as the minimum protocol version. Client contexts use SSL_VERIFY_NONE: peer fingerprint verification is deferred to the SDP exchange (the test pins the expected fingerprint out of band).

Definition at line 120 of file dtls.c.

References cap, SSL_CTX_check_private_key, SSL_CTX_free, SSL_CTX_new, SSL_CTX_set_min_proto_version, SSL_CTX_set_verify, and SSL_VERIFY_NONE.

◆ cwist_dtls_fingerprint()

int cwist_dtls_fingerprint ( X509 *  cert,
char *  out,
size_t  cap 
)

SHA-256 fingerprint of cert in SDP form ("AB:CD:...").

Returns
0 or -1.

SHA-256 fingerprint of cert in SDP form ("AB:CD:...").

Parameters
certCertificate to hash; must be non-NULL.
outOutput buffer receiving the fingerprint text.
capCapacity of out in bytes (including the NUL terminator).
Returns
0 on success, -1 if the DER encoding fails, the certificate exceeds the internal 2048-byte DER buffer, or the digest does not fit in out.
Return values
0Fingerprint written, e.g. "AB:12:...:EF" in uppercase hex.
-1Encoding, size, or digest error.
Note
The result is the colon-separated uppercase hex SHA-256 digest as used in the SDP a=fingerprint attribute. No terminator is written beyond what snprintf() already emits.

Definition at line 90 of file dtls.c.

References cap, EVP_MAX_MD_SIZE, and EVP_sha256.

◆ cwist_dtls_generate_cert()

int cwist_dtls_generate_cert ( X509 **  cert_out,
EVP_PKEY **  pkey_out 
)

Generate an ephemeral self-signed P-256 certificate.

Returns
0 or -1.

Generate an ephemeral self-signed P-256 certificate.

Parameters
cert_outReceives a new X509 certificate; set to NULL on entry and on failure.
pkey_outReceives the matching EVP_PKEY; set to NULL on entry and on failure.
Returns
0 on success, -1 on any OpenSSL error.
Note
The key is P-256 (NID_X9_62_prime256v1). The certificate has version 3 (v2 encoding), serial 1, a 30-day validity starting now, and CN "cwist-webrtc". On failure all intermediate objects are freed and both outputs remain NULL; on success ownership moves to the caller, who must free them with X509_free()/EVP_PKEY_free().

Definition at line 36 of file dtls.c.

References cap, and EVP_sha256.

◆ cwist_dtls_link_mtu()

unsigned int cwist_dtls_link_mtu ( void  )

DTLS link MTU (bytes per datagram) used for handshake fragmentation.

DTLS link MTU (bytes per datagram) used for handshake fragmentation.

Returns
The value of CWIST_DTLS_MTU (1200).

Definition at line 142 of file dtls.c.

References CWIST_DTLS_MTU.

◆ cwist_ice_random_creds()

void cwist_ice_random_creds ( char *  ufrag,
size_t  ufrag_cap,
char *  pwd,
size_t  pwd_cap 
)

Generate random ICE credentials (ice-chars only).

Generate random ICE credentials (ice-chars only).

Both strings are drawn from 24 bytes of CSPRNG output encoded with the base64url alphabet (6 bits per character).

Parameters
ufragOutput buffer for the ufrag.
ufrag_capCapacity of ufrag; up to 8 characters plus NUL are written.
pwdOutput buffer for the password.
pwd_capCapacity of pwd; up to 32 characters plus NUL are written.
Warning
Behavior is undefined if either capacity is 0 (cap - 1 underflows).

Definition at line 592 of file ice.c.

References cap, and RAND_bytes.

◆ cwist_ice_stun_build_request()

int cwist_ice_stun_build_request ( uint8_t *  out,
size_t  cap,
const uint8_t  txid[12],
const char *  username 
)

Build an unsigned Binding request (USERNAME, USE-CANDIDATE, ...).

Returns
Message length, or -1 if it does not fit in cap.

Build an unsigned Binding request (USERNAME, USE-CANDIDATE, ...).

The request carries USERNAME (only if username is non-empty), ICE-CONTROLLING, USE-CANDIDATE, and PRIORITY attributes. MESSAGE-INTEGRITY and FINGERPRINT are intentionally not added here; the MI key (the peer's password) is only known at send time, so the caller signs the built request with cwist_ice_stun_sign_request().

Parameters
outDestination buffer.
capCapacity of out in bytes.
txid12-byte transaction ID to copy into the header.
username"remoteufrag:localufrag" string for the USERNAME attribute; skipped when empty.
Returns
Total request length in bytes, or -1 if cap is too small.

Definition at line 361 of file ice.c.

References ATTR_ICE_CONTROLLING, ATTR_PRIORITY, ATTR_USE_CANDIDATE, ATTR_USERNAME, cap, CWIST_STUN_BINDING_REQUEST, STUN_HDR_LEN, and STUN_MAGIC.

◆ cwist_ice_stun_build_response()

int cwist_ice_stun_build_response ( uint8_t *  out,
size_t  cap,
const uint8_t *  req,
size_t  req_len,
const struct sockaddr_in *  mapped,
const char *  pwd 
)

Build a signed Binding success response with XOR-MAPPED-ADDRESS.

Returns
Response length, or -1 if it does not fit in cap.

Build a signed Binding success response with XOR-MAPPED-ADDRESS.

Emits a message containing XOR-MAPPED-ADDRESS (IPv4 only), MESSAGE-INTEGRITY (HMAC-SHA1 keyed with pwd), and FINGERPRINT attributes, reusing the request's transaction ID.

Parameters
outDestination buffer.
capCapacity of out in bytes.
reqThe received binding request (used for its transaction ID).
req_lenBytes at req; currently unused.
mappedAddress/port to report back (XOR-encoded with the magic cookie).
pwdShort-term credential password for the MI HMAC.
Returns
Total response length in bytes, or -1 if cap is too small or OpenSSL allocation fails.

Definition at line 281 of file ice.c.

References ATTR_FINGERPRINT, ATTR_MESSAGE_INTEGRITY, ATTR_XOR_MAPPED_ADDRESS, cap, CWIST_STUN_BINDING_RESPONSE, HMAC_CTX_free, HMAC_CTX_new, HMAC_Final, HMAC_Init_ex, HMAC_Update, STUN_HDR_LEN, and STUN_MAGIC.

◆ cwist_ice_stun_get_remote_ufrag()

int cwist_ice_stun_get_remote_ufrag ( const uint8_t *  buf,
size_t  len,
char *  out,
size_t  cap 
)

Extract the sender's ufrag from USERNAME ("recipient:sender", RFC 8445 section 7.2.2) – the peer's SDP ice-ufrag.

Returns
0 on success, -1 if USERNAME is missing or has no ':'.

Extract the sender's ufrag from USERNAME ("recipient:sender", RFC 8445 section 7.2.2) – the peer's SDP ice-ufrag.

A Binding request's USERNAME is "<recipient ufrag>:<sender ufrag>" (RFC 8445 section 7.2.2), so for a request we received this returns the part after the first ':' – the remote peer's ufrag, as it appears in the peer's SDP.

Parameters
bufDatagram bytes.
lenNumber of bytes at buf.
outOutput buffer for the NUL-terminated ufrag.
capCapacity of out; output is truncated to cap - 1 characters.
Return values
0Ufrag extracted (possibly truncated).
-1Not a STUN message, or no USERNAME attribute present.

Definition at line 556 of file ice.c.

References ATTR_USERNAME, cap, cwist_ice_stun_is_message(), and STUN_HDR_LEN.

◆ cwist_ice_stun_has_use_candidate()

bool cwist_ice_stun_has_use_candidate ( const uint8_t *  buf,
size_t  len 
)

true if the request carries USE-CANDIDATE (nomination).

true if the request carries USE-CANDIDATE (nomination).

Parameters
bufDatagram bytes.
lenNumber of bytes at buf.

Definition at line 197 of file ice.c.

References ATTR_USE_CANDIDATE.

◆ cwist_ice_stun_is_binding_request()

bool cwist_ice_stun_is_binding_request ( const uint8_t *  buf,
size_t  len 
)

true if buf is a STUN Binding request.

true if buf is a STUN Binding request.

Parameters
bufDatagram bytes.
lenNumber of bytes at buf.
Returns
true if buf is a STUN message with type CWIST_STUN_BINDING_REQUEST.

Definition at line 119 of file ice.c.

References cwist_ice_stun_is_message(), and CWIST_STUN_BINDING_REQUEST.

◆ cwist_ice_stun_is_message()

int cwist_ice_stun_is_message ( const uint8_t *  buf,
size_t  len 
)

Non-zero if buf looks like a STUN message (magic cookie, length).

Non-zero if buf looks like a STUN message (magic cookie, length).

Parameters
bufDatagram bytes.
lenNumber of bytes at buf.
Returns
1 if buf holds at least a STUN header and the magic cookie matches, 0 otherwise.
Note
This is a shape check only; no attribute parsing or auth is performed.

Definition at line 110 of file ice.c.

References STUN_HDR_LEN, and STUN_MAGIC.

Referenced by cwist_ice_stun_get_remote_ufrag(), cwist_ice_stun_is_binding_request(), cwist_ice_stun_parse_response(), and cwist_ice_stun_validate_request().

◆ cwist_ice_stun_parse_response()

int cwist_ice_stun_parse_response ( const uint8_t *  buf,
size_t  len,
const uint8_t  txid[12],
const char *  pwd,
struct sockaddr_in *  mapped 
)

Validate a response: cookie, txid match, MI with pwd, return mapped address.

Parameters
mappedFilled with the XOR-MAPPED-ADDRESS on success.
Returns
1 on success, 0 on validation failure.

Validate a response: cookie, txid match, MI with pwd, return mapped address.

Checks, in order: well-formed STUN message of type BINDING-RESPONSE, transaction ID match against txid, MESSAGE-INTEGRITY HMAC with pwd (via cwist_ice_stun_validate_request()), and presence of a MESSAGE-INTEGRITY attribute. If mapped is non-NULL, an IPv4 XOR-MAPPED-ADDRESS must also be present and is decoded into it.

Parameters
bufDatagram bytes.
lenNumber of bytes at buf.
txidTransaction ID the response must carry (the one we sent).
pwdLocal password used as the MI HMAC key.
mappedOptional output for the decoded mapped address.
Return values
1Valid response.
0Any check failed or the message is malformed.

Definition at line 518 of file ice.c.

References cap, cwist_ice_stun_is_message(), cwist_ice_stun_validate_request(), and CWIST_STUN_BINDING_RESPONSE.

◆ cwist_ice_stun_sign_request()

int cwist_ice_stun_sign_request ( uint8_t *  msg,
size_t  cap,
size_t  msg_len,
const char *  pwd 
)

Append MESSAGE-INTEGRITY and FINGERPRINT to a request.

Returns
New message length, or -1 if it does not fit in cap.

Append MESSAGE-INTEGRITY and FINGERPRINT to a request.

Parameters
msgBuffer holding the request; extended in place.
capTotal capacity of msg.
msg_lenCurrent request length.
pwdPeer password used as the MI HMAC key.
Returns
New total length, or -1 on failure (see stun_sign()).

Definition at line 474 of file ice.c.

References cap.

◆ cwist_ice_stun_validate_request()

int cwist_ice_stun_validate_request ( const uint8_t *  buf,
size_t  len,
const char *  pwd 
)

Check MESSAGE-INTEGRITY (HMAC-SHA1 keyed with pwd).

Returns
1 if valid, 0 otherwise.

Check MESSAGE-INTEGRITY (HMAC-SHA1 keyed with pwd).

Parameters
bufDatagram bytes (any STUN message type; also used for responses).
lenNumber of bytes at buf.
pwdShort-term credential password used as the HMAC-SHA1 key.
Returns
1 if the HMAC matches, 0 if the message is malformed, the HMAC does not match, or OpenSSL allocation fails.
Return values
1Also when no MESSAGE-INTEGRITY attribute is present: accepted for MVP interoperability (RFC 8445 requires MI, but the agent stays permissive here).
Note
The HMAC covers the message up to and including the MI attribute header, with the header's length field adjusted to end at the MI attribute, per RFC 5389.

Definition at line 241 of file ice.c.

References cap, cwist_ice_stun_is_message(), EVP_MAX_MD_SIZE, HMAC_CTX_free, HMAC_CTX_new, HMAC_Final, HMAC_Init_ex, HMAC_Update, and STUN_HDR_LEN.

Referenced by cwist_ice_stun_parse_response().

◆ cwist_sctp_assoc_established()

bool cwist_sctp_assoc_established ( struct cwist_webrtc_conn *  conn)

true once the SCTP association is up.

Definition at line 399 of file sctp.c.

References cap, cwist_webrtc_conn::is_server_role, cwist_webrtc_conn::sctp_acc, and cwist_webrtc_conn::sctp_sock.

◆ cwist_sctp_conn_close()

◆ cwist_sctp_conn_drain()

int cwist_sctp_conn_drain ( struct cwist_webrtc_conn *  conn)

Accept (server role), then deliver every complete inbound message.

Returns
0 normally, -1 if the association is gone and the conn should close.

Definition at line 343 of file sctp.c.

References cap, cwist_webrtc_conn::close_requested, cwist_webrtc_conn::ctx, CWIST_WEBRTC_MAX_MESSAGE, cwist_webrtc_conn::detached, cwist_webrtc_conn::is_server_role, cwist_webrtc_conn::rx_partial, cwist_webrtc_conn::rx_partial_len, cwist_webrtc_conn::sctp_acc, cwist_webrtc_ctx::sctp_buf, and cwist_webrtc_conn::sctp_sock.

◆ cwist_sctp_conn_input()

void cwist_sctp_conn_input ( struct cwist_webrtc_conn *  conn,
const uint8_t *  data,
size_t  len 
)

Feed one decrypted SCTP packet to usrsctp.

Definition at line 317 of file sctp.c.

References cap, cwist_webrtc_conn::sctp_acc, and cwist_webrtc_conn::sctp_sock.

◆ cwist_sctp_conn_open()

int cwist_sctp_conn_open ( struct cwist_webrtc_conn *  conn)

Create, bind and listen/connect the conn's SCTP socket.

Returns
0 or -1.

Definition at line 265 of file sctp.c.

References cap, cwist_sctp_conn_close(), cwist_sctp_global_init(), CWIST_SCTP_PORT, cwist_webrtc_conn::is_server_role, and cwist_webrtc_conn::sctp_sock.

◆ cwist_sctp_global_init()

int cwist_sctp_global_init ( void  )

One-time usrsctp initialisation (no threads, AF_CONN only).

Returns
0.

Definition at line 106 of file sctp.c.

References cap.

Referenced by cwist_sctp_conn_open().

◆ cwist_sctp_send()

int cwist_sctp_send ( struct cwist_webrtc_conn *  conn,
uint16_t  channel,
const uint8_t *  data,
size_t  len,
int  is_string 
)

Send one message, announcing the channel with DCEP OPEN first if we are opening it.

Returns
0 when SCTP took it, 1 when the send buffer is full (retry after the next inbound packet), -1 on a hard error (message dropped).

Definition at line 410 of file sctp.c.

References cap, CWIST_SCTP_STREAMS, DCEP_OPEN, PPID_BINARY, PPID_BINARY_EMPTY, PPID_STRING, PPID_STRING_EMPTY, and cwist_webrtc_conn::sctp_ready.

Referenced by cwist_webrtc_conn_send().

◆ cwist_sctp_tick()

void cwist_sctp_tick ( void  )

Advance usrsctp's process-wide timer clock to now. Any thread.

Definition at line 111 of file sctp.c.

References cap.

◆ cwist_sdp_parse()

int cwist_sdp_parse ( const char *  sdp,
size_t  len,
cwist_sdp_info *  info 
)

Parse the fields the ICE-lite agent needs from an SDP description.

Parameters
infoFilled on return; missing fields stay zeroed.
Returns
0 on success, -1 if ice-ufrag or ice-pwd is missing or empty.

Parse the fields the ICE-lite agent needs from an SDP description.

Parameters
sdpSDP text (not required to be NUL-terminated).
lenLength of sdp in bytes.
infoOutput structure; zeroed first, then filled from recognized lines.
Returns
0 on success, -1 if ice-ufrag or ice-pwd is missing or empty.
Note
Lines longer than 511 bytes are truncated. If an attribute appears more than once, the last occurrence wins. Sets info->has_lite when "a=ice-lite" appears.

Definition at line 43 of file sdp.c.

References cap.

Referenced by cwist_webrtc_handle_offer().

◆ cwist_sdp_write_answer()

int cwist_sdp_write_answer ( char *  out,
size_t  cap,
const char *  fingerprint,
const char *  ufrag,
const char *  pwd,
const char *  mid,
const char *  host,
uint16_t  port 
)

Write an ICE-lite, DTLS-passive DataChannel answer.

Returns
0 on success, -1 if it does not fit in cap.

Write an ICE-lite, DTLS-passive DataChannel answer.

Parameters
outOutput buffer, receives CRLF-terminated SDP answer.
capCapacity of out in bytes.
fingerprintDTLS certificate fingerprint (hex, colon-separated).
ufragICE username fragment.
pwdICE password.
midMedia section ID (also used in the BUNDLE group).
hostLocal host address for the a=candidate line.
portLocal UDP port for the a=candidate line.
Returns
0 on success, -1 if the output was truncated or encoding failed.
Note
Declares ice-lite and hardcodes a=setup:passive, since the ICE-lite answering endpoint takes the passive DTLS role, and emits a single host candidate followed by end-of-candidates.

Definition at line 129 of file sdp.c.

References cap.

Referenced by cwist_webrtc_handle_offer().

◆ cwist_sdp_write_offer()

int cwist_sdp_write_offer ( char *  out,
size_t  cap,
const char *  fingerprint,
const char *  ufrag,
const char *  pwd,
const char *  mid 
)

Write a DTLS-active DataChannel offer (loopback tests).

Returns
0 on success, -1 if it does not fit in cap.

Write a DTLS-active DataChannel offer (loopback tests).

Parameters
outOutput buffer, receives CRLF-terminated SDP offer.
capCapacity of out in bytes.
fingerprintDTLS certificate fingerprint (hex, colon-separated).
ufragICE username fragment.
pwdICE password.
midMedia section ID (also used in the BUNDLE group).
Returns
0 on success, -1 if the output was truncated or encoding failed.
Note
Thin wrapper over write_session() with setup "active", no ice-lite, and a fixed session ID; the full agent offering endpoint takes the active DTLS role.

Definition at line 168 of file sdp.c.

References cap.

◆ cwist_webrtc_conn_dtls_out()

void cwist_webrtc_conn_dtls_out ( struct cwist_webrtc_conn *  conn,
const uint8_t *  data,
size_t  len 
)

Queue one datagram to the conn's peer (owner thread).

Definition at line 554 of file webrtc.c.

References cwist_webrtc_conn::ctx, and cwist_webrtc_conn::remote.

◆ cwist_webrtc_conn_on_channel_open()

void cwist_webrtc_conn_on_channel_open ( struct cwist_webrtc_conn *  conn,
uint16_t  channel,
const char *  label 
)

Report a channel the peer opened to the channel handler.

Definition at line 548 of file webrtc.c.

References cap, cwist_webrtc_ctx::ch_cb, cwist_webrtc_ctx::ch_user, and cwist_webrtc_conn::ctx.

◆ cwist_webrtc_conn_on_message()

void cwist_webrtc_conn_on_message ( struct cwist_webrtc_conn *  conn,
uint16_t  channel,
const uint8_t *  data,
size_t  len,
int  is_string 
)

Deliver a complete inbound message to the message handler.

Definition at line 540 of file webrtc.c.

References cwist_webrtc_conn::ctx, CWIST_WEBRTC_DATA_BINARY, CWIST_WEBRTC_DATA_STRING, cwist_webrtc_ctx::msg_cb, and cwist_webrtc_ctx::msg_user.

◆ cwist_webrtc_conn_sctp_out()

void cwist_webrtc_conn_sctp_out ( struct cwist_webrtc_conn *  conn,
const void *  buffer,
size_t  len 
)

usrsctp output for conn; marshalled to the owner thread when needed.

Definition at line 577 of file webrtc.c.

References cap, cwist_webrtc_conn::ctx, cwist_alloc(), cwist_webrtc_conn_retain(), cwist_webrtc_on_owner(), cwist_webrtc_ctx::reactor, cwist_webrtc_conn::ssl, and SSL_write.

◆ cwist_webrtc_connect()

cwist_webrtc_conn * cwist_webrtc_connect ( cwist_webrtc_ctx *  ctx,
const struct sockaddr_in *  remote,
const char *  peer_ufrag,
const char *  peer_pwd 
)

Offering-side dialer used by the loopback tests: registers a client conn to remote and starts ICE.

Returns
A conn reference owned by the caller (cwist_webrtc_conn_release()), or NULL.

Definition at line 1078 of file webrtc.c.

References cap, cwist_reactor_post::cb, cwist_reactor_post::ctx, cwist_webrtc_conn::ctx, cwist_webrtc_conn_retain(), cwist_webrtc_conn::peer_pwd, cwist_webrtc_conn::peer_ufrag, RAND_bytes, cwist_webrtc_ctx::reactor, cwist_webrtc_conn::reg_post, cwist_webrtc_conn::remote, and cwist_webrtc_conn::stun_txid.

◆ cwist_webrtc_on_owner()

bool cwist_webrtc_on_owner ( const cwist_webrtc_ctx *  ctx)

true when the calling thread is servicing ctx right now.

Definition at line 100 of file webrtc.c.

Referenced by cwist_webrtc_conn_close(), cwist_webrtc_conn_sctp_out(), and cwist_webrtc_conn_send().