Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/clippy-rustfmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
run: cargo fmt --all -- --check

- name: clippy
run: cargo clippy
run: cargo clippy -- -D warnings
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ readme = "README.md"
description = "generic netlink packet types"

[dependencies]
anyhow = "1.0.39"
byteorder = "1.4.2"
netlink-packet-core = { version = "0.7.0" }
netlink-packet-utils = { version = "0.5.2" }
netlink-packet-core = { version = "0.8.0" }

[dev-dependencies]
netlink-sys = { version = "0.8.3" }
2 changes: 1 addition & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Buffer definition of generic netlink packet
use crate::{constants::GENL_HDRLEN, header::GenlHeader, message::GenlMessage};
use netlink_packet_utils::{DecodeError, Parseable, ParseableParametrized};
use netlink_packet_core::{DecodeError, Parseable, ParseableParametrized};
use std::fmt::Debug;

buffer!(GenlBuffer(GENL_HDRLEN) {
Expand Down
6 changes: 4 additions & 2 deletions src/ctrl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

use self::nlas::*;
use crate::{constants::*, traits::*, GenlHeader};
use anyhow::Context;
use netlink_packet_utils::{nla::NlasIterator, traits::*, DecodeError};
use netlink_packet_core::{
DecodeError, Emitable, ErrorContext, NlasIterator, Parseable,
ParseableParametrized,
};
use std::convert::{TryFrom, TryInto};

/// Netlink attributes for this family
Expand Down
14 changes: 5 additions & 9 deletions src/ctrl/nlas/mcast.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// SPDX-License-Identifier: MIT

use crate::constants::*;
use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
nla::{Nla, NlaBuffer},
parsers::*,
traits::*,
DecodeError,
use netlink_packet_core::{
emit_u32, parse_string, parse_u32, DecodeError, Emitable, ErrorContext,
Nla, NlaBuffer, Parseable,
};
use std::{mem::size_of_val, ops::Deref};

Expand Down Expand Up @@ -67,7 +63,7 @@ impl Nla for McastGrpAttrs {
fn value_len(&self) -> usize {
use McastGrpAttrs::*;
match self {
Name(s) => s.as_bytes().len() + 1,
Name(s) => s.len() + 1,
Id(v) => size_of_val(v),
}
}
Expand All @@ -87,7 +83,7 @@ impl Nla for McastGrpAttrs {
buffer[..s.len()].copy_from_slice(s.as_bytes());
buffer[s.len()] = 0;
}
Id(v) => NativeEndian::write_u32(buffer, *v),
Id(v) => emit_u32(buffer, *v).unwrap(),
}
}
}
Expand Down
20 changes: 8 additions & 12 deletions src/ctrl/nlas/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// SPDX-License-Identifier: MIT

use crate::constants::*;
use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
nla::{Nla, NlaBuffer, NlasIterator},
parsers::*,
traits::*,
DecodeError,
use netlink_packet_core::{
emit_u16, emit_u32, parse_string, parse_u16, parse_u32, DecodeError,
Emitable, ErrorContext, Nla, NlaBuffer, NlasIterator, Parseable,
};
use std::mem::size_of_val;

Expand Down Expand Up @@ -73,14 +69,14 @@ impl Nla for GenlCtrlAttrs {
fn emit_value(&self, buffer: &mut [u8]) {
use GenlCtrlAttrs::*;
match self {
FamilyId(v) => NativeEndian::write_u16(buffer, *v),
FamilyId(v) => emit_u16(buffer, *v).unwrap(),
FamilyName(s) => {
buffer[..s.len()].copy_from_slice(s.as_bytes());
buffer[s.len()] = 0;
}
Version(v) => NativeEndian::write_u32(buffer, *v),
HdrSize(v) => NativeEndian::write_u32(buffer, *v),
MaxAttr(v) => NativeEndian::write_u32(buffer, *v),
Version(v) => emit_u32(buffer, *v).unwrap(),
HdrSize(v) => emit_u32(buffer, *v).unwrap(),
MaxAttr(v) => emit_u32(buffer, *v).unwrap(),
Ops(nlas) => {
OpList::from(nlas).as_slice().emit(buffer);
}
Expand All @@ -89,7 +85,7 @@ impl Nla for GenlCtrlAttrs {
}
Policy(nla) => nla.emit_value(buffer),
OpPolicy(nla) => nla.emit_value(buffer),
Op(v) => NativeEndian::write_u32(buffer, *v),
Op(v) => emit_u32(buffer, *v).unwrap(),
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/ctrl/nlas/oppolicy.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// SPDX-License-Identifier: MIT

use crate::constants::*;
use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
nla::{Nla, NlaBuffer, NlasIterator},
parsers::*,
traits::*,
DecodeError,
use netlink_packet_core::{
emit_u32, parse_u32, DecodeError, Emitable, ErrorContext, Nla, NlaBuffer,
NlasIterator, Parseable,
};
use std::mem::size_of_val;

Expand Down Expand Up @@ -76,8 +72,8 @@ impl Nla for OppolicyIndexAttr {
fn emit_value(&self, buffer: &mut [u8]) {
use OppolicyIndexAttr::*;
match self {
Do(v) => NativeEndian::write_u32(buffer, *v),
Dump(v) => NativeEndian::write_u32(buffer, *v),
Do(v) => emit_u32(buffer, *v).unwrap(),
Dump(v) => emit_u32(buffer, *v).unwrap(),
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/ctrl/nlas/ops.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// SPDX-License-Identifier: MIT

use crate::constants::*;
use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
nla::{Nla, NlaBuffer},
parsers::*,
traits::*,
DecodeError,
use netlink_packet_core::{
emit_u32, parse_u32, DecodeError, Emitable, ErrorContext, Nla, NlaBuffer,
Parseable,
};
use std::{mem::size_of_val, ops::Deref};

Expand Down Expand Up @@ -82,8 +78,8 @@ impl Nla for OpAttrs {
fn emit_value(&self, buffer: &mut [u8]) {
use OpAttrs::*;
match self {
Id(v) => NativeEndian::write_u32(buffer, *v),
Flags(v) => NativeEndian::write_u32(buffer, *v),
Id(v) => emit_u32(buffer, *v).unwrap(),
Flags(v) => emit_u32(buffer, *v).unwrap(),
}
}
}
Expand Down
45 changes: 15 additions & 30 deletions src/ctrl/nlas/policy.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
// SPDX-License-Identifier: MIT

use crate::constants::*;
use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
nla::{Nla, NlaBuffer, NlasIterator},
parsers::*,
traits::*,
DecodeError,
};
use std::{
convert::TryFrom,
mem::{size_of, size_of_val},
use netlink_packet_core::{
emit_i64, emit_u32, emit_u64, parse_i64, parse_u32, parse_u64, DecodeError,
Emitable, ErrorContext, Nla, NlaBuffer, NlasIterator, Parseable,
};
use std::{convert::TryFrom, mem::size_of_val};

// PolicyAttr

Expand Down Expand Up @@ -150,17 +143,17 @@ impl Nla for NlPolicyTypeAttrs {
fn emit_value(&self, buffer: &mut [u8]) {
use NlPolicyTypeAttrs::*;
match self {
Type(v) => NativeEndian::write_u32(buffer, u32::from(*v)),
MinValueSigned(v) => NativeEndian::write_i64(buffer, *v),
MaxValueSigned(v) => NativeEndian::write_i64(buffer, *v),
MaxValueUnsigned(v) => NativeEndian::write_u64(buffer, *v),
MinValueUnsigned(v) => NativeEndian::write_u64(buffer, *v),
MinLength(v) => NativeEndian::write_u32(buffer, *v),
MaxLength(v) => NativeEndian::write_u32(buffer, *v),
PolicyIdx(v) => NativeEndian::write_u32(buffer, *v),
PolicyMaxType(v) => NativeEndian::write_u32(buffer, *v),
Bitfield32Mask(v) => NativeEndian::write_u32(buffer, *v),
Mask(v) => NativeEndian::write_u64(buffer, *v),
Type(v) => emit_u32(buffer, u32::from(*v)).unwrap(),
MinValueSigned(v) => emit_i64(buffer, *v).unwrap(),
MaxValueSigned(v) => emit_i64(buffer, *v).unwrap(),
MaxValueUnsigned(v) => emit_u64(buffer, *v).unwrap(),
MinValueUnsigned(v) => emit_u64(buffer, *v).unwrap(),
MinLength(v) => emit_u32(buffer, *v).unwrap(),
MaxLength(v) => emit_u32(buffer, *v).unwrap(),
PolicyIdx(v) => emit_u32(buffer, *v).unwrap(),
PolicyMaxType(v) => emit_u32(buffer, *v).unwrap(),
Bitfield32Mask(v) => emit_u32(buffer, *v).unwrap(),
Mask(v) => emit_u64(buffer, *v).unwrap(),
}
}
}
Expand Down Expand Up @@ -296,11 +289,3 @@ impl TryFrom<u32> for NlaType {
})
}
}

// FIXME: Add this into netlink_packet_utils::parser
fn parse_i64(payload: &[u8]) -> Result<i64, DecodeError> {
if payload.len() != size_of::<i64>() {
return Err(format!("invalid i64: {payload:?}").into());
}
Ok(NativeEndian::read_i64(payload))
}
2 changes: 1 addition & 1 deletion src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! header definition of generic netlink packet
use crate::{buffer::GenlBuffer, constants::GENL_HDRLEN};
use netlink_packet_utils::{DecodeError, Emitable, Parseable};
use netlink_packet_core::{DecodeError, Emitable, Parseable};

/// Generic Netlink header
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
//! serialization process including its specific header (if any) and the netlink
//! attributes.
//!
//! To achieve this, you should implement [`netlink_packet_utils::Emitable`]
//! To achieve this, you should implement [`netlink_packet_core::Emitable`]
//! trait for the payload type.
//!
//! For deserialization, [`netlink_packet_utils::ParseableParametrized<[u8],
//! GenlHeader>`](netlink_packet_utils::ParseableParametrized) trait should be
//! For deserialization, [`netlink_packet_core::ParseableParametrized<[u8],
//! GenlHeader>`](netlink_packet_core::ParseableParametrized) trait should be
//! implemented. As mention above, to provide more scalability, we use the
//! simplest buffer type: `[u8]` here. You can turn it into other buffer type
//! easily during deserializing.
Expand Down Expand Up @@ -68,7 +68,7 @@
//! the header data structure in your payload type and handle the serialization.

#[macro_use]
extern crate netlink_packet_utils;
extern crate netlink_packet_core;

pub mod buffer;
pub use self::buffer::GenlBuffer;
Expand Down
4 changes: 2 additions & 2 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use crate::{buffer::GenlBuffer, header::GenlHeader, traits::*};
use netlink_packet_core::{
NetlinkDeserializable, NetlinkHeader, NetlinkPayload, NetlinkSerializable,
DecodeError, Emitable, NetlinkDeserializable, NetlinkHeader,
NetlinkPayload, NetlinkSerializable, ParseableParametrized,
};
use netlink_packet_utils::{DecodeError, Emitable, ParseableParametrized};
use std::fmt::Debug;

#[cfg(doc)]
Expand Down