Skip to content

Commit 6b9660d

Browse files
committed
diag: add support for SOCK_DESTROY
It is implemented on top of SockDiagMessage, and allows using Linux's CONFIG_INET_DIAG_DESTROY feature to close an arbitrary socket. A long running, but buggy process might have hanging sockets kept alive by error. Using SOCK_DESTROY allows closing arbitrary sockets as root; an example tool that uses this feature and this crate is at: https://github.com/anisse/tcpkill
1 parent f5f4ad7 commit 6b9660d

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/message.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use netlink_packet_utils::{
88
DecodeError,
99
};
1010

11-
use crate::{inet, unix, SockDiagBuffer, SOCK_DIAG_BY_FAMILY};
11+
use crate::{inet, unix, SockDiagBuffer, SOCK_DESTROY, SOCK_DIAG_BY_FAMILY};
1212

1313
#[derive(Debug, PartialEq, Eq, Clone)]
1414
pub enum SockDiagMessage {
@@ -93,3 +93,44 @@ impl From<SockDiagMessage> for NetlinkPayload<SockDiagMessage> {
9393
NetlinkPayload::InnerMessage(message)
9494
}
9595
}
96+
97+
#[derive(Debug, PartialEq, Eq, Clone)]
98+
pub struct SockDiagDestroy(SockDiagMessage);
99+
100+
impl SockDiagDestroy {
101+
pub fn new(message: SockDiagMessage) -> SockDiagDestroy {
102+
SockDiagDestroy(message)
103+
}
104+
}
105+
106+
impl NetlinkSerializable for SockDiagDestroy {
107+
fn message_type(&self) -> u16 {
108+
SOCK_DESTROY
109+
}
110+
111+
fn buffer_len(&self) -> usize {
112+
NetlinkSerializable::buffer_len(&self.0)
113+
}
114+
115+
fn serialize(&self, buffer: &mut [u8]) {
116+
self.0.serialize(buffer)
117+
}
118+
}
119+
120+
impl NetlinkDeserializable for SockDiagDestroy {
121+
type Error = DecodeError;
122+
fn deserialize(
123+
header: &NetlinkHeader,
124+
payload: &[u8],
125+
) -> Result<Self, Self::Error> {
126+
Ok(SockDiagDestroy::new(SockDiagMessage::deserialize(
127+
header, payload,
128+
)?))
129+
}
130+
}
131+
132+
impl From<SockDiagDestroy> for NetlinkPayload<SockDiagDestroy> {
133+
fn from(message: SockDiagDestroy) -> Self {
134+
NetlinkPayload::InnerMessage(message)
135+
}
136+
}

0 commit comments

Comments
 (0)