From 297a24732036a083c18f91b220b6c3976cadcc18 Mon Sep 17 00:00:00 2001 From: Anisse Astier Date: Tue, 26 Jul 2022 23:07:25 +0200 Subject: [PATCH] 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. --- netlink-packet-sock-diag/src/message.rs | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/netlink-packet-sock-diag/src/message.rs b/netlink-packet-sock-diag/src/message.rs index 519814da..4471379d 100644 --- a/netlink-packet-sock-diag/src/message.rs +++ b/netlink-packet-sock-diag/src/message.rs @@ -10,6 +10,7 @@ use crate::{ NetlinkPayload, NetlinkSerializable, SockDiagBuffer, + SOCK_DESTROY, SOCK_DIAG_BY_FAMILY, }; @@ -93,3 +94,41 @@ impl From for NetlinkPayload { NetlinkPayload::InnerMessage(message) } } + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct SockDiagDestroy(SockDiagMessage); + +impl SockDiagDestroy { + pub fn new(message: SockDiagMessage) -> SockDiagDestroy { + SockDiagDestroy(message) + } +} + +impl NetlinkSerializable for SockDiagDestroy { + fn message_type(&self) -> u16 { + SOCK_DESTROY + } + + fn buffer_len(&self) -> usize { + NetlinkSerializable::buffer_len(&self.0) + } + + fn serialize(&self, buffer: &mut [u8]) { + self.0.serialize(buffer) + } +} + +impl NetlinkDeserializable for SockDiagDestroy { + type Error = DecodeError; + fn deserialize(header: &NetlinkHeader, payload: &[u8]) -> Result { + Ok(SockDiagDestroy::new(SockDiagMessage::deserialize( + header, payload, + )?)) + } +} + +impl From for NetlinkPayload { + fn from(message: SockDiagDestroy) -> Self { + NetlinkPayload::InnerMessage(message) + } +}