2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2019-01-13 22:14:59 -05:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::net::ToSocketAddrs;
|
2020-12-15 07:02:26 -05:00
|
|
|
use tokio::net::lookup_host;
|
2019-01-13 22:14:59 -05:00
|
|
|
|
2020-12-15 07:02:26 -05:00
|
|
|
/// Resolve network address *asynchronously*.
|
|
|
|
pub async fn resolve_addr(
|
|
|
|
hostname: &str,
|
|
|
|
port: u16,
|
2020-12-29 20:19:28 -05:00
|
|
|
) -> Result<impl Iterator<Item = SocketAddr> + '_, AnyError> {
|
2020-12-15 07:02:26 -05:00
|
|
|
let addr_port_pair = make_addr_port_pair(hostname, port);
|
2020-12-29 20:19:28 -05:00
|
|
|
let result = lookup_host(addr_port_pair).await?;
|
|
|
|
Ok(result)
|
2020-12-15 07:02:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve network address *synchronously*.
|
|
|
|
pub fn resolve_addr_sync(
|
|
|
|
hostname: &str,
|
|
|
|
port: u16,
|
2020-12-29 20:19:28 -05:00
|
|
|
) -> Result<impl Iterator<Item = SocketAddr>, AnyError> {
|
2020-12-15 07:02:26 -05:00
|
|
|
let addr_port_pair = make_addr_port_pair(hostname, port);
|
2020-12-29 20:19:28 -05:00
|
|
|
let result = addr_port_pair.to_socket_addrs()?;
|
|
|
|
Ok(result)
|
2020-12-15 07:02:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_addr_port_pair(hostname: &str, port: u16) -> (&str, u16) {
|
2020-04-15 23:14:28 -04:00
|
|
|
// Default to localhost if given just the port. Example: ":80"
|
2020-12-15 07:02:26 -05:00
|
|
|
if hostname.is_empty() {
|
|
|
|
return ("0.0.0.0", port);
|
|
|
|
}
|
2019-01-13 22:14:59 -05:00
|
|
|
|
2020-04-15 23:14:28 -04:00
|
|
|
// If this looks like an ipv6 IP address. Example: "[2001:db8::1]"
|
|
|
|
// Then we remove the brackets.
|
2020-12-15 07:02:26 -05:00
|
|
|
let addr = hostname.trim_start_matches('[').trim_end_matches(']');
|
|
|
|
(addr, port)
|
2019-01-13 22:14:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::net::Ipv4Addr;
|
|
|
|
use std::net::Ipv6Addr;
|
|
|
|
use std::net::SocketAddrV4;
|
|
|
|
use std::net::SocketAddrV6;
|
|
|
|
|
2020-12-15 07:02:26 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_addr1() {
|
2020-12-29 20:19:28 -05:00
|
|
|
let expected = vec![SocketAddr::V4(SocketAddrV4::new(
|
|
|
|
Ipv4Addr::new(127, 0, 0, 1),
|
|
|
|
80,
|
|
|
|
))];
|
|
|
|
let actual = resolve_addr("127.0.0.1", 80)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.collect::<Vec<_>>();
|
2020-12-15 07:02:26 -05:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_addr2() {
|
2020-12-29 20:19:28 -05:00
|
|
|
let expected = vec![SocketAddr::V4(SocketAddrV4::new(
|
|
|
|
Ipv4Addr::new(0, 0, 0, 0),
|
|
|
|
80,
|
|
|
|
))];
|
|
|
|
let actual = resolve_addr("", 80).await.unwrap().collect::<Vec<_>>();
|
2020-12-15 07:02:26 -05:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_addr3() {
|
2020-12-29 20:19:28 -05:00
|
|
|
let expected = vec![SocketAddr::V4(SocketAddrV4::new(
|
|
|
|
Ipv4Addr::new(192, 0, 2, 1),
|
|
|
|
25,
|
|
|
|
))];
|
|
|
|
let actual = resolve_addr("192.0.2.1", 25)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.collect::<Vec<_>>();
|
2020-12-15 07:02:26 -05:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_addr_ipv6() {
|
2020-12-29 20:19:28 -05:00
|
|
|
let expected = vec![SocketAddr::V6(SocketAddrV6::new(
|
2020-12-15 07:02:26 -05:00
|
|
|
Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
|
|
|
|
8080,
|
|
|
|
0,
|
|
|
|
0,
|
2020-12-29 20:19:28 -05:00
|
|
|
))];
|
|
|
|
let actual = resolve_addr("[2001:db8::1]", 8080)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.collect::<Vec<_>>();
|
2020-12-15 07:02:26 -05:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_addr_err() {
|
|
|
|
assert!(resolve_addr("INVALID ADDR", 1234).await.is_err());
|
|
|
|
}
|
|
|
|
|
2019-01-13 22:14:59 -05:00
|
|
|
#[test]
|
2020-12-15 07:02:26 -05:00
|
|
|
fn resolve_addr_sync1() {
|
2020-12-29 20:19:28 -05:00
|
|
|
let expected = vec![SocketAddr::V4(SocketAddrV4::new(
|
|
|
|
Ipv4Addr::new(127, 0, 0, 1),
|
|
|
|
80,
|
|
|
|
))];
|
|
|
|
let actual = resolve_addr_sync("127.0.0.1", 80)
|
|
|
|
.unwrap()
|
|
|
|
.collect::<Vec<_>>();
|
2019-10-23 10:19:27 -04:00
|
|
|
assert_eq!(actual, expected);
|
2019-01-13 22:14:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-12-15 07:02:26 -05:00
|
|
|
fn resolve_addr_sync2() {
|
2020-12-29 20:19:28 -05:00
|
|
|
let expected = vec![SocketAddr::V4(SocketAddrV4::new(
|
|
|
|
Ipv4Addr::new(0, 0, 0, 0),
|
|
|
|
80,
|
|
|
|
))];
|
|
|
|
let actual = resolve_addr_sync("", 80).unwrap().collect::<Vec<_>>();
|
2019-01-13 22:14:59 -05:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-12-15 07:02:26 -05:00
|
|
|
fn resolve_addr_sync3() {
|
2020-12-29 20:19:28 -05:00
|
|
|
let expected = vec![SocketAddr::V4(SocketAddrV4::new(
|
|
|
|
Ipv4Addr::new(192, 0, 2, 1),
|
|
|
|
25,
|
|
|
|
))];
|
|
|
|
let actual = resolve_addr_sync("192.0.2.1", 25)
|
|
|
|
.unwrap()
|
|
|
|
.collect::<Vec<_>>();
|
2019-01-13 22:14:59 -05:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-12-15 07:02:26 -05:00
|
|
|
fn resolve_addr_sync_ipv6() {
|
2020-12-29 20:19:28 -05:00
|
|
|
let expected = vec![SocketAddr::V6(SocketAddrV6::new(
|
2019-01-13 22:14:59 -05:00
|
|
|
Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
|
|
|
|
8080,
|
|
|
|
0,
|
|
|
|
0,
|
2020-12-29 20:19:28 -05:00
|
|
|
))];
|
|
|
|
let actual = resolve_addr_sync("[2001:db8::1]", 8080)
|
|
|
|
.unwrap()
|
|
|
|
.collect::<Vec<_>>();
|
2019-01-13 22:14:59 -05:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
2020-12-15 07:02:26 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_addr_sync_err() {
|
|
|
|
assert!(resolve_addr_sync("INVALID ADDR", 1234).is_err());
|
|
|
|
}
|
2019-01-13 22:14:59 -05:00
|
|
|
}
|