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
#[cfg(unix)]
use libc as c;

#[cfg(windows)]
use windows_sys::Win32::Networking::WinSock as c;

// Parameters of addrinfo are c_int / i32 on libc and winsys on all architectures.
#[allow(non_camel_case_types)]
type c_int = i32;

/// Socket Type
///
/// Cross platform enum of common Socket Types. For missing types use
/// the `libc` and `windows-sys` crates, depending on platform.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum SockType {
    /// Sequenced, reliable, connection-based byte streams.
    Stream,
    /// Connectionless, unreliable datagrams of fixed max length.
    DGram,
    /// Raw protocol interface.
    #[cfg(not(target_os = "redox"))]
    Raw,
    /// Reliably-delivered messages.
    #[cfg(not(target_os = "redox"))]
    RDM,
}

impl From<SockType> for c_int {
    fn from(sock: SockType) -> c_int {
        (match sock {
            SockType::Stream => c::SOCK_STREAM,
            SockType::DGram => c::SOCK_DGRAM,
            #[cfg(not(target_os = "redox"))]
            SockType::Raw => c::SOCK_RAW,
            #[cfg(not(target_os = "redox"))]
            SockType::RDM => c::SOCK_RDM,
        })
        .into()
    }
}

impl PartialEq<c_int> for SockType {
    fn eq(&self, other: &c_int) -> bool {
        let int: c_int = (*self).into();
        *other == int
    }
}

impl PartialEq<SockType> for c_int {
    fn eq(&self, other: &SockType) -> bool {
        let int: c_int = (*other).into();
        *self == int
    }
}

/// Socket Protocol
///
/// Cross platform enum of common Socket Protocols. For missing types use
/// the `libc` and `windows-sys` crates, depending on platform.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Protocol {
    /// Internet Control Message Protocol.
    ICMP,
    /// Transmission Control Protocol.
    TCP,
    /// User Datagram Protocol.
    UDP,
}

impl From<Protocol> for c_int {
    fn from(sock: Protocol) -> c_int {
        (match sock {
            Protocol::ICMP => c::IPPROTO_ICMP,
            Protocol::TCP => c::IPPROTO_TCP,
            Protocol::UDP => c::IPPROTO_UDP,
        })
        .into()
    }
}

impl PartialEq<c_int> for Protocol {
    fn eq(&self, other: &c_int) -> bool {
        let int: c_int = (*self).into();
        *other == int
    }
}

impl PartialEq<Protocol> for c_int {
    fn eq(&self, other: &Protocol) -> bool {
        let int: c_int = (*other).into();
        *self == int
    }
}

/// Address Family
///
/// Cross platform enum of common Address Families. For missing types use
/// the `libc` and `windows-sys` crates, depending on platform.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum AddrFamily {
    /// Local to host (pipes and file-domain)
    Unix,
    /// IP protocol family.
    Inet,
    /// IP version 6.
    Inet6,
}

impl From<AddrFamily> for c_int {
    fn from(sock: AddrFamily) -> c_int {
        (match sock {
            AddrFamily::Unix => c::AF_UNIX,
            AddrFamily::Inet => c::AF_INET,
            AddrFamily::Inet6 => c::AF_INET6,
        })
        .into()
    }
}

impl PartialEq<c_int> for AddrFamily {
    fn eq(&self, other: &c_int) -> bool {
        let int: c_int = (*self).into();
        *other == int
    }
}

impl PartialEq<AddrFamily> for c_int {
    fn eq(&self, other: &AddrFamily) -> bool {
        let int: c_int = (*other).into();
        *self == int
    }
}