rsipstack 0.5.5

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
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
use super::dialog::DialogInnerRef;
use super::DialogId;
use crate::sip::prelude::HasHeaders;
use crate::sip::{prelude::HeadersExt, Header};
use crate::sip::{Response, SipMessage, StatusCode};
use crate::transaction::transaction::Transaction;
use crate::Result;
use crate::{
    dialog::{
        authenticate::handle_client_authenticate,
        dialog::{DialogState, TerminatedReason, TransactionHandle},
        subscription::ClientSubscriptionDialog,
    },
    transaction::key::TransactionRole,
};
use std::sync::atomic::Ordering;
use tokio_util::sync::CancellationToken;
use tracing::{debug, info, trace};

/// Client-side INVITE Dialog (UAC)
///
/// `ClientInviteDialog` represents a client-side INVITE dialog in SIP. This is used
/// when the local user agent acts as a User Agent Client (UAC) and initiates
/// an INVITE transaction to establish a session with a remote party.
///
/// # Key Features
///
/// * **Session Initiation** - Initiates INVITE transactions to establish calls
/// * **In-dialog Requests** - Sends UPDATE, INFO, OPTIONS within established dialogs
/// * **Session Termination** - Handles BYE and CANCEL for ending sessions
/// * **Re-INVITE Support** - Supports session modification via re-INVITE
/// * **Authentication** - Handles 401/407 authentication challenges
/// * **State Management** - Tracks dialog state transitions
///
/// # Dialog Lifecycle
///
/// 1. **Creation** - Dialog created when sending INVITE
/// 2. **Early State** - Receives provisional responses (1xx)
/// 3. **Confirmed** - Receives 2xx response and sends ACK
/// 4. **Active** - Can send in-dialog requests (UPDATE, INFO, etc.)
/// 5. **Termination** - Sends BYE or CANCEL to end session
///
/// # Examples
///
/// ## Basic Call Flow
///
/// ```rust,no_run
/// # use rsipstack::dialog::client_dialog::ClientInviteDialog;
/// # async fn example() -> rsipstack::Result<()> {
/// # let dialog: ClientInviteDialog = todo!(); // Dialog is typically created by DialogLayer.do_invite()
/// # let new_sdp_body = vec![];
/// # let info_body = vec![];
/// // After dialog is established:
///
/// // Send an UPDATE request
/// let response = dialog.update(None, Some(new_sdp_body)).await?;
///
/// // Send INFO request
/// let response = dialog.info(None, Some(info_body)).await?;
///
/// // End the call
/// dialog.bye().await?;
/// # Ok(())
/// # }
/// ```
///
/// ## Session Modification
///
/// ```rust,no_run
/// # use rsipstack::dialog::client_dialog::ClientInviteDialog;
/// # async fn example() -> rsipstack::Result<()> {
/// # let dialog: ClientInviteDialog = todo!();
/// # let new_sdp = vec![];
/// // Modify session with re-INVITE
/// let headers = vec![
///     rsipstack::sip::Header::ContentType("application/sdp".into())
/// ];
/// let response = dialog.reinvite(Some(headers), Some(new_sdp)).await?;
///
/// if let Some(resp) = response {
///     if resp.status_code == rsipstack::sip::StatusCode::OK {
///         println!("Session modified successfully");
///     }
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Thread Safety
///
/// ClientInviteDialog is thread-safe and can be cloned and shared across tasks.
/// All operations are atomic and properly synchronized.
#[derive(Clone)]
pub struct ClientInviteDialog {
    pub(super) inner: DialogInnerRef,
}

impl ClientInviteDialog {
    /// Get the dialog identifier
    ///
    /// Returns the unique DialogId that identifies this dialog instance.
    /// The DialogId consists of Call-ID, from-tag, and to-tag.
    pub fn id(&self) -> DialogId {
        self.inner.id.lock().clone()
    }

    pub fn state(&self) -> DialogState {
        self.inner.state.lock().clone()
    }

    pub fn from_inner(inner: DialogInnerRef) -> Self {
        Self { inner }
    }

    pub fn snapshot(&self) -> super::dialog::DialogSnapshot {
        self.inner.snapshot()
    }

    /// Get the cancellation token for this dialog
    ///
    /// Returns a reference to the CancellationToken that can be used to
    /// cancel ongoing operations for this dialog.
    pub fn cancel_token(&self) -> &CancellationToken {
        &self.inner.cancel_token
    }

    /// Send a BYE request to terminate the dialog.
    ///
    /// Thin wrapper over `bye_with_headers(None)`.
    ///
    /// ```rust,no_run
    /// # use rsipstack::dialog::client_dialog::ClientInviteDialog;
    /// # async fn example() -> rsipstack::Result<()> {
    /// # let dialog: ClientInviteDialog = todo!();
    /// // End an established call
    /// dialog.bye().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn bye(&self) -> Result<()> {
        self.bye_with_headers(None).await
    }

    /// Send a BYE request with custom headers to terminate the dialog.
    ///
    /// This is the low-level variant used to add SIP headers (e.g. `Reason`)
    /// to the outgoing BYE request.
    ///
    /// The dialog must be in `Confirmed` state for BYE to be sent; otherwise
    /// this method is a no-op.
    ///
    /// # Parameters
    /// * `headers` - Optional extra SIP headers to include in the BYE request.
    ///
    /// # Returns
    /// * `Ok(())` - BYE was sent successfully or dialog is not confirmed.
    /// * `Err(Error)` - Failed to build/send BYE request.
    pub async fn bye_with_headers(&self, headers: Option<Vec<crate::sip::Header>>) -> Result<()> {
        if !self.inner.is_confirmed() {
            return Ok(());
        }

        let request =
            self.inner
                .make_request(crate::sip::Method::Bye, None, None, None, headers, None)?;

        if let Err(e) = self.inner.do_request(request).await {
            info!(error = %e, "bye error");
        }

        self.inner
            .transition(DialogState::Terminated(self.id(), TerminatedReason::UacBye))?;
        Ok(())
    }

    /// Send a BYE request with a SIP `Reason` header.
    ///
    /// Convenience wrapper over `bye_with_headers()` that adds:
    /// `Reason: <reason>`.
    ///
    /// Typical values:
    /// * `SIP;cause=804;text="MEDIA_TIMEOUT"`
    /// * `Q.850;cause=16;text="Normal call clearing"`
    ///
    /// # Parameters
    /// * `reason` - Value of the `Reason` header (without the `Reason:` name).
    pub async fn bye_with_reason(&self, reason: String) -> Result<()> {
        self.bye_with_headers(Some(vec![crate::sip::Header::Reason(reason.into())]))
            .await
    }

    /// Hang up the call
    ///
    /// If the dialog is confirmed, send a BYE request to terminate the call.
    /// If the dialog is not confirmed, send a CANCEL request to cancel the call.
    ///
    /// Thin wrapper over `hangup_with_headers(None)`.
    pub async fn hangup(&self) -> Result<()> {
        self.hangup_with_headers(None).await
    }

    /// Hang up the call with custom headers.
    ///
    /// If the dialog is still in early phase and can be canceled, this sends `CANCEL`.
    /// Headers are not attached to the CANCEL request by default.
    ///
    /// If the dialog is confirmed, this sends `BYE` and attaches the provided headers.
    ///
    /// # Parameters
    /// * `headers` - Optional extra SIP headers to include when BYE is used.
    pub async fn hangup_with_headers(
        &self,
        headers: Option<Vec<crate::sip::Header>>,
    ) -> Result<()> {
        if self.inner.can_cancel() {
            self.cancel().await
        } else {
            self.bye_with_headers(headers).await
        }
    }

    /// Hang up the call and attach a SIP `Reason` header when BYE is used.
    ///
    /// Convenience wrapper over `hangup_with_headers()` that adds:
    /// `Reason: <reason>`.
    ///
    /// # Parameters
    /// * `reason` - Value of the `Reason` header used for BYE.
    pub async fn hangup_with_reason(&self, reason: String) -> Result<()> {
        self.hangup_with_headers(Some(vec![crate::sip::Header::Reason(reason.into())]))
            .await
    }

    /// Send a CANCEL request to cancel an ongoing INVITE
    ///
    /// Sends a CANCEL request to cancel an INVITE transaction that has not
    /// yet been answered with a final response. This is used to abort
    /// call setup before the call is established.
    ///
    /// # Returns
    ///
    /// * `Ok(())` - CANCEL was sent successfully
    /// * `Err(Error)` - Failed to send CANCEL request
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use rsipstack::dialog::client_dialog::ClientInviteDialog;
    /// # async fn example() -> rsipstack::Result<()> {
    /// # let dialog: ClientInviteDialog = todo!();
    /// // Cancel an outgoing call before it's answered
    /// dialog.cancel().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn cancel(&self) -> Result<()> {
        if self.inner.is_confirmed() {
            return Ok(());
        }
        debug!(id = %self.id(), "sending cancel request");
        let mut cancel_request = self.inner.initial_request.lock().clone();
        let invite_seq = cancel_request.cseq_header()?.seq()?;
        cancel_request
            .headers_mut()
            .retain(|h| !matches!(h, Header::ContentLength(_) | Header::ContentType(_)));

        cancel_request.method = crate::sip::Method::Cancel;
        cancel_request
            .cseq_header_mut()?
            .mut_seq(invite_seq)?
            .mut_method(crate::sip::Method::Cancel)?;
        cancel_request.body = vec![];
        self.inner.do_request(cancel_request).await?;
        Ok(())
    }

    /// Send a re-INVITE request to modify the session
    ///
    /// Sends a re-INVITE request within an established dialog to modify
    /// the session parameters (e.g., change media, add/remove streams).
    /// This can only be called for confirmed dialogs.
    ///
    /// # Parameters
    ///
    /// * `headers` - Optional additional headers to include
    /// * `body` - Optional message body (typically new SDP)
    ///
    /// # Returns
    ///
    /// * `Ok(Some(Response))` - Response to the re-INVITE
    /// * `Ok(None)` - Dialog not confirmed, no request sent
    /// * `Err(Error)` - Failed to send re-INVITE
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use rsipstack::dialog::client_dialog::ClientInviteDialog;
    /// # async fn example() -> rsipstack::Result<()> {
    /// # let dialog: ClientInviteDialog = todo!();
    /// let new_sdp = b"v=0\r\no=- 123 456 IN IP4 192.168.1.1\r\n...";
    /// let response = dialog.reinvite(None, Some(new_sdp.to_vec())).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn reinvite(
        &self,
        headers: Option<Vec<crate::sip::Header>>,
        body: Option<Vec<u8>>,
    ) -> Result<Option<crate::sip::Response>> {
        if !self.inner.is_confirmed() {
            return Ok(None);
        }
        debug!(id = %self.id(), ?body, "sending re-invite request");
        let request =
            self.inner
                .make_request(crate::sip::Method::Invite, None, None, None, headers, body)?;
        let resp = self.inner.do_request(request.clone()).await;
        match resp {
            Ok(Some(ref resp)) => {
                if resp.status_code == StatusCode::OK {
                    let (handle, _) = TransactionHandle::new();
                    self.inner
                        .transition(DialogState::Updated(self.id(), request, handle))?;
                }
            }
            _ => {}
        }
        resp
    }

    /// Send an UPDATE request to modify session parameters
    ///
    /// Sends an UPDATE request within an established dialog to modify
    /// session parameters without the complexity of a re-INVITE.
    /// This is typically used for smaller session modifications.
    ///
    /// # Parameters
    ///
    /// * `headers` - Optional additional headers to include
    /// * `body` - Optional message body (typically SDP)
    ///
    /// # Returns
    ///
    /// * `Ok(Some(Response))` - Response to the UPDATE
    /// * `Ok(None)` - Dialog not confirmed, no request sent
    /// * `Err(Error)` - Failed to send UPDATE
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use rsipstack::dialog::client_dialog::ClientInviteDialog;
    /// # async fn example() -> rsipstack::Result<()> {
    /// # let dialog: ClientInviteDialog = todo!();
    /// # let sdp_body = vec![];
    /// let response = dialog.update(None, Some(sdp_body)).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn update(
        &self,
        headers: Option<Vec<crate::sip::Header>>,
        body: Option<Vec<u8>>,
    ) -> Result<Option<crate::sip::Response>> {
        if !self.inner.is_confirmed() {
            return Ok(None);
        }
        debug!(id = %self.id(), ?body, "sending update request");
        let request =
            self.inner
                .make_request(crate::sip::Method::Update, None, None, None, headers, body)?;
        self.inner.do_request(request.clone()).await
    }

    /// Send an INFO request for mid-dialog information
    ///
    /// Sends an INFO request within an established dialog to exchange
    /// application-level information. This is commonly used for DTMF
    /// tones, but can carry any application-specific data.
    ///
    /// # Parameters
    ///
    /// * `headers` - Optional additional headers to include
    /// * `body` - Optional message body (application-specific data)
    ///
    /// # Returns
    ///
    /// * `Ok(Some(Response))` - Response to the INFO
    /// * `Ok(None)` - Dialog not confirmed, no request sent
    /// * `Err(Error)` - Failed to send INFO
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use rsipstack::dialog::client_dialog::ClientInviteDialog;
    /// # async fn example() -> rsipstack::Result<()> {
    /// # let dialog: ClientInviteDialog = todo!();
    /// // Send DTMF tone
    /// let dtmf_body = b"Signal=1\r\nDuration=100\r\n";
    /// let headers = vec![
    ///     rsipstack::sip::Header::ContentType("application/dtmf-relay".into())
    /// ];
    /// let response = dialog.info(Some(headers), Some(dtmf_body.to_vec())).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn info(
        &self,
        headers: Option<Vec<crate::sip::Header>>,
        body: Option<Vec<u8>>,
    ) -> Result<Option<crate::sip::Response>> {
        if !self.inner.is_confirmed() {
            return Ok(None);
        }
        debug!(id = %self.id(), ?body, "sending info request");
        let request =
            self.inner
                .make_request(crate::sip::Method::Info, None, None, None, headers, body)?;
        self.inner.do_request(request.clone()).await
    }

    pub async fn options(
        &self,
        headers: Option<Vec<crate::sip::Header>>,
        body: Option<Vec<u8>>,
    ) -> Result<Option<crate::sip::Response>> {
        if !self.inner.is_confirmed() {
            return Ok(None);
        }
        debug!(id = %self.id(), ?body, "sending option request");
        let request = self.inner.make_request(
            crate::sip::Method::Options,
            None,
            None,
            None,
            headers,
            body,
        )?;
        self.inner.do_request(request.clone()).await
    }

    /// Send a generic in-dialog request
    ///
    /// This method allows sending any SIP request within the dialog.
    /// It automatically handles CSeq increment, Call-ID, From/To tags, and Route set.
    pub async fn request(
        &self,
        method: crate::sip::Method,
        headers: Option<Vec<crate::sip::Header>>,
        body: Option<Vec<u8>>,
    ) -> Result<Option<crate::sip::Response>> {
        if !self.inner.is_confirmed() {
            return Ok(None);
        }
        debug!(id = %self.id(), %method, "sending request");
        let request = self
            .inner
            .make_request(method, None, None, None, headers, body)?;
        self.inner.do_request(request).await
    }

    /// Send a NOTIFY request
    pub async fn notify(
        &self,
        headers: Option<Vec<crate::sip::Header>>,
        body: Option<Vec<u8>>,
    ) -> Result<Option<crate::sip::Response>> {
        self.request(crate::sip::Method::Notify, headers, body)
            .await
    }

    /// Send a REFER request
    ///
    /// Sends a REFER request to transfer the call to another destination.
    ///
    /// # Parameters
    ///
    /// * `refer_to` - The URI to refer to (Refer-To header value)
    /// * `headers` - Optional additional headers
    /// * `body` - Optional message body
    pub async fn refer(
        &self,
        refer_to: crate::sip::Uri,
        headers: Option<Vec<crate::sip::Header>>,
        body: Option<Vec<u8>>,
    ) -> Result<Option<crate::sip::Response>> {
        let mut headers = headers.unwrap_or_default();
        headers.push(crate::sip::Header::Other(
            "Refer-To".into(),
            format!("<{}>", refer_to).into(),
        ));
        self.request(crate::sip::Method::Refer, Some(headers), body)
            .await
    }

    /// Send a REFER progress notification (RFC 3515)
    ///
    /// This is used by the REFER recipient to notify the sender about the
    /// progress of the referred action.
    ///
    /// # Parameters
    ///
    /// * `status` - The status of the referred action (e.g., 100 Trying, 200 OK)
    /// * `sub_state` - The subscription state (e.g., "active", "terminated;reason=noresource")
    pub async fn notify_refer(
        &self,
        status: crate::sip::StatusCode,
        sub_state: &str,
    ) -> Result<Option<crate::sip::Response>> {
        let headers = vec![
            crate::sip::Header::Event("refer".into()),
            crate::sip::Header::SubscriptionState(sub_state.into()),
            crate::sip::Header::ContentType("message/sipfrag".into()),
        ];

        let body = format!("SIP/2.0 {} {:?}", u16::from(status.clone()), status).into_bytes();

        self.notify(Some(headers), Some(body)).await
    }

    /// Convert this INVITE dialog to a subscription dialog
    ///
    /// This is useful for handling implicit subscriptions created by REFER.
    pub fn as_subscription(&self) -> ClientSubscriptionDialog {
        ClientSubscriptionDialog {
            inner: self.inner.clone(),
        }
    }

    /// Send a MESSAGE request
    ///
    /// Sends an instant message within the dialog.
    pub async fn message(
        &self,
        headers: Option<Vec<crate::sip::Header>>,
        body: Option<Vec<u8>>,
    ) -> Result<Option<crate::sip::Response>> {
        self.request(crate::sip::Method::Message, headers, body)
            .await
    }

    /// Handle incoming transaction for this dialog
    ///
    /// Processes incoming SIP requests that are routed to this dialog.
    /// This method handles sequence number validation and dispatches
    /// to appropriate handlers based on the request method.
    ///
    /// # Parameters
    ///
    /// * `tx` - The incoming transaction to handle
    ///
    /// # Returns
    ///
    /// * `Ok(())` - Request handled successfully
    /// * `Err(Error)` - Failed to handle request
    ///
    /// # Supported Methods
    ///
    /// * `BYE` - Terminates the dialog
    /// * `INFO` - Handles information exchange
    /// * `OPTIONS` - Handles capability queries
    /// * `UPDATE` - Handles session updates
    /// * `INVITE` - Handles re-INVITE (when confirmed)
    pub async fn handle(&mut self, tx: &mut Transaction) -> Result<()> {
        trace!(
            id = %self.id(),
            method = %tx.original.method,
            state = %self.inner.state.lock(),
            "handle request"
        );

        let cseq = tx.original.cseq_header()?.seq()?;
        let remote_seq = self.inner.remote_seq.load(Ordering::Relaxed);
        if remote_seq > 0 && cseq < remote_seq {
            debug!(
                id = %self.id(),
                remote_seq = %remote_seq,
                cseq = %cseq,
                "received old request"
            );
            tx.reply(crate::sip::StatusCode::ServerInternalError)
                .await?;
            return Ok(());
        }

        self.inner
            .remote_seq
            .compare_exchange(remote_seq, cseq, Ordering::Relaxed, Ordering::Relaxed)
            .ok();

        if self.inner.is_confirmed() {
            match tx.original.method {
                crate::sip::Method::Invite => return self.handle_reinvite(tx).await,
                crate::sip::Method::Bye => return self.handle_bye(tx).await,
                crate::sip::Method::Info => return self.handle_info(tx).await,
                crate::sip::Method::Options => return self.handle_options(tx).await,
                crate::sip::Method::Update => return self.handle_update(tx).await,
                crate::sip::Method::Refer => return self.handle_refer(tx).await,
                crate::sip::Method::Message => return self.handle_message(tx).await,
                crate::sip::Method::Notify => return self.handle_notify(tx).await,
                _ => {
                    debug!(id = %self.id(), method = ?tx.original.method, "invalid request method");
                    tx.reply(crate::sip::StatusCode::MethodNotAllowed).await?;
                    return Err(crate::Error::DialogError(
                        "invalid request".to_string(),
                        self.id(),
                        crate::sip::StatusCode::MethodNotAllowed,
                    ));
                }
            }
        } else {
            debug!(
                id = %self.id(),
                method = ?tx.original.method,
                "received request not confirmed"
            );
        }
        Ok(())
    }

    async fn handle_bye(&mut self, tx: &mut Transaction) -> Result<()> {
        debug!(id = %self.id(), uri = %tx.original.uri, "received bye");
        self.inner
            .transition(DialogState::Terminated(self.id(), TerminatedReason::UasBye))?;
        tx.reply(crate::sip::StatusCode::OK).await?;
        Ok(())
    }

    async fn handle_info(&mut self, tx: &mut Transaction) -> Result<()> {
        debug!(id = %self.id(), uri = %tx.original.uri, "received info");
        let (handle, rx) = TransactionHandle::new();
        self.inner
            .transition(DialogState::Info(self.id(), tx.original.clone(), handle))?;
        self.inner.process_transaction_handle(tx, rx).await
    }

    async fn handle_options(&mut self, tx: &mut Transaction) -> Result<()> {
        debug!(id = %self.id(), uri = %tx.original.uri, "received options");
        let (handle, rx) = TransactionHandle::new();
        self.inner
            .transition(DialogState::Options(self.id(), tx.original.clone(), handle))?;
        self.inner.process_transaction_handle(tx, rx).await
    }

    async fn handle_update(&mut self, tx: &mut Transaction) -> Result<()> {
        debug!(id = %self.id(), uri = %tx.original.uri, "received update");
        let (handle, rx) = TransactionHandle::new();
        self.inner
            .transition(DialogState::Updated(self.id(), tx.original.clone(), handle))?;
        self.inner.process_transaction_handle(tx, rx).await
    }

    async fn handle_reinvite(&mut self, tx: &mut Transaction) -> Result<()> {
        debug!(id = %self.id(), uri = %tx.original.uri, "received reinvite");
        let (handle, rx) = TransactionHandle::new();
        self.inner
            .transition(DialogState::Updated(self.id(), tx.original.clone(), handle))?;

        self.inner.process_transaction_handle(tx, rx).await?;

        // wait for ACK
        while let Some(msg) = tx.receive().await {
            match msg {
                SipMessage::Request(req) if req.method == crate::sip::Method::Ack => {
                    debug!(id = %self.id(), "received ACK for re-INVITE");
                    break;
                }
                _ => {}
            }
        }
        Ok(())
    }

    async fn handle_refer(&mut self, tx: &mut Transaction) -> Result<()> {
        debug!(id = %self.id(), uri = %tx.original.uri, "received refer");
        let (handle, rx) = TransactionHandle::new();
        self.inner
            .transition(DialogState::Refer(self.id(), tx.original.clone(), handle))?;

        self.inner.process_transaction_handle(tx, rx).await
    }

    async fn handle_message(&mut self, tx: &mut Transaction) -> Result<()> {
        debug!(id = %self.id(), uri = %tx.original.uri, "received message");
        let (handle, rx) = TransactionHandle::new();
        self.inner
            .transition(DialogState::Message(self.id(), tx.original.clone(), handle))?;

        self.inner.process_transaction_handle(tx, rx).await
    }

    async fn handle_notify(&mut self, tx: &mut Transaction) -> Result<()> {
        debug!(id = %self.id(), uri = %tx.original.uri, "received notify");
        let (handle, rx) = TransactionHandle::new();
        self.inner
            .transition(DialogState::Notify(self.id(), tx.original.clone(), handle))?;
        self.inner.process_transaction_handle(tx, rx).await
    }

    pub async fn process_invite(
        &self,
        tx: &mut Transaction,
    ) -> Result<(DialogId, Option<Response>)> {
        self.inner.transition(DialogState::Calling(self.id()))?;
        let mut auth_sent = false;
        tx.send().await?;
        let mut dialog_id = self.id();
        let mut final_response = None;
        while let Some(msg) = tx.receive().await {
            match msg {
                SipMessage::Request(_) => {}
                SipMessage::Response(resp) => {
                    let status = resp.status_code.clone();

                    if status == StatusCode::Trying {
                        self.inner.transition(DialogState::Trying(self.id()))?;
                        continue;
                    }

                    if matches!(status.kind(), crate::sip::StatusCodeKind::Provisional) {
                        self.inner.handle_provisional_response(&resp).await?;
                        self.inner.transition(DialogState::Early(self.id(), resp))?;
                        continue;
                    }

                    if matches!(
                        status,
                        StatusCode::ProxyAuthenticationRequired | StatusCode::Unauthorized
                    ) {
                        if auth_sent {
                            final_response = Some(resp.clone());
                            debug!(id = %self.id(), ?status, "received auth response after auth sent");
                            self.inner.transition(DialogState::Terminated(
                                self.id(),
                                TerminatedReason::ProxyAuthRequired,
                            ))?;
                            break;
                        }
                        auth_sent = true;
                        if let Some(credential) = &self.inner.credential {
                            *tx = handle_client_authenticate(
                                self.inner.increment_local_seq(),
                                tx,
                                resp,
                                credential,
                            )
                            .await?;
                            tx.send().await?;
                            self.inner.update_remote_tag("").ok();
                            // Update initial_request with the new invite request
                            {
                                let mut req = self.inner.initial_request.lock();
                                *req = tx.original.clone();
                            }
                            continue;
                        } else {
                            debug!(id=%self.id(),"received 407 response without auth option");
                            self.inner.transition(DialogState::Terminated(
                                self.id(),
                                TerminatedReason::ProxyAuthRequired,
                            ))?;
                            continue;
                        }
                    }
                    final_response = Some(resp.clone());
                    match resp.to_header()?.tag()? {
                        Some(tag) => self.inner.update_remote_tag(tag.value())?,
                        None => {}
                    }

                    if let Ok(id) = DialogId::try_from((&resp, TransactionRole::Client)) {
                        dialog_id = id;
                    }
                    match resp.status_code {
                        StatusCode::Ringing | StatusCode::SessionProgress
                            if resp
                                .to_header()
                                .ok()
                                .and_then(|h| h.tag().ok().flatten())
                                .is_some() =>
                        {
                            self.inner.update_route_set_from_response(&resp);
                        }
                        StatusCode::OK => {
                            self.inner.update_route_set_from_response(&resp);
                            // 200 response to INVITE always contains Contact header
                            let contact = resp.contact_header()?;
                            self.inner.remote_contact.lock().replace(contact.clone());

                            let contact_uri = resp
                                .typed_contact_headers()?
                                .first()
                                .map(|c| c.uri.clone())
                                .ok_or_else(|| {
                                    crate::Error::Error("missing Contact header".to_string())
                                })?;
                            *self.inner.remote_uri.lock() = contact_uri;
                            self.inner
                                .transition(DialogState::Confirmed(dialog_id.clone(), resp))?;
                        }
                        _ => {
                            self.inner.transition(DialogState::Terminated(
                                self.id(),
                                TerminatedReason::UasOther(resp.status_code.clone()),
                            ))?;
                        }
                    }
                    break;
                }
            }
        }
        Ok((dialog_id, final_response))
    }
}