diff --git a/src/transaction/endpoint.rs b/src/transaction/endpoint.rs index 973c620..733c06a 100644 --- a/src/transaction/endpoint.rs +++ b/src/transaction/endpoint.rs @@ -528,13 +528,19 @@ impl EndpointInner { .first() .ok_or(Error::EndpointError("not sipaddrs".to_string())) .cloned()?; - let mut uri: crate::sip::Uri = first_addr.into(); + Ok(self.get_record_route_with_addr(first_addr)) + } + + /// Record-Route advertising `addr` instead of the endpoint's first listener, for an + /// endpoint with several listeners where the one a peer must use is not the first. + pub fn get_record_route_with_addr(&self, addr: SipAddr) -> crate::sip::typed::RecordRoute { + let mut uri: crate::sip::Uri = addr.into(); uri.params.push(crate::sip::Param::Lr); - Ok(crate::sip::typed::RecordRoute { + crate::sip::typed::RecordRoute { display_name: None, uri, params: vec![], - }) + } } pub fn get_via( diff --git a/src/transaction/tests/test_endpoint.rs b/src/transaction/tests/test_endpoint.rs index 8613e3b..bd07023 100644 --- a/src/transaction/tests/test_endpoint.rs +++ b/src/transaction/tests/test_endpoint.rs @@ -112,3 +112,25 @@ async fn test_endpoint_recvrequests() { } } } + +#[tokio::test] +async fn test_get_record_route_addr_override() { + let endpoint = super::create_test_endpoint(Some("127.0.0.1:15060")) + .await + .expect("create_test_endpoint"); + + let default_rr = endpoint.inner.get_record_route().expect("get_record_route"); + assert_eq!(default_rr.uri.to_string(), "sip:127.0.0.1:15060;lr"); + + let override_addr = crate::transport::SipAddr { + r#type: Some(crate::sip::Transport::Udp), + addr: crate::sip::HostWithPort { + host: crate::sip::Host::IpAddr(std::net::IpAddr::V4(std::net::Ipv4Addr::new( + 127, 0, 0, 1, + ))), + port: Some(15061.into()), + }, + }; + let override_rr = endpoint.inner.get_record_route_with_addr(override_addr); + assert_eq!(override_rr.uri.to_string(), "sip:127.0.0.1:15061;lr"); +}