nlink 0.15.1

Async netlink library for Linux network configuration
Documentation
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! Socket query filters.
//!
//! This module provides builder-pattern APIs for constructing socket queries
//! with various filter criteria.

use std::net::IpAddr;

use super::types::{AddressFamily, InetExtension, Protocol, TcpState, UnixShow};

/// Socket filter builder.
///
/// Use this to construct queries for different socket types.
#[derive(Debug, Clone)]
pub struct SocketFilter {
    /// The specific filter type.
    pub kind: FilterKind,
}

/// The kind of filter to apply.
#[derive(Debug, Clone)]
pub enum FilterKind {
    /// Filter for inet (TCP/UDP) sockets.
    Inet(InetFilter),
    /// Filter for Unix domain sockets.
    Unix(UnixFilter),
    /// Filter for Netlink sockets.
    Netlink(NetlinkFilter),
    /// Filter for Packet sockets.
    Packet(PacketFilter),
}

impl SocketFilter {
    /// Create a TCP socket filter.
    pub fn tcp() -> InetFilterBuilder {
        InetFilterBuilder::new(Protocol::Tcp)
    }

    /// Create a UDP socket filter.
    pub fn udp() -> InetFilterBuilder {
        InetFilterBuilder::new(Protocol::Udp)
    }

    /// Create an MPTCP socket filter.
    pub fn mptcp() -> InetFilterBuilder {
        InetFilterBuilder::new(Protocol::Mptcp)
    }

    /// Create an SCTP socket filter.
    pub fn sctp() -> InetFilterBuilder {
        InetFilterBuilder::new(Protocol::Sctp)
    }

    /// Create a DCCP socket filter.
    pub fn dccp() -> InetFilterBuilder {
        InetFilterBuilder::new(Protocol::Dccp)
    }

    /// Create a raw IP socket filter.
    pub fn raw() -> InetFilterBuilder {
        InetFilterBuilder::new(Protocol::Raw)
    }

    /// Create a Unix socket filter.
    pub fn unix() -> UnixFilterBuilder {
        UnixFilterBuilder::new()
    }

    /// Create a Netlink socket filter.
    pub fn netlink() -> NetlinkFilterBuilder {
        NetlinkFilterBuilder::new()
    }

    /// Create a Packet socket filter.
    pub fn packet() -> PacketFilterBuilder {
        PacketFilterBuilder::new()
    }
}

/// Filter for inet (TCP/UDP/SCTP) sockets.
#[derive(Debug, Clone)]
pub struct InetFilter {
    /// Address family (IPv4, IPv6, or both).
    pub family: Option<AddressFamily>,
    /// Protocol.
    pub protocol: Protocol,
    /// State bitmask.
    pub states: u32,
    /// Extensions to request.
    pub extensions: u8,
    /// Filter by local address.
    pub local_addr: Option<IpAddr>,
    /// Filter by local port.
    pub local_port: Option<u16>,
    /// Filter by remote address.
    pub remote_addr: Option<IpAddr>,
    /// Filter by remote port.
    pub remote_port: Option<u16>,
    /// Filter by interface index.
    pub interface: Option<u32>,
    /// Filter by socket mark.
    pub mark: Option<(u32, u32)>, // (value, mask)
    /// Filter by cgroup ID.
    pub cgroup_id: Option<u64>,
}

impl Default for InetFilter {
    fn default() -> Self {
        Self {
            family: None,
            protocol: Protocol::Tcp,
            states: TcpState::all_mask(),
            extensions: 0,
            local_addr: None,
            local_port: None,
            remote_addr: None,
            remote_port: None,
            interface: None,
            mark: None,
            cgroup_id: None,
        }
    }
}

/// Builder for inet socket filters.
#[derive(Debug, Clone)]
pub struct InetFilterBuilder {
    filter: InetFilter,
}

impl InetFilterBuilder {
    /// Create a new builder for the given protocol.
    pub fn new(protocol: Protocol) -> Self {
        Self {
            filter: InetFilter {
                protocol,
                ..Default::default()
            },
        }
    }

    /// Filter by address family.
    pub fn family(mut self, family: AddressFamily) -> Self {
        self.filter.family = Some(family);
        self
    }

    /// Filter by IPv4 only.
    pub fn ipv4(self) -> Self {
        self.family(AddressFamily::Inet)
    }

    /// Filter by IPv6 only.
    pub fn ipv6(self) -> Self {
        self.family(AddressFamily::Inet6)
    }

    /// Filter by socket states.
    pub fn states(mut self, states: &[TcpState]) -> Self {
        self.filter.states = states.iter().fold(0, |acc, s| acc | s.mask());
        self
    }

    /// Filter by all states.
    pub fn all_states(mut self) -> Self {
        self.filter.states = TcpState::all_mask();
        self
    }

    /// Filter by connected states only.
    pub fn connected(mut self) -> Self {
        self.filter.states = TcpState::connected_mask();
        self
    }

    /// Filter by listening state only.
    pub fn listening(mut self) -> Self {
        self.filter.states = TcpState::Listen.mask();
        self
    }

    /// Request memory info extension.
    pub fn with_mem_info(mut self) -> Self {
        self.filter.extensions |= InetExtension::MemInfo.mask();
        self
    }

    /// Request TCP info extension.
    pub fn with_tcp_info(mut self) -> Self {
        self.filter.extensions |= InetExtension::Info.mask();
        self
    }

    /// Request congestion info extension.
    pub fn with_congestion(mut self) -> Self {
        self.filter.extensions |= InetExtension::Cong.mask();
        self
    }

    /// Request TOS extension.
    pub fn with_tos(mut self) -> Self {
        self.filter.extensions |= InetExtension::Tos.mask();
        self
    }

    /// Request all extensions.
    pub fn with_all_extensions(mut self) -> Self {
        self.filter.extensions = 0xFF;
        self
    }

    /// Filter by local address.
    pub fn local_addr(mut self, addr: IpAddr) -> Self {
        self.filter.local_addr = Some(addr);
        self
    }

    /// Filter by local port.
    pub fn local_port(mut self, port: u16) -> Self {
        self.filter.local_port = Some(port);
        self
    }

    /// Filter by remote address.
    pub fn remote_addr(mut self, addr: IpAddr) -> Self {
        self.filter.remote_addr = Some(addr);
        self
    }

    /// Filter by remote port.
    pub fn remote_port(mut self, port: u16) -> Self {
        self.filter.remote_port = Some(port);
        self
    }

    /// Filter by interface index.
    pub fn interface(mut self, ifindex: u32) -> Self {
        self.filter.interface = Some(ifindex);
        self
    }

    /// Filter by socket mark.
    pub fn mark(mut self, value: u32, mask: u32) -> Self {
        self.filter.mark = Some((value, mask));
        self
    }

    /// Filter by cgroup ID.
    pub fn cgroup(mut self, cgroup_id: u64) -> Self {
        self.filter.cgroup_id = Some(cgroup_id);
        self
    }

    /// Build the filter.
    pub fn build(self) -> SocketFilter {
        SocketFilter {
            kind: FilterKind::Inet(self.filter),
        }
    }
}

/// Filter for Unix domain sockets.
#[derive(Debug, Clone)]
pub struct UnixFilter {
    /// Socket types to query.
    pub socket_types: u32,
    /// State bitmask.
    pub states: u32,
    /// What to show in response.
    pub show: u32,
    /// Filter by specific inode.
    pub inode: Option<u32>,
    /// Filter by path pattern.
    pub path_pattern: Option<String>,
}

impl Default for UnixFilter {
    fn default() -> Self {
        Self {
            socket_types: 0xFFFFFFFF,
            states: TcpState::all_mask(),
            show: UnixShow::combine(&[UnixShow::Name, UnixShow::Peer, UnixShow::RqLen]),
            inode: None,
            path_pattern: None,
        }
    }
}

/// Builder for Unix socket filters.
#[derive(Debug, Clone)]
pub struct UnixFilterBuilder {
    filter: UnixFilter,
}

impl UnixFilterBuilder {
    /// Create a new Unix filter builder.
    pub fn new() -> Self {
        Self {
            filter: UnixFilter::default(),
        }
    }

    /// Filter by stream sockets only.
    pub fn stream(mut self) -> Self {
        self.filter.socket_types = 1 << libc::SOCK_STREAM;
        self
    }

    /// Filter by datagram sockets only.
    pub fn dgram(mut self) -> Self {
        self.filter.socket_types = 1 << libc::SOCK_DGRAM;
        self
    }

    /// Filter by seqpacket sockets only.
    pub fn seqpacket(mut self) -> Self {
        self.filter.socket_types = 1 << libc::SOCK_SEQPACKET;
        self
    }

    /// Filter by states.
    pub fn states(mut self, states: &[TcpState]) -> Self {
        self.filter.states = states.iter().fold(0, |acc, s| acc | s.mask());
        self
    }

    /// Filter by listening state.
    pub fn listening(mut self) -> Self {
        self.filter.states = TcpState::Listen.mask();
        self
    }

    /// Filter by connected state.
    pub fn connected(mut self) -> Self {
        self.filter.states = TcpState::connected_mask();
        self
    }

    /// Show socket name.
    pub fn show_name(mut self) -> Self {
        self.filter.show |= UnixShow::Name.mask();
        self
    }

    /// Show VFS info.
    pub fn show_vfs(mut self) -> Self {
        self.filter.show |= UnixShow::Vfs.mask();
        self
    }

    /// Show peer info.
    pub fn show_peer(mut self) -> Self {
        self.filter.show |= UnixShow::Peer.mask();
        self
    }

    /// Show pending connections.
    pub fn show_icons(mut self) -> Self {
        self.filter.show |= UnixShow::Icons.mask();
        self
    }

    /// Show queue lengths.
    pub fn show_rqlen(mut self) -> Self {
        self.filter.show |= UnixShow::RqLen.mask();
        self
    }

    /// Show memory info.
    pub fn show_meminfo(mut self) -> Self {
        self.filter.show |= UnixShow::MemInfo.mask();
        self
    }

    /// Show UID.
    pub fn show_uid(mut self) -> Self {
        self.filter.show |= UnixShow::Uid.mask();
        self
    }

    /// Show all information.
    pub fn show_all(mut self) -> Self {
        self.filter.show = UnixShow::combine(&[
            UnixShow::Name,
            UnixShow::Vfs,
            UnixShow::Peer,
            UnixShow::Icons,
            UnixShow::RqLen,
            UnixShow::MemInfo,
            UnixShow::Uid,
        ]);
        self
    }

    /// Filter by specific inode.
    pub fn inode(mut self, inode: u32) -> Self {
        self.filter.inode = Some(inode);
        self
    }

    /// Filter by path pattern.
    pub fn path(mut self, pattern: impl Into<String>) -> Self {
        self.filter.path_pattern = Some(pattern.into());
        self
    }

    /// Build the filter.
    pub fn build(self) -> SocketFilter {
        SocketFilter {
            kind: FilterKind::Unix(self.filter),
        }
    }
}

impl Default for UnixFilterBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Filter for Netlink sockets.
#[derive(Debug, Clone)]
pub struct NetlinkFilter {
    /// Netlink protocol to filter by (None = all).
    pub protocol: Option<u8>,
    /// Show memory info.
    pub show_meminfo: bool,
    /// Show groups.
    pub show_groups: bool,
}

impl Default for NetlinkFilter {
    fn default() -> Self {
        Self {
            protocol: None,
            show_meminfo: false,
            show_groups: true,
        }
    }
}

/// Builder for Netlink socket filters.
#[derive(Debug, Clone)]
pub struct NetlinkFilterBuilder {
    filter: NetlinkFilter,
}

impl NetlinkFilterBuilder {
    /// Create a new Netlink filter builder.
    pub fn new() -> Self {
        Self {
            filter: NetlinkFilter::default(),
        }
    }

    /// Filter by protocol.
    pub fn protocol(mut self, protocol: u8) -> Self {
        self.filter.protocol = Some(protocol);
        self
    }

    /// Show memory info.
    pub fn show_meminfo(mut self) -> Self {
        self.filter.show_meminfo = true;
        self
    }

    /// Show groups.
    pub fn show_groups(mut self) -> Self {
        self.filter.show_groups = true;
        self
    }

    /// Build the filter.
    pub fn build(self) -> SocketFilter {
        SocketFilter {
            kind: FilterKind::Netlink(self.filter),
        }
    }
}

impl Default for NetlinkFilterBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Filter for Packet sockets.
#[derive(Debug, Clone)]
pub struct PacketFilter {
    /// Show memory info.
    pub show_meminfo: bool,
    /// Show info.
    pub show_info: bool,
    /// Show fanout.
    pub show_fanout: bool,
}

impl Default for PacketFilter {
    fn default() -> Self {
        Self {
            show_meminfo: false,
            show_info: true,
            show_fanout: true,
        }
    }
}

/// Builder for Packet socket filters.
#[derive(Debug, Clone)]
pub struct PacketFilterBuilder {
    filter: PacketFilter,
}

impl PacketFilterBuilder {
    /// Create a new Packet filter builder.
    pub fn new() -> Self {
        Self {
            filter: PacketFilter::default(),
        }
    }

    /// Show memory info.
    pub fn show_meminfo(mut self) -> Self {
        self.filter.show_meminfo = true;
        self
    }

    /// Show socket info.
    pub fn show_info(mut self) -> Self {
        self.filter.show_info = true;
        self
    }

    /// Show fanout info.
    pub fn show_fanout(mut self) -> Self {
        self.filter.show_fanout = true;
        self
    }

    /// Build the filter.
    pub fn build(self) -> SocketFilter {
        SocketFilter {
            kind: FilterKind::Packet(self.filter),
        }
    }
}

impl Default for PacketFilterBuilder {
    fn default() -> Self {
        Self::new()
    }
}