rsipstack 0.7.2

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
//! A 2xx that answers the INVITE after we sent CANCEL (the callee picked up
//! while the CANCEL was in flight) establishes a dialog at the far end. The
//! CANCEL has no effect on it, so the UAC must ACK the 2xx and then end the
//! session with a BYE (RFC 3261 §9.1, §15; RFC 5407 §3.1.2).
//!
//! These tests drop the `do_invite` future after a 180 — the documented way to
//! abandon an outgoing call, which cancels it — and answer the INVITE with a
//! 200 from a raw UDP peer in each wire ordering.
use crate::dialog::{
    dialog::{DialogState, DialogStateReceiver, TerminatedReason},
    dialog_layer::DialogLayer,
    invitation::InviteOption,
};
use crate::sip::{prelude::HeadersExt, Method, Request, SipMessage, Uri};
use crate::transport::{udp::UdpConnection, TransportLayer};
use crate::EndpointBuilder;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tokio::net::UdpSocket;
use tokio::sync::mpsc::unbounded_channel;
use tokio_util::sync::CancellationToken;

const PEER_TAG: &str = "peer-tag";

/// Receive the next request with `method` on the raw peer socket within
/// `wait`, skipping anything else.
async fn recv_request(socket: &UdpSocket, method: Method, wait: Duration) -> (Request, SocketAddr) {
    let mut buf = vec![0u8; 4096];
    let deadline = tokio::time::Instant::now() + wait;
    loop {
        let (len, from) = tokio::time::timeout_at(deadline, socket.recv_from(&mut buf))
            .await
            .unwrap_or_else(|_| panic!("timeout waiting for {method}"))
            .expect("recv_from failed");
        let text = std::str::from_utf8(&buf[..len]).expect("non utf-8 SIP message");
        if let Ok(SipMessage::Request(req)) = SipMessage::try_from(text) {
            if req.method == method {
                return (req, from);
            }
        }
    }
}

/// Fail if the peer receives a BYE within `wait`.
async fn assert_no_bye(socket: &UdpSocket, wait: Duration, what: &str) {
    let mut buf = vec![0u8; 4096];
    let deadline = tokio::time::Instant::now() + wait;
    while let Ok(Ok((len, _))) = tokio::time::timeout_at(deadline, socket.recv_from(&mut buf)).await
    {
        let text = std::str::from_utf8(&buf[..len]).unwrap();
        if let Ok(SipMessage::Request(req)) = SipMessage::try_from(text) {
            assert_ne!(req.method, Method::Bye, "{what}");
        }
    }
}

/// Build a response to `req` as the peer UA, adding the peer's To tag.
fn response(req: &Request, code: u16, reason: &str, contact: &str) -> String {
    let to = req.to_header().unwrap().value().to_string();
    let to = if to.contains(";tag=") {
        to
    } else {
        format!("{to};tag={PEER_TAG}")
    };
    format!(
        "SIP/2.0 {code} {reason}\r\n\
         Via: {}\r\n\
         From: {}\r\n\
         To: {to}\r\n\
         Call-ID: {}\r\n\
         CSeq: {}\r\n\
         Contact: <{contact}>\r\n\
         Content-Length: 0\r\n\r\n",
        req.via_header().unwrap().value(),
        req.from_header().unwrap().value(),
        req.call_id_header().unwrap().value(),
        req.cseq_header().unwrap().value(),
    )
}

async fn reply(socket: &UdpSocket, to: SocketAddr, req: &Request, code: u16, reason: &str) {
    let contact = format!("sip:bob@{}", socket.local_addr().unwrap());
    socket
        .send_to(response(req, code, reason, &contact).as_bytes(), to)
        .await
        .expect("send_to failed");
}

struct Uac {
    dialog_layer: Arc<DialogLayer>,
    option: InviteOption,
    peer: UdpSocket,
}

async fn setup(token: &CancellationToken) -> crate::Result<Uac> {
    let peer = UdpSocket::bind("127.0.0.1:0").await?;
    let peer_port = peer.local_addr()?.port();

    let transport_layer = TransportLayer::new(token.child_token());
    let udp = UdpConnection::create_connection(
        "127.0.0.1:0".parse().unwrap(),
        None,
        Some(token.child_token()),
    )
    .await?;
    let uac_addr = udp.get_addr().addr.clone();
    transport_layer.add_transport(udp.into());
    let endpoint = EndpointBuilder::new()
        .with_user_agent("rsipstack-test")
        .with_transport_layer(transport_layer)
        .with_cancel_token(token.child_token())
        .build();
    let endpoint_inner = endpoint.inner.clone();
    tokio::spawn(async move {
        let _ = endpoint_inner.serve().await;
    });
    let dialog_layer = Arc::new(DialogLayer::new(endpoint.inner.clone()));
    let option = InviteOption {
        caller: Uri::try_from("sip:alice@example.com")?,
        callee: Uri::try_from(format!("sip:bob@127.0.0.1:{peer_port};transport=udp").as_str())?,
        contact: Uri::try_from(format!("sip:alice@{uac_addr}").as_str())?,
        ..Default::default()
    };
    Ok(Uac {
        dialog_layer,
        option,
        peer,
    })
}

async fn wait_for_state(
    rx: &mut DialogStateReceiver,
    what: &str,
    wait: Duration,
    pred: impl Fn(&DialogState) -> bool,
) -> DialogState {
    tokio::time::timeout(wait, async {
        loop {
            let state = rx.recv().await.expect("state channel closed");
            if pred(&state) {
                return state;
            }
        }
    })
    .await
    .unwrap_or_else(|_| panic!("timeout waiting for {what}"))
}

/// The ACK and the BYE must belong to the dialog the 2xx established:
/// same Call-ID, our From tag, and the peer's To tag.
fn assert_in_dialog(msg: &Request, invite: &Request, what: &str) {
    assert_eq!(
        msg.call_id_header().unwrap().value(),
        invite.call_id_header().unwrap().value(),
        "{what} must carry the INVITE's Call-ID"
    );
    assert_eq!(
        msg.from_header().unwrap().tag().unwrap(),
        invite.from_header().unwrap().tag().unwrap(),
        "{what} must carry our From tag"
    );
    assert_eq!(
        msg.to_header()
            .unwrap()
            .tag()
            .unwrap()
            .map(|t| t.value().to_string()),
        Some(PEER_TAG.to_string()),
        "{what} must carry the 2xx's To tag"
    );
}

#[derive(Clone, Copy, Debug)]
enum Order {
    /// 200 to the INVITE, then 200 to the CANCEL.
    InviteOkFirst,
    /// 200 to the CANCEL, then 200 to the INVITE right away.
    CancelOkFirst,
    /// 200 to the CANCEL, then 200 to the INVITE after the 2 s settle window.
    InviteOkLate,
}

async fn run_crossing_2xx(order: Order) -> crate::Result<()> {
    let token = CancellationToken::new();
    let Uac {
        dialog_layer,
        option,
        peer,
    } = setup(&token).await?;
    let wait = Duration::from_secs(2);

    let (state_sender, mut states) = unbounded_channel();
    let invite = tokio::spawn(async move { dialog_layer.do_invite(option, state_sender).await });

    let (inv, uac) = recv_request(&peer, Method::Invite, wait).await;
    reply(&peer, uac, &inv, 180, "Ringing").await;
    wait_for_state(&mut states, "Early", wait, |s| {
        matches!(s, DialogState::Early(_, _))
    })
    .await;

    // The application abandons the call: dropping the `do_invite` future
    // cancels the INVITE.
    invite.abort();
    let _ = invite.await;
    let (cancel, _) = recv_request(&peer, Method::Cancel, wait).await;

    let mut terminated = None;
    match order {
        Order::InviteOkFirst => {
            reply(&peer, uac, &inv, 200, "OK").await;
            reply(&peer, uac, &cancel, 200, "OK").await;
        }
        Order::CancelOkFirst => {
            reply(&peer, uac, &cancel, 200, "OK").await;
            reply(&peer, uac, &inv, 200, "OK").await;
        }
        Order::InviteOkLate => {
            reply(&peer, uac, &cancel, 200, "OK").await;
            // The dialog reports Terminated(UacCancel) once the 2 s settle
            // window passes without a final response.
            terminated = Some(
                wait_for_state(&mut states, "Terminated", Duration::from_secs(4), |s| {
                    matches!(s, DialogState::Terminated(_, _))
                })
                .await,
            );
            tokio::time::sleep(Duration::from_millis(2500)).await;
            reply(&peer, uac, &inv, 200, "OK").await;
        }
    }

    let (ack, _) = recv_request(&peer, Method::Ack, wait).await;
    assert_in_dialog(&ack, &inv, "ACK");
    assert_eq!(
        ack.cseq_header().unwrap().seq().unwrap(),
        inv.cseq_header().unwrap().seq().unwrap(),
        "the ACK must acknowledge the INVITE"
    );

    let (bye, _) = recv_request(&peer, Method::Bye, wait).await;
    assert_in_dialog(&bye, &inv, "BYE");
    reply(&peer, uac, &bye, 200, "OK").await;

    // A retransmitted 2xx is ACKed again but starts no second BYE.
    reply(&peer, uac, &inv, 200, "OK").await;
    let (ack, _) = recv_request(&peer, Method::Ack, wait).await;
    assert_in_dialog(&ack, &inv, "ACK of the retransmitted 2xx");
    assert_no_bye(
        &peer,
        Duration::from_millis(500),
        "one BYE per abandoned call",
    )
    .await;

    // The abandoned call reports Terminated(UacCancel) once and is never
    // reported as Confirmed.
    tokio::time::sleep(Duration::from_millis(200)).await;
    let mut seen = Vec::new();
    while let Ok(state) = states.try_recv() {
        seen.push(state);
    }
    if let Some(t) = terminated {
        seen.insert(0, t);
    }
    assert!(
        !seen
            .iter()
            .any(|s| matches!(s, DialogState::Confirmed(_, _))),
        "an abandoned call must not report Confirmed, got {seen:?}"
    );
    let terminations: Vec<_> = seen
        .iter()
        .filter(|s| matches!(s, DialogState::Terminated(_, _)))
        .collect();
    assert!(
        matches!(
            terminations.as_slice(),
            [DialogState::Terminated(_, TerminatedReason::UacCancel)]
        ),
        "expected exactly one Terminated(UacCancel), got {seen:?}"
    );
    assert!(
        bye.cseq_header().unwrap().seq().unwrap() > inv.cseq_header().unwrap().seq().unwrap(),
        "the BYE must use a new CSeq"
    );
    assert_eq!(
        bye.uri.to_string(),
        format!("sip:bob@{}", peer.local_addr()?),
        "the BYE must target the 2xx's Contact"
    );
    token.cancel();
    Ok(())
}

#[tokio::test]
async fn test_2xx_before_cancel_response_is_acked_and_byed() -> crate::Result<()> {
    run_crossing_2xx(Order::InviteOkFirst).await
}

#[tokio::test]
async fn test_2xx_after_cancel_response_is_acked_and_byed() -> crate::Result<()> {
    run_crossing_2xx(Order::CancelOkFirst).await
}

#[tokio::test]
async fn test_2xx_after_cancel_settle_window_is_acked_and_byed() -> crate::Result<()> {
    run_crossing_2xx(Order::InviteOkLate).await
}

/// A CANCEL that wins the race (487 to the INVITE) ends the call as before:
/// the 487 is ACKed and no BYE is sent.
#[tokio::test]
async fn test_cancel_answered_487_sends_no_bye() -> crate::Result<()> {
    let token = CancellationToken::new();
    let Uac {
        dialog_layer,
        option,
        peer,
    } = setup(&token).await?;
    let wait = Duration::from_secs(2);

    let (state_sender, mut states) = unbounded_channel();
    let invite = tokio::spawn(async move { dialog_layer.do_invite(option, state_sender).await });
    let (inv, uac) = recv_request(&peer, Method::Invite, wait).await;
    reply(&peer, uac, &inv, 180, "Ringing").await;
    wait_for_state(&mut states, "Early", wait, |s| {
        matches!(s, DialogState::Early(_, _))
    })
    .await;
    invite.abort();
    let _ = invite.await;
    let (cancel, _) = recv_request(&peer, Method::Cancel, wait).await;
    reply(&peer, uac, &cancel, 200, "OK").await;
    reply(&peer, uac, &inv, 487, "Request Terminated").await;
    let (ack, _) = recv_request(&peer, Method::Ack, wait).await;
    assert_in_dialog(&ack, &inv, "ACK");
    assert_eq!(
        ack.cseq_header().unwrap().seq().unwrap(),
        inv.cseq_header().unwrap().seq().unwrap(),
        "the ACK must acknowledge the INVITE"
    );

    assert_no_bye(
        &peer,
        Duration::from_millis(1500),
        "a cancelled call must not be BYE'd",
    )
    .await;
    token.cancel();
    Ok(())
}

/// Dropped before any response: Terminated(UacCancel) is reported at once and
/// nothing is sent while no provisional has arrived (RFC 3261 §9.1). The first
/// response then gets a CANCEL (180), or an ACK and a BYE (200).
async fn run_dropped_before_provisional(first: u16) -> crate::Result<()> {
    let token = CancellationToken::new();
    let Uac {
        dialog_layer,
        option,
        peer,
    } = setup(&token).await?;
    let wait = Duration::from_secs(2);

    let (state_sender, mut states) = unbounded_channel();
    let invite = tokio::spawn(async move { dialog_layer.do_invite(option, state_sender).await });
    let (inv, uac) = recv_request(&peer, Method::Invite, wait).await;
    invite.abort();
    let _ = invite.await;
    let terminated = wait_for_state(&mut states, "Terminated", Duration::from_millis(200), |s| {
        matches!(s, DialogState::Terminated(_, _))
    })
    .await;
    assert!(matches!(
        terminated,
        DialogState::Terminated(_, TerminatedReason::UacCancel)
    ));
    // Over twice T1: not even an INVITE retransmission.
    let mut buf = vec![0u8; 4096];
    let silent = tokio::time::timeout(Duration::from_millis(1100), peer.recv_from(&mut buf)).await;
    assert!(silent.is_err(), "nothing may be sent before a provisional");

    match first {
        180 => {
            reply(&peer, uac, &inv, 180, "Ringing").await;
            let (cancel, _) = recv_request(&peer, Method::Cancel, wait).await;
            assert_eq!(cancel.cseq_header()?.seq()?, inv.cseq_header()?.seq()?);
            reply(&peer, uac, &cancel, 200, "OK").await;
            reply(&peer, uac, &inv, 487, "Request Terminated").await;
        }
        200 => reply(&peer, uac, &inv, 200, "OK").await,
        _ => reply(&peer, uac, &inv, first, "Busy Here").await,
    }
    let (ack, _) = recv_request(&peer, Method::Ack, wait).await;
    assert_in_dialog(&ack, &inv, "ACK");
    if first == 200 {
        let (bye, _) = recv_request(&peer, Method::Bye, wait).await;
        assert_in_dialog(&bye, &inv, "BYE");
        reply(&peer, uac, &bye, 200, "OK").await;
    } else {
        assert_no_bye(
            &peer,
            Duration::from_millis(500),
            "a call that was not answered",
        )
        .await;
    }
    assert!(
        !std::iter::from_fn(|| states.try_recv().ok())
            .any(|s| matches!(s, DialogState::Terminated(..) | DialogState::Confirmed(..))),
        "no second Terminated and no Confirmed"
    );
    token.cancel();
    Ok(())
}

#[tokio::test]
async fn test_dropped_before_provisional_is_cancelled_after_the_180() -> crate::Result<()> {
    run_dropped_before_provisional(180).await
}

#[tokio::test]
async fn test_dropped_before_provisional_2xx_is_acked_and_byed() -> crate::Result<()> {
    run_dropped_before_provisional(200).await
}

#[tokio::test]
async fn test_dropped_before_provisional_final_failure_is_acked_only() -> crate::Result<()> {
    run_dropped_before_provisional(486).await
}