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::net::AcceptArgs;
|
|
|
|
use crate::ops::net::ReceiveArgs;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::bad_resource;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::error::custom_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
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-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;
|
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-03-23 18:02:51 -04:00
|
|
|
use tokio::net::UnixDatagram;
|
|
|
|
use tokio::net::UnixListener;
|
|
|
|
pub use tokio::net::UnixStream;
|
|
|
|
|
|
|
|
struct UnixListenerResource {
|
2020-12-16 11:14:12 -05:00
|
|
|
listener: AsyncRefCell<UnixListener>,
|
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for UnixListenerResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"unixListener".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel();
|
|
|
|
}
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UnixDatagramResource {
|
2020-12-16 11:14:12 -05:00
|
|
|
pub socket: AsyncRefCell<UnixDatagram>,
|
|
|
|
pub cancel: CancelHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for UnixDatagramResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"unixDatagram".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel();
|
|
|
|
}
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
|
|
|
.get::<UnixListenerResource>(rid)
|
|
|
|
.ok_or_else(|| bad_resource("Listener has been closed"))?;
|
|
|
|
let mut listener = RcRef::map(&resource, |r| &r.listener)
|
|
|
|
.try_borrow_mut()
|
|
|
|
.ok_or_else(|| custom_error("Busy", "Listener already in use"))?;
|
|
|
|
let cancel = RcRef::map(resource, |r| &r.cancel);
|
|
|
|
let (unix_stream, _socket_addr) =
|
|
|
|
listener.accept().try_or_cancel(cancel).await?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
let local_addr = unix_stream.local_addr()?;
|
|
|
|
let remote_addr = unix_stream.peer_addr()?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = StreamResource::unix_stream(unix_stream);
|
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(resource);
|
2020-08-28 11:08:24 -04:00
|
|
|
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 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::<UnixDatagramResource>(rid)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(|| bad_resource("Socket has been closed"))?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let mut socket = RcRef::map(&resource, |r| &r.socket)
|
|
|
|
.try_borrow_mut()
|
|
|
|
.ok_or_else(|| custom_error("Busy", "Socket already in use"))?;
|
|
|
|
let cancel = RcRef::map(resource, |r| &r.cancel);
|
|
|
|
let (size, remote_addr) =
|
|
|
|
socket.recv_from(&mut buf).try_or_cancel(cancel).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()?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let listener_resource = UnixListenerResource {
|
|
|
|
listener: AsyncRefCell::new(listener),
|
|
|
|
cancel: Default::default(),
|
|
|
|
};
|
|
|
|
let rid = state.resource_table.add(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 {
|
2020-12-16 11:14:12 -05:00
|
|
|
socket: AsyncRefCell::new(socket),
|
|
|
|
cancel: Default::default(),
|
2020-03-23 18:02:51 -04:00
|
|
|
};
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(datagram_resource);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
Ok((rid, local_addr))
|
|
|
|
}
|