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
/*
 * Copyright (c)2013-2021 ZeroTier, Inc.
 *
 * Use of this software is governed by the Business Source License included
 * in the LICENSE.TXT file in the project's root directory.
 *
 * Change Date: 2026-01-01
 *
 * On the date above, in accordance with the Business Source License, use
 * of this software will be governed by version 2.0 of the Apache License.
 */
/****/

use std::convert::TryInto;
use std::ffi::{c_void, CString};
use std::io::{self /*, Error, ErrorKind*/};
use std::net::{/*Ipv4Addr, Ipv6Addr,*/ SocketAddr, ToSocketAddrs};
use std::os::raw::c_int;
use std::time::Duration;
//use std::cmp;

use crate::socket::Socket;
use crate::utils::*;

//----------------------------------------------------------------------------//
// UdpSocketImpl                                                              //
//----------------------------------------------------------------------------//

pub struct UdpSocketImpl {
    inner: Socket,
}

impl UdpSocketImpl {
    pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<UdpSocketImpl> {
        let addr = addr?;
        let socket = Socket::new(addr, ZTS_SOCK_DGRAM as i32)?;
        // TODO: Possibly set SO_REUSEADDR
        //let (addrp, len) = addr.into_inner();
        unsafe {
            //zts_bsd_bind(*socket.as_inner(), addrp, len as _);
            // TODO: Find a better way to split this address string
            let full_str = addr.to_string();
            let full_vec = full_str.split(":");
            let lvec: Vec<&str> = full_vec.collect();
            let addr_str = lvec[0];
            let port = addr.port();
            // TODO: Handle native error code, consider cvt?
            // This is a false-positive by the linter
            // See: https://github.com/rust-lang/rust/issues/78691
            #[allow(temporary_cstring_as_ptr)]
            zts_bind(
                *socket.as_inner(),
                CString::new(addr_str).unwrap().as_ptr(),
                port,
            );
        }
        Ok(UdpSocketImpl { inner: socket })
    }

    pub fn socket(&self) -> &Socket {
        &self.inner
    }

    pub fn into_socket(self) -> Socket {
        self.inner
    }

    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
        sockname(|buf, len| unsafe { zts_bsd_getpeername(*self.inner.as_inner(), buf, len) })
    }

    pub fn socket_addr(&self) -> io::Result<SocketAddr> {
        sockname(|buf, len| unsafe { zts_bsd_getsockname(*self.inner.as_inner(), buf, len) })
    }

    pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
        self.inner.recv_from(buf)
    }

    pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
        self.inner.peek_from(buf)
    }

    /*
        pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> {
            let len = cmp::min(buf.len(), <size_t>::MAX as usize) as size_t;
            let (dstp, dstlen) = dst.into_inner();
            let ret = cvt(unsafe {
                zts_bsd_sendto(
                    *self.inner.as_inner(),
                    buf.as_ptr() as *const c_void,
                    len,
                    ZTS_MSG_NOSIGNAL,
                    dstp,
                    dstlen,
                )
            })?;
            Ok(ret as usize)
        }
    */

    pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
        self.inner.set_timeout(dur, ZTS_SO_RCVTIMEO as i32)
    }

    pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
        self.inner.set_timeout(dur, ZTS_SO_SNDTIMEO as i32)
    }

    pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
        self.inner.timeout(ZTS_SO_RCVTIMEO as i32)
    }

    pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
        self.inner.timeout(ZTS_SO_SNDTIMEO as i32)
    }

    pub fn set_broadcast(&self, broadcast: bool) -> io::Result<()> {
        setsockopt(
            &self.inner,
            ZTS_SOL_SOCKET as i32,
            ZTS_SO_BROADCAST as i32,
            broadcast as c_int,
        )
    }

    pub fn broadcast(&self) -> io::Result<bool> {
        let raw: c_int = getsockopt(&self.inner, ZTS_SOL_SOCKET as i32, ZTS_SO_BROADCAST as i32)?;
        Ok(raw != 0)
    }
    /*
        pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
            setsockopt(
                &self.inner,
                ZTS_IPPROTO_IP,
                ZTS_IP_MULTICAST_LOOP,
                multicast_loop_v4 as IpV4MultiCastType,
            )
        }

        pub fn multicast_loop_v4(&self) -> io::Result<bool> {
            let raw: IpV4MultiCastType = getsockopt(&self.inner, ZTS_IPPROTO_IP, ZTS_IP_MULTICAST_LOOP)?;
            Ok(raw != 0)
        }

        pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
            setsockopt(
                &self.inner,
                ZTS_IPPROTO_IP,
                ZTS_IP_MULTICAST_TTL,
                multicast_ttl_v4 as IpV4MultiCastType,
            )
        }

        pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
            let raw: IpV4MultiCastType = getsockopt(&self.inner, ZTS_IPPROTO_IP, ZTS_IP_MULTICAST_TTL)?;
            Ok(raw as u32)
        }

        pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
            setsockopt(&self.inner, ZTS_IPPROTO_IPV6, ZTS_IPV6_MULTICAST_LOOP, multicast_loop_v6 as c_int)
        }

        pub fn multicast_loop_v6(&self) -> io::Result<bool> {
            let raw: c_int = getsockopt(&self.inner, ZTS_IPPROTO_IPV6, ZTS_IPV6_MULTICAST_LOOP)?;
            Ok(raw != 0)
        }

        pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
            let mreq = zts_ip_mreq {
                imr_multiaddr: multiaddr.into_inner(),
                imr_interface: interface.into_inner(),
            };
            setsockopt(&self.inner, ZTS_IPPROTO_IP, ZTS_IP_ADD_MEMBERSHIP, mreq)
        }

        pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
            let mreq = zts_ipv6_mreq {
                ipv6mr_multiaddr: *multiaddr.as_inner(),
                ipv6mr_interface: to_ipv6mr_interface(interface),
            };
            setsockopt(&self.inner, ZTS_IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, mreq)
        }

        pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
            let mreq = zts_ip_mreq {
                imr_multiaddr: multiaddr.into_inner(),
                imr_interface: interface.into_inner(),
            };
            setsockopt(&self.inner, ZTS_IPPROTO_IP, ZTS_IP_DROP_MEMBERSHIP, mreq)
        }

        pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
            let mreq = zts_ipv6_mreq {
                ipv6mr_multiaddr: *multiaddr.as_inner(),
                ipv6mr_interface: to_ipv6mr_interface(interface),
            };
            setsockopt(&self.inner, ZTS_IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, mreq)
        }
    */
    pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
        setsockopt(
            &self.inner,
            ZTS_IPPROTO_IP as i32,
            ZTS_IP_TTL as i32,
            ttl as c_int,
        )
    }

    pub fn ttl(&self) -> io::Result<u32> {
        let raw: c_int = getsockopt(&self.inner, ZTS_IPPROTO_IP as i32, ZTS_IP_TTL as i32)?;
        Ok(raw as u32)
    }

    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
        self.inner.take_error()
    }

    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
        self.inner.set_nonblocking(nonblocking)
    }

    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.read(buf)
    }

    pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.peek(buf)
    }

    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
        /*
        let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
        let ret = cvt(unsafe {
            zts_bsd_send(*self.inner.as_inner(), buf.as_ptr() as *const c_void, len, ZTS_MSG_NOSIGNAL)
        })?;
        Ok(ret as usize)
        */
        unsafe {
            let raw = zts_bsd_write(
                *self.inner.as_inner(),
                buf.as_ptr() as *const c_void,
                buf.len().try_into().unwrap(),
            );
            if raw >= 0 {
                Ok(raw.try_into().unwrap())
            } else {
                Err(io::Error::from_raw_os_error(raw as i32))
            }
        }
    }

    pub fn connect(&self, addr: io::Result<&SocketAddr>) -> io::Result<()> {
        let addr = addr?;
        //let (addrp, len) = addr?.into_inner();
        unsafe {
            let full_str = addr.to_string();
            let full_vec = full_str.split(":");
            let lvec: Vec<&str> = full_vec.collect();
            let addr_str = lvec[0];
            let port = addr.port();
            let timeout_ms = 0;
            // TODO: Handle native error code, consider cvt?
            // This is a false-positive by the linter
            // See: https://github.com/rust-lang/rust/issues/78691
            #[allow(temporary_cstring_as_ptr)]
            cvt(zts_connect(
                *self.inner.as_inner(),
                CString::new(addr_str).unwrap().as_ptr(),
                port,
                timeout_ms,
            ))?;
        }

        Ok(())
    }
}

impl FromInner<Socket> for UdpSocketImpl {
    fn from_inner(socket: Socket) -> UdpSocketImpl {
        UdpSocketImpl { inner: socket }
    }
}

/*
impl fmt::Debug for UdpSocketImpl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut res = f.debug_struct("UdpSocketImpl");

        if let Ok(addr) = self.socket_addr() {
            res.field("addr", &addr);
        }

        let name = if cfg!(windows) { "socket" } else { "fd" };
        res.field(name, &self.inner.as_inner()).finish()
    }
}
*/

//----------------------------------------------------------------------------//
// UdpSocket                                                                  //
//----------------------------------------------------------------------------//

pub struct UdpSocket(UdpSocketImpl);

impl UdpSocket {
    pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> {
        each_addr(addr, UdpSocketImpl::bind).map(UdpSocket)
    }

    pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
        self.0.recv_from(buf)
    }

    pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
        self.0.peek_from(buf)
    }
    /*
        pub fn send_to<A: ToSocketAddrs>(&self, buf: &[u8], addr: A) -> io::Result<usize> {
            match addr.to_socket_addrs()?.next() {
                Some(addr) => self.0.send_to(buf, &addr),
                None => Err(Error::new(
                    ErrorKind::InvalidInput,
                    "No address to send data to",
                )),
            }
        }
    */
    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
        self.0.peer_addr()
    }

    pub fn local_addr(&self) -> io::Result<SocketAddr> {
        self.0.socket_addr()
    }

    pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
        self.0.set_read_timeout(dur)
    }

    pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
        self.0.set_write_timeout(dur)
    }

    pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
        self.0.read_timeout()
    }

    pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
        self.0.write_timeout()
    }

    pub fn set_broadcast(&self, broadcast: bool) -> io::Result<()> {
        self.0.set_broadcast(broadcast)
    }

    pub fn broadcast(&self) -> io::Result<bool> {
        self.0.broadcast()
    }
    /*
        pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
            self.0.set_multicast_loop_v4(multicast_loop_v4)
        }

        pub fn multicast_loop_v4(&self) -> io::Result<bool> {
            self.0.multicast_loop_v4()
        }

        pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
            self.0.set_multicast_ttl_v4(multicast_ttl_v4)
        }

        pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
            self.0.multicast_ttl_v4()
        }

        pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
            self.0.set_multicast_loop_v6(multicast_loop_v6)
        }

        pub fn multicast_loop_v6(&self) -> io::Result<bool> {
            self.0.multicast_loop_v6()
        }
    */
    pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
        self.0.set_ttl(ttl)
    }

    pub fn ttl(&self) -> io::Result<u32> {
        self.0.ttl()
    }
    /*
        pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
            self.0.join_multicast_v4(multiaddr, interface)
        }

        pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
            self.0.join_multicast_v6(multiaddr, interface)
        }

        pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
            self.0.leave_multicast_v4(multiaddr, interface)
        }

        pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
            self.0.leave_multicast_v6(multiaddr, interface)
        }
    */
    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
        self.0.take_error()
    }

    pub fn connect<A: ToSocketAddrs>(&self, addr: A) -> io::Result<()> {
        each_addr(addr, |addr| self.0.connect(addr))
    }

    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
        self.0.send(buf)
    }

    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
        self.0.recv(buf)
    }

    pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
        self.0.peek(buf)
    }

    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
        self.0.set_nonblocking(nonblocking)
    }
}

impl AsInner<UdpSocketImpl> for UdpSocket {
    fn as_inner(&self) -> &UdpSocketImpl {
        &self.0
    }
}

impl FromInner<UdpSocketImpl> for UdpSocket {
    fn from_inner(inner: UdpSocketImpl) -> UdpSocket {
        UdpSocket(inner)
    }
}

impl IntoInner<UdpSocketImpl> for UdpSocket {
    fn into_inner(self) -> UdpSocketImpl {
        self.0
    }
}

/*
impl fmt::Debug for UdpSocket {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}
*/