uni-addr 0.3.8

A unified address type that can represent a `std::net::SocketAddr`, a `std::os::unix::net::SocketAddr`, or a host name with port.
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#![doc = include_str!("../README.md")]
#![allow(clippy::must_use_candidate)]
#![deprecated(
    since = "0.0.0",
    note = "This crate is deprecated, use `uaddr` instead: https://crates.io/crates/uaddr"
)]

use std::borrow::Cow;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
use std::str::FromStr;
use std::sync::Arc;
use std::{fmt, io};

#[cfg(unix)]
pub mod unix;

/// The prefix for Unix domain socket URIs.
///
/// - `unix:///path/to/socket` for a pathname socket address.
/// - `unix://@abstract.unix.socket` for an abstract socket address.
pub const UNIX_URI_PREFIX: &str = "unix://";

wrapper_lite::wrapper!(
    #[wrapper_impl(Debug)]
    #[wrapper_impl(Display)]
    #[wrapper_impl(AsRef)]
    #[wrapper_impl(Deref)]
    #[repr(align(cache))]
    #[derive(Clone, PartialEq, Eq, Hash)]
    /// A unified address type that can represent:
    ///
    /// - [`std::net::SocketAddr`]
    /// - [`unix::SocketAddr`] (a wrapper over
    ///   [`std::os::unix::net::SocketAddr`])
    /// - A host name with port. See [`ToSocketAddrs`].
    ///
    /// # Parsing Behaviour
    ///
    /// - Checks if the address started with [`UNIX_URI_PREFIX`]: parse as a UDS
    ///   address.
    /// - Checks if the address is started with a alphabetic character (a-z,
    ///   A-Z): treat as a host name. Notes that we will not validate if the
    ///   host name is valid.
    /// - Tries to parse as a network socket address.
    /// - Otherwise, treats the input as a host name.
    pub struct UniAddr(UniAddrInner);
);

impl From<SocketAddr> for UniAddr {
    fn from(addr: SocketAddr) -> Self {
        UniAddr::from_inner(UniAddrInner::Inet(addr))
    }
}

#[cfg(unix)]
impl From<std::os::unix::net::SocketAddr> for UniAddr {
    fn from(addr: std::os::unix::net::SocketAddr) -> Self {
        UniAddr::from_inner(UniAddrInner::Unix(addr.into()))
    }
}

#[cfg(all(unix, feature = "feat-tokio"))]
impl From<tokio::net::unix::SocketAddr> for UniAddr {
    fn from(addr: tokio::net::unix::SocketAddr) -> Self {
        UniAddr::from_inner(UniAddrInner::Unix(unix::SocketAddr::from(addr.into())))
    }
}

#[cfg(feature = "feat-socket2")]
impl TryFrom<socket2::SockAddr> for UniAddr {
    type Error = io::Error;

    fn try_from(addr: socket2::SockAddr) -> Result<Self, Self::Error> {
        UniAddr::try_from(&addr)
    }
}

#[cfg(feature = "feat-socket2")]
impl TryFrom<&socket2::SockAddr> for UniAddr {
    type Error = io::Error;

    fn try_from(addr: &socket2::SockAddr) -> Result<Self, Self::Error> {
        if let Some(addr) = addr.as_socket() {
            return Ok(Self::from(addr));
        }

        #[cfg(unix)]
        if let Some(addr) = addr.as_unix() {
            return Ok(Self::from(addr));
        }

        #[cfg(unix)]
        if addr.is_unnamed() {
            return Ok(Self::from(crate::unix::SocketAddr::new_unnamed()));
        }

        #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
        if let Some(addr) = addr.as_abstract_namespace() {
            return crate::unix::SocketAddr::new_abstract(addr).map(Self::from);
        }

        Err(io::Error::new(
            io::ErrorKind::Other,
            "unsupported address type",
        ))
    }
}

#[cfg(feature = "feat-socket2")]
impl TryFrom<UniAddr> for socket2::SockAddr {
    type Error = io::Error;

    fn try_from(addr: UniAddr) -> Result<Self, Self::Error> {
        socket2::SockAddr::try_from(&addr)
    }
}

#[cfg(feature = "feat-socket2")]
impl TryFrom<&UniAddr> for socket2::SockAddr {
    type Error = io::Error;

    fn try_from(addr: &UniAddr) -> Result<Self, Self::Error> {
        match &addr.inner {
            UniAddrInner::Inet(addr) => Ok(socket2::SockAddr::from(*addr)),
            #[cfg(unix)]
            UniAddrInner::Unix(addr) => socket2::SockAddr::unix(addr.to_os_string()),
            UniAddrInner::Host(_) => Err(io::Error::new(
                io::ErrorKind::Other,
                "The host name address must be resolved before converting to SockAddr",
            )),
        }
    }
}

#[cfg(unix)]
impl From<crate::unix::SocketAddr> for UniAddr {
    fn from(addr: crate::unix::SocketAddr) -> Self {
        UniAddr::from_inner(UniAddrInner::Unix(addr))
    }
}

impl FromStr for UniAddr {
    type Err = ParseError;

    fn from_str(addr: &str) -> Result<Self, Self::Err> {
        Self::new(addr)
    }
}

#[cfg(feature = "feat-serde")]
impl serde::Serialize for UniAddr {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_str())
    }
}

#[cfg(feature = "feat-serde")]
impl<'de> serde::Deserialize<'de> for UniAddr {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Self::new(&String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
    }
}

impl UniAddr {
    #[inline]
    /// Creates a new [`UniAddr`] from its string representation.
    ///
    /// # Errors
    ///
    /// Not a valid address string.
    pub fn new(addr: &str) -> Result<Self, ParseError> {
        if addr.is_empty() {
            return Err(ParseError::Empty);
        }

        #[cfg(unix)]
        if let Some(addr) = addr.strip_prefix(UNIX_URI_PREFIX) {
            return unix::SocketAddr::new(addr)
                .map(UniAddrInner::Unix)
                .map(Self::from_inner)
                .map_err(ParseError::InvalidUDSAddress);
        }

        #[cfg(not(unix))]
        if let Some(_addr) = addr.strip_prefix(UNIX_URI_PREFIX) {
            return Err(ParseError::Unsupported);
        }

        let Some((host, port)) = addr.rsplit_once(':') else {
            return Err(ParseError::InvalidPort);
        };

        let Ok(port) = port.parse::<u16>() else {
            return Err(ParseError::InvalidPort);
        };

        // Short-circuit: IPv4 address starts with a digit.
        if host.chars().next().is_some_and(|c| c.is_ascii_digit()) {
            return Ipv4Addr::from_str(host)
                .map(|ip| SocketAddr::V4(SocketAddrV4::new(ip, port)))
                .map(UniAddrInner::Inet)
                .map(Self::from_inner)
                .map_err(|_| ParseError::InvalidHost)
                .or_else(|_| {
                    // A host name may also start with a digit.
                    Self::new_host(addr, Some((host, port)))
                });
        }

        // Short-circuit: if starts with '[' and ends with ']', may be an IPv6 address
        // and can never be a host.
        if let Some(ipv6_addr) = host.strip_prefix('[').and_then(|s| s.strip_suffix(']')) {
            return Ipv6Addr::from_str(ipv6_addr)
                .map(|ip| SocketAddr::V6(SocketAddrV6::new(ip, port, 0, 0)))
                .map(UniAddrInner::Inet)
                .map(Self::from_inner)
                .map_err(|_| ParseError::InvalidHost);
        }

        // Fallback: check if is a valid host name.
        Self::new_host(addr, Some((host, port)))
    }

    /// Creates a new [`UniAddr`] from a string containing a host name and port,
    /// like `example.com:8080`.
    ///
    /// # Errors
    ///
    /// - [`ParseError::InvalidHost`] if the host name is invalid.
    /// - [`ParseError::InvalidPort`] if the port is invalid.
    pub fn new_host(addr: &str, parsed: Option<(&str, u16)>) -> Result<Self, ParseError> {
        let (hostname, _port) = match parsed {
            Some((hostname, port)) => (hostname, port),
            None => addr
                .rsplit_once(':')
                .ok_or(ParseError::InvalidPort)
                .and_then(|(hostname, port)| {
                    let Ok(port) = port.parse::<u16>() else {
                        return Err(ParseError::InvalidPort);
                    };

                    Ok((hostname, port))
                })?,
        };

        Self::validate_host_name(hostname.as_bytes()).map_err(|()| ParseError::InvalidHost)?;

        Ok(Self::from_inner(UniAddrInner::Host(Arc::from(addr))))
    }

    // https://github.com/rustls/pki-types/blob/b8c04aa6b7a34875e2c4a33edc9b78d31da49523/src/server_name.rs
    const fn validate_host_name(input: &[u8]) -> Result<(), ()> {
        enum State {
            Start,
            Next,
            NumericOnly { len: usize },
            NextAfterNumericOnly,
            Subsequent { len: usize },
            Hyphen { len: usize },
        }

        use State::{Hyphen, Next, NextAfterNumericOnly, NumericOnly, Start, Subsequent};

        /// "Labels must be 63 characters or less."
        const MAX_LABEL_LENGTH: usize = 63;

        /// <https://devblogs.microsoft.com/oldnewthing/20120412-00/?p=7873>
        const MAX_NAME_LENGTH: usize = 253;

        let mut state = Start;

        if input.len() > MAX_NAME_LENGTH {
            return Err(());
        }

        let mut idx = 0;
        while idx < input.len() {
            let ch = input[idx];
            state = match (state, ch) {
                (Start | Next | NextAfterNumericOnly | Hyphen { .. }, b'.') => {
                    return Err(());
                }
                (Subsequent { .. }, b'.') => Next,
                (NumericOnly { .. }, b'.') => NextAfterNumericOnly,
                (Subsequent { len } | NumericOnly { len } | Hyphen { len }, _)
                    if len >= MAX_LABEL_LENGTH =>
                {
                    return Err(());
                }
                (Start | Next | NextAfterNumericOnly, b'0'..=b'9') => NumericOnly { len: 1 },
                (NumericOnly { len }, b'0'..=b'9') => NumericOnly { len: len + 1 },
                (Start | Next | NextAfterNumericOnly, b'a'..=b'z' | b'A'..=b'Z' | b'_') => {
                    Subsequent { len: 1 }
                }
                (Subsequent { len } | NumericOnly { len } | Hyphen { len }, b'-') => {
                    Hyphen { len: len + 1 }
                }
                (
                    Subsequent { len } | NumericOnly { len } | Hyphen { len },
                    b'a'..=b'z' | b'A'..=b'Z' | b'_' | b'0'..=b'9',
                ) => Subsequent { len: len + 1 },
                _ => return Err(()),
            };
            idx += 1;
        }

        if matches!(
            state,
            Start | Hyphen { .. } | NumericOnly { .. } | NextAfterNumericOnly | Next
        ) {
            return Err(());
        }

        Ok(())
    }

    /// Resolves the address if it is a host name.
    ///
    /// By default, we utilize the method [`ToSocketAddrs::to_socket_addrs`]
    /// provided by the standard library to perform DNS resolution, which is a
    /// **blocking** operation and may take an arbitrary amount of time to
    /// complete, use with caution when called in asynchronous contexts.
    ///
    /// # Errors
    ///
    /// Resolution failure, or if no socket address resolved.
    pub fn blocking_resolve_socket_addrs(&mut self) -> io::Result<()> {
        self.blocking_resolve_socket_addrs_with(ToSocketAddrs::to_socket_addrs)
    }

    /// Resolves the address if it is a host name using a custom resolver
    /// function.
    ///
    /// # Errors
    ///
    /// Resolution failure, or if no socket address resolved.
    pub fn blocking_resolve_socket_addrs_with<F, A>(&mut self, f: F) -> io::Result<()>
    where
        F: FnOnce(&str) -> io::Result<A>,
        A: Iterator<Item = SocketAddr>,
    {
        if let UniAddrInner::Host(addr) = self.as_inner() {
            let resolved = f(addr)?.next().ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::Other,
                    "Host resolution failed, no available address",
                )
            })?;

            *self = Self::from_inner(UniAddrInner::Inet(resolved));
        }

        Ok(())
    }

    #[cfg(feature = "feat-tokio")]
    /// Asynchronously resolves the address if it is a host name.
    ///
    /// This method will spawn a blocking Tokio task to perform the resolution
    /// using [`ToSocketAddrs::to_socket_addrs`] provided by the standard
    /// library.
    ///
    /// # Errors
    ///
    /// Resolution failure, or if no socket address resolved.
    pub async fn resolve_socket_addrs(&mut self) -> io::Result<()> {
        if let UniAddrInner::Host(addr) = self.as_inner() {
            let addr = addr.clone();
            let resolved = tokio::task::spawn_blocking(move || addr.to_socket_addrs())
                .await??
                .next()
                .ok_or_else(|| {
                    io::Error::new(
                        io::ErrorKind::Other,
                        "Host resolution failed, no available address",
                    )
                })?;

            *self = Self::from_inner(UniAddrInner::Inet(resolved));
        }

        Ok(())
    }

    #[inline]
    /// Serializes the address to a string.
    pub fn to_str(&self) -> Cow<'_, str> {
        self.as_inner().to_str()
    }
}

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// See [`UniAddr`].
///
/// Generally, you should use [`UniAddr`] instead of this type directly, as
/// we expose this type only for easier pattern matching. A valid [`UniAddr`]
/// can be constructed only through [`FromStr`] implementation.
pub enum UniAddrInner {
    /// See [`SocketAddr`].
    Inet(SocketAddr),

    #[cfg(unix)]
    /// See [`SocketAddr`](crate::unix::SocketAddr).
    Unix(crate::unix::SocketAddr),

    /// A host name with port.
    ///
    /// Please refer to [`ToSocketAddrs`], and
    /// [`UniAddr::blocking_resolve_socket_addrs`], etc to resolve the
    /// address when needed.
    Host(Arc<str>),
}

impl fmt::Display for UniAddrInner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.to_str().fmt(f)
    }
}

impl UniAddrInner {
    #[inline]
    /// Serializes the address to a string.
    pub fn to_str(&self) -> Cow<'_, str> {
        match self {
            Self::Inet(addr) => addr.to_string().into(),
            #[cfg(unix)]
            Self::Unix(addr) => addr
                .to_os_string_impl(UNIX_URI_PREFIX, "@")
                .to_string_lossy()
                .to_string()
                .into(),
            Self::Host(host) => Cow::Borrowed(host),
        }
    }
}

#[derive(Debug)]
/// Errors that can occur when parsing a [`UniAddr`] from a string.
pub enum ParseError {
    /// Empty input string
    Empty,

    /// Invalid or missing hostname, or an invalid Ipv4 / IPv6 address
    InvalidHost,

    /// Invalid address format: missing or invalid port
    InvalidPort,

    /// Invalid UDS address format
    InvalidUDSAddress(io::Error),

    /// Unsupported address type on this platform
    Unsupported,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => write!(f, "empty address string"),
            Self::InvalidHost => write!(f, "invalid or missing host address"),
            Self::InvalidPort => write!(f, "invalid or missing port"),
            Self::InvalidUDSAddress(err) => write!(f, "invalid UDS address: {err}"),
            Self::Unsupported => write!(f, "unsupported address type on this platform"),
        }
    }
}

impl std::error::Error for ParseError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::InvalidUDSAddress(err) => Some(err),
            _ => None,
        }
    }
}

impl From<ParseError> for io::Error {
    fn from(value: ParseError) -> Self {
        io::Error::new(io::ErrorKind::Other, value)
    }
}

#[cfg(test)]
mod tests {
    #![allow(non_snake_case)]

    use rstest::rstest;

    use super::*;

    #[rstest]
    #[case("0.0.0.0:0")]
    #[case("0.0.0.0:8080")]
    #[case("127.0.0.1:0")]
    #[case("127.0.0.1:8080")]
    #[case("[::]:0")]
    #[case("[::]:8080")]
    #[case("[::1]:0")]
    #[case("[::1]:8080")]
    #[case("example.com:8080")]
    #[case("1example.com:8080")]
    #[cfg_attr(unix, case("unix://"))]
    #[cfg_attr(
        any(target_os = "android", target_os = "linux", target_os = "cygwin"),
        case("unix://@")
    )]
    #[cfg_attr(unix, case("unix:///tmp/test_UniAddr_new_Display.socket"))]
    #[cfg_attr(
        any(target_os = "android", target_os = "linux", target_os = "cygwin"),
        case("unix://@test_UniAddr_new_Display.socket")
    )]
    fn test_UniAddr_new_Display(#[case] addr: &str) {
        let addr_displayed = UniAddr::new(addr).unwrap().to_string();

        assert_eq!(
            addr_displayed, addr,
            "addr_displayed {addr_displayed:?} != {addr:?}"
        );
    }

    #[rstest]
    #[case("example.com:8080")]
    #[case("1example.com:8080")]
    #[should_panic]
    #[case::panic("1example.com")]
    #[should_panic]
    #[case::panic("1example.com.")]
    #[should_panic]
    #[case::panic("1example.com.:14514")]
    #[should_panic]
    #[case::panic("1example.com:1919810")]
    #[should_panic]
    #[case::panic("this-is-a-long-host-name-this-is-a-long-host-name-this-is-a-long-host-name-this-is-a-long-host-name-this-is-a-long-host-name-this-is-a-long-host-name-this-is-a-long-host-name-this-is-a-long-host-name-this-is-a-long-host-name-this-is-a-long-host-name-this-is-a-long-host-name:19810")]
    fn test_UniAddr_new_host(#[case] addr: &str) {
        let addr_displayed = UniAddr::new_host(addr, None).unwrap().to_string();

        assert_eq!(
            addr_displayed, addr,
            "addr_displayed {addr_displayed:?} != {addr:?}"
        );
    }

    #[rstest]
    #[should_panic]
    #[case::panic("")]
    #[should_panic]
    #[case::panic("not-an-address")]
    #[should_panic]
    #[case::panic("127.0.0.1")]
    #[should_panic]
    #[case::panic("127.0.0.1:99999")]
    #[should_panic]
    #[case::panic("127.0.0.256:99999")]
    #[should_panic]
    #[case::panic("::1")]
    #[should_panic]
    #[case::panic("[::1]")]
    #[should_panic]
    #[case::panic("[::1]:99999")]
    #[should_panic]
    #[case::panic("[::gg]:99999")]
    #[should_panic]
    #[case::panic("example.com")]
    #[should_panic]
    #[case::panic("example.com:99999")]
    #[should_panic]
    #[case::panic("examp😀le.com:99999")]
    fn test_UniAddr_new_invalid(#[case] addr: &str) {
        let _ = UniAddr::new(addr).unwrap();
    }

    #[cfg(not(unix))]
    #[test]
    fn test_UniAddr_new_unsupported() {
        // Unix sockets should be unsupported on non-Unix platforms
        let result = UniAddr::new("unix:///tmp/test.sock");

        assert!(matches!(result.unwrap_err(), ParseError::Unsupported));
    }

    #[rstest]
    #[case("0.0.0.0:0")]
    #[case("0.0.0.0:8080")]
    #[case("127.0.0.1:0")]
    #[case("127.0.0.1:8080")]
    #[case("[::]:0")]
    #[case("[::]:8080")]
    #[case("[::1]:0")]
    #[case("[::1]:8080")]
    #[cfg_attr(unix, case("unix:///tmp/test_socket2_sock_addr_conversion.socket"))]
    #[cfg_attr(unix, case("unix://"))]
    #[cfg_attr(
        any(target_os = "android", target_os = "linux", target_os = "cygwin"),
        case("unix://@test_socket2_sock_addr_conversion.socket")
    )]
    fn test_socket2_SockAddr_conversion(#[case] addr: &str) {
        let uni_addr = UniAddr::new(addr).unwrap();
        let sock_addr = socket2::SockAddr::try_from(&uni_addr).unwrap();
        let uni_addr_converted = UniAddr::try_from(sock_addr).unwrap();

        assert_eq!(
            uni_addr, uni_addr_converted,
            "{uni_addr} != {uni_addr_converted}"
        );
    }
}