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

use super::Transformable;

#[cfg(feature = "std")]
use crate::utils::invalid_data;

/// The wire error type for [`IpAddr`].
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum IpAddrTransformError {
  /// Returned when the buffer is too small to encode the [`IpAddr`].
  #[cfg_attr(
    feature = "std",
    error(
      "buffer is too small, use `IpAddr::encoded_len` to pre-allocate a buffer with enough space"
    )
  )]
  EncodeBufferTooSmall,
  /// Returned when the address family is unknown.
  #[cfg_attr(
    feature = "std",
    error("invalid address family: {0}, only IPv4 and IPv6 are supported")
  )]
  UnknownAddressFamily(u8),
  /// Returned when the address is corrupted.
  #[cfg_attr(feature = "std", error("{0}"))]
  NotEnoughBytes(&'static str),
}

const MIN_ENCODED_LEN: usize = TAG_SIZE + V4_SIZE;
const V6_ENCODED_LEN: usize = TAG_SIZE + V6_SIZE;
const V6_SIZE: usize = 16;
const V4_SIZE: usize = 4;
const TAG_SIZE: usize = 1;

impl Transformable for IpAddr {
  type Error = IpAddrTransformError;

  fn encode(&self, dst: &mut [u8]) -> Result<usize, Self::Error> {
    let encoded_len = self.encoded_len();
    if dst.len() < encoded_len {
      return Err(Self::Error::EncodeBufferTooSmall);
    }
    dst[0] = match self {
      IpAddr::V4(_) => 4,
      IpAddr::V6(_) => 6,
    };
    match self {
      IpAddr::V4(addr) => {
        dst[1..5].copy_from_slice(&addr.octets());
      }
      IpAddr::V6(addr) => {
        dst[1..17].copy_from_slice(&addr.octets());
      }
    }

    Ok(encoded_len)
  }

  #[cfg(feature = "std")]
  #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
  fn encode_to_writer<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<usize> {
    match self {
      IpAddr::V4(addr) => {
        let mut buf = [0u8; 7];
        buf[0] = 4;
        buf[1..5].copy_from_slice(&addr.octets());
        writer.write_all(&buf).map(|_| 7)
      }
      IpAddr::V6(addr) => {
        let mut buf = [0u8; 19];
        buf[0] = 6;
        buf[1..17].copy_from_slice(&addr.octets());
        writer.write_all(&buf).map(|_| 19)
      }
    }
  }

  #[cfg(feature = "async")]
  #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
  async fn encode_to_async_writer<W: futures_util::io::AsyncWrite + Send + Unpin>(
    &self,
    writer: &mut W,
  ) -> std::io::Result<usize> {
    use futures_util::AsyncWriteExt;

    match self {
      IpAddr::V4(addr) => {
        let mut buf = [0u8; 7];
        buf[0] = 4;
        buf[1..5].copy_from_slice(&addr.octets());
        writer.write_all(&buf).await.map(|_| 7)
      }
      IpAddr::V6(addr) => {
        let mut buf = [0u8; 19];
        buf[0] = 6;
        buf[1..17].copy_from_slice(&addr.octets());
        writer.write_all(&buf).await.map(|_| 19)
      }
    }
  }

  fn encoded_len(&self) -> usize {
    1 + match self {
      IpAddr::V4(_) => 4,
      IpAddr::V6(_) => 16,
    } + core::mem::size_of::<u16>()
  }

  fn decode(src: &[u8]) -> Result<(usize, Self), Self::Error>
  where
    Self: Sized,
  {
    match src[0] {
      4 => {
        if src.len() < 7 {
          return Err(IpAddrTransformError::NotEnoughBytes(
            "not enough bytes to decode socket v4 address",
          ));
        }

        let ip = std::net::Ipv4Addr::new(src[1], src[2], src[3], src[4]);
        Ok((MIN_ENCODED_LEN, IpAddr::from(ip)))
      }
      6 => {
        if src.len() < 19 {
          return Err(IpAddrTransformError::NotEnoughBytes(
            "not enough bytes to decode socket v6 address",
          ));
        }

        let mut buf = [0u8; 16];
        buf.copy_from_slice(&src[1..17]);
        let ip = std::net::Ipv6Addr::from(buf);
        Ok((V6_ENCODED_LEN, IpAddr::from(ip)))
      }
      val => Err(IpAddrTransformError::UnknownAddressFamily(val)),
    }
  }

  #[cfg(feature = "std")]
  #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
  fn decode_from_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<(usize, Self)>
  where
    Self: Sized,
  {
    let mut buf = [0; MIN_ENCODED_LEN];
    reader.read_exact(&mut buf)?;
    match buf[0] {
      4 => {
        let ip = Ipv4Addr::new(buf[1], buf[2], buf[3], buf[4]);
        Ok((MIN_ENCODED_LEN, IpAddr::from(ip)))
      }
      6 => {
        let mut remaining = [0; V6_ENCODED_LEN - MIN_ENCODED_LEN];
        reader.read_exact(&mut remaining)?;
        let mut ipv6 = [0; V6_SIZE];
        ipv6[..MIN_ENCODED_LEN - TAG_SIZE].copy_from_slice(&buf[TAG_SIZE..]);
        ipv6[MIN_ENCODED_LEN - TAG_SIZE..]
          .copy_from_slice(&remaining[..V6_ENCODED_LEN - MIN_ENCODED_LEN]);
        let ip = Ipv6Addr::from(ipv6);
        Ok((V6_ENCODED_LEN, IpAddr::from(ip)))
      }
      val => Err(invalid_data(IpAddrTransformError::UnknownAddressFamily(
        val,
      ))),
    }
  }

  #[cfg(feature = "async")]
  #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
  async fn decode_from_async_reader<R: futures_util::io::AsyncRead + Send + Unpin>(
    reader: &mut R,
  ) -> std::io::Result<(usize, Self)>
  where
    Self: Sized,
  {
    use futures_util::AsyncReadExt;

    let mut buf = [0; MIN_ENCODED_LEN];
    reader.read_exact(&mut buf).await?;
    match buf[0] {
      4 => {
        let ip = Ipv4Addr::new(buf[1], buf[2], buf[3], buf[4]);
        Ok((MIN_ENCODED_LEN, IpAddr::from(ip)))
      }
      6 => {
        let mut remaining = [0; V6_ENCODED_LEN - MIN_ENCODED_LEN];
        reader.read_exact(&mut remaining).await?;
        let mut ipv6 = [0; V6_SIZE];
        ipv6[..MIN_ENCODED_LEN - TAG_SIZE].copy_from_slice(&buf[TAG_SIZE..]);
        ipv6[MIN_ENCODED_LEN - TAG_SIZE..]
          .copy_from_slice(&remaining[..V6_ENCODED_LEN - MIN_ENCODED_LEN]);
        let ip = Ipv6Addr::from(ipv6);
        Ok((V6_ENCODED_LEN, IpAddr::from(ip)))
      }
      val => Err(invalid_data(IpAddrTransformError::UnknownAddressFamily(
        val,
      ))),
    }
  }
}

test_transformable!(IpAddr => test_socket_addr_v4_transformable(
  IpAddr::V4(
    Ipv4Addr::new(127, 0, 0, 1),
  )
));

test_transformable!(IpAddr => test_socket_addr_v6_transformable(
  IpAddr::V6(
    Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
  )
));