mping 0.4.2

an easy to use ping library, supports single target and multiple targets, has high throughput.
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
use std::collections::BTreeMap;
use std::io::{Error, ErrorKind};
use std::net::{IpAddr, SocketAddr};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use cfg_if::cfg_if;

cfg_if! {
    if #[cfg(target_os = "linux")] {
        use libc::{
            c_int, c_void, cmsghdr, iovec, msghdr, recvmsg, setsockopt, timespec, timeval, MSG_DONTWAIT,
            MSG_ERRQUEUE, SOL_SOCKET, SO_TIMESTAMP, SO_TIMESTAMPING,
        };
        use libc::{
            SCM_TIMESTAMPING, SOF_TIMESTAMPING_OPT_CMSG, SOF_TIMESTAMPING_OPT_TSONLY,
            SOF_TIMESTAMPING_RAW_HARDWARE, SOF_TIMESTAMPING_RX_HARDWARE, SOF_TIMESTAMPING_RX_SOFTWARE,
            SOF_TIMESTAMPING_SOFTWARE, SOF_TIMESTAMPING_SYS_HARDWARE, SOF_TIMESTAMPING_TX_HARDWARE,
            SOF_TIMESTAMPING_TX_SOFTWARE,
        };
        use std::mem;
    } else {
        use libc::{ c_void, iovec, msghdr, recvmsg};
    }
}

use std::os::unix::io::AsRawFd;

use log::{error, info, warn};
use rand::Rng;
use rate_limit::SyncLimiter;
use ticker::Ticker;

use pnet_packet::icmp::{self, echo_reply, echo_request, IcmpTypes};
use pnet_packet::ipv4::Ipv4Packet;
use pnet_packet::Packet;
use socket2::{Domain, Protocol, Socket, Type};

use crate::stat::{Buckets, Result, TargetResult};

/// Ping option struct for ping function.
/// ``` rust
/// use std::time::Duration;
/// use mping::PingOption;
///
/// let popt = PingOption {
///    timeout: Duration::from_secs(1),
///    ttl: 64,
///    tos: None,
///    ident: 1234,
///    len: 56,
///    rate: 100,
///    rate_for_all: false,
///    delay: 3,
///    count: None,
/// };
/// ```
pub struct PingOption {
    /// Timeout for each ping write and read.
    pub timeout: Duration,
    /// TTL for each ping packet.
    pub ttl: u32,
    /// TOS for each ping packet.
    pub tos: Option<u32>,
    /// Id for each ICMP packet.
    pub ident: u16,
    /// Payload length for each ICMP packet.
    pub len: usize,
    /// Ping rate for each target.
    pub rate: u64,
    /// Rate for all targets or single target.
    /// if true, each target will send packets with rate for all targets.
    /// if false, each target will send packets with their own rate.
    pub rate_for_all: bool,
    /// Delay for output ping results.
    pub delay: u64,
    /// Max ping count for each target. Not check in case of None.
    pub count: Option<i64>,
}

/// Ping function.
///
/// # Arguments
///
/// - `addrs` is a vector of ip address.
/// - `popt` is a PingOption struct.
/// - `enable_print_stat` is a bool value to enable print ping stat in log.
/// - `tx`` is a Sender for send ping result. If tx is None, ping will not send result.
///   You can use mpsc::Receiver to receive ping results.
pub fn ping(
    addrs: Vec<IpAddr>,
    popt: PingOption,
    enable_print_stat: bool,
    tx: Option<Sender<TargetResult>>,
) -> anyhow::Result<()> {
    let pid = popt.ident;

    let rand_payload = random_bytes(popt.len);
    let read_rand_payload = rand_payload.clone();

    let buckets = Arc::new(Mutex::new(Buckets::new()));
    let send_buckets = buckets.clone();
    let read_buckets = buckets.clone();
    let stat_buckets = buckets.clone();

    let socket = Socket::new(Domain::IPV4, Type::RAW, Some(Protocol::ICMPV4)).unwrap();
    socket.set_ttl(popt.ttl).unwrap();
    socket.set_write_timeout(Some(popt.timeout)).unwrap();
    if let Some(tos_value) = popt.tos {
        socket.set_tos(tos_value).unwrap();
    }
    let socket2 = socket.try_clone().expect("Failed to clone socket");

    let has_tx = tx.is_some();

    // send
    thread::spawn(move || {
        cfg_if! {
            if #[cfg(target_os = "linux")] {
                let mut support_tx_timestamping = true;
                let raw_fd = socket.as_raw_fd();
                let enable = SOF_TIMESTAMPING_SOFTWARE
                    | SOF_TIMESTAMPING_TX_SOFTWARE
                    | SOF_TIMESTAMPING_SYS_HARDWARE
                    | SOF_TIMESTAMPING_TX_HARDWARE
                    | SOF_TIMESTAMPING_RAW_HARDWARE
                    | SOF_TIMESTAMPING_OPT_CMSG
                    | SOF_TIMESTAMPING_OPT_TSONLY;
                let ret = unsafe {
                    setsockopt(
                        raw_fd,
                        SOL_SOCKET,
                        SO_TIMESTAMPING,
                        &enable as *const _ as *const c_void,
                        mem::size_of_val(&enable) as u32,
                    )
                };

                if ret == -1 {
                    warn!("Failed to set SO_TIMESTAMPING");
                    support_tx_timestamping = false;
                }
            } else {
                let support_tx_timestamping = false;
            }
        }

        let zero_payload = vec![0; popt.len];
        let one_payload = vec![1; popt.len];
        let fivea_payload = vec![0x5A; popt.len];

        let payloads: [&[u8]; 4] = [&rand_payload, &zero_payload, &one_payload, &fivea_payload];

        let limiter = SyncLimiter::full(popt.rate, Duration::from_millis(1000));
        let mut seq = 1u16;
        let mut sent_count = 0;

        cfg_if! {
            if #[cfg(target_os = "linux")] {
                let mut buf = [0; 2048];
                let mut control_buf = [0; 1024];
                let mut iovec = iovec {
                    iov_base: buf.as_mut_ptr() as *mut c_void,
                    iov_len: buf.len(),
                };

                let mut msghdr = msghdr {
                    msg_name: std::ptr::null_mut(),
                    msg_namelen: 0,
                    msg_iov: &mut iovec,
                    msg_iovlen: 1,
                    msg_control: control_buf.as_mut_ptr() as *mut c_void,
                    msg_controllen: control_buf.len(),
                    msg_flags: 0,
                };
            }
        }

        loop {
            if !popt.rate_for_all {
                limiter.take();
            }

            let payload = payloads[seq as usize % payloads.len()];
            for ip in &addrs {
                if popt.rate_for_all {
                    limiter.take();
                }

                let mut buf = vec![0; 8 + payload.len()]; // 8 bytes of header, then payload
                let mut packet = echo_request::MutableEchoRequestPacket::new(&mut buf[..]).unwrap();
                packet.set_icmp_type(icmp::IcmpTypes::EchoRequest);
                packet.set_identifier(pid);
                packet.set_sequence_number(seq);

                let now = SystemTime::now();
                let since_the_epoch = now.duration_since(UNIX_EPOCH).unwrap();
                let timestamp = since_the_epoch.as_nanos();

                let ts_bytes = timestamp.to_be_bytes();
                let mut send_payload = vec![0; payload.len()];
                send_payload[..16].copy_from_slice(&ts_bytes[..16]);
                send_payload[16..].copy_from_slice(&payload[16..]);

                packet.set_payload(&send_payload);

                let icmp_packet = icmp::IcmpPacket::new(packet.packet()).unwrap();
                let checksum = icmp::checksum(&icmp_packet);
                packet.set_checksum(checksum);

                let dest = SocketAddr::new(*ip, 0);
                let key = timestamp / 1_000_000_000;
                let target = dest.ip().to_string();

                let data = send_buckets.lock().unwrap();
                data.add(
                    key,
                    Result {
                        txts: timestamp,
                        target: target.clone(),
                        seq,
                        latency: 0,
                        received: false,
                        bitflip: false,
                        ..Default::default()
                    },
                );
                drop(data);

                match socket.send_to(&buf, &dest.into()) {
                    Ok(_) => {}
                    Err(e) => {
                        error!("Error in send: {:?}", e);
                        return;
                    }
                }

                if support_tx_timestamping {
                    cfg_if! {
                        if #[cfg(target_os = "linux")] {
                            unsafe {
                                let _ = recvmsg(raw_fd, &mut msghdr, MSG_ERRQUEUE | MSG_DONTWAIT);
                            }
                            if let Some(txts) = get_timestamp(&mut msghdr) {
                                let ts = txts.duration_since(UNIX_EPOCH).unwrap().as_nanos();
                                let data = send_buckets.lock().unwrap();
                                data.update_txts(key, target, seq, ts);
                                drop(data);
                            }
                        }
                    }
                }
            }

            seq += 1;
            sent_count += 1;

            if popt.count.is_some() && sent_count >= popt.count.unwrap() {
                thread::sleep(Duration::from_secs(popt.delay));
                info!("reached {} and exit", sent_count);
                if has_tx {
                    return;
                }
                std::process::exit(0);
            }
        }
    });

    thread::spawn(move || print_stat(stat_buckets, popt.delay, enable_print_stat, tx.clone()));

    // read
    let zero_payload = vec![0; popt.len];
    let one_payload = vec![1; popt.len];
    let fivea_payload = vec![0x5A; popt.len];

    let payloads: [&[u8]; 4] = [
        &read_rand_payload,
        &zero_payload,
        &one_payload,
        &fivea_payload,
    ];

    socket2.set_read_timeout(Some(popt.timeout))?;
    let raw_fd = socket2.as_raw_fd();

    cfg_if! {
        if #[cfg(target_os = "linux")] {
            let enable = SOF_TIMESTAMPING_SOFTWARE
                | SOF_TIMESTAMPING_TX_SOFTWARE
                | SOF_TIMESTAMPING_RX_SOFTWARE
                | SOF_TIMESTAMPING_SYS_HARDWARE
                | SOF_TIMESTAMPING_TX_HARDWARE
                | SOF_TIMESTAMPING_RX_HARDWARE
                | SOF_TIMESTAMPING_RAW_HARDWARE
                | SOF_TIMESTAMPING_OPT_CMSG
                | SOF_TIMESTAMPING_OPT_TSONLY;
            let ret = unsafe {
                setsockopt(
                    raw_fd,
                    SOL_SOCKET,
                    SO_TIMESTAMPING,
                    &enable as *const _ as *const c_void,
                    mem::size_of_val(&enable) as u32,
                )
            };
            if ret == -1 {
                warn!("Failed to set read SO_TIMESTAMPING");
                let enable: c_int = 1;
                let ret = unsafe {
                    setsockopt(
                        raw_fd,
                        SOL_SOCKET,
                        SO_TIMESTAMP,
                        &enable as *const _ as *const c_void,
                        std::mem::size_of_val(&enable) as u32,
                    )
                };
                if ret == -1 {
                    warn!("Failed to set SO_TIMESTAMP");
                }
            }
        }
    }

    let mut buffer: [u8; 2048] = [0; 2048];
    let mut control_buf = [0; 1024];

    let mut iovec = iovec {
        iov_base: buffer.as_mut_ptr() as *mut c_void,
        iov_len: buffer.len(),
    };

    cfg_if! {
        if #[cfg(target_os = "linux")] {
            let mut msghdr = msghdr {
                msg_name: std::ptr::null_mut(),
                msg_namelen: 0,
                msg_iov: &mut iovec,
                msg_iovlen: 1,
                msg_control: control_buf.as_mut_ptr() as *mut c_void,
                msg_controllen: control_buf.len(),
                msg_flags: 0,
            };
        } else {
            let mut msghdr = msghdr {
                msg_name: std::ptr::null_mut(),
                msg_namelen: 0,
                msg_iov: &mut iovec,
                msg_iovlen: 1,
                msg_control: control_buf.as_mut_ptr() as *mut c_void,
                msg_controllen: control_buf.len() as u32,
                msg_flags: 0,
            };
        }
    }

    loop {
        let nbytes = unsafe { recvmsg(raw_fd, &mut msghdr, 0) };
        if nbytes == -1 {
            let err = Error::last_os_error();
            if err.kind() == ErrorKind::WouldBlock {
                continue;
            }

            error!("Failed torr receive message");
            return Err(Error::new(ErrorKind::Other, "Failed to receive message").into());
        }

        let buf = &buffer[..nbytes as usize];

        let ipv4_packet = Ipv4Packet::new(buf).unwrap();
        let icmp_packet = pnet_packet::icmp::IcmpPacket::new(ipv4_packet.payload()).unwrap();

        if icmp_packet.get_icmp_type() != IcmpTypes::EchoReply
            || icmp_packet.get_icmp_code() != echo_reply::IcmpCodes::NoCode
        {
            continue;
        }

        let echo_reply = match icmp::echo_reply::EchoReplyPacket::new(icmp_packet.packet()) {
            Some(echo_reply) => echo_reply,
            None => {
                continue;
            }
        };

        if echo_reply.get_identifier() != pid {
            continue;
        }

        let mut bitflip = false;
        if payloads[echo_reply.get_sequence_number() as usize % payloads.len()][16..]
            != echo_reply.payload()[16..]
        {
            warn!(
                "bitflip detected! seq={:?},",
                echo_reply.get_sequence_number()
            );
            bitflip = true;
        }

        let payload = echo_reply.payload();
        let ts_bytes = &payload[..16];
        let txts = u128::from_be_bytes(ts_bytes.try_into().unwrap());
        let dest_ip = ipv4_packet.get_source();

        let now = SystemTime::now();
        let since_the_epoch = now.duration_since(UNIX_EPOCH).unwrap();

        cfg_if! {
            if #[cfg(target_os = "linux")] {
                let mut timestamp = since_the_epoch.as_nanos();
                if let Some(rxts) = get_timestamp(&mut msghdr) {
                    timestamp = rxts.duration_since(UNIX_EPOCH).unwrap().as_nanos();
                }
            } else {
                let timestamp = since_the_epoch.as_nanos();
            }
        }

        let buckets = read_buckets.lock().unwrap();
        buckets.add_reply(
            txts / 1_000_000_000,
            Result {
                txts,
                rxts: timestamp,
                target: dest_ip.to_string(),
                seq: echo_reply.get_sequence_number(),
                latency: 0,
                received: true,
                bitflip,
            },
        );
    }
}

fn random_bytes(len: usize) -> Vec<u8> {
    let mut rng = rand::thread_rng();
    let mut vec = vec![0u8; len];
    rng.fill(&mut vec[..]);

    vec
}

fn print_stat(
    buckets: Arc<Mutex<Buckets>>,
    delay: u64,
    enable_print_stat: bool,
    tx: Option<Sender<TargetResult>>,
) -> anyhow::Result<()> {
    let delay = Duration::from_secs(delay).as_nanos(); // 5s
    let mut last_key = 0;

    let has_sender = tx.is_some();

    let ticker = Ticker::new(0.., Duration::from_secs(1));
    for _ in ticker {
        let buckets = buckets.lock().unwrap();
        let bucket = buckets.last();
        if bucket.is_none() {
            continue;
        }

        let bucket = bucket.unwrap();
        if bucket.key <= last_key {
            buckets.pop();
            continue;
        }

        if bucket.key
            <= SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_nanos()
                - delay
        {
            if let Some(pop) = buckets.pop() {
                if pop.key < bucket.key {
                    continue;
                }

                last_key = pop.key;

                // cacl stat
                let mut target_results = BTreeMap::new();

                for r in pop.values() {
                    let target_result = target_results
                        .entry(r.target.clone())
                        .or_insert_with(TargetResult::default);

                    target_result.latency += r.latency;

                    if r.received {
                        target_result.received += 1;
                    } else {
                        target_result.loss += 1;
                    }

                    if r.bitflip {
                        target_result.bitflip_count += 1;
                    }
                }

                // output
                for (target, tr) in &target_results {
                    let total = tr.received + tr.loss;
                    let loss_rate = if total == 0 {
                        0.0
                    } else {
                        (tr.loss as f64) / (total as f64)
                    };

                    if enable_print_stat {
                        if tr.received == 0 {
                            info!(
                                "{}: sent:{}, recv:{}, loss rate: {:.2}%, latency: {}ms",
                                target,
                                total,
                                tr.received,
                                loss_rate * 100.0,
                                0
                            )
                        } else {
                            info!(
                                "{}: sent:{}, recv:{},  loss rate: {:.2}%, latency: {:.2}ms",
                                target,
                                total,
                                tr.received,
                                loss_rate * 100.0,
                                Duration::from_nanos(tr.latency as u64 / (tr.received as u64))
                                    .as_secs_f64()
                                    * 1000.0
                            )
                        }
                    }

                    if has_sender {
                        let mut tr = tr.clone();
                        tr.target = target.clone();
                        tr.loss_rate = loss_rate;
                        let _ = tx.as_ref().unwrap().send(tr);
                    }
                }
            }
        }
    }

    Ok(())
}

#[cfg(target_os = "linux")]
fn get_timestamp(msghdr: &mut msghdr) -> Option<SystemTime> {
    let mut cmsg: *mut cmsghdr = unsafe { libc::CMSG_FIRSTHDR(msghdr) };

    while !cmsg.is_null() {
        if unsafe { (*cmsg).cmsg_level == SOL_SOCKET && (*cmsg).cmsg_type == SO_TIMESTAMP } {
            let tv: *mut timeval = unsafe { libc::CMSG_DATA(cmsg) } as *mut timeval;
            let timestamp = unsafe { *tv };
            return Some(
                SystemTime::UNIX_EPOCH
                    + Duration::new(timestamp.tv_sec as u64, timestamp.tv_usec as u32 * 1000),
            );
        }
        if unsafe { (*cmsg).cmsg_level == SOL_SOCKET && (*cmsg).cmsg_type == SCM_TIMESTAMPING } {
            let tv: *mut [timespec; 3] = unsafe { libc::CMSG_DATA(cmsg) } as *mut [timespec; 3];
            let timestamps = unsafe { *tv };

            for timestamp in &timestamps {
                if timestamp.tv_sec != 0 || timestamp.tv_nsec != 0 {
                    let seconds = Duration::from_secs(timestamp.tv_sec as u64);
                    let nanoseconds = Duration::from_nanos(timestamp.tv_nsec as u64);
                    if let Some(duration) = seconds.checked_add(nanoseconds) {
                        return Some(SystemTime::UNIX_EPOCH + duration);
                    }
                }
            }
        }

        cmsg = unsafe { libc::CMSG_NXTHDR(msghdr, cmsg) };
    }

    None
}

/// ping the target once and return bitflip or not and latency.
pub fn ping_once(
    addr: String,
    timeout: Option<Duration>,
    seq: Option<u16>,
    ttl: Option<u32>,
    tos: Option<u32>,
    len: Option<usize>,
) -> anyhow::Result<(bool, Duration)> {
    let mut rng = rand::thread_rng();

    // prepare pamaters
    let ip = addr.parse::<IpAddr>()?;
    let target = SocketAddr::new(ip, 0);
    let pid = std::process::id() as u16;
    let timeout = timeout.unwrap_or(Duration::from_secs(1));
    let seq = seq.unwrap_or(rng.gen());
    let ttl = ttl.unwrap_or(64);
    let len = len.unwrap_or(64);

    // prepare payloads
    let rand_payload = random_bytes(len);
    let zero_payload = vec![0; len];
    let one_payload = vec![1; len];
    let fivea_payload = vec![0x5A; len];
    let payloads: [&[u8]; 4] = [&rand_payload, &zero_payload, &one_payload, &fivea_payload];

    // set socket
    let socket = Socket::new(Domain::IPV4, Type::RAW, Some(Protocol::ICMPV4))?;
    socket.set_ttl(ttl)?;
    socket.set_write_timeout(Some(timeout))?;
    socket.set_read_timeout(Some(timeout))?;
    if let Some(tos_value) = tos {
        socket.set_tos(tos_value).unwrap();
    }
    let raw_fd = socket.as_raw_fd();

    // timestamp
    cfg_if! {
        if #[cfg(target_os = "linux")] {
            let mut support_tx_timestamping = true;
            let enable = SOF_TIMESTAMPING_SOFTWARE
                | SOF_TIMESTAMPING_TX_SOFTWARE
                | SOF_TIMESTAMPING_SYS_HARDWARE
                | SOF_TIMESTAMPING_TX_HARDWARE
                | SOF_TIMESTAMPING_RAW_HARDWARE
                | SOF_TIMESTAMPING_OPT_CMSG
                | SOF_TIMESTAMPING_OPT_TSONLY;
            let ret = unsafe {
                setsockopt(
                    raw_fd,
                    SOL_SOCKET,
                    SO_TIMESTAMPING,
                    &enable as *const _ as *const c_void,
                    mem::size_of_val(&enable) as u32,
                )
            };
            if ret == -1 {
                warn!("Failed to set SO_TIMESTAMPING");
                support_tx_timestamping = false;
            }
        }
    }

    cfg_if! {
        if #[cfg(target_os = "linux")] {
            // msghdr
            let mut buf = [0; 2048];
            let mut control_buf = [0; 1024];
            let mut iovec = iovec {
                iov_base: buf.as_mut_ptr() as *mut c_void,
                iov_len: buf.len(),
            };

            let mut msghdr = msghdr {
                msg_name: std::ptr::null_mut(),
                msg_namelen: 0,
                msg_iov: &mut iovec,
                msg_iovlen: 1,
                msg_control: control_buf.as_mut_ptr() as *mut c_void,
                msg_controllen: control_buf.len(),
                msg_flags: 0,
            };
        }
    }

    // prepare packet
    let payload = payloads[seq as usize % payloads.len()];
    let mut buf = vec![0; 8 + payload.len()]; // 8 bytes of header, then payload
    let mut packet = echo_request::MutableEchoRequestPacket::new(&mut buf[..]).unwrap();
    packet.set_icmp_type(icmp::IcmpTypes::EchoRequest);
    packet.set_identifier(pid);
    packet.set_sequence_number(seq);
    packet.set_payload(payload);

    let icmp_packet = icmp::IcmpPacket::new(packet.packet()).unwrap();
    let checksum = icmp::checksum(&icmp_packet);
    packet.set_checksum(checksum);

    // send the packet
    let now = SystemTime::now();
    let since_the_epoch = now.duration_since(UNIX_EPOCH)?;
    socket.send_to(&buf, &target.into())?;

    // read tx timestamp

    cfg_if! {
        if #[cfg(target_os = "linux")] {
            if support_tx_timestamping {
            let mut txts = since_the_epoch.as_nanos();

            unsafe {
                let _ = recvmsg(raw_fd, &mut msghdr, MSG_ERRQUEUE | MSG_DONTWAIT);
            }
            if let Some(ts) = get_timestamp(&mut msghdr) {
                txts = ts.duration_since(UNIX_EPOCH).unwrap().as_nanos();
            }
        }
        } else {
            let txts = since_the_epoch.as_nanos();
        }
    }

    // read
    let mut buffer: [u8; 2048] = [0; 2048];
    let mut control_buf = [0; 1024];

    let mut iovec = iovec {
        iov_base: buffer.as_mut_ptr() as *mut c_void,
        iov_len: buffer.len(),
    };

    cfg_if! {
        if #[cfg(target_os = "linux")] {
            let mut msghdr = msghdr {
                msg_name: std::ptr::null_mut(),
                msg_namelen: 0,
                msg_iov: &mut iovec,
                msg_iovlen: 1,
                msg_control: control_buf.as_mut_ptr() as *mut c_void,
                msg_controllen: control_buf.len(),
                msg_flags: 0,
            };
        } else {
            let mut msghdr = msghdr {
                msg_name: std::ptr::null_mut(),
                msg_namelen: 0,
                msg_iov: &mut iovec,
                msg_iovlen: 1,
                msg_control: control_buf.as_mut_ptr() as *mut c_void,
                msg_controllen: control_buf.len() as u32,
                msg_flags: 0,
            };
        }
    }

    let mut n = timeout.as_secs() + 1;
    if n < 2 {
        n = 2;
    }
    for _ in 1..n {
        let nbytes = unsafe { recvmsg(raw_fd, &mut msghdr, 0) };
        if nbytes == -1 {
            let err = Error::last_os_error();
            if err.kind() == ErrorKind::WouldBlock {
                continue;
            }

            error!("Failed to receive message");
            return Err(Error::new(ErrorKind::TimedOut, "timeout").into());
        }

        let buf = &buffer[..nbytes as usize];

        let ipv4_packet = Ipv4Packet::new(buf).unwrap();
        let icmp_packet = pnet_packet::icmp::IcmpPacket::new(ipv4_packet.payload()).unwrap();

        if icmp_packet.get_icmp_type() != IcmpTypes::EchoReply
            || icmp_packet.get_icmp_code() != echo_reply::IcmpCodes::NoCode
        {
            continue;
        }

        let echo_reply = match icmp::echo_reply::EchoReplyPacket::new(icmp_packet.packet()) {
            Some(echo_reply) => echo_reply,
            None => {
                continue;
            }
        };

        if echo_reply.get_identifier() != pid {
            continue;
        }

        let mut bitflip = false;
        if payloads[echo_reply.get_sequence_number() as usize % payloads.len()]
            != echo_reply.payload()
        {
            warn!(
                "bitflip detected! seq={:?},",
                echo_reply.get_sequence_number()
            );
            bitflip = true;
        }
        let dest_ip = ipv4_packet.get_source();
        if dest_ip != ip {
            continue;
        }

        let since_the_epoch = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();

        cfg_if! {
            if #[cfg(target_os = "linux")] {
                let mut rxts = since_the_epoch.as_nanos();

                if let Some(ts) = get_timestamp(&mut msghdr) {
                    rxts = ts.duration_since(UNIX_EPOCH).unwrap().as_nanos();
                }
            } else {
                let rxts = since_the_epoch.as_nanos();
            }
        }

        return Ok((bitflip, Duration::from_nanos((rxts - txts) as u64)));
    }

    Err(Error::new(ErrorKind::TimedOut, "timeout").into())
}