22#ifndef __CWIST_WEBRTC_INTERNAL_H__
23#define __CWIST_WEBRTC_INTERNAL_H__
28#include <netinet/in.h>
36#include <sys/socket.h>
43#define CWIST_WEBRTC_MAX_MESSAGE 262144
46#define CWIST_WEBRTC_MAX_BUFFERED (4u << 20)
48#define CWIST_WEBRTC_DGRAM_MAX 2048
50#define CWIST_WEBRTC_RX_BATCH 32
52#define CWIST_WEBRTC_TX_BATCH 64
195#define CWIST_STUN_BINDING_REQUEST 0x0001
196#define CWIST_STUN_BINDING_RESPONSE 0x0101
250 char fingerprint[128];
269 const char *
pwd,
const char *mid,
const char *host,
uint16_t port);
275 const char *
pwd,
const char *mid);
321 size_t len,
int is_string);
334 const uint8_t *data,
size_t len,
int is_string);
345 const char *peer_ufrag,
const char *peer_pwd);
struct cwist_reactor cwist_reactor_t
mbedTLS TLS backend: OpenSSL-shaped seam over the CWIST TLS subset.
struct cwist_mtls_ssl SSL
struct cwist_mtls_ctx SSL_CTX
Fields the ICE-lite agent needs from an SDP description.
One peer: ICE state, DTLS session, SCTP association.
cwist_reactor_post_t reg_post
struct socket * sctp_sock
struct cwist_webrtc_conn * hnext
struct sockaddr_in remote
struct cwist_webrtc_conn * dirty_next
cwist_webrtc_msg * pending_head
_Atomic(cwist_webrtc_msg *) inbox
cwist_webrtc_msg * pending_tail
cwist_reactor_post_t kick_post
cwist_reactor_post_t close_post
cwist_reactor_timer_t timer
A UDP endpoint bound to one reactor, serving many conns.
uint64_t last_activity_ns
cwist_webrtc_close_cb close_cb
cwist_reactor_t * reactor
cwist_webrtc_message_cb msg_cb
cwist_reactor_post_t free_post
struct sockaddr_in tx_addr[CWIST_WEBRTC_TX_BATCH]
cwist_webrtc_conn ** buckets
cwist_webrtc_conn * dirty
cwist_reactor_timer_t sctp_timer
cwist_webrtc_channel_cb ch_cb
atomic_int ice_lite_server
size_t tx_len[CWIST_WEBRTC_TX_BATCH]
cwist_webrtc_conn * parked
A DataChannel message on its way into SCTP.
struct cwist_webrtc_msg * next
struct cwist_webrtc_conn * conn
WebRTC DataChannel server (SDP + ICE-lite + DTLS + SCTP) interface.
void(* cwist_webrtc_channel_cb)(cwist_webrtc_conn *conn, uint16_t channel_id, const char *label, void *user)
Optional DataChannel open notification (remote peer opened a channel).
void(* cwist_webrtc_close_cb)(cwist_webrtc_conn *conn, void *user)
Optional connection-closed notification. Runs once per connection whose SCTP association came up; aft...
void(* cwist_webrtc_message_cb)(cwist_webrtc_conn *conn, uint16_t channel_id, const uint8_t *data, size_t len, cwist_webrtc_data_type type, void *user)
DataChannel message callback, invoked on the ctx's reactor thread.
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.
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).
int cwist_dtls_fingerprint(X509 *cert, char *out, size_t cap)
SHA-256 fingerprint of cert in SDP form ("AB:CD:...").
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.
int cwist_sctp_conn_open(struct cwist_webrtc_conn *conn)
Create, bind and listen/connect the conn's SCTP socket.
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).
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_sign_request(uint8_t *msg, size_t cap, size_t msg_len, const char *pwd)
Append MESSAGE-INTEGRITY and FINGERPRINT to a request.
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.
int cwist_sctp_global_init(void)
One-time usrsctp initialisation (no threads, AF_CONN only).
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_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_sctp_conn_drain(struct cwist_webrtc_conn *conn)
Accept (server role), then deliver every complete inbound message.
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_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....
bool cwist_ice_stun_is_binding_request(const uint8_t *buf, size_t len)
true if buf is a STUN Binding request.
void cwist_ice_random_creds(char *ufrag, size_t ufrag_cap, char *pwd, size_t pwd_cap)
Generate random ICE credentials (ice-chars only).
void cwist_sctp_tick(void)
Advance usrsctp's process-wide timer clock to now. Any thread.
#define CWIST_WEBRTC_TX_BATCH
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.
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.
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.
bool cwist_webrtc_on_owner(const cwist_webrtc_ctx *ctx)
true when the calling thread is servicing ctx right now.
bool cwist_sctp_assoc_established(struct cwist_webrtc_conn *conn)
true once the SCTP association is up.
unsigned int cwist_dtls_link_mtu(void)
DTLS link MTU (bytes per datagram) used for handshake fragmentation.
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).
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.
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...
void cwist_sctp_conn_close(struct cwist_webrtc_conn *conn)
Abort the association and release SCTP state. Idempotent.
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_dtls_generate_cert(X509 **cert, EVP_PKEY **pkey)
Generate an ephemeral self-signed P-256 certificate.
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_sctp_conn_input(struct cwist_webrtc_conn *conn, const uint8_t *data, size_t len)
Feed one decrypted SCTP packet to usrsctp.