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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::{
mem,
net::{IpAddr, Ipv6Addr, SocketAddr},
ptr,
};
use bytes::{Bytes, BytesMut};
#[derive(Copy, Clone)]
#[repr(align(8))] pub struct Aligned<T>(pub T);
pub struct Encoder<'a> {
hdr: &'a mut libc::msghdr,
cmsg: Option<&'a mut libc::cmsghdr>,
len: usize,
}
impl<'a> Encoder<'a> {
pub unsafe fn new(hdr: &'a mut libc::msghdr) -> Self {
Self {
cmsg: libc::CMSG_FIRSTHDR(hdr).as_mut(),
hdr,
len: 0,
}
}
pub fn push<T: Copy + ?Sized>(&mut self, level: libc::c_int, ty: libc::c_int, value: T) {
assert!(mem::align_of::<T>() <= mem::align_of::<libc::cmsghdr>());
let space = unsafe { libc::CMSG_SPACE(mem::size_of_val(&value) as _) as usize };
assert!(
self.hdr.msg_controllen as usize >= self.len + space,
"control message buffer too small. Required: {}, Available: {}",
self.len + space,
self.hdr.msg_controllen
);
let cmsg = self.cmsg.take().expect("no control buffer space remaining");
cmsg.cmsg_level = level;
cmsg.cmsg_type = ty;
cmsg.cmsg_len = unsafe { libc::CMSG_LEN(mem::size_of_val(&value) as _) } as _;
unsafe {
ptr::write(libc::CMSG_DATA(cmsg) as *const T as *mut T, value);
}
self.len += space;
self.cmsg = unsafe { libc::CMSG_NXTHDR(self.hdr, cmsg).as_mut() };
}
pub fn finish(self) {
}
}
impl<'a> Drop for Encoder<'a> {
fn drop(&mut self) {
self.hdr.msg_controllen = self.len as _;
}
}
pub unsafe fn decode<T: Copy>(cmsg: &libc::cmsghdr) -> T {
assert!(mem::align_of::<T>() <= mem::align_of::<libc::cmsghdr>());
debug_assert_eq!(
cmsg.cmsg_len as usize,
libc::CMSG_LEN(mem::size_of::<T>() as _) as usize
);
ptr::read(libc::CMSG_DATA(cmsg) as *const T)
}
pub struct Iter<'a> {
hdr: &'a libc::msghdr,
cmsg: Option<&'a libc::cmsghdr>,
}
impl<'a> Iter<'a> {
pub unsafe fn new(hdr: &'a libc::msghdr) -> Self {
Self {
hdr,
cmsg: libc::CMSG_FIRSTHDR(hdr).as_ref(),
}
}
}
impl<'a> Iterator for Iter<'a> {
type Item = &'a libc::cmsghdr;
fn next(&mut self) -> Option<&'a libc::cmsghdr> {
let current = self.cmsg.take()?;
self.cmsg = unsafe { libc::CMSG_NXTHDR(self.hdr, current).as_ref() };
Some(current)
}
}
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum EcnCodepoint {
#[doc(hidden)]
Ect0 = 0b10,
#[doc(hidden)]
Ect1 = 0b01,
#[doc(hidden)]
Ce = 0b11,
}
impl EcnCodepoint {
pub fn from_bits(x: u8) -> Option<Self> {
use self::EcnCodepoint::*;
Some(match x & 0b11 {
0b10 => Ect0,
0b01 => Ect1,
0b11 => Ce,
_ => {
return None;
}
})
}
}
#[derive(Debug)]
pub struct Transmit<B> {
pub dst: SocketAddr,
pub ecn: Option<EcnCodepoint>,
pub contents: B,
pub segment_size: Option<usize>,
pub src: Option<Source>,
}
impl<B: AsPtr<u8>> Transmit<B> {
pub fn new(dst: SocketAddr, contents: B) -> Self {
Self {
dst,
contents,
ecn: None,
segment_size: None,
src: None,
}
}
pub fn src_ip(self, src_ip: Source) -> Self {
Transmit {
src: Some(src_ip),
..self
}
}
pub fn segment_size(self, size: usize) -> Self {
Transmit {
segment_size: Some(size),
..self
}
}
pub fn ecn(self, ecn: EcnCodepoint) -> Self {
Transmit {
ecn: Some(ecn),
..self
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
pub enum Source {
Ip(IpAddr),
Interface(u32),
InterfaceV6(u32, Ipv6Addr),
}
pub trait AsPtr<T> {
fn as_ptr(&self) -> *const T;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<T> AsPtr<T> for Vec<T> {
fn as_ptr(&self) -> *const T {
self.as_ptr()
}
fn len(&self) -> usize {
self.len()
}
}
impl<T> AsPtr<T> for [T] {
fn as_ptr(&self) -> *const T {
self.as_ptr()
}
fn len(&self) -> usize {
self.len()
}
}
impl AsPtr<u8> for BytesMut {
fn as_ptr(&self) -> *const u8 {
<[u8]>::as_ptr(self.as_ref())
}
fn len(&self) -> usize {
self.len()
}
}
impl AsPtr<u8> for Bytes {
fn as_ptr(&self) -> *const u8 {
<[u8]>::as_ptr(self.as_ref())
}
fn len(&self) -> usize {
self.len()
}
}