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
use crate::header::{extract_tag, extract_uri, HeaderName};
use crate::message::{SipMessage, SipMethod};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DialogState {
    /// Dialog created, INVITE sent, waiting for response
    Early,
    /// 2xx received, dialog confirmed
    Confirmed,
    /// BYE sent or received, dialog is ending
    Terminated,
}

#[derive(Debug, Clone)]
pub struct SipDialog {
    pub call_id: String,
    pub local_tag: String,
    pub remote_tag: Option<String>,
    pub local_uri: String,
    pub remote_uri: String,
    pub remote_target: Option<String>,
    pub local_cseq: u32,
    pub remote_cseq: Option<u32>,
    pub state: DialogState,
}

impl SipDialog {
    /// Create a dialog from an outgoing INVITE request (UAC side)
    pub fn new_uac(call_id: String, local_tag: String, local_uri: String, remote_uri: String) -> Self {
        Self {
            call_id,
            local_tag,
            remote_tag: None,
            local_uri,
            remote_uri,
            remote_target: None,
            local_cseq: 1,
            remote_cseq: None,
            state: DialogState::Early,
        }
    }

    /// Create a dialog from an incoming INVITE request (UAS side)
    pub fn new_uas(
        call_id: String,
        local_tag: String,
        remote_tag: String,
        local_uri: String,
        remote_uri: String,
    ) -> Self {
        Self {
            call_id,
            local_tag,
            remote_tag: Some(remote_tag),
            local_uri,
            remote_uri,
            remote_target: None,
            local_cseq: 1,
            remote_cseq: None,
            state: DialogState::Early,
        }
    }

    /// Try to create a dialog from an incoming INVITE request
    pub fn from_invite(msg: &SipMessage) -> Option<Self> {
        if let SipMessage::Request(req) = msg {
            if req.method != SipMethod::Invite {
                return None;
            }

            let call_id = req.headers.get(&HeaderName::CallId)?.0.clone();

            let from_val = req.headers.get(&HeaderName::From)?.as_str();
            let remote_tag = extract_tag(from_val)?;
            let remote_uri = extract_uri(from_val)?;

            let to_val = req.headers.get(&HeaderName::To)?.as_str();
            let local_uri = extract_uri(to_val)?;

            let local_tag = crate::header::generate_tag();

            let contact = req
                .headers
                .get(&HeaderName::Contact)
                .and_then(|v| extract_uri(v.as_str()));

            Some(Self {
                call_id,
                local_tag,
                remote_tag: Some(remote_tag),
                local_uri,
                remote_uri,
                remote_target: contact,
                local_cseq: 1,
                remote_cseq: None,
                state: DialogState::Early,
            })
        } else {
            None
        }
    }

    /// Process an incoming response (for UAC dialogs)
    pub fn process_response(&mut self, msg: &SipMessage) -> bool {
        if let SipMessage::Response(res) = msg {
            // Verify Call-ID matches
            if let Some(call_id) = res.headers.get(&HeaderName::CallId) {
                if call_id.0 != self.call_id {
                    return false;
                }
            } else {
                return false;
            }

            // Extract remote tag from To header
            if let Some(to_val) = res.headers.get(&HeaderName::To) {
                if let Some(tag) = extract_tag(to_val.as_str()) {
                    self.remote_tag = Some(tag);
                }
            }

            // Extract remote target from Contact
            if let Some(contact) = res.headers.get(&HeaderName::Contact) {
                self.remote_target = extract_uri(contact.as_str());
            }

            // Update state based on status code
            match res.status.0 {
                100..=199 => {
                    // Provisional: dialog stays Early
                    if self.state == DialogState::Early {
                        // Already early, no change
                    }
                }
                200..=299 => {
                    self.state = DialogState::Confirmed;
                }
                300..=699 => {
                    self.state = DialogState::Terminated;
                }
                _ => {}
            }

            true
        } else {
            false
        }
    }

    /// Process an incoming BYE request
    pub fn process_bye(&mut self, msg: &SipMessage) -> bool {
        if let SipMessage::Request(req) = msg {
            if req.method == SipMethod::Bye {
                if let Some(call_id) = req.headers.get(&HeaderName::CallId) {
                    if call_id.0 == self.call_id {
                        self.state = DialogState::Terminated;
                        return true;
                    }
                }
            }
        }
        false
    }

    /// Mark the dialog as terminated (when we send BYE)
    pub fn terminate(&mut self) {
        self.state = DialogState::Terminated;
    }

    /// Get the next CSeq number
    pub fn next_cseq(&mut self) -> u32 {
        self.local_cseq += 1;
        self.local_cseq
    }

    /// Check if a message belongs to this dialog
    pub fn matches(&self, msg: &SipMessage) -> bool {
        let headers = msg.headers();

        // Check Call-ID
        if let Some(call_id) = headers.get(&HeaderName::CallId) {
            if call_id.0 != self.call_id {
                return false;
            }
        } else {
            return false;
        }

        // Check tags
        if let Some(from_val) = headers.get(&HeaderName::From) {
            let from_tag = extract_tag(from_val.as_str());
            if let Some(to_val) = headers.get(&HeaderName::To) {
                let to_tag = extract_tag(to_val.as_str());

                // For requests coming from the remote side
                if msg.is_request() {
                    if let Some(ref rt) = self.remote_tag {
                        if from_tag.as_deref() != Some(rt.as_str()) {
                            return false;
                        }
                    }
                    if to_tag.as_deref() != Some(self.local_tag.as_str()) {
                        return false;
                    }
                } else {
                    // For responses
                    if let Some(ref rt) = self.remote_tag {
                        if to_tag.as_deref() != Some(rt.as_str())
                            && from_tag.as_deref() != Some(self.local_tag.as_str())
                        {
                            return false;
                        }
                    }
                }
            }
        }

        true
    }

    pub fn is_confirmed(&self) -> bool {
        self.state == DialogState::Confirmed
    }

    pub fn is_terminated(&self) -> bool {
        self.state == DialogState::Terminated
    }

    pub fn is_early(&self) -> bool {
        self.state == DialogState::Early
    }
}

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

    fn make_invite() -> SipMessage {
        let mut headers = Headers::new();
        headers.add(
            HeaderName::Via,
            "SIP/2.0/UDP 10.0.0.1:5060;branch=z9hG4bK776",
        );
        headers.add(
            HeaderName::From,
            "\"Alice\" <sip:alice@atlanta.com>;tag=abc123",
        );
        headers.add(HeaderName::To, "<sip:bob@biloxi.com>");
        headers.add(HeaderName::CallId, "test-call-id-12345");
        headers.add(HeaderName::CSeq, "1 INVITE");
        headers.add(HeaderName::Contact, "<sip:alice@10.0.0.1:5060>");
        headers.add(HeaderName::MaxForwards, "70");
        headers.add(HeaderName::ContentLength, "0");

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

    fn make_200_ok(call_id: &str, from_tag: &str, to_tag: &str) -> SipMessage {
        let mut headers = Headers::new();
        headers.add(
            HeaderName::Via,
            "SIP/2.0/UDP 10.0.0.1:5060;branch=z9hG4bK776",
        );
        headers.add(
            HeaderName::From,
            format!("<sip:alice@atlanta.com>;tag={}", from_tag),
        );
        headers.add(
            HeaderName::To,
            format!("<sip:bob@biloxi.com>;tag={}", to_tag),
        );
        headers.add(HeaderName::CallId, call_id);
        headers.add(HeaderName::CSeq, "1 INVITE");
        headers.add(HeaderName::Contact, "<sip:bob@10.0.0.2:5060>");
        headers.add(HeaderName::ContentLength, "0");

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

    fn make_bye(call_id: &str, from_tag: &str, to_tag: &str) -> SipMessage {
        let mut headers = Headers::new();
        headers.add(
            HeaderName::Via,
            "SIP/2.0/UDP 10.0.0.2:5060;branch=z9hG4bKbye",
        );
        headers.add(
            HeaderName::From,
            format!("<sip:bob@biloxi.com>;tag={}", from_tag),
        );
        headers.add(
            HeaderName::To,
            format!("<sip:alice@atlanta.com>;tag={}", to_tag),
        );
        headers.add(HeaderName::CallId, call_id);
        headers.add(HeaderName::CSeq, "1 BYE");
        headers.add(HeaderName::ContentLength, "0");

        SipMessage::Request(SipRequest {
            method: SipMethod::Bye,
            uri: "sip:alice@10.0.0.1:5060".to_string(),
            version: "SIP/2.0".to_string(),
            headers,
            body: None,
        })
    }

    #[test]
    fn test_dialog_new_uac() {
        let dialog = SipDialog::new_uac(
            "call-123".to_string(),
            "tag-local".to_string(),
            "sip:alice@atlanta.com".to_string(),
            "sip:bob@biloxi.com".to_string(),
        );

        assert_eq!(dialog.state, DialogState::Early);
        assert_eq!(dialog.call_id, "call-123");
        assert_eq!(dialog.local_tag, "tag-local");
        assert!(dialog.remote_tag.is_none());
        assert!(dialog.is_early());
    }

    #[test]
    fn test_dialog_from_invite() {
        let invite = make_invite();
        let dialog = SipDialog::from_invite(&invite).unwrap();

        assert_eq!(dialog.call_id, "test-call-id-12345");
        assert_eq!(dialog.remote_tag, Some("abc123".to_string()));
        assert_eq!(dialog.remote_uri, "sip:alice@atlanta.com");
        assert_eq!(dialog.local_uri, "sip:bob@biloxi.com");
        assert_eq!(dialog.state, DialogState::Early);
    }

    #[test]
    fn test_dialog_from_invite_requires_invite_method() {
        let mut headers = Headers::new();
        headers.add(HeaderName::From, "<sip:alice@atlanta.com>;tag=abc");
        headers.add(HeaderName::To, "<sip:bob@biloxi.com>");
        headers.add(HeaderName::CallId, "test");

        let bye = SipMessage::Request(SipRequest {
            method: SipMethod::Bye,
            uri: "sip:bob@biloxi.com".to_string(),
            version: "SIP/2.0".to_string(),
            headers,
            body: None,
        });

        assert!(SipDialog::from_invite(&bye).is_none());
    }

    #[test]
    fn test_dialog_process_response_ok() {
        let mut dialog = SipDialog::new_uac(
            "test-call-id-12345".to_string(),
            "local-tag".to_string(),
            "sip:alice@atlanta.com".to_string(),
            "sip:bob@biloxi.com".to_string(),
        );

        let ok = make_200_ok("test-call-id-12345", "local-tag", "remote-tag");
        assert!(dialog.process_response(&ok));
        assert_eq!(dialog.state, DialogState::Confirmed);
        assert_eq!(dialog.remote_tag, Some("remote-tag".to_string()));
        assert!(dialog.is_confirmed());
    }

    #[test]
    fn test_dialog_process_response_wrong_callid() {
        let mut dialog = SipDialog::new_uac(
            "call-1".to_string(),
            "tag-1".to_string(),
            "sip:alice@a.com".to_string(),
            "sip:bob@b.com".to_string(),
        );

        let ok = make_200_ok("call-2", "tag-1", "tag-remote");
        assert!(!dialog.process_response(&ok));
        assert_eq!(dialog.state, DialogState::Early);
    }

    #[test]
    fn test_dialog_process_bye() {
        let mut dialog = SipDialog::new_uac(
            "call-123".to_string(),
            "local-tag".to_string(),
            "sip:alice@atlanta.com".to_string(),
            "sip:bob@biloxi.com".to_string(),
        );
        dialog.state = DialogState::Confirmed;
        dialog.remote_tag = Some("remote-tag".to_string());

        let bye = make_bye("call-123", "remote-tag", "local-tag");
        assert!(dialog.process_bye(&bye));
        assert_eq!(dialog.state, DialogState::Terminated);
        assert!(dialog.is_terminated());
    }

    #[test]
    fn test_dialog_terminate() {
        let mut dialog = SipDialog::new_uac(
            "call-1".to_string(),
            "t1".to_string(),
            "sip:a@a.com".to_string(),
            "sip:b@b.com".to_string(),
        );
        dialog.state = DialogState::Confirmed;
        dialog.terminate();
        assert_eq!(dialog.state, DialogState::Terminated);
    }

    #[test]
    fn test_dialog_next_cseq() {
        let mut dialog = SipDialog::new_uac(
            "call-1".to_string(),
            "t1".to_string(),
            "sip:a@a.com".to_string(),
            "sip:b@b.com".to_string(),
        );
        assert_eq!(dialog.next_cseq(), 2);
        assert_eq!(dialog.next_cseq(), 3);
        assert_eq!(dialog.next_cseq(), 4);
    }

    #[test]
    fn test_dialog_provisional_keeps_early() {
        let mut dialog = SipDialog::new_uac(
            "call-prov".to_string(),
            "local-tag".to_string(),
            "sip:alice@a.com".to_string(),
            "sip:bob@b.com".to_string(),
        );

        let mut headers = Headers::new();
        headers.add(HeaderName::Via, "SIP/2.0/UDP 10.0.0.1:5060;branch=z9hG4bK1");
        headers.add(HeaderName::From, "<sip:alice@a.com>;tag=local-tag");
        headers.add(HeaderName::To, "<sip:bob@b.com>;tag=remote-tag");
        headers.add(HeaderName::CallId, "call-prov");
        headers.add(HeaderName::CSeq, "1 INVITE");
        headers.add(HeaderName::ContentLength, "0");

        let ringing = SipMessage::Response(SipResponse {
            version: "SIP/2.0".to_string(),
            status: StatusCode::RINGING,
            reason: "Ringing".to_string(),
            headers,
            body: None,
        });

        assert!(dialog.process_response(&ringing));
        assert_eq!(dialog.state, DialogState::Early);
        assert_eq!(dialog.remote_tag, Some("remote-tag".to_string()));
    }

    #[test]
    fn test_dialog_error_response_terminates() {
        let mut dialog = SipDialog::new_uac(
            "call-err".to_string(),
            "local-tag".to_string(),
            "sip:alice@a.com".to_string(),
            "sip:bob@b.com".to_string(),
        );

        let mut headers = Headers::new();
        headers.add(HeaderName::Via, "SIP/2.0/UDP 10.0.0.1:5060;branch=z9hG4bK1");
        headers.add(HeaderName::From, "<sip:alice@a.com>;tag=local-tag");
        headers.add(HeaderName::To, "<sip:bob@b.com>;tag=remote-tag");
        headers.add(HeaderName::CallId, "call-err");
        headers.add(HeaderName::CSeq, "1 INVITE");
        headers.add(HeaderName::ContentLength, "0");

        let not_found = SipMessage::Response(SipResponse {
            version: "SIP/2.0".to_string(),
            status: StatusCode::NOT_FOUND,
            reason: "Not Found".to_string(),
            headers,
            body: None,
        });

        assert!(dialog.process_response(&not_found));
        assert_eq!(dialog.state, DialogState::Terminated);
    }

    #[test]
    fn test_dialog_new_uas() {
        let dialog = SipDialog::new_uas(
            "call-uas".to_string(),
            "local-tag".to_string(),
            "remote-tag".to_string(),
            "sip:bob@b.com".to_string(),
            "sip:alice@a.com".to_string(),
        );

        assert_eq!(dialog.state, DialogState::Early);
        assert_eq!(dialog.remote_tag, Some("remote-tag".to_string()));
    }
}