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
//! Bounded endpoint observation and the two narrow policy seams.

use std::net::IpAddr;
use std::sync::{Arc, Mutex};

use sipx_sip::{Header, Message, Request};
use tokio::sync::mpsc;

use crate::counters::Meters;
use crate::{ConnectionKey, Target, TransportKind};

/// One exact IP network used by live source admission.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SourcePrefix {
    network: IpAddr,
    bits: u8,
}

impl SourcePrefix {
    /// Build a prefix, rejecting a width outside its address family.
    #[must_use]
    pub fn new(network: IpAddr, bits: u8) -> Option<Self> {
        let max = if network.is_ipv4() { 32 } else { 128 };
        (bits <= max).then_some(Self { network, bits })
    }

    /// One address, expressed as its narrowest prefix.
    #[must_use]
    pub const fn address(address: IpAddr) -> Self {
        let bits = if address.is_ipv4() { 32 } else { 128 };
        Self {
            network: address,
            bits,
        }
    }

    /// Whether `candidate` belongs to this network.
    #[must_use]
    pub fn contains(self, candidate: IpAddr) -> bool {
        match (self.network, candidate) {
            (IpAddr::V4(network), IpAddr::V4(candidate)) => prefix_matches(
                u32::from(network).into(),
                u32::from(candidate).into(),
                self.bits,
                32,
            ),
            (IpAddr::V6(network), IpAddr::V6(candidate)) => {
                prefix_matches(u128::from(network), u128::from(candidate), self.bits, 128)
            }
            _ => false,
        }
    }
}

fn prefix_matches(network: u128, candidate: u128, bits: u8, width: u8) -> bool {
    if bits == 0 {
        return true;
    }
    let shift = u32::from(width.saturating_sub(bits));
    (network >> shift) == (candidate >> shift)
}

#[derive(Debug, Clone)]
struct AdmissionGeneration {
    number: u64,
    prefixes: Option<Arc<[SourcePrefix]>>,
}

/// Atomic publication point for source-admission generations.
#[derive(Debug)]
pub(crate) struct SourceAdmission {
    current: Mutex<AdmissionGeneration>,
    limit: usize,
}

impl Default for SourceAdmission {
    fn default() -> Self {
        Self::new(1024)
    }
}

impl SourceAdmission {
    pub(crate) fn new(limit: usize) -> Self {
        Self {
            current: Mutex::new(AdmissionGeneration {
                number: 0,
                prefixes: None,
            }),
            limit,
        }
    }
    /// Admit an address and return the immutable generation that made the decision.
    pub(crate) fn admit(&self, address: IpAddr) -> Option<u64> {
        let current = self
            .current
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let allowed = current
            .prefixes
            .as_ref()
            .is_none_or(|prefixes| prefixes.iter().any(|prefix| prefix.contains(address)));
        allowed.then_some(current.number)
    }

    pub(crate) fn replace(&self, prefixes: Vec<SourcePrefix>) -> crate::Result<u64> {
        if prefixes.len() > self.limit {
            return Err(crate::Error::SourceAdmissionCapacity {
                max: self.limit,
                attempted: prefixes.len(),
            });
        }
        Ok(self.publish(Some(prefixes.into())))
    }

    pub(crate) fn clear(&self) -> u64 {
        self.publish(None)
    }

    fn publish(&self, prefixes: Option<Arc<[SourcePrefix]>>) -> u64 {
        let mut current = self
            .current
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        current.number = current.number.wrapping_add(1).max(1);
        current.prefixes = prefixes;
        current.number
    }
}

/// What a pre-transaction request policy decided.
#[derive(Debug)]
pub enum RequestPolicyDecision {
    /// Continue without adding fields.
    Allow,
    /// Refuse the send before a transaction exists.
    Reject(String),
    /// Continue after appending application-owned headers.
    AddHeaders(Vec<Header>),
}

/// An immutable pre-transaction request policy.
pub trait RequestPolicy: Send + Sync {
    /// Decide one send. The request and target cannot be mutated or replaced.
    fn decide(&self, request: &Request, target: &Target) -> RequestPolicyDecision;
}

/// Cloneable configured request policy.
#[derive(Clone)]
pub struct RequestPolicyRef(Arc<dyn RequestPolicy>);

impl RequestPolicyRef {
    /// Wrap a request policy for [`crate::Config`].
    #[must_use]
    pub fn new(policy: impl RequestPolicy + 'static) -> Self {
        Self(Arc::new(policy))
    }

    pub(crate) fn decide(&self, request: &Request, target: &Target) -> RequestPolicyDecision {
        self.0.decide(request, target)
    }
}

impl std::fmt::Debug for RequestPolicyRef {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("RequestPolicyRef(..)")
    }
}

/// Which side of the endpoint boundary a message crossed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageDirection {
    /// Parsed network input.
    Inbound,
    /// Finalized network output.
    Outbound,
}

/// How the transaction layer classified an observed message.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransactionClass {
    /// A new server transaction.
    ServerCreated,
    /// A message matched an existing transaction.
    Matched,
    /// No transaction matched.
    Unmatched,
    /// A new client transaction.
    ClientCreated,
    /// A request deliberately sent without a transaction.
    Direct,
}

/// A parsed inbound or finalized outbound SIP message.
#[derive(Debug, Clone)]
pub struct MessageObservation {
    /// Immutable message snapshot.
    pub message: Message,
    /// Endpoint address.
    pub local: std::net::SocketAddr,
    /// Remote address.
    pub peer: std::net::SocketAddr,
    /// Wire transport.
    pub transport: TransportKind,
    /// Boundary direction.
    pub direction: MessageDirection,
    /// Transaction-layer classification.
    pub transaction: TransactionClass,
}

/// Stable identity of one pooled connection incarnation.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConnectionId {
    /// Pool key, including verified identity and WebSocket resource when present.
    pub key: ConnectionKey,
    /// Pool incarnation; replacement receives another value.
    pub generation: u64,
    /// Source-admission generation retained by an inbound connection.
    pub admission_generation: Option<u64>,
}

/// A connection lifecycle transition.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionState {
    /// An inbound socket was accepted into the pool.
    Accepted,
    /// An outbound or inbound stream became usable.
    Opened,
    /// A secure transport authenticated its peer.
    Authenticated,
    /// The connection joined the bounded pool.
    Pooled,
    /// Existing pooled transport carried another send.
    Reused,
    /// Opening, authentication or framing failed.
    Failed,
    /// The incarnation closed.
    Closed,
}

/// One connection transition.
#[derive(Debug, Clone)]
pub struct ConnectionObservation {
    /// Stable connection incarnation.
    pub connection: ConnectionId,
    /// Transition.
    pub state: ConnectionState,
}

/// One item on the bounded endpoint observation stream.
#[derive(Debug, Clone)]
pub enum EndpointObservation {
    /// A parsed or finalized SIP message.
    Message(Box<MessageObservation>),
    /// A connection lifecycle transition.
    Connection(ConnectionObservation),
}

/// Shared non-blocking fan-in for every endpoint task.
#[derive(Debug)]
pub(crate) struct ObservationHub {
    sink: Mutex<Option<mpsc::Sender<EndpointObservation>>>,
    meters: Arc<Meters>,
}

impl ObservationHub {
    pub(crate) fn new(meters: Arc<Meters>) -> Self {
        Self {
            sink: Mutex::new(None),
            meters,
        }
    }

    pub(crate) fn subscribe(&self, capacity: usize) -> mpsc::Receiver<EndpointObservation> {
        let (sender, receiver) = mpsc::channel(capacity.max(1));
        *self
            .sink
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(sender);
        receiver
    }

    pub(crate) fn emit(&self, event: EndpointObservation) {
        let mut sink = self
            .sink
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let Some(sender) = sink.as_ref() else {
            return;
        };
        match sender.try_send(event) {
            Ok(()) => {}
            Err(mpsc::error::TrySendError::Full(_)) => self.meters.observation_drop(),
            Err(mpsc::error::TrySendError::Closed(_)) => *sink = None,
        }
    }
}

pub(crate) fn connection_event(
    key: ConnectionKey,
    generation: u64,
    admission_generation: Option<u64>,
    state: ConnectionState,
) -> EndpointObservation {
    EndpointObservation::Connection(ConnectionObservation {
        connection: ConnectionId {
            key,
            generation,
            admission_generation,
        },
        state,
    })
}

/// Resolve deliberate `Other` construction before deciding whether policy may append the field.
pub(crate) fn policy_header(name: &sipx_sip::HeaderName) -> (sipx_sip::HeaderName, bool) {
    use sipx_sip::HeaderName;
    let semantic = HeaderName::parse(&bytes::Bytes::copy_from_slice(name.canonical()));
    let allowed = matches!(
        semantic,
        HeaderName::AlertInfo
            | HeaderName::CallInfo
            | HeaderName::Organization
            | HeaderName::Priority
            | HeaderName::Subject
            | HeaderName::UserAgent
            | HeaderName::Other(_)
    );
    (semantic, allowed)
}

pub(crate) fn duplicate_policy_header(request: &Request, semantic: &sipx_sip::HeaderName) -> bool {
    !matches!(semantic, sipx_sip::HeaderName::Other(_)) && request.headers.get(semantic).is_some()
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::indexing_slicing
)]
mod tests {
    use super::*;
    use bytes::Bytes;
    use sipx_sip::HeaderName;
    use std::net::{Ipv4Addr, Ipv6Addr};

    #[test]
    fn prefixes_match_only_their_network_and_family() {
        let v4 = SourcePrefix::new(Ipv4Addr::new(192, 0, 2, 0).into(), 24).unwrap();
        assert!(v4.contains(Ipv4Addr::new(192, 0, 2, 42).into()));
        assert!(!v4.contains(Ipv4Addr::new(192, 0, 3, 1).into()));
        assert!(!v4.contains(Ipv6Addr::LOCALHOST.into()));
    }

    #[test]
    fn replacement_publishes_complete_generations() {
        let admission = SourceAdmission::default();
        assert_eq!(admission.admit(IpAddr::V4(Ipv4Addr::LOCALHOST)), Some(0));
        let one = admission
            .replace(vec![SourcePrefix::address(IpAddr::V4(Ipv4Addr::LOCALHOST))])
            .unwrap();
        assert_eq!(admission.admit(IpAddr::V4(Ipv4Addr::LOCALHOST)), Some(one));
        assert_eq!(admission.admit(IpAddr::V4(Ipv4Addr::UNSPECIFIED)), None);
        let two = admission.clear();
        assert!(two > one);
        assert_eq!(
            admission.admit(IpAddr::V4(Ipv4Addr::UNSPECIFIED)),
            Some(two)
        );
    }

    #[test]
    fn oversized_replacement_preserves_the_old_generation() {
        let admission = SourceAdmission::new(1);
        let first = admission
            .replace(vec![SourcePrefix::address(IpAddr::V4(Ipv4Addr::LOCALHOST))])
            .unwrap();
        let error = admission
            .replace(vec![
                SourcePrefix::address(IpAddr::V4(Ipv4Addr::LOCALHOST)),
                SourcePrefix::address(IpAddr::V4(Ipv4Addr::UNSPECIFIED)),
            ])
            .unwrap_err();
        assert!(matches!(
            error,
            crate::Error::SourceAdmissionCapacity { .. }
        ));
        assert_eq!(
            admission.admit(IpAddr::V4(Ipv4Addr::LOCALHOST)),
            Some(first)
        );
        assert_eq!(admission.admit(IpAddr::V4(Ipv4Addr::UNSPECIFIED)), None);
    }

    #[test]
    fn request_policy_allows_only_application_fields_and_unknown_extensions() {
        for name in [HeaderName::Subject, HeaderName::Organization] {
            assert!(policy_header(&name).1);
        }
        assert!(policy_header(&HeaderName::Other(Bytes::from_static(b"X-Trace"))).1);
        for name in [
            HeaderName::Contact,
            HeaderName::ContentType,
            HeaderName::Event,
        ] {
            assert!(!policy_header(&name).1);
        }
        for raw in [b"vIa".as_slice(), b"v".as_slice()] {
            let (semantic, allowed) =
                policy_header(&HeaderName::Other(Bytes::copy_from_slice(raw)));
            assert_eq!(semantic, HeaderName::Via);
            assert!(!allowed);
        }
    }
}