summaryrefslogtreecommitdiff
path: root/ext/net/ops_unix.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-10-25 20:32:51 +0200
committerGitHub <noreply@github.com>2022-10-25 20:32:51 +0200
commit8e3f825c921b38141afa7a69a0664881c5c94461 (patch)
tree09ffac0d9118d7d5214e247dbf55db06398f1bf9 /ext/net/ops_unix.rs
parent1f6aeb430b71c16c2d9525edba63032d9ac7b372 (diff)
Revert "refactor(ext/net): clean up variadic network ops (#16392)" (#16417)
Should fix https://github.com/denoland/deno_std/issues/2807
Diffstat (limited to 'ext/net/ops_unix.rs')
-rw-r--r--ext/net/ops_unix.rs157
1 files changed, 46 insertions, 111 deletions
diff --git a/ext/net/ops_unix.rs b/ext/net/ops_unix.rs
index b45b02343..181dcacec 100644
--- a/ext/net/ops_unix.rs
+++ b/ext/net/ops_unix.rs
@@ -1,18 +1,20 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use crate::io::UnixStreamResource;
-use crate::NetPermissions;
+use crate::ops::AcceptArgs;
+use crate::ops::OpAddr;
+use crate::ops::OpConn;
+use crate::ops::OpPacket;
+use crate::ops::ReceiveArgs;
use deno_core::error::bad_resource;
use deno_core::error::custom_error;
use deno_core::error::AnyError;
-use deno_core::op;
use deno_core::AsyncRefCell;
use deno_core::CancelHandle;
use deno_core::CancelTryFuture;
use deno_core::OpState;
use deno_core::RcRef;
use deno_core::Resource;
-use deno_core::ResourceId;
use deno_core::ZeroCopyBuf;
use serde::Deserialize;
use serde::Serialize;
@@ -72,11 +74,13 @@ pub struct UnixListenArgs {
pub path: String,
}
-#[op]
-pub async fn op_net_accept_unix(
+pub(crate) async fn accept_unix(
state: Rc<RefCell<OpState>>,
- rid: ResourceId,
-) -> Result<(ResourceId, Option<String>, Option<String>), AnyError> {
+ args: AcceptArgs,
+ _: (),
+) -> Result<OpConn, AnyError> {
+ let rid = args.rid;
+
let resource = state
.borrow()
.resource_table
@@ -94,52 +98,27 @@ pub async fn op_net_accept_unix(
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 resource = UnixStreamResource::new(unix_stream.into_split());
let mut state = state.borrow_mut();
let rid = state.resource_table.add(resource);
- Ok((rid, local_addr_path, remote_addr_path))
-}
-
-#[op]
-pub async fn op_net_connect_unix<NP>(
- state: Rc<RefCell<OpState>>,
- path: String,
-) -> Result<(ResourceId, Option<String>, Option<String>), AnyError>
-where
- NP: NetPermissions + 'static,
-{
- let address_path = Path::new(&path);
- super::check_unstable2(&state, "Deno.connect");
- {
- 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))
+ 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),
+ })),
+ })
}
-#[op]
-pub async fn op_net_recv_unixpacket(
+pub(crate) async fn receive_unix_packet(
state: Rc<RefCell<OpState>>,
- rid: ResourceId,
+ args: ReceiveArgs,
mut buf: ZeroCopyBuf,
-) -> Result<(usize, Option<String>), AnyError> {
+) -> Result<OpPacket, AnyError> {
+ let rid = args.rid;
+
let resource = state
.borrow()
.resource_table
@@ -149,90 +128,46 @@ pub async fn op_net_recv_unixpacket(
.try_borrow_mut()
.ok_or_else(|| custom_error("Busy", "Socket already in use"))?;
let cancel = RcRef::map(resource, |r| &r.cancel);
- let (nread, remote_addr) =
+ let (size, remote_addr) =
socket.recv_from(&mut buf).try_or_cancel(cancel).await?;
- let path = remote_addr.as_pathname().map(pathstring).transpose()?;
- Ok((nread, path))
-}
-
-#[op]
-async fn op_net_send_unixpacket<NP>(
- state: Rc<RefCell<OpState>>,
- rid: ResourceId,
- path: String,
- zero_copy: ZeroCopyBuf,
-) -> 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)
+ Ok(OpPacket {
+ size,
+ remote_addr: OpAddr::UnixPacket(UnixAddr {
+ path: remote_addr.as_pathname().and_then(pathstring),
+ }),
+ })
}
-#[op]
-pub fn op_net_listen_unix<NP>(
+pub fn listen_unix(
state: &mut OpState,
- path: String,
-) -> Result<(ResourceId, Option<String>), AnyError>
-where
- NP: NetPermissions + 'static,
-{
- let address_path = Path::new(&path);
- super::check_unstable(state, "Deno.listen");
- let permissions = state.borrow_mut::<NP>();
- permissions.check_read(address_path, "Deno.listen()")?;
- permissions.check_write(address_path, "Deno.listen()")?;
- let listener = UnixListener::bind(&address_path)?;
+ addr: &Path,
+) -> Result<(u32, tokio::net::unix::SocketAddr), AnyError> {
+ let listener = UnixListener::bind(&addr)?;
let local_addr = listener.local_addr()?;
- let pathname = local_addr.as_pathname().map(pathstring).transpose()?;
let listener_resource = UnixListenerResource {
listener: AsyncRefCell::new(listener),
cancel: Default::default(),
};
let rid = state.resource_table.add(listener_resource);
- Ok((rid, pathname))
+
+ Ok((rid, local_addr))
}
-#[op]
-pub fn op_net_listen_unixpacket<NP>(
+pub fn listen_unix_packet(
state: &mut OpState,
- path: String,
-) -> Result<(ResourceId, Option<String>), AnyError>
-where
- NP: NetPermissions + 'static,
-{
- let address_path = Path::new(&path);
- super::check_unstable(state, "Deno.listenDatagram");
- let permissions = state.borrow_mut::<NP>();
- permissions.check_read(address_path, "Deno.listenDatagram()")?;
- permissions.check_write(address_path, "Deno.listenDatagram()")?;
- let socket = UnixDatagram::bind(&address_path)?;
+ addr: &Path,
+) -> Result<(u32, tokio::net::unix::SocketAddr), AnyError> {
+ let socket = UnixDatagram::bind(&addr)?;
let local_addr = socket.local_addr()?;
- let pathname = local_addr.as_pathname().map(pathstring).transpose()?;
let datagram_resource = UnixDatagramResource {
socket: AsyncRefCell::new(socket),
cancel: Default::default(),
};
let rid = state.resource_table.add(datagram_resource);
- Ok((rid, pathname))
+
+ Ok((rid, local_addr))
}
-pub fn pathstring(pathname: &Path) -> Result<String, AnyError> {
- into_string(pathname.into())
+pub fn pathstring(pathname: &Path) -> Option<String> {
+ into_string(pathname.into()).ok()
}