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};
|
2020-03-11 18:19:24 -04:00
|
|
|
use super::io::{StreamResource, StreamResourceHolder};
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2019-08-21 20:42:48 -04:00
|
|
|
use crate::resolve_addr::resolve_addr;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
|
|
|
use deno_core::ResourceTable;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-02-27 15:08:21 -05:00
|
|
|
use futures::future::poll_fn;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
2019-08-21 20:42:48 -04:00
|
|
|
use std::convert::From;
|
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::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-04-23 05:51:07 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &State) {
|
2020-04-21 09:48:44 -04:00
|
|
|
i.register_op("op_accept", s.stateful_json_op2(op_accept));
|
|
|
|
i.register_op("op_connect", s.stateful_json_op2(op_connect));
|
|
|
|
i.register_op("op_shutdown", s.stateful_json_op2(op_shutdown));
|
|
|
|
i.register_op("op_listen", s.stateful_json_op2(op_listen));
|
|
|
|
i.register_op("op_receive", s.stateful_json_op2(op_receive));
|
|
|
|
i.register_op("op_send", s.stateful_json_op2(op_send));
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct AcceptArgs {
|
|
|
|
rid: i32,
|
2020-03-23 18:02:51 -04:00
|
|
|
transport: String,
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
fn accept_tcp(
|
2020-04-23 05:51:07 -04:00
|
|
|
isolate: &mut CoreIsolate,
|
2020-03-23 18:02:51 -04:00
|
|
|
args: AcceptArgs,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-11-07 09:59:02 -05:00
|
|
|
let rid = args.rid as u32;
|
2020-04-21 09:48:44 -04:00
|
|
|
let resource_table = isolate.resource_table.clone();
|
2020-02-27 15:08:21 -05:00
|
|
|
|
2020-01-04 05:20:52 -05:00
|
|
|
let op = async move {
|
2020-02-27 15:08:21 -05:00
|
|
|
let accept_fut = poll_fn(|cx| {
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
2020-02-27 15:08:21 -05:00
|
|
|
let listener_resource = resource_table
|
|
|
|
.get_mut::<TcpListenerResource>(rid)
|
|
|
|
.ok_or_else(|| {
|
2020-03-10 15:14:22 -04:00
|
|
|
OpError::bad_resource("Listener has been closed".to_string())
|
2020-02-27 15:08:21 -05:00
|
|
|
})?;
|
|
|
|
let listener = &mut listener_resource.listener;
|
|
|
|
match listener.poll_accept(cx).map_err(OpError::from) {
|
|
|
|
Poll::Ready(Ok((stream, addr))) => {
|
|
|
|
listener_resource.untrack_task();
|
|
|
|
Poll::Ready(Ok((stream, addr)))
|
|
|
|
}
|
|
|
|
Poll::Pending => {
|
|
|
|
listener_resource.track_task(cx)?;
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
Poll::Ready(Err(e)) => {
|
|
|
|
listener_resource.untrack_task();
|
|
|
|
Poll::Ready(Err(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-02-25 09:14:27 -05:00
|
|
|
let (tcp_stream, _socket_addr) = accept_fut.await?;
|
2020-01-04 05:20:52 -05:00
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let rid = resource_table.add(
|
2020-03-11 18:19:24 -04:00
|
|
|
"tcpStream",
|
2020-04-18 11:21:20 -04:00
|
|
|
Box::new(StreamResourceHolder::new(StreamResource::TcpStream(Some(
|
2020-03-11 18:19:24 -04:00
|
|
|
tcp_stream,
|
2020-04-18 11:21:20 -04:00
|
|
|
)))),
|
2020-03-11 18:19:24 -04:00
|
|
|
);
|
2020-01-04 05:20:52 -05:00
|
|
|
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
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
Ok(JsonOp::Async(op.boxed_local()))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
fn op_accept(
|
2020-04-23 05:51:07 -04:00
|
|
|
isolate: &mut CoreIsolate,
|
2020-04-21 09:48:44 -04:00
|
|
|
_state: &State,
|
2020-03-23 18:02:51 -04:00
|
|
|
args: Value,
|
|
|
|
zero_copy: Option<ZeroCopyBuf>,
|
|
|
|
) -> Result<JsonOp, OpError> {
|
|
|
|
let args: AcceptArgs = serde_json::from_value(args)?;
|
|
|
|
match args.transport.as_str() {
|
2020-04-21 09:48:44 -04:00
|
|
|
"tcp" => accept_tcp(isolate, args, zero_copy),
|
2020-03-23 18:02:51 -04:00
|
|
|
#[cfg(unix)]
|
2020-04-21 09:48:44 -04:00
|
|
|
"unix" => net_unix::accept_unix(isolate, args.rid as u32, zero_copy),
|
2020-03-23 18:02:51 -04:00
|
|
|
_ => Err(OpError::other(format!(
|
|
|
|
"Unsupported transport protocol {}",
|
|
|
|
args.transport
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ReceiveArgs {
|
|
|
|
rid: i32,
|
2020-03-23 18:02:51 -04:00
|
|
|
transport: String,
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
fn receive_udp(
|
2020-04-23 05:51:07 -04:00
|
|
|
isolate: &mut CoreIsolate,
|
2020-04-21 09:48:44 -04:00
|
|
|
_state: &State,
|
2020-03-23 18:02:51 -04:00
|
|
|
args: ReceiveArgs,
|
2020-02-21 11:26:54 -05:00
|
|
|
zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-02-27 15:08:21 -05:00
|
|
|
let mut buf = zero_copy.unwrap();
|
2020-02-21 11:26:54 -05:00
|
|
|
|
|
|
|
let rid = args.rid as u32;
|
|
|
|
|
2020-04-21 09:48:44 -04:00
|
|
|
let resource_table = isolate.resource_table.clone();
|
2020-02-21 11:26:54 -05:00
|
|
|
|
|
|
|
let op = async move {
|
2020-02-27 15:08:21 -05:00
|
|
|
let receive_fut = poll_fn(|cx| {
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
2020-02-27 15:08:21 -05:00
|
|
|
let resource = resource_table
|
|
|
|
.get_mut::<UdpSocketResource>(rid)
|
2020-03-10 15:14:22 -04:00
|
|
|
.ok_or_else(|| {
|
|
|
|
OpError::bad_resource("Socket has been closed".to_string())
|
|
|
|
})?;
|
2020-02-27 15:08:21 -05:00
|
|
|
let socket = &mut resource.socket;
|
|
|
|
socket.poll_recv_from(cx, &mut buf).map_err(OpError::from)
|
|
|
|
});
|
2020-02-25 09:14:27 -05:00
|
|
|
let (size, remote_addr) = receive_fut.await?;
|
2020-02-21 11:26:54 -05:00
|
|
|
Ok(json!({
|
|
|
|
"size": size,
|
|
|
|
"remoteAddr": {
|
|
|
|
"hostname": remote_addr.ip().to_string(),
|
|
|
|
"port": remote_addr.port(),
|
|
|
|
"transport": "udp",
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(JsonOp::Async(op.boxed_local()))
|
|
|
|
}
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
fn op_receive(
|
2020-04-23 05:51:07 -04:00
|
|
|
isolate: &mut CoreIsolate,
|
2020-03-23 18:02:51 -04:00
|
|
|
state: &State,
|
|
|
|
args: Value,
|
|
|
|
zero_copy: Option<ZeroCopyBuf>,
|
|
|
|
) -> Result<JsonOp, OpError> {
|
|
|
|
assert!(zero_copy.is_some());
|
|
|
|
let args: ReceiveArgs = serde_json::from_value(args)?;
|
|
|
|
match args.transport.as_str() {
|
2020-04-21 09:48:44 -04:00
|
|
|
"udp" => receive_udp(isolate, state, args, zero_copy),
|
2020-03-23 18:02:51 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
"unixpacket" => {
|
2020-04-21 09:48:44 -04:00
|
|
|
net_unix::receive_unix_packet(isolate, args.rid as u32, zero_copy)
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
_ => Err(OpError::other(format!(
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
fn op_send(
|
2020-04-23 05:51:07 -04:00
|
|
|
isolate: &mut CoreIsolate,
|
2020-02-21 11:26:54 -05:00
|
|
|
state: &State,
|
|
|
|
args: Value,
|
|
|
|
zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-02-21 11:26:54 -05:00
|
|
|
assert!(zero_copy.is_some());
|
|
|
|
let buf = zero_copy.unwrap();
|
2020-04-21 09:48:44 -04:00
|
|
|
let resource_table = isolate.resource_table.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" => {
|
|
|
|
state.check_net(&args.hostname, args.port)?;
|
|
|
|
|
|
|
|
let op = async move {
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let resource = resource_table
|
2020-03-23 18:02:51 -04:00
|
|
|
.get_mut::<UdpSocketResource>(rid as u32)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
OpError::bad_resource("Socket has been closed".to_string())
|
|
|
|
})?;
|
|
|
|
let socket = &mut resource.socket;
|
2020-04-15 23:14:28 -04:00
|
|
|
let addr = resolve_addr(&args.hostname, args.port)?;
|
2020-03-23 18:02:51 -04:00
|
|
|
socket.send_to(&buf, addr).await?;
|
|
|
|
Ok(json!({}))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(JsonOp::Async(op.boxed_local()))
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
SendArgs {
|
|
|
|
rid,
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Unix(args),
|
|
|
|
} if transport == "unixpacket" => {
|
|
|
|
let address_path = net_unix::Path::new(&args.address);
|
|
|
|
state.check_read(&address_path)?;
|
|
|
|
let op = async move {
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let resource = resource_table
|
2020-03-23 18:02:51 -04:00
|
|
|
.get_mut::<net_unix::UnixDatagramResource>(rid as u32)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
OpError::other("Socket has been closed".to_string())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let socket = &mut resource.socket;
|
|
|
|
socket
|
|
|
|
.send_to(&buf, &resource.local_addr.as_pathname().unwrap())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(json!({}))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(JsonOp::Async(op.boxed_local()))
|
|
|
|
}
|
|
|
|
_ => Err(OpError::other("Wrong argument format!".to_owned())),
|
|
|
|
}
|
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-01-18 12:35:12 -05:00
|
|
|
fn op_connect(
|
2020-04-23 05:51:07 -04:00
|
|
|
isolate: &mut CoreIsolate,
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-21 09:48:44 -04:00
|
|
|
let resource_table = isolate.resource_table.clone();
|
2020-03-23 18:02:51 -04:00
|
|
|
match serde_json::from_value(args)? {
|
|
|
|
ConnectArgs {
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Ip(args),
|
|
|
|
} if transport == "tcp" => {
|
|
|
|
state.check_net(&args.hostname, args.port)?;
|
|
|
|
let op = async move {
|
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 tcp_stream = TcpStream::connect(&addr).await?;
|
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let rid = resource_table.add(
|
2020-03-23 18:02:51 -04:00
|
|
|
"tcpStream",
|
2020-04-18 11:21:20 -04:00
|
|
|
Box::new(StreamResourceHolder::new(StreamResource::TcpStream(Some(
|
2020-03-23 18:02:51 -04:00
|
|
|
tcp_stream,
|
2020-04-18 11:21:20 -04:00
|
|
|
)))),
|
2020-03-23 18:02:51 -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,
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
};
|
|
|
|
Ok(JsonOp::Async(op.boxed_local()))
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
ConnectArgs {
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Unix(args),
|
|
|
|
} if transport == "unix" => {
|
|
|
|
let address_path = net_unix::Path::new(&args.address);
|
|
|
|
state.check_read(&address_path)?;
|
|
|
|
let op = async move {
|
|
|
|
let address = args.address;
|
|
|
|
let unix_stream =
|
|
|
|
net_unix::UnixStream::connect(net_unix::Path::new(&address)).await?;
|
|
|
|
let local_addr = unix_stream.local_addr()?;
|
|
|
|
let remote_addr = unix_stream.peer_addr()?;
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let rid = resource_table.add(
|
2020-03-23 18:02:51 -04:00
|
|
|
"unixStream",
|
|
|
|
Box::new(StreamResourceHolder::new(StreamResource::UnixStream(
|
|
|
|
unix_stream,
|
|
|
|
))),
|
|
|
|
);
|
|
|
|
Ok(json!({
|
|
|
|
"rid": rid,
|
|
|
|
"localAddr": {
|
|
|
|
"address": local_addr.as_pathname(),
|
|
|
|
"transport": transport,
|
|
|
|
},
|
|
|
|
"remoteAddr": {
|
|
|
|
"address": remote_addr.as_pathname(),
|
|
|
|
"transport": transport,
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
};
|
|
|
|
Ok(JsonOp::Async(op.boxed_local()))
|
|
|
|
}
|
|
|
|
_ => Err(OpError::other("Wrong argument format!".to_owned())),
|
|
|
|
}
|
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-04-23 05:51:07 -04:00
|
|
|
isolate: &mut CoreIsolate,
|
2020-04-28 01:36:47 -04:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-28 01:36:47 -04:00
|
|
|
state.check_unstable("Deno.shutdown");
|
|
|
|
|
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-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
|
|
|
let resource_holder = resource_table
|
2020-03-11 18:19:24 -04:00
|
|
|
.get_mut::<StreamResourceHolder>(rid)
|
2020-03-05 08:30:41 -05:00
|
|
|
.ok_or_else(OpError::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-02-23 14:51:29 -05:00
|
|
|
TcpStream::shutdown(stream, shutdown_mode).map_err(OpError::from)?;
|
2019-11-07 11:11:15 -05:00
|
|
|
}
|
2020-03-23 18:02:51 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
StreamResource::UnixStream(ref mut stream) => {
|
|
|
|
net_unix::UnixStream::shutdown(stream, shutdown_mode)
|
|
|
|
.map_err(OpError::from)?;
|
|
|
|
}
|
2020-03-05 08:30:41 -05:00
|
|
|
_ => return Err(OpError::bad_resource_id()),
|
2019-11-07 11:11:15 -05:00
|
|
|
}
|
|
|
|
|
2019-09-10 00:59:40 -04:00
|
|
|
Ok(JsonOp::Sync(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-02-23 14:51:29 -05:00
|
|
|
pub fn track_task(&mut self, cx: &Context) -> Result<(), OpError> {
|
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-02-23 14:51:29 -05:00
|
|
|
return Err(OpError::other("Another accept task is ongoing".to_string()));
|
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-04-21 09:48:44 -04:00
|
|
|
resource_table: &mut ResourceTable,
|
2020-02-21 11:26:54 -05:00
|
|
|
addr: SocketAddr,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<(u32, SocketAddr), OpError> {
|
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-04-21 09:48:44 -04:00
|
|
|
let rid = resource_table.add("tcpListener", Box::new(listener_resource));
|
2020-02-21 11:26:54 -05:00
|
|
|
|
|
|
|
Ok((rid, local_addr))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn listen_udp(
|
2020-04-21 09:48:44 -04:00
|
|
|
resource_table: &mut ResourceTable,
|
2020-02-21 11:26:54 -05:00
|
|
|
addr: SocketAddr,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<(u32, SocketAddr), OpError> {
|
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-04-21 09:48:44 -04:00
|
|
|
let rid = 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-04-23 05:51:07 -04:00
|
|
|
isolate: &mut CoreIsolate,
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
2020-03-23 18:02:51 -04:00
|
|
|
match serde_json::from_value(args)? {
|
|
|
|
ListenArgs {
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Ip(args),
|
|
|
|
} => {
|
|
|
|
state.check_net(&args.hostname, args.port)?;
|
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-04-21 09:48:44 -04:00
|
|
|
listen_tcp(&mut resource_table, addr)?
|
2020-03-23 18:02:51 -04:00
|
|
|
} else {
|
2020-04-21 09:48:44 -04:00
|
|
|
listen_udp(&mut resource_table, addr)?
|
2020-03-23 18:02:51 -04:00
|
|
|
};
|
|
|
|
debug!(
|
|
|
|
"New listener {} {}:{}",
|
|
|
|
rid,
|
|
|
|
local_addr.ip().to_string(),
|
|
|
|
local_addr.port()
|
|
|
|
);
|
|
|
|
Ok(JsonOp::Sync(json!({
|
|
|
|
"rid": rid,
|
|
|
|
"localAddr": {
|
|
|
|
"hostname": local_addr.ip().to_string(),
|
|
|
|
"port": local_addr.port(),
|
|
|
|
"transport": transport,
|
|
|
|
},
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
ListenArgs {
|
|
|
|
transport,
|
|
|
|
transport_args: ArgsEnum::Unix(args),
|
|
|
|
} if transport == "unix" || transport == "unixpacket" => {
|
|
|
|
let address_path = net_unix::Path::new(&args.address);
|
|
|
|
state.check_read(&address_path)?;
|
2020-04-27 16:56:24 -04:00
|
|
|
state.check_write(&address_path)?;
|
2020-03-23 18:02:51 -04:00
|
|
|
let (rid, local_addr) = if transport == "unix" {
|
2020-04-21 09:48:44 -04:00
|
|
|
net_unix::listen_unix(&mut resource_table, &address_path)?
|
2020-03-23 18:02:51 -04:00
|
|
|
} else {
|
2020-04-21 09:48:44 -04:00
|
|
|
net_unix::listen_unix_packet(&mut resource_table, &address_path)?
|
2020-03-23 18:02:51 -04:00
|
|
|
};
|
|
|
|
debug!(
|
|
|
|
"New listener {} {}",
|
|
|
|
rid,
|
|
|
|
local_addr.as_pathname().unwrap().display(),
|
|
|
|
);
|
|
|
|
Ok(JsonOp::Sync(json!({
|
|
|
|
"rid": rid,
|
|
|
|
"localAddr": {
|
|
|
|
"address": local_addr.as_pathname(),
|
|
|
|
"transport": transport,
|
|
|
|
},
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
_ => Err(OpError::other("Wrong argument format!".to_owned())),
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|