This repository has been archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
net.rs
186 lines (167 loc) · 4.66 KB
/
net.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
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
use std::io;
use std::net::{IpAddr, SocketAddr};
use futures::prelude::*;
use tokio::net;
use crate::endpoint::{Endpoint, ToEndpoint};
use crate::resolver::{CpuPoolResolver, Resolver};
lazy_static::lazy_static! {
static ref POOL: CpuPoolResolver = CpuPoolResolver::new(5);
}
/// Resolve a hostname to a sequence of ip addresses using the default resolver.
///
/// # Example
/// ```
/// tokio_dns::resolve_ip_addr("rust-lang.org");
/// ```
pub async fn resolve_ip_addr(host: &str) -> io::Result<Vec<IpAddr>> {
POOL.resolve(host).await
}
/// Resolve a hostname to a sequence of ip addresses using a custom resolver.
///
/// # Example
/// ```
/// # use tokio_dns::CpuPoolResolver;
/// let resolver = CpuPoolResolver::new(10);
///
/// tokio_dns::resolve_ip_addr_with("rust-lang.org", resolver.clone());
/// ```
pub async fn resolve_ip_addr_with<R>(host: &str, resolver: R) -> io::Result<Vec<IpAddr>>
where
R: Resolver,
{
resolver.resolve(host).await
}
/// Resolve an endpoint to a sequence of socket addresses using the default resolver.
///
/// # Example
/// ```
/// tokio_dns::resolve_sock_addr(("rust-lang.org", 80));
/// ```
pub async fn resolve_sock_addr<'a, T>(endpoint: T) -> io::Result<Vec<SocketAddr>>
where
T: ToEndpoint<'a>,
{
resolve_endpoint(endpoint, POOL.clone()).await
}
/// Resolve an endpoint to a sequence of socket addresses using a custom resolver.
///
/// # Example
/// ```
/// # use tokio_dns::CpuPoolResolver;
/// let resolver = CpuPoolResolver::new(10);
///
/// tokio_dns::resolve_sock_addr_with(("rust-lang.org", 80), resolver.clone());
/// ```
pub async fn resolve_sock_addr_with<'a, T, R>(
endpoint: T,
resolver: R,
) -> io::Result<Vec<SocketAddr>>
where
T: ToEndpoint<'a>,
R: Resolver,
{
resolve_endpoint(endpoint, resolver).await
}
/// Shim for tokio::net::TcpStream
pub struct TcpStream;
impl TcpStream {
/// Connect to the endpoint using the default resolver.
pub async fn connect<'a, T>(ep: T) -> io::Result<net::TcpStream>
where
T: ToEndpoint<'a>,
{
TcpStream::connect_with(ep, POOL.clone()).await
}
/// Connect to the endpoint using a custom resolver.
pub async fn connect_with<'a, T, R>(ep: T, resolver: R) -> io::Result<net::TcpStream>
where
T: ToEndpoint<'a>,
R: Resolver,
{
try_until_ok(
resolve_endpoint(ep, resolver).await?,
net::TcpStream::connect,
)
.await
}
}
/// Shim for tokio::net::TcpListener
pub struct TcpListener;
impl TcpListener {
/// Bind to the endpoint using the default resolver.
pub async fn bind<'a, T>(ep: T) -> io::Result<net::TcpListener>
where
T: ToEndpoint<'a>,
{
TcpListener::bind_with(ep, POOL.clone()).await
}
/// Bind to the endpoint using a custom resolver.
pub async fn bind_with<'a, T, R>(ep: T, resolver: R) -> io::Result<net::TcpListener>
where
T: ToEndpoint<'a>,
R: Resolver,
{
try_until_ok(
resolve_endpoint(ep, resolver).await?,
net::TcpListener::bind,
)
.await
}
}
/// Shim for tokio::net::UdpSocket
pub struct UdpSocket;
impl UdpSocket {
/// Bind to the endpoint using the default resolver.
pub async fn bind<'a, T>(ep: T) -> io::Result<net::UdpSocket>
where
T: ToEndpoint<'a>,
{
UdpSocket::bind_with(ep, POOL.clone()).await
}
/// Bind to the endpoint using a custom resolver.
pub async fn bind_with<'a, T, R>(ep: T, resolver: R) -> io::Result<net::UdpSocket>
where
T: ToEndpoint<'a>,
R: Resolver,
{
try_until_ok(resolve_endpoint(ep, resolver).await?, net::UdpSocket::bind).await
}
}
/// Resolves endpoint into a vector of socket addresses.
async fn resolve_endpoint<'a, T, R>(ep: T, resolver: R) -> io::Result<Vec<SocketAddr>>
where
R: Resolver,
T: ToEndpoint<'a>,
{
let ep = match ep.to_endpoint() {
Ok(ep) => ep,
Err(e) => return Err(e),
};
Ok({
match ep {
Endpoint::Host(host, port) => resolver
.resolve(host)
.await?
.into_iter()
.map(|addr| SocketAddr::new(addr, port))
.collect(),
Endpoint::SocketAddr(addr) => vec![addr],
}
})
}
async fn try_until_ok<F, R, I>(addrs: Vec<SocketAddr>, f: F) -> io::Result<I>
where
F: Fn(SocketAddr) -> R + Send + 'static,
R: Future<Output = io::Result<I>> + Send + 'static,
I: Send + 'static,
{
for addr in addrs {
if let Ok(i) = f(addr).await {
return Ok(i);
}
}
Err(io::Error::new(
io::ErrorKind::Other,
"could not resolve to any address",
))
}