From f7e9ae642bf59ffa37da62b5b0f276e0c56a5f3d Mon Sep 17 00:00:00 2001 From: Konstantin Baltruschat Date: Wed, 13 Sep 2023 16:51:27 +0200 Subject: [PATCH] sip-ua: Allow handling of 423 errors when registering --- crates/sip-types/src/header/typed/expires.rs | 27 ++++++++++++++++++++ crates/sip-types/src/header/typed/mod.rs | 2 +- crates/sip-ua/src/register/mod.rs | 17 +++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/crates/sip-types/src/header/typed/expires.rs b/crates/sip-types/src/header/typed/expires.rs index 45c036e..906b173 100644 --- a/crates/sip-types/src/header/typed/expires.rs +++ b/crates/sip-types/src/header/typed/expires.rs @@ -12,6 +12,13 @@ from_str_header! { u32 } +from_str_header! { + /// `Min-Expires` header + MinExpires, + Name::MIN_EXPIRES, + u32 +} + #[cfg(test)] mod test { use super::*; @@ -36,4 +43,24 @@ mod test { let expires: Expires = headers.get_named().unwrap(); assert_eq!(expires, EXPIRES); } + + const MIN_EXPIRES: MinExpires = MinExpires(300); + + #[test] + fn print_min_expires() { + let mut headers = Headers::new(); + headers.insert_named(&MIN_EXPIRES); + let headers = headers.to_string(); + + assert_eq!(headers, "Min-Expires: 300\r\n"); + } + + #[test] + fn parse_min_expires() { + let mut headers = Headers::new(); + headers.insert(Name::MIN_EXPIRES, "300"); + + let min_expires: MinExpires = headers.get_named().unwrap(); + assert_eq!(min_expires, MIN_EXPIRES); + } } diff --git a/crates/sip-types/src/header/typed/mod.rs b/crates/sip-types/src/header/typed/mod.rs index c0b7e34..5eb3411 100644 --- a/crates/sip-types/src/header/typed/mod.rs +++ b/crates/sip-types/src/header/typed/mod.rs @@ -25,7 +25,7 @@ pub use call_id::CallID; pub use contact::Contact; pub use content::{ContentLength, ContentType}; pub use cseq::CSeq; -pub use expires::Expires; +pub use expires::{Expires, MinExpires}; pub use extensions::{Require, Supported}; pub use from_to::FromTo; pub use max_fwd::MaxForwards; diff --git a/crates/sip-ua/src/register/mod.rs b/crates/sip-ua/src/register/mod.rs index 45d903c..9e149bc 100644 --- a/crates/sip-ua/src/register/mod.rs +++ b/crates/sip-ua/src/register/mod.rs @@ -1,7 +1,7 @@ use crate::util::{random_sequence_number, random_string}; use sip_core::transaction::TsxResponse; use sip_core::Request; -use sip_types::header::typed::{CSeq, CallID, Contact, Expires, FromTo}; +use sip_types::header::typed::{CSeq, CallID, Contact, Expires, FromTo, MinExpires}; use sip_types::uri::{NameAddr, Uri}; use sip_types::{CodeKind, Method, Name}; use std::time::Duration; @@ -80,6 +80,21 @@ impl Registration { } } + pub fn receive_error_response(&mut self, response: TsxResponse) -> bool { + if !matches!(response.line.code.kind(), CodeKind::RequestFailure) { + return false; + } + + let Ok(expires) = response.headers.get_named::() else { + return false; + }; + + self.expires = expires.0; + self.register_interval = create_reg_interval(self.expires); + + true + } + pub async fn wait_for_expiry(&mut self) { self.register_interval.tick().await; }