2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-21 08:26:41 -04:00
|
|
|
|
2021-04-06 04:33:43 -04:00
|
|
|
use super::utils::into_string;
|
2021-01-14 23:32:27 -05:00
|
|
|
use crate::ops::io::UnixStreamResource;
|
2020-09-05 20:34:02 -04:00
|
|
|
use crate::ops::net::AcceptArgs;
|
2021-04-06 04:33:43 -04:00
|
|
|
use crate::ops::net::OpAddr;
|
|
|
|
use crate::ops::net::OpConn;
|
|
|
|
use crate::ops::net::OpPacket;
|
2020-09-05 20:34:02 -04:00
|
|
|
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;
|
2021-04-02 09:47:57 -04:00
|
|
|
use deno_core::error::null_opbuf;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
|
|
|
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;
|
2021-04-02 09:47:57 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2021-04-05 12:40:24 -04:00
|
|
|
use serde::Serialize;
|
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;
|
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
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct UnixAddr {
|
2021-04-06 04:33:43 -04:00
|
|
|
pub path: Option<String>,
|
2021-04-05 12:40:24 -04:00
|
|
|
}
|
|
|
|
|
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,
|
2021-04-02 09:47:57 -04:00
|
|
|
_bufs: Option<ZeroCopyBuf>,
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpConn, AnyError> {
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2020-09-05 20:34:02 -04:00
|
|
|
|
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"))?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let listener = RcRef::map(&resource, |r| &r.listener)
|
2020-12-16 11:14:12 -05:00
|
|
|
.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()?;
|
2021-01-14 23:32:27 -05:00
|
|
|
let resource = UnixStreamResource::new(unix_stream.into_split());
|
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);
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(OpConn {
|
|
|
|
rid,
|
|
|
|
local_addr: Some(OpAddr::Unix(UnixAddr {
|
|
|
|
path: local_addr.as_pathname().and_then(pathstring),
|
|
|
|
})),
|
|
|
|
remote_addr: Some(OpAddr::Unix(UnixAddr {
|
|
|
|
path: remote_addr.as_pathname().and_then(pathstring),
|
|
|
|
})),
|
|
|
|
})
|
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,
|
2021-04-02 09:47:57 -04:00
|
|
|
buf: Option<ZeroCopyBuf>,
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpPacket, AnyError> {
|
2021-04-02 09:47:57 -04:00
|
|
|
let mut buf = buf.ok_or_else(null_opbuf)?;
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
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"))?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let socket = RcRef::map(&resource, |r| &r.socket)
|
2020-12-16 11:14:12 -05:00
|
|
|
.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?;
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(OpPacket {
|
|
|
|
size,
|
|
|
|
remote_addr: OpAddr::UnixPacket(UnixAddr {
|
|
|
|
path: remote_addr.as_pathname().and_then(pathstring),
|
|
|
|
}),
|
|
|
|
})
|
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,
|
2021-01-12 02:50:02 -05:00
|
|
|
) -> Result<(u32, tokio::net::unix::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,
|
2021-01-12 02:50:02 -05:00
|
|
|
) -> Result<(u32, tokio::net::unix::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))
|
|
|
|
}
|
2021-04-06 04:33:43 -04:00
|
|
|
|
|
|
|
pub fn pathstring(pathname: &Path) -> Option<String> {
|
|
|
|
into_string(pathname.into()).ok()
|
|
|
|
}
|