rsiprtp 0.4.1

Modular SIP/RTP communications stack for Rust with Sans-IO state machines
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
//! Dialog states and identifiers per RFC 3261.
//!
//! A dialog is a peer-to-peer SIP relationship between two UAs that persists
//! for some time. It facilitates sequencing of messages between the UAs and
//! proper routing of requests between both of them.

use crate::sip::{RecordRoute as SipRecordRoute, SipRequest, SipResponse, Via};

/// Dialog identifier per RFC 3261.
///
/// A dialog is identified by the combination of Call-ID, local tag, and remote tag.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DialogId {
    /// Call-ID header value.
    pub call_id: String,
    /// Local tag (From tag for UAC, To tag for UAS).
    pub local_tag: String,
    /// Remote tag (To tag for UAC, From tag for UAS).
    pub remote_tag: String,
}

impl DialogId {
    /// Create a new dialog ID.
    pub fn new(
        call_id: impl Into<String>,
        local_tag: impl Into<String>,
        remote_tag: impl Into<String>,
    ) -> Self {
        Self {
            call_id: call_id.into(),
            local_tag: local_tag.into(),
            remote_tag: remote_tag.into(),
        }
    }

    /// Create a dialog ID from a request (UAC perspective).
    ///
    /// Returns None if the request doesn't have the required tags.
    pub fn from_request_uac(request: &SipRequest, remote_tag: &str) -> Option<Self> {
        let call_id = request.call_id().ok()?;
        let local_tag = request.from_tag().ok()?;

        Some(Self {
            call_id,
            local_tag,
            remote_tag: remote_tag.to_string(),
        })
    }

    /// Create a dialog ID from a response (UAC perspective).
    ///
    /// Returns None if the response doesn't have the required tags.
    pub fn from_response_uac(response: &SipResponse) -> Option<Self> {
        let call_id = response.call_id().ok()?;
        let local_tag = response.from_tag().ok()?;
        let remote_tag = response.to_tag()?;

        Some(Self {
            call_id,
            local_tag,
            remote_tag,
        })
    }

    /// Create a dialog ID from a request (UAS perspective).
    ///
    /// Returns None if the request doesn't have the required tags.
    pub fn from_request_uas(request: &SipRequest, local_tag: &str) -> Option<Self> {
        let call_id = request.call_id().ok()?;
        let remote_tag = request.from_tag().ok()?;

        Some(Self {
            call_id,
            local_tag: local_tag.to_string(),
            remote_tag,
        })
    }
}

/// State of a dialog.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DialogState {
    /// Early dialog - provisional response received but no final response yet.
    Early,
    /// Confirmed dialog - 2xx response received.
    Confirmed,
    /// Dialog is being terminated.
    Terminating,
    /// Dialog has been terminated.
    Terminated,
}

/// Route set for in-dialog requests (RFC 3261 Section 12.2).
#[derive(Debug, Clone, Default)]
pub struct RouteSet {
    /// List of Route URIs (derived from Record-Route headers).
    routes: Vec<String>,
    /// Whether routes use loose routing (have ;lr parameter).
    loose_routing: bool,
}

impl RouteSet {
    /// Create an empty route set.
    pub fn new() -> Self {
        Self {
            routes: Vec::new(),
            loose_routing: false,
        }
    }

    /// Create a route set from Record-Route header values.
    ///
    /// For UAC (caller), the routes should be reversed (top Record-Route becomes last route).
    /// For UAS (callee), the routes are used in order as received.
    pub fn from_record_route_values(record_route_values: &[String], reverse: bool) -> Self {
        let record_routes = SipRecordRoute::parse_all(record_route_values);

        let mut routes: Vec<String> = record_routes
            .iter()
            .map(|rr| rr.to_header_value())
            .collect();

        if reverse {
            routes.reverse();
        }

        // Check if first route uses loose routing
        let loose_routing = record_routes.first().map(|rr| rr.lr).unwrap_or(false);

        Self {
            routes,
            loose_routing,
        }
    }

    /// Get the routes as string values.
    pub fn routes(&self) -> &[String] {
        &self.routes
    }

    /// Check if the route set is empty.
    pub fn is_empty(&self) -> bool {
        self.routes.is_empty()
    }

    /// Check if the route set uses loose routing.
    pub fn is_loose_routing(&self) -> bool {
        self.loose_routing
    }

    /// Get the number of routes.
    pub fn len(&self) -> usize {
        self.routes.len()
    }
}

/// Dialog state information.
#[derive(Debug, Clone)]
pub struct DialogInfo {
    /// Dialog ID.
    pub id: DialogId,
    /// Current state.
    pub state: DialogState,
    /// Local sequence number (for outgoing requests).
    pub local_seq: u32,
    /// Remote sequence number (for incoming requests).
    pub remote_seq: Option<u32>,
    /// Local URI.
    pub local_uri: String,
    /// Remote URI.
    pub remote_uri: String,
    /// Remote target (Contact URI from peer).
    pub remote_target: String,
    /// Local contact URI (the URI we advertise in our Contact: header
    /// for in-dialog requests/responses, RFC 3261 §12.2.1.1).
    pub local_contact: String,
    /// Route set.
    pub route_set: RouteSet,
    /// Whether this is a secure dialog (established over TLS).
    pub secure: bool,
}

impl DialogInfo {
    /// Create dialog info for a UAC from an outgoing INVITE and incoming response.
    pub fn from_invite_response_uac(
        request: &SipRequest,
        response: &SipResponse,
        state: DialogState,
    ) -> Option<Self> {
        let id = DialogId::from_response_uac(response)?;
        let local_uri = request.from_uri().ok()?.to_string();
        let remote_uri = request.to_uri().ok()?.to_string();
        let remote_target = response.contact_uri()?.to_string();
        let local_seq = request.cseq().ok()?;

        // Extract Record-Route headers and reverse for UAC (RFC 3261 Section 12.1.2)
        let record_routes = response.record_routes();
        let route_set = RouteSet::from_record_route_values(&record_routes, true);

        // Detect if dialog is secure from transport (TLS/SIPS)
        let secure = Self::detect_secure_transport(request);

        // For UAC, our local contact is the Contact we put in the INVITE.
        let local_contact = request
            .contact_uri()
            .map(|u| u.to_string())
            .unwrap_or_default();

        Some(Self {
            id,
            state,
            local_seq,
            remote_seq: None,
            local_uri,
            remote_uri,
            remote_target,
            local_contact,
            route_set,
            secure,
        })
    }

    /// Create dialog info for a UAS from an incoming INVITE.
    pub fn from_invite_uas(
        request: &SipRequest,
        local_tag: &str,
        local_contact: &str,
        state: DialogState,
    ) -> Option<Self> {
        let call_id = request.call_id().ok()?;
        let (remote_tag, remote_uri) = request.from_tag_and_uri().ok()?;
        let id = DialogId::new(&call_id, local_tag, &remote_tag);
        let local_uri = request.to_uri().ok()?.to_string();
        let remote_target = request.contact_uri()?.to_string();
        let remote_seq = request.cseq().ok()?;

        // Extract Record-Route headers (not reversed for UAS per RFC 3261 Section 12.1.1)
        let record_routes = request.record_routes();
        let route_set = RouteSet::from_record_route_values(&record_routes, false);

        // Detect if dialog is secure from transport
        let secure = Self::detect_secure_transport(request);

        Some(Self {
            id,
            state,
            local_seq: 0, // Will be set when sending first in-dialog request
            remote_seq: Some(remote_seq),
            local_uri,
            remote_uri: remote_uri.to_string(),
            remote_target,
            local_contact: local_contact.to_string(),
            route_set,
            secure,
        })
    }

    /// Detect if the transport is secure (TLS) from the Via header.
    fn detect_secure_transport(request: &SipRequest) -> bool {
        let via_values = request.via_headers_raw();
        if let Some(first_via) = via_values.first() {
            let via_value = first_via
                .trim()
                .strip_prefix("Via:")
                .unwrap_or(first_via)
                .trim();
            if let Ok(via) = Via::parse(via_value) {
                return via.protocol.eq_ignore_ascii_case("TLS");
            }
        }
        false
    }

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

    /// Update remote sequence number.
    ///
    /// Returns true if the sequence number is valid (greater than current).
    pub fn update_remote_seq(&mut self, seq: u32) -> bool {
        match self.remote_seq {
            None => {
                self.remote_seq = Some(seq);
                true
            }
            Some(current) if seq > current => {
                self.remote_seq = Some(seq);
                true
            }
            _ => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sip::{Method, SipMessage};

    fn create_invite() -> SipRequest {
        SipRequest::builder()
            .method(Method::Invite)
            .uri("sip:bob@example.com")
            .via("192.168.1.1", 5060, "UDP", "z9hG4bKtest")
            .from("sip:alice@example.com", "fromtag")
            .to("sip:bob@example.com")
            .call_id("test@example.com")
            .cseq(1)
            .contact("sip:alice@192.168.1.1:5060")
            .build()
            .unwrap()
    }

    fn parse_request(raw: &str) -> SipRequest {
        let msg = SipMessage::parse(raw.as_bytes()).unwrap();
        msg.as_request().unwrap().clone()
    }

    fn parse_response(raw: &str) -> SipResponse {
        let msg = SipMessage::parse(raw.as_bytes()).unwrap();
        msg.as_response().unwrap().clone()
    }

    fn create_response(request: &SipRequest) -> SipResponse {
        SipResponse::builder()
            .status(200, "OK")
            .from_request(request)
            .to_tag("totag")
            .contact("sip:bob@192.168.1.2:5060")
            .build()
            .unwrap()
    }

    #[test]
    fn test_dialog_id_from_response_uac() {
        let invite = create_invite();
        let response = create_response(&invite);
        let id = DialogId::from_response_uac(&response).unwrap();

        assert_eq!(id.call_id, "test@example.com");
        assert_eq!(id.local_tag, "fromtag");
        assert_eq!(id.remote_tag, "totag");
    }

    #[test]
    fn test_dialog_id_from_request_uac_missing_call_id() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let id = DialogId::from_request_uac(&invite, "remotetag");
        assert!(id.is_none());
    }

    #[test]
    fn test_dialog_id_from_request_uac_missing_from_tag() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let id = DialogId::from_request_uac(&invite, "remotetag");
        assert!(id.is_none());
    }

    #[test]
    fn test_dialog_id_from_response_uac_missing_call_id() {
        let response = parse_response(
            "SIP/2.0 200 OK\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>;tag=totag\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:bob@192.168.1.2:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let id = DialogId::from_response_uac(&response);
        assert!(id.is_none());
    }

    #[test]
    fn test_dialog_id_from_response_uac_missing_from_tag() {
        let response = parse_response(
            "SIP/2.0 200 OK\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>\r\n\
To: <sip:bob@example.com>;tag=totag\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:bob@192.168.1.2:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let id = DialogId::from_response_uac(&response);
        assert!(id.is_none());
    }

    #[test]
    fn test_dialog_id_from_request_uas_missing_call_id() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let id = DialogId::from_request_uas(&invite, "localtag");
        assert!(id.is_none());
    }

    #[test]
    fn test_dialog_id_from_request_uas_missing_from_tag() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let id = DialogId::from_request_uas(&invite, "mytag");
        assert!(id.is_none());
    }

    #[test]
    fn test_dialog_id_from_request_uac() {
        let invite = create_invite();
        let id = DialogId::from_request_uac(&invite, "remotetag").unwrap();
        assert_eq!(id.call_id, "test@example.com");
        assert_eq!(id.local_tag, "fromtag");
        assert_eq!(id.remote_tag, "remotetag");
    }

    #[test]
    fn test_dialog_id_from_request_uas() {
        let invite = create_invite();
        let id = DialogId::from_request_uas(&invite, "mytag").unwrap();

        assert_eq!(id.call_id, "test@example.com");
        assert_eq!(id.local_tag, "mytag");
        assert_eq!(id.remote_tag, "fromtag");
    }

    #[test]
    fn test_dialog_info_from_invite_uas_option() {
        let invite = create_invite();
        let info = DialogInfo::from_invite_uas(
            &invite,
            "localtag",
            "sip:local@example.com",
            DialogState::Early,
        )
        .unwrap();

        assert_eq!(info.state, DialogState::Early);
        assert_eq!(info.remote_seq, Some(1));
        assert_eq!(info.remote_uri, "sip:alice@example.com");
    }

    #[test]
    fn test_dialog_info_from_response() {
        let invite = create_invite();
        let response = create_response(&invite);
        let info = DialogInfo::from_invite_response_uac(&invite, &response, DialogState::Confirmed)
            .unwrap();

        assert_eq!(info.state, DialogState::Confirmed);
        assert_eq!(info.local_seq, 1);
        assert!(info.remote_seq.is_none());
    }

    #[test]
    fn test_dialog_info_from_response_invalid_from_uri() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@[::1>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        assert!(invite.from_uri().is_err());
        let response = parse_response(
            "SIP/2.0 200 OK\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>;tag=totag\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:bob@192.168.1.2:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let info = DialogInfo::from_invite_response_uac(&invite, &response, DialogState::Confirmed);
        assert!(info.is_none());
    }

    #[test]
    fn test_dialog_info_from_response_invalid_to_uri() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@[::1>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        assert!(invite.to_uri().is_err());
        let response = parse_response(
            "SIP/2.0 200 OK\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>;tag=totag\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:bob@192.168.1.2:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let info = DialogInfo::from_invite_response_uac(&invite, &response, DialogState::Confirmed);
        assert!(info.is_none());
    }

    #[test]
    fn test_dialog_info_from_response_missing_contact() {
        let invite = create_invite();
        let response = parse_response(
            "SIP/2.0 200 OK\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>;tag=totag\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let info = DialogInfo::from_invite_response_uac(&invite, &response, DialogState::Confirmed);
        assert!(info.is_none());
    }

    #[test]
    fn test_dialog_info_from_response_invalid_cseq() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: abc INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let response = create_response(&invite);
        let info = DialogInfo::from_invite_response_uac(&invite, &response, DialogState::Confirmed);
        assert!(info.is_none());
    }

    #[test]
    fn test_dialog_info_from_invite_uas_invalid_from_uri() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@[::1>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        assert!(invite.from_uri().is_err());
        let info = DialogInfo::from_invite_uas(
            &invite,
            "localtag",
            "sip:local@example.com",
            DialogState::Early,
        );
        assert!(info.is_none());
    }

    #[test]
    fn test_dialog_info_from_invite_uas_invalid_to_uri() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@[::1>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let info = DialogInfo::from_invite_uas(
            &invite,
            "localtag",
            "sip:local@example.com",
            DialogState::Early,
        );
        assert!(info.is_none());
    }

    #[test]
    fn test_dialog_info_from_invite_uas_invalid_contact() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@[::1>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let info = DialogInfo::from_invite_uas(
            &invite,
            "localtag",
            "sip:local@example.com",
            DialogState::Early,
        );
        assert!(info.is_none());
    }

    #[test]
    fn test_dialog_info_from_invite_uas_invalid_cseq() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: abc INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let info = DialogInfo::from_invite_uas(
            &invite,
            "localtag",
            "sip:local@example.com",
            DialogState::Early,
        );
        assert!(info.is_none());
    }

    #[test]
    fn test_dialog_info_from_invite_uas_missing_call_id() {
        let invite = parse_request(
            "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/UDP 192.168.1.1:5060;branch=z9hG4bKtest\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n",
        );
        let info = DialogInfo::from_invite_uas(
            &invite,
            "localtag",
            "sip:local@example.com",
            DialogState::Early,
        );
        assert!(info.is_none());
    }

    #[test]
    fn test_detect_secure_transport_prefix_via() {
        let raw = "INVITE sip:bob@example.com SIP/2.0\r\n\
Via: SIP/2.0/TLS 192.168.1.1:5061;branch=z9hG4bKtls\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n";
        let invite = parse_request(raw);
        let info = DialogInfo::from_invite_uas(
            &invite,
            "localtag",
            "sip:local@example.com",
            DialogState::Early,
        )
        .unwrap();
        assert!(info.secure);
    }

    #[test]
    fn test_detect_secure_transport_prefix_via_lowercase() {
        let raw = "INVITE sip:bob@example.com SIP/2.0\r\n\
via: SIP/2.0/TLS 192.168.1.1:5061;branch=z9hG4bKtls\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Contact: <sip:alice@192.168.1.1:5060>\r\n\
Content-Length: 0\r\n\
\r\n";
        let invite = parse_request(raw);
        let info = DialogInfo::from_invite_uas(
            &invite,
            "localtag",
            "sip:local@example.com",
            DialogState::Early,
        )
        .unwrap();
        assert!(info.secure);
    }

    #[test]
    fn test_next_local_seq() {
        let invite = create_invite();
        let response = create_response(&invite);
        let mut info =
            DialogInfo::from_invite_response_uac(&invite, &response, DialogState::Confirmed)
                .unwrap();

        assert_eq!(info.next_local_seq(), 2);
        assert_eq!(info.next_local_seq(), 3);
    }

    #[test]
    fn test_update_remote_seq() {
        let invite = create_invite();
        let response = create_response(&invite);
        let mut info =
            DialogInfo::from_invite_response_uac(&invite, &response, DialogState::Confirmed)
                .unwrap();

        assert!(info.update_remote_seq(1));
        assert!(info.update_remote_seq(2));
        assert!(!info.update_remote_seq(1)); // Old seq rejected
        assert!(!info.update_remote_seq(2)); // Same seq rejected
    }

    // Additional tests for coverage

    #[test]
    fn test_dialog_id_new() {
        let id = DialogId::new("call123", "local456", "remote789");
        assert_eq!(id.call_id, "call123");
        assert_eq!(id.local_tag, "local456");
        assert_eq!(id.remote_tag, "remote789");
    }

    #[test]
    fn test_dialog_id_clone() {
        let id = DialogId::new("call123", "local456", "remote789");
        let cloned = id.clone();
        assert_eq!(cloned.call_id, id.call_id);
        assert_eq!(cloned.local_tag, id.local_tag);
        assert_eq!(cloned.remote_tag, id.remote_tag);
    }

    #[test]
    fn test_dialog_id_eq() {
        let id1 = DialogId::new("call123", "local456", "remote789");
        let id2 = DialogId::new("call123", "local456", "remote789");
        let id3 = DialogId::new("call999", "local456", "remote789");
        assert_eq!(id1, id2);
        assert_ne!(id1, id3);
    }

    #[test]
    fn test_dialog_id_hash() {
        use std::collections::HashSet;
        let mut set = HashSet::new();
        set.insert(DialogId::new("call123", "local456", "remote789"));
        assert!(set.contains(&DialogId::new("call123", "local456", "remote789")));
        assert!(!set.contains(&DialogId::new("call999", "local456", "remote789")));
    }

    #[test]
    fn test_dialog_id_debug() {
        let id = DialogId::new("call123", "local456", "remote789");
        let debug = format!("{:?}", id);
        assert!(debug.contains("call123"));
        assert!(debug.contains("local456"));
        assert!(debug.contains("remote789"));
    }

    #[test]
    fn test_dialog_state_all_variants() {
        let states = [
            DialogState::Early,
            DialogState::Confirmed,
            DialogState::Terminating,
            DialogState::Terminated,
        ];
        for state in states {
            let _ = format!("{:?}", state);
        }
    }

    #[test]
    fn test_dialog_state_clone() {
        let state = DialogState::Early;
        let cloned = state;
        assert_eq!(state, cloned);
    }

    #[test]
    fn test_route_set_new() {
        let route_set = RouteSet::new();
        assert!(route_set.is_empty());
        assert_eq!(route_set.len(), 0);
        assert!(!route_set.is_loose_routing());
    }

    #[test]
    fn test_route_set_default() {
        let route_set = RouteSet::default();
        assert!(route_set.is_empty());
        assert!(!route_set.is_loose_routing());
    }

    #[test]
    fn test_route_set_routes() {
        let routes = RouteSet::new();
        assert!(routes.routes().is_empty());
    }

    #[test]
    fn test_route_set_from_record_route_values() {
        let record_routes = vec![
            "<sip:proxy1.example.com;lr>".to_string(),
            "<sip:proxy2.example.com;lr>".to_string(),
        ];

        let route_set = RouteSet::from_record_route_values(&record_routes, false);
        assert!(!route_set.is_empty());
        assert_eq!(route_set.len(), 2);
        // The lr parameter detection depends on parsing
        // Just check that routes are not empty
    }

    #[test]
    fn test_route_set_from_record_route_values_reversed() {
        let record_routes = vec![
            "<sip:proxy1.example.com;lr>".to_string(),
            "<sip:proxy2.example.com;lr>".to_string(),
        ];

        let route_set = RouteSet::from_record_route_values(&record_routes, true);
        assert!(!route_set.is_empty());
        assert_eq!(route_set.len(), 2);
        // Routes should be reversed
        let routes = route_set.routes();
        assert!(routes[0].contains("proxy2"));
        assert!(routes[1].contains("proxy1"));
    }

    #[test]
    fn test_route_set_from_record_route_values_no_lr() {
        let record_routes = vec!["<sip:proxy1.example.com>".to_string()];

        let route_set = RouteSet::from_record_route_values(&record_routes, false);
        assert!(!route_set.is_empty());
        // No lr parameter
        assert!(!route_set.is_loose_routing());
    }

    #[test]
    fn test_route_set_from_record_route_values_empty() {
        let record_routes: Vec<String> = vec![];
        let route_set = RouteSet::from_record_route_values(&record_routes, false);
        assert!(route_set.is_empty());
        assert!(!route_set.is_loose_routing());
    }

    #[test]
    fn test_route_set_debug() {
        let route_set = RouteSet::new();
        let debug = format!("{:?}", route_set);
        assert!(debug.contains("RouteSet"));
    }

    #[test]
    fn test_route_set_clone() {
        let record_routes = vec!["<sip:proxy.example.com;lr>".to_string()];
        let route_set = RouteSet::from_record_route_values(&record_routes, false);
        let cloned = route_set.clone();
        assert_eq!(cloned.len(), route_set.len());
        assert_eq!(cloned.is_loose_routing(), route_set.is_loose_routing());
    }

    #[test]
    fn test_dialog_info_from_invite_uas() {
        let invite = create_invite();
        let info = DialogInfo::from_invite_uas(
            &invite,
            "mytag",
            "sip:me@192.168.1.2:5060",
            DialogState::Early,
        );

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.state, DialogState::Early);
        assert_eq!(info.id.local_tag, "mytag");
        assert_eq!(info.id.remote_tag, "fromtag");
        assert_eq!(info.local_seq, 0);
        assert!(info.remote_seq.is_some());
        assert_eq!(info.remote_seq.unwrap(), 1);
    }

    #[test]
    fn test_dialog_id_from_request_uac_option() {
        let invite = create_invite();
        let id = DialogId::from_request_uac(&invite, "remote_tag");

        assert!(id.is_some());
        let id = id.unwrap();
        assert_eq!(id.call_id, "test@example.com");
        assert_eq!(id.local_tag, "fromtag");
        assert_eq!(id.remote_tag, "remote_tag");
    }

    #[test]
    fn test_dialog_info_secure_transport_detection() {
        // Test the detect_secure_transport function indirectly
        // The TLS transport detection checks the Via protocol
        let invite = create_invite(); // Uses UDP
        let is_secure = DialogInfo::detect_secure_transport(&invite);
        assert!(!is_secure); // UDP is not secure
    }

    #[test]
    fn test_dialog_info_secure_transport_tls() {
        let invite = SipRequest::builder()
            .method(Method::Invite)
            .uri("sip:bob@example.com")
            .via("192.168.1.1", 5061, "TLS", "z9hG4bKtest")
            .from("sip:alice@example.com", "fromtag")
            .to("sip:bob@example.com")
            .call_id("test@example.com")
            .cseq(1)
            .build()
            .unwrap();
        let is_secure = DialogInfo::detect_secure_transport(&invite);
        assert!(is_secure);
    }

    #[test]
    fn test_dialog_info_secure_transport_missing_via() {
        let msg = b"INVITE sip:bob@example.com SIP/2.0\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Content-Length: 0\r\n\
\r\n";
        let parsed = SipMessage::parse(msg).unwrap();
        let invite = parsed.as_request().expect("expected request");
        let is_secure = DialogInfo::detect_secure_transport(invite);
        assert!(!is_secure);
    }

    #[test]
    fn test_dialog_info_secure_transport_invalid_via() {
        let msg = b"INVITE sip:bob@example.com SIP/2.0\r\n\
Via: INVALID\r\n\
From: <sip:alice@example.com>;tag=fromtag\r\n\
To: <sip:bob@example.com>\r\n\
Call-ID: test@example.com\r\n\
CSeq: 1 INVITE\r\n\
Content-Length: 0\r\n\
\r\n";
        let parsed = SipMessage::parse(msg).unwrap();
        let invite = parsed.as_request().expect("expected request");
        let is_secure = DialogInfo::detect_secure_transport(invite);
        assert!(!is_secure);
    }

    #[test]
    fn test_dialog_info_secure_transport_udp() {
        let invite = create_invite(); // Uses UDP
        let info = DialogInfo::from_invite_uas(
            &invite,
            "mytag",
            "sip:me@192.168.1.2:5060",
            DialogState::Early,
        )
        .unwrap();
        assert!(!info.secure);
    }

    #[test]
    fn test_dialog_info_clone() {
        let invite = create_invite();
        let response = create_response(&invite);
        let info = DialogInfo::from_invite_response_uac(&invite, &response, DialogState::Confirmed)
            .unwrap();
        let cloned = info.clone();

        assert_eq!(cloned.id.call_id, info.id.call_id);
        assert_eq!(cloned.state, info.state);
        assert_eq!(cloned.local_seq, info.local_seq);
        assert_eq!(cloned.remote_seq, info.remote_seq);
    }

    #[test]
    fn test_dialog_info_debug() {
        let invite = create_invite();
        let response = create_response(&invite);
        let info = DialogInfo::from_invite_response_uac(&invite, &response, DialogState::Confirmed)
            .unwrap();
        let debug = format!("{:?}", info);
        assert!(debug.contains("DialogInfo"));
    }
}