2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 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;
|
2022-10-25 16:50:55 -04:00
|
|
|
use crate::NetPermissions;
|
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;
|
2023-09-12 09:39:21 -04:00
|
|
|
use deno_core::op2;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
|
|
|
use deno_core::CancelHandle;
|
|
|
|
use deno_core::CancelTryFuture;
|
2023-06-22 17:37:56 -04:00
|
|
|
use deno_core::JsBuffer;
|
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;
|
2022-10-25 16:50:55 -04:00
|
|
|
use deno_core::ResourceId;
|
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-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| {
|
2023-01-27 10:43:16 -05:00
|
|
|
let message = format!("File name or path {s:?} is not valid UTF-8");
|
2021-06-28 19:43:03 -04:00
|
|
|
custom_error("InvalidData", message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-22 13:48:21 -04:00
|
|
|
pub(crate) struct UnixListenerResource {
|
|
|
|
pub listener: AsyncRefCell<UnixListener>,
|
2020-12-16 11:14:12 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-12 09:39:21 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2022-10-25 16:50:55 -04:00
|
|
|
pub async fn op_net_accept_unix(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2022-10-25 16:50:55 -04:00
|
|
|
) -> Result<(ResourceId, Option<String>, Option<String>), AnyError> {
|
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()?;
|
2022-10-25 16:50:55 -04:00
|
|
|
let local_addr_path = local_addr.as_pathname().map(pathstring).transpose()?;
|
|
|
|
let remote_addr_path =
|
|
|
|
remote_addr.as_pathname().map(pathstring).transpose()?;
|
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);
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((rid, local_addr_path, remote_addr_path))
|
2022-10-24 08:55:39 -04:00
|
|
|
}
|
2020-03-23 18:02:51 -04:00
|
|
|
|
2023-09-12 09:39:21 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2022-10-25 16:50:55 -04:00
|
|
|
pub async fn op_net_connect_unix<NP>(
|
2022-10-24 08:55:39 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[string] path: String,
|
2022-10-25 16:50:55 -04:00
|
|
|
) -> Result<(ResourceId, Option<String>, Option<String>), AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
|
|
|
let address_path = Path::new(&path);
|
2023-10-04 15:42:17 -04:00
|
|
|
super::check_unstable(&state.borrow(), "Deno.connect");
|
2022-10-25 16:50:55 -04:00
|
|
|
{
|
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
state_
|
|
|
|
.borrow_mut::<NP>()
|
|
|
|
.check_read(address_path, "Deno.connect()")?;
|
|
|
|
state_
|
|
|
|
.borrow_mut::<NP>()
|
|
|
|
.check_write(address_path, "Deno.connect()")?;
|
|
|
|
}
|
|
|
|
let unix_stream = UnixStream::connect(Path::new(&path)).await?;
|
|
|
|
let local_addr = unix_stream.local_addr()?;
|
|
|
|
let remote_addr = unix_stream.peer_addr()?;
|
|
|
|
let local_addr_path = local_addr.as_pathname().map(pathstring).transpose()?;
|
|
|
|
let remote_addr_path =
|
|
|
|
remote_addr.as_pathname().map(pathstring).transpose()?;
|
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
let resource = UnixStreamResource::new(unix_stream.into_split());
|
|
|
|
let rid = state_.resource_table.add(resource);
|
|
|
|
Ok((rid, local_addr_path, remote_addr_path))
|
|
|
|
}
|
2022-10-25 14:32:51 -04:00
|
|
|
|
2023-09-12 09:39:21 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2022-10-25 16:50:55 -04:00
|
|
|
pub async fn op_net_recv_unixpacket(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[smi] rid: ResourceId,
|
|
|
|
#[buffer] mut buf: JsBuffer,
|
2022-10-25 16:50:55 -04:00
|
|
|
) -> Result<(usize, Option<String>), AnyError> {
|
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);
|
2022-10-25 16:50:55 -04:00
|
|
|
let (nread, remote_addr) =
|
2020-12-16 11:14:12 -05:00
|
|
|
socket.recv_from(&mut buf).try_or_cancel(cancel).await?;
|
2022-10-25 16:50:55 -04:00
|
|
|
let path = remote_addr.as_pathname().map(pathstring).transpose()?;
|
|
|
|
Ok((nread, path))
|
|
|
|
}
|
|
|
|
|
2023-09-24 18:07:22 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[number]
|
|
|
|
pub async fn op_net_send_unixpacket<NP>(
|
2022-10-25 16:50:55 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-24 18:07:22 -04:00
|
|
|
#[smi] rid: ResourceId,
|
|
|
|
#[string] path: String,
|
|
|
|
#[buffer] zero_copy: JsBuffer,
|
2022-10-25 16:50:55 -04:00
|
|
|
) -> Result<usize, AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
|
|
|
let address_path = Path::new(&path);
|
|
|
|
{
|
|
|
|
let mut s = state.borrow_mut();
|
|
|
|
s.borrow_mut::<NP>()
|
|
|
|
.check_write(address_path, "Deno.DatagramConn.send()")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
|
|
|
.get::<UnixDatagramResource>(rid)
|
|
|
|
.map_err(|_| custom_error("NotConnected", "Socket has been closed"))?;
|
|
|
|
let socket = RcRef::map(&resource, |r| &r.socket)
|
|
|
|
.try_borrow_mut()
|
|
|
|
.ok_or_else(|| custom_error("Busy", "Socket already in use"))?;
|
|
|
|
let nwritten = socket.send_to(&zero_copy, address_path).await?;
|
|
|
|
|
|
|
|
Ok(nwritten)
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
|
2023-09-12 09:39:21 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-10-25 16:50:55 -04:00
|
|
|
pub fn op_net_listen_unix<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[string] path: String,
|
2023-10-03 22:37:39 -04:00
|
|
|
#[string] api_name: String,
|
2022-10-25 16:50:55 -04:00
|
|
|
) -> Result<(ResourceId, Option<String>), AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
|
|
|
let address_path = Path::new(&path);
|
|
|
|
let permissions = state.borrow_mut::<NP>();
|
2023-10-03 22:37:39 -04:00
|
|
|
let api_call_expr = format!("{}()", api_name);
|
|
|
|
permissions.check_read(address_path, &api_call_expr)?;
|
|
|
|
permissions.check_write(address_path, &api_call_expr)?;
|
2022-11-17 20:59:10 -05:00
|
|
|
let listener = UnixListener::bind(address_path)?;
|
2020-03-23 18:02:51 -04:00
|
|
|
let local_addr = listener.local_addr()?;
|
2022-10-25 16:50:55 -04:00
|
|
|
let pathname = local_addr.as_pathname().map(pathstring).transpose()?;
|
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);
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((rid, pathname))
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
pub fn net_listen_unixpacket<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-10-25 16:50:55 -04:00
|
|
|
path: String,
|
|
|
|
) -> Result<(ResourceId, Option<String>), AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
|
|
|
let address_path = Path::new(&path);
|
|
|
|
let permissions = state.borrow_mut::<NP>();
|
|
|
|
permissions.check_read(address_path, "Deno.listenDatagram()")?;
|
|
|
|
permissions.check_write(address_path, "Deno.listenDatagram()")?;
|
2022-11-17 20:59:10 -05:00
|
|
|
let socket = UnixDatagram::bind(address_path)?;
|
2020-03-23 18:02:51 -04:00
|
|
|
let local_addr = socket.local_addr()?;
|
2022-10-25 16:50:55 -04:00
|
|
|
let pathname = local_addr.as_pathname().map(pathstring).transpose()?;
|
2020-03-23 18:02:51 -04:00
|
|
|
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);
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((rid, pathname))
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
2021-04-06 04:33:43 -04:00
|
|
|
|
2023-09-12 09:39:21 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-11-10 16:03:28 -05:00
|
|
|
pub fn op_net_listen_unixpacket<NP>(
|
|
|
|
state: &mut OpState,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[string] path: String,
|
2022-11-10 16:03:28 -05:00
|
|
|
) -> Result<(ResourceId, Option<String>), AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
|
|
|
super::check_unstable(state, "Deno.listenDatagram");
|
|
|
|
net_listen_unixpacket::<NP>(state, path)
|
|
|
|
}
|
|
|
|
|
2023-09-12 09:39:21 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-11-10 16:03:28 -05:00
|
|
|
pub fn op_node_unstable_net_listen_unixpacket<NP>(
|
|
|
|
state: &mut OpState,
|
2023-09-12 09:39:21 -04:00
|
|
|
#[string] path: String,
|
2022-11-10 16:03:28 -05:00
|
|
|
) -> Result<(ResourceId, Option<String>), AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
|
|
|
net_listen_unixpacket::<NP>(state, path)
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
pub fn pathstring(pathname: &Path) -> Result<String, AnyError> {
|
|
|
|
into_string(pathname.into())
|
2021-04-06 04:33:43 -04:00
|
|
|
}
|