timestamped-socket 0.3.0

Implementation of async UDP and raw ethernet sockets with timestamping
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
use std::{
    marker::PhantomData,
    net::{SocketAddr, SocketAddrV4, SocketAddrV6},
    os::fd::AsRawFd,
    sync::Arc,
};

use tokio::io::{unix::AsyncFd, Interest};

use crate::{
    control_message::{ControlMessage, MessageQueue, EXPECTED_MAX_CMSG_SIZE},
    interface::InterfaceName,
    networkaddress::{sealed::PrivateToken, MulticastJoinable, NetworkAddress},
    raw_socket::RawSocket,
};

#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "macos")))]
mod fallback;
#[cfg(target_os = "freebsd")]
mod freebsd;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;

#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "macos")))]
use self::fallback::*;
#[cfg(target_os = "freebsd")]
use self::freebsd::*;
#[cfg(target_os = "linux")]
pub use self::linux::*;
#[cfg(target_os = "macos")]
use self::macos::*;

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Default)]
pub struct TimestampData {
    /// The timestamping mode requested by the user when creating the socket.
    ///
    /// If this was not available, this is set to `None`.
    pub timestamp_mode: InterfaceTimestampMode,
    /// The hardware based timestamp returned by the OS
    pub hardware: Option<Timestamp>,
    /// The software based timestamp returned by the OS
    pub software: Option<Timestamp>,
}

impl TimestampData {
    /// Creates a new `TimestampData` with the given requested timestamping mode, but no timestamps.
    fn empty(timestamp_mode: InterfaceTimestampMode) -> Self {
        Self::default().with_timestamp_mode(timestamp_mode)
    }

    /// Returns the timestamp type selected by the requested timestamping mode.
    ///
    /// If the requested timestamping mode is `None` or if the requested timestamp
    /// is not available, this function will return `None`.
    pub fn selected_timestamp(self) -> Option<Timestamp> {
        use InterfaceTimestampMode::*;

        match self.timestamp_mode {
            SoftwareAll | SoftwareRecv => self.software,
            HardwareAll | HardwareRecv | HardwarePTPAll | HardwarePTPRecv => self.hardware,
            None => Option::None,
        }
    }

    /// Create a new `TimestampData` with the given requested timestamping mode.
    fn with_timestamp_mode(self, timestamp_mode: InterfaceTimestampMode) -> Self {
        Self {
            timestamp_mode,
            ..self
        }
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, Default)]
pub struct Timestamp {
    pub seconds: i64,
    pub nanos: u32,
}

impl Timestamp {
    #[cfg_attr(target_os = "macos", allow(unused))] // macos does not do nanoseconds
    pub(crate) fn from_timespec(timespec: libc::timespec) -> Self {
        Self {
            seconds: timespec.tv_sec as _,
            nanos: timespec.tv_nsec as _,
        }
    }

    pub(crate) fn from_timeval(timeval: libc::timeval) -> Self {
        Self {
            seconds: timeval.tv_sec as _,
            nanos: (1000 * timeval.tv_usec) as _,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum GeneralTimestampMode {
    SoftwareAll,
    SoftwareRecv,
    #[default]
    None,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum InterfaceTimestampMode {
    HardwareAll,
    HardwareRecv,
    HardwarePTPAll,
    HardwarePTPRecv,
    SoftwareAll,
    SoftwareRecv,
    #[default]
    None,
}

impl From<GeneralTimestampMode> for InterfaceTimestampMode {
    fn from(value: GeneralTimestampMode) -> Self {
        match value {
            GeneralTimestampMode::SoftwareAll => InterfaceTimestampMode::SoftwareAll,
            GeneralTimestampMode::SoftwareRecv => InterfaceTimestampMode::SoftwareRecv,
            GeneralTimestampMode::None => InterfaceTimestampMode::None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RecvResult<A> {
    pub bytes_read: usize,
    pub remote_addr: A,
    pub local_addr: A,
    pub timestamp_data: TimestampData,
}

#[derive(Debug)]
pub struct Socket<A, S> {
    timestamp_mode: InterfaceTimestampMode,
    // FIXME: Remove the arc once tokio also allows polling asyncfds for the Error interest
    socket: Arc<AsyncFd<RawSocket>>,
    #[cfg(target_os = "linux")]
    send_counter: std::sync::Mutex<u32>,
    local_addr: A,
    _state: PhantomData<S>,
}

#[non_exhaustive]
pub struct Open;
#[non_exhaustive]
pub struct Connected;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SendTimestampToken(u32);

impl<A: NetworkAddress, S> Socket<A, S> {
    pub fn local_addr(&self) -> A {
        self.local_addr
    }

    fn inner_recv(&self, buf: &mut [u8], socket: &RawSocket) -> std::io::Result<RecvResult<A>> {
        let mut control_buf = [0; EXPECTED_MAX_CMSG_SIZE];

        // loops for when we receive an interrupt during the recv
        let (bytes_read, control_messages, remote_address) =
            socket.receive_message(buf, &mut control_buf, MessageQueue::Normal)?;

        let mut timestamp_data = TimestampData::empty(self.timestamp_mode);
        let mut local_addr = self.local_addr;

        // Loops through the control messages, but we should only get a single message
        // in practice
        for msg in control_messages {
            match msg {
                ControlMessage::Timestamping { software, hardware } => {
                    tracing::trace!("Timestamps: {:?} {:?}", software, hardware);

                    // Keep the first timestamp of each kind
                    timestamp_data.software = timestamp_data.software.or(software);
                    timestamp_data.hardware = timestamp_data.hardware.or(hardware);
                }

                #[cfg(target_os = "linux")]
                ControlMessage::ReceiveError(error) => {
                    tracing::debug!(
                        "unexpected error control message on receive: {}",
                        error.ee_errno
                    );
                }

                ControlMessage::DestinationIp(addr) => {
                    if let Some(addr) = A::from_ip_and_port(addr, self.local_addr.port()) {
                        local_addr = addr;
                    }
                }

                ControlMessage::Other(msg) => {
                    tracing::debug!(
                        "unexpected control message on receive: {} {}",
                        msg.cmsg_level,
                        msg.cmsg_type,
                    );
                }
            }
        }

        let remote_addr =
            A::from_sockaddr(remote_address, PrivateToken).ok_or(std::io::ErrorKind::Other)?;

        Ok(RecvResult {
            bytes_read,
            remote_addr,
            local_addr,
            timestamp_data,
        })
    }

    /// Poll to receive a packet on the socket.
    ///
    /// Note that on multiple calls to [`poll_recv`](Socket::poll_recv), only
    /// the [`Waker`](std::task::Waker) from the [`Context`](std::task::Context)
    /// on the most recent call is scheduled to receive a wakeup.
    pub fn poll_recv(
        &self,
        buf: &mut [u8],
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<RecvResult<A>>> {
        match self.socket.poll_read_ready(cx) {
            std::task::Poll::Ready(Ok(mut guard)) => {
                match guard.try_io(|inner| self.inner_recv(buf, inner.get_ref())) {
                    Ok(result) => std::task::Poll::Ready(result),
                    Err(_) => std::task::Poll::Pending,
                }
            }
            std::task::Poll::Ready(Err(e)) => std::task::Poll::Ready(Err(e)),
            std::task::Poll::Pending => std::task::Poll::Pending,
        }
    }

    pub async fn recv(&self, buf: &mut [u8]) -> std::io::Result<RecvResult<A>> {
        self.socket
            .async_io(Interest::READABLE, |socket| self.inner_recv(buf, socket))
            .await
    }

    fn send_inner(
        &self,
        send_call: impl FnOnce() -> std::io::Result<()>,
    ) -> std::io::Result<Option<SendTimestampToken>> {
        if matches!(
            self.timestamp_mode,
            InterfaceTimestampMode::HardwarePTPAll | InterfaceTimestampMode::SoftwareAll
        ) {
            #[cfg(target_os = "linux")]
            {
                let mut counter = self.send_counter.lock().unwrap();
                send_call()?;
                let token = SendTimestampToken(*counter);
                *counter = counter.wrapping_add(1);
                Ok(Some(token))
            }

            #[cfg(not(target_os = "linux"))]
            {
                unreachable!("Should not be able to create send timestamping sockets on platforms other than linux")
            }
        } else {
            send_call()?;
            Ok(None)
        }
    }

    /// Wait for the next send timestamp to be returned.
    ///
    /// Note: There is no poll variant for this function, as that is currently
    /// impossible to implement with the tools tokio provides. To compensate,
    /// the future returned here is independent of the self reference and can
    /// be stored to simulate the existence of a poll function.
    pub fn get_send_timestamp(
        &self,
    ) -> impl std::future::Future<Output = std::io::Result<(SendTimestampToken, TimestampData)>>
           + Send
           + Sync
           + 'static {
        let timestamp_mode = self.timestamp_mode;
        let socket = self.socket.clone();
        async move {
            if matches!(
                timestamp_mode,
                InterfaceTimestampMode::HardwarePTPAll | InterfaceTimestampMode::SoftwareAll
            ) {
                #[cfg(target_os = "linux")]
                {
                    let (counter, timestamp) = Self::fetch_send_timestamp(socket).await?;
                    Ok((
                        SendTimestampToken(counter),
                        timestamp.with_timestamp_mode(timestamp_mode),
                    ))
                }

                #[cfg(not(target_os = "linux"))]
                {
                    let _ = socket;
                    unreachable!("Should not be able to create send timestamping sockets on platforms other than linux")
                }
            } else {
                Err(std::io::ErrorKind::Unsupported.into())
            }
        }
    }

    /// Retrieves the timestamp for a given SendTimestampToken.
    ///
    /// If multiple tokens are still pending for a send timestamp, this function may
    /// drop the timestamps for those tokens . Therefore, this should not be used in
    /// contexts where the socket may also be polled or there may otherwise be multiple
    /// tokens in use.
    async fn send_timestamp_for_transmit(
        &mut self,
        token: SendTimestampToken,
    ) -> std::io::Result<TimestampData> {
        use std::time::Duration;

        const TIMEOUT: Duration = Duration::from_millis(200);

        tokio::time::timeout(TIMEOUT, async {
            loop {
                let (cur_token, timestamp_data) = self.get_send_timestamp().await?;
                if cur_token == token {
                    return Ok(timestamp_data.with_timestamp_mode(self.timestamp_mode));
                }
            }
        })
        .await
        .unwrap_or(Ok(TimestampData::empty(self.timestamp_mode)))
    }
}

impl<A: NetworkAddress> Socket<A, Open> {
    /// Send a packet to a given receiver on the socket.
    ///
    /// Note that on multiple calls to `poll_send_*`, only
    /// the [`Waker`](std::task::Waker) from the [`Context`](std::task::Context)
    /// on the most recent call is scheduled to receive a wakeup.
    pub fn poll_send_to(
        &self,
        buf: &[u8],
        addr: A,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<Option<SendTimestampToken>>> {
        let addr = addr.to_sockaddr(PrivateToken);

        match self.socket.poll_write_ready(cx) {
            std::task::Poll::Ready(Ok(mut guard)) => {
                match guard.try_io(|inner| self.send_inner(|| inner.get_ref().send_to(buf, addr))) {
                    Ok(result) => std::task::Poll::Ready(result),
                    Err(_) => std::task::Poll::Pending,
                }
            }
            std::task::Poll::Ready(Err(e)) => std::task::Poll::Ready(Err(e)),
            std::task::Poll::Pending => std::task::Poll::Pending,
        }
    }

    /// Send a packet to a given receiver on the socket.
    ///
    /// When used in combination with the polling send functions, if there are timestamps
    /// pending for some `SendTimestampToken`, these may be skipped and become unavailable
    /// when calling this function.
    pub async fn send_to(&mut self, buf: &[u8], addr: A) -> std::io::Result<TimestampData> {
        let addr = addr.to_sockaddr(PrivateToken);

        if let Some(token) = self
            .socket
            .async_io(Interest::WRITABLE, |socket| {
                self.send_inner(|| socket.send_to(buf, addr))
            })
            .await?
        {
            self.send_timestamp_for_transmit(token).await
        } else {
            Ok(TimestampData::empty(self.timestamp_mode))
        }
    }

    /// Send a packet to a given receiver on the socket, using the specified origin address.
    ///
    /// Note that on multiple calls to `poll_send_*`, only
    /// the [`Waker`](std::task::Waker) from the [`Context`](std::task::Context)
    /// on the most recent call is scheduled to receive a wakeup.
    pub fn poll_send_from_to(
        &self,
        buf: &[u8],
        from: A,
        to: A,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<Option<SendTimestampToken>>> {
        let from = from.to_sockaddr(PrivateToken);
        let to = to.to_sockaddr(PrivateToken);

        match self.socket.poll_write_ready(cx) {
            std::task::Poll::Ready(Ok(mut guard)) => match guard
                .try_io(|inner| self.send_inner(|| inner.get_ref().send_from_to(buf, from, to)))
            {
                Ok(result) => std::task::Poll::Ready(result),
                Err(_) => std::task::Poll::Pending,
            },
            std::task::Poll::Ready(Err(e)) => std::task::Poll::Ready(Err(e)),
            std::task::Poll::Pending => std::task::Poll::Pending,
        }
    }

    /// Send a packet to a given receiver on the socket, using the specified origin address.
    ///
    /// When used in combination with the polling send functions, if there are timestamps
    /// pending for some `SendTimestampToken`, these may be skipped and become unavailable
    /// when calling this function.
    pub async fn send_from_to(
        &mut self,
        buf: &[u8],
        from: A,
        to: A,
    ) -> std::io::Result<TimestampData> {
        let from = from.to_sockaddr(PrivateToken);
        let to = to.to_sockaddr(PrivateToken);

        if let Some(token) = self
            .socket
            .async_io(Interest::WRITABLE, |socket| {
                self.send_inner(|| socket.send_from_to(buf, from, to))
            })
            .await?
        {
            self.send_timestamp_for_transmit(token).await
        } else {
            Ok(TimestampData::empty(self.timestamp_mode))
        }
    }

    pub fn connect(self, addr: A) -> std::io::Result<Socket<A, Connected>> {
        let addr = addr.to_sockaddr(PrivateToken);
        self.socket.get_ref().connect(addr)?;
        Ok(Socket {
            timestamp_mode: self.timestamp_mode,
            socket: self.socket,
            #[cfg(target_os = "linux")]
            send_counter: self.send_counter,
            local_addr: self.local_addr,
            _state: PhantomData,
        })
    }
}

impl<A: NetworkAddress> Socket<A, Connected> {
    pub fn peer_addr(&self) -> std::io::Result<A> {
        let addr = self.socket.get_ref().getpeername()?;
        A::from_sockaddr(addr, PrivateToken).ok_or_else(|| std::io::ErrorKind::Other.into())
    }

    /// Send a packet on the socket.
    ///
    /// Note that on multiple calls to `poll_send_*`, only
    /// the [`Waker`](std::task::Waker) from the [`Context`](std::task::Context)
    /// on the most recent call is scheduled to receive a wakeup.
    pub fn poll_send(
        &self,
        buf: &[u8],
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<Option<SendTimestampToken>>> {
        match self.socket.poll_write_ready(cx) {
            std::task::Poll::Ready(Ok(mut guard)) => {
                match guard.try_io(|inner| self.send_inner(|| inner.get_ref().send(buf))) {
                    Ok(result) => std::task::Poll::Ready(result),
                    Err(_) => std::task::Poll::Pending,
                }
            }
            std::task::Poll::Ready(Err(e)) => std::task::Poll::Ready(Err(e)),
            std::task::Poll::Pending => std::task::Poll::Pending,
        }
    }

    /// Send a packet on the socket.
    ///
    /// When used in combination with the polling send functions, if there are timestamps
    /// pending for some `SendTimestampToken`, these may be skipped and become unavailable
    /// when calling this function.
    pub async fn send(&mut self, buf: &[u8]) -> std::io::Result<TimestampData> {
        if let Some(token) = self
            .socket
            .async_io(Interest::WRITABLE, |socket| {
                self.send_inner(|| socket.send(buf))
            })
            .await?
        {
            self.send_timestamp_for_transmit(token).await
        } else {
            Ok(TimestampData::empty(self.timestamp_mode))
        }
    }

    /// Send a packet on the socket.
    ///
    /// Note that on multiple calls to `poll_send_*`, only
    /// the [`Waker`](std::task::Waker) from the [`Context`](std::task::Context)
    /// on the most recent call is scheduled to receive a wakeup.
    pub fn poll_send_from(
        &self,
        buf: &[u8],
        from: A,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<Option<SendTimestampToken>>> {
        let from = from.to_sockaddr(PrivateToken);

        match self.socket.poll_write_ready(cx) {
            std::task::Poll::Ready(Ok(mut guard)) => match guard
                .try_io(|inner| self.send_inner(|| inner.get_ref().send_from(buf, from)))
            {
                Ok(result) => std::task::Poll::Ready(result),
                Err(_) => std::task::Poll::Pending,
            },
            std::task::Poll::Ready(Err(e)) => std::task::Poll::Ready(Err(e)),
            std::task::Poll::Pending => std::task::Poll::Pending,
        }
    }

    /// Send a packet on the socket, with the given local address.
    ///
    /// When used in combination with the polling send functions, if there are timestamps
    /// pending for some `SendTimestampToken`, these may be skipped and become unavailable
    /// when calling this function.
    pub async fn send_from(&mut self, buf: &[u8], from: A) -> std::io::Result<TimestampData> {
        let from = from.to_sockaddr(PrivateToken);

        if let Some(token) = self
            .socket
            .async_io(Interest::WRITABLE, |socket| {
                self.send_inner(|| socket.send_from(buf, from))
            })
            .await?
        {
            self.send_timestamp_for_transmit(token).await
        } else {
            Ok(TimestampData::empty(self.timestamp_mode))
        }
    }
}

impl<A: MulticastJoinable, S> Socket<A, S> {
    pub fn join_multicast(&self, addr: A, interface: InterfaceName) -> std::io::Result<()> {
        addr.join_multicast(self.socket.get_ref().as_raw_fd(), interface, PrivateToken)
    }

    pub fn leave_multicast(&self, addr: A, interface: InterfaceName) -> std::io::Result<()> {
        addr.leave_multicast(self.socket.get_ref().as_raw_fd(), interface, PrivateToken)
    }
}

pub fn open_ip(
    addr: SocketAddr,
    timestamping: GeneralTimestampMode,
    #[cfg_attr(not(target_os = "linux"), expect(unused))] reuse_addr: bool,
) -> std::io::Result<Socket<SocketAddr, Open>> {
    // Setup the socket
    let socket = match addr {
        SocketAddr::V4(_) => RawSocket::open(libc::PF_INET, libc::SOCK_DGRAM, libc::IPPROTO_UDP),
        SocketAddr::V6(_) => RawSocket::open(libc::PF_INET6, libc::SOCK_DGRAM, libc::IPPROTO_UDP),
    }?;
    match addr {
        SocketAddr::V4(_) => socket.enable_destination_ipv4()?,
        SocketAddr::V6(_) => socket.enable_destination_ipv6()?,
    }
    #[cfg(target_os = "linux")]
    if reuse_addr {
        socket.reuse_addr()?;
    }
    socket.bind(addr.to_sockaddr(PrivateToken))?;
    socket.set_nonblocking(true)?;
    configure_timestamping(&socket, None, timestamping.into(), None)?;

    let local_addr = SocketAddr::from_sockaddr(socket.getsockname()?, PrivateToken)
        .ok_or::<std::io::Error>(std::io::ErrorKind::Other.into())?;

    Ok(Socket {
        timestamp_mode: timestamping.into(),
        socket: Arc::new(AsyncFd::new(socket)?),
        #[cfg(target_os = "linux")]
        send_counter: std::sync::Mutex::new(0),
        local_addr,
        _state: PhantomData,
    })
}

pub fn open_ipv4(
    addr: SocketAddrV4,
    timestamping: GeneralTimestampMode,
    #[cfg_attr(not(target_os = "linux"), expect(unused))] reuse_addr: bool,
) -> std::io::Result<Socket<SocketAddrV4, Open>> {
    // Setup the socket
    let socket = RawSocket::open(libc::PF_INET, libc::SOCK_DGRAM, libc::IPPROTO_UDP)?;
    socket.enable_destination_ipv4()?;
    #[cfg(target_os = "linux")]
    if reuse_addr {
        socket.reuse_addr()?;
    }
    socket.bind(addr.to_sockaddr(PrivateToken))?;
    socket.set_nonblocking(true)?;
    configure_timestamping(&socket, None, timestamping.into(), None)?;

    let local_addr = SocketAddrV4::from_sockaddr(socket.getsockname()?, PrivateToken)
        .ok_or::<std::io::Error>(std::io::ErrorKind::Other.into())?;

    Ok(Socket {
        timestamp_mode: timestamping.into(),
        socket: Arc::new(AsyncFd::new(socket)?),
        #[cfg(target_os = "linux")]
        send_counter: std::sync::Mutex::new(0),
        local_addr,
        _state: PhantomData,
    })
}

pub fn open_ipv6(
    addr: SocketAddrV6,
    timestamping: GeneralTimestampMode,
    #[cfg_attr(not(target_os = "linux"), expect(unused))] reuse_addr: bool,
) -> std::io::Result<Socket<SocketAddrV6, Open>> {
    // Setup the socket
    let socket = RawSocket::open(libc::PF_INET6, libc::SOCK_DGRAM, libc::IPPROTO_UDP)?;
    socket.ipv6_only()?;
    socket.enable_destination_ipv6()?;
    #[cfg(target_os = "linux")]
    if reuse_addr {
        socket.reuse_addr()?;
    }
    socket.bind(addr.to_sockaddr(PrivateToken))?;
    socket.set_nonblocking(true)?;
    configure_timestamping(&socket, None, timestamping.into(), None)?;

    let local_addr = SocketAddrV6::from_sockaddr(socket.getsockname()?, PrivateToken)
        .ok_or::<std::io::Error>(std::io::ErrorKind::Other.into())?;

    Ok(Socket {
        timestamp_mode: timestamping.into(),
        socket: Arc::new(AsyncFd::new(socket)?),
        #[cfg(target_os = "linux")]
        send_counter: std::sync::Mutex::new(0),
        local_addr,
        _state: PhantomData,
    })
}

pub fn connect_address(
    addr: SocketAddr,
    timestamping: GeneralTimestampMode,
) -> std::io::Result<Socket<SocketAddr, Connected>> {
    // Setup the socket
    let socket = match addr {
        SocketAddr::V4(_) => RawSocket::open(libc::PF_INET, libc::SOCK_DGRAM, libc::IPPROTO_UDP),
        SocketAddr::V6(_) => RawSocket::open(libc::PF_INET6, libc::SOCK_DGRAM, libc::IPPROTO_UDP),
    }?;
    match addr {
        SocketAddr::V4(_) => socket.enable_destination_ipv4()?,
        SocketAddr::V6(_) => socket.enable_destination_ipv6()?,
    }
    socket.connect(addr.to_sockaddr(PrivateToken))?;
    socket.set_nonblocking(true)?;
    configure_timestamping(&socket, None, timestamping.into(), None)?;

    let local_addr = SocketAddr::from_sockaddr(socket.getsockname()?, PrivateToken)
        .ok_or::<std::io::Error>(std::io::ErrorKind::Other.into())?;

    Ok(Socket {
        timestamp_mode: timestamping.into(),
        socket: Arc::new(AsyncFd::new(socket)?),
        #[cfg(target_os = "linux")]
        send_counter: std::sync::Mutex::new(0),
        local_addr,
        _state: PhantomData,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

    #[tokio::test]
    async fn test_open_ip() {
        let mut a = open_ip(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 5125),
            GeneralTimestampMode::None,
            false,
        )
        .unwrap();
        let mut b = connect_address(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 5125),
            GeneralTimestampMode::None,
        )
        .unwrap();
        assert!(b.send(&[1, 2, 3]).await.is_ok());
        let mut buf = [0; 4];
        let recv_result = a.recv(&mut buf).await.unwrap();
        assert_eq!(recv_result.bytes_read, 3);
        assert_eq!(&buf[0..3], &[1, 2, 3]);
        assert!(a.send_to(&[4, 5, 6], recv_result.remote_addr).await.is_ok());
        let recv_result = b.recv(&mut buf).await.unwrap();
        assert_eq!(recv_result.bytes_read, 3);
        assert_eq!(&buf[0..3], &[4, 5, 6]);
    }

    #[tokio::test]
    async fn test_open_ip_dest_addr() {
        let a = open_ip(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 5127),
            GeneralTimestampMode::None,
            false,
        )
        .unwrap();
        let mut b = connect_address(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 5127),
            GeneralTimestampMode::None,
        )
        .unwrap();
        assert!(b.send(&[1, 2, 3]).await.is_ok());
        let mut buf = [0; 4];
        let recv_result = a.recv(&mut buf).await.unwrap();
        assert_eq!(recv_result.bytes_read, 3);
        assert_eq!(&buf[0..3], &[1, 2, 3]);
        assert_eq!(
            recv_result.local_addr,
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 5127)
        );
        assert_ne!(a.local_addr().ip(), IpAddr::V4(Ipv4Addr::LOCALHOST));

        let a = open_ip(
            SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 5129),
            GeneralTimestampMode::None,
            false,
        )
        .unwrap();
        let mut b = connect_address(
            SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 5129),
            GeneralTimestampMode::None,
        )
        .unwrap();
        assert!(b.send(&[1, 2, 3]).await.is_ok());
        let mut buf = [0; 4];
        let recv_result = a.recv(&mut buf).await.unwrap();
        assert_eq!(recv_result.bytes_read, 3);
        assert_eq!(&buf[0..3], &[1, 2, 3]);
        assert_eq!(
            recv_result.local_addr,
            SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 5129)
        );
        assert_ne!(a.local_addr().ip(), IpAddr::V6(Ipv6Addr::LOCALHOST));
    }

    #[tokio::test]
    async fn test_send_from() {
        let mut a = open_ip(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 5130),
            GeneralTimestampMode::None,
            false,
        )
        .unwrap();
        let mut b = connect_address(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 5130),
            GeneralTimestampMode::None,
        )
        .unwrap();
        b.send_from(
            &[1, 2, 3],
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0),
        )
        .await
        .unwrap();
        let mut buf = [0; 4];
        let recv_result = a.recv(&mut buf).await.unwrap();
        assert_eq!(recv_result.bytes_read, 3);
        assert_eq!(&buf[0..3], &[1, 2, 3]);
        assert_eq!(
            recv_result.remote_addr.ip(),
            IpAddr::V4(Ipv4Addr::LOCALHOST)
        );

        a.send_from_to(
            &[1, 2, 3],
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0),
            dbg!(b.local_addr()),
        )
        .await
        .unwrap();
        let mut buf = [0; 4];
        let recv_result = b.recv(&mut buf).await.unwrap();
        assert_eq!(recv_result.bytes_read, 3);
        assert_eq!(&buf[0..3], &[1, 2, 3]);
        assert_eq!(
            recv_result.remote_addr.ip(),
            IpAddr::V4(Ipv4Addr::LOCALHOST)
        );
    }

    #[tokio::test]
    async fn test_send_from_v6() {
        let mut a = open_ip(
            SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 5131),
            GeneralTimestampMode::None,
            false,
        )
        .unwrap();
        let mut b = connect_address(
            SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 5131),
            GeneralTimestampMode::None,
        )
        .unwrap();
        b.send_from(
            &[1, 2, 3],
            SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 0),
        )
        .await
        .unwrap();
        let mut buf = [0; 4];
        let recv_result = a.recv(&mut buf).await.unwrap();
        assert_eq!(recv_result.bytes_read, 3);
        assert_eq!(&buf[0..3], &[1, 2, 3]);
        assert_eq!(
            recv_result.remote_addr.ip(),
            IpAddr::V6(Ipv6Addr::LOCALHOST)
        );

        a.send_from_to(
            &[1, 2, 3],
            SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 0),
            dbg!(b.local_addr()),
        )
        .await
        .unwrap();
        let mut buf = [0; 4];
        let recv_result = b.recv(&mut buf).await.unwrap();
        assert_eq!(recv_result.bytes_read, 3);
        assert_eq!(&buf[0..3], &[1, 2, 3]);
        assert_eq!(
            recv_result.remote_addr.ip(),
            IpAddr::V6(Ipv6Addr::LOCALHOST)
        );
    }
}