use crate::sip::{headers::*, prelude::HeadersExt, Method, SipMessage, Version};
use crate::transaction::endpoint::{Endpoint, EndpointOption};
use crate::transaction::key::{TransactionKey, TransactionRole};
use crate::transaction::transaction::Transaction;
use crate::transaction::{EndpointBuilder, TransactionState};
use crate::transport::udp::UdpConnection;
use crate::transport::TransportLayer;
use crate::Result;
use std::net::SocketAddr;
use std::time::{Duration, Instant};
use tokio::net::UdpSocket;
use tokio_util::sync::CancellationToken;
const CALL_ID: &str = "auto-ack-2xx@test";
const FROM_TAG: &str = "autoack-from";
async fn build_endpoint(auto_ack_2xx: bool) -> Result<Endpoint> {
let token = CancellationToken::new();
let tl = TransportLayer::new(token.child_token());
let udp = UdpConnection::create_connection("127.0.0.1:0".parse()?, None, None).await?;
tl.add_transport(udp.into());
let option = EndpointOption {
auto_ack_2xx,
..Default::default()
};
Ok(EndpointBuilder::new()
.with_user_agent("rsipstack-test")
.with_transport_layer(tl)
.with_option(option)
.build())
}
fn make_invite(peer: SocketAddr) -> Result<crate::sip::Request> {
Ok(crate::sip::Request {
method: Method::Invite,
uri: crate::sip::Uri::try_from(format!("sip:bob@{peer}").as_str())?,
headers: vec![
Via::new("SIP/2.0/UDP 127.0.0.1:5060;branch=z9hG4bK-autoack").into(),
CSeq::new("1 INVITE").into(),
From::new(format!("<sip:alice@example.com>;tag={FROM_TAG}")).into(),
To::new("<sip:bob@example.com>").into(),
CallId::new(CALL_ID).into(),
MaxForwards::new("70").into(),
Contact::new("<sip:alice@127.0.0.1:5060>").into(),
ContentLength::new("0").into(),
]
.into(),
version: Version::V2,
body: vec![],
})
}
async fn spawn_peer(socket: UdpSocket) -> tokio::sync::mpsc::Receiver<String> {
let (tx, rx) = tokio::sync::mpsc::channel(32);
tokio::spawn(async move {
let mut buf = vec![0u8; 4096];
let mut ok_text: Option<String> = None;
let mut ok_dest: Option<SocketAddr> = None;
let mut resends_left = 0u8;
let mut next_send_at: Option<Instant> = None;
loop {
if let Some(at) = next_send_at {
if Instant::now() >= at {
if let (Some(text), Some(dest)) = (ok_text.as_ref(), ok_dest) {
socket.send_to(text.as_bytes(), dest).await.ok();
}
resends_left = resends_left.saturating_sub(1);
next_send_at = if resends_left > 0 {
Some(Instant::now() + Duration::from_millis(400))
} else {
None
};
}
}
let wait = next_send_at
.map(|at| at.saturating_duration_since(Instant::now()))
.unwrap_or(Duration::from_secs(5));
let Ok(Ok((len, src))) = tokio::time::timeout(wait, socket.recv_from(&mut buf)).await
else {
continue;
};
let text = String::from_utf8_lossy(&buf[..len]).to_string();
let Ok(msg) = SipMessage::try_from(text.as_str()) else {
continue;
};
match msg {
SipMessage::Request(req) if req.method == Method::Invite => {
ok_dest = Some(src);
let reply = match ok_text.as_ref() {
Some(text) => text.clone(),
None => {
let text = format!(
"SIP/2.0 200 OK\r\n\
Via: {}\r\n\
From: {}\r\n\
To: {};tag=peer-tag\r\n\
Call-ID: {}\r\n\
CSeq: {}\r\n\
Contact: <sip:bob@{}>\r\n\
Content-Length: 0\r\n\r\n",
req.via_header().unwrap().value(),
req.from_header().unwrap().value(),
req.to_header().unwrap().value(),
req.call_id_header().unwrap().value(),
req.cseq_header().unwrap().value(),
socket.local_addr().unwrap(),
);
ok_text = Some(text.clone());
resends_left = 2;
next_send_at = Some(Instant::now() + Duration::from_millis(400));
text
}
};
socket.send_to(reply.as_bytes(), src).await.ok();
}
SipMessage::Request(req) if req.method == Method::Ack => {
tx.send(text).await.ok();
}
_ => {}
}
}
});
rx
}
async fn next_wire_event(
rx: &mut tokio::sync::mpsc::Receiver<String>,
deadline: Duration,
) -> Option<String> {
tokio::time::timeout(deadline, rx.recv()).await.ok()?
}
#[tokio::test]
async fn test_default_auto_acks_2xx_once() -> Result<()> {
let endpoint = build_endpoint(true).await?;
let socket = UdpSocket::bind("127.0.0.1:0").await?;
let peer_addr = socket.local_addr()?;
let mut peer = spawn_peer(socket).await;
let inner = endpoint.inner.clone();
let invite = make_invite(peer_addr)?;
let client = async move {
let key = TransactionKey::from_request(&invite, TransactionRole::Client)?;
let mut tx = Transaction::new_client(key, invite, inner.clone(), None);
tx.send().await?;
let first = tokio::time::timeout(Duration::from_secs(2), tx.receive())
.await
.expect("timeout waiting for the 200")
.expect("transaction ended before the 200");
match first {
SipMessage::Response(ref resp) => {
assert_eq!(resp.status_code.code(), 200);
}
other => panic!("expected the 200 OK, got {other}"),
}
assert_eq!(tx.state, TransactionState::Accepted);
let second = tokio::time::timeout(Duration::from_millis(1200), tx.receive())
.await
.expect("timeout waiting for the retransmitted 200")
.expect("transaction ended before the retransmitted 200");
match second {
SipMessage::Response(ref resp) => {
assert_eq!(resp.status_code.code(), 200);
}
other => panic!("expected the retransmitted 200 OK, got {other}"),
}
assert_eq!(tx.state, TransactionState::Accepted);
Ok::<_, crate::Error>(())
};
let endpoint_inner = endpoint.inner.clone();
let serve = tokio::spawn(async move {
let _ = endpoint_inner.serve().await;
});
tokio::time::timeout(Duration::from_secs(6), client)
.await
.expect("client timed out")
.expect("client failed");
let ack = next_wire_event(&mut peer, Duration::from_secs(2))
.await
.expect("the transaction must ACK the 2xx");
assert!(ack.starts_with("ACK"), "expected an ACK, got {ack}");
assert!(
ack.contains("CSeq: 1 ACK"),
"the ACK must carry the INVITE's CSeq, got {ack}"
);
assert!(
ack.contains(";tag=peer-tag"),
"the ACK must carry the 2xx's To tag, got {ack}"
);
let re_ack = next_wire_event(&mut peer, Duration::from_millis(1100)).await;
let re_ack = re_ack.expect("a retransmitted 200 OK must be re-ACKed");
assert!(
re_ack.contains("CSeq: 1 ACK"),
"the re-ACK must carry the INVITE's CSeq, got {re_ack}"
);
serve.abort();
Ok(())
}
#[tokio::test]
async fn test_auto_ack_2xx_false_never_acks() -> Result<()> {
let endpoint = build_endpoint(false).await?;
let socket = UdpSocket::bind("127.0.0.1:0").await?;
let peer_addr = socket.local_addr()?;
let mut peer = spawn_peer(socket).await;
let inner = endpoint.inner.clone();
let invite = make_invite(peer_addr)?;
let client = async move {
let key = TransactionKey::from_request(&invite, TransactionRole::Client)?;
let mut tx = Transaction::new_client(key, invite, inner.clone(), None);
tx.send().await?;
let first = tokio::time::timeout(Duration::from_secs(2), tx.receive())
.await
.expect("timeout waiting for the 200")
.expect("transaction ended before the 200");
match first {
SipMessage::Response(ref resp) => {
assert_eq!(resp.status_code.code(), 200);
}
other => panic!("expected the 200 OK, got {other}"),
}
assert_eq!(tx.state, TransactionState::Terminated);
Ok::<_, crate::Error>(())
};
let endpoint_inner = endpoint.inner.clone();
let serve = tokio::spawn(async move {
let _ = endpoint_inner.serve().await;
});
tokio::time::timeout(Duration::from_secs(6), client)
.await
.expect("client timed out")
.expect("client failed");
let first_ack = next_wire_event(&mut peer, Duration::from_millis(1200)).await;
assert!(
first_ack.is_none(),
"proxy mode must not ACK the 2xx, got {first_ack:?}"
);
let retransmit_ack = next_wire_event(&mut peer, Duration::from_millis(1200)).await;
assert!(
retransmit_ack.is_none(),
"proxy mode must not ACK a retransmitted 2xx, got {retransmit_ack:?}"
);
serve.abort();
Ok(())
}