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
#![forbid(unsafe_code)]

use std::{io, net::SocketAddr};

use ntp_proto::NtpTimestamp;
use tokio::io::{unix::AsyncFd, Interest};
use tracing::instrument;

use crate::{
    interface::InterfaceName,
    raw_socket::{
        control_message_space, receive_message, set_timestamping_options, ControlMessage,
        MessageQueue, TimestampMethod,
    },
    EnableTimestamps,
};

#[cfg(target_os = "linux")]
use crate::raw_socket::err_queue_waiter::ErrQueueWaiter;

pub struct UdpSocket {
    io: AsyncFd<std::net::UdpSocket>,
    #[cfg(target_os = "linux")]
    err_queue_waiter: ErrQueueWaiter,
    send_counter: u32,
    timestamping: EnableTimestamps,
}

#[cfg(target_os = "linux")]
const DEFAULT_TIMESTAMP_METHOD: TimestampMethod = TimestampMethod::SoTimestamping;

#[cfg(all(unix, not(target_os = "linux")))]
const DEFAULT_TIMESTAMP_METHOD: TimestampMethod = TimestampMethod::SoTimestamp;

impl UdpSocket {
    #[instrument(level = "debug", skip(peer_addr))]
    pub async fn client(listen_addr: SocketAddr, peer_addr: SocketAddr) -> io::Result<UdpSocket> {
        Self::client_with_timestamping(
            listen_addr,
            peer_addr,
            InterfaceName::DEFAULT,
            EnableTimestamps::default(),
        )
        .await
    }

    pub async fn client_with_timestamping(
        listen_addr: SocketAddr,
        peer_addr: SocketAddr,
        interface: Option<InterfaceName>,
        timestamping: EnableTimestamps,
    ) -> io::Result<UdpSocket> {
        Self::client_with_timestamping_internal(
            listen_addr,
            peer_addr,
            interface,
            DEFAULT_TIMESTAMP_METHOD,
            timestamping,
        )
        .await
    }

    async fn client_with_timestamping_internal(
        listen_addr: SocketAddr,
        peer_addr: SocketAddr,
        interface: Option<InterfaceName>,
        method: TimestampMethod,
        timestamping: EnableTimestamps,
    ) -> io::Result<UdpSocket> {
        let socket = tokio::net::UdpSocket::bind(listen_addr).await?;
        tracing::debug!(
            local_addr = ?socket.local_addr().unwrap(),
            "client socket bound"
        );

        // bind the socket to a specific interface. This is relevant for hardware timestamping,
        // because the interface determines which clock is used to produce the timestamps.
        if let Some(_interface) = interface {
            #[cfg(target_os = "linux")]
            socket.bind_device(Some(&_interface)).unwrap();
        }

        socket.connect(peer_addr).await?;
        tracing::debug!(
            local_addr = ?socket.local_addr().unwrap(),
            peer_addr = ?socket.peer_addr().unwrap(),
            "client socket connected"
        );

        let socket = socket.into_std()?;

        set_timestamping_options(&socket, method, timestamping)?;

        Ok(UdpSocket {
            #[cfg(target_os = "linux")]
            err_queue_waiter: ErrQueueWaiter::new(&socket)?,
            io: AsyncFd::new(socket)?,
            send_counter: 0,
            timestamping,
        })
    }

    #[instrument(level = "debug")]
    pub async fn server(
        listen_addr: SocketAddr,
        interface: Option<InterfaceName>,
    ) -> io::Result<UdpSocket> {
        let socket = tokio::net::UdpSocket::bind(listen_addr).await?;
        tracing::debug!(
            local_addr = ?socket.local_addr().unwrap(),
            "server socket bound"
        );

        // bind the socket to a specific interface. This is relevant for hardware timestamping,
        // because the interface determines which clock is used to produce the timestamps.
        if let Some(_interface) = interface {
            #[cfg(target_os = "linux")]
            socket.bind_device(Some(&_interface)).unwrap();
        }

        let socket = socket.into_std()?;

        // our supported kernel versions always have receive timestamping. Send timestamping for a
        // server connection is not relevant, so we don't even bother with checking if it is supported
        let timestamping = EnableTimestamps {
            rx_software: true,
            tx_software: false,
            rx_hardware: false,
            tx_hardware: false,
        };

        set_timestamping_options(&socket, DEFAULT_TIMESTAMP_METHOD, timestamping)?;

        Ok(UdpSocket {
            #[cfg(target_os = "linux")]
            err_queue_waiter: ErrQueueWaiter::new(&socket)?,
            io: AsyncFd::new(socket)?,
            send_counter: 0,
            timestamping,
        })
    }

    #[instrument(level = "trace", skip(self, buf), fields(
        local_addr = debug(self.as_ref().local_addr().unwrap()),
        peer_addr = debug(self.as_ref().peer_addr()),
        buf_size = buf.len(),
    ))]
    pub async fn send(&mut self, buf: &[u8]) -> io::Result<(usize, Option<NtpTimestamp>)> {
        tracing::trace!(size = buf.len(), "sending bytes");

        let result = self
            .io
            .async_io(Interest::WRITABLE, |inner| inner.send(buf))
            .await;

        let send_size = match result {
            Ok(size) => {
                tracing::trace!(sent = size, "sent bytes");
                size
            }
            Err(e) => {
                tracing::debug!(error = debug(&e), "error sending data");
                return Err(e);
            }
        };

        debug_assert_eq!(buf.len(), send_size);

        let expected_counter = self.send_counter;
        self.send_counter = self.send_counter.wrapping_add(1);

        if self.timestamping.tx_software {
            #[cfg(target_os = "linux")]
            {
                // the send timestamp may never come set a very short timeout to prevent hanging forever.
                // We automatically fall back to a less accurate timestamp when this function returns None
                let timeout = std::time::Duration::from_millis(10);
                match tokio::time::timeout(timeout, self.fetch_send_timestamp(expected_counter))
                    .await
                {
                    Err(_) => {
                        tracing::warn!("Packet without timestamp");
                        Ok((send_size, None))
                    }
                    Ok(send_timestamp) => Ok((send_size, Some(send_timestamp?))),
                }
            }

            #[cfg(any(target_os = "macos", target_os = "freebsd"))]
            {
                let _ = expected_counter;
                Ok((send_size, None))
            }
        } else {
            tracing::trace!("send timestamping not supported");
            Ok((send_size, None))
        }
    }

    #[cfg(target_os = "linux")]
    async fn fetch_send_timestamp(&self, expected_counter: u32) -> io::Result<NtpTimestamp> {
        let msg = "waiting for timestamp socket to become readable to fetch a send timestamp";
        tracing::trace!(msg);

        // Send timestamps are sent to the udp socket's error queue. Sadly, tokio does not
        // currently support awaiting whether there is something in the error queue
        // see https://github.com/tokio-rs/tokio/issues/4885.
        //
        // Therefore, we manually configure an extra file descriptor to listen for POLLPRI on
        // the main udp socket. This `exceptional_condition` file descriptor becomes readable
        // when there is something in the error queue.
        loop {
            // Send timestamps are sent to the udp socket's error queue. Sadly, tokio does not
            // currently support awaiting whether there is something in the error queue
            // see https://github.com/tokio-rs/tokio/issues/4885.
            self.err_queue_waiter.wait().await?;

            match fetch_send_timestamp_help(self.io.get_ref(), expected_counter) {
                Ok(Some(send_timestamp)) => {
                    return Ok(send_timestamp);
                }
                Ok(None) => {
                    continue;
                }
                Err(e) => {
                    tracing::warn!(error = ?&e, "Error fetching timestamp");
                    return Err(e);
                }
            }
        }
    }

    #[instrument(level = "trace", skip(self, buf), fields(
        local_addr = debug(self.as_ref().local_addr().unwrap()),
        buf_size = buf.len(),
    ))]
    pub async fn send_to(&self, buf: &[u8], addr: SocketAddr) -> io::Result<usize> {
        tracing::trace!(size = buf.len(), ?addr, "sending bytes");

        let result = self
            .io
            .async_io(Interest::WRITABLE, |inner| inner.send_to(buf, addr))
            .await;

        match &result {
            Ok(size) => tracing::trace!(sent = size, "sent bytes"),
            Err(e) => tracing::debug!(error = debug(e), "error sending data"),
        }

        result
    }

    #[instrument(level = "trace", skip(self, buf), fields(
        local_addr = debug(self.as_ref().local_addr().unwrap()),
        peer_addr = debug(self.as_ref().peer_addr().ok()),
        buf_size = buf.len(),
    ))]
    pub async fn recv(
        &self,
        buf: &mut [u8],
    ) -> io::Result<(usize, SocketAddr, Option<NtpTimestamp>)> {
        tracing::trace!("waiting for socket to become readable");

        let result = self
            .io
            .async_io(Interest::READABLE, |inner| recv(inner, buf))
            .await;

        match &result {
            Ok((size, addr, ts)) => {
                tracing::trace!(size, ts = ?ts, addr = ?addr, "received message");
            }
            Err(e) => tracing::debug!(error = ?e, "error receiving data"),
        }

        result
    }
}

impl AsRef<std::net::UdpSocket> for UdpSocket {
    fn as_ref(&self) -> &std::net::UdpSocket {
        self.io.get_ref()
    }
}

fn recv(
    socket: &std::net::UdpSocket,
    buf: &mut [u8],
) -> io::Result<(usize, SocketAddr, Option<NtpTimestamp>)> {
    let mut control_buf = [0; control_message_space::<[libc::timespec; 3]>()];

    // loops for when we receive an interrupt during the recv
    let (bytes_read, control_messages, sock_addr) =
        receive_message(socket, buf, &mut control_buf, MessageQueue::Normal)?;
    let sock_addr =
        sock_addr.unwrap_or_else(|| unreachable!("We never constructed a non-ip socket"));

    // Loops through the control messages, but we should only get a single message in practice
    for msg in control_messages {
        match msg {
            ControlMessage::Timestamping(libc_timestamp) => {
                let ntp_timestamp = libc_timestamp.into_ntp_timestamp();
                return Ok((bytes_read as usize, sock_addr, Some(ntp_timestamp)));
            }

            #[cfg(target_os = "linux")]
            ControlMessage::ReceiveError(_error) => {
                tracing::warn!("unexpected error message on the MSG_ERRQUEUE");
            }

            ControlMessage::Other(msg) => {
                tracing::warn!(
                    "weird control message {:?} {:?}",
                    msg.cmsg_level,
                    msg.cmsg_type
                );
            }
        }
    }

    Ok((bytes_read as usize, sock_addr, None))
}

#[cfg(target_os = "linux")]
fn fetch_send_timestamp_help(
    socket: &std::net::UdpSocket,
    expected_counter: u32,
) -> io::Result<Option<NtpTimestamp>> {
    // we get back two control messages: one with the timestamp (just like a receive timestamp),
    // and one error message with no error reason. The payload for this second message is kind of
    // undocumented.
    //
    // section 2.1.1 of https://www.kernel.org/doc/Documentation/networking/timestamping.txt says that
    // a `sock_extended_err` is returned, but in practice we also see a socket address. The linux
    // kernel also has this https://github.com/torvalds/linux/blob/master/tools/testing/selftests/net/so_txtime.c#L153=
    //
    // sockaddr_storage is bigger than we need, but sockaddr is too small for ipv6
    const CONTROL_SIZE: usize = control_message_space::<[libc::timespec; 3]>()
        + control_message_space::<(libc::sock_extended_err, libc::sockaddr_storage)>();

    let mut control_buf = [0; CONTROL_SIZE];

    let (_, control_messages, _) =
        receive_message(socket, &mut [], &mut control_buf, MessageQueue::Error)?;

    let mut send_ts = None;
    for msg in control_messages {
        match msg {
            ControlMessage::Timestamping(timestamp) => {
                send_ts = Some(timestamp);
            }

            ControlMessage::ReceiveError(error) => {
                // the timestamping does not set a message; if there is a message, that means
                // something else is wrong, and we want to know about it.
                if error.ee_errno as libc::c_int != libc::ENOMSG {
                    tracing::warn!(
                        expected_counter,
                        error.ee_data,
                        "error message on the MSG_ERRQUEUE"
                    );
                }

                // Check that this message belongs to the send we are interested in
                if error.ee_data != expected_counter {
                    tracing::debug!(
                        error.ee_data,
                        expected_counter,
                        "Timestamp for unrelated packet"
                    );
                    return Ok(None);
                }
            }

            ControlMessage::Other(msg) => {
                tracing::warn!(
                    msg.cmsg_level,
                    msg.cmsg_type,
                    "unexpected message on the MSG_ERRQUEUE",
                );
            }
        }
    }

    Ok(send_ts.map(|ts| ts.into_ntp_timestamp()))
}

#[cfg(test)]
mod tests {
    use std::net::Ipv4Addr;

    use super::*;

    #[tokio::test]
    async fn test_client_basic_ipv4() {
        let mut a = UdpSocket::client(
            "127.0.0.1:10000".parse().unwrap(),
            "127.0.0.1:10001".parse().unwrap(),
        )
        .await
        .unwrap();
        let mut b = UdpSocket::client(
            "127.0.0.1:10001".parse().unwrap(),
            "127.0.0.1:10000".parse().unwrap(),
        )
        .await
        .unwrap();

        a.send(&[1; 48]).await.unwrap();
        let mut buf = [0; 48];
        let (size, addr, _) = b.recv(&mut buf).await.unwrap();
        assert_eq!(size, 48);
        assert_eq!(addr, "127.0.0.1:10000".parse().unwrap());
        assert_eq!(buf, [1; 48]);

        b.send(&[2; 48]).await.unwrap();
        let (size, addr, _) = a.recv(&mut buf).await.unwrap();
        assert_eq!(size, 48);
        assert_eq!(addr, "127.0.0.1:10001".parse().unwrap());
        assert_eq!(buf, [2; 48]);
    }

    #[tokio::test]
    async fn test_client_basic_ipv6() {
        let mut a = UdpSocket::client(
            "[::1]:10000".parse().unwrap(),
            "[::1]:10001".parse().unwrap(),
        )
        .await
        .unwrap();
        let mut b = UdpSocket::client(
            "[::1]:10001".parse().unwrap(),
            "[::1]:10000".parse().unwrap(),
        )
        .await
        .unwrap();

        a.send(&[1; 48]).await.unwrap();
        let mut buf = [0; 48];
        let (size, addr, _) = b.recv(&mut buf).await.unwrap();
        assert_eq!(size, 48);
        assert_eq!(addr, "[::1]:10000".parse().unwrap());
        assert_eq!(buf, [1; 48]);

        b.send(&[2; 48]).await.unwrap();
        let (size, addr, _) = a.recv(&mut buf).await.unwrap();
        assert_eq!(size, 48);
        assert_eq!(addr, "[::1]:10001".parse().unwrap());
        assert_eq!(buf, [2; 48]);
    }

    #[tokio::test]
    async fn test_server_basic_ipv4() {
        let a = UdpSocket::server("127.0.0.1:10002".parse().unwrap(), InterfaceName::DEFAULT)
            .await
            .unwrap();
        let mut b = UdpSocket::client(
            "127.0.0.1:10003".parse().unwrap(),
            "127.0.0.1:10002".parse().unwrap(),
        )
        .await
        .unwrap();

        b.send(&[1; 48]).await.unwrap();
        let mut buf = [0; 48];
        let (size, addr, _) = a.recv(&mut buf).await.unwrap();
        assert_eq!(size, 48);
        assert_eq!(addr, "127.0.0.1:10003".parse().unwrap());
        assert_eq!(buf, [1; 48]);

        a.send_to(&[2; 48], addr).await.unwrap();
        let (size, addr, _) = b.recv(&mut buf).await.unwrap();
        assert_eq!(size, 48);
        assert_eq!(addr, "127.0.0.1:10002".parse().unwrap());
        assert_eq!(buf, [2; 48]);
    }

    #[tokio::test]
    async fn test_server_basic_ipv6() {
        let a = UdpSocket::server("[::1]:10002".parse().unwrap(), InterfaceName::DEFAULT)
            .await
            .unwrap();
        let mut b = UdpSocket::client(
            "[::1]:10003".parse().unwrap(),
            "[::1]:10002".parse().unwrap(),
        )
        .await
        .unwrap();

        b.send(&[1; 48]).await.unwrap();
        let mut buf = [0; 48];
        let (size, addr, _) = a.recv(&mut buf).await.unwrap();
        assert_eq!(size, 48);
        assert_eq!(addr, "[::1]:10003".parse().unwrap());
        assert_eq!(buf, [1; 48]);

        a.send_to(&[2; 48], addr).await.unwrap();
        let (size, addr, _) = b.recv(&mut buf).await.unwrap();
        assert_eq!(size, 48);
        assert_eq!(addr, "[::1]:10002".parse().unwrap());
        assert_eq!(buf, [2; 48]);
    }

    async fn timestamping_reasonable(method: TimestampMethod, p1: u16, p2: u16) {
        let mut a = UdpSocket::client(
            SocketAddr::from((Ipv4Addr::LOCALHOST, p1)),
            SocketAddr::from((Ipv4Addr::LOCALHOST, p2)),
        )
        .await
        .unwrap();
        let b = UdpSocket::client_with_timestamping_internal(
            SocketAddr::from((Ipv4Addr::LOCALHOST, p2)),
            SocketAddr::from((Ipv4Addr::LOCALHOST, p1)),
            InterfaceName::DEFAULT,
            method,
            EnableTimestamps {
                rx_software: true,
                tx_software: true,
                rx_hardware: false,
                tx_hardware: false,
            },
        )
        .await
        .unwrap();

        tokio::spawn(async move {
            a.send(&[1; 48]).await.unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(200)).await;
            a.send(&[2; 48]).await.unwrap();
        });

        let mut buf = [0; 48];
        let (s1, _, t1) = b.recv(&mut buf).await.unwrap();
        let (s2, _, t2) = b.recv(&mut buf).await.unwrap();
        assert_eq!(s1, 48);
        assert_eq!(s2, 48);

        let t1 = t1.unwrap();
        let t2 = t2.unwrap();
        let delta = t2 - t1;

        // this can be flaky on freebsd
        assert!(
            delta.to_seconds() > 0.15 && delta.to_seconds() < 0.25,
            "delta was {}s",
            delta.to_seconds()
        );
    }

    #[tokio::test]
    #[cfg(target_os = "linux")]
    async fn timestamping_reasonable_so_timestamping() {
        timestamping_reasonable(TimestampMethod::SoTimestamping, 8000, 8001).await;
    }

    #[tokio::test]
    #[cfg(target_os = "linux")]
    async fn timestamping_reasonable_so_timestampns() {
        timestamping_reasonable(TimestampMethod::SoTimestampns, 8002, 8003).await;
    }

    #[tokio::test]
    #[cfg(unix)]
    async fn timestamping_reasonable_so_timestamp() {
        timestamping_reasonable(TimestampMethod::SoTimestamp, 8004, 8005).await;
    }

    #[tokio::test]
    #[cfg_attr(
        any(target_os = "macos", target_os = "freebsd"),
        ignore = "send timestamps are not supported"
    )]
    async fn test_send_timestamp() {
        let mut a = UdpSocket::client_with_timestamping(
            SocketAddr::from((Ipv4Addr::LOCALHOST, 8012)),
            SocketAddr::from((Ipv4Addr::LOCALHOST, 8013)),
            InterfaceName::DEFAULT,
            EnableTimestamps {
                rx_software: true,
                tx_software: true,
                rx_hardware: false,
                tx_hardware: false,
            },
        )
        .await
        .unwrap();
        let b = UdpSocket::client(
            SocketAddr::from((Ipv4Addr::LOCALHOST, 8013)),
            SocketAddr::from((Ipv4Addr::LOCALHOST, 8012)),
        )
        .await
        .unwrap();

        let (ssend, tsend) = a.send(&[1; 48]).await.unwrap();
        let mut buf = [0; 48];
        let (srecv, _, trecv) = b.recv(&mut buf).await.unwrap();

        assert_eq!(ssend, 48);
        assert_eq!(srecv, 48);

        let tsend = tsend.unwrap();
        let trecv = trecv.unwrap();
        let delta = trecv - tsend;
        assert!(delta.to_seconds().abs() < 0.2);
    }
}