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
use std::{io::Error as IoError, net::UdpSocket};

use crate::config::Config;

// Ref https://github.com/kolapapa/surge-ping/blob/0.7.3/src/client.rs#L36-L54
pub fn new_socket2_socket(config: &Config) -> Result<socket2::Socket, IoError> {
    use socket2::{Domain, Protocol, SockAddr, Socket, Type};

    let socket = if config.is_ipv6() {
        Socket::new(Domain::IPV6, Type::DGRAM, Some(Protocol::ICMPV6))?
    } else {
        Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::ICMPV4))?
    };

    socket.set_nonblocking(true)?;

    if let Some(bind) = config.bind {
        socket.bind(&SockAddr::from(bind))?;
    }
    #[cfg(any(
        target_os = "ios",
        target_os = "macos",
        target_os = "tvos",
        target_os = "watchos",
    ))]
    if let Some(interface_index) = config.interface_index {
        socket.bind_device_by_index(Some(interface_index))?;
    }
    if let Some(ttl) = config.ttl {
        socket.set_ttl(ttl)?;
    }
    #[cfg(target_os = "freebsd")]
    if let Some(fib) = config.fib {
        socket.set_fib(fib)?;
    }

    Ok(socket)
}

//
pub fn new_std_udp_socket(config: &Config) -> Result<UdpSocket, IoError> {
    #[cfg(unix)]
    use std::os::fd::{FromRawFd as _, IntoRawFd as _};
    #[cfg(windows)]
    use std::os::windows::{FromRawSocket as _, IntoRawSocket as _};

    let socket = new_socket2_socket(config)?;

    #[cfg(unix)]
    let udp_socket = unsafe { UdpSocket::from_raw_fd(socket.into_raw_fd()) };
    #[cfg(windows)]
    let udp_socket = unsafe { UdpSocket::from_raw_socket(socket.into_raw_socket()) };

    Ok(udp_socket)
}