deno.land / x / deno@v1.28.2 / ext / net / ops_unix.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use crate::io::UnixStreamResource;use crate::NetPermissions;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;use std::borrow::Cow;use std::cell::RefCell;use std::path::Path;use std::rc::Rc;use tokio::net::UnixDatagram;use tokio::net::UnixListener;pub use tokio::net::UnixStream;
/// A utility function to map OsStrings to Stringspub 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) })}
struct UnixListenerResource { listener: AsyncRefCell<UnixListener>, cancel: CancelHandle,}
impl Resource for UnixListenerResource { fn name(&self) -> Cow<str> { "unixListener".into() }
fn close(self: Rc<Self>) { self.cancel.cancel(); }}
pub struct UnixDatagramResource { 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(); }}
#[derive(Serialize)]pub struct UnixAddr { pub path: Option<String>,}
#[derive(Deserialize)]pub struct UnixListenArgs { pub path: String,}
#[op]pub async fn op_net_accept_unix( state: Rc<RefCell<OpState>>, rid: ResourceId,) -> Result<(ResourceId, Option<String>, Option<String>), AnyError> { let resource = state .borrow() .resource_table .get::<UnixListenerResource>(rid) .map_err(|_| bad_resource("Listener has been closed"))?; let 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 .map_err(crate::ops::accept_err)?;
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))}
#[op]pub async fn op_net_recv_unixpacket( state: Rc<RefCell<OpState>>, rid: ResourceId, mut buf: ZeroCopyBuf,) -> Result<(usize, Option<String>), AnyError> { let resource = state .borrow() .resource_table .get::<UnixDatagramResource>(rid) .map_err(|_| bad_resource("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 cancel = RcRef::map(resource, |r| &r.cancel); let (nread, 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)}
#[op]pub fn op_net_listen_unix<NP>( 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)?; 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))}
pub fn net_listen_unixpacket<NP>( state: &mut OpState, 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()")?; let socket = UnixDatagram::bind(address_path)?; 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))}
#[op]pub fn op_net_listen_unixpacket<NP>( state: &mut OpState, path: String,) -> Result<(ResourceId, Option<String>), AnyError>where NP: NetPermissions + 'static,{ super::check_unstable(state, "Deno.listenDatagram"); net_listen_unixpacket::<NP>(state, path)}
#[op]pub fn op_node_unstable_net_listen_unixpacket<NP>( state: &mut OpState, path: String,) -> Result<(ResourceId, Option<String>), AnyError>where NP: NetPermissions + 'static,{ net_listen_unixpacket::<NP>(state, path)}
pub fn pathstring(pathname: &Path) -> Result<String, AnyError> { into_string(pathname.into())}
deno

Version Info

Tagged at
a year ago