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
//! MPROXY: Socket Dispatcher. Platform-agnostic API to bind unicast and multicast UDP socket addresses.
//! Provides socket interface for crates `mproxy-client`, `mproxy-server`, `mproxy-proxy`, and `mproxy-reverseproxy`.

use std::io;
use std::net::SocketAddr;

pub use socket2::{Domain, Protocol, Socket, Type};

pub const BUFSIZE: usize = 8192;

#[cfg(unix)]
pub fn bind_socket(socket: &Socket, addr: &SocketAddr) -> io::Result<()> {
    socket.bind(&socket2::SockAddr::from(*addr))
}

/// https://msdn.microsoft.com/en-us/library/windows/desktop/ms737550(v=vs.85).aspx
#[cfg(windows)]
use std::net::{Ipv4Addr, Ipv6Addr};
#[cfg(windows)]
pub fn bind_socket(socket: &Socket, addr: &SocketAddr) -> io::Result<()> {
    let addr = match addr.ip().is_multicast() {
        true => match addr {
            SocketAddr::V4(addr) => SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), addr.port()),
            SocketAddr::V6(addr) => {
                SocketAddr::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).into(), addr.port())
            }
        },
        false => *addr,
    };
    socket.bind(&socket2::SockAddr::from(addr))
}

pub fn new_socket(addr: &SocketAddr) -> io::Result<Socket> {
    let domain = if addr.is_ipv4() {
        Domain::IPV4
    } else if addr.is_ipv6() {
        Domain::IPV6
    } else {
        #[cfg(windows)]
        panic!();
        #[cfg(unix)]
        Domain::UNIX
    };

    let socket = Socket::new(domain, Type::DGRAM, Some(Protocol::UDP))?;

    #[cfg(unix)]
    if let Err(e) = socket.set_reuse_port(true) {
        eprintln!(
            "Could not set reusable port! This feature requires unix. {}",
            e
        );
    }
    #[cfg(target_os = "linux")]
    if let Err(e) = socket.set_freebind(true) {
        eprintln!(
            "Could not set freebind socket! This feature requires linux. {}",
            e
        );
    }
    socket.set_read_timeout(None)?;
    Ok(socket)
}