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-12-16 11:14:12 -05:00
|
|
|
use crate::ops::io::TcpStreamResource;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
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;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::bad_resource;
|
|
|
|
use deno_core::error::bad_resource_id;
|
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::BufVec;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::CancelHandle;
|
|
|
|
use deno_core::CancelTryFuture;
|
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;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
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;
|
2021-01-12 02:50:02 -05:00
|
|
|
use tokio::io::AsyncWriteExt;
|
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;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
use super::net_unix;
|
2020-09-14 12:48:57 -04:00
|
|
|
#[cfg(unix)]
|
2020-12-16 11:14:12 -05:00
|
|
|
use crate::ops::io::StreamResource;
|
|
|
|
#[cfg(unix)]
|
2020-09-14 12:48:57 -04:00
|
|
|
use std::path::Path;
|
2020-03-23 18:02:51 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_async(rt, "op_accept", op_accept);
|
|
|
|
super::reg_json_async(rt, "op_connect", op_connect);
|
2020-12-16 11:14:12 -05:00
|
|
|
super::reg_json_async(rt, "op_shutdown", op_shutdown);
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_listen", op_listen);
|
|
|
|
super::reg_json_async(rt, "op_datagram_receive", op_datagram_receive);
|
|
|
|
super::reg_json_async(rt, "op_datagram_send", op_datagram_send);
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
2020-09-05 20:34:02 -04:00
|
|
|
pub(crate) struct AcceptArgs {
|
|
|
|
pub rid: i32,
|
|
|
|
pub transport: String,
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
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,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-11-07 09:59:02 -05:00
|
|
|
let rid = args.rid as u32;
|
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)
|
|
|
|
.ok_or_else(|| 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);
|
|
|
|
let (tcp_stream, _socket_addr) =
|
|
|
|
listener.accept().try_or_cancel(cancel).await.map_err(|e| {
|
|
|
|
// 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
|
|
|
}
|
2020-12-16 11:14:12 -05:00
|
|
|
})?;
|
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()));
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
|
|
|
"rid": rid,
|
|
|
|
"localAddr": {
|
|
|
|
"hostname": local_addr.ip().to_string(),
|
|
|
|
"port": local_addr.port(),
|
|
|
|
"transport": "tcp",
|
|
|
|
},
|
|
|
|
"remoteAddr": {
|
|
|
|
"hostname": remote_addr.ip().to_string(),
|
|
|
|
"port": remote_addr.port(),
|
|
|
|
"transport": "tcp",
|
|
|
|
}
|
|
|
|
}))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_accept(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2020-03-23 18:02:51 -04:00
|
|
|
args: Value,
|
2020-09-05 20:34:02 -04:00
|
|
|
bufs: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-03-23 18:02:51 -04:00
|
|
|
let args: AcceptArgs = serde_json::from_value(args)?;
|
|
|
|
match args.transport.as_str() {
|
2020-09-05 20:34:02 -04:00
|
|
|
"tcp" => accept_tcp(state, args, bufs).await,
|
2020-03-23 18:02:51 -04:00
|
|
|
#[cfg(unix)]
|
2020-09-05 20:34:02 -04:00
|
|
|
"unix" => net_unix::accept_unix(state, args, bufs).await,
|
2020-09-14 12:48:57 -04:00
|
|
|
_ => Err(generic_error(format!(
|
2020-03-23 18:02:51 -04:00
|
|
|
"Unsupported transport protocol {}",
|
|
|
|
args.transport
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
#[derive(Deserialize)]
|
2020-09-05 20:34:02 -04:00
|
|
|
pub(crate) struct ReceiveArgs {
|
|
|
|
pub rid: i32,
|
|
|
|
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,
|
2020-08-28 11:08:24 -04:00
|
|
|
zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-06-01 14:20:47 -04:00
|
|
|
assert_eq!(zero_copy.len(), 1, "Invalid number of arguments");
|
|
|
|
let mut zero_copy = zero_copy[0].clone();
|
2020-02-21 11:26:54 -05:00
|
|
|
|
|
|
|
let rid = args.rid as u32;
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<UdpSocketResource>(rid)
|
|
|
|
.ok_or_else(|| 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?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
|
|
|
"size": size,
|
|
|
|
"remoteAddr": {
|
|
|
|
"hostname": remote_addr.ip().to_string(),
|
|
|
|
"port": remote_addr.port(),
|
|
|
|
"transport": "udp",
|
|
|
|
}
|
|
|
|
}))
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_datagram_receive(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2020-03-23 18:02:51 -04:00
|
|
|
args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-06-01 14:20:47 -04:00
|
|
|
assert_eq!(zero_copy.len(), 1, "Invalid number of arguments");
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
let args: ReceiveArgs = serde_json::from_value(args)?;
|
|
|
|
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,
|
2020-09-14 12:48:57 -04:00
|
|
|
_ => Err(generic_error(format!(
|
2020-03-23 18:02:51 -04:00
|
|
|
"Unsupported transport protocol {}",
|
|
|
|
args.transport
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SendArgs {
|
|
|
|
rid: i32,
|
|
|
|
transport: String,
|
2020-03-23 18:02:51 -04:00
|
|
|
#[serde(flatten)]
|
|
|
|
transport_args: ArgsEnum,
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_datagram_send(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2020-02-21 11:26:54 -05:00
|
|
|
args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-06-01 14:20:47 -04:00
|
|
|
assert_eq!(zero_copy.len(), 1, "Invalid number of arguments");
|
|
|
|
let zero_copy = zero_copy[0].clone();
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
match serde_json::from_value(args)? {
|
|
|
|
SendArgs {
|
|
|
|
rid,
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Ip(args),
|
|
|
|
} if transport == "udp" => {
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let s = state.borrow();
|
|
|
|
s.borrow::<Permissions>()
|
2020-12-30 17:35:28 -05:00
|
|
|
.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
|
|
|
|
.get::<UdpSocketResource>(rid as u32)
|
|
|
|
.ok_or_else(|| 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?;
|
2020-12-16 11:14:12 -05:00
|
|
|
Ok(json!(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
|
|
|
{
|
|
|
|
let s = state.borrow();
|
2020-11-27 13:43:39 -05:00
|
|
|
s.borrow::<Permissions>().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
|
2020-12-16 11:14:12 -05:00
|
|
|
.get::<net_unix::UnixDatagramResource>(rid as u32)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(|| {
|
|
|
|
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?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(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)]
|
2020-01-18 12:35:12 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_connect(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-03-23 18:02:51 -04:00
|
|
|
match serde_json::from_value(args)? {
|
|
|
|
ConnectArgs {
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Ip(args),
|
|
|
|
} if transport == "tcp" => {
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state_ = state.borrow();
|
|
|
|
state_
|
|
|
|
.borrow::<Permissions>()
|
2020-12-30 17:35:28 -05:00
|
|
|
.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()));
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
|
|
|
"rid": rid,
|
|
|
|
"localAddr": {
|
|
|
|
"hostname": local_addr.ip().to_string(),
|
|
|
|
"port": local_addr.port(),
|
|
|
|
"transport": transport,
|
|
|
|
},
|
|
|
|
"remoteAddr": {
|
|
|
|
"hostname": remote_addr.ip().to_string(),
|
|
|
|
"port": remote_addr.port(),
|
|
|
|
"transport": transport,
|
|
|
|
}
|
|
|
|
}))
|
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
|
|
|
{
|
|
|
|
let state_ = state.borrow();
|
|
|
|
state_.borrow::<Permissions>().check_read(&address_path)?;
|
2020-11-27 13:43:39 -05:00
|
|
|
state_.borrow::<Permissions>().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();
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = StreamResource::unix_stream(unix_stream);
|
|
|
|
let rid = state_.resource_table.add(resource);
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
|
|
|
"rid": rid,
|
|
|
|
"localAddr": {
|
|
|
|
"path": local_addr.as_pathname(),
|
|
|
|
"transport": transport,
|
|
|
|
},
|
|
|
|
"remoteAddr": {
|
|
|
|
"path": remote_addr.as_pathname(),
|
|
|
|
"transport": transport,
|
|
|
|
}
|
|
|
|
}))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ShutdownArgs {
|
|
|
|
rid: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
async fn op_shutdown(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-12-16 11:14:12 -05:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-12-16 11:14:12 -05:00
|
|
|
super::check_unstable2(&state, "Deno.shutdown");
|
2020-04-28 01:36:47 -04:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: ShutdownArgs = serde_json::from_value(args)?;
|
|
|
|
|
2019-09-10 00:59:40 -04:00
|
|
|
let rid = args.rid as u32;
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow()
|
2020-09-10 09:57:45 -04:00
|
|
|
.resource_table
|
2020-12-16 11:14:12 -05:00
|
|
|
.get_any(rid)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
2020-12-16 11:14:12 -05:00
|
|
|
if let Some(stream) = resource.downcast_rc::<TcpStreamResource>() {
|
2021-01-12 02:50:02 -05:00
|
|
|
let mut wr = stream.wr_borrow_mut().await;
|
|
|
|
wr.shutdown().await?;
|
2020-12-16 11:14:12 -05:00
|
|
|
return Ok(json!({}));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
if let Some(stream) = resource.downcast_rc::<StreamResource>() {
|
|
|
|
if stream.unix_stream.is_some() {
|
2021-01-12 02:50:02 -05:00
|
|
|
let mut wr = RcRef::map(stream, |r| r.unix_stream.as_ref().unwrap())
|
2020-12-16 11:14:12 -05:00
|
|
|
.borrow_mut()
|
|
|
|
.await;
|
2021-01-12 02:50:02 -05:00
|
|
|
wr.shutdown().await?;
|
2020-12-16 11:14:12 -05:00
|
|
|
return Ok(json!({}));
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
2019-11-07 11:11:15 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
Err(bad_resource_id())
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-11-07 09:59:02 -05:00
|
|
|
struct TcpListenerResource {
|
2020-12-16 11:14:12 -05:00
|
|
|
listener: AsyncRefCell<TcpListener>,
|
|
|
|
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)?;
|
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))
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_listen(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = state.borrow::<Permissions>();
|
2020-03-23 18:02:51 -04:00
|
|
|
match serde_json::from_value(args)? {
|
|
|
|
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
|
|
|
}
|
2020-12-30 17:35:28 -05:00
|
|
|
permissions.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()
|
|
|
|
);
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
2020-03-23 18:02:51 -04:00
|
|
|
"rid": rid,
|
|
|
|
"localAddr": {
|
|
|
|
"hostname": local_addr.ip().to_string(),
|
|
|
|
"port": local_addr.port(),
|
|
|
|
"transport": transport,
|
|
|
|
},
|
2020-08-28 11:08:24 -04:00
|
|
|
}))
|
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
|
|
|
}
|
2020-09-19 19:17:35 -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" {
|
2020-09-05 20:34:02 -04:00
|
|
|
net_unix::listen_unix(state, &address_path)?
|
2020-03-23 18:02:51 -04:00
|
|
|
} else {
|
2020-09-05 20:34:02 -04:00
|
|
|
net_unix::listen_unix_packet(state, &address_path)?
|
2020-03-23 18:02:51 -04:00
|
|
|
};
|
|
|
|
debug!(
|
|
|
|
"New listener {} {}",
|
|
|
|
rid,
|
|
|
|
local_addr.as_pathname().unwrap().display(),
|
|
|
|
);
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
2020-03-23 18:02:51 -04:00
|
|
|
"rid": rid,
|
|
|
|
"localAddr": {
|
2020-04-28 12:37:59 -04:00
|
|
|
"path": local_addr.as_pathname(),
|
2020-03-23 18:02:51 -04:00
|
|
|
"transport": transport,
|
|
|
|
},
|
2020-08-28 11:08:24 -04:00
|
|
|
}))
|
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
|
|
|
}
|