moto-rt 0.15.1

Motor OS Runtime.
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use super::netc;
use crate::ErrorCode;
use crate::RtFd;
use crate::RtVdsoVtable;
use crate::ok_or_error;
use core::sync::atomic::Ordering;
use core::time::Duration;

#[cfg(not(feature = "rustc-dep-of-std"))]
extern crate alloc;

// In theory, max UDP payload over IPv4 is
// 65507 = 65535 - 20 (IP header) - 8 (UDP header).
//
// But in practice smoltcp refuses to fragment UDP datagrams
// larger than 65493 bytes, so our practical MAX UDP payload is
// this weird number.
//
// Some argue that it does not make sense to fragment UDP
// datagrams, and so UDP payload should be 1472, or less.
// While smoltcp may well be susceptible to DDOS fragmentation
// atacks in this case, and so we may have to eventually
// disable UDP packet (de)fragmentation, for now we try
// to do our best and allow large UDP payloads.
pub const MAX_UDP_PAYLOAD: usize = 65493;

pub const SHUTDOWN_READ: u8 = 1;
pub const SHUTDOWN_WRITE: u8 = 2;

pub const PROTO_TCP: u8 = 1;
pub const PROTO_UDP: u8 = 2;

pub const SO_RCVTIMEO: u64 = 1;
pub const SO_SNDTIMEO: u64 = 2;
pub const SO_SHUTDOWN: u64 = 3;
pub const SO_NODELAY: u64 = 4;
pub const SO_TTL: u64 = 5;
pub const SO_NONBLOCKING: u64 = 6;
pub const SO_ERROR: u64 = 7;
pub const SO_ONLY_IPV6: u64 = 8;
pub const SO_LINGER: u64 = 9;
pub const SO_BROADCAST: u64 = 10;
pub const SO_MULTICAST_LOOP_V4: u64 = 11;
pub const SO_MULTICAST_LOOP_V6: u64 = 12;
pub const SO_MULTICAST_TTL_V4: u64 = 13;

fn setsockopt(rt_fd: RtFd, opt: u64, ptr: usize, len: usize) -> Result<(), ErrorCode> {
    let vdso_setsockopt: extern "C" fn(RtFd, u64, usize, usize) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_setsockopt.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_setsockopt(rt_fd, opt, ptr, len))
}

fn getsockopt(rt_fd: RtFd, opt: u64, ptr: usize, len: usize) -> Result<(), ErrorCode> {
    let vdso_getsockopt: extern "C" fn(RtFd, u64, usize, usize) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_getsockopt.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_getsockopt(rt_fd, opt, ptr, len))
}

fn setsockopt_bool(rt_fd: RtFd, val: bool, opt: u64) -> Result<(), ErrorCode> {
    let val: u8 = if val { 1 } else { 0 };
    setsockopt(rt_fd, opt, &val as *const _ as usize, 1)
}

fn getsockopt_bool(rt_fd: RtFd, opt: u64) -> Result<bool, ErrorCode> {
    let mut val = 0_u8;
    getsockopt(rt_fd, opt, &mut val as *mut _ as usize, 1)?;
    match val {
        0 => Ok(false),
        1 => Ok(true),
        _ => panic!("bad bool opt val {val} for opt {opt}"),
    }
}

pub fn bind(proto: u8, addr: &netc::sockaddr) -> Result<RtFd, ErrorCode> {
    let vdso_bind: extern "C" fn(u8, *const netc::sockaddr) -> RtFd = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_bind.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    to_result!(vdso_bind(proto, addr))
}

pub fn listen(rt_fd: RtFd, max_backlog: u32) -> Result<(), ErrorCode> {
    let vdso_listen: extern "C" fn(RtFd, u32) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_listen.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_listen(rt_fd, max_backlog))
}

pub fn accept(rt_fd: RtFd) -> Result<(RtFd, netc::sockaddr), ErrorCode> {
    let vdso_accept: extern "C" fn(RtFd, *mut netc::sockaddr) -> RtFd = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_accept.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let mut addr: netc::sockaddr = unsafe { core::mem::zeroed() };
    let res = vdso_accept(rt_fd, &mut addr);
    if res < 0 {
        return Err(-res as ErrorCode);
    }

    Ok((res, addr))
}

/// Create a TCP stream by connecting to a remote addr.
pub fn tcp_connect(
    addr: &netc::sockaddr,
    timeout: Duration,
    nonblocking: bool,
) -> Result<RtFd, ErrorCode> {
    let vdso_tcp_connect: extern "C" fn(*const netc::sockaddr, u64, bool) -> RtFd = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_tcp_connect.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let timeout = timeout.as_nanos().try_into().unwrap_or(u64::MAX);
    to_result!(vdso_tcp_connect(addr, timeout, nonblocking))
}

pub fn udp_connect(rt_fd: RtFd, addr: &netc::sockaddr) -> Result<(), ErrorCode> {
    let vdso_udp_connect: extern "C" fn(RtFd, *const netc::sockaddr) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_udp_connect.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_udp_connect(rt_fd, addr))
}

pub fn socket_addr(rt_fd: RtFd) -> Result<netc::sockaddr, ErrorCode> {
    let vdso_socket_addr: extern "C" fn(RtFd, *mut netc::sockaddr) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_socket_addr.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let mut addr: netc::sockaddr = unsafe { core::mem::zeroed() };
    let res = vdso_socket_addr(rt_fd, &mut addr);
    if res != crate::E_OK {
        return Err(res as ErrorCode);
    }

    Ok(addr)
}

pub fn peer_addr(rt_fd: RtFd) -> Result<netc::sockaddr, ErrorCode> {
    let vdso_peer_addr: extern "C" fn(RtFd, *mut netc::sockaddr) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_peer_addr.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let mut addr: netc::sockaddr = unsafe { core::mem::zeroed() };
    let res = vdso_peer_addr(rt_fd, &mut addr);
    if res != crate::E_OK {
        return Err(res as ErrorCode);
    }

    Ok(addr)
}

pub fn set_ttl(rt_fd: RtFd, ttl: u32) -> Result<(), ErrorCode> {
    setsockopt(rt_fd, SO_TTL, &ttl as *const _ as usize, 4)
}

pub fn ttl(rt_fd: RtFd) -> Result<u32, ErrorCode> {
    let mut ttl = 0_u32;
    getsockopt(rt_fd, SO_TTL, &mut ttl as *mut _ as usize, 4)?;
    Ok(ttl)
}

pub fn set_only_v6(rt_fd: RtFd, only_v6: bool) -> Result<(), ErrorCode> {
    setsockopt_bool(rt_fd, only_v6, SO_ONLY_IPV6)
}

pub fn only_v6(rt_fd: RtFd) -> Result<bool, ErrorCode> {
    getsockopt_bool(rt_fd, SO_ONLY_IPV6)
}

pub fn take_error(rt_fd: RtFd) -> Result<ErrorCode, ErrorCode> {
    let mut error = 0_u16;
    getsockopt(rt_fd, SO_ERROR, &mut error as *mut _ as usize, 2)?;
    Ok(error as ErrorCode)
}

pub fn set_nonblocking(rt_fd: RtFd, nonblocking: bool) -> Result<(), ErrorCode> {
    setsockopt_bool(rt_fd, nonblocking, SO_NONBLOCKING)
}

pub fn peek(rt_fd: RtFd, buf: &mut [u8]) -> Result<usize, ErrorCode> {
    let vdso_peek: extern "C" fn(i32, *mut u8, usize) -> i64 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_peek.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    to_result!(vdso_peek(rt_fd, buf.as_mut_ptr(), buf.len()))
}

pub fn set_read_timeout(rt_fd: RtFd, timeout: Option<Duration>) -> Result<(), ErrorCode> {
    let timeout: u64 = match timeout {
        Some(dur) => dur.as_nanos().try_into().unwrap_or(u64::MAX),
        None => u64::MAX,
    };

    if timeout == 0 {
        // See TcpStream::set_read_timeout() doc in Rust stdlib.
        return Err(crate::E_INVALID_ARGUMENT);
    }

    setsockopt(
        rt_fd,
        SO_RCVTIMEO,
        &timeout as *const _ as usize,
        core::mem::size_of::<u64>(),
    )
}

pub fn read_timeout(rt_fd: RtFd) -> Result<Option<Duration>, ErrorCode> {
    let mut timeout_ns = 0_u64;

    getsockopt(
        rt_fd,
        SO_RCVTIMEO,
        &mut timeout_ns as *mut _ as usize,
        core::mem::size_of::<u64>(),
    )?;

    if timeout_ns == u64::MAX {
        Ok(None)
    } else {
        Ok(Some(Duration::from_nanos(timeout_ns)))
    }
}

pub fn set_write_timeout(rt_fd: RtFd, timeout: Option<Duration>) -> Result<(), ErrorCode> {
    let timeout: u64 = match timeout {
        Some(dur) => dur.as_nanos().try_into().unwrap_or(u64::MAX),
        None => u64::MAX,
    };

    if timeout == 0 {
        // See TcpStream::set_write_timeout() doc in Rust stdlib.
        return Err(crate::E_INVALID_ARGUMENT);
    }

    setsockopt(
        rt_fd,
        SO_SNDTIMEO,
        &timeout as *const _ as usize,
        core::mem::size_of::<u64>(),
    )
}

pub fn write_timeout(rt_fd: RtFd) -> Result<Option<Duration>, ErrorCode> {
    let mut timeout_ns = 0_u64;

    getsockopt(
        rt_fd,
        SO_SNDTIMEO,
        &mut timeout_ns as *mut _ as usize,
        core::mem::size_of::<u64>(),
    )?;

    if timeout_ns == u64::MAX {
        Ok(None)
    } else {
        Ok(Some(Duration::from_nanos(timeout_ns)))
    }
}

pub fn shutdown(rt_fd: RtFd, shutdown: u8) -> Result<(), ErrorCode> {
    if 0 != ((shutdown & !SHUTDOWN_READ) & !SHUTDOWN_WRITE) {
        return Err(crate::E_INVALID_ARGUMENT);
    }

    setsockopt(rt_fd, SO_SHUTDOWN, &shutdown as *const _ as usize, 1)
}

const MAX_LINGER_MS: u64 = 60_000; // 60 sec.
pub fn set_linger(rt_fd: RtFd, timeout: Option<Duration>) -> Result<(), ErrorCode> {
    let linger_millis: u64 = if let Some(timo) = timeout {
        let millis = timo.as_millis();
        if millis > (MAX_LINGER_MS as u128) {
            MAX_LINGER_MS
        } else {
            millis as u64
        }
    } else {
        u64::MAX
    };
    setsockopt(rt_fd, SO_LINGER, &linger_millis as *const u64 as usize, 8)
}

pub fn linger(rt_fd: RtFd) -> Result<Option<Duration>, ErrorCode> {
    let mut linger_millis = 0_u64;
    getsockopt(rt_fd, SO_LINGER, &mut linger_millis as *mut _ as usize, 8)?;
    match linger_millis {
        val if val <= MAX_LINGER_MS => Ok(Some(Duration::from_millis(val))),
        u64::MAX => Ok(None),
        _ => panic!("bad linger {linger_millis}"),
    }
}

pub fn set_nodelay(rt_fd: RtFd, nodelay: bool) -> Result<(), ErrorCode> {
    setsockopt_bool(rt_fd, nodelay, SO_NODELAY)
}

pub fn nodelay(rt_fd: RtFd) -> Result<bool, ErrorCode> {
    getsockopt_bool(rt_fd, SO_NODELAY)
}

pub fn set_udp_broadcast(rt_fd: RtFd, val: bool) -> Result<(), ErrorCode> {
    setsockopt_bool(rt_fd, val, SO_BROADCAST)
}

pub fn udp_broadcast(rt_fd: RtFd) -> Result<bool, ErrorCode> {
    getsockopt_bool(rt_fd, SO_BROADCAST)
}

pub fn udp_recv_from(rt_fd: RtFd, buf: &mut [u8]) -> Result<(usize, netc::sockaddr), ErrorCode> {
    let mut addr: netc::sockaddr = unsafe { core::mem::zeroed() };

    let vdso_udp_recv_from: extern "C" fn(i32, *mut u8, usize, *mut netc::sockaddr) -> i64 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get()
                .net_udp_recv_from
                .load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let res = vdso_udp_recv_from(rt_fd, buf.as_mut_ptr(), buf.len(), &mut addr as *mut _);
    if res < 0 {
        Err((-res) as ErrorCode)
    } else {
        Ok(((res as usize), addr))
    }
}

pub fn udp_peek_from(rt_fd: RtFd, buf: &mut [u8]) -> Result<(usize, netc::sockaddr), ErrorCode> {
    let mut addr: netc::sockaddr = unsafe { core::mem::zeroed() };

    let vdso_udp_peek_from: extern "C" fn(i32, *mut u8, usize, *mut netc::sockaddr) -> i64 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get()
                .net_udp_peek_from
                .load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let res = vdso_udp_peek_from(rt_fd, buf.as_mut_ptr(), buf.len(), &mut addr as *mut _);
    if res < 0 {
        Err((-res) as ErrorCode)
    } else {
        Ok(((res as usize), addr))
    }
}

pub fn udp_send_to(rt_fd: RtFd, buf: &[u8], addr: &netc::sockaddr) -> Result<usize, ErrorCode> {
    let vdso_udp_send_to: extern "C" fn(i32, *const u8, usize, *const netc::sockaddr) -> i64 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().net_udp_send_to.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    to_result!(vdso_udp_send_to(
        rt_fd,
        buf.as_ptr(),
        buf.len(),
        addr as *const _
    ))
}

pub fn set_udp_multicast_loop_v4(rt_fd: RtFd, val: bool) -> Result<(), ErrorCode> {
    setsockopt_bool(rt_fd, val, SO_MULTICAST_LOOP_V4)
}

pub fn udp_multicast_loop_v4(rt_fd: RtFd) -> Result<bool, ErrorCode> {
    getsockopt_bool(rt_fd, SO_MULTICAST_LOOP_V4)
}

pub fn set_udp_multicast_ttl_v4(rt_fd: RtFd, val: u32) -> Result<(), ErrorCode> {
    setsockopt(rt_fd, SO_MULTICAST_TTL_V4, &val as *const _ as usize, 4)
}

pub fn udp_multicast_ttl_v4(rt_fd: RtFd) -> Result<u32, ErrorCode> {
    let mut ttl = 0_u32;
    getsockopt(rt_fd, SO_MULTICAST_TTL_V4, &mut ttl as *mut _ as usize, 4)?;
    Ok(ttl)
}

pub fn set_udp_multicast_loop_v6(rt_fd: RtFd, val: bool) -> Result<(), ErrorCode> {
    setsockopt_bool(rt_fd, val, SO_MULTICAST_LOOP_V6)
}

pub fn udp_multicast_loop_v6(rt_fd: RtFd) -> Result<bool, ErrorCode> {
    getsockopt_bool(rt_fd, SO_MULTICAST_LOOP_V6)
}

pub const JOIN_MULTICAST_OP: u64 = 1;
pub const LEAVE_MULTICAST_OP: u64 = 2;

pub fn join_udp_multicast_v4(
    rt_fd: RtFd,
    addr: &netc::in_addr,
    iface: &netc::in_addr,
) -> Result<(), ErrorCode> {
    let vdso_multicast_op_v4: extern "C" fn(
        i32,
        u64,
        *const netc::in_addr,
        *const netc::in_addr,
    ) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get()
                .net_udp_multicast_op_v4
                .load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_multicast_op_v4(
        rt_fd,
        JOIN_MULTICAST_OP,
        addr as *const _,
        iface as *const _,
    ))
}

pub fn leave_udp_multicast_v4(
    rt_fd: RtFd,
    addr: &netc::in_addr,
    iface: &netc::in_addr,
) -> Result<(), ErrorCode> {
    let vdso_multicast_op_v4: extern "C" fn(
        i32,
        u64,
        *const netc::in_addr,
        *const netc::in_addr,
    ) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get()
                .net_udp_multicast_op_v4
                .load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_multicast_op_v4(
        rt_fd,
        LEAVE_MULTICAST_OP,
        addr as *const _,
        iface as *const _,
    ))
}

pub fn join_udp_multicast_v6(
    rt_fd: RtFd,
    addr: &netc::in6_addr,
    iface: u32,
) -> Result<(), ErrorCode> {
    let vdso_multicast_op_v6: extern "C" fn(i32, u64, *const netc::in6_addr, u32) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get()
                .net_udp_multicast_op_v6
                .load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_multicast_op_v6(
        rt_fd,
        JOIN_MULTICAST_OP,
        addr as *const _,
        iface,
    ))
}

pub fn leave_udp_multicast_v6(
    rt_fd: RtFd,
    addr: &netc::in6_addr,
    iface: u32,
) -> Result<(), ErrorCode> {
    let vdso_multicast_op_v6: extern "C" fn(i32, u64, *const netc::in6_addr, u32) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get()
                .net_udp_multicast_op_v6
                .load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_multicast_op_v6(
        rt_fd,
        LEAVE_MULTICAST_OP,
        addr as *const _,
        iface,
    ))
}

pub fn lookup_host(
    host: &str,
    port: u16,
) -> Result<(u16, alloc::collections::VecDeque<netc::sockaddr>), ErrorCode> {
    let vdso_lookup: extern "C" fn(
        /* host_bytes */ *const u8,
        /* host_bytes_sz */ usize,
        /* port */ u16,
        /* result_addr */ *mut usize,
        /* result_len */ *mut usize,
    ) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().dns_lookup.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let mut result_addr: usize = 0;
    let mut result_num: usize = 0;

    let res = vdso_lookup(
        host.as_bytes().as_ptr(),
        host.len(),
        port,
        &mut result_addr,
        &mut result_num,
    );
    if res != crate::E_OK {
        return Err(res);
    }

    let addresses: &[netc::sockaddr] =
        unsafe { core::slice::from_raw_parts(result_addr as *const netc::sockaddr, result_num) };

    let mut vecdec = alloc::collections::VecDeque::new();
    for addr in addresses {
        vecdec.push_back(*addr);
    }

    let layout = core::alloc::Layout::from_size_align(
        core::mem::size_of::<netc::sockaddr>() * result_num,
        16,
    )
    .unwrap();
    unsafe { crate::alloc::dealloc(result_addr as *mut u8, layout) };

    Ok((port, vecdec))
}