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        (match sock {
32            SockType::Stream => c::SOCK_STREAM,
33            SockType::DGram => c::SOCK_DGRAM,
34            #[cfg(not(target_os = "redox"))]
35            SockType::Raw => c::SOCK_RAW,
36            #[cfg(not(target_os = "redox"))]
37            SockType::RDM => c::SOCK_RDM,
38        })
39        .into()
40    }
41}
42
43impl PartialEq<c_int> for SockType {
44    fn eq(&self, other: &c_int) -> bool {
45        let int: c_int = (*self).into();
46        *other == int
47    }
48}
49
50impl PartialEq<SockType> for c_int {
51    fn eq(&self, other: &SockType) -> bool {
52        let int: c_int = (*other).into();
53        *self == int
54    }
55}
56
57/// Socket Protocol
58///
59/// Cross platform enum of common Socket Protocols. For missing types use
60/// the `libc` and `windows-sys` crates, depending on platform.
61#[derive(Copy, Clone, Debug, PartialEq, Eq)]
62pub enum Protocol {
63    /// Internet Control Message Protocol.
64    ICMP,
65    /// Transmission Control Protocol.
66    TCP,
67    /// User Datagram Protocol.
68    UDP,
69}
70
71impl From<Protocol> for c_int {
72    fn from(sock: Protocol) -> c_int {
73        (match sock {
74            Protocol::ICMP => c::IPPROTO_ICMP,
75            Protocol::TCP => c::IPPROTO_TCP,
76            Protocol::UDP => c::IPPROTO_UDP,
77        })
78        .into()
79    }
80}
81
82impl PartialEq<c_int> for Protocol {
83    fn eq(&self, other: &c_int) -> bool {
84        let int: c_int = (*self).into();
85        *other == int
86    }
87}
88
89impl PartialEq<Protocol> for c_int {
90    fn eq(&self, other: &Protocol) -> bool {
91        let int: c_int = (*other).into();
92        *self == int
93    }
94}
95
96/// Address Family
97///
98/// Cross platform enum of common Address Families. For missing types use
99/// the `libc` and `windows-sys` crates, depending on platform.
100#[derive(Copy, Clone, Debug, PartialEq, Eq)]
101pub enum AddrFamily {
102    /// Local to host (pipes and file-domain)
103    Unix,
104    /// IP protocol family.
105    Inet,
106    /// IP version 6.
107    Inet6,
108}
109
110impl From<AddrFamily> for c_int {
111    fn from(sock: AddrFamily) -> c_int {
112        (match sock {
113            AddrFamily::Unix => c::AF_UNIX,
114            AddrFamily::Inet => c::AF_INET,
115            AddrFamily::Inet6 => c::AF_INET6,
116        })
117        .into()
118    }
119}
120
121impl PartialEq<c_int> for AddrFamily {
122    fn eq(&self, other: &c_int) -> bool {
123        let int: c_int = (*self).into();
124        *other == int
125    }
126}
127
128impl PartialEq<AddrFamily> for c_int {
129    fn eq(&self, other: &AddrFamily) -> bool {
130        let int: c_int = (*other).into();
131        *self == int
132    }
133}