forked from linkerd/linkerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.rs
144 lines (122 loc) · 3.42 KB
/
connect.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
use futures::Future;
use tokio_connect;
use tokio_core::reactor::Handle;
use std::io;
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;
use http;
use connection;
use dns;
#[derive(Debug, Clone)]
pub struct Connect {
addr: SocketAddr,
handle: Handle,
}
#[derive(Clone, Debug)]
pub struct HostAndPort {
pub host: Host,
pub port: u16,
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct DnsNameAndPort {
pub host: dns::Name,
pub port: u16,
}
#[derive(Clone, Debug)]
pub enum Host {
DnsName(dns::Name),
Ip(IpAddr),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum HostAndPortError {
/// The host is not a valid DNS name or IP address.
InvalidHost,
/// The port is missing.
MissingPort,
}
#[derive(Debug, Clone)]
pub struct LookupAddressAndConnect {
host_and_port: HostAndPort,
dns_resolver: dns::Resolver,
handle: Handle,
}
// ===== impl HostAndPort =====
impl HostAndPort {
pub fn normalize(a: &http::uri::Authority, default_port: Option<u16>)
-> Result<Self, HostAndPortError>
{
let host = IpAddr::from_str(a.host())
.map(Host::Ip)
.unwrap_or_else(|_| Host::DnsName(dns::Name::from(a.host())));
let port = a.port()
.or(default_port)
.ok_or_else(|| HostAndPortError::MissingPort)?;
Ok(HostAndPort {
host,
port
})
}
}
impl<'a> From<&'a HostAndPort> for http::uri::Authority {
fn from(a: &HostAndPort) -> Self {
let s = match a.host {
Host::DnsName(ref n) => format!("{}:{}", n, a.port),
Host::Ip(ref ip) => format!("{}:{}", ip, a.port),
};
http::uri::Authority::from_str(&s).unwrap()
}
}
// ===== impl Connect =====
impl Connect {
/// Returns a `Connect` to `addr` and `handle`.
pub fn new(addr: SocketAddr, handle: &Handle) -> Self {
Self {
addr,
handle: handle.clone(),
}
}
}
impl tokio_connect::Connect for Connect {
type Connected = connection::Connection;
type Error = io::Error;
type Future = connection::Connecting;
fn connect(&self) -> Self::Future {
connection::connect(&self.addr, &self.handle)
}
}
// ===== impl LookupAddressAndConnect =====
impl LookupAddressAndConnect {
pub fn new(
host_and_port: HostAndPort,
dns_resolver: dns::Resolver,
handle: &Handle,
) -> Self {
Self {
host_and_port,
dns_resolver,
handle: handle.clone(),
}
}
}
impl tokio_connect::Connect for LookupAddressAndConnect {
type Connected = connection::Connection;
type Error = io::Error;
type Future = Box<Future<Item = connection::Connection, Error = io::Error>>;
fn connect(&self) -> Self::Future {
let port = self.host_and_port.port;
let handle = self.handle.clone();
let host = self.host_and_port.host.clone();
let c = self.dns_resolver
.resolve_one_ip(&self.host_and_port.host)
.map_err(|_| {
io::Error::new(io::ErrorKind::NotFound, "DNS resolution failed")
})
.and_then(move |ip_addr: IpAddr| {
info!("DNS resolved {:?} to {}", host, ip_addr);
let addr = SocketAddr::from((ip_addr, port));
trace!("connect {}", addr);
connection::connect(&addr, &handle)
});
Box::new(c)
}
}