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

Commit

Permalink
make imports more consistent with the other crates
Browse files Browse the repository at this point in the history
  • Loading branch information
little-dude committed Feb 20, 2022
1 parent 38068f2 commit a8b9e01
Show file tree
Hide file tree
Showing 18 changed files with 161 additions and 138 deletions.
20 changes: 11 additions & 9 deletions netlink-packet-netfilter/examples/nflog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ use std::{net::Ipv4Addr, time::Duration};
use byteorder::{ByteOrder, NetworkEndian};
use netlink_packet_netfilter::{
constants::*,
message::{NetfilterMessage, NetfilterMessageInner},
nflog::{
self,
config::{ConfigCmd, ConfigFlags, ConfigMode, Timeout},
packet::PacketNla,
config_request,
nlas::{
config::{ConfigCmd, ConfigFlags, ConfigMode, Timeout},
packet::PacketNla,
},
NfLogMessage,
},
NetlinkMessage,
NetlinkPayload,
nl::{NetlinkMessage, NetlinkPayload},
NetfilterMessage,
NetfilterMessageInner,
};
use netlink_sys::{constants::NETLINK_NETFILTER, Socket};

Expand All @@ -45,7 +47,7 @@ fn main() {
socket.bind_auto().unwrap();

// Then we issue the PfBind command
let packet = nflog::config::config_request(AF_INET, 0, vec![ConfigCmd::PfBind.into()]);
let packet = config_request(AF_INET, 0, vec![ConfigCmd::PfBind.into()]);
let mut buf = vec![0; packet.header.length as usize];
packet.serialize(&mut buf[..]);
println!(">>> {:?}", packet);
Expand All @@ -60,7 +62,7 @@ fn main() {

// After that we issue a Bind command, to start receiving packets. We can also set various parameters at the same time
let timeout: Timeout = Duration::from_millis(100).into();
let packet = nflog::config::config_request(
let packet = config_request(
AF_INET,
1,
vec![
Expand Down Expand Up @@ -92,7 +94,7 @@ fn main() {
let rx_packet = <NetlinkMessage<NetfilterMessage>>::deserialize(bytes).unwrap();

for nla in get_packet_nlas(&rx_packet) {
if let nflog::packet::PacketNla::Payload(payload) = nla {
if let PacketNla::Payload(payload) = nla {
let src = Ipv4Addr::from(NetworkEndian::read_u32(&payload[12..]));
let dst = Ipv4Addr::from(NetworkEndian::read_u32(&payload[16..]));
println!("Packet from {} to {}", src, dst);
Expand Down
31 changes: 31 additions & 0 deletions netlink-packet-netfilter/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,34 @@ pub const NFNL_SUBSYS_CTNETLINK_TIMEOUT: u8 = libc::NFNL_SUBSYS_CTNETLINK_TIMEOU
pub const NFNL_SUBSYS_CTHELPER: u8 = libc::NFNL_SUBSYS_CTHELPER as u8;
pub const NFNL_SUBSYS_NFTABLES: u8 = libc::NFNL_SUBSYS_NFTABLES as u8;
pub const NFNL_SUBSYS_NFT_COMPAT: u8 = libc::NFNL_SUBSYS_NFT_COMPAT as u8;

pub const NFULA_CFG_CMD: u16 = libc::NFULA_CFG_CMD as u16;
pub const NFULA_CFG_MODE: u16 = libc::NFULA_CFG_MODE as u16;
pub const NFULA_CFG_NLBUFSIZ: u16 = libc::NFULA_CFG_NLBUFSIZ as u16;
pub const NFULA_CFG_TIMEOUT: u16 = libc::NFULA_CFG_TIMEOUT as u16;
pub const NFULA_CFG_QTHRESH: u16 = libc::NFULA_CFG_QTHRESH as u16;
pub const NFULA_CFG_FLAGS: u16 = libc::NFULA_CFG_FLAGS as u16;
pub const NLBUFSIZ_MAX: u32 = 131072;

pub const NFULA_PACKET_HDR: u16 = libc::NFULA_PACKET_HDR as u16;
pub const NFULA_MARK: u16 = libc::NFULA_MARK as u16;
pub const NFULA_TIMESTAMP: u16 = libc::NFULA_TIMESTAMP as u16;
pub const NFULA_IFINDEX_INDEV: u16 = libc::NFULA_IFINDEX_INDEV as u16;
pub const NFULA_IFINDEX_OUTDEV: u16 = libc::NFULA_IFINDEX_OUTDEV as u16;
pub const NFULA_IFINDEX_PHYSINDEV: u16 = libc::NFULA_IFINDEX_PHYSINDEV as u16;
pub const NFULA_IFINDEX_PHYSOUTDEV: u16 = libc::NFULA_IFINDEX_PHYSOUTDEV as u16;
pub const NFULA_HWADDR: u16 = libc::NFULA_HWADDR as u16;
pub const NFULA_PAYLOAD: u16 = libc::NFULA_PAYLOAD as u16;
pub const NFULA_PREFIX: u16 = libc::NFULA_PREFIX as u16;
pub const NFULA_UID: u16 = libc::NFULA_UID as u16;
pub const NFULA_SEQ: u16 = libc::NFULA_SEQ as u16;
pub const NFULA_SEQ_GLOBAL: u16 = libc::NFULA_SEQ_GLOBAL as u16;
pub const NFULA_GID: u16 = libc::NFULA_GID as u16;
pub const NFULA_HWTYPE: u16 = libc::NFULA_HWTYPE as u16;
pub const NFULA_HWHEADER: u16 = libc::NFULA_HWHEADER as u16;
pub const NFULA_HWLEN: u16 = libc::NFULA_HWLEN as u16;
pub const NFULA_CT: u16 = libc::NFULA_CT as u16;
pub const NFULA_CT_INFO: u16 = libc::NFULA_CT_INFO as u16;

pub const NFULNL_MSG_CONFIG: u8 = libc::NFULNL_MSG_CONFIG as u8;
pub const NFULNL_MSG_PACKET: u8 = libc::NFULNL_MSG_PACKET as u8;
14 changes: 5 additions & 9 deletions netlink-packet-netfilter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// SPDX-License-Identifier: MIT

pub extern crate netlink_packet_core as nl;
pub(crate) extern crate netlink_packet_utils as utils;
pub use self::utils::{traits, DecodeError};
pub use netlink_packet_core::{
ErrorMessage,
NetlinkBuffer,
NetlinkHeader,
NetlinkMessage,
NetlinkPayload,
};

pub use self::utils::{nla, traits, DecodeError};

pub(crate) mod buffer;
pub mod constants;
pub mod message;
mod message;
pub use message::{NetfilterHeader, NetfilterMessage, NetfilterMessageInner};
pub mod nflog;
1 change: 0 additions & 1 deletion netlink-packet-netfilter/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ impl Emitable for NetfilterMessageInner {
}

#[derive(Debug, PartialEq, Eq, Clone)]

pub struct NetfilterMessage {
pub header: NetfilterHeader,
pub inner: NetfilterMessageInner,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
// SPDX-License-Identifier: MIT

use netlink_packet_core::DecodeError;
use netlink_packet_utils::{nla::DefaultNla, Emitable, Parseable, ParseableParametrized};
use std::fmt::Debug;

use crate::{buffer::NetfilterBuffer, constants::NFNL_SUBSYS_ULOG};

use config::ConfigNla;

use self::packet::PacketNla;

pub const NFULNL_MSG_CONFIG: u8 = libc::NFULNL_MSG_CONFIG as u8;
pub const NFULNL_MSG_PACKET: u8 = libc::NFULNL_MSG_PACKET as u8;

pub const NFULA_CFG_CMD: u16 = libc::NFULA_CFG_CMD as u16;
pub const NFULA_CFG_MODE: u16 = libc::NFULA_CFG_MODE as u16;
pub const NFULA_CFG_NLBUFSIZ: u16 = libc::NFULA_CFG_NLBUFSIZ as u16;
pub const NFULA_CFG_QTHRESH: u16 = libc::NFULA_CFG_QTHRESH as u16;

pub mod config;
pub mod packet;
use crate::{
buffer::NetfilterBuffer,
constants::{NFNL_SUBSYS_ULOG, NFULNL_MSG_CONFIG, NFULNL_MSG_PACKET},
nflog::nlas::{config::ConfigNla, packet::PacketNla},
nla::DefaultNla,
traits::{Emitable, Parseable, ParseableParametrized},
DecodeError,
};

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum NfLogMessage {
Expand Down
32 changes: 32 additions & 0 deletions netlink-packet-netfilter/src/nflog/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT

mod message;
pub use message::NfLogMessage;
pub mod nlas;

use crate::{
constants::NFNETLINK_V0,
nflog::nlas::config::ConfigNla,
nl::{NetlinkHeader, NetlinkMessage, NetlinkPayload, NLM_F_ACK, NLM_F_REQUEST},
NetfilterHeader,
NetfilterMessage,
};

pub fn config_request(
family: u8,
group_num: u16,
nlas: Vec<ConfigNla>,
) -> NetlinkMessage<NetfilterMessage> {
let mut message = NetlinkMessage {
header: NetlinkHeader {
flags: NLM_F_REQUEST | NLM_F_ACK,
..Default::default()
},
payload: NetlinkPayload::from(NetfilterMessage::new(
NetfilterHeader::new(family, NFNETLINK_V0, group_num),
NfLogMessage::Config(nlas),
)),
};
message.finalize();
message
}
12 changes: 12 additions & 0 deletions netlink-packet-netfilter/src/nflog/nlas/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
mod config_cmd;
mod config_flags;
mod config_mode;
mod nla;
mod timeout;

pub use config_cmd::ConfigCmd;
pub use config_flags::ConfigFlags;
pub use config_mode::{ConfigMode, CopyMode};
pub use nla::ConfigNla;
pub use timeout::Timeout;
Original file line number Diff line number Diff line change
@@ -1,48 +1,29 @@
// SPDX-License-Identifier: MIT

use anyhow::Context;
use byteorder::{BigEndian, ByteOrder};
use derive_more::{From, IsVariant};
use netlink_packet_core::{
DecodeError,
NetlinkHeader,
NetlinkMessage,
NetlinkPayload,
NLM_F_ACK,
NLM_F_REQUEST,
};
use netlink_packet_utils::{
nla::{DefaultNla, Nla, NlaBuffer},
parsers::{parse_u16_be, parse_u32_be, parse_u8},
Parseable,
};

pub use config_cmd::ConfigCmd;
pub use config_flags::ConfigFlags;
pub use config_mode::{ConfigMode, CopyMode};
pub use timeout::Timeout;

use crate::{
constants::NFNETLINK_V0,
message::{NetfilterHeader, NetfilterMessage},
constants::{
NFULA_CFG_CMD,
NFULA_CFG_FLAGS,
NFULA_CFG_MODE,
NFULA_CFG_NLBUFSIZ,
NFULA_CFG_QTHRESH,
NFULA_CFG_TIMEOUT,
},
nflog::nlas::config::{
config_mode::ConfigModeBuffer,
ConfigCmd,
ConfigFlags,
ConfigMode,
Timeout,
},
nl::DecodeError,
nla::{DefaultNla, Nla, NlaBuffer},
traits::Parseable,
utils::parsers::{parse_u16_be, parse_u32_be, parse_u8},
};

use super::NfLogMessage;

mod config_cmd;
mod config_flags;
mod config_mode;
mod timeout;

pub const NFULA_CFG_CMD: u16 = libc::NFULA_CFG_CMD as u16;
pub const NFULA_CFG_MODE: u16 = libc::NFULA_CFG_MODE as u16;
pub const NFULA_CFG_NLBUFSIZ: u16 = libc::NFULA_CFG_NLBUFSIZ as u16;
pub const NFULA_CFG_TIMEOUT: u16 = libc::NFULA_CFG_TIMEOUT as u16;
pub const NFULA_CFG_QTHRESH: u16 = libc::NFULA_CFG_QTHRESH as u16;
pub const NFULA_CFG_FLAGS: u16 = libc::NFULA_CFG_FLAGS as u16;

pub const NLBUFSIZ_MAX: u32 = 131072;

#[derive(Clone, Debug, PartialEq, Eq, From, IsVariant)]
pub enum ConfigNla {
Cmd(ConfigCmd),
Expand Down Expand Up @@ -103,7 +84,7 @@ impl<'buffer, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'buffer T>> for Conf
ConfigCmd::from(parse_u8(payload).context("invalid NFULA_CFG_CMD value")?).into()
}
NFULA_CFG_MODE => {
let buf = config_mode::ConfigModeBuffer::new_checked(payload)?;
let buf = ConfigModeBuffer::new_checked(payload)?;
ConfigMode::parse(&buf)?.into()
}
NFULA_CFG_NLBUFSIZ => ConfigNla::NlBufSiz(
Expand All @@ -125,22 +106,3 @@ impl<'buffer, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'buffer T>> for Conf
Ok(nla)
}
}

pub fn config_request(
family: u8,
group_num: u16,
nlas: Vec<ConfigNla>,
) -> NetlinkMessage<NetfilterMessage> {
let mut message = NetlinkMessage {
header: NetlinkHeader {
flags: NLM_F_REQUEST | NLM_F_ACK,
..Default::default()
},
payload: NetlinkPayload::from(NetfilterMessage::new(
NetfilterHeader::new(family, NFNETLINK_V0, group_num),
NfLogMessage::Config(nlas),
)),
};
message.finalize();
message
}
2 changes: 2 additions & 0 deletions netlink-packet-netfilter/src/nflog/nlas/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod config;
pub mod packet;
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// SPDX-License-Identifier: MIT

use netlink_packet_core::DecodeError;
use netlink_packet_utils::{buffer, nla::Nla, Parseable};

use super::NFULA_HWADDR;
use crate::{constants::NFULA_HWADDR, nla::Nla, traits::Parseable, utils::buffer, DecodeError};

const HW_ADDR_LEN: usize = 12;

Expand Down
9 changes: 9 additions & 0 deletions netlink-packet-netfilter/src/nflog/nlas/packet/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod hw_addr;
mod nla;
mod packet_hdr;
mod timestamp;

pub use hw_addr::{HwAddr, HwAddrBuffer};
pub use nla::PacketNla;
pub use packet_hdr::{PacketHdr, PacketHdrBuffer};
pub use timestamp::{TimeStamp, TimeStampBuffer};
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,38 @@ use std::ffi::{CStr, CString};
use anyhow::Context;
use byteorder::{BigEndian, ByteOrder};
use derive_more::{From, IsVariant};
use netlink_packet_core::DecodeError;
use netlink_packet_utils::{
nla::{DefaultNla, Nla, NlaBuffer},
parsers::{parse_u16_be, parse_u32_be},
Parseable,
};

use self::{
hw_addr::{HwAddr, HwAddrBuffer},
packet_hdr::{PacketHdr, PacketHdrBuffer},
timestamp::{TimeStamp, TimeStampBuffer},
use crate::{
constants::{
NFULA_GID,
NFULA_HWADDR,
NFULA_HWHEADER,
NFULA_HWLEN,
NFULA_HWTYPE,
NFULA_IFINDEX_INDEV,
NFULA_IFINDEX_OUTDEV,
NFULA_IFINDEX_PHYSINDEV,
NFULA_IFINDEX_PHYSOUTDEV,
NFULA_MARK,
NFULA_PACKET_HDR,
NFULA_PAYLOAD,
NFULA_PREFIX,
NFULA_SEQ,
NFULA_SEQ_GLOBAL,
NFULA_TIMESTAMP,
NFULA_UID,
},
nflog::nlas::packet::{
hw_addr::{HwAddr, HwAddrBuffer},
packet_hdr::{PacketHdr, PacketHdrBuffer},
timestamp::{TimeStamp, TimeStampBuffer},
},
nla::{DefaultNla, Nla, NlaBuffer},
traits::Parseable,
utils::parsers::{parse_u16_be, parse_u32_be},
DecodeError,
};

mod hw_addr;
mod packet_hdr;
mod timestamp;

pub const NFULA_PACKET_HDR: u16 = libc::NFULA_PACKET_HDR as u16;
pub const NFULA_MARK: u16 = libc::NFULA_MARK as u16;
pub const NFULA_TIMESTAMP: u16 = libc::NFULA_TIMESTAMP as u16;
pub const NFULA_IFINDEX_INDEV: u16 = libc::NFULA_IFINDEX_INDEV as u16;
pub const NFULA_IFINDEX_OUTDEV: u16 = libc::NFULA_IFINDEX_OUTDEV as u16;
pub const NFULA_IFINDEX_PHYSINDEV: u16 = libc::NFULA_IFINDEX_PHYSINDEV as u16;
pub const NFULA_IFINDEX_PHYSOUTDEV: u16 = libc::NFULA_IFINDEX_PHYSOUTDEV as u16;
pub const NFULA_HWADDR: u16 = libc::NFULA_HWADDR as u16;
pub const NFULA_PAYLOAD: u16 = libc::NFULA_PAYLOAD as u16;
pub const NFULA_PREFIX: u16 = libc::NFULA_PREFIX as u16;
pub const NFULA_UID: u16 = libc::NFULA_UID as u16;
pub const NFULA_SEQ: u16 = libc::NFULA_SEQ as u16;
pub const NFULA_SEQ_GLOBAL: u16 = libc::NFULA_SEQ_GLOBAL as u16;
pub const NFULA_GID: u16 = libc::NFULA_GID as u16;
pub const NFULA_HWTYPE: u16 = libc::NFULA_HWTYPE as u16;
pub const NFULA_HWHEADER: u16 = libc::NFULA_HWHEADER as u16;
pub const NFULA_HWLEN: u16 = libc::NFULA_HWLEN as u16;
pub const NFULA_CT: u16 = libc::NFULA_CT as u16;
pub const NFULA_CT_INFO: u16 = libc::NFULA_CT_INFO as u16;

#[derive(Clone, Debug, PartialEq, Eq, From, IsVariant)]
pub enum PacketNla {
#[from]
Expand Down
Loading

0 comments on commit a8b9e01

Please sign in to comment.