summaryrefslogtreecommitdiffhomepage
path: root/src/net.rs
blob: fb119ea8afd8262e9d9547f2929bde13b7c99071 (plain)
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
use std::fmt;
use std::io;
use std::iter;
use std::net::SocketAddr;
use std::option;
use std::slice;
use std::str::FromStr;
use std::vec;


error_chain! {
    errors {
        /// Error indicating parsing the address failed
        AddrParse(s: String) {
            description("Invalid address format")
            display("Unable to parse address. {}", s)
        }
    }
}


/// Representation of a TCP or UDP endpoint. The IP level address is represented by either an IP
/// directly or a hostname/domain. The IP level address together with a port becomes a socket
/// address.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RemoteAddr {
    /// Endpoint represented by an IP and a port.
    SocketAddr(SocketAddr),
    /// Endpoint represented by a hostname or domain and a port.
    Domain(String, u16),
}

impl RemoteAddr {
    /// Constructs a new `RemoteAddr` from the given address (hostname or domain) and port. To
    /// construct a `RemoteAddr` based on IP rather than domain, use the From<SocketAddr> impl.
    pub fn new(address: &str, port: u16) -> Self {
        RemoteAddr::Domain(address.to_owned(), port)
    }

    /// Returns the address associated with this `RemoteAddr`. If it is backed by an IP that will
    /// be formatted as a string.
    pub fn address(&self) -> String {
        match *self {
            RemoteAddr::SocketAddr(ref addr) => addr.ip().to_string(),
            RemoteAddr::Domain(ref address, _) => address.to_owned(),
        }
    }

    /// Returns the port associated with this `RemoteAddr`.
    pub fn port(&self) -> u16 {
        match *self {
            RemoteAddr::SocketAddr(addr) => addr.port(),
            RemoteAddr::Domain(_, port) => port,
        }
    }

    fn from_domain_str(s: &str) -> Result<Self> {
        let (address, port_str) = Self::split_at_last_colon(s)?;
        let port = u16::from_str(port_str).chain_err(|| {
            ErrorKind::AddrParse(format!("Invalid port: \"{}\"", port_str))
        })?;
        if address.is_empty() || address.contains(':') {
            let msg = format!("Invalid IP or domain: \"{}\"", address);
            return Err(ErrorKind::AddrParse(msg).into());
        }
        Ok(RemoteAddr::Domain(address.to_owned(), port))
    }

    fn split_at_last_colon(s: &str) -> Result<(&str, &str)> {
        let mut iter = s.rsplitn(2, ':');
        let port = iter.next().unwrap();
        let address = iter.next()
            .ok_or_else(|| Error::from(ErrorKind::AddrParse("No colon".to_owned())))?;
        Ok((address, port))
    }
}

impl From<SocketAddr> for RemoteAddr {
    fn from(socket_addr: SocketAddr) -> Self {
        RemoteAddr::SocketAddr(socket_addr)
    }
}

impl FromStr for RemoteAddr {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self> {
        if let Ok(addr) = SocketAddr::from_str(s) {
            Ok(RemoteAddr::from(addr))
        } else {
            Self::from_domain_str(s)
        }
    }
}

impl fmt::Display for RemoteAddr {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            RemoteAddr::SocketAddr(ref addr) => addr.fmt(fmt),
            RemoteAddr::Domain(ref address, ref port) => write!(fmt, "{}:{}", address, port),
        }
    }
}

/// A trait for objects which can be converted to one or more `RemoteAddr` values.
pub trait ToRemoteAddrs {
    /// Returned iterator over remote addresses which this type may correspond
    /// to.
    type Iter: Iterator<Item = RemoteAddr>;

    /// Converts this object to an iterator of parsed `RemoteAddr`s.
    ///
    /// # Errors
    ///
    /// Any errors encountered during parsing will be returned as an `Err`.
    fn to_remote_addrs(&self) -> io::Result<Self::Iter>;
}

impl ToRemoteAddrs for RemoteAddr {
    type Iter = option::IntoIter<RemoteAddr>;

    fn to_remote_addrs(&self) -> io::Result<Self::Iter> {
        Ok(Some(self.clone()).into_iter())
    }
}

impl<'a> ToRemoteAddrs for &'a [RemoteAddr] {
    type Iter = iter::Cloned<slice::Iter<'a, RemoteAddr>>;

    fn to_remote_addrs(&self) -> io::Result<Self::Iter> {
        Ok(self.iter().cloned())
    }
}

impl<'a> ToRemoteAddrs for &'a str {
    type Iter = option::IntoIter<RemoteAddr>;

    fn to_remote_addrs(&self) -> io::Result<Self::Iter> {
        let parsed_addr = str_to_remote_addr(self)?;
        Ok(Some(parsed_addr).into_iter())
    }
}

impl<'a> ToRemoteAddrs for &'a [&'a str] {
    type Iter = vec::IntoIter<RemoteAddr>;

    fn to_remote_addrs(&self) -> io::Result<Self::Iter> {
        let mut addrs = Vec::with_capacity(self.len());
        for addr in self.iter() {
            addrs.push(str_to_remote_addr(addr)?);
        }
        Ok(addrs.into_iter())
    }
}

fn str_to_remote_addr(s: &str) -> io::Result<RemoteAddr> {
    RemoteAddr::from_str(s)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e.description()))
}



#[cfg(test)]
mod remote_addr_tests {
    use super::*;

    use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
    use std::str::FromStr;

    #[test]
    fn new_and_getters() {
        let testee = RemoteAddr::new("a_domain", 543);
        assert_eq!("a_domain", testee.address());
        assert_eq!(543, testee.port());
    }

    #[test]
    fn from_socket_addr() {
        let socket_addr = SocketAddr::from_str("10.0.1.1:76").unwrap();
        let testee: RemoteAddr = socket_addr.into();
        assert_eq!("10.0.1.1", testee.address());
        assert_eq!(76, testee.port());
    }

    #[test]
    fn from_str() {
        let testee = RemoteAddr::from_str("example.com:3333").unwrap();
        assert_eq!("example.com", testee.address());
        assert_eq!(3333, testee.port());
    }

    #[test]
    fn from_ipv6_str_without_brackets() {
        let result = RemoteAddr::from_str("fe80::1:1337");
        assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _)));
    }

    #[test]
    fn from_ipv6_str_with_brackets() {
        let testee = RemoteAddr::from_str("[fe80::1]:1337").unwrap();
        assert_eq!("fe80::1", testee.address());
        assert_eq!(1337, testee.port());
    }

    #[test]
    fn from_ipv6_str_without_port() {
        let result = RemoteAddr::from_str("fe80::1");
        assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _)));
    }

    #[test]
    fn from_str_no_colon() {
        let result = RemoteAddr::from_str("example.com");
        assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _)));
    }

    #[test]
    fn from_str_invalid_port_large() {
        let result = RemoteAddr::from_str("example.com:99999");
        assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _)));
    }

    #[test]
    fn from_str_empty_address() {
        let result = RemoteAddr::from_str(":100");
        assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _)));
    }

    #[test]
    fn from_str_empty_port() {
        let result = RemoteAddr::from_str("example.com:");
        assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _)));
    }

    #[test]
    fn to_string_domain() {
        let testee = RemoteAddr::new("example.com", 3333);
        assert_eq!("example.com:3333", testee.to_string());
    }

    #[test]
    fn to_string_ipv4() {
        let socket_addr = SocketAddr::V4(SocketAddrV4::from_str("127.1.2.3:1337").unwrap());
        let testee = RemoteAddr::from(socket_addr);
        assert_eq!("127.1.2.3:1337", testee.to_string());
    }

    #[test]
    fn to_string_ipv6() {
        let socket_addr = SocketAddr::V6(SocketAddrV6::from_str("[2001:beef::1]:9876").unwrap());
        let testee = RemoteAddr::from(socket_addr);
        assert_eq!("[2001:beef::1]:9876", testee.to_string());
    }
}