core_net/
parser.rs

1//! A private parser implementation of IPv4, IPv6, and socket addresses.
2//!
3//! This module is "publicly exported" through the `FromStr` implementations
4//! below.
5
6use core::convert::TryInto;
7use std::error::Error;
8use core::fmt;
9use crate::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
10use core::str::FromStr;
11
12trait ReadNumberHelper: core::marker::Sized {
13    const ZERO: Self;
14    fn checked_mul(&self, other: u32) -> Option<Self>;
15    fn checked_add(&self, other: u32) -> Option<Self>;
16}
17
18macro_rules! impl_helper {
19    ($($t:ty)*) => ($(impl ReadNumberHelper for $t {
20        const ZERO: Self = 0;
21        #[inline]
22        fn checked_mul(&self, other: u32) -> Option<Self> {
23            Self::checked_mul(*self, other.try_into().ok()?)
24        }
25        #[inline]
26        fn checked_add(&self, other: u32) -> Option<Self> {
27            Self::checked_add(*self, other.try_into().ok()?)
28        }
29    })*)
30}
31
32impl_helper! { u8 u16 u32 }
33
34struct Parser<'a> {
35    // Parsing as ASCII, so can use byte array.
36    state: &'a [u8],
37}
38
39impl<'a> Parser<'a> {
40    fn new(input: &'a [u8]) -> Parser<'a> {
41        Parser { state: input }
42    }
43
44    /// Run a parser, and restore the pre-parse state if it fails.
45    fn read_atomically<T, F>(&mut self, inner: F) -> Option<T>
46    where
47        F: FnOnce(&mut Parser<'_>) -> Option<T>,
48    {
49        let state = self.state;
50        let result = inner(self);
51        if result.is_none() {
52            self.state = state;
53        }
54        result
55    }
56
57    /// Run a parser, but fail if the entire input wasn't consumed.
58    /// Doesn't run atomically.
59    fn parse_with<T, F>(&mut self, inner: F, kind: AddrKind) -> Result<T, AddrParseError>
60    where
61        F: FnOnce(&mut Parser<'_>) -> Option<T>,
62    {
63        let result = inner(self);
64        if self.state.is_empty() { result } else { None }.ok_or(AddrParseError(kind))
65    }
66
67    /// Peek the next character from the input
68    fn peek_char(&self) -> Option<char> {
69        self.state.first().map(|&b| char::from(b))
70    }
71
72    /// Read the next character from the input
73    fn read_char(&mut self) -> Option<char> {
74        self.state.split_first().map(|(&b, tail)| {
75            self.state = tail;
76            char::from(b)
77        })
78    }
79
80    #[must_use]
81    /// Read the next character from the input if it matches the target.
82    fn read_given_char(&mut self, target: char) -> Option<()> {
83        self.read_atomically(|p| {
84            p.read_char().and_then(|c| if c == target { Some(()) } else { None })
85        })
86    }
87
88    /// Helper for reading separators in an indexed loop. Reads the separator
89    /// character iff index > 0, then runs the parser. When used in a loop,
90    /// the separator character will only be read on index > 0 (see
91    /// read_ipv4_addr for an example)
92    fn read_separator<T, F>(&mut self, sep: char, index: usize, inner: F) -> Option<T>
93    where
94        F: FnOnce(&mut Parser<'_>) -> Option<T>,
95    {
96        self.read_atomically(move |p| {
97            if index > 0 {
98                p.read_given_char(sep)?;
99            }
100            inner(p)
101        })
102    }
103
104    // Read a number off the front of the input in the given radix, stopping
105    // at the first non-digit character or eof. Fails if the number has more
106    // digits than max_digits or if there is no number.
107    fn read_number<T: ReadNumberHelper>(
108        &mut self,
109        radix: u32,
110        max_digits: Option<usize>,
111        allow_zero_prefix: bool,
112    ) -> Option<T> {
113        self.read_atomically(move |p| {
114            let mut result = T::ZERO;
115            let mut digit_count = 0;
116            let has_leading_zero = p.peek_char() == Some('0');
117
118            while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
119                result = result.checked_mul(radix)?;
120                result = result.checked_add(digit)?;
121                digit_count += 1;
122                if let Some(max_digits) = max_digits {
123                    if digit_count > max_digits {
124                        return None;
125                    }
126                }
127            }
128
129            if digit_count == 0 {
130                None
131            } else if !allow_zero_prefix && has_leading_zero && digit_count > 1 {
132                None
133            } else {
134                Some(result)
135            }
136        })
137    }
138
139    /// Read an IPv4 address.
140    fn read_ipv4_addr(&mut self) -> Option<Ipv4Addr> {
141        self.read_atomically(|p| {
142            let mut groups = [0; 4];
143
144            for (i, slot) in groups.iter_mut().enumerate() {
145                *slot = p.read_separator('.', i, |p| {
146                    // Disallow octal number in IP string.
147                    // https://tools.ietf.org/html/rfc6943#section-3.1.1
148                    p.read_number(10, Some(3), false)
149                })?;
150            }
151
152            Some(groups.into())
153        })
154    }
155
156    /// Read an IPv6 Address.
157    fn read_ipv6_addr(&mut self) -> Option<Ipv6Addr> {
158        /// Read a chunk of an IPv6 address into `groups`. Returns the number
159        /// of groups read, along with a bool indicating if an embedded
160        /// trailing IPv4 address was read. Specifically, read a series of
161        /// colon-separated IPv6 groups (0x0000 - 0xFFFF), with an optional
162        /// trailing embedded IPv4 address.
163        fn read_groups(p: &mut Parser<'_>, groups: &mut [u16]) -> (usize, bool) {
164            let limit = groups.len();
165
166            for (i, slot) in groups.iter_mut().enumerate() {
167                // Try to read a trailing embedded IPv4 address. There must be
168                // at least two groups left.
169                if i < limit - 1 {
170                    let ipv4 = p.read_separator(':', i, |p| p.read_ipv4_addr());
171
172                    if let Some(v4_addr) = ipv4 {
173                        let [one, two, three, four] = v4_addr.octets();
174                        groups[i + 0] = u16::from_be_bytes([one, two]);
175                        groups[i + 1] = u16::from_be_bytes([three, four]);
176                        return (i + 2, true);
177                    }
178                }
179
180                let group = p.read_separator(':', i, |p| p.read_number(16, Some(4), true));
181
182                match group {
183                    Some(g) => *slot = g,
184                    None => return (i, false),
185                }
186            }
187            (groups.len(), false)
188        }
189
190        self.read_atomically(|p| {
191            // Read the front part of the address; either the whole thing, or up
192            // to the first ::
193            let mut head = [0; 8];
194            let (head_size, head_ipv4) = read_groups(p, &mut head);
195
196            if head_size == 8 {
197                return Some(head.into());
198            }
199
200            // IPv4 part is not allowed before `::`
201            if head_ipv4 {
202                return None;
203            }
204
205            // Read `::` if previous code parsed less than 8 groups.
206            // `::` indicates one or more groups of 16 bits of zeros.
207            p.read_given_char(':')?;
208            p.read_given_char(':')?;
209
210            // Read the back part of the address. The :: must contain at least one
211            // set of zeroes, so our max length is 7.
212            let mut tail = [0; 7];
213            let limit = 8 - (head_size + 1);
214            let (tail_size, _) = read_groups(p, &mut tail[..limit]);
215
216            // Concat the head and tail of the IP address
217            head[(8 - tail_size)..8].copy_from_slice(&tail[..tail_size]);
218
219            Some(head.into())
220        })
221    }
222
223    /// Read an IP Address, either IPv4 or IPv6.
224    fn read_ip_addr(&mut self) -> Option<IpAddr> {
225        self.read_ipv4_addr().map(IpAddr::V4).or_else(move || self.read_ipv6_addr().map(IpAddr::V6))
226    }
227
228    /// Read a `:` followed by a port in base 10.
229    fn read_port(&mut self) -> Option<u16> {
230        self.read_atomically(|p| {
231            p.read_given_char(':')?;
232            p.read_number(10, None, true)
233        })
234    }
235
236    /// Read a `%` followed by a scope ID in base 10.
237    fn read_scope_id(&mut self) -> Option<u32> {
238        self.read_atomically(|p| {
239            p.read_given_char('%')?;
240            p.read_number(10, None, true)
241        })
242    }
243
244    /// Read an IPv4 address with a port.
245    fn read_socket_addr_v4(&mut self) -> Option<SocketAddrV4> {
246        self.read_atomically(|p| {
247            let ip = p.read_ipv4_addr()?;
248            let port = p.read_port()?;
249            Some(SocketAddrV4::new(ip, port))
250        })
251    }
252
253    /// Read an IPv6 address with a port.
254    fn read_socket_addr_v6(&mut self) -> Option<SocketAddrV6> {
255        self.read_atomically(|p| {
256            p.read_given_char('[')?;
257            let ip = p.read_ipv6_addr()?;
258            let scope_id = p.read_scope_id().unwrap_or(0);
259            p.read_given_char(']')?;
260
261            let port = p.read_port()?;
262            Some(SocketAddrV6::new(ip, port, 0, scope_id))
263        })
264    }
265
266    /// Read an IP address with a port
267    fn read_socket_addr(&mut self) -> Option<SocketAddr> {
268        self.read_socket_addr_v4()
269            .map(SocketAddr::V4)
270            .or_else(|| self.read_socket_addr_v6().map(SocketAddr::V6))
271    }
272}
273
274impl IpAddr {
275    /// Parse an IP address from a slice of bytes.
276    ///
277    /// ```
278    /// use core_net::{IpAddr, Ipv4Addr, Ipv6Addr};
279    ///
280    /// let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
281    /// let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
282    ///
283    /// assert_eq!(IpAddr::parse_ascii(b"127.0.0.1"), Ok(localhost_v4));
284    /// assert_eq!(IpAddr::parse_ascii(b"::1"), Ok(localhost_v6));
285    /// ```
286    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
287        Parser::new(b).parse_with(|p| p.read_ip_addr(), AddrKind::Ip)
288    }
289}
290
291impl FromStr for IpAddr {
292    type Err = AddrParseError;
293    fn from_str(s: &str) -> Result<IpAddr, AddrParseError> {
294        Self::parse_ascii(s.as_bytes())
295    }
296}
297
298impl Ipv4Addr {
299    /// Parse an IPv4 address from a slice of bytes.
300    ///
301    /// ```
302    /// use core_net::Ipv4Addr;
303    ///
304    /// let localhost = Ipv4Addr::new(127, 0, 0, 1);
305    ///
306    /// assert_eq!(Ipv4Addr::parse_ascii(b"127.0.0.1"), Ok(localhost));
307    /// ```
308    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
309        // don't try to parse if too long
310        if b.len() > 15 {
311            Err(AddrParseError(AddrKind::Ipv4))
312        } else {
313            Parser::new(b).parse_with(|p| p.read_ipv4_addr(), AddrKind::Ipv4)
314        }
315    }
316}
317
318impl FromStr for Ipv4Addr {
319    type Err = AddrParseError;
320    fn from_str(s: &str) -> Result<Ipv4Addr, AddrParseError> {
321        Self::parse_ascii(s.as_bytes())
322    }
323}
324
325impl Ipv6Addr {
326    /// Parse an IPv6 address from a slice of bytes.
327    ///
328    /// ```
329    /// use core_net::Ipv6Addr;
330    ///
331    /// let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
332    ///
333    /// assert_eq!(Ipv6Addr::parse_ascii(b"::1"), Ok(localhost));
334    /// ```
335    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
336        Parser::new(b).parse_with(|p| p.read_ipv6_addr(), AddrKind::Ipv6)
337    }
338}
339
340impl FromStr for Ipv6Addr {
341    type Err = AddrParseError;
342    fn from_str(s: &str) -> Result<Ipv6Addr, AddrParseError> {
343        Self::parse_ascii(s.as_bytes())
344    }
345}
346
347impl SocketAddrV4 {
348    /// Parse an IPv4 socket address from a slice of bytes.
349    ///
350    /// ```
351    /// use core_net::{Ipv4Addr, SocketAddrV4};
352    ///
353    /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
354    ///
355    /// assert_eq!(SocketAddrV4::parse_ascii(b"127.0.0.1:8080"), Ok(socket));
356    /// ```
357    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
358        Parser::new(b).parse_with(|p| p.read_socket_addr_v4(), AddrKind::SocketV4)
359    }
360}
361
362impl FromStr for SocketAddrV4 {
363    type Err = AddrParseError;
364    fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError> {
365        Self::parse_ascii(s.as_bytes())
366    }
367}
368
369impl SocketAddrV6 {
370    /// Parse an IPv6 socket address from a slice of bytes.
371    ///
372    /// ```
373    /// use core_net::{Ipv6Addr, SocketAddrV6};
374    ///
375    /// let socket = SocketAddrV6::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
376    ///
377    /// assert_eq!(SocketAddrV6::parse_ascii(b"[2001:db8::1]:8080"), Ok(socket));
378    /// ```
379    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
380        Parser::new(b).parse_with(|p| p.read_socket_addr_v6(), AddrKind::SocketV6)
381    }
382}
383
384impl FromStr for SocketAddrV6 {
385    type Err = AddrParseError;
386    fn from_str(s: &str) -> Result<SocketAddrV6, AddrParseError> {
387        Self::parse_ascii(s.as_bytes())
388    }
389}
390
391impl SocketAddr {
392    /// Parse a socket address from a slice of bytes.
393    ///
394    /// ```
395    /// use core_net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
396    ///
397    /// let socket_v4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
398    /// let socket_v6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 8080);
399    ///
400    /// assert_eq!(SocketAddr::parse_ascii(b"127.0.0.1:8080"), Ok(socket_v4));
401    /// assert_eq!(SocketAddr::parse_ascii(b"[::1]:8080"), Ok(socket_v6));
402    /// ```
403    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
404        Parser::new(b).parse_with(|p| p.read_socket_addr(), AddrKind::Socket)
405    }
406}
407
408impl FromStr for SocketAddr {
409    type Err = AddrParseError;
410    fn from_str(s: &str) -> Result<SocketAddr, AddrParseError> {
411        Self::parse_ascii(s.as_bytes())
412    }
413}
414
415#[derive(Debug, Clone, PartialEq, Eq)]
416enum AddrKind {
417    Ip,
418    Ipv4,
419    Ipv6,
420    Socket,
421    SocketV4,
422    SocketV6,
423}
424
425/// An error which can be returned when parsing an IP address or a socket address.
426///
427/// This error is used as the error type for the [`FromStr`] implementation for
428/// [`IpAddr`], [`Ipv4Addr`], [`Ipv6Addr`], [`SocketAddr`], [`SocketAddrV4`], and
429/// [`SocketAddrV6`].
430///
431/// # Potential causes
432///
433/// `AddrParseError` may be thrown because the provided string does not parse as the given type,
434/// often because it includes information only handled by a different address type.
435///
436/// ```should_panic
437/// use core_net::IpAddr;
438/// let _foo: IpAddr = "127.0.0.1:8080".parse().expect("Cannot handle the socket port");
439/// ```
440///
441/// [`IpAddr`] doesn't handle the port. Use [`SocketAddr`] instead.
442///
443/// ```
444/// use core_net::SocketAddr;
445///
446/// // No problem, the `panic!` message has disappeared.
447/// let _foo: SocketAddr = "127.0.0.1:8080".parse().expect("unreachable panic");
448/// ```
449#[derive(Debug, Clone, PartialEq, Eq)]
450pub struct AddrParseError(AddrKind);
451
452impl fmt::Display for AddrParseError {
453    #[allow(deprecated, deprecated_in_future)]
454    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
455        fmt.write_str(self.description())
456    }
457}
458
459impl Error for AddrParseError {
460    #[allow(deprecated)]
461    fn description(&self) -> &str {
462        match self.0 {
463            AddrKind::Ip => "invalid IP address syntax",
464            AddrKind::Ipv4 => "invalid IPv4 address syntax",
465            AddrKind::Ipv6 => "invalid IPv6 address syntax",
466            AddrKind::Socket => "invalid socket address syntax",
467            AddrKind::SocketV4 => "invalid IPv4 socket address syntax",
468            AddrKind::SocketV6 => "invalid IPv6 socket address syntax",
469        }
470    }
471}