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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use crate::error::{LxError, LxResult};
use crate::fs::{FileLike, OpenFlags, PollStatus};
use crate::net::*;
use alloc::{boxed::Box, sync::Arc, vec};
use async_trait::async_trait;
use lock::Mutex;
use smoltcp::socket::{UdpPacketMetadata, UdpSocket, UdpSocketBuffer};
#[allow(unused_imports)]
use zircon_object::impl_kobject;
#[allow(unused_imports)]
use zircon_object::object::*;
pub struct UdpSocketState {
base: KObjectBase,
inner: Mutex<UdpInner>,
}
pub struct UdpInner {
handle: GlobalSocketHandle,
remote_endpoint: Option<IpEndpoint>,
flags: OpenFlags,
}
impl Default for UdpSocketState {
fn default() -> Self {
UdpSocketState::new()
}
}
impl UdpSocketState {
pub fn new() -> Self {
info!("udp new");
let rx_buffer = UdpSocketBuffer::new(
vec![UdpPacketMetadata::EMPTY; UDP_METADATA_BUF],
vec![0; UDP_RECVBUF],
);
let tx_buffer = UdpSocketBuffer::new(
vec![UdpPacketMetadata::EMPTY; UDP_METADATA_BUF],
vec![0; UDP_SENDBUF],
);
let socket = UdpSocket::new(rx_buffer, tx_buffer);
let handle = GlobalSocketHandle(get_sockets().lock().add(socket));
UdpSocketState {
base: KObjectBase::new(),
inner: Mutex::new(UdpInner {
handle,
remote_endpoint: None,
flags: OpenFlags::RDWR,
}),
}
}
}
#[async_trait]
impl Socket for UdpSocketState {
async fn read(&self, data: &mut [u8]) -> (SysResult, Endpoint) {
info!("udp read");
let inner = self.inner.lock();
loop {
let sets = get_sockets();
let mut sets = sets.lock();
let mut socket = sets.get::<UdpSocket>(inner.handle.0);
let copied_len = socket.recv_slice(data);
drop(socket);
drop(sets);
match copied_len {
Ok((size, endpoint)) => return (Ok(size), Endpoint::Ip(endpoint)),
Err(smoltcp::Error::Exhausted) => {
poll_ifaces();
if inner.flags.contains(OpenFlags::NON_BLOCK) {
debug!("NON_BLOCK: Try again later...");
return (Err(LxError::EAGAIN), Endpoint::Ip(IpEndpoint::UNSPECIFIED));
} else {
trace!("udp Exhausted. try again")
}
}
Err(err) => {
error!("udp socket recv_slice error: {:?}", err);
return (
Err(LxError::ENOTCONN),
Endpoint::Ip(IpEndpoint::UNSPECIFIED),
);
}
}
}
}
fn write(&self, data: &[u8], sendto_endpoint: Option<Endpoint>) -> SysResult {
info!("udp write");
let inner = self.inner.lock();
let remote_endpoint = {
if let Some(Endpoint::Ip(ref endpoint)) = sendto_endpoint {
endpoint
} else if let Some(ref endpoint) = inner.remote_endpoint {
endpoint
} else {
return Err(LxError::ENOTCONN);
}
};
let sets = get_sockets();
let mut sets = sets.lock();
let mut socket = sets.get::<UdpSocket>(inner.handle.0);
if socket.endpoint().port == 0 {
socket
.bind(IpEndpoint::new(
IpAddress::Unspecified,
get_ephemeral_port(),
))
.unwrap();
}
let _len = socket.send_slice(data, *remote_endpoint);
drop(socket);
drop(sets);
poll_ifaces();
Ok(data.len())
}
async fn connect(&self, endpoint: Endpoint) -> SysResult {
if let Endpoint::Ip(ip) = endpoint {
self.inner.lock().remote_endpoint = Some(ip);
Ok(0)
} else {
Err(LxError::EINVAL)
}
}
fn poll(&self, events: PollEvents) -> (bool, bool, bool) {
let inner = self.inner.lock();
let (recv_state, send_state) = {
let sets = get_sockets();
let mut sets = sets.lock();
let socket = sets.get::<UdpSocket>(inner.handle.0);
(socket.can_recv(), socket.can_send())
};
if (events.contains(PollEvents::IN) && !recv_state)
|| (events.contains(PollEvents::OUT) && !send_state)
{
poll_ifaces();
}
let (mut input, mut output, mut err) = (false, false, false);
let sets = get_sockets();
let mut sets = sets.lock();
let socket = sets.get::<UdpSocket>(inner.handle.0);
if !socket.is_open() {
err = true;
} else {
if socket.can_recv() {
input = true;
}
if socket.can_send() {
output = true;
}
}
debug!("udp poll: {:?}", (input, output, err));
(input, output, err)
}
fn bind(&self, endpoint: Endpoint) -> SysResult {
info!("udp bind");
#[allow(irrefutable_let_patterns)]
if let Endpoint::Ip(mut ip) = endpoint {
if ip.port == 0 {
ip.port = get_ephemeral_port();
}
let sockets = get_sockets();
let mut set = sockets.lock();
let mut socket = set.get::<UdpSocket>(self.inner.lock().handle.0);
match socket.bind(ip) {
Ok(()) => Ok(0),
Err(_) => Err(LxError::EINVAL),
}
} else {
Err(LxError::EINVAL)
}
}
fn listen(&self) -> SysResult {
warn!("listen is unimplemented");
Err(LxError::EINVAL)
}
fn shutdown(&self) -> SysResult {
warn!("shutdown is unimplemented");
Err(LxError::EINVAL)
}
async fn accept(&self) -> LxResult<(Arc<dyn FileLike>, Endpoint)> {
warn!("accept is unimplemented");
Err(LxError::EINVAL)
}
fn endpoint(&self) -> Option<Endpoint> {
let net_sockets = get_sockets();
let mut sockets = net_sockets.lock();
let socket = sockets.get::<UdpSocket>(self.inner.lock().handle.0);
let endpoint = socket.endpoint();
if endpoint.port != 0 {
Some(Endpoint::Ip(endpoint))
} else {
None
}
}
fn remote_endpoint(&self) -> Option<Endpoint> {
self.inner.lock().remote_endpoint.map(Endpoint::Ip)
}
fn setsockopt(&self, _level: usize, _opt: usize, _data: &[u8]) -> SysResult {
warn!("setsockopt is unimplemented");
Ok(0)
}
fn ioctl(&self, request: usize, arg1: usize, _arg2: usize, _arg3: usize) -> SysResult {
warn!("ioctl is unimplemented for this socket");
info!("udp ioctrl");
match request {
0x8954 => {
#[allow(unsafe_code)]
let req = unsafe { &mut *(arg1 as *mut ArpReq) };
if let AddressFamily::Internet = AddressFamily::from(req.arp_pa.family) {
let name = req.arp_dev.as_ptr();
#[allow(unsafe_code)]
let _ifname = unsafe { from_cstr(name) };
let addr = &req.arp_pa as *const SockAddrPlaceholder as *const SockAddr;
#[allow(unsafe_code)]
let _addr = unsafe {
IpAddress::from(Ipv4Address::from_bytes(
&u32::from_be((*addr).addr_in.sin_addr).to_be_bytes()[..],
))
};
Err(LxError::ENOENT)
} else {
Err(LxError::EINVAL)
}
}
_ => Ok(0),
}
}
fn get_buffer_capacity(&self) -> Option<(usize, usize)> {
let sockets = get_sockets();
let mut set = sockets.lock();
let socket = set.get::<UdpSocket>(self.inner.lock().handle.0);
let (recv_ca, send_ca) = (
socket.payload_recv_capacity(),
socket.payload_send_capacity(),
);
Some((recv_ca, send_ca))
}
fn socket_type(&self) -> Option<SocketType> {
Some(SocketType::SOCK_DGRAM)
}
}
impl_kobject!(UdpSocketState);
#[async_trait]
impl FileLike for UdpSocketState {
fn flags(&self) -> OpenFlags {
self.inner.lock().flags
}
fn set_flags(&self, f: OpenFlags) -> LxResult {
let flags = &mut self.inner.lock().flags;
flags.set(OpenFlags::APPEND, f.contains(OpenFlags::APPEND));
flags.set(OpenFlags::NON_BLOCK, f.contains(OpenFlags::NON_BLOCK));
flags.set(OpenFlags::CLOEXEC, f.contains(OpenFlags::CLOEXEC));
Ok(())
}
async fn read(&self, buf: &mut [u8]) -> LxResult<usize> {
Socket::read(self, buf).await.0
}
async fn read_at(&self, _offset: u64, _buf: &mut [u8]) -> LxResult<usize> {
unimplemented!()
}
fn write(&self, buf: &[u8]) -> LxResult<usize> {
Socket::write(self, buf, None)
}
fn poll(&self, events: PollEvents) -> LxResult<PollStatus> {
let (read, write, error) = Socket::poll(self, events);
Ok(PollStatus { read, write, error })
}
async fn async_poll(&self, events: PollEvents) -> LxResult<PollStatus> {
let (read, write, error) = Socket::poll(self, events);
Ok(PollStatus { read, write, error })
}
fn ioctl(&self, request: usize, arg1: usize, arg2: usize, arg3: usize) -> LxResult<usize> {
Socket::ioctl(self, request, arg1, arg2, arg3)
}
fn as_socket(&self) -> LxResult<&dyn Socket> {
Ok(self)
}
}