2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 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 crate::ops::io::StreamResource;
|
|
|
|
use crate::ops::io::StreamResourceHolder;
|
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-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::futures;
|
|
|
|
use deno_core::futures::future::poll_fn;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::BufVec;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std::net::Shutdown;
|
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-11-16 19:17:47 -05:00
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
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)]
|
|
|
|
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);
|
|
|
|
super::reg_json_sync(rt, "op_shutdown", op_shutdown);
|
|
|
|
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-08-28 11:08:24 -04:00
|
|
|
let accept_fut = poll_fn(|cx| {
|
2020-09-10 09:57:45 -04:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let listener_resource = state
|
|
|
|
.resource_table
|
2020-08-28 11:08:24 -04:00
|
|
|
.get_mut::<TcpListenerResource>(rid)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(|| bad_resource("Listener has been closed"))?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let listener = &mut listener_resource.listener;
|
2020-09-14 12:48:57 -04:00
|
|
|
match listener.poll_accept(cx).map_err(AnyError::from) {
|
2020-08-28 11:08:24 -04:00
|
|
|
Poll::Ready(Ok((stream, addr))) => {
|
|
|
|
listener_resource.untrack_task();
|
|
|
|
Poll::Ready(Ok((stream, addr)))
|
2020-01-18 15:49:55 -05:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
Poll::Pending => {
|
|
|
|
listener_resource.track_task(cx)?;
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
Poll::Ready(Err(e)) => {
|
|
|
|
listener_resource.untrack_task();
|
|
|
|
Poll::Ready(Err(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let (tcp_stream, _socket_addr) = accept_fut.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();
|
|
|
|
let rid = state.resource_table.add(
|
2020-08-28 11:08:24 -04:00
|
|
|
"tcpStream",
|
|
|
|
Box::new(StreamResourceHolder::new(StreamResource::TcpStream(Some(
|
|
|
|
tcp_stream,
|
|
|
|
)))),
|
|
|
|
);
|
|
|
|
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-08-28 11:08:24 -04:00
|
|
|
let receive_fut = poll_fn(|cx| {
|
2020-09-10 09:57:45 -04:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let resource = state
|
|
|
|
.resource_table
|
2020-08-28 11:08:24 -04:00
|
|
|
.get_mut::<UdpSocketResource>(rid)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(|| bad_resource("Socket has been closed"))?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let socket = &mut resource.socket;
|
|
|
|
socket
|
|
|
|
.poll_recv_from(cx, &mut zero_copy)
|
2020-09-14 12:48:57 -04:00
|
|
|
.map_err(AnyError::from)
|
2020-08-28 11:08:24 -04:00
|
|
|
});
|
|
|
|
let (size, remote_addr) = receive_fut.await?;
|
|
|
|
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>()
|
|
|
|
.check_net(&args.hostname, args.port)?;
|
|
|
|
}
|
2020-06-10 11:26:28 -04:00
|
|
|
let addr = resolve_addr(&args.hostname, args.port)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
poll_fn(move |cx| {
|
2020-09-10 09:57:45 -04:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let resource = state
|
|
|
|
.resource_table
|
2020-03-23 18:02:51 -04:00
|
|
|
.get_mut::<UdpSocketResource>(rid as u32)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(|| bad_resource("Socket has been closed"))?;
|
2020-06-10 11:26:28 -04:00
|
|
|
resource
|
|
|
|
.socket
|
|
|
|
.poll_send_to(cx, &zero_copy, &addr)
|
2020-06-13 10:14:31 -04:00
|
|
|
.map_ok(|byte_length| json!(byte_length))
|
2020-09-14 12:48:57 -04:00
|
|
|
.map_err(AnyError::from)
|
2020-08-28 11:08:24 -04:00
|
|
|
})
|
|
|
|
.await
|
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();
|
|
|
|
s.borrow::<Permissions>().check_read(&address_path)?;
|
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let resource = state
|
|
|
|
.resource_table
|
2020-08-28 11:08:24 -04:00
|
|
|
.get_mut::<net_unix::UnixDatagramResource>(rid as u32)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(|| {
|
|
|
|
custom_error("NotConnected", "Socket has been closed")
|
|
|
|
})?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let socket = &mut resource.socket;
|
|
|
|
let byte_length = socket
|
|
|
|
.send_to(&zero_copy, &resource.local_addr.as_pathname().unwrap())
|
|
|
|
.await?;
|
2020-03-23 18:02:51 -04:00
|
|
|
|
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>()
|
|
|
|
.check_net(&args.hostname, args.port)?;
|
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
let addr = resolve_addr(&args.hostname, args.port)?;
|
|
|
|
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();
|
|
|
|
let rid = state_.resource_table.add(
|
2020-08-28 11:08:24 -04:00
|
|
|
"tcpStream",
|
|
|
|
Box::new(StreamResourceHolder::new(StreamResource::TcpStream(Some(
|
|
|
|
tcp_stream,
|
|
|
|
)))),
|
|
|
|
);
|
|
|
|
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-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();
|
|
|
|
let rid = state_.resource_table.add(
|
2020-08-28 11:08:24 -04:00
|
|
|
"unixStream",
|
|
|
|
Box::new(StreamResourceHolder::new(StreamResource::UnixStream(
|
|
|
|
unix_stream,
|
|
|
|
))),
|
|
|
|
);
|
|
|
|
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,
|
|
|
|
how: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_shutdown(
|
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-26 14:26:51 -04:00
|
|
|
super::check_unstable(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;
|
2019-08-26 08:50:21 -04:00
|
|
|
let how = args.how;
|
2019-09-10 00:59:40 -04:00
|
|
|
|
|
|
|
let shutdown_mode = match how {
|
|
|
|
0 => Shutdown::Read,
|
|
|
|
1 => Shutdown::Write,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
let resource_holder = state
|
|
|
|
.resource_table
|
2020-03-11 18:19:24 -04:00
|
|
|
.get_mut::<StreamResourceHolder>(rid)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
2020-03-11 18:19:24 -04:00
|
|
|
match resource_holder.resource {
|
2020-04-18 11:21:20 -04:00
|
|
|
StreamResource::TcpStream(Some(ref mut stream)) => {
|
2020-08-25 18:22:15 -04:00
|
|
|
TcpStream::shutdown(stream, shutdown_mode)?;
|
2019-11-07 11:11:15 -05:00
|
|
|
}
|
2020-03-23 18:02:51 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
StreamResource::UnixStream(ref mut stream) => {
|
2020-08-25 18:22:15 -04:00
|
|
|
net_unix::UnixStream::shutdown(stream, shutdown_mode)?;
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
2020-09-14 12:48:57 -04:00
|
|
|
_ => return Err(bad_resource_id()),
|
2019-11-07 11:11:15 -05:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-11-07 09:59:02 -05:00
|
|
|
#[allow(dead_code)]
|
|
|
|
struct TcpListenerResource {
|
2019-12-30 08:57:17 -05:00
|
|
|
listener: TcpListener,
|
2019-11-16 19:17:47 -05:00
|
|
|
waker: Option<futures::task::AtomicWaker>,
|
2019-11-07 09:59:02 -05:00
|
|
|
local_addr: SocketAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TcpListenerResource {
|
|
|
|
fn drop(&mut self) {
|
2019-11-16 19:17:47 -05:00
|
|
|
self.wake_task();
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TcpListenerResource {
|
|
|
|
/// Track the current task so future awaiting for connection
|
|
|
|
/// can be notified when listener is closed.
|
|
|
|
///
|
|
|
|
/// Throws an error if another task is already tracked.
|
2020-09-14 12:48:57 -04:00
|
|
|
pub fn track_task(&mut self, cx: &Context) -> Result<(), AnyError> {
|
2019-11-07 09:59:02 -05:00
|
|
|
// Currently, we only allow tracking a single accept task for a listener.
|
|
|
|
// This might be changed in the future with multiple workers.
|
|
|
|
// Caveat: TcpListener by itself also only tracks an accept task at a time.
|
|
|
|
// See https://github.com/tokio-rs/tokio/issues/846#issuecomment-454208883
|
2019-11-16 19:17:47 -05:00
|
|
|
if self.waker.is_some() {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(custom_error("Busy", "Another accept task is ongoing"));
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
let waker = futures::task::AtomicWaker::new();
|
|
|
|
waker.register(cx.waker());
|
|
|
|
self.waker.replace(waker);
|
2019-11-07 09:59:02 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Notifies a task when listener is closed so accept future can resolve.
|
2019-11-16 19:17:47 -05:00
|
|
|
pub fn wake_task(&mut self) {
|
|
|
|
if let Some(waker) = self.waker.as_ref() {
|
|
|
|
waker.wake();
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop tracking a task.
|
|
|
|
/// Happens when the task is done and thus no further tracking is needed.
|
|
|
|
pub fn untrack_task(&mut self) {
|
2019-11-16 19:17:47 -05:00
|
|
|
if self.waker.is_some() {
|
|
|
|
self.waker.take();
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
struct UdpSocketResource {
|
|
|
|
socket: UdpSocket,
|
|
|
|
}
|
|
|
|
|
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)?;
|
|
|
|
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 {
|
|
|
|
listener,
|
|
|
|
waker: None,
|
|
|
|
local_addr,
|
|
|
|
};
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid = state
|
|
|
|
.resource_table
|
|
|
|
.add("tcpListener", Box::new(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)?;
|
|
|
|
let socket = UdpSocket::from_std(std_socket)?;
|
2020-02-21 11:26:54 -05:00
|
|
|
let local_addr = socket.local_addr()?;
|
|
|
|
let socket_resource = UdpSocketResource { socket };
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid = state
|
|
|
|
.resource_table
|
|
|
|
.add("udpSocket", Box::new(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-09-19 19:17:35 -04:00
|
|
|
permissions.check_net(&args.hostname, args.port)?;
|
2020-04-28 15:46:39 -04:00
|
|
|
}
|
2020-04-15 23:14:28 -04:00
|
|
|
let addr = resolve_addr(&args.hostname, args.port)?;
|
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
|
|
|
}
|