cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
webrtc_internal.h
Go to the documentation of this file.
1
22#ifndef __CWIST_WEBRTC_INTERNAL_H__
23#define __CWIST_WEBRTC_INTERNAL_H__
24
25#include <cwist/net/webrtc.h>
27
28#include <netinet/in.h>
29#include <openssl/ssl.h>
30#include <pthread.h>
31#include <stdatomic.h>
32#include <stdbool.h>
33#include <stddef.h>
34#include <stdint.h>
35#include <stdio.h>
36#include <sys/socket.h>
37#include <sys/types.h>
38
39struct socket; /* usrsctp */
40
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
53
61
76
130
192
195#define CWIST_STUN_BINDING_REQUEST 0x0001
196#define CWIST_STUN_BINDING_RESPONSE 0x0101
202int cwist_ice_stun_is_message(const uint8_t *buf, size_t len);
204bool cwist_ice_stun_is_binding_request(const uint8_t *buf, size_t len);
206bool cwist_ice_stun_has_use_candidate(const uint8_t *buf, size_t len);
212int cwist_ice_stun_get_remote_ufrag(const uint8_t *buf, size_t len, char *out, size_t cap);
217int cwist_ice_stun_validate_request(const uint8_t *buf, size_t len, const char *pwd);
222int cwist_ice_stun_build_response(uint8_t *out, size_t cap, const uint8_t *req, size_t req_len,
223 const struct sockaddr_in *mapped, const char *pwd);
228int cwist_ice_stun_build_request(uint8_t *out, size_t cap, const uint8_t txid[12],
229 const char *username);
234int cwist_ice_stun_sign_request(uint8_t *msg, size_t cap, size_t msg_len, const char *pwd);
240int cwist_ice_stun_parse_response(const uint8_t *buf, size_t len, const uint8_t txid[12],
241 const char *pwd, struct sockaddr_in *mapped);
243void cwist_ice_random_creds(char *ufrag, size_t ufrag_cap, char *pwd, size_t pwd_cap);
247typedef struct {
248 char ice_ufrag[64];
249 char ice_pwd[128];
250 char fingerprint[128];
251 char setup[16];
252 char mid[16];
255
263int cwist_sdp_parse(const char *sdp, size_t len, cwist_sdp_info *info);
268int cwist_sdp_write_answer(char *out, size_t cap, const char *fingerprint, const char *ufrag,
269 const char *pwd, const char *mid, const char *host, uint16_t port);
274int cwist_sdp_write_offer(char *out, size_t cap, const char *fingerprint, const char *ufrag,
275 const char *pwd, const char *mid);
281int cwist_dtls_generate_cert(X509 **cert, EVP_PKEY **pkey);
283int cwist_dtls_fingerprint(X509 *cert, char *out, size_t cap);
285SSL_CTX *cwist_dtls_ctx_new(int is_server, X509 *cert, EVP_PKEY *pkey);
292unsigned int cwist_dtls_link_mtu(void);
298int cwist_sctp_global_init(void);
300void cwist_sctp_tick(void);
306void cwist_sctp_conn_input(struct cwist_webrtc_conn *conn, const uint8_t *data, size_t len);
320int cwist_sctp_send(struct cwist_webrtc_conn *conn, uint16_t channel, const uint8_t *data,
321 size_t len, int is_string);
329void cwist_webrtc_conn_dtls_out(struct cwist_webrtc_conn *conn, const uint8_t *data, size_t len);
331void cwist_webrtc_conn_sctp_out(struct cwist_webrtc_conn *conn, const void *buffer, size_t len);
334 const uint8_t *data, size_t len, int is_string);
337 const char *label);
345 const char *peer_ufrag, const char *peer_pwd);
348#endif
size_t cap
Definition otel.c:56
struct cwist_reactor cwist_reactor_t
Definition reactor.h:14
mbedTLS TLS backend: OpenSSL-shaped seam over the CWIST TLS subset.
struct cwist_mtls_ssl SSL
Definition ssl.h:44
struct cwist_mtls_ctx SSL_CTX
Definition ssl.h:43
Definition reactor.h:46
Fields the ICE-lite agent needs from an SDP description.
One peer: ICE state, DTLS session, SCTP association.
cwist_reactor_post_t reg_post
const uint8_t * dtls_in
cwist_webrtc_ctx * ctx
struct socket * sctp_sock
struct cwist_webrtc_conn * hnext
atomic_size_t buffered
struct sockaddr_in remote
struct cwist_webrtc_conn * dirty_next
cwist_conn_state state
atomic_bool close_posted
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
struct socket * sctp_acc
A UDP endpoint bound to one reactor, serving many conns.
pthread_mutex_t free_mu
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]
pthread_cond_t free_cv
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).
Definition webrtc.h:57
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...
Definition webrtc.h:65
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.
Definition webrtc.h:50
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.
Definition ice.c:518
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).
Definition ice.c:110
int cwist_dtls_fingerprint(X509 *cert, char *out, size_t cap)
SHA-256 fingerprint of cert in SDP form ("AB:CD:...").
Definition dtls.c:90
cwist_conn_state
@ CWIST_CONN_CLOSED
@ CWIST_CONN_STUN
@ CWIST_CONN_DTLS
@ CWIST_CONN_ESTABLISHED
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 webrtc.c:548
int cwist_sctp_conn_open(struct cwist_webrtc_conn *conn)
Create, bind and listen/connect the conn's SCTP socket.
Definition sctp.c:265
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).
Definition sdp.c:168
bool cwist_ice_stun_has_use_candidate(const uint8_t *buf, size_t len)
true if the request carries USE-CANDIDATE (nomination).
Definition ice.c:197
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.
Definition ice.c:474
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 webrtc.c:577
int cwist_sctp_global_init(void)
One-time usrsctp initialisation (no threads, AF_CONN only).
Definition sctp.c:106
int cwist_ice_stun_validate_request(const uint8_t *buf, size_t len, const char *pwd)
Check MESSAGE-INTEGRITY (HMAC-SHA1 keyed with pwd).
Definition ice.c:241
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, ...).
Definition ice.c:361
int cwist_sctp_conn_drain(struct cwist_webrtc_conn *conn)
Accept (server role), then deliver every complete inbound message.
Definition sctp.c:343
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.
Definition sdp.c:129
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....
Definition ice.c:556
bool cwist_ice_stun_is_binding_request(const uint8_t *buf, size_t len)
true if buf is a STUN Binding request.
Definition ice.c:119
void cwist_ice_random_creds(char *ufrag, size_t ufrag_cap, char *pwd, size_t pwd_cap)
Generate random ICE credentials (ice-chars only).
Definition ice.c:592
void cwist_sctp_tick(void)
Advance usrsctp's process-wide timer clock to now. Any thread.
Definition sctp.c:111
#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.
Definition sdp.c:43
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.
Definition webrtc.c:1078
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.
Definition dtls.c:120
bool cwist_webrtc_on_owner(const cwist_webrtc_ctx *ctx)
true when the calling thread is servicing ctx right now.
Definition webrtc.c:100
bool cwist_sctp_assoc_established(struct cwist_webrtc_conn *conn)
true once the SCTP association is up.
Definition sctp.c:399
unsigned int cwist_dtls_link_mtu(void)
DTLS link MTU (bytes per datagram) used for handshake fragmentation.
Definition dtls.c:142
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 webrtc.c:554
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.
Definition sctp.c:410
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...
Definition dtls.c:211
void cwist_sctp_conn_close(struct cwist_webrtc_conn *conn)
Abort the association and release SCTP state. Idempotent.
Definition sctp.c:296
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.
Definition ice.c:281
int cwist_dtls_generate_cert(X509 **cert, EVP_PKEY **pkey)
Generate an ephemeral self-signed P-256 certificate.
Definition dtls.c:36
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 webrtc.c:540
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 sctp.c:317