net-io 0.5.0

Network IO substrate for Rust: TCP, UDP and local-socket listeners, connection lifecycle, limits, graceful drain, PROXY protocol, and a TLS seam. Protocol-free and dependency-free.
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
//! What net-io knows about one connection.

use std::fmt;
use std::net::SocketAddr;
#[cfg(unix)]
use std::path::Path;
use std::sync::Arc;
use std::time::Instant;

use crate::addr::{PrefixKey, PrefixPolicy};
use crate::id::{ConnectionId, ListenerId, WorkerId};
use crate::proxy::ProxyFacts;
use crate::tls::TlsInfo;

/// An opaque importance class the gate assigns at admission; lower is more important.
///
/// net-io uses it only to shed lanes under load and for per-lane reservations.
///
/// # Examples
///
/// ```
/// use net_io::Lane;
/// assert!(Lane::MOST_IMPORTANT < Lane(3));
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct Lane(pub u8);

impl Lane {
    /// Lane 0.
    pub const MOST_IMPORTANT: Lane = Lane(0);
}

/// An opaque value the gate returns at admission, stored in [`ConnectionInfo::gate`].
///
/// It lets a protocol engine name the connection to the gate without being
/// generic over the gate's type. net-io never interprets it.
///
/// # Examples
///
/// ```
/// let tag = net_io::GateTag(17);
/// assert_eq!(tag.0, 17);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub struct GateTag(pub u64);

/// The kind of socket a connection uses.
///
/// ```
/// assert_ne!(net_io::Transport::Tcp, net_io::Transport::Unix);
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Transport {
    /// TCP.
    Tcp,
    /// UDP.
    Udp,
    /// A Unix domain socket.
    Unix,
    /// A Windows named pipe.
    Pipe,
}

/// How a connection came to exist.
///
/// ```
/// assert_ne!(net_io::Origin::Accepted, net_io::Origin::Dialed);
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Origin {
    /// Accepted by a listener.
    Accepted,
    /// Dialed by this process.
    Dialed,
    /// Dialed and then adopted into a server.
    Adopted,
    /// Inherited from another process.
    Inherited,
}

/// A Unix socket path in a fixed array (up to 108 bytes).
///
/// ```
/// let path = net_io::UnixPath::new(b"/run/app.sock").unwrap();
/// assert_eq!(path.as_bytes(), b"/run/app.sock");
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnixPath {
    bytes: [u8; 108],
    len: u8,
}

impl UnixPath {
    /// The largest path this type holds.
    pub const MAX: usize = 108;

    /// A path from bytes; `None` when longer than [`UnixPath::MAX`].
    #[must_use]
    pub fn new(bytes: &[u8]) -> Option<UnixPath> {
        if bytes.len() > Self::MAX {
            return None;
        }
        let mut out = [0u8; 108];
        out[..bytes.len()].copy_from_slice(bytes);
        Some(UnixPath {
            bytes: out,
            len: u8::try_from(bytes.len()).ok()?,
        })
    }

    /// The path bytes.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes[..usize::from(self.len)]
    }

    /// The path as a `Path`.
    ///
    /// ```
    /// # #[cfg(unix)] {
    /// let path = net_io::UnixPath::new(b"/tmp/x").unwrap();
    /// assert_eq!(path.as_path(), std::path::Path::new("/tmp/x"));
    /// # }
    /// ```
    #[cfg(unix)]
    #[must_use]
    pub fn as_path(&self) -> &Path {
        use std::os::unix::ffi::OsStrExt;
        Path::new(std::ffi::OsStr::from_bytes(self.as_bytes()))
    }
}

impl std::fmt::Debug for UnixPath {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "UnixPath({:?})",
            String::from_utf8_lossy(self.as_bytes())
        )
    }
}

/// A Windows pipe name in a fixed array (up to 256 UTF-16 units).
///
/// ```
/// let name = net_io::PipeName::new("myapp").unwrap();
/// assert_eq!(name.to_string_lossy(), "myapp");
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct PipeName {
    units: [u16; 256],
    len: u16,
}

impl PipeName {
    /// The longest name this type holds, in UTF-16 units.
    pub const MAX: usize = 256;

    /// A name from text; `None` when longer than [`PipeName::MAX`] units.
    #[must_use]
    pub fn new(name: &str) -> Option<PipeName> {
        let mut units = [0u16; 256];
        let mut len = 0usize;
        for u in name.encode_utf16() {
            if len == Self::MAX {
                return None;
            }
            units[len] = u;
            len += 1;
        }
        Some(PipeName {
            units,
            len: u16::try_from(len).ok()?,
        })
    }

    /// The name as UTF-16 units.
    #[must_use]
    pub fn as_wide(&self) -> &[u16] {
        &self.units[..usize::from(self.len)]
    }

    /// The name as text, with invalid units replaced.
    #[must_use]
    pub fn to_string_lossy(&self) -> String {
        String::from_utf16_lossy(self.as_wide())
    }
}

impl std::fmt::Debug for PipeName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "PipeName({:?})", self.to_string_lossy())
    }
}

/// One end of a connection.
///
/// ```
/// let end = net_io::Endpoint::Ip("127.0.0.1:80".parse().unwrap());
/// assert_eq!(end.socket_addr().unwrap().port(), 80);
/// ```
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Endpoint {
    /// An IP socket address.
    Ip(SocketAddr),
    /// A Unix socket path (shared with the listener that owns it).
    Unix(Arc<UnixPath>),
    /// A Windows pipe name (shared with the listener that owns it).
    Pipe(Arc<PipeName>),
    /// An unnamed Unix socket peer.
    Unnamed,
}

/// The address as text: `127.0.0.1:80`, a Unix path, a pipe name, or `unnamed`.
///
/// ```
/// let e = net_io::Endpoint::Ip("127.0.0.1:80".parse().unwrap());
/// assert_eq!(e.to_string(), "127.0.0.1:80");
/// ```
impl fmt::Display for Endpoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Endpoint::Ip(a) => write!(f, "{a}"),
            Endpoint::Unix(p) => write!(f, "{}", String::from_utf8_lossy(p.as_bytes())),
            Endpoint::Pipe(p) => {
                for c in char::decode_utf16(p.as_wide().iter().copied()) {
                    write!(f, "{}", c.unwrap_or(char::REPLACEMENT_CHARACTER))?;
                }
                Ok(())
            }
            Endpoint::Unnamed => f.write_str("unnamed"),
        }
    }
}

impl Endpoint {
    /// The socket address, for IP endpoints.
    #[must_use]
    pub fn socket_addr(&self) -> Option<SocketAddr> {
        match self {
            Endpoint::Ip(a) => Some(*a),
            _ => None,
        }
    }
}

/// Who is on the other end of a local socket. A field the platform cannot report is `None`.
///
/// ```
/// let creds = net_io::PeerCredentials { pid: Some(1), uid: None, gid: None };
/// assert_eq!(creds.pid, Some(1));
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PeerCredentials {
    /// The peer's process id.
    pub pid: Option<u32>,
    /// The peer's user id.
    pub uid: Option<u32>,
    /// The peer's group id.
    pub gid: Option<u32>,
}

/// What net-io knows about one connection, built at accept and completed as the
/// handshake reveals more. It holds no heap memory.
///
/// # Examples
///
/// ```
/// let info = net_io::ConnectionInfo::for_testing("192.0.2.1:5000".parse().unwrap());
/// assert_eq!(info.client_addr().unwrap().port(), 5000);
/// let key = info.client_key(net_io::PrefixPolicy::DEFAULT).unwrap();
/// assert_eq!(key.to_string(), "192.0.2.1/32");
/// ```
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct ConnectionInfo {
    /// The connection's id.
    pub id: ConnectionId,
    /// The listener that accepted it, if any.
    pub listener: Option<ListenerId>,
    /// The worker that owns it.
    pub worker: WorkerId,
    /// The kind of socket.
    pub transport: Transport,
    /// How it came to exist.
    pub origin: Origin,
    /// Our end.
    pub local: Endpoint,
    /// The socket peer, normalized.
    pub peer: Endpoint,
    /// The real client from a trusted PROXY header, normalized.
    pub real_peer: Option<SocketAddr>,
    /// Facts copied from a trusted PROXY header (pooled per worker, no allocation after warm-up).
    pub proxy: Option<Arc<ProxyFacts>>,
    /// Who is on the other end of a local socket.
    pub peer_credentials: Option<PeerCredentials>,
    /// TLS facts after the handshake (pooled per worker, no allocation after warm-up).
    pub tls: Option<Arc<TlsInfo>>,
    /// The lane the gate assigned.
    pub lane: Lane,
    /// The gate's tag for this connection.
    pub gate: GateTag,
    /// When the connection was accepted or dialed.
    pub accepted_at: Instant,
}

impl ConnectionInfo {
    /// The client address: the real peer from a trusted PROXY header when set,
    /// otherwise the IP peer.
    #[inline]
    #[must_use]
    pub fn client_addr(&self) -> Option<SocketAddr> {
        self.real_peer.or_else(|| self.peer.socket_addr())
    }

    /// The client address grouped under `policy`.
    #[must_use]
    pub fn client_key(&self, policy: PrefixPolicy) -> Option<PrefixKey> {
        self.client_addr().map(|a| PrefixKey::of(a.ip(), policy))
    }

    /// The socket peer, ignoring any PROXY header.
    #[must_use]
    pub fn socket_peer(&self) -> Option<SocketAddr> {
        self.peer.socket_addr()
    }

    /// A connection fact set attached to no socket, for testing gates and sessions.
    ///
    /// The id comes from the process counter, so it never collides with a real
    /// connection. Fields are public to adjust.
    #[must_use]
    pub fn for_testing(peer: SocketAddr) -> ConnectionInfo {
        let local = if peer.is_ipv4() {
            SocketAddr::from(([127, 0, 0, 1], 0))
        } else {
            SocketAddr::from(([0u16, 0, 0, 0, 0, 0, 0, 1], 0))
        };
        ConnectionInfo::new_tcp(
            crate::id::issue_single(),
            None,
            WorkerId(0),
            Origin::Accepted,
            local,
            crate::addr::normalize_socket(peer),
            Instant::now(),
        )
    }

    /// A connection on a Unix domain socket or a named pipe.
    #[cfg(feature = "local")]
    pub(crate) fn new_unix(
        id: ConnectionId,
        listener: Option<ListenerId>,
        origin: Origin,
        local: Endpoint,
        peer: Endpoint,
        credentials: Option<PeerCredentials>,
        at: Instant,
    ) -> ConnectionInfo {
        ConnectionInfo {
            id,
            listener,
            worker: WorkerId(0),
            transport: Transport::Unix,
            origin,
            local,
            peer,
            real_peer: None,
            proxy: None,
            peer_credentials: credentials,
            tls: None,
            lane: Lane::MOST_IMPORTANT,
            gate: GateTag(0),
            accepted_at: at,
        }
    }

    pub(crate) fn new_tcp(
        id: ConnectionId,
        listener: Option<ListenerId>,
        worker: WorkerId,
        origin: Origin,
        local: SocketAddr,
        peer: SocketAddr,
        at: Instant,
    ) -> ConnectionInfo {
        ConnectionInfo {
            id,
            listener,
            worker,
            transport: Transport::Tcp,
            origin,
            local: Endpoint::Ip(local),
            peer: Endpoint::Ip(peer),
            real_peer: None,
            proxy: None,
            peer_credentials: None,
            tls: None,
            lane: Lane::MOST_IMPORTANT,
            gate: GateTag(0),
            accepted_at: at,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_client_addr_prefers_real_peer() {
        let mut info = ConnectionInfo::for_testing("10.0.0.1:1".parse().unwrap());
        assert_eq!(info.client_addr().unwrap().ip().to_string(), "10.0.0.1");
        info.real_peer = Some("203.0.113.5:9".parse().unwrap());
        assert_eq!(info.client_addr().unwrap().ip().to_string(), "203.0.113.5");
        assert_eq!(info.socket_peer().unwrap().ip().to_string(), "10.0.0.1");
    }

    #[test]
    fn test_for_testing_normalizes_mapped_peer() {
        let info = ConnectionInfo::for_testing("[::ffff:1.2.3.4]:80".parse().unwrap());
        assert_eq!(info.socket_peer().unwrap().to_string(), "1.2.3.4:80");
    }

    #[test]
    fn test_unix_path_bounds() {
        assert!(UnixPath::new(&[b'a'; 108]).is_some());
        assert!(UnixPath::new(&[b'a'; 109]).is_none());
    }

    #[test]
    fn test_pipe_name_bounds() {
        assert!(PipeName::new(&"a".repeat(256)).is_some());
        assert!(PipeName::new(&"a".repeat(257)).is_none());
    }

    #[test]
    fn test_connection_info_size_fits_idle_budget() {
        // Stored once per connection; the idle-connection target is 512 bytes of net-io state.
        assert!(
            std::mem::size_of::<ConnectionInfo>() <= 256,
            "{}",
            std::mem::size_of::<ConnectionInfo>()
        );
    }
}