2019-01-13 22:14:59 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-07-10 18:53:48 -04:00
|
|
|
use deno::ErrBox;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
2019-01-13 22:14:59 -05:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::net::ToSocketAddrs;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2019-01-13 22:14:59 -05:00
|
|
|
|
2019-10-23 10:19:27 -04:00
|
|
|
/// Resolve network address. Returns a future.
|
|
|
|
pub fn resolve_addr(hostname: &str, port: u16) -> ResolveAddrFuture {
|
2019-01-13 22:14:59 -05:00
|
|
|
ResolveAddrFuture {
|
2019-10-23 10:19:27 -04:00
|
|
|
hostname: hostname.to_string(),
|
|
|
|
port,
|
2019-01-13 22:14:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ResolveAddrFuture {
|
2019-10-23 10:19:27 -04:00
|
|
|
hostname: String,
|
|
|
|
port: u16,
|
2019-01-13 22:14:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for ResolveAddrFuture {
|
2019-11-16 19:17:47 -05:00
|
|
|
type Output = Result<SocketAddr, ErrBox>;
|
2019-01-13 22:14:59 -05:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let inner = self.get_mut();
|
2019-01-13 22:14:59 -05:00
|
|
|
// The implementation of this is not actually async at the moment,
|
|
|
|
// however we intend to use async DNS resolution in the future and
|
|
|
|
// so we expose this as a future instead of Result.
|
|
|
|
|
|
|
|
// Default to localhost if given just the port. Example: ":80"
|
2019-11-16 19:17:47 -05:00
|
|
|
let addr: &str = if !inner.hostname.is_empty() {
|
|
|
|
&inner.hostname
|
2019-10-23 10:19:27 -04:00
|
|
|
} else {
|
|
|
|
"0.0.0.0"
|
|
|
|
};
|
|
|
|
|
2019-01-13 22:14:59 -05:00
|
|
|
// If this looks like an ipv6 IP address. Example: "[2001:db8::1]"
|
|
|
|
// Then we remove the brackets.
|
|
|
|
let addr = if addr.starts_with('[') && addr.ends_with(']') {
|
|
|
|
let l = addr.len() - 1;
|
|
|
|
addr.get(1..l).unwrap()
|
|
|
|
} else {
|
|
|
|
addr
|
|
|
|
};
|
2019-11-16 19:17:47 -05:00
|
|
|
let addr_port_pair = (addr, inner.port);
|
2019-10-23 10:19:27 -04:00
|
|
|
let r = addr_port_pair.to_socket_addrs().map_err(ErrBox::from);
|
2019-01-13 22:14:59 -05:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Ready(r.and_then(|mut iter| match iter.next() {
|
|
|
|
Some(a) => Ok(a),
|
2019-10-23 10:19:27 -04:00
|
|
|
None => panic!("There should be at least one result"),
|
2019-11-16 19:17:47 -05:00
|
|
|
}))
|
2019-10-23 10:19:27 -04:00
|
|
|
}
|
2019-01-13 22:14:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::executor::block_on;
|
2019-01-13 22:14:59 -05:00
|
|
|
use std::net::Ipv4Addr;
|
|
|
|
use std::net::Ipv6Addr;
|
|
|
|
use std::net::SocketAddrV4;
|
|
|
|
use std::net::SocketAddrV6;
|
|
|
|
|
|
|
|
#[test]
|
2019-10-23 10:19:27 -04:00
|
|
|
fn resolve_addr1() {
|
|
|
|
let expected =
|
|
|
|
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 80));
|
2019-11-16 19:17:47 -05:00
|
|
|
let actual = block_on(resolve_addr("127.0.0.1", 80)).unwrap();
|
2019-10-23 10:19:27 -04:00
|
|
|
assert_eq!(actual, expected);
|
2019-01-13 22:14:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-10-23 10:19:27 -04:00
|
|
|
fn resolve_addr2() {
|
2019-01-13 22:14:59 -05:00
|
|
|
let expected =
|
2019-10-23 10:19:27 -04:00
|
|
|
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 80));
|
2019-11-16 19:17:47 -05:00
|
|
|
let actual = block_on(resolve_addr("", 80)).unwrap();
|
2019-01-13 22:14:59 -05:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_addr3() {
|
|
|
|
let expected =
|
|
|
|
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 0, 2, 1), 25));
|
2019-11-16 19:17:47 -05:00
|
|
|
let actual = block_on(resolve_addr("192.0.2.1", 25)).unwrap();
|
2019-01-13 22:14:59 -05:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_addr_ipv6() {
|
|
|
|
let expected = SocketAddr::V6(SocketAddrV6::new(
|
|
|
|
Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
|
|
|
|
8080,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
));
|
2019-11-16 19:17:47 -05:00
|
|
|
let actual = block_on(resolve_addr("[2001:db8::1]", 8080)).unwrap();
|
2019-01-13 22:14:59 -05:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
}
|