2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
use crate::ops::io::StreamResource;
|
|
|
|
use crate::ops::io::StreamResourceHolder;
|
|
|
|
use crate::ops::net::AcceptArgs;
|
|
|
|
use crate::ops::net::ReceiveArgs;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::bad_resource;
|
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::future::poll_fn;
|
|
|
|
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-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-03-23 18:02:51 -04:00
|
|
|
use std::fs::remove_file;
|
|
|
|
use std::os::unix;
|
2020-09-14 12:48:57 -04:00
|
|
|
use std::path::Path;
|
2020-08-28 11:08:24 -04:00
|
|
|
use std::rc::Rc;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::task::Poll;
|
2020-03-23 18:02:51 -04:00
|
|
|
use tokio::net::UnixDatagram;
|
|
|
|
use tokio::net::UnixListener;
|
|
|
|
pub use tokio::net::UnixStream;
|
|
|
|
|
|
|
|
struct UnixListenerResource {
|
|
|
|
listener: UnixListener,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UnixDatagramResource {
|
|
|
|
pub socket: UnixDatagram,
|
|
|
|
pub local_addr: unix::net::SocketAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct UnixListenArgs {
|
2020-04-28 12:37:59 -04:00
|
|
|
pub path: String,
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
pub(crate) async fn accept_unix(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2020-09-05 20:34:02 -04:00
|
|
|
args: AcceptArgs,
|
|
|
|
_bufs: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid = args.rid as u32;
|
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
let accept_fut = poll_fn(|cx| {
|
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let listener_resource = state
|
|
|
|
.resource_table
|
2020-08-28 11:08:24 -04:00
|
|
|
.get_mut::<UnixListenerResource>(rid)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(|| bad_resource("Listener has been closed"))?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let listener = &mut listener_resource.listener;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::StreamExt;
|
2020-09-10 09:57:45 -04:00
|
|
|
match listener.poll_next_unpin(cx) {
|
|
|
|
Poll::Ready(Some(stream)) => {
|
|
|
|
//listener_resource.untrack_task();
|
|
|
|
Poll::Ready(stream)
|
|
|
|
}
|
|
|
|
Poll::Ready(None) => todo!(),
|
|
|
|
Poll::Pending => {
|
|
|
|
//listener_resource.track_task(cx)?;
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
2020-09-14 12:48:57 -04:00
|
|
|
.map_err(AnyError::from)
|
2020-09-10 09:57:45 -04:00
|
|
|
});
|
|
|
|
let unix_stream = accept_fut.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": "unix",
|
|
|
|
},
|
|
|
|
"remoteAddr": {
|
|
|
|
"path": remote_addr.as_pathname(),
|
|
|
|
"transport": "unix",
|
|
|
|
}
|
|
|
|
}))
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
pub(crate) async fn receive_unix_packet(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2020-09-05 20:34:02 -04:00
|
|
|
args: ReceiveArgs,
|
|
|
|
bufs: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-05 20:34:02 -04:00
|
|
|
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
|
|
|
|
|
|
|
|
let rid = args.rid as u32;
|
|
|
|
let mut buf = bufs.into_iter().next().unwrap();
|
2020-03-23 18:02:51 -04:00
|
|
|
|
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::<UnixDatagramResource>(rid)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(|| bad_resource("Socket has been closed"))?;
|
2020-09-05 20:34:02 -04:00
|
|
|
let (size, remote_addr) = resource.socket.recv_from(&mut buf).await?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
|
|
|
"size": size,
|
|
|
|
"remoteAddr": {
|
|
|
|
"path": remote_addr.as_pathname(),
|
|
|
|
"transport": "unixpacket",
|
|
|
|
}
|
|
|
|
}))
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn listen_unix(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-03-23 18:02:51 -04:00
|
|
|
addr: &Path,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(u32, unix::net::SocketAddr), AnyError> {
|
2020-03-23 18:02:51 -04:00
|
|
|
if addr.exists() {
|
|
|
|
remove_file(&addr).unwrap();
|
|
|
|
}
|
|
|
|
let listener = UnixListener::bind(&addr)?;
|
|
|
|
let local_addr = listener.local_addr()?;
|
|
|
|
let listener_resource = UnixListenerResource { listener };
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid = state
|
|
|
|
.resource_table
|
|
|
|
.add("unixListener", Box::new(listener_resource));
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
Ok((rid, local_addr))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn listen_unix_packet(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-03-23 18:02:51 -04:00
|
|
|
addr: &Path,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(u32, unix::net::SocketAddr), AnyError> {
|
2020-03-23 18:02:51 -04:00
|
|
|
if addr.exists() {
|
|
|
|
remove_file(&addr).unwrap();
|
|
|
|
}
|
|
|
|
let socket = UnixDatagram::bind(&addr)?;
|
|
|
|
let local_addr = socket.local_addr()?;
|
|
|
|
let datagram_resource = UnixDatagramResource {
|
|
|
|
socket,
|
|
|
|
local_addr: local_addr.clone(),
|
|
|
|
};
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid = state
|
|
|
|
.resource_table
|
|
|
|
.add("unixDatagram", Box::new(datagram_resource));
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
Ok((rid, local_addr))
|
|
|
|
}
|