From 714717ad0ef82da3d25e139ddf5010291237e179 Mon Sep 17 00:00:00 2001 From: yeoleobun Date: Thu, 25 Dec 2025 16:17:47 +0800 Subject: [PATCH] remove to tag from CANCEL, and update INVITE when auth failed --- src/dialog/client_dialog.rs | 22 ++++++++++++++++------ src/dialog/dialog.rs | 11 ++++++++--- src/dialog/server_dialog.rs | 21 +++++++++++---------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/dialog/client_dialog.rs b/src/dialog/client_dialog.rs index 3f7d004..f609a4d 100644 --- a/src/dialog/client_dialog.rs +++ b/src/dialog/client_dialog.rs @@ -192,17 +192,18 @@ impl ClientInviteDialog { return Ok(()); } info!(id=%self.id(),"sending cancel request"); - let mut cancel_request = self.inner.initial_request.clone(); + let mut cancel_request = self + .inner + .initial_request + .lock() + .expect("cancel mutext poisoned") + .clone(); + let invite_seq = cancel_request.cseq_header()?.seq()?; cancel_request .headers_mut() .retain(|h| !matches!(h, Header::ContentLength(_) | Header::ContentType(_))); - cancel_request - .to_header_mut()? - .mut_tag(self.id().to_tag.clone().into())?; // ensure to-tag has tag param - cancel_request.method = rsip::Method::Cancel; - let invite_seq = self.inner.initial_request.cseq_header()?.seq()?; cancel_request .cseq_header_mut()? .mut_seq(invite_seq)? @@ -539,6 +540,15 @@ impl ClientInviteDialog { .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() + .expect("update initial request mutex poisoned"); + *req = tx.original.clone(); + } continue; } else { info!(id=%self.id(),"received 407 response without auth option"); diff --git a/src/dialog/dialog.rs b/src/dialog/dialog.rs index a09c220..b3027cf 100644 --- a/src/dialog/dialog.rs +++ b/src/dialog/dialog.rs @@ -189,7 +189,8 @@ pub struct DialogInner { pub(super) endpoint_inner: EndpointInnerRef, pub(super) state_sender: DialogStateSender, pub(super) tu_sender: TransactionEventSender, - pub(super) initial_request: Request, + // initial request updated when INVITE auth failed with new INVITE + pub(super) initial_request: Mutex, pub(super) supports_100rel: bool, pub(super) remote_reliable: Mutex>, } @@ -285,7 +286,7 @@ impl DialogInner { state_sender, tu_sender, state: Mutex::new(DialogState::Calling(id)), - initial_request, + initial_request: Mutex::new(initial_request), local_contact, remote_contact: Mutex::new(None), supports_100rel, @@ -512,7 +513,11 @@ impl DialogInner { pub(super) fn build_vias_from_request(&self) -> Result> { let mut vias = vec![]; - for header in self.initial_request.headers.iter() { + let initial_request = self + .initial_request + .lock() + .expect("build vias from request poisoned mutex"); + for header in initial_request.headers.iter() { if let Header::Via(via) = header { if let Ok(mut typed_via) = via.typed() { for param in typed_via.params.iter_mut() { diff --git a/src/dialog/server_dialog.rs b/src/dialog/server_dialog.rs index 40929b4..dc931a4 100644 --- a/src/dialog/server_dialog.rs +++ b/src/dialog/server_dialog.rs @@ -116,8 +116,12 @@ impl ServerInviteDialog { /// Returns a reference to the initial INVITE request that created /// this dialog. This can be used to access the original request /// headers, body, and other information. - pub fn initial_request(&self) -> &Request { - &self.inner.initial_request + pub fn initial_request(&self) -> Request { + self.inner + .initial_request + .lock() + .expect("get initial request posioned") + .clone() } pub fn ringing(&self, headers: Option>, body: Option>) -> Result<()> { @@ -126,7 +130,7 @@ impl ServerInviteDialog { } info!(id = %self.id(), "sending ringing response"); let resp = self.inner.make_response( - &self.inner.initial_request, + &self.initial_request(), if body.is_some() { StatusCode::SessionProgress } else { @@ -173,12 +177,9 @@ impl ServerInviteDialog { /// # } /// ``` pub fn accept(&self, headers: Option>, body: Option>) -> Result<()> { - let resp = self.inner.make_response( - &self.inner.initial_request, - rsip::StatusCode::OK, - headers, - body, - ); + let resp = + self.inner + .make_response(&self.initial_request(), rsip::StatusCode::OK, headers, body); self.inner .tu_sender .send(TransactionEvent::Respond(resp.clone()))?; @@ -290,7 +291,7 @@ impl ServerInviteDialog { None }; let resp = self.inner.make_response( - &self.inner.initial_request, + &self.initial_request(), code.unwrap_or(rsip::StatusCode::Decline), headers, None,