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-06-28 19:43:03 -04:00
|
|
|
use crate::io::UnixStreamResource;
|
|
|
|
use crate::ops::AcceptArgs;
|
|
|
|
use crate::ops::OpAddr;
|
|
|
|
use crate::ops::OpConn;
|
|
|
|
use crate::ops::OpPacket;
|
|
|
|
use crate::ops::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-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;
|
|
|
|
|
2021-06-28 19:43:03 -04:00
|
|
|
/// A utility function to map OsStrings to Strings
|
|
|
|
pub fn into_string(s: std::ffi::OsString) -> Result<String, AnyError> {
|
|
|
|
s.into_string().map_err(|s| {
|
|
|
|
let message = format!("File name or path {:?} is not valid UTF-8", s);
|
|
|
|
custom_error("InvalidData", message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
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-05-08 08:37:42 -04:00
|
|
|
_: (),
|
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)
|
2021-08-15 07:29:19 -04:00
|
|
|
.map_err(|_| 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);
|
2021-12-14 17:27:04 -05:00
|
|
|
let (unix_stream, _socket_addr) = listener
|
|
|
|
.accept()
|
|
|
|
.try_or_cancel(cancel)
|
|
|
|
.await
|
|
|
|
.map_err(crate::ops::accept_err)?;
|
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-10-05 16:38:27 -04:00
|
|
|
mut buf: ZeroCopyBuf,
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpPacket, AnyError> {
|
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)
|
2021-08-15 07:29:19 -04:00
|
|
|
.map_err(|_| 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()
|
|
|
|
}
|