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
#[cfg(unix)]
use libc as c;
#[cfg(windows)]
use winapi as c;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum SockType {
Stream,
DGram,
Raw,
RDM,
}
impl From<SockType> for c::c_int {
fn from(sock: SockType) -> c::c_int {
match sock {
SockType::Stream => c::SOCK_STREAM,
SockType::DGram => c::SOCK_DGRAM,
SockType::Raw => c::SOCK_RAW,
SockType::RDM => c::SOCK_RDM,
}
}
}
impl PartialEq<c::c_int> for SockType {
fn eq(&self, other: &c::c_int) -> bool {
let int: c::c_int = (*self).into();
*other == int
}
}
impl PartialEq<SockType> for c::c_int {
fn eq(&self, other: &SockType) -> bool {
let int: c::c_int = (*other).into();
*self == int
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Protocol {
ICMP,
TCP,
UDP,
}
impl From<Protocol> for c::c_int {
#[cfg(unix)]
fn from(sock: Protocol) -> c::c_int {
match sock {
Protocol::ICMP => c::IPPROTO_ICMP,
Protocol::TCP => c::IPPROTO_TCP,
Protocol::UDP => c::IPPROTO_UDP,
}
}
#[cfg(windows)]
fn from(sock: Protocol) -> c::c_int {
match sock {
Protocol::ICMP => c::IPPROTO_ICMP.0 as c::c_int,
Protocol::TCP => c::IPPROTO_TCP.0 as c::c_int,
Protocol::UDP => c::IPPROTO_UDP.0 as c::c_int,
}
}
}
impl PartialEq<c::c_int> for Protocol {
fn eq(&self, other: &c::c_int) -> bool {
let int: c::c_int = (*self).into();
*other == int
}
}
impl PartialEq<Protocol> for c::c_int {
fn eq(&self, other: &Protocol) -> bool {
let int: c::c_int = (*other).into();
*self == int
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum AddrFamily {
Unix,
Inet,
Inet6
}
impl From<AddrFamily> for c::c_int {
fn from(sock: AddrFamily) -> c::c_int {
match sock {
AddrFamily::Unix => c::AF_UNIX,
AddrFamily::Inet => c::AF_INET,
AddrFamily::Inet6 => c::AF_INET6,
}
}
}
impl PartialEq<c::c_int> for AddrFamily {
fn eq(&self, other: &c::c_int) -> bool {
let int: c::c_int = (*self).into();
*other == int
}
}
impl PartialEq<AddrFamily> for c::c_int {
fn eq(&self, other: &AddrFamily) -> bool {
let int: c::c_int = (*other).into();
*self == int
}
}