rsipstack 0.8.0

SIP Stack Rust library for building SIP applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use crate::platform::mpsc::{UnboundedReceiver, UnboundedSender};
use crate::prelude::*;
use crate::transport::{SipAddr, SipConnection};
use core::time::Duration;
use key::TransactionKey;
pub use transaction::Transaction;
pub mod endpoint;
pub mod key;
pub mod message;
mod timer;
pub mod transaction;
pub use endpoint::Endpoint;
pub use endpoint::EndpointBuilder;
#[cfg(test)]
mod tests;

pub const TO_TAG_LEN: usize = 8;
pub const BRANCH_LEN: usize = 12;
pub const CNONCE_LEN: usize = 8;
pub const CALL_ID_LEN: usize = 22;
pub const DEFAULT_CALLID_SUFFIX: &str = "restsend.com";

/// Format used when generating a new Call-ID (dialog identifier).
///
/// * `UuidWithSuffix` - `lower(uuid)@suffix`, e.g. `0b9e6c1e-1b40-4e8f-9c21-2f5a8d3e7b61@restsend.com` (default)
/// * `Uuid` - plain lower-case UUID v4, e.g. `0b9e6c1e-1b40-4e8f-9c21-2f5a8d3e7b61`
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum CallIdFormat {
    #[default]
    UuidWithSuffix,
    Uuid,
}
pub struct IncomingRequest {
    pub request: crate::sip::Request,
    pub connection: SipConnection,
    pub from: SipAddr,
}

pub type TransactionReceiver = UnboundedReceiver<Transaction>;
pub type TransactionSender = UnboundedSender<Transaction>;

/// SIP Transaction State
///
/// `TransactionState` represents the various states a SIP transaction can be in
/// during its lifecycle. These states implement the transaction state machines
/// defined in RFC 3261 for both client and server transactions, with the RFC 6026
/// `Accepted` state for INVITE 2xx response handling.
///
/// # States
///
/// * `Nothing` - Initial state for client transactions created
/// * `Calling` - Initial state for client transactions when request is sent or received
/// * `Trying` - Request has been sent/received, waiting for response/processing
/// * `Proceeding` - Provisional response received/sent (1xx except 100 Trying)
/// * `Accepted` - INVITE transaction received/sent a 2xx final response (RFC 6026 §7.1/§7.2);
///   server transaction waits for ACKs and absorbs 2xx retransmissions from the TU until
///   Timer L fires; client transaction absorbs 2xx retransmissions from the server until
///   Timer M fires.
/// * `Completed` - Final non-2xx response received/sent (RFC 3261 §17), waiting for ACK (server INVITE)
///   or response retransmissions (client). For INVITE 2xx, see `Accepted` (RFC 6026 supersedes RFC 3261 §17.2.1 paragraph 4 / §17.1.1.2).
/// * `Confirmed` - ACK received/sent for INVITE non-2xx (3xx-6xx) transactions
/// * `Terminated` - Transaction has completed and is being cleaned up
///
/// # State Transitions
///
/// ## Client Non-INVITE Transaction (RFC 3261 §17.1.2)
/// ```text
/// Nothing → Calling → Trying → Proceeding → Completed → Terminated
/// ```
///
/// ## Client INVITE Transaction (RFC 3261 §17.1.1 + RFC 6026 §7.2)
/// ```text
/// Nothing → Calling → Trying → Proceeding ──2xx──→ Accepted ──Timer M──→ Terminated
///                                       │
///                                       └──3xx-6xx──→ Completed → Confirmed → Terminated
/// ```
///
/// ## Server INVITE Transaction (RFC 3261 §17.2.1 + RFC 6026 §7.1)
/// ```text
/// Calling → Trying → Proceeding ──2xx──→ Accepted ──Timer L──→ Terminated
///                              │
///                              └──3xx-6xx──→ Completed ──ACK──→ Confirmed → Terminated
/// ```
///
/// ## Server Non-INVITE Transaction (RFC 3261 §17.2.2)
/// ```text
/// Calling → Trying → Proceeding → Completed → Terminated
/// ```
///
/// # Examples
///
/// ```rust
/// use rsipstack::transaction::TransactionState;
///
/// let state = TransactionState::Proceeding;
/// match state {
///     TransactionState::Nothing => println!("Transaction starting"),
///     TransactionState::Calling => println!("Request sent"),
///     TransactionState::Trying => println!("Request sent/received"),
///     TransactionState::Proceeding => println!("Provisional response"),
///     TransactionState::Accepted => println!("2xx accepted; waiting for Timer L/M"),
///     TransactionState::Completed => println!("Final non-2xx response"),
///     TransactionState::Confirmed => println!("ACK received/sent"),
///     TransactionState::Terminated => println!("Transaction complete"),
///     // `#[non_exhaustive]`: downstream code MUST keep a wildcard arm to
///     // remain forward-compatible with future state additions.
///     _ => println!("future RFC-extension state"),
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum TransactionState {
    Nothing,
    Calling,
    Trying,
    Proceeding,
    /// RFC 6026 §7.1 (server) / §7.2 (client): an INVITE transaction has sent or received
    /// a 2xx final response. The server absorbs 2xx retransmissions from the TU and waits
    /// for ACK until Timer L (= 64*T1) fires; the client absorbs server-retransmitted 2xx
    /// responses until Timer M (= 64*T1) fires. Replaces the RFC 3261 §17.1.1.2 / §17.2.1
    /// behaviour that incorrectly routed 2xx through the `Completed` state.
    Accepted,
    Completed,
    Confirmed,
    Terminated,
}

impl core::fmt::Display for TransactionState {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            TransactionState::Nothing => write!(f, "Nothing"),
            TransactionState::Calling => write!(f, "Calling"),
            TransactionState::Trying => write!(f, "Trying"),
            TransactionState::Proceeding => write!(f, "Proceeding"),
            TransactionState::Accepted => write!(f, "Accepted"),
            TransactionState::Completed => write!(f, "Completed"),
            TransactionState::Confirmed => write!(f, "Confirmed"),
            TransactionState::Terminated => write!(f, "Terminated"),
        }
    }
}
/// SIP Transaction Type
///
/// `TransactionType` distinguishes between the four types of SIP transactions
/// as defined in RFC 3261. Each type has different behavior for retransmissions,
/// timers, and state transitions.
///
/// # Types
///
/// * `ClientInvite` - Client-side INVITE transaction (UAC INVITE)
/// * `ClientNonInvite` - Client-side non-INVITE transaction (UAC non-INVITE)
/// * `ServerInvite` - Server-side INVITE transaction (UAS INVITE)
/// * `ServerNonInvite` - Server-side non-INVITE transaction (UAS non-INVITE)
///
/// # Characteristics
///
/// ## Client INVITE
/// * Longer timeouts due to human interaction
/// * ACK handling for 2xx responses
/// * CANCEL support for early termination
///
/// ## Client Non-INVITE
/// * Shorter timeouts for automated responses
/// * No ACK required
/// * Simpler state machine
///
/// ## Server INVITE
/// * Must handle ACK for final responses
/// * Supports provisional responses
/// * Complex retransmission rules
///
/// ## Server Non-INVITE
/// * Simple request/response pattern
/// * No ACK handling
/// * Faster completion
///
/// # Examples
///
/// ```rust
/// use rsipstack::transaction::TransactionType;
/// use rsipstack::sip::Method;
///
/// fn get_transaction_type(method: &Method, is_client: bool) -> TransactionType {
///     match (method, is_client) {
///         (Method::Invite, true) => TransactionType::ClientInvite,
///         (Method::Invite, false) => TransactionType::ServerInvite,
///         (_, true) => TransactionType::ClientNonInvite,
///         (_, false) => TransactionType::ServerNonInvite,
///     }
/// }
/// ```
#[derive(Debug, PartialEq)]
pub enum TransactionType {
    ClientInvite,
    ClientNonInvite,
    ServerInvite,
    ServerNonInvite,
}
impl core::fmt::Display for TransactionType {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            TransactionType::ClientInvite => write!(f, "ClientInvite"),
            TransactionType::ClientNonInvite => write!(f, "ClientNonInvite"),
            TransactionType::ServerInvite => write!(f, "ServerInvite"),
            TransactionType::ServerNonInvite => write!(f, "ServerNonInvite"),
        }
    }
}
/// SIP Transaction Timers
///
/// `TransactionTimer` represents the various timers used in SIP transactions
/// as defined in RFC 3261 and RFC 6026. These timers ensure reliable message
/// delivery and proper transaction cleanup.
///
/// # Timer Types
///
/// * `TimerA` - Retransmission timer for client transactions (unreliable transport)
/// * `TimerB` - Transaction timeout timer for client transactions
/// * `TimerC` - Proceeding timeout for client INVITE
/// * `TimerD` - Wait timer for response retransmissions (client)
/// * `TimerK` - Wait timer for ACK (server INVITE non-2xx) / cleanup (client non-INVITE)
/// * `TimerG` - Retransmission timer for INVITE server transactions (non-2xx only per RFC 6026 §7.1)
/// * `TimerL` - RFC 6026 §7.1 server INVITE Accepted-state timer (64*T1)
/// * `TimerM` - RFC 6026 §7.2 client INVITE Accepted-state timer (64*T1)
/// * `TimerCleanup` - Internal cleanup timer for transaction removal
///
/// # Timer Values (RFC 3261 + RFC 6026)
///
/// * T1 = 500ms (RTT estimate)
/// * T2 = 4s (maximum retransmit interval)
/// * T4 = 5s (maximum duration a message will remain in the network)
///
/// ## Timer Calculations
/// * Timer A: starts at T1, doubles each retransmission up to T2
/// * Timer B: 64*T1 (32 seconds)
/// * Timer D: 32 seconds for unreliable, 0 for reliable transports
/// * Timer E: starts at T1, doubles up to T2
/// * Timer F: 64*T1 (32 seconds)
/// * Timer G: starts at T1, doubles up to T2 (non-2xx final response retransmits only)
/// * Timer K: T4 for unreliable, 0 for reliable transports
/// * **Timer L: 64*T1 (32 seconds) — RFC 6026 §7.1**
/// * **Timer M: 64*T1 (32 seconds) — RFC 6026 §7.2**
///
/// # Examples
///
/// ```rust
/// use rsipstack::transaction::{TransactionTimer, key::{TransactionKey, TransactionRole}};
/// use core::time::Duration;
///
/// # fn example() -> rsipstack::Result<()> {
/// // Create a mock request to generate a transaction key
/// let request = rsipstack::sip::Request {
///     method: rsipstack::sip::Method::Register,
///     uri: rsipstack::sip::Uri::try_from("sip:example.com")?,
///     headers: vec![
///         rsipstack::sip::Header::Via("SIP/2.0/UDP example.com:5060;branch=z9hG4bKnashds".into()),
///         rsipstack::sip::Header::CSeq("1 REGISTER".into()),
///         rsipstack::sip::Header::From("Alice <sip:alice@example.com>;tag=1928301774".into()),
///         rsipstack::sip::Header::CallId("a84b4c76e66710@pc33.atlanta.com".into()),
///     ].into(),
///     version: rsipstack::sip::Version::V2,
///     body: Default::default(),
/// };
/// let key = TransactionKey::from_request(&request, TransactionRole::Client)?;
///
/// let timer = TransactionTimer::TimerA(key.clone(), Duration::from_millis(500));
/// match timer {
///     TransactionTimer::TimerA(key, duration) => {
///         println!("Timer A fired for transaction {}", key);
///     },
///     TransactionTimer::TimerB(key) => {
///         println!("Transaction {} timed out", key);
///     },
///     // `#[non_exhaustive]`: downstream code MUST keep a wildcard arm to
///     // remain forward-compatible with future timer additions (e.g. RFC
///     // extensions like Timer L / Timer M / future RFC variants).
///     _ => {}
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Usage
///
/// Timers are automatically managed by the transaction layer:
/// * Started when entering appropriate states
/// * Cancelled when leaving states or receiving responses
/// * Fire events that drive state machine transitions
/// * Handle retransmissions and timeouts
#[non_exhaustive]
pub enum TransactionTimer {
    TimerA(TransactionKey, Duration),
    TimerB(TransactionKey),
    TimerC(TransactionKey),
    TimerD(TransactionKey),
    TimerK(TransactionKey),
    TimerG(TransactionKey, Duration),
    /// RFC 6026 §7.1: server INVITE Accepted-state timer. Fires once at 64*T1
    /// after a 2xx final response is sent, then transitions the transaction to
    /// Terminated. Absorbs ACKs for the 2xx and late 2xx retransmissions from
    /// the TU. Replaces the RFC 3261 §17.2.1 Timer K (T4) usage for 2xx
    /// responses, which was too short to handle proxy-chain ACK fan-in.
    TimerL(TransactionKey),
    /// RFC 6026 §7.2: client INVITE Accepted-state timer. Fires once at 64*T1
    /// after a 2xx final response is received, then transitions the
    /// transaction to Terminated. Absorbs server-retransmitted 2xx responses
    /// (the TU is responsible for the ACK).
    TimerM(TransactionKey),
    TimerCleanup(TransactionKey),
}

impl TransactionTimer {
    pub fn key(&self) -> &TransactionKey {
        match self {
            TransactionTimer::TimerA(key, _) => key,
            TransactionTimer::TimerB(key) => key,
            TransactionTimer::TimerC(key) => key,
            TransactionTimer::TimerD(key) => key,
            TransactionTimer::TimerG(key, _) => key,
            TransactionTimer::TimerK(key) => key,
            TransactionTimer::TimerL(key) => key,
            TransactionTimer::TimerM(key) => key,
            TransactionTimer::TimerCleanup(key) => key,
        }
    }
}

impl core::fmt::Display for TransactionTimer {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            TransactionTimer::TimerA(key, duration) => {
                write!(f, "TimerA: {} {}", key, duration.as_millis())
            }
            TransactionTimer::TimerB(key) => write!(f, "TimerB: {}", key),
            TransactionTimer::TimerC(key) => write!(f, "TimerC: {}", key),
            TransactionTimer::TimerD(key) => write!(f, "TimerD: {}", key),
            TransactionTimer::TimerG(key, duration) => {
                write!(f, "TimerG: {} {}", key, duration.as_millis())
            }
            TransactionTimer::TimerK(key) => write!(f, "TimerK: {}", key),
            TransactionTimer::TimerL(key) => write!(f, "TimerL: {}", key),
            TransactionTimer::TimerM(key) => write!(f, "TimerM: {}", key),
            TransactionTimer::TimerCleanup(key) => write!(f, "TimerCleanup: {}", key),
        }
    }
}

pub fn make_via_branch() -> crate::sip::Param {
    crate::sip::Param::Branch(format!("z9hG4bK{}", random_text(BRANCH_LEN)).into())
}

pub fn make_call_id(suffix: Option<&str>, format: CallIdFormat) -> crate::sip::headers::CallId {
    match format {
        CallIdFormat::UuidWithSuffix => format!(
            "{}@{}",
            make_uuid_v4(),
            suffix.unwrap_or(DEFAULT_CALLID_SUFFIX)
        )
        .into(),
        CallIdFormat::Uuid => make_uuid_v4().into(),
    }
}

pub fn make_uuid_v4() -> String {
    let mut b = [0u8; 16];
    fill_random_bytes(&mut b);
    b[6] = (b[6] & 0x0f) | 0x40;
    b[8] = (b[8] & 0x3f) | 0x80;
    format!(
        "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
        b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13],
        b[14], b[15]
    )
}

#[cfg(all(feature = "std", not(target_family = "wasm")))]
fn fill_random_bytes(buf: &mut [u8]) {
    use rand::Rng;
    rand::rng().fill_bytes(buf);
}

/// no_std fallback: xorshift64* over a process-lifetime counter. Uniqueness
/// is what tags/branches need; hardware entropy arrives with the embassy
/// backend (WP3) — do not rely on this for secrets.
#[cfg(not(feature = "std"))]
fn fill_random_bytes(buf: &mut [u8]) {
    if crate::platform::rng::try_fill(buf) {
        return;
    }
    use crate::platform::atomic64::AtomicU64;
    use core::sync::atomic::Ordering;
    static STATE: AtomicU64 = AtomicU64::new(0x9E37_79B9_7F4A_7C15);
    let mut i = 0;
    while i < buf.len() {
        let mut x = STATE.load(Ordering::Relaxed);
        x ^= x >> 12;
        x ^= x << 25;
        x ^= x >> 27;
        STATE.store(x, Ordering::Relaxed);
        let out = x.wrapping_mul(0x2545_F491_4F6C_DD1D);
        let take = (buf.len() - i).min(8);
        buf[i..i + take].copy_from_slice(&out.to_le_bytes()[..take]);
        i += take;
    }
}

#[cfg(target_family = "wasm")]
fn fill_random_bytes(buf: &mut [u8]) {
    for byte in buf.iter_mut() {
        *byte = (js_sys::Math::random() * 256.0) as u8;
    }
}

pub fn make_tag() -> crate::sip::param::Tag {
    random_text(TO_TAG_LEN).into()
}

#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub fn random_text(count: usize) -> String {
    use rand::RngExt;
    rand::rng()
        .sample_iter(rand::distr::Alphanumeric)
        .take(count)
        .map(char::from)
        .collect::<String>()
}

#[cfg(not(feature = "std"))]
pub fn random_text(count: usize) -> String {
    const ALNUM: &[u8; 62] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    let mut bytes = vec![0u8; count];
    fill_random_bytes(&mut bytes);
    bytes
        .iter()
        .map(|b| ALNUM[(b % 62) as usize] as char)
        .collect()
}

#[cfg(target_family = "wasm")]
pub fn random_text(count: usize) -> String {
    (0..count)
        .map(|_| {
            let r = js_sys::Math::random();
            let c = (r * 16.0) as u8;
            if c < 10 {
                (c + 48) as char
            } else {
                (c + 87) as char
            }
        })
        .collect()
}