sipx-transport 1.0.0-rc.2

Async SIP transports: UDP, TCP, TLS, WebSocket, experimental QUIC, and RFC 3263 resolution
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Experimental SIP-over-QUIC mapping from `docs/specs/sip-quic.md`.
//!
//! QUIC is connection-oriented for transaction timers but message-oriented for framing: each
//! request gets one bidirectional stream, and responses return on that same stream. Certificate
//! policy is built in [`crate::tls`] and converted here; this module never installs a verifier.

use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use std::time::Duration;

use bytes::{Bytes, BytesMut};
use quinn::{Connection, Endpoint, RecvStream, SendStream};
use sipx_sip::{Limits, Message, parse_datagram};
use tokio::sync::mpsc;
use tokio::task::JoinSet;

use crate::error::Result;
use crate::target::{ConnectionKey, TransportKind};
use crate::tcp::Event;

/// What failed while establishing or using a QUIC connection.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum QuicError {
    /// The certificate did not cover the URI host.
    #[error("certificate for {peer} has the wrong host: {detail}")]
    WrongHost {
        /// Peer being authenticated.
        peer: String,
        /// Verification detail without certificate material.
        detail: String,
    },
    /// No configured trust anchor vouched for the certificate.
    #[error("certificate for {peer} has an unknown issuer: {detail}")]
    UnknownIssuer {
        /// Peer being authenticated.
        peer: String,
        /// Verification detail without certificate material.
        detail: String,
    },
    /// The peer did not negotiate the required `sip/2` application protocol.
    #[error("QUIC peer {peer} negotiated the wrong ALPN: {detail}")]
    WrongAlpn {
        /// Peer being authenticated.
        peer: String,
        /// Negotiation detail.
        detail: String,
    },
    /// The authenticated connection closed.
    #[error("QUIC connection to {peer} closed: {detail}")]
    ConnectionClosed {
        /// Peer whose connection closed.
        peer: String,
        /// Close code and reason.
        detail: String,
    },
    /// Another handshake failure.
    #[error("QUIC handshake with {peer}: {detail}")]
    Handshake {
        /// Peer being authenticated.
        peer: String,
        /// Backend detail.
        detail: String,
    },
}

impl QuicError {
    pub(crate) fn handshake(peer: String, detail: String) -> Self {
        let folded = detail.to_ascii_lowercase();
        if folded.contains("notvalidforname") || folded.contains("not valid for") {
            Self::WrongHost { peer, detail }
        } else if folded.contains("unknownissuer") || folded.contains("unknown issuer") {
            Self::UnknownIssuer { peer, detail }
        } else if folded.contains("application protocol")
            || folded.contains("known protocol")
            || folded.contains("alpn")
        {
            Self::WrongAlpn { peer, detail }
        } else {
            Self::Handshake { peer, detail }
        }
    }
}

/// A route back to the send half of the exact stream carrying an inbound request.
pub(crate) type Reply = mpsc::Sender<Bytes>;

const KEEPALIVE: Duration = Duration::from_secs(25);

pub(crate) fn transport_config() -> Arc<quinn::TransportConfig> {
    let mut transport = quinn::TransportConfig::default();
    transport.keep_alive_interval(Some(KEEPALIVE));
    transport.max_concurrent_bidi_streams(64_u8.into());
    transport.max_concurrent_uni_streams(0_u8.into());
    Arc::new(transport)
}

/// Bind the UDP socket Quinn owns, optionally accepting incoming connections.
pub(crate) fn endpoint(
    ip: IpAddr,
    port: u16,
    client: Option<&crate::tls::ClientTls>,
    server: Option<&crate::tls::ServerTls>,
) -> Result<Endpoint> {
    let bind = SocketAddr::new(ip, port);
    let mut endpoint = match server {
        Some(server) => {
            let mut config = server.quic_config()?;
            config.transport_config(transport_config());
            Endpoint::server(config, bind)?
        }
        None => Endpoint::client(bind)?,
    };
    if let Some(client) = client {
        let mut config = client.quic_config()?;
        config.transport_config(transport_config());
        endpoint.set_default_client_config(config);
    }
    Ok(endpoint)
}

/// Drive all streams on one authenticated QUIC connection.
pub(crate) async fn pump(
    connection: Connection,
    key: ConnectionKey,
    id: u64,
    mut outgoing: mpsc::Receiver<Bytes>,
    events: mpsc::Sender<Event>,
    limits: Limits,
) {
    let mut streams = JoinSet::new();
    let detail = loop {
        tokio::select! {
            incoming = connection.accept_bi() => match incoming {
                Ok((send, recv)) => {
                    let events = events.clone();
                    let key = key.clone();
                    let connection = connection.clone();
                    streams.spawn(async move {
                        receive_request(connection, send, recv, key, id, events, limits).await;
                    });
                }
                Err(error) => break error.to_string(),
            },
            Some(bytes) = outgoing.recv() => {
                let connection = connection.clone();
                let events = events.clone();
                let key = key.clone();
                streams.spawn(async move {
                    send_request(connection, bytes, key, id, events, limits).await;
                });
            }
            completed = streams.join_next(), if !streams.is_empty() => {
                if let Some(Err(error)) = completed {
                    tracing::debug!(%error, peer = %key.peer, "QUIC stream task failed");
                }
            }
            error = connection.closed() => break error.to_string(),
            else => break "connection task ended".to_owned(),
        }
    };
    streams.abort_all();
    while streams.join_next().await.is_some() {}
    // discard: a closed event channel means the endpoint already stopped and owns no transactions.
    let _ = events.send(Event::QuicClosed { key, id, detail }).await;
}

async fn receive_request(
    connection: Connection,
    send: SendStream,
    mut recv: RecvStream,
    key: ConnectionKey,
    id: u64,
    events: mpsc::Sender<Event>,
    limits: Limits,
) {
    let peer = key.peer;
    let bytes = match recv.read_to_end(limits.max_message_bytes).await {
        Ok(bytes) => Bytes::from(bytes),
        Err(quinn::ReadToEndError::Read(quinn::ReadError::ConnectionLost(error))) => {
            tracing::debug!(%error, %peer, "QUIC connection closed while reading a request");
            return;
        }
        Err(error) => {
            tracing::debug!(%error, %peer, "QUIC request stream could not be read");
            // discard: if the driver stopped, no counter or transaction remains to receive this.
            let _ = events.send(Event::FramingFailed { key }).await;
            connection.close(1_u8.into(), b"malformed SIP stream");
            return;
        }
    };
    let message = match parse_one(bytes, &limits) {
        Ok(message @ Message::Request(_)) => message,
        Ok(Message::Response(_)) => {
            tracing::debug!(%peer, "QUIC peer opened a stream with a response");
            // discard: if the driver stopped, no counter or transaction remains to receive this.
            let _ = events.send(Event::FramingFailed { key }).await;
            connection.close(1_u8.into(), b"response opened a request stream");
            return;
        }
        Err(error) => {
            // discard: the malformed stream itself; closing the connection is the mandated result.
            tracing::debug!(%error, %peer, "malformed QUIC request stream");
            // discard: if the driver stopped, no counter or transaction remains to receive this.
            let _ = events.send(Event::FramingFailed { key }).await;
            connection.close(1_u8.into(), b"malformed SIP stream");
            return;
        }
    };
    let (reply, replies) = mpsc::channel(16);
    if events
        .send(Event::Message {
            message: Box::new(message),
            source: peer,
            transport: TransportKind::Quic,
            id,
            quic_reply: Some(reply),
        })
        .await
        .is_ok()
    {
        write_responses(send, replies).await;
    }
}

async fn write_responses(mut send: SendStream, mut replies: mpsc::Receiver<Bytes>) {
    while let Some(bytes) = replies.recv().await {
        let final_response = parse_datagram(bytes.clone(), &Limits::stream())
            .ok()
            .and_then(|message| match message {
                Message::Response(response) => Some(response.status.is_final()),
                Message::Request(_) => None,
            })
            .unwrap_or(true);
        if send.write_all(&bytes).await.is_err() {
            return;
        }
        if final_response {
            // discard: the response bytes were written; a finish error means the peer closed first.
            let _ = send.finish();
            return;
        }
    }
    // discard: no response sender remains; finishing is best-effort because the peer may be gone.
    let _ = send.finish();
}

async fn send_request(
    connection: Connection,
    bytes: Bytes,
    key: ConnectionKey,
    id: u64,
    events: mpsc::Sender<Event>,
    limits: Limits,
) {
    let peer = key.peer;
    let Some(mut recv) = write_request(&connection, &events, &key, bytes).await else {
        return;
    };

    let mut pending = BytesMut::new();
    let mut final_response = None;
    loop {
        let chunk = match recv.read_chunk(usize::MAX, true).await {
            Ok(Some(chunk)) => chunk.bytes,
            Ok(None) => break,
            Err(quinn::ReadError::ConnectionLost(error)) => {
                tracing::debug!(%error, %peer, "QUIC connection closed while reading a response");
                return;
            }
            Err(error) => {
                reject_response_stream(&connection, &events, &key, peer, error.to_string()).await;
                return;
            }
        };
        if final_response.is_some() {
            reject_response_stream(
                &connection,
                &events,
                &key,
                peer,
                "bytes followed the final response".to_owned(),
            )
            .await;
            return;
        }
        pending.extend_from_slice(&chunk);
        loop {
            let message = match take_response(&mut pending, false, &limits) {
                Ok(Some(message)) => message,
                Ok(None) => break,
                Err(error) => {
                    reject_response_stream(&connection, &events, &key, peer, error).await;
                    return;
                }
            };
            let is_final = match &message {
                Message::Response(response) => response.status.is_final(),
                Message::Request(_) => {
                    reject_response_stream(
                        &connection,
                        &events,
                        &key,
                        peer,
                        "request on response stream".to_owned(),
                    )
                    .await;
                    return;
                }
            };
            if is_final {
                final_response = Some(message);
                if !pending.is_empty() {
                    reject_response_stream(
                        &connection,
                        &events,
                        &key,
                        peer,
                        "bytes followed the final response".to_owned(),
                    )
                    .await;
                    return;
                }
                break;
            }
            if send_response_event(&events, message, peer, id)
                .await
                .is_err()
            {
                return;
            }
        }
    }

    let message = match complete_response(final_response, &mut pending, &limits) {
        Ok(message) => message,
        Err(error) => {
            reject_response_stream(&connection, &events, &key, peer, error).await;
            return;
        }
    };
    // discard: if the driver stopped, no transaction remains to receive this response.
    let _ = send_response_event(&events, message, peer, id).await;
}

async fn write_request(
    connection: &Connection,
    events: &mpsc::Sender<Event>,
    key: &ConnectionKey,
    bytes: Bytes,
) -> Option<RecvStream> {
    let peer = key.peer;
    let (mut send, recv) = match connection.open_bi().await {
        Ok(stream) => stream,
        Err(error) => {
            tracing::debug!(%error, %peer, "QUIC stream could not be opened");
            return None;
        }
    };
    if let Err(error) = send.write_all(&bytes).await {
        match error {
            quinn::WriteError::ConnectionLost(error) => {
                tracing::debug!(%error, %peer, "QUIC connection closed while writing a request");
            }
            other => {
                reject_response_stream(connection, events, key, peer, other.to_string()).await;
            }
        }
        return None;
    }
    if let Err(error) = send.finish() {
        tracing::debug!(%error, %peer, "QUIC request stream closed before it could finish");
        return None;
    }
    Some(recv)
}

fn complete_response(
    final_response: Option<Message>,
    pending: &mut BytesMut,
    limits: &Limits,
) -> std::result::Result<Message, String> {
    if let Some(message) = final_response {
        return pending
            .is_empty()
            .then_some(message)
            .ok_or_else(|| "bytes followed the final response".to_owned());
    }
    match take_response(pending, true, limits)? {
        Some(message)
            if matches!(&message, Message::Response(response) if response.status.is_final())
                && pending.is_empty() =>
        {
            Ok(message)
        }
        Some(_) | None => Err("response stream ended without one final response".to_owned()),
    }
}

async fn send_response_event(
    events: &mpsc::Sender<Event>,
    message: Message,
    peer: SocketAddr,
    id: u64,
) -> std::result::Result<(), mpsc::error::SendError<Event>> {
    events
        .send(Event::Message {
            message: Box::new(message),
            source: peer,
            transport: TransportKind::Quic,
            id,
            quic_reply: None,
        })
        .await
}

async fn reject_response_stream(
    connection: &Connection,
    events: &mpsc::Sender<Event>,
    key: &ConnectionKey,
    peer: SocketAddr,
    detail: String,
) {
    // discard: a malformed peer is closed deliberately, and if the driver has stopped no counter
    // or transaction remains to receive the framing-failure event.
    tracing::debug!(%detail, %peer, "malformed QUIC response stream");
    let _ = events.send(Event::FramingFailed { key: key.clone() }).await;
    connection.close(1_u8.into(), b"malformed SIP stream");
}

/// Remove one response whose boundary is knowable now.
///
/// `Content-Length` makes provisional responses deliverable before FIN. Without it, FIN is the
/// mapping's body delimiter, so the response deliberately remains buffered until `end`.
fn take_response(
    pending: &mut BytesMut,
    end: bool,
    limits: &Limits,
) -> std::result::Result<Option<Message>, String> {
    let Some(head_end) = pending.windows(4).position(|window| window == b"\r\n\r\n") else {
        if pending.len() > limits.max_message_bytes || end && !pending.is_empty() {
            return Err("response has no complete header section".to_owned());
        }
        return Ok(None);
    };
    let body_start = head_end
        .checked_add(4)
        .ok_or_else(|| "response length overflow".to_owned())?;
    let header = pending
        .get(..head_end)
        .ok_or_else(|| "response header length overflow".to_owned())?;
    let frame_len = match declared_content_length(header)? {
        Some(declared) => body_start
            .checked_add(declared)
            .ok_or_else(|| "response length overflow".to_owned())?,
        None if end => pending.len(),
        None => {
            if pending.len() > limits.max_message_bytes {
                return Err("response exceeds the configured message limit".to_owned());
            }
            return Ok(None);
        }
    };
    if frame_len > limits.max_message_bytes {
        return Err("response exceeds the configured message limit".to_owned());
    }
    if pending.len() < frame_len {
        if end {
            return Err("response body ended before Content-Length".to_owned());
        }
        return Ok(None);
    }
    parse_one(pending.split_to(frame_len).freeze(), limits).map(Some)
}

fn declared_content_length(head: &[u8]) -> std::result::Result<Option<usize>, String> {
    let mut lines = head.split(|byte| *byte == b'\n');
    let _start_line = lines.next();
    let mut value: Option<Vec<u8>> = None;
    let mut continuing_length = false;
    for raw_line in lines {
        let line = raw_line.strip_suffix(b"\r").unwrap_or(raw_line);
        if line.first().is_some_and(u8::is_ascii_whitespace) {
            if continuing_length {
                let stored = value
                    .as_mut()
                    .ok_or_else(|| "missing Content-Length value".to_owned())?;
                stored.push(b' ');
                stored.extend_from_slice(trim_ascii(line));
            }
            continue;
        }
        continuing_length = false;
        let mut parts = line.splitn(2, |byte| *byte == b':');
        let name = parts.next().unwrap_or_default();
        let Some(raw_value) = parts.next() else {
            continue;
        };
        let name = trim_ascii(name);
        if name.eq_ignore_ascii_case(b"content-length") || name.eq_ignore_ascii_case(b"l") {
            if value.is_some() {
                return Err("repeated Content-Length".to_owned());
            }
            value = Some(trim_ascii(raw_value).to_vec());
            continuing_length = true;
        }
    }
    value
        .map(|value| {
            std::str::from_utf8(&value)
                .map_err(|error| error.to_string())?
                .parse::<usize>()
                .map_err(|error| error.to_string())
        })
        .transpose()
}

fn trim_ascii(mut bytes: &[u8]) -> &[u8] {
    while bytes.first().is_some_and(u8::is_ascii_whitespace) {
        bytes = bytes.get(1..).unwrap_or_default();
    }
    while bytes.last().is_some_and(u8::is_ascii_whitespace) {
        bytes = bytes
            .get(..bytes.len().saturating_sub(1))
            .unwrap_or_default();
    }
    bytes
}

/// Parse the stream's one message and reject a declared length that does not consume it exactly.
#[allow(
    clippy::needless_pass_by_value,
    reason = "the parsed message retains views into this owned Bytes allocation"
)]
fn parse_one(bytes: Bytes, limits: &Limits) -> std::result::Result<Message, String> {
    let head_end = bytes
        .windows(4)
        .position(|window| window == b"\r\n\r\n")
        .ok_or_else(|| "no header terminator".to_owned())?;
    let message = parse_datagram(bytes.clone(), limits).map_err(|error| error.to_string())?;
    if let Some(value) = message
        .headers()
        .value(&sipx_sip::HeaderName::ContentLength)
    {
        let declared = std::str::from_utf8(&value)
            .map_err(|error| error.to_string())?
            .trim()
            .parse::<usize>()
            .map_err(|error| error.to_string())?;
        let actual = bytes.len().saturating_sub(head_end + 4);
        if declared != actual {
            return Err(format!(
                "Content-Length {declared} does not match {actual} stream bytes"
            ));
        }
    }
    Ok(message)
}