sip-core 0.3.0

SIP protocol library for parsing, serialization, and dialog management
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
use crate::header::HeaderName;
use crate::message::{SipMessage, SipMethod, StatusCode};
use std::time::{Duration, Instant};

/// SIP transaction timer values (RFC 3261 Section 17)
pub const T1: Duration = Duration::from_millis(500);
pub const T2: Duration = Duration::from_secs(4);
pub const T4: Duration = Duration::from_secs(5);
pub const TIMER_B: Duration = Duration::from_secs(32); // 64*T1
pub const TIMER_D: Duration = Duration::from_secs(32);
pub const TIMER_F: Duration = Duration::from_secs(32);
pub const TIMER_H: Duration = Duration::from_secs(32);

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransactionKind {
    ClientInvite,
    ClientNonInvite,
    ServerInvite,
    ServerNonInvite,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransactionState {
    /// Initial state — request not yet sent/received
    Trying,
    /// INVITE client: 1xx received
    Proceeding,
    /// INVITE client: 2xx received (pass to TU), INVITE server: 2xx sent
    Completed,
    /// ACK sent (INVITE client) or ACK received (INVITE server)
    Confirmed,
    /// Transaction is done and can be cleaned up
    Terminated,
}

#[derive(Debug, Clone)]
pub struct SipTransaction {
    pub id: String,
    pub kind: TransactionKind,
    pub state: TransactionState,
    pub method: SipMethod,
    pub branch: String,
    pub call_id: String,
    pub original_request: Option<SipMessage>,
    pub last_response: Option<SipMessage>,
    pub retransmit_count: u32,
    pub created_at: Instant,
    pub last_retransmit: Option<Instant>,
}

impl SipTransaction {
    /// Create a new client transaction from an outgoing request
    pub fn new_client(request: &SipMessage) -> Option<Self> {
        if let SipMessage::Request(req) = request {
            let branch = Self::extract_branch(&request)?;
            let call_id = req.headers.get(&HeaderName::CallId)?.0.clone();

            let kind = if req.method == SipMethod::Invite {
                TransactionKind::ClientInvite
            } else {
                TransactionKind::ClientNonInvite
            };

            let id = format!("{}:{}", branch, req.method);

            Some(Self {
                id,
                kind,
                state: TransactionState::Trying,
                method: req.method.clone(),
                branch,
                call_id,
                original_request: Some(request.clone()),
                last_response: None,
                retransmit_count: 0,
                created_at: Instant::now(),
                last_retransmit: None,
            })
        } else {
            None
        }
    }

    /// Create a new server transaction from an incoming request
    pub fn new_server(request: &SipMessage) -> Option<Self> {
        if let SipMessage::Request(req) = request {
            let branch = Self::extract_branch(&request)?;
            let call_id = req.headers.get(&HeaderName::CallId)?.0.clone();

            let kind = if req.method == SipMethod::Invite {
                TransactionKind::ServerInvite
            } else {
                TransactionKind::ServerNonInvite
            };

            let id = format!("{}:{}", branch, req.method);

            Some(Self {
                id,
                kind,
                state: TransactionState::Trying,
                method: req.method.clone(),
                branch,
                call_id,
                original_request: Some(request.clone()),
                last_response: None,
                retransmit_count: 0,
                created_at: Instant::now(),
                last_retransmit: None,
            })
        } else {
            None
        }
    }

    /// Process an incoming response for a client transaction
    pub fn process_response(&mut self, response: &SipMessage) -> TransactionAction {
        if let SipMessage::Response(res) = response {
            match &self.kind {
                TransactionKind::ClientInvite => {
                    self.process_client_invite_response(res.status)
                }
                TransactionKind::ClientNonInvite => {
                    self.process_client_non_invite_response(res.status)
                }
                _ => TransactionAction::None,
            }
        } else {
            TransactionAction::None
        }
    }

    fn process_client_invite_response(&mut self, status: StatusCode) -> TransactionAction {
        match self.state {
            TransactionState::Trying | TransactionState::Proceeding => {
                if status.is_provisional() {
                    self.state = TransactionState::Proceeding;
                    TransactionAction::PassToTU
                } else if status.is_success() {
                    self.state = TransactionState::Terminated;
                    TransactionAction::PassToTU
                } else {
                    // 3xx-6xx
                    self.state = TransactionState::Completed;
                    TransactionAction::SendAck
                }
            }
            TransactionState::Completed => {
                // Retransmission of final response
                TransactionAction::SendAck
            }
            _ => TransactionAction::None,
        }
    }

    fn process_client_non_invite_response(&mut self, status: StatusCode) -> TransactionAction {
        match self.state {
            TransactionState::Trying | TransactionState::Proceeding => {
                if status.is_provisional() {
                    self.state = TransactionState::Proceeding;
                    TransactionAction::PassToTU
                } else {
                    self.state = TransactionState::Completed;
                    TransactionAction::PassToTU
                }
            }
            _ => TransactionAction::None,
        }
    }

    /// Process an outgoing response for a server transaction
    pub fn send_response(&mut self, response: &SipMessage) -> TransactionAction {
        if let SipMessage::Response(res) = response {
            self.last_response = Some(response.clone());

            match &self.kind {
                TransactionKind::ServerInvite => {
                    if res.status.is_provisional() {
                        self.state = TransactionState::Proceeding;
                        TransactionAction::SendResponse
                    } else if res.status.is_success() {
                        self.state = TransactionState::Terminated;
                        TransactionAction::SendResponse
                    } else {
                        self.state = TransactionState::Completed;
                        TransactionAction::SendResponse
                    }
                }
                TransactionKind::ServerNonInvite => {
                    if res.status.is_provisional() {
                        self.state = TransactionState::Proceeding;
                        TransactionAction::SendResponse
                    } else {
                        self.state = TransactionState::Completed;
                        TransactionAction::SendResponse
                    }
                }
                _ => TransactionAction::None,
            }
        } else {
            TransactionAction::None
        }
    }

    /// Check if the transaction should retransmit (for unreliable transport)
    pub fn should_retransmit(&self) -> bool {
        if self.kind != TransactionKind::ClientInvite && self.kind != TransactionKind::ClientNonInvite {
            return false;
        }

        match self.state {
            TransactionState::Trying => true,
            TransactionState::Proceeding if self.kind == TransactionKind::ClientInvite => true,
            _ => false,
        }
    }

    /// Get the next retransmit interval
    pub fn retransmit_interval(&self) -> Duration {
        let base = T1;
        let multiplier = 2u32.pow(self.retransmit_count.min(6));
        let interval = base * multiplier;

        match self.kind {
            TransactionKind::ClientInvite => interval.min(T2),
            TransactionKind::ClientNonInvite => interval.min(T2),
            _ => interval,
        }
    }

    /// Mark that a retransmission was done
    pub fn mark_retransmit(&mut self) {
        self.retransmit_count += 1;
        self.last_retransmit = Some(Instant::now());
    }

    /// Check if the transaction has timed out
    pub fn is_timed_out(&self) -> bool {
        let elapsed = self.created_at.elapsed();
        match self.kind {
            TransactionKind::ClientInvite => elapsed > TIMER_B,
            TransactionKind::ClientNonInvite => elapsed > TIMER_F,
            TransactionKind::ServerInvite => {
                self.state == TransactionState::Completed && elapsed > TIMER_H
            }
            TransactionKind::ServerNonInvite => {
                self.state == TransactionState::Completed && elapsed > Duration::from_secs(32)
            }
        }
    }

    /// Check if the transaction is in a terminal state
    pub fn is_terminated(&self) -> bool {
        self.state == TransactionState::Terminated
    }

    /// Check if this transaction matches a given message
    pub fn matches(&self, msg: &SipMessage) -> bool {
        if let Some(branch) = Self::extract_branch(msg) {
            if branch == self.branch {
                // Also check method for CANCEL matching
                if let Some((_seq, method)) = msg.cseq() {
                    return method == self.method;
                }
                return true;
            }
        }
        false
    }

    fn extract_branch(msg: &SipMessage) -> Option<String> {
        let via = msg.headers().get(&HeaderName::Via)?;
        let via_str = via.as_str();
        for param in via_str.split(';') {
            let param = param.trim();
            if let Some(branch) = param.strip_prefix("branch=") {
                return Some(branch.to_string());
            }
        }
        None
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransactionAction {
    /// No action needed
    None,
    /// Pass the message to the Transaction User (dialog layer)
    PassToTU,
    /// Send an ACK for a non-2xx final response
    SendAck,
    /// Send the response on the transport
    SendResponse,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::header::Headers;
    use crate::message::{SipRequest, SipResponse};

    fn make_request(method: SipMethod, branch: &str, call_id: &str) -> SipMessage {
        let mut headers = Headers::new();
        headers.add(
            HeaderName::Via,
            format!("SIP/2.0/UDP 10.0.0.1:5060;branch={}", branch),
        );
        headers.add(HeaderName::From, "<sip:alice@a.com>;tag=t1");
        headers.add(HeaderName::To, "<sip:bob@b.com>");
        headers.add(HeaderName::CallId, call_id);
        headers.add(
            HeaderName::CSeq,
            format!("1 {}", method.as_str()),
        );
        headers.add(HeaderName::ContentLength, "0");

        SipMessage::Request(SipRequest {
            method,
            uri: "sip:bob@b.com".to_string(),
            version: "SIP/2.0".to_string(),
            headers,
            body: None,
        })
    }

    fn make_response(status: StatusCode, branch: &str, call_id: &str, method: &str) -> SipMessage {
        let mut headers = Headers::new();
        headers.add(
            HeaderName::Via,
            format!("SIP/2.0/UDP 10.0.0.1:5060;branch={}", branch),
        );
        headers.add(HeaderName::From, "<sip:alice@a.com>;tag=t1");
        headers.add(HeaderName::To, "<sip:bob@b.com>;tag=t2");
        headers.add(HeaderName::CallId, call_id);
        headers.add(HeaderName::CSeq, format!("1 {}", method));
        headers.add(HeaderName::ContentLength, "0");

        SipMessage::Response(SipResponse {
            version: "SIP/2.0".to_string(),
            status,
            reason: status.reason_phrase().to_string(),
            headers,
            body: None,
        })
    }

    #[test]
    fn test_create_client_invite_transaction() {
        let req = make_request(SipMethod::Invite, "z9hG4bK776", "call-1");
        let txn = SipTransaction::new_client(&req).unwrap();

        assert_eq!(txn.kind, TransactionKind::ClientInvite);
        assert_eq!(txn.state, TransactionState::Trying);
        assert_eq!(txn.method, SipMethod::Invite);
        assert_eq!(txn.branch, "z9hG4bK776");
        assert_eq!(txn.call_id, "call-1");
    }

    #[test]
    fn test_create_client_non_invite_transaction() {
        let req = make_request(SipMethod::Register, "z9hG4bK777", "call-2");
        let txn = SipTransaction::new_client(&req).unwrap();

        assert_eq!(txn.kind, TransactionKind::ClientNonInvite);
        assert_eq!(txn.state, TransactionState::Trying);
        assert_eq!(txn.method, SipMethod::Register);
    }

    #[test]
    fn test_create_server_transaction() {
        let req = make_request(SipMethod::Invite, "z9hG4bK778", "call-3");
        let txn = SipTransaction::new_server(&req).unwrap();

        assert_eq!(txn.kind, TransactionKind::ServerInvite);
        assert_eq!(txn.state, TransactionState::Trying);
    }

    #[test]
    fn test_client_invite_provisional_response() {
        let req = make_request(SipMethod::Invite, "z9hG4bK779", "call-4");
        let mut txn = SipTransaction::new_client(&req).unwrap();

        let ringing = make_response(StatusCode::RINGING, "z9hG4bK779", "call-4", "INVITE");
        let action = txn.process_response(&ringing);

        assert_eq!(action, TransactionAction::PassToTU);
        assert_eq!(txn.state, TransactionState::Proceeding);
    }

    #[test]
    fn test_client_invite_success_response() {
        let req = make_request(SipMethod::Invite, "z9hG4bK780", "call-5");
        let mut txn = SipTransaction::new_client(&req).unwrap();

        let ok = make_response(StatusCode::OK, "z9hG4bK780", "call-5", "INVITE");
        let action = txn.process_response(&ok);

        assert_eq!(action, TransactionAction::PassToTU);
        assert_eq!(txn.state, TransactionState::Terminated);
    }

    #[test]
    fn test_client_invite_error_response() {
        let req = make_request(SipMethod::Invite, "z9hG4bK781", "call-6");
        let mut txn = SipTransaction::new_client(&req).unwrap();

        let not_found = make_response(StatusCode::NOT_FOUND, "z9hG4bK781", "call-6", "INVITE");
        let action = txn.process_response(&not_found);

        assert_eq!(action, TransactionAction::SendAck);
        assert_eq!(txn.state, TransactionState::Completed);
    }

    #[test]
    fn test_client_non_invite_success() {
        let req = make_request(SipMethod::Register, "z9hG4bK782", "call-7");
        let mut txn = SipTransaction::new_client(&req).unwrap();

        let ok = make_response(StatusCode::OK, "z9hG4bK782", "call-7", "REGISTER");
        let action = txn.process_response(&ok);

        assert_eq!(action, TransactionAction::PassToTU);
        assert_eq!(txn.state, TransactionState::Completed);
    }

    #[test]
    fn test_server_invite_provisional() {
        let req = make_request(SipMethod::Invite, "z9hG4bK783", "call-8");
        let mut txn = SipTransaction::new_server(&req).unwrap();

        let ringing = make_response(StatusCode::RINGING, "z9hG4bK783", "call-8", "INVITE");
        let action = txn.send_response(&ringing);

        assert_eq!(action, TransactionAction::SendResponse);
        assert_eq!(txn.state, TransactionState::Proceeding);
    }

    #[test]
    fn test_server_invite_success() {
        let req = make_request(SipMethod::Invite, "z9hG4bK784", "call-9");
        let mut txn = SipTransaction::new_server(&req).unwrap();

        let ok = make_response(StatusCode::OK, "z9hG4bK784", "call-9", "INVITE");
        let action = txn.send_response(&ok);

        assert_eq!(action, TransactionAction::SendResponse);
        assert_eq!(txn.state, TransactionState::Terminated);
    }

    #[test]
    fn test_transaction_matching() {
        let req = make_request(SipMethod::Invite, "z9hG4bK785", "call-10");
        let txn = SipTransaction::new_client(&req).unwrap();

        // Same branch should match
        let response = make_response(StatusCode::OK, "z9hG4bK785", "call-10", "INVITE");
        assert!(txn.matches(&response));

        // Different branch should not match
        let other = make_response(StatusCode::OK, "z9hG4bK999", "call-10", "INVITE");
        assert!(!txn.matches(&other));
    }

    #[test]
    fn test_retransmit_interval() {
        let req = make_request(SipMethod::Invite, "z9hG4bK786", "call-11");
        let mut txn = SipTransaction::new_client(&req).unwrap();

        assert_eq!(txn.retransmit_interval(), T1); // 500ms
        txn.mark_retransmit();
        assert_eq!(txn.retransmit_interval(), T1 * 2); // 1000ms
        txn.mark_retransmit();
        assert_eq!(txn.retransmit_interval(), T1 * 4); // 2000ms
        txn.mark_retransmit();
        assert_eq!(txn.retransmit_interval(), T2); // 4000ms (capped)
    }

    #[test]
    fn test_should_retransmit() {
        let req = make_request(SipMethod::Invite, "z9hG4bK787", "call-12");
        let mut txn = SipTransaction::new_client(&req).unwrap();

        assert!(txn.should_retransmit()); // Trying state

        let ringing = make_response(StatusCode::RINGING, "z9hG4bK787", "call-12", "INVITE");
        txn.process_response(&ringing);
        assert!(txn.should_retransmit()); // Proceeding state for INVITE

        let ok = make_response(StatusCode::OK, "z9hG4bK787", "call-12", "INVITE");
        txn.process_response(&ok);
        assert!(!txn.should_retransmit()); // Terminated
    }

    #[test]
    fn test_transaction_terminated() {
        let req = make_request(SipMethod::Register, "z9hG4bK788", "call-13");
        let mut txn = SipTransaction::new_client(&req).unwrap();

        assert!(!txn.is_terminated());

        let ok = make_response(StatusCode::OK, "z9hG4bK788", "call-13", "REGISTER");
        txn.process_response(&ok);

        // Non-invite goes to Completed, not Terminated
        assert!(!txn.is_terminated());
        assert_eq!(txn.state, TransactionState::Completed);
    }

    #[test]
    fn test_create_from_response_fails() {
        let response = make_response(StatusCode::OK, "z9hG4bK789", "call-14", "INVITE");
        assert!(SipTransaction::new_client(&response).is_none());
        assert!(SipTransaction::new_server(&response).is_none());
    }

    #[test]
    fn test_process_response_on_request() {
        let req = make_request(SipMethod::Invite, "z9hG4bK790", "call-15");
        let mut txn = SipTransaction::new_client(&req).unwrap();

        // Passing a request to process_response should return None action
        let action = txn.process_response(&req);
        assert_eq!(action, TransactionAction::None);
    }

    #[test]
    fn test_server_non_invite_response() {
        let req = make_request(SipMethod::Register, "z9hG4bK791", "call-16");
        let mut txn = SipTransaction::new_server(&req).unwrap();

        assert_eq!(txn.kind, TransactionKind::ServerNonInvite);

        let ok = make_response(StatusCode::OK, "z9hG4bK791", "call-16", "REGISTER");
        let action = txn.send_response(&ok);
        assert_eq!(action, TransactionAction::SendResponse);
        assert_eq!(txn.state, TransactionState::Completed);
    }
}