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
use alloc::boxed::Box;
use core::time::Duration;
use ax_errno::{AxError, AxResult, LinuxError};
use enum_dispatch::enum_dispatch;
/// Linux-like TCP connection state reported by TCP_INFO.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum TcpState {
/// No active TCP connection.
#[default]
Closed,
/// Passive listener.
Listen,
/// SYN sent, waiting for a matching SYN+ACK.
SynSent,
/// SYN received, waiting for the final ACK.
SynReceived,
/// Fully established connection.
Established,
/// Local endpoint has closed and sent FIN.
FinWait1,
/// Local FIN has been acknowledged.
FinWait2,
/// Remote endpoint has closed first.
CloseWait,
/// Both endpoints have closed simultaneously.
Closing,
/// Waiting for ACK of the local FIN after remote close.
LastAck,
/// Waiting for delayed packets to expire.
TimeWait,
}
bitflags::bitflags! {
/// Negotiated TCP options reported by TCP_INFO.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct TcpInfoOptions: u8 {
/// TCP timestamps are enabled.
const TIMESTAMPS = 1 << 0;
/// Selective ACK is enabled.
const SACK = 1 << 1;
/// Window scaling is enabled.
const WSCALE = 1 << 2;
/// Explicit congestion notification is enabled.
const ECN = 1 << 3;
/// ECN has been seen on the connection.
const ECN_SEEN = 1 << 4;
/// SYN data was used by the connection.
const SYN_DATA = 1 << 5;
}
}
/// Transport-independent TCP_INFO snapshot.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct TcpInfo {
/// Current TCP state.
pub state: TcpState,
/// Congestion-avoidance state. Zero means open.
pub ca_state: u8,
/// Number of unacknowledged retransmits.
pub retransmits: u8,
/// Number of pending keepalive or zero-window probes.
pub probes: u8,
/// Exponential backoff counter.
pub backoff: u8,
/// Negotiated TCP options.
pub options: TcpInfoOptions,
/// Send window scale.
pub snd_wscale: u8,
/// Receive window scale.
pub rcv_wscale: u8,
/// Retransmission timeout in microseconds.
pub rto_micros: u32,
/// Delayed ACK timeout in microseconds.
pub ato_micros: u32,
/// Send maximum segment size.
pub snd_mss: u32,
/// Receive maximum segment size.
pub rcv_mss: u32,
/// Bytes currently queued for transmit.
pub notsent_bytes: u32,
/// Advertised path MTU.
pub pmtu: u32,
/// Advertised maximum segment size.
pub advmss: u32,
/// Current send congestion window, in segments.
pub snd_cwnd: u32,
/// Packet reordering tolerance.
pub reordering: u32,
/// Available receive buffer space.
pub rcv_space: u32,
/// Current send window estimate.
pub snd_wnd: u32,
/// Current receive window estimate.
pub rcv_wnd: u32,
}
macro_rules! define_options {
($($name:ident($value:ty),)*) => {
/// Operation to get a socket option.
///
/// See [`Configurable::get_option`].
#[allow(missing_docs)]
pub enum GetSocketOption<'a> {
$(
$name(&'a mut $value),
)*
}
/// Operation to set a socket option.
///
/// See [`Configurable::set_option`].
#[allow(missing_docs)]
#[derive(Clone, Copy)]
pub enum SetSocketOption<'a> {
$(
$name(&'a $value),
)*
}
};
}
/// Corresponds to `struct ucred` in Linux.
#[repr(C)]
#[derive(Default, Debug, Clone)]
pub struct UnixCredentials {
/// Process ID.
pub pid: u32,
/// User ID.
pub uid: u32,
/// Group ID.
pub gid: u32,
}
impl UnixCredentials {
/// Create a new `UnixCredentials` with the given PID and default UID/GID.
pub fn new(pid: u32) -> Self {
UnixCredentials {
pid,
uid: 0,
gid: 0,
}
}
}
define_options! {
// ---- Socket level options (SO_*) ----
ReuseAddress(bool),
Error(i32),
DontRoute(bool),
SendBuffer(usize),
ReceiveBuffer(usize),
KeepAlive(bool),
SendTimeout(Duration),
ReceiveTimeout(Duration),
SendBufferForce(usize),
PassCredentials(bool),
PeerCredentials(UnixCredentials),
SocketType(i32),
SocketProtocol(i32),
SocketDomain(i32),
// --- TCP level options (TCP_*) ----
NoDelay(bool),
MaxSegment(usize),
TcpKeepIdle(u32),
TcpKeepInterval(u32),
TcpKeepCount(u32),
TcpUserTimeout(u32),
TcpInfo(TcpInfo),
// ---- IP level options (IP_*) ----
Ttl(u8),
RecvErr(bool),
// ---- Extra options ----
NonBlocking(bool),
}
/// Trait for configurable socket-like objects.
#[enum_dispatch]
pub trait Configurable {
/// Get a socket option, returns `true` if the socket supports the option.
fn get_option_inner(&self, opt: &mut GetSocketOption) -> AxResult<bool>;
/// Set a socket option, returns `true` if the socket supports the option.
fn set_option_inner(&self, opt: SetSocketOption) -> AxResult<bool>;
/// Get a socket option. Dispatches to [`Configurable::get_option_inner`].
fn get_option(&self, mut opt: GetSocketOption) -> AxResult {
self.get_option_inner(&mut opt).and_then(|supported| {
if !supported {
Err(AxError::from(LinuxError::ENOPROTOOPT))
} else {
Ok(())
}
})
}
/// Set a socket option. Dispatches to [`Configurable::set_option_inner`].
fn set_option(&self, opt: SetSocketOption) -> AxResult {
self.set_option_inner(opt).and_then(|supported| {
if !supported {
Err(AxError::from(LinuxError::ENOPROTOOPT))
} else {
Ok(())
}
})
}
}
impl<T: Configurable + ?Sized> Configurable for Box<T> {
fn get_option_inner(&self, opt: &mut GetSocketOption) -> AxResult<bool> {
self.as_ref().get_option_inner(opt)
}
fn set_option_inner(&self, opt: SetSocketOption) -> AxResult<bool> {
self.as_ref().set_option_inner(opt)
}
}