forky_tun/
address.rs

1//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2//                    Version 2, December 2004
3//
4// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
5//
6// Everyone is permitted to copy and distribute verbatim or modified
7// copies of this license document, and changing it is allowed as long
8// as the name is changed.
9//
10//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12//
13//  0. You just DO WHAT THE FUCK YOU WANT TO.
14
15use std::net::{IpAddr, Ipv4Addr};
16use std::net::{SocketAddr, SocketAddrV4};
17
18use crate::error::*;
19
20/// Helper trait to convert things into IPv4 addresses.
21#[allow(clippy::wrong_self_convention)]
22pub trait IntoAddress {
23    /// Convert the type to an `Ipv4Addr`.
24    fn into_address(&self) -> Result<Ipv4Addr>;
25}
26
27impl IntoAddress for u32 {
28    fn into_address(&self) -> Result<Ipv4Addr> {
29        Ok(Ipv4Addr::new(
30            ((*self) & 0xff) as u8,
31            ((*self >> 8) & 0xff) as u8,
32            ((*self >> 16) & 0xff) as u8,
33            ((*self >> 24) & 0xff) as u8,
34        ))
35    }
36}
37
38impl IntoAddress for i32 {
39    fn into_address(&self) -> Result<Ipv4Addr> {
40        (*self as u32).into_address()
41    }
42}
43
44impl IntoAddress for (u8, u8, u8, u8) {
45    fn into_address(&self) -> Result<Ipv4Addr> {
46        Ok(Ipv4Addr::new(self.0, self.1, self.2, self.3))
47    }
48}
49
50impl IntoAddress for str {
51    fn into_address(&self) -> Result<Ipv4Addr> {
52        self.parse().map_err(|_| Error::InvalidAddress)
53    }
54}
55
56impl<'a> IntoAddress for &'a str {
57    fn into_address(&self) -> Result<Ipv4Addr> {
58        (*self).into_address()
59    }
60}
61
62impl IntoAddress for String {
63    fn into_address(&self) -> Result<Ipv4Addr> {
64        self.as_str().into_address()
65    }
66}
67
68impl<'a> IntoAddress for &'a String {
69    fn into_address(&self) -> Result<Ipv4Addr> {
70        self.as_str().into_address()
71    }
72}
73
74impl IntoAddress for Ipv4Addr {
75    fn into_address(&self) -> Result<Ipv4Addr> {
76        Ok(*self)
77    }
78}
79
80impl<'a> IntoAddress for &'a Ipv4Addr {
81    fn into_address(&self) -> Result<Ipv4Addr> {
82        (*self).into_address()
83    }
84}
85
86impl IntoAddress for IpAddr {
87    fn into_address(&self) -> Result<Ipv4Addr> {
88        match self {
89            IpAddr::V4(ref value) => Ok(*value),
90
91            IpAddr::V6(_) => Err(Error::InvalidAddress),
92        }
93    }
94}
95
96impl<'a> IntoAddress for &'a IpAddr {
97    fn into_address(&self) -> Result<Ipv4Addr> {
98        (*self).into_address()
99    }
100}
101
102impl IntoAddress for SocketAddrV4 {
103    fn into_address(&self) -> Result<Ipv4Addr> {
104        Ok(*self.ip())
105    }
106}
107
108impl<'a> IntoAddress for &'a SocketAddrV4 {
109    fn into_address(&self) -> Result<Ipv4Addr> {
110        (*self).into_address()
111    }
112}
113
114impl IntoAddress for SocketAddr {
115    fn into_address(&self) -> Result<Ipv4Addr> {
116        match self {
117            SocketAddr::V4(ref value) => Ok(*value.ip()),
118
119            SocketAddr::V6(_) => Err(Error::InvalidAddress),
120        }
121    }
122}
123
124impl<'a> IntoAddress for &'a SocketAddr {
125    fn into_address(&self) -> Result<Ipv4Addr> {
126        (*self).into_address()
127    }
128}