dns_lookup/
types.rs

1#[cfg(unix)]
2use libc as c;
3
4#[cfg(windows)]
5use windows_sys::Win32::Networking::WinSock as c;
6
7// Parameters of addrinfo are c_int / i32 on libc and winsys on all architectures.
8#[allow(non_camel_case_types)]
9type c_int = i32;
10
11/// Socket Type
12///
13/// Cross platform enum of common Socket Types. For missing types use
14/// the `libc` and `windows-sys` crates, depending on platform.
15#[derive(Copy, Clone, Debug, PartialEq, Eq)]
16pub enum SockType {
17    /// Sequenced, reliable, connection-based byte streams.
18    Stream,
19    /// Connectionless, unreliable datagrams of fixed max length.
20    DGram,
21    /// Raw protocol interface.
22    #[cfg(not(target_os = "redox"))]
23    Raw,
24    /// Reliably-delivered messages.
25    #[cfg(not(target_os = "redox"))]
26    RDM,
27}
28
29impl From<SockType> for c_int {
30    fn from(sock: SockType) -> c_int {
31        #[allow(clippy::useless_conversion)]
32        (match sock {
33            SockType::Stream => c::SOCK_STREAM,
34            SockType::DGram => c::SOCK_DGRAM,
35            #[cfg(not(target_os = "redox"))]
36            SockType::Raw => c::SOCK_RAW,
37            #[cfg(not(target_os = "redox"))]
38            SockType::RDM => c::SOCK_RDM,
39        })
40        .into()
41    }
42}
43
44impl PartialEq<c_int> for SockType {
45    fn eq(&self, other: &c_int) -> bool {
46        let int: c_int = (*self).into();
47        *other == int
48    }
49}
50
51impl PartialEq<SockType> for c_int {
52    fn eq(&self, other: &SockType) -> bool {
53        let int: c_int = (*other).into();
54        *self == int
55    }
56}
57
58/// Socket Protocol
59///
60/// Cross platform enum of common Socket Protocols. For missing types use
61/// the `libc` and `windows-sys` crates, depending on platform.
62#[derive(Copy, Clone, Debug, PartialEq, Eq)]
63pub enum Protocol {
64    /// Internet Control Message Protocol.
65    ICMP,
66    /// Transmission Control Protocol.
67    TCP,
68    /// User Datagram Protocol.
69    UDP,
70}
71
72impl From<Protocol> for c_int {
73    fn from(sock: Protocol) -> c_int {
74        #[allow(clippy::useless_conversion)]
75        (match sock {
76            Protocol::ICMP => c::IPPROTO_ICMP,
77            Protocol::TCP => c::IPPROTO_TCP,
78            Protocol::UDP => c::IPPROTO_UDP,
79        })
80        .into()
81    }
82}
83
84impl PartialEq<c_int> for Protocol {
85    fn eq(&self, other: &c_int) -> bool {
86        let int: c_int = (*self).into();
87        *other == int
88    }
89}
90
91impl PartialEq<Protocol> for c_int {
92    fn eq(&self, other: &Protocol) -> bool {
93        let int: c_int = (*other).into();
94        *self == int
95    }
96}
97
98/// Address Family
99///
100/// Cross platform enum of common Address Families. For missing types use
101/// the `libc` and `windows-sys` crates, depending on platform.
102#[derive(Copy, Clone, Debug, PartialEq, Eq)]
103pub enum AddrFamily {
104    /// Local to host (pipes and file-domain)
105    Unix,
106    /// IP protocol family.
107    Inet,
108    /// IP version 6.
109    Inet6,
110}
111
112impl From<AddrFamily> for c_int {
113    fn from(sock: AddrFamily) -> c_int {
114        #[allow(clippy::useless_conversion)]
115        (match sock {
116            AddrFamily::Unix => c::AF_UNIX,
117            AddrFamily::Inet => c::AF_INET,
118            AddrFamily::Inet6 => c::AF_INET6,
119        })
120        .into()
121    }
122}
123
124impl PartialEq<c_int> for AddrFamily {
125    fn eq(&self, other: &c_int) -> bool {
126        let int: c_int = (*self).into();
127        *other == int
128    }
129}
130
131impl PartialEq<AddrFamily> for c_int {
132    fn eq(&self, other: &AddrFamily) -> bool {
133        let int: c_int = (*other).into();
134        *self == int
135    }
136}