2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2019-11-14 12:10:25 -05:00
|
|
|
use super::io::StreamResource;
|
2019-11-07 09:59:02 -05:00
|
|
|
use crate::deno_error::bad_resource;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops::json_op;
|
2019-08-21 20:42:48 -04:00
|
|
|
use crate::resolve_addr::resolve_addr;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::*;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std;
|
2019-08-21 20:42:48 -04:00
|
|
|
use std::convert::From;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std::net::Shutdown;
|
2019-11-07 09:59:02 -05:00
|
|
|
use std::net::SocketAddr;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2019-08-14 11:03:02 -04:00
|
|
|
use tokio;
|
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use tokio::net::TcpStream;
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|
|
|
i.register_op("accept", s.core_op(json_op(s.stateful_op(op_accept))));
|
2020-01-18 12:35:12 -05:00
|
|
|
i.register_op("connect", s.core_op(json_op(s.stateful_op(op_connect))));
|
2019-10-11 14:41:54 -04:00
|
|
|
i.register_op("shutdown", s.core_op(json_op(s.stateful_op(op_shutdown))));
|
|
|
|
i.register_op("listen", s.core_op(json_op(s.stateful_op(op_listen))));
|
|
|
|
}
|
|
|
|
|
2019-11-07 09:59:02 -05:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum AcceptState {
|
|
|
|
Pending,
|
|
|
|
Done,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simply accepts a connection.
|
2019-11-14 12:10:25 -05:00
|
|
|
pub fn accept(state: &ThreadSafeState, rid: ResourceId) -> Accept {
|
2019-11-07 09:59:02 -05:00
|
|
|
Accept {
|
2019-12-02 15:21:33 -05:00
|
|
|
accept_state: AcceptState::Pending,
|
2019-11-07 09:59:02 -05:00
|
|
|
rid,
|
2020-01-04 05:20:52 -05:00
|
|
|
state,
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A future representing state of accepting a TCP connection.
|
2020-01-04 05:20:52 -05:00
|
|
|
pub struct Accept<'a> {
|
2019-11-14 12:10:25 -05:00
|
|
|
accept_state: AcceptState,
|
2019-11-07 09:59:02 -05:00
|
|
|
rid: ResourceId,
|
2020-01-04 05:20:52 -05:00
|
|
|
state: &'a ThreadSafeState,
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
|
2020-01-04 05:20:52 -05:00
|
|
|
impl Future for Accept<'_> {
|
2019-11-16 19:17:47 -05:00
|
|
|
type Output = Result<(TcpStream, SocketAddr), ErrBox>;
|
2019-11-07 09:59:02 -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();
|
|
|
|
if inner.accept_state == AcceptState::Done {
|
2019-11-07 09:59:02 -05:00
|
|
|
panic!("poll Accept after it's done");
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
let mut table = inner.state.lock_resource_table();
|
2019-11-07 09:59:02 -05:00
|
|
|
let listener_resource = table
|
2019-11-16 19:17:47 -05:00
|
|
|
.get_mut::<TcpListenerResource>(inner.rid)
|
2019-11-07 09:59:02 -05:00
|
|
|
.ok_or_else(|| {
|
|
|
|
let e = std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Listener has been closed",
|
|
|
|
);
|
|
|
|
ErrBox::from(e)
|
|
|
|
})?;
|
|
|
|
|
2019-12-30 08:57:17 -05:00
|
|
|
let listener = &mut listener_resource.listener;
|
2019-11-07 09:59:02 -05:00
|
|
|
|
2019-12-30 08:57:17 -05:00
|
|
|
match listener.poll_accept(cx).map_err(ErrBox::from) {
|
|
|
|
Poll::Ready(Ok((stream, addr))) => {
|
2019-11-07 09:59:02 -05:00
|
|
|
listener_resource.untrack_task();
|
2019-11-16 19:17:47 -05:00
|
|
|
inner.accept_state = AcceptState::Done;
|
|
|
|
Poll::Ready(Ok((stream, addr)))
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Pending => {
|
|
|
|
listener_resource.track_task(cx)?;
|
|
|
|
Poll::Pending
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
2019-12-30 08:57:17 -05:00
|
|
|
Poll::Ready(Err(e)) => {
|
2019-11-07 09:59:02 -05:00
|
|
|
listener_resource.untrack_task();
|
2019-11-16 19:17:47 -05:00
|
|
|
inner.accept_state = AcceptState::Done;
|
|
|
|
Poll::Ready(Err(e))
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct AcceptArgs {
|
|
|
|
rid: i32,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_accept(
|
2019-11-14 12:10:25 -05:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: AcceptArgs = serde_json::from_value(args)?;
|
2019-11-07 09:59:02 -05:00
|
|
|
let rid = args.rid as u32;
|
2019-11-14 12:10:25 -05:00
|
|
|
let state_ = state.clone();
|
|
|
|
let table = state.lock_resource_table();
|
2019-11-07 09:59:02 -05:00
|
|
|
table
|
|
|
|
.get::<TcpListenerResource>(rid)
|
|
|
|
.ok_or_else(bad_resource)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-01-04 05:20:52 -05:00
|
|
|
let op = async move {
|
|
|
|
let (tcp_stream, _socket_addr) = accept(&state_, rid).await?;
|
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
|
|
|
let mut table = state_.lock_resource_table();
|
|
|
|
let rid =
|
|
|
|
table.add("tcpStream", Box::new(StreamResource::TcpStream(tcp_stream)));
|
|
|
|
Ok(json!({
|
|
|
|
"rid": rid,
|
2020-01-18 15:49:55 -05:00
|
|
|
"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",
|
|
|
|
}
|
2020-01-04 05:20:52 -05:00
|
|
|
}))
|
|
|
|
};
|
2019-09-10 00:59:40 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
Ok(JsonOp::Async(op.boxed()))
|
2019-08-14 11:03:02 -04: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,
|
|
|
|
hostname: String,
|
|
|
|
port: u16,
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2020-01-18 12:35:12 -05:00
|
|
|
fn op_connect(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
2020-01-18 12:35:12 -05:00
|
|
|
let args: ConnectArgs = serde_json::from_value(args)?;
|
2019-09-20 18:32:18 -04:00
|
|
|
assert_eq!(args.transport, "tcp"); // TODO Support others.
|
2019-11-14 12:10:25 -05:00
|
|
|
let state_ = state.clone();
|
2019-10-23 10:19:27 -04:00
|
|
|
state.check_net(&args.hostname, args.port)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-12-30 08:57:17 -05:00
|
|
|
let op = async move {
|
|
|
|
let addr = resolve_addr(&args.hostname, args.port).await?;
|
|
|
|
let tcp_stream = TcpStream::connect(&addr).await?;
|
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
|
|
|
let mut table = state_.lock_resource_table();
|
|
|
|
let rid =
|
|
|
|
table.add("tcpStream", Box::new(StreamResource::TcpStream(tcp_stream)));
|
|
|
|
Ok(json!({
|
|
|
|
"rid": rid,
|
2020-01-18 15:49:55 -05:00
|
|
|
"localAddr": {
|
|
|
|
"hostname": local_addr.ip().to_string(),
|
|
|
|
"port": local_addr.port(),
|
|
|
|
"transport": args.transport,
|
|
|
|
},
|
|
|
|
"remoteAddr": {
|
|
|
|
"hostname": remote_addr.ip().to_string(),
|
|
|
|
"port": remote_addr.port(),
|
|
|
|
"transport": args.transport,
|
|
|
|
}
|
2019-12-30 08:57:17 -05:00
|
|
|
}))
|
|
|
|
};
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
Ok(JsonOp::Async(op.boxed()))
|
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(
|
2019-11-14 12:10:25 -05:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
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!(),
|
|
|
|
};
|
|
|
|
|
2019-11-14 12:10:25 -05:00
|
|
|
let mut table = state.lock_resource_table();
|
|
|
|
let resource = table
|
|
|
|
.get_mut::<StreamResource>(rid)
|
|
|
|
.ok_or_else(bad_resource)?;
|
2019-11-07 11:11:15 -05:00
|
|
|
match resource {
|
2019-11-14 12:10:25 -05:00
|
|
|
StreamResource::TcpStream(ref mut stream) => {
|
2019-11-07 11:11:15 -05:00
|
|
|
TcpStream::shutdown(stream, shutdown_mode).map_err(ErrBox::from)?;
|
|
|
|
}
|
|
|
|
_ => return Err(bad_resource()),
|
|
|
|
}
|
|
|
|
|
2019-09-10 00:59:40 -04:00
|
|
|
Ok(JsonOp::Sync(json!({})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ListenArgs {
|
2019-09-20 18:32:18 -04:00
|
|
|
transport: String,
|
|
|
|
hostname: String,
|
|
|
|
port: u16,
|
2019-08-26 08:50:21 -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,
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:11:15 -05:00
|
|
|
impl Resource for TcpListenerResource {}
|
2019-11-07 09:59:02 -05:00
|
|
|
|
|
|
|
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.
|
2019-11-16 19:17:47 -05:00
|
|
|
pub fn track_task(&mut self, cx: &Context) -> Result<(), ErrBox> {
|
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() {
|
2019-11-07 09:59:02 -05:00
|
|
|
let e = std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Another accept task is ongoing",
|
|
|
|
);
|
|
|
|
return Err(ErrBox::from(e));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_listen(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: ListenArgs = serde_json::from_value(args)?;
|
2019-09-20 18:32:18 -04:00
|
|
|
assert_eq!(args.transport, "tcp");
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2019-10-23 10:19:27 -04:00
|
|
|
state.check_net(&args.hostname, args.port)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
let addr =
|
|
|
|
futures::executor::block_on(resolve_addr(&args.hostname, args.port))?;
|
2019-12-30 08:57:17 -05:00
|
|
|
let listener = futures::executor::block_on(TcpListener::bind(&addr))?;
|
2019-08-27 11:35:32 -04:00
|
|
|
let local_addr = listener.local_addr()?;
|
2019-11-07 09:59:02 -05:00
|
|
|
let listener_resource = TcpListenerResource {
|
2019-12-30 08:57:17 -05:00
|
|
|
listener,
|
2019-11-16 19:17:47 -05:00
|
|
|
waker: None,
|
2019-11-07 09:59:02 -05:00
|
|
|
local_addr,
|
|
|
|
};
|
2019-11-14 12:10:25 -05:00
|
|
|
let mut table = state.lock_resource_table();
|
2019-11-07 09:59:02 -05:00
|
|
|
let rid = table.add("tcpListener", Box::new(listener_resource));
|
2020-01-18 15:49:55 -05:00
|
|
|
debug!(
|
|
|
|
"New listener {} {}:{}",
|
|
|
|
rid,
|
|
|
|
local_addr.ip().to_string(),
|
|
|
|
local_addr.port()
|
|
|
|
);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-27 11:35:32 -04:00
|
|
|
Ok(JsonOp::Sync(json!({
|
2019-11-07 09:59:02 -05:00
|
|
|
"rid": rid,
|
2020-01-18 15:49:55 -05:00
|
|
|
"localAddr": {
|
|
|
|
"hostname": local_addr.ip().to_string(),
|
|
|
|
"port": local_addr.port(),
|
|
|
|
"transport": args.transport,
|
|
|
|
},
|
2019-08-27 11:35:32 -04:00
|
|
|
})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|