From adc6b9d86a17f8e86dc45757310ece3155769e32 Mon Sep 17 00:00:00 2001 From: Bruno Kirschner Date: Wed, 12 Jul 2017 16:44:04 +0200 Subject: [PATCH 1/4] [Bindings] Adds bindgen as dependency to allow auto generation of the netfilter_queue bindings during build. --- Cargo.toml | 7 ++++++- src/bindings.rs | 1 + src/generation/generate_bindings.rs | 26 ++++++++++++++++++++++++++ src/generation/wrapper.h | 2 ++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/bindings.rs create mode 100644 src/generation/generate_bindings.rs create mode 100644 src/generation/wrapper.h diff --git a/Cargo.toml b/Cargo.toml index c2796ec..5e647aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,13 @@ name = "nfqueue" version = "0.9.1" authors = [ "Pierre Chifflier " ] +build = "src/generation/generate_bindings.rs" + +[build-dependencies] +bindgen = "^0.26" + [dependencies] libc = "^0.2" -[dev_dependencies] +[dev-dependencies] pnet = "0.17.1" diff --git a/src/bindings.rs b/src/bindings.rs new file mode 100644 index 0000000..7fc370f --- /dev/null +++ b/src/bindings.rs @@ -0,0 +1 @@ +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); \ No newline at end of file diff --git a/src/generation/generate_bindings.rs b/src/generation/generate_bindings.rs new file mode 100644 index 0000000..0ee2179 --- /dev/null +++ b/src/generation/generate_bindings.rs @@ -0,0 +1,26 @@ +extern crate bindgen; + +use std::env; +use std::path::PathBuf; + +fn main() { + + // Tell cargo to tell rustc to link the netfilter_queue shared library + println!("cargo:rustc-link-lib=netfilter_queue"); + + let bindings = bindgen::Builder::default() + .header("src/generation/wrapper.h") + // White listings became necessary as I needed to included in wrapper.h to + // define the default integer types like uint32_t. I think I miss something but I + // couldn't grab it. + .whitelisted_type("(nfq|NFQ)_.*") + .whitelisted_function("(nfq|NFQ)_.*") + .whitelisted_var("(nfq|NFQ)_.*") + .generate() + .expect("Unable to generate bindings"); + + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + bindings + .write_to_file(out_path.join("bindings.rs")) + .expect("Couldn't write bindings!"); +} \ No newline at end of file diff --git a/src/generation/wrapper.h b/src/generation/wrapper.h new file mode 100644 index 0000000..1a36923 --- /dev/null +++ b/src/generation/wrapper.h @@ -0,0 +1,2 @@ +#include +#include \ No newline at end of file From 4e8c25a1b3e6046cf10b971b601395566c7085cb Mon Sep 17 00:00:00 2001 From: Bruno Kirschner Date: Wed, 12 Jul 2017 16:50:44 +0200 Subject: [PATCH 2/4] [Bindings] Integrates new auto generated bindings in favor of the previous existing ones. --- examples/nfq-example.rs | 11 ++++---- examples/nfq-parse.rs | 10 ++++--- src/bindings.rs | 4 +++ src/lib.rs | 52 +++++++++++++++-------------------- src/message.rs | 60 +++++++++++++---------------------------- 5 files changed, 56 insertions(+), 81 deletions(-) diff --git a/examples/nfq-example.rs b/examples/nfq-example.rs index 3867db8..9cad4a9 100644 --- a/examples/nfq-example.rs +++ b/examples/nfq-example.rs @@ -11,7 +11,7 @@ impl State { } } -fn queue_callback(msg: &nfqueue::Message, state:&mut State) { +fn queue_callback(msg: nfqueue::Message, state:&mut State) -> i32 { println!("Packet received [id: 0x{:x}]\n", msg.get_id()); println!(" -> msg: {}", msg); @@ -21,18 +21,19 @@ fn queue_callback(msg: &nfqueue::Message, state:&mut State) { state.count += 1; println!("count: {}", state.count); - msg.set_verdict(nfqueue::Verdict::Accept); + msg.set_verdict(nfqueue::Verdict::Accept) } fn main() { let mut q = nfqueue::Queue::new(State::new()); println!("nfqueue example program: print packets metadata and accept packets"); - q.open(); - q.unbind(libc::AF_INET); // ignore result, failure is not critical here + let protocol_family = libc::AF_INET as u16; + q.open(); + q.unbind(protocol_family); // ignore result, failure is not critical here - let rc = q.bind(libc::AF_INET); + let rc = q.bind(protocol_family); assert!(rc == 0); q.create_queue(0, queue_callback); diff --git a/examples/nfq-parse.rs b/examples/nfq-parse.rs index 87336f6..44ed5a3 100644 --- a/examples/nfq-parse.rs +++ b/examples/nfq-parse.rs @@ -134,7 +134,7 @@ fn handle_transport_protocol(id: u32, source: IpAddr, destination: IpAddr, proto } } -fn queue_callback(msg: &nfqueue::Message, state: &mut State) { +fn queue_callback(msg: nfqueue::Message, state: &mut State) -> i32 { println!("\n---"); println!("Packet received [id: 0x{:x}]\n", msg.get_id()); @@ -153,18 +153,20 @@ fn queue_callback(msg: &nfqueue::Message, state: &mut State) { None => println!("Malformed IPv4 packet"), } - msg.set_verdict(nfqueue::Verdict::Accept); + msg.set_verdict(nfqueue::Verdict::Accept) } fn main() { let mut q = nfqueue::Queue::new(State::new()); println!("nfqueue example program: parse packet protocol layers and accept packet"); + let protocol_family = libc::AF_INET as u16; + q.open(); - q.unbind(libc::AF_INET); // ignore result, failure is not critical here + q.unbind(protocol_family); // ignore result, failure is not critical here - let rc = q.bind(libc::AF_INET); + let rc = q.bind(protocol_family); assert!(rc == 0); q.create_queue(0, queue_callback); diff --git a/src/bindings.rs b/src/bindings.rs index 7fc370f..428a22c 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -1 +1,5 @@ +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + include!(concat!(env!("OUT_DIR"), "/bindings.rs")); \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index bda33b5..73e6a9f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,22 +71,8 @@ pub type NfqueueCallback = fn (&Message) -> (); type NfqueueCCallback = extern "C" fn (*const libc::c_void, *const libc::c_void, *const libc::c_void, *const libc::c_void ); -#[link(name = "netfilter_queue")] -extern { - // library setup - fn nfq_open() -> NfqueueHandle; - fn nfq_close(qh: NfqueueHandle); - fn nfq_bind_pf (qh: NfqueueHandle, pf: libc::c_int) -> libc::c_int; - fn nfq_unbind_pf (qh: NfqueueHandle, pf: libc::c_int) -> libc::c_int; - - // queue handling - fn nfq_fd (h: NfqueueHandle) -> libc::c_int; - fn nfq_create_queue(qh: NfqueueHandle, num: u16, cb: NfqueueCCallback, data: *mut libc::c_void) -> NfqueueQueueHandle; - fn nfq_destroy_queue(qh: NfqueueHandle) -> libc::c_int; - fn nfq_handle_packet(qh: NfqueueHandle, buf: *mut libc::c_void, rc: libc::c_int) -> libc::c_int; - fn nfq_set_mode (gh: NfqueueQueueHandle, mode: u8, range: u32) -> libc::c_int; - fn nfq_set_queuelen (gh: NfqueueQueueHandle, queuelen: u32) -> libc::c_int; -} +pub use bindings::*; +mod bindings; /// Copy modes pub enum CopyMode { @@ -104,9 +90,9 @@ const NFQNL_COPY_PACKET : u8 = 0x02; /// Opaque struct `Queue`: abstracts an NFLOG queue pub struct Queue { - qh : NfqueueHandle, - qqh : NfqueueQueueHandle, - cb : Option ()>, + qh : *mut nfq_handle, + qqh : *mut nfq_q_handle, + cb : Option i32>, data: T, } @@ -154,7 +140,7 @@ impl Queue { /// Remarks: /// /// **Requires root privileges** - pub fn bind(&self, pf: libc::c_int) -> i32 { + pub fn bind(&self, pf: libc::c_ushort) -> i32 { assert!(!self.qh.is_null()); return unsafe { nfq_bind_pf(self.qh,pf) }; } @@ -171,7 +157,7 @@ impl Queue { /// Remarks: /// /// **Requires root privileges** - pub fn unbind(&self, pf: libc::c_int) -> i32 { + pub fn unbind(&self, pf: libc::c_ushort) -> i32 { assert!(!self.qh.is_null()); return unsafe { nfq_unbind_pf(self.qh,pf) } } @@ -197,12 +183,14 @@ impl Queue { /// /// * `num`: the number of the queue to bind to /// * `cb`: callback function to call for each queued packet - pub fn create_queue(&mut self, num: u16, cb: fn(&Message, &mut T)) { + pub fn create_queue(&mut self, num: u16, cb: fn(Message, &mut T) -> i32) -> bool { assert!(!self.qh.is_null()); assert!(self.qqh.is_null()); let self_ptr = unsafe { std::mem::transmute(&*self) }; self.cb = Some(cb); - self.qqh = unsafe { nfq_create_queue(self.qh, num, real_callback::, self_ptr) }; + self.qqh = unsafe { nfq_create_queue(self.qh, num, Some(real_callback::), self_ptr) }; + + !self.qqh.is_null() } /// Destroys a group handle @@ -247,9 +235,9 @@ impl Queue { /// Sets the size of the queue in kernel. This fixes the maximum number of /// packets the kernel will store before internally before dropping upcoming /// packets - pub fn set_queuelen(&self, queuelen: u32) { + pub fn set_queue_maxlen(&self, queuelen: u32) -> i32 { assert!(!self.qqh.is_null()); - unsafe { nfq_set_queuelen(self.qqh, queuelen); } + unsafe { nfq_set_queue_maxlen(self.qqh, queuelen) } } /// Runs an infinite loop, waiting for packets and triggering the callback. @@ -260,11 +248,11 @@ impl Queue { let fd = self.fd(); let mut buf : [u8;65536] = [0;65536]; - let buf_ptr = buf.as_mut_ptr() as *mut libc::c_void; + let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char; let buf_len = buf.len() as libc::size_t; loop { - let rc = unsafe { libc::recv(fd,buf_ptr,buf_len,0) }; + let rc = unsafe { libc::recv(fd, buf_ptr as *mut libc::c_void, buf_len, 0) }; if rc < 0 { panic!("error in recv()"); }; let rv = unsafe { nfq_handle_packet(self.qh, buf_ptr, rc as libc::c_int) }; @@ -277,7 +265,7 @@ impl Queue { #[doc(hidden)] -extern "C" fn real_callback(qqh: *const libc::c_void, _nfmsg: *const libc::c_void, nfad: *const libc::c_void, data: *const libc::c_void ) { +unsafe extern "C" fn real_callback(qqh: *mut nfq_q_handle, _nfmsg: *mut nfgenmsg, nfad: *mut nfq_data, data: *mut std::os::raw::c_void) -> i32 { let raw : *mut Queue = unsafe { std::mem::transmute(data) }; let ref mut q = unsafe { &mut *raw }; @@ -286,7 +274,7 @@ extern "C" fn real_callback(qqh: *const libc::c_void, _nfmsg: *const libc::c_ match q.cb { None => panic!("no callback registered"), Some(callback) => { - callback(&mut msg, &mut q.data); + callback(msg, &mut q.data) }, } } @@ -329,9 +317,11 @@ mod tests { assert!(!q.qh.is_null()); - let rc = q.bind(libc::AF_INET); + let protocol_family = libc::AF_INET as u16; + let rc = q.bind(protocol_family); + println!("q.bind: {}", rc); - assert!(q.bind(libc::AF_INET) == 0); + assert!(q.bind(protocol_family) == 0); q.close(); } diff --git a/src/message.rs b/src/message.rs index 95e4e53..fe5673d 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,5 +1,6 @@ extern crate libc; +use bindings::*; use hwaddr::*; use std; @@ -7,8 +8,8 @@ type NfqueueData = *const libc::c_void; /// Opaque struct `Message`: abstracts NFLOG data representing a packet data and metadata pub struct Message { - qqh : *const libc::c_void, - nfad : NfqueueData, + qqh : *mut nfq_q_handle, + nfad : *mut nfq_data, id : u32, l3_proto : u16, } @@ -64,13 +65,13 @@ pub enum XMLFormatFlags { XmlAll, } -const NFQ_XML_HW : u32 = (1 << 0); -const NFQ_XML_MARK : u32 = (1 << 1); -const NFQ_XML_DEV : u32 = (1 << 2); -const NFQ_XML_PHYSDEV : u32 = (1 << 3); -const NFQ_XML_PAYLOAD : u32 = (1 << 4); -const NFQ_XML_TIME : u32 = (1 << 5); -const NFQ_XML_ALL : u32 = (!0u32); +const NFQ_XML_HW : i32 = (1 << 0); +const NFQ_XML_MARK : i32 = (1 << 1); +const NFQ_XML_DEV : i32 = (1 << 2); +const NFQ_XML_PHYSDEV : i32 = (1 << 3); +const NFQ_XML_PAYLOAD : i32 = (1 << 4); +const NFQ_XML_TIME : i32 = (1 << 5); +const NFQ_XML_ALL : i32 = (!0u32) as i32; /// Hardware address #[repr(C)] @@ -94,35 +95,12 @@ pub struct NfMsgPacketHdr { pub hook : u8, } -#[link(name = "netfilter_queue")] -extern { - // queue handling - //fn nfq_set_verdict(qqh: *const libc::c_void, id: u32, verdict: u32, data_len: u32, data: *const libc::c_uchar); - // requires netfilter_queue >= 1.0 - fn nfq_set_verdict2(qqh: *const libc::c_void, id: u32, verdict: u32, mark: u32, data_len: u32, data: *const libc::c_uchar); - - // message parsing functions - fn nfq_get_msg_packet_hdr(nfad: NfqueueData) -> *const libc::c_void; - fn nfq_get_nfmark (nfad: NfqueueData) -> u32; - fn nfq_get_timestamp (nfad: NfqueueData, tv: *mut libc::timeval) -> u32; - fn nfq_get_indev (nfad: NfqueueData) -> u32; - fn nfq_get_physindev (nfad: NfqueueData) -> u32; - fn nfq_get_outdev (nfad: NfqueueData) -> u32; - fn nfq_get_physoutdev (nfad: NfqueueData) -> u32; - - fn nfq_get_packet_hw (nfad: NfqueueData) -> *const NfMsgPacketHw; - fn nfq_get_payload (nfad: NfqueueData, data: &*mut libc::c_void) -> libc::c_int; - - // printing functions - fn nfq_snprintf_xml (buf: *mut u8, rem: libc::size_t, tb: NfqueueData, flags: libc::c_uint) -> libc::c_int; -} - impl Message { /// Create a `Messsage` from a valid NfqueueData pointer /// /// **This function should never be called directly** #[doc(hidden)] - pub fn new(qqh: *const libc::c_void, nfad: *const libc::c_void) -> Message { + pub fn new(qqh: *mut nfq_q_handle, nfad: *mut nfq_data) -> Message { let msg_hdr = unsafe { nfq_get_msg_packet_hdr(nfad) as *const NfMsgPacketHdr }; assert!(!msg_hdr.is_null()); let id = u32::from_be( unsafe{(*msg_hdr).packet_id} ); @@ -151,8 +129,8 @@ impl Message { } /// Get the packet timestamp - pub fn get_timestamp(&self) -> Result { - let mut tv = libc::timeval { + pub fn get_timestamp(&self) -> Result { + let mut tv = timeval { tv_sec: 0, tv_usec: 0, }; @@ -216,7 +194,7 @@ impl Message { pub fn get_packet_hw<'a>(&'a self) -> Result,NfqueueError> { let c_hw = unsafe { nfq_get_packet_hw(self.nfad) }; - if c_hw == std::ptr::null() { + if c_hw.is_null() { return Err(NfqueueError::NoSuchAttribute); } @@ -241,11 +219,11 @@ impl Message { /// /// * `verdict`: verdict to return to netfilter (`Verdict::Accept`, /// `Verdict::Drop`, ...) - pub fn set_verdict(&self, verdict: Verdict) { + pub fn set_verdict(&self, verdict: Verdict) -> i32 { assert!(!self.qqh.is_null()); let c_verdict = u32_of_verdict(verdict); //unsafe { nfq_set_verdict(self.qqh, self.id, c_verdict, 0, std::ptr::null_mut()) }; - unsafe { nfq_set_verdict2(self.qqh, self.id, c_verdict, 0, 0, std::ptr::null_mut()) }; + unsafe { nfq_set_verdict2(self.qqh, self.id, c_verdict, 0, 0, std::ptr::null_mut()) } } /// Issue a verdict on a packet, with a mark @@ -299,7 +277,7 @@ impl Message { /// depend on the mode set with the `set_mode()` function. pub fn get_payload<'a>(&'a self) -> &'a [u8] { let c_ptr = std::ptr::null_mut(); - let payload_len = unsafe { nfq_get_payload(self.nfad, &c_ptr) }; + let payload_len = unsafe { nfq_get_payload(self.nfad, *c_ptr) }; let payload : &[u8] = unsafe { std::slice::from_raw_parts(c_ptr as *mut u8, payload_len as usize) }; return payload; @@ -309,7 +287,7 @@ impl Message { pub fn as_xml_str(&self, flags: &[XMLFormatFlags]) -> Result { // if buffer size is smaller than output, nfq_snprintf_xml will fail let mut buf : [u8;65536] = [0;65536]; - let buf_ptr = buf.as_mut_ptr() as *mut libc::c_uchar; + let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char; let buf_len = buf.len() as libc::size_t; let xml_flags = flags.iter().map(|flag| { @@ -322,7 +300,7 @@ impl Message { XMLFormatFlags::XmlTime => NFQ_XML_TIME, XMLFormatFlags::XmlAll => NFQ_XML_ALL, } - }).fold(0u32, |acc, i| acc | i); + }).fold(0i32, |acc, i| acc | i); let rc = unsafe { nfq_snprintf_xml(buf_ptr, buf_len, self.nfad, xml_flags) }; if rc < 0 { panic!("nfq_snprintf_xml"); } // XXX see snprintf error codes From 9d802e1fb3fcda67ea8733602580cf2cceaad710 Mon Sep 17 00:00:00 2001 From: Bruno Kirschner Date: Wed, 12 Jul 2017 17:08:56 +0200 Subject: [PATCH 3/4] [Wrapper] Removes now unused code. --- examples/nfq-example.rs | 6 +--- examples/nfq-parse.rs | 7 +---- src/lib.rs | 64 +++++++++++++++++++---------------------- src/message.rs | 13 --------- 4 files changed, 31 insertions(+), 59 deletions(-) diff --git a/examples/nfq-example.rs b/examples/nfq-example.rs index 9cad4a9..5c1c90c 100644 --- a/examples/nfq-example.rs +++ b/examples/nfq-example.rs @@ -1,5 +1,5 @@ -extern crate nfqueue; extern crate libc; +extern crate nfqueue; struct State { count: u32, @@ -39,10 +39,6 @@ fn main() { q.create_queue(0, queue_callback); q.set_mode(nfqueue::CopyMode::CopyPacket, 0xffff); - q.run_loop(); - - - q.close(); } diff --git a/examples/nfq-parse.rs b/examples/nfq-parse.rs index 44ed5a3..014de7f 100644 --- a/examples/nfq-parse.rs +++ b/examples/nfq-parse.rs @@ -1,7 +1,7 @@ // Some code borrowed from https://github.com/libpnet/libpnet/blob/master/examples/packetdump.rs -extern crate nfqueue; extern crate libc; +extern crate nfqueue; use std::net::IpAddr; @@ -165,17 +165,12 @@ fn main() { q.open(); q.unbind(protocol_family); // ignore result, failure is not critical here - let rc = q.bind(protocol_family); assert!(rc == 0); q.create_queue(0, queue_callback); q.set_mode(nfqueue::CopyMode::CopyPacket, 0xffff); - q.run_loop(); - - - q.close(); } diff --git a/src/lib.rs b/src/lib.rs index 73e6a9f..f9b306b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,42 +18,51 @@ //! ```rust,ignore //! extern crate libc; //! extern crate nfqueue; -//! use std::fmt::Write; //! -//! fn callback(msg: &nfqueue::Message) { -//! println!(" -> msg: {}", msg); +//! struct State { +//! count: u32, +//! } //! -//! let payload_data = msg.get_payload(); -//! let mut s = String::new(); -//! for &byte in payload_data { -//! write!(&mut s, "{:X} ", byte).unwrap(); +//! impl State { +//! pub fn new() -> State { +//! State{ count:0 } //! } -//! println!("{}", s); +//! } +//! +//! fn queue_callback(msg: nfqueue::Message, state:&mut State) -> i32 { +//! println!("Packet received [id: 0x{:x}]\n", msg.get_id()); +//! +//! println!(" -> msg: {}", msg); //! //! println!("XML\n{}", msg.as_xml_str(&[nfqueue::XMLFormatFlags::XmlAll]).unwrap()); //! -//! msg.set_verdict(nfqueue::Verdict::Accept); +//! state.count += 1; +//! println!("count: {}", state.count); +//! +//! msg.set_verdict(nfqueue::Verdict::Accept) //! } //! //! fn main() { -//! let mut q = nfqueue::Queue::new(); +//! let mut q = nfqueue::Queue::new(State::new()); +//! println!("nfqueue example program: print packets metadata and accept packets"); +//! +//! let protocol_family = libc::AF_INET as u16; //! //! q.open(); +//! q.unbind(protocol_family); // ignore result, failure is not critical here //! -//! let rc = q.bind(libc::AF_INET); +//! let rc = q.bind(protocol_family); //! assert!(rc == 0); //! -//! q.create_queue(0, callback); +//! q.create_queue(0, queue_callback); //! q.set_mode(nfqueue::CopyMode::CopyPacket, 0xffff); //! -//! q.set_callback(callback); //! q.run_loop(); -//! //! q.close(); //! } +//! //! ``` - extern crate libc; pub use hwaddr::*; @@ -62,15 +71,6 @@ mod hwaddr; pub use message::*; mod message; -type NfqueueHandle = *const libc::c_void; -type NfqueueQueueHandle = *const libc::c_void; - -/// Prototype for the callback function, triggered when a packet is received -pub type NfqueueCallback = fn (&Message) -> (); - -type NfqueueCCallback = extern "C" fn (*const libc::c_void, *const libc::c_void, *const libc::c_void, *const libc::c_void ); - - pub use bindings::*; mod bindings; @@ -262,14 +262,12 @@ impl Queue { } - - #[doc(hidden)] -unsafe extern "C" fn real_callback(qqh: *mut nfq_q_handle, _nfmsg: *mut nfgenmsg, nfad: *mut nfq_data, data: *mut std::os::raw::c_void) -> i32 { +extern "C" fn real_callback(qqh: *mut nfq_q_handle, _nfmsg: *mut nfgenmsg, nfad: *mut nfq_data, data: *mut std::os::raw::c_void) -> i32 { let raw : *mut Queue = unsafe { std::mem::transmute(data) }; let ref mut q = unsafe { &mut *raw }; - let mut msg = Message::new (qqh, nfad); + let msg = Message::new (qqh, nfad); match q.cb { None => panic!("no callback registered"), @@ -279,13 +277,6 @@ unsafe extern "C" fn real_callback(qqh: *mut nfq_q_handle, _nfmsg: *mut nfgen } } - - - - - - - #[cfg(test)] mod tests { @@ -305,6 +296,9 @@ mod tests { q.close(); } + // Can't run this test by default as we do should not have enough rights. + // You need to enable it manually to run it via `cargo test` after you ensured that the program + // will have the right capabilities. #[test] #[ignore] fn nfqueue_bind() { diff --git a/src/message.rs b/src/message.rs index fe5673d..095e736 100644 --- a/src/message.rs +++ b/src/message.rs @@ -4,8 +4,6 @@ use bindings::*; use hwaddr::*; use std; -type NfqueueData = *const libc::c_void; - /// Opaque struct `Message`: abstracts NFLOG data representing a packet data and metadata pub struct Message { qqh : *mut nfq_q_handle, @@ -73,17 +71,6 @@ const NFQ_XML_PAYLOAD : i32 = (1 << 4); const NFQ_XML_TIME : i32 = (1 << 5); const NFQ_XML_ALL : i32 = (!0u32) as i32; -/// Hardware address -#[repr(C)] -struct NfMsgPacketHw { - /// Hardware address length - pub hw_addrlen : u16, - /// Padding (should be ignored) - pub _pad : u16, - /// The hardware address - pub hw_addr : [u8;8], -} - /// Metaheader wrapping a packet #[repr(C)] pub struct NfMsgPacketHdr { From 325e37b62c83a64717e525d351bd234bc73c5464 Mon Sep 17 00:00:00 2001 From: Bruno Kirschner Date: Wed, 12 Jul 2017 17:33:17 +0200 Subject: [PATCH 4/4] [Bindings|Wrapper] Moves enum generation to constified by default and removes duplicated XML flag constants. --- src/generation/generate_bindings.rs | 1 + src/message.rs | 12 ++---------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/generation/generate_bindings.rs b/src/generation/generate_bindings.rs index 0ee2179..1ab0fe5 100644 --- a/src/generation/generate_bindings.rs +++ b/src/generation/generate_bindings.rs @@ -16,6 +16,7 @@ fn main() { .whitelisted_type("(nfq|NFQ)_.*") .whitelisted_function("(nfq|NFQ)_.*") .whitelisted_var("(nfq|NFQ)_.*") + .constified_enum(".*") .generate() .expect("Unable to generate bindings"); diff --git a/src/message.rs b/src/message.rs index 095e736..8b29891 100644 --- a/src/message.rs +++ b/src/message.rs @@ -63,14 +63,6 @@ pub enum XMLFormatFlags { XmlAll, } -const NFQ_XML_HW : i32 = (1 << 0); -const NFQ_XML_MARK : i32 = (1 << 1); -const NFQ_XML_DEV : i32 = (1 << 2); -const NFQ_XML_PHYSDEV : i32 = (1 << 3); -const NFQ_XML_PAYLOAD : i32 = (1 << 4); -const NFQ_XML_TIME : i32 = (1 << 5); -const NFQ_XML_ALL : i32 = (!0u32) as i32; - /// Metaheader wrapping a packet #[repr(C)] pub struct NfMsgPacketHdr { @@ -287,9 +279,9 @@ impl Message { XMLFormatFlags::XmlTime => NFQ_XML_TIME, XMLFormatFlags::XmlAll => NFQ_XML_ALL, } - }).fold(0i32, |acc, i| acc | i); + }).fold(0u32, |acc, i| acc | i); - let rc = unsafe { nfq_snprintf_xml(buf_ptr, buf_len, self.nfad, xml_flags) }; + let rc = unsafe { nfq_snprintf_xml(buf_ptr, buf_len, self.nfad, xml_flags as i32) }; if rc < 0 { panic!("nfq_snprintf_xml"); } // XXX see snprintf error codes match std::str::from_utf8(&buf) {