2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-06-28 19:43:03 -04:00
|
|
|
|
|
|
|
use crate::io::TcpStreamResource;
|
2019-08-21 20:42:48 -04:00
|
|
|
use crate::resolve_addr::resolve_addr;
|
2020-12-15 07:02:26 -05:00
|
|
|
use crate::resolve_addr::resolve_addr_sync;
|
2021-06-28 19:43:03 -04:00
|
|
|
use crate::NetPermissions;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::bad_resource;
|
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::op_async;
|
|
|
|
use deno_core::op_sync;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
2021-11-26 13:59:53 -05:00
|
|
|
use deno_core::ByteString;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::CancelHandle;
|
|
|
|
use deno_core::CancelTryFuture;
|
2021-06-28 19:43:03 -04:00
|
|
|
use deno_core::OpPair;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2021-01-19 09:39:04 -05:00
|
|
|
use serde::Serialize;
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2019-11-07 09:59:02 -05:00
|
|
|
use std::net::SocketAddr;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2019-08-14 11:03:02 -04:00
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use tokio::net::TcpStream;
|
2020-02-21 11:26:54 -05:00
|
|
|
use tokio::net::UdpSocket;
|
2021-01-19 09:39:04 -05:00
|
|
|
use trust_dns_proto::rr::record_data::RData;
|
|
|
|
use trust_dns_proto::rr::record_type::RecordType;
|
|
|
|
use trust_dns_resolver::config::NameServerConfigGroup;
|
|
|
|
use trust_dns_resolver::config::ResolverConfig;
|
|
|
|
use trust_dns_resolver::config::ResolverOpts;
|
2021-10-10 06:14:45 -04:00
|
|
|
use trust_dns_resolver::error::ResolveErrorKind;
|
2021-01-19 09:39:04 -05:00
|
|
|
use trust_dns_resolver::system_conf;
|
|
|
|
use trust_dns_resolver::AsyncResolver;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
#[cfg(unix)]
|
2021-06-28 19:43:03 -04:00
|
|
|
use super::ops_unix as net_unix;
|
2020-09-14 12:48:57 -04:00
|
|
|
#[cfg(unix)]
|
2021-06-28 19:43:03 -04:00
|
|
|
use crate::io::UnixStreamResource;
|
2020-12-16 11:14:12 -05:00
|
|
|
#[cfg(unix)]
|
2020-09-14 12:48:57 -04:00
|
|
|
use std::path::Path;
|
2020-03-23 18:02:51 -04:00
|
|
|
|
2021-06-28 19:43:03 -04:00
|
|
|
pub fn init<P: NetPermissions + 'static>() -> Vec<OpPair> {
|
|
|
|
vec![
|
2021-10-30 12:51:42 -04:00
|
|
|
("op_net_accept", op_async(op_net_accept)),
|
|
|
|
("op_net_connect", op_async(op_net_connect::<P>)),
|
|
|
|
("op_net_listen", op_sync(op_net_listen::<P>)),
|
|
|
|
("op_dgram_recv", op_async(op_dgram_recv)),
|
|
|
|
("op_dgram_send", op_async(op_dgram_send::<P>)),
|
2021-06-28 19:43:03 -04:00
|
|
|
("op_dns_resolve", op_async(op_dns_resolve::<P>)),
|
|
|
|
]
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2021-04-06 04:33:43 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct OpConn {
|
|
|
|
pub rid: ResourceId,
|
|
|
|
pub remote_addr: Option<OpAddr>,
|
|
|
|
pub local_addr: Option<OpAddr>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(tag = "transport", rename_all = "lowercase")]
|
|
|
|
pub enum OpAddr {
|
|
|
|
Tcp(IpAddr),
|
|
|
|
Udp(IpAddr),
|
|
|
|
#[cfg(unix)]
|
|
|
|
Unix(net_unix::UnixAddr),
|
|
|
|
#[cfg(unix)]
|
|
|
|
UnixPacket(net_unix::UnixAddr),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
/// A received datagram packet (from udp or unixpacket)
|
|
|
|
pub struct OpPacket {
|
|
|
|
pub size: usize,
|
|
|
|
pub remote_addr: OpAddr,
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:59:53 -05:00
|
|
|
#[derive(Serialize, Clone, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct TlsHandshakeInfo {
|
|
|
|
pub alpn_protocol: Option<ByteString>,
|
|
|
|
}
|
|
|
|
|
2021-04-06 04:33:43 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct IpAddr {
|
|
|
|
pub hostname: String,
|
|
|
|
pub port: u16,
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
2020-09-05 20:34:02 -04:00
|
|
|
pub(crate) struct AcceptArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
pub rid: ResourceId,
|
2020-09-05 20:34:02 -04:00
|
|
|
pub transport: String,
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2021-12-14 17:27:04 -05:00
|
|
|
pub(crate) fn accept_err(e: std::io::Error) -> AnyError {
|
|
|
|
// FIXME(bartlomieju): compatibility with current JS implementation
|
|
|
|
if let std::io::ErrorKind::Interrupted = e.kind() {
|
|
|
|
bad_resource("Listener has been closed")
|
|
|
|
} else {
|
|
|
|
e.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn accept_tcp(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2020-03-23 18:02:51 -04:00
|
|
|
args: AcceptArgs,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpConn, AnyError> {
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2020-02-27 15:08:21 -05:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
|
|
|
.get::<TcpListenerResource>(rid)
|
2021-08-15 07:29:19 -04:00
|
|
|
.map_err(|_| bad_resource("Listener has been closed"))?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let listener = RcRef::map(&resource, |r| &r.listener)
|
2020-12-16 11:14:12 -05:00
|
|
|
.try_borrow_mut()
|
|
|
|
.ok_or_else(|| custom_error("Busy", "Another accept task is ongoing"))?;
|
|
|
|
let cancel = RcRef::map(resource, |r| &r.cancel);
|
2021-12-14 17:27:04 -05:00
|
|
|
let (tcp_stream, _socket_addr) = listener
|
|
|
|
.accept()
|
|
|
|
.try_or_cancel(cancel)
|
|
|
|
.await
|
|
|
|
.map_err(accept_err)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
|
|
|
let mut state = state.borrow_mut();
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state
|
|
|
|
.resource_table
|
|
|
|
.add(TcpStreamResource::new(tcp_stream.into_split()));
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(OpConn {
|
|
|
|
rid,
|
|
|
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: local_addr.ip().to_string(),
|
|
|
|
port: local_addr.port(),
|
|
|
|
})),
|
|
|
|
remote_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: remote_addr.ip().to_string(),
|
|
|
|
port: remote_addr.port(),
|
|
|
|
})),
|
|
|
|
})
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2021-10-30 12:51:42 -04:00
|
|
|
async fn op_net_accept(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
args: AcceptArgs,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpConn, AnyError> {
|
2020-03-23 18:02:51 -04:00
|
|
|
match args.transport.as_str() {
|
2021-05-08 08:37:42 -04:00
|
|
|
"tcp" => accept_tcp(state, args, ()).await,
|
2020-03-23 18:02:51 -04:00
|
|
|
#[cfg(unix)]
|
2021-05-08 08:37:42 -04:00
|
|
|
"unix" => net_unix::accept_unix(state, args, ()).await,
|
2021-04-06 04:33:43 -04:00
|
|
|
other => Err(bad_transport(other)),
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 04:33:43 -04:00
|
|
|
fn bad_transport(transport: &str) -> AnyError {
|
|
|
|
generic_error(format!("Unsupported transport protocol {}", transport))
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
#[derive(Deserialize)]
|
2020-09-05 20:34:02 -04:00
|
|
|
pub(crate) struct ReceiveArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
pub rid: ResourceId,
|
2020-09-05 20:34:02 -04:00
|
|
|
pub transport: String,
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn receive_udp(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2020-03-23 18:02:51 -04:00
|
|
|
args: ReceiveArgs,
|
2021-10-05 16:38:27 -04:00
|
|
|
zero_copy: ZeroCopyBuf,
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpPacket, AnyError> {
|
2021-04-02 09:47:57 -04:00
|
|
|
let mut zero_copy = zero_copy.clone();
|
2020-02-21 11:26:54 -05:00
|
|
|
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2020-02-21 11:26:54 -05:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<UdpSocketResource>(rid)
|
2021-08-15 07:29:19 -04:00
|
|
|
.map_err(|_| bad_resource("Socket has been closed"))?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let socket = RcRef::map(&resource, |r| &r.socket).borrow().await;
|
|
|
|
let cancel_handle = RcRef::map(&resource, |r| &r.cancel);
|
|
|
|
let (size, remote_addr) = socket
|
2020-12-16 11:14:12 -05:00
|
|
|
.recv_from(&mut zero_copy)
|
2021-01-12 02:50:02 -05:00
|
|
|
.try_or_cancel(cancel_handle)
|
2020-12-16 11:14:12 -05:00
|
|
|
.await?;
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(OpPacket {
|
|
|
|
size,
|
|
|
|
remote_addr: OpAddr::Udp(IpAddr {
|
|
|
|
hostname: remote_addr.ip().to_string(),
|
|
|
|
port: remote_addr.port(),
|
|
|
|
}),
|
|
|
|
})
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2021-10-30 12:51:42 -04:00
|
|
|
async fn op_dgram_recv(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
args: ReceiveArgs,
|
2021-10-05 16:38:27 -04:00
|
|
|
zero_copy: ZeroCopyBuf,
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpPacket, AnyError> {
|
2020-03-23 18:02:51 -04:00
|
|
|
match args.transport.as_str() {
|
2020-09-05 20:34:02 -04:00
|
|
|
"udp" => receive_udp(state, args, zero_copy).await,
|
2020-03-23 18:02:51 -04:00
|
|
|
#[cfg(unix)]
|
2020-09-05 20:34:02 -04:00
|
|
|
"unixpacket" => net_unix::receive_unix_packet(state, args, zero_copy).await,
|
2021-04-06 04:33:43 -04:00
|
|
|
other => Err(bad_transport(other)),
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SendArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
rid: ResourceId,
|
2020-02-21 11:26:54 -05:00
|
|
|
transport: String,
|
2020-03-23 18:02:51 -04:00
|
|
|
#[serde(flatten)]
|
|
|
|
transport_args: ArgsEnum,
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2021-10-30 12:51:42 -04:00
|
|
|
async fn op_dgram_send<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
args: SendArgs,
|
2021-10-05 16:38:27 -04:00
|
|
|
zero_copy: ZeroCopyBuf,
|
2021-06-28 19:43:03 -04:00
|
|
|
) -> Result<usize, AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2021-04-02 09:47:57 -04:00
|
|
|
let zero_copy = zero_copy.clone();
|
2020-06-01 14:20:47 -04:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
match args {
|
2020-03-23 18:02:51 -04:00
|
|
|
SendArgs {
|
|
|
|
rid,
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Ip(args),
|
|
|
|
} if transport == "udp" => {
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut s = state.borrow_mut();
|
2021-06-28 19:43:03 -04:00
|
|
|
s.borrow_mut::<NP>()
|
|
|
|
.check_net(&(&args.hostname, Some(args.port)))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-12-29 20:19:28 -05:00
|
|
|
let addr = resolve_addr(&args.hostname, args.port)
|
|
|
|
.await?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
2020-12-16 11:14:12 -05:00
|
|
|
|
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2021-03-18 20:55:31 -04:00
|
|
|
.get::<UdpSocketResource>(rid)
|
2021-08-15 07:29:19 -04:00
|
|
|
.map_err(|_| bad_resource("Socket has been closed"))?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let socket = RcRef::map(&resource, |r| &r.socket).borrow().await;
|
|
|
|
let byte_length = socket.send_to(&zero_copy, &addr).await?;
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(byte_length)
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
SendArgs {
|
|
|
|
rid,
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Unix(args),
|
|
|
|
} if transport == "unixpacket" => {
|
2020-09-14 12:48:57 -04:00
|
|
|
let address_path = Path::new(&args.path);
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut s = state.borrow_mut();
|
2021-07-30 09:03:41 -04:00
|
|
|
s.borrow_mut::<NP>().check_write(address_path)?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
let resource = state
|
2020-12-16 11:14:12 -05:00
|
|
|
.borrow()
|
2020-09-10 09:57:45 -04:00
|
|
|
.resource_table
|
2021-03-18 20:55:31 -04:00
|
|
|
.get::<net_unix::UnixDatagramResource>(rid)
|
2021-08-15 07:29:19 -04:00
|
|
|
.map_err(|_| custom_error("NotConnected", "Socket has been closed"))?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let socket = RcRef::map(&resource, |r| &r.socket)
|
2020-12-16 11:14:12 -05:00
|
|
|
.try_borrow_mut()
|
|
|
|
.ok_or_else(|| custom_error("Busy", "Socket already in use"))?;
|
|
|
|
let byte_length = socket.send_to(&zero_copy, address_path).await?;
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(byte_length)
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
2020-09-14 12:48:57 -04:00
|
|
|
_ => Err(type_error("Wrong argument format!")),
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
2021-09-20 10:05:23 -04:00
|
|
|
pub struct ConnectArgs {
|
2019-09-20 18:32:18 -04:00
|
|
|
transport: String,
|
2020-03-23 18:02:51 -04:00
|
|
|
#[serde(flatten)]
|
|
|
|
transport_args: ArgsEnum,
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2021-10-30 12:51:42 -04:00
|
|
|
pub async fn op_net_connect<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
args: ConnectArgs,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-06-28 19:43:03 -04:00
|
|
|
) -> Result<OpConn, AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2021-04-05 12:40:24 -04:00
|
|
|
match args {
|
2020-03-23 18:02:51 -04:00
|
|
|
ConnectArgs {
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Ip(args),
|
|
|
|
} if transport == "tcp" => {
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state_ = state.borrow_mut();
|
2020-09-19 19:17:35 -04:00
|
|
|
state_
|
2021-06-28 19:43:03 -04:00
|
|
|
.borrow_mut::<NP>()
|
|
|
|
.check_net(&(&args.hostname, Some(args.port)))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-12-29 20:19:28 -05:00
|
|
|
let addr = resolve_addr(&args.hostname, args.port)
|
|
|
|
.await?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let tcp_stream = TcpStream::connect(&addr).await?;
|
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
|
|
|
let mut state_ = state.borrow_mut();
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state_
|
|
|
|
.resource_table
|
|
|
|
.add(TcpStreamResource::new(tcp_stream.into_split()));
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(OpConn {
|
|
|
|
rid,
|
|
|
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: local_addr.ip().to_string(),
|
|
|
|
port: local_addr.port(),
|
|
|
|
})),
|
|
|
|
remote_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: remote_addr.ip().to_string(),
|
|
|
|
port: remote_addr.port(),
|
|
|
|
})),
|
|
|
|
})
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
ConnectArgs {
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Unix(args),
|
|
|
|
} if transport == "unix" => {
|
2020-09-14 12:48:57 -04:00
|
|
|
let address_path = Path::new(&args.path);
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable2(&state, "Deno.connect");
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state_ = state.borrow_mut();
|
2021-07-30 09:03:41 -04:00
|
|
|
state_.borrow_mut::<NP>().check_read(address_path)?;
|
|
|
|
state_.borrow_mut::<NP>().check_write(address_path)?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
let path = args.path;
|
2020-09-14 12:48:57 -04:00
|
|
|
let unix_stream = net_unix::UnixStream::connect(Path::new(&path)).await?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let local_addr = unix_stream.local_addr()?;
|
|
|
|
let remote_addr = unix_stream.peer_addr()?;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
|
|
|
let mut state_ = state.borrow_mut();
|
2021-01-14 23:32:27 -05:00
|
|
|
let resource = UnixStreamResource::new(unix_stream.into_split());
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state_.resource_table.add(resource);
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(OpConn {
|
|
|
|
rid,
|
|
|
|
local_addr: Some(OpAddr::Unix(net_unix::UnixAddr {
|
|
|
|
path: local_addr.as_pathname().and_then(net_unix::pathstring),
|
|
|
|
})),
|
|
|
|
remote_addr: Some(OpAddr::Unix(net_unix::UnixAddr {
|
|
|
|
path: remote_addr.as_pathname().and_then(net_unix::pathstring),
|
|
|
|
})),
|
|
|
|
})
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
2020-09-14 12:48:57 -04:00
|
|
|
_ => Err(type_error("Wrong argument format!")),
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2021-04-08 18:34:15 -04:00
|
|
|
pub struct TcpListenerResource {
|
|
|
|
pub listener: AsyncRefCell<TcpListener>,
|
|
|
|
pub cancel: CancelHandle,
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
impl Resource for TcpListenerResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"tcpListener".into()
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel();
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
2020-12-16 11:14:12 -05:00
|
|
|
}
|
2019-11-07 09:59:02 -05:00
|
|
|
|
2021-01-12 02:50:02 -05:00
|
|
|
struct UdpSocketResource {
|
|
|
|
socket: AsyncRefCell<UdpSocket>,
|
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
2019-11-07 09:59:02 -05:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
impl Resource for UdpSocketResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"udpSocket".into()
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
fn close(self: Rc<Self>) {
|
2021-01-12 02:50:02 -05:00
|
|
|
self.cancel.cancel()
|
2020-12-16 11:14:12 -05:00
|
|
|
}
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct IpListenArgs {
|
|
|
|
hostname: String,
|
|
|
|
port: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum ArgsEnum {
|
|
|
|
Ip(IpListenArgs),
|
|
|
|
#[cfg(unix)]
|
|
|
|
Unix(net_unix::UnixListenArgs),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ListenArgs {
|
|
|
|
transport: String,
|
|
|
|
#[serde(flatten)]
|
|
|
|
transport_args: ArgsEnum,
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
fn listen_tcp(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-02-21 11:26:54 -05:00
|
|
|
addr: SocketAddr,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(u32, SocketAddr), AnyError> {
|
2020-04-17 12:45:34 -04:00
|
|
|
let std_listener = std::net::TcpListener::bind(&addr)?;
|
2021-01-12 02:50:02 -05:00
|
|
|
std_listener.set_nonblocking(true)?;
|
2020-04-17 12:45:34 -04:00
|
|
|
let listener = TcpListener::from_std(std_listener)?;
|
2020-02-21 11:26:54 -05:00
|
|
|
let local_addr = listener.local_addr()?;
|
|
|
|
let listener_resource = TcpListenerResource {
|
2020-12-16 11:14:12 -05:00
|
|
|
listener: AsyncRefCell::new(listener),
|
|
|
|
cancel: Default::default(),
|
2020-02-21 11:26:54 -05:00
|
|
|
};
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(listener_resource);
|
2020-02-21 11:26:54 -05:00
|
|
|
|
|
|
|
Ok((rid, local_addr))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn listen_udp(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-02-21 11:26:54 -05:00
|
|
|
addr: SocketAddr,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(u32, SocketAddr), AnyError> {
|
2020-04-17 12:45:34 -04:00
|
|
|
let std_socket = std::net::UdpSocket::bind(&addr)?;
|
2021-01-12 02:50:02 -05:00
|
|
|
std_socket.set_nonblocking(true)?;
|
2021-11-29 05:14:46 -05:00
|
|
|
// Enable messages to be sent to the broadcast address (255.255.255.255) by default
|
|
|
|
std_socket.set_broadcast(true)?;
|
2020-04-17 12:45:34 -04:00
|
|
|
let socket = UdpSocket::from_std(std_socket)?;
|
2020-02-21 11:26:54 -05:00
|
|
|
let local_addr = socket.local_addr()?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let socket_resource = UdpSocketResource {
|
|
|
|
socket: AsyncRefCell::new(socket),
|
|
|
|
cancel: Default::default(),
|
|
|
|
};
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(socket_resource);
|
2020-02-21 11:26:54 -05:00
|
|
|
|
|
|
|
Ok((rid, local_addr))
|
|
|
|
}
|
|
|
|
|
2021-10-30 12:51:42 -04:00
|
|
|
fn op_net_listen<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
args: ListenArgs,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-06-28 19:43:03 -04:00
|
|
|
) -> Result<OpConn, AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2021-04-05 12:40:24 -04:00
|
|
|
match args {
|
2020-03-23 18:02:51 -04:00
|
|
|
ListenArgs {
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Ip(args),
|
|
|
|
} => {
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
|
|
|
if transport == "udp" {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.listenDatagram");
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2021-04-11 22:15:43 -04:00
|
|
|
state
|
2021-06-28 19:43:03 -04:00
|
|
|
.borrow_mut::<NP>()
|
|
|
|
.check_net(&(&args.hostname, Some(args.port)))?;
|
2020-04-28 15:46:39 -04:00
|
|
|
}
|
2020-12-29 20:19:28 -05:00
|
|
|
let addr = resolve_addr_sync(&args.hostname, args.port)?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
2020-03-23 18:02:51 -04:00
|
|
|
let (rid, local_addr) = if transport == "tcp" {
|
2020-09-05 20:34:02 -04:00
|
|
|
listen_tcp(state, addr)?
|
2020-03-23 18:02:51 -04:00
|
|
|
} else {
|
2020-09-05 20:34:02 -04:00
|
|
|
listen_udp(state, addr)?
|
2020-03-23 18:02:51 -04:00
|
|
|
};
|
|
|
|
debug!(
|
|
|
|
"New listener {} {}:{}",
|
|
|
|
rid,
|
|
|
|
local_addr.ip().to_string(),
|
|
|
|
local_addr.port()
|
|
|
|
);
|
2021-04-06 04:33:43 -04:00
|
|
|
let ip_addr = IpAddr {
|
|
|
|
hostname: local_addr.ip().to_string(),
|
|
|
|
port: local_addr.port(),
|
|
|
|
};
|
|
|
|
Ok(OpConn {
|
|
|
|
rid,
|
|
|
|
local_addr: Some(match transport.as_str() {
|
|
|
|
"udp" => OpAddr::Udp(ip_addr),
|
|
|
|
"tcp" => OpAddr::Tcp(ip_addr),
|
|
|
|
// NOTE: This could be unreachable!()
|
|
|
|
other => return Err(bad_transport(other)),
|
|
|
|
}),
|
|
|
|
remote_addr: None,
|
|
|
|
})
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
ListenArgs {
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Unix(args),
|
|
|
|
} if transport == "unix" || transport == "unixpacket" => {
|
2020-09-14 12:48:57 -04:00
|
|
|
let address_path = Path::new(&args.path);
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
|
|
|
if transport == "unix" {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.listen");
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
|
|
|
if transport == "unixpacket" {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.listenDatagram");
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2021-06-28 19:43:03 -04:00
|
|
|
let permissions = state.borrow_mut::<NP>();
|
2021-07-30 09:03:41 -04:00
|
|
|
permissions.check_read(address_path)?;
|
|
|
|
permissions.check_write(address_path)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2020-03-23 18:02:51 -04:00
|
|
|
let (rid, local_addr) = if transport == "unix" {
|
2021-07-30 09:03:41 -04:00
|
|
|
net_unix::listen_unix(state, address_path)?
|
2020-03-23 18:02:51 -04:00
|
|
|
} else {
|
2021-07-30 09:03:41 -04:00
|
|
|
net_unix::listen_unix_packet(state, address_path)?
|
2020-03-23 18:02:51 -04:00
|
|
|
};
|
2021-10-02 13:09:59 -04:00
|
|
|
debug!("New listener {} {:?}", rid, local_addr);
|
2021-04-06 04:33:43 -04:00
|
|
|
let unix_addr = net_unix::UnixAddr {
|
|
|
|
path: local_addr.as_pathname().and_then(net_unix::pathstring),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(OpConn {
|
|
|
|
rid,
|
|
|
|
local_addr: Some(match transport.as_str() {
|
|
|
|
"unix" => OpAddr::Unix(unix_addr),
|
|
|
|
"unixpacket" => OpAddr::UnixPacket(unix_addr),
|
|
|
|
other => return Err(bad_transport(other)),
|
|
|
|
}),
|
|
|
|
remote_addr: None,
|
|
|
|
})
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
2020-09-14 12:48:57 -04:00
|
|
|
_ => Err(type_error("Wrong argument format!")),
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
|
|
|
|
#[derive(Serialize, PartialEq, Debug)]
|
|
|
|
#[serde(untagged)]
|
2021-10-10 06:16:11 -04:00
|
|
|
pub enum DnsReturnRecord {
|
2021-01-19 09:39:04 -05:00
|
|
|
A(String),
|
2021-03-25 14:17:37 -04:00
|
|
|
Aaaa(String),
|
|
|
|
Aname(String),
|
|
|
|
Cname(String),
|
|
|
|
Mx {
|
2021-01-19 09:39:04 -05:00
|
|
|
preference: u16,
|
|
|
|
exchange: String,
|
|
|
|
},
|
2021-03-25 14:17:37 -04:00
|
|
|
Ptr(String),
|
|
|
|
Srv {
|
2021-01-19 09:39:04 -05:00
|
|
|
priority: u16,
|
|
|
|
weight: u16,
|
|
|
|
port: u16,
|
|
|
|
target: String,
|
|
|
|
},
|
2021-03-25 14:17:37 -04:00
|
|
|
Txt(Vec<String>),
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ResolveAddrArgs {
|
|
|
|
query: String,
|
|
|
|
record_type: RecordType,
|
|
|
|
options: Option<ResolveDnsOption>,
|
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ResolveDnsOption {
|
|
|
|
name_server: Option<NameServer>,
|
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
fn default_port() -> u16 {
|
|
|
|
53
|
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct NameServer {
|
|
|
|
ip_addr: String,
|
|
|
|
#[serde(default = "default_port")]
|
|
|
|
port: u16,
|
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
|
2021-10-10 06:16:11 -04:00
|
|
|
pub async fn op_dns_resolve<NP>(
|
2021-03-18 14:42:01 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
args: ResolveAddrArgs,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-06-28 19:43:03 -04:00
|
|
|
) -> Result<Vec<DnsReturnRecord>, AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2021-01-19 09:39:04 -05:00
|
|
|
let ResolveAddrArgs {
|
|
|
|
query,
|
|
|
|
record_type,
|
|
|
|
options,
|
2021-03-18 14:42:01 -04:00
|
|
|
} = args;
|
2021-01-19 09:39:04 -05:00
|
|
|
|
|
|
|
let (config, opts) = if let Some(name_server) =
|
|
|
|
options.as_ref().and_then(|o| o.name_server.as_ref())
|
|
|
|
{
|
|
|
|
let group = NameServerConfigGroup::from_ips_clear(
|
|
|
|
&[name_server.ip_addr.parse()?],
|
|
|
|
name_server.port,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
(
|
|
|
|
ResolverConfig::from_parts(None, vec![], group),
|
|
|
|
ResolverOpts::default(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
system_conf::read_system_conf()?
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut s = state.borrow_mut();
|
2021-06-28 19:43:03 -04:00
|
|
|
let perm = s.borrow_mut::<NP>();
|
2021-01-19 09:39:04 -05:00
|
|
|
|
|
|
|
// Checks permission against the name servers which will be actually queried.
|
|
|
|
for ns in config.name_servers() {
|
|
|
|
let socker_addr = &ns.socket_addr;
|
|
|
|
let ip = socker_addr.ip().to_string();
|
|
|
|
let port = socker_addr.port();
|
2021-06-28 19:43:03 -04:00
|
|
|
perm.check_net(&(ip, Some(port)))?;
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let resolver = AsyncResolver::tokio(config, opts)?;
|
|
|
|
|
2021-04-06 04:33:43 -04:00
|
|
|
let results = resolver
|
2021-01-19 09:39:04 -05:00
|
|
|
.lookup(query, record_type, Default::default())
|
2021-01-20 09:47:17 -05:00
|
|
|
.await
|
2021-10-10 06:14:45 -04:00
|
|
|
.map_err(|e| {
|
|
|
|
let message = format!("{}", e);
|
|
|
|
match e.kind() {
|
|
|
|
ResolveErrorKind::NoRecordsFound { .. } => {
|
|
|
|
custom_error("NotFound", message)
|
|
|
|
}
|
|
|
|
ResolveErrorKind::Message("No connections available") => {
|
|
|
|
custom_error("NotConnected", message)
|
|
|
|
}
|
|
|
|
ResolveErrorKind::Timeout => custom_error("TimedOut", message),
|
|
|
|
_ => generic_error(message),
|
|
|
|
}
|
|
|
|
})?
|
2021-01-19 09:39:04 -05:00
|
|
|
.iter()
|
|
|
|
.filter_map(rdata_to_return_record(record_type))
|
|
|
|
.collect();
|
|
|
|
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(results)
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn rdata_to_return_record(
|
|
|
|
ty: RecordType,
|
|
|
|
) -> impl Fn(&RData) -> Option<DnsReturnRecord> {
|
|
|
|
use RecordType::*;
|
|
|
|
move |r: &RData| -> Option<DnsReturnRecord> {
|
|
|
|
match ty {
|
|
|
|
A => r.as_a().map(ToString::to_string).map(DnsReturnRecord::A),
|
|
|
|
AAAA => r
|
|
|
|
.as_aaaa()
|
|
|
|
.map(ToString::to_string)
|
2021-03-25 14:17:37 -04:00
|
|
|
.map(DnsReturnRecord::Aaaa),
|
2021-01-19 09:39:04 -05:00
|
|
|
ANAME => r
|
|
|
|
.as_aname()
|
|
|
|
.map(ToString::to_string)
|
2021-03-25 14:17:37 -04:00
|
|
|
.map(DnsReturnRecord::Aname),
|
2021-01-19 09:39:04 -05:00
|
|
|
CNAME => r
|
|
|
|
.as_cname()
|
|
|
|
.map(ToString::to_string)
|
2021-03-25 14:17:37 -04:00
|
|
|
.map(DnsReturnRecord::Cname),
|
|
|
|
MX => r.as_mx().map(|mx| DnsReturnRecord::Mx {
|
2021-01-19 09:39:04 -05:00
|
|
|
preference: mx.preference(),
|
|
|
|
exchange: mx.exchange().to_string(),
|
|
|
|
}),
|
|
|
|
PTR => r
|
|
|
|
.as_ptr()
|
|
|
|
.map(ToString::to_string)
|
2021-03-25 14:17:37 -04:00
|
|
|
.map(DnsReturnRecord::Ptr),
|
|
|
|
SRV => r.as_srv().map(|srv| DnsReturnRecord::Srv {
|
2021-01-19 09:39:04 -05:00
|
|
|
priority: srv.priority(),
|
|
|
|
weight: srv.weight(),
|
|
|
|
port: srv.port(),
|
|
|
|
target: srv.target().to_string(),
|
|
|
|
}),
|
|
|
|
TXT => r.as_txt().map(|txt| {
|
|
|
|
let texts: Vec<String> = txt
|
|
|
|
.iter()
|
|
|
|
.map(|bytes| {
|
|
|
|
// Tries to parse these bytes as Latin-1
|
|
|
|
bytes.iter().map(|&b| b as char).collect::<String>()
|
|
|
|
})
|
|
|
|
.collect();
|
2021-03-25 14:17:37 -04:00
|
|
|
DnsReturnRecord::Txt(texts)
|
2021-01-19 09:39:04 -05:00
|
|
|
}),
|
|
|
|
// TODO(magurotuna): Other record types are not supported
|
|
|
|
_ => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::net::Ipv4Addr;
|
|
|
|
use std::net::Ipv6Addr;
|
|
|
|
use trust_dns_proto::rr::rdata::mx::MX;
|
|
|
|
use trust_dns_proto::rr::rdata::srv::SRV;
|
|
|
|
use trust_dns_proto::rr::rdata::txt::TXT;
|
|
|
|
use trust_dns_proto::rr::record_data::RData;
|
|
|
|
use trust_dns_proto::rr::Name;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_a() {
|
|
|
|
let func = rdata_to_return_record(RecordType::A);
|
|
|
|
let rdata = RData::A(Ipv4Addr::new(127, 0, 0, 1));
|
|
|
|
assert_eq!(
|
|
|
|
func(&rdata),
|
|
|
|
Some(DnsReturnRecord::A("127.0.0.1".to_string()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_aaaa() {
|
|
|
|
let func = rdata_to_return_record(RecordType::AAAA);
|
|
|
|
let rdata = RData::AAAA(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
|
2021-03-25 14:17:37 -04:00
|
|
|
assert_eq!(func(&rdata), Some(DnsReturnRecord::Aaaa("::1".to_string())));
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_aname() {
|
|
|
|
let func = rdata_to_return_record(RecordType::ANAME);
|
|
|
|
let rdata = RData::ANAME(Name::new());
|
2021-03-25 14:17:37 -04:00
|
|
|
assert_eq!(func(&rdata), Some(DnsReturnRecord::Aname("".to_string())));
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_cname() {
|
|
|
|
let func = rdata_to_return_record(RecordType::CNAME);
|
|
|
|
let rdata = RData::CNAME(Name::new());
|
2021-03-25 14:17:37 -04:00
|
|
|
assert_eq!(func(&rdata), Some(DnsReturnRecord::Cname("".to_string())));
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_mx() {
|
|
|
|
let func = rdata_to_return_record(RecordType::MX);
|
|
|
|
let rdata = RData::MX(MX::new(10, Name::new()));
|
|
|
|
assert_eq!(
|
|
|
|
func(&rdata),
|
2021-03-25 14:17:37 -04:00
|
|
|
Some(DnsReturnRecord::Mx {
|
2021-01-19 09:39:04 -05:00
|
|
|
preference: 10,
|
|
|
|
exchange: "".to_string()
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_ptr() {
|
|
|
|
let func = rdata_to_return_record(RecordType::PTR);
|
|
|
|
let rdata = RData::PTR(Name::new());
|
2021-03-25 14:17:37 -04:00
|
|
|
assert_eq!(func(&rdata), Some(DnsReturnRecord::Ptr("".to_string())));
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_srv() {
|
|
|
|
let func = rdata_to_return_record(RecordType::SRV);
|
|
|
|
let rdata = RData::SRV(SRV::new(1, 2, 3, Name::new()));
|
|
|
|
assert_eq!(
|
|
|
|
func(&rdata),
|
2021-03-25 14:17:37 -04:00
|
|
|
Some(DnsReturnRecord::Srv {
|
2021-01-19 09:39:04 -05:00
|
|
|
priority: 1,
|
|
|
|
weight: 2,
|
|
|
|
port: 3,
|
|
|
|
target: "".to_string()
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_txt() {
|
|
|
|
let func = rdata_to_return_record(RecordType::TXT);
|
|
|
|
let rdata = RData::TXT(TXT::from_bytes(vec![
|
|
|
|
"foo".as_bytes(),
|
|
|
|
"bar".as_bytes(),
|
|
|
|
&[0xa3], // "£" in Latin-1
|
|
|
|
&[0xe3, 0x81, 0x82], // "あ" in UTF-8
|
|
|
|
]));
|
|
|
|
assert_eq!(
|
|
|
|
func(&rdata),
|
2021-03-25 14:17:37 -04:00
|
|
|
Some(DnsReturnRecord::Txt(vec![
|
2021-01-19 09:39:04 -05:00
|
|
|
"foo".to_string(),
|
|
|
|
"bar".to_string(),
|
|
|
|
"£".to_string(),
|
|
|
|
"ã\u{81}\u{82}".to_string(),
|
|
|
|
]))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|