rtnetlink/
handle.rs

1// SPDX-License-Identifier: MIT
2
3use futures_util::stream::Stream;
4use netlink_packet_core::NetlinkMessage;
5use netlink_packet_route::RouteNetlinkMessage;
6use netlink_proto::{sys::SocketAddr, ConnectionHandle};
7
8use crate::{
9    AddressHandle, Error, LinkHandle, NeighbourHandle, RouteHandle, RuleHandle,
10};
11#[cfg(not(target_os = "freebsd"))]
12use crate::{
13    QDiscHandle, TrafficChainHandle, TrafficClassHandle, TrafficFilterHandle,
14};
15
16#[derive(Clone, Debug)]
17pub struct Handle(ConnectionHandle<RouteNetlinkMessage>);
18
19impl Handle {
20    pub(crate) fn new(conn: ConnectionHandle<RouteNetlinkMessage>) -> Self {
21        Handle(conn)
22    }
23
24    pub fn request(
25        &mut self,
26        message: NetlinkMessage<RouteNetlinkMessage>,
27    ) -> Result<impl Stream<Item = NetlinkMessage<RouteNetlinkMessage>>, Error>
28    {
29        self.0
30            .request(message, SocketAddr::new(0, 0))
31            .map_err(|_| Error::RequestFailed)
32    }
33
34    pub fn notify(
35        &mut self,
36        msg: NetlinkMessage<RouteNetlinkMessage>,
37    ) -> Result<(), Error> {
38        self.0
39            .notify(msg, SocketAddr::new(0, 0))
40            .map_err(|_| Error::RequestFailed)?;
41        Ok(())
42    }
43
44    /// Create a new handle, specifically for link requests (equivalent to `ip
45    /// link` commands)
46    pub fn link(&self) -> LinkHandle {
47        LinkHandle::new(self.clone())
48    }
49
50    /// Create a new handle, specifically for address requests (equivalent to
51    /// `ip addr` commands)
52    pub fn address(&self) -> AddressHandle {
53        AddressHandle::new(self.clone())
54    }
55
56    /// Create a new handle, specifically for routing table requests (equivalent
57    /// to `ip route` commands)
58    pub fn route(&self) -> RouteHandle {
59        RouteHandle::new(self.clone())
60    }
61
62    /// Create a new handle, specifically for routing rule requests (equivalent
63    /// to `ip rule` commands)
64    pub fn rule(&self) -> RuleHandle {
65        RuleHandle::new(self.clone())
66    }
67
68    /// Create a new handle, specifically for routing neighbours requests
69    /// (equivalent to `ip neighbour` commands)
70    pub fn neighbours(&self) -> NeighbourHandle {
71        NeighbourHandle::new(self.clone())
72    }
73
74    /// Create a new handle, specifically for traffic control qdisc requests
75    /// (equivalent to `tc qdisc show` commands)
76    #[cfg(not(target_os = "freebsd"))]
77    pub fn qdisc(&self) -> QDiscHandle {
78        QDiscHandle::new(self.clone())
79    }
80
81    /// Create a new handle, specifically for traffic control class requests
82    /// (equivalent to `tc class show dev <interface_name>` commands)
83    #[cfg(not(target_os = "freebsd"))]
84    pub fn traffic_class(&self, ifindex: i32) -> TrafficClassHandle {
85        TrafficClassHandle::new(self.clone(), ifindex)
86    }
87
88    /// Create a new handle, specifically for traffic control filter requests
89    /// (equivalent to `tc filter show dev <interface_name>` commands)
90    #[cfg(not(target_os = "freebsd"))]
91    pub fn traffic_filter(&self, ifindex: i32) -> TrafficFilterHandle {
92        TrafficFilterHandle::new(self.clone(), ifindex)
93    }
94
95    /// Create a new handle, specifically for traffic control chain requests
96    /// (equivalent to `tc chain show dev <interface_name>` commands)
97    #[cfg(not(target_os = "freebsd"))]
98    pub fn traffic_chain(&self, ifindex: i32) -> TrafficChainHandle {
99        TrafficChainHandle::new(self.clone(), ifindex)
100    }
101}