proxy_header/
util.rs

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
use std::{
    net::{IpAddr, Ipv4Addr, Ipv6Addr},
    str::FromStr,
};

pub fn read_until(buf: &[u8], delim: u8) -> Option<&[u8]> {
    for i in 0..buf.len() {
        if buf[i] == delim {
            return Some(&buf[..i]);
        }
    }
    None
}

pub trait AddressFamily: FromStr {
    const BYTES: usize;

    fn to_ip_addr(self) -> IpAddr;
    fn from_slice(slice: &[u8]) -> Self;
}

impl AddressFamily for Ipv4Addr {
    const BYTES: usize = 4;

    fn to_ip_addr(self) -> IpAddr {
        IpAddr::V4(self)
    }

    fn from_slice(slice: &[u8]) -> Self {
        let arr: [u8; 4] = slice.try_into().expect("slice must be 4 bytes");
        arr.into()
    }
}

impl AddressFamily for Ipv6Addr {
    const BYTES: usize = 16;

    fn to_ip_addr(self) -> IpAddr {
        IpAddr::V6(self)
    }

    fn from_slice(slice: &[u8]) -> Self {
        let arr: [u8; 16] = slice.try_into().expect("slice must be 16 bytes");
        arr.into()
    }
}

macro_rules! tlv {
    ($self:expr, $kind:ident) => {{
        $self.tlvs().find_map(|f| match f {
            Ok(crate::Tlv::$kind(v)) => Some(v),
            _ => None,
        })
    }};
}

macro_rules! tlv_borrowed {
    ($self:expr, $kind:ident) => {{
        $self.tlvs().find_map(|f| match f {
            Ok(crate::Tlv::$kind(v)) => match v {
                // It is more ergonomic to return the borrowed value directly rather
                // than it wrapped in a `Cow::Borrowed`. We know that tlvs always borrows
                // so we can safely unwrap the `Cow::Borrowed` and return the borrowed value.
                Cow::Owned(_) => unreachable!(),
                Cow::Borrowed(v) => Some(v),
            },
            _ => None,
        })
    }};
}

pub(crate) use {tlv, tlv_borrowed};