Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Commit

Permalink
Use CString for the prefix NLA
Browse files Browse the repository at this point in the history
  • Loading branch information
dzamlo committed Nov 30, 2021
1 parent 106843e commit b5520b3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions netlink-packet-netfilter/src/nflog/packet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: MIT

use std::ffi::{CStr, CString};

use anyhow::Context;
use byteorder::{BigEndian, ByteOrder};
use derive_more::{From, IsVariant};
Expand Down Expand Up @@ -54,7 +56,7 @@ pub enum PacketNla {
#[from]
HwAddr(HwAddr),
Payload(Vec<u8>),
Prefix(Vec<u8>),
Prefix(CString),
Uid(u32),
Seq(u32),
SeqGlobal(u32),
Expand All @@ -78,7 +80,7 @@ impl Nla for PacketNla {
PacketNla::IfIndexPhysOutDev(_) => 4,
PacketNla::HwAddr(attr) => attr.value_len(),
PacketNla::Payload(vec) => vec.len(),
PacketNla::Prefix(vec) => vec.len(),
PacketNla::Prefix(cstring) => cstring.as_bytes_with_nul().len(),
PacketNla::Uid(_) => 4,
PacketNla::Seq(_) => 4,
PacketNla::SeqGlobal(_) => 4,
Expand Down Expand Up @@ -124,7 +126,7 @@ impl Nla for PacketNla {
PacketNla::IfIndexPhysOutDev(value) => BigEndian::write_u32(buffer, *value),
PacketNla::HwAddr(attr) => attr.emit_value(buffer),
PacketNla::Payload(vec) => buffer.copy_from_slice(vec),
PacketNla::Prefix(vec) => buffer.copy_from_slice(vec),
PacketNla::Prefix(cstring) => buffer.copy_from_slice(cstring.as_bytes_with_nul()),
PacketNla::Uid(value) => BigEndian::write_u32(buffer, *value),
PacketNla::Seq(value) => BigEndian::write_u32(buffer, *value),
PacketNla::SeqGlobal(value) => BigEndian::write_u32(buffer, *value),
Expand Down Expand Up @@ -174,7 +176,11 @@ impl<'buffer, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'buffer T>> for Pack
PacketNla::HwAddr(HwAddr::parse(&buf)?)
}
NFULA_PAYLOAD => PacketNla::Payload(payload.to_vec()),
NFULA_PREFIX => PacketNla::Prefix(payload.to_vec()),
NFULA_PREFIX => PacketNla::Prefix(
CStr::from_bytes_with_nul(payload)
.context("invalid NFULA_PREFIX value")?
.to_owned(),
),
NFULA_UID => PacketNla::Uid(parse_u32_be(payload).context("invalid NFULA_UID value")?),
NFULA_SEQ => PacketNla::Seq(parse_u32_be(payload).context("invalid NFULA_SEQ value")?),
NFULA_SEQ_GLOBAL => PacketNla::SeqGlobal(
Expand Down

0 comments on commit b5520b3

Please sign in to comment.