Skip to content

Commit 38585b7

Browse files
committed
Use netlink-packet-core 0.8
Also removed the need of `anyhow`, `byteorder` and `netlink-packet-utils`. Signed-off-by: Gris Ge <[email protected]>
1 parent 2b701ac commit 38585b7

File tree

11 files changed

+51
-83
lines changed

11 files changed

+51
-83
lines changed

Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ readme = "README.md"
1111
description = "generic netlink packet types"
1212

1313
[dependencies]
14-
anyhow = "1.0.39"
15-
byteorder = "1.4.2"
16-
netlink-packet-core = { version = "0.7.0" }
17-
netlink-packet-utils = { version = "0.5.2" }
14+
netlink-packet-core = { version = "0.8.0" }
1815

1916
[dev-dependencies]
2017
netlink-sys = { version = "0.8.3" }

src/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

88
buffer!(GenlBuffer(GENL_HDRLEN) {

src/ctrl/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
88
use self::nlas::*;
99
use crate::{constants::*, traits::*, GenlHeader};
10-
use anyhow::Context;
11-
use netlink_packet_utils::{nla::NlasIterator, traits::*, DecodeError};
10+
use netlink_packet_core::{
11+
DecodeError, Emitable, ErrorContext, NlasIterator, Parseable,
12+
ParseableParametrized,
13+
};
1214
use std::convert::{TryFrom, TryInto};
1315

1416
/// Netlink attributes for this family

src/ctrl/nlas/mcast.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
// SPDX-License-Identifier: MIT
22

33
use crate::constants::*;
4-
use anyhow::Context;
5-
use byteorder::{ByteOrder, NativeEndian};
6-
use netlink_packet_utils::{
7-
nla::{Nla, NlaBuffer},
8-
parsers::*,
9-
traits::*,
10-
DecodeError,
4+
use netlink_packet_core::{
5+
emit_u32, parse_string, parse_u32, DecodeError, Emitable, ErrorContext,
6+
Nla, NlaBuffer, Parseable,
117
};
128
use std::{mem::size_of_val, ops::Deref};
139

@@ -67,7 +63,7 @@ impl Nla for McastGrpAttrs {
6763
fn value_len(&self) -> usize {
6864
use McastGrpAttrs::*;
6965
match self {
70-
Name(s) => s.as_bytes().len() + 1,
66+
Name(s) => s.len() + 1,
7167
Id(v) => size_of_val(v),
7268
}
7369
}
@@ -87,7 +83,7 @@ impl Nla for McastGrpAttrs {
8783
buffer[..s.len()].copy_from_slice(s.as_bytes());
8884
buffer[s.len()] = 0;
8985
}
90-
Id(v) => NativeEndian::write_u32(buffer, *v),
86+
Id(v) => emit_u32(buffer, *v).unwrap(),
9187
}
9288
}
9389
}

src/ctrl/nlas/mod.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
// SPDX-License-Identifier: MIT
22

33
use crate::constants::*;
4-
use anyhow::Context;
5-
use byteorder::{ByteOrder, NativeEndian};
6-
use netlink_packet_utils::{
7-
nla::{Nla, NlaBuffer, NlasIterator},
8-
parsers::*,
9-
traits::*,
10-
DecodeError,
4+
use netlink_packet_core::{
5+
emit_u16, emit_u32, parse_string, parse_u16, parse_u32, DecodeError,
6+
Emitable, ErrorContext, Nla, NlaBuffer, NlasIterator, Parseable,
117
};
128
use std::mem::size_of_val;
139

@@ -73,14 +69,14 @@ impl Nla for GenlCtrlAttrs {
7369
fn emit_value(&self, buffer: &mut [u8]) {
7470
use GenlCtrlAttrs::*;
7571
match self {
76-
FamilyId(v) => NativeEndian::write_u16(buffer, *v),
72+
FamilyId(v) => emit_u16(buffer, *v).unwrap(),
7773
FamilyName(s) => {
7874
buffer[..s.len()].copy_from_slice(s.as_bytes());
7975
buffer[s.len()] = 0;
8076
}
81-
Version(v) => NativeEndian::write_u32(buffer, *v),
82-
HdrSize(v) => NativeEndian::write_u32(buffer, *v),
83-
MaxAttr(v) => NativeEndian::write_u32(buffer, *v),
77+
Version(v) => emit_u32(buffer, *v).unwrap(),
78+
HdrSize(v) => emit_u32(buffer, *v).unwrap(),
79+
MaxAttr(v) => emit_u32(buffer, *v).unwrap(),
8480
Ops(nlas) => {
8581
OpList::from(nlas).as_slice().emit(buffer);
8682
}
@@ -89,7 +85,7 @@ impl Nla for GenlCtrlAttrs {
8985
}
9086
Policy(nla) => nla.emit_value(buffer),
9187
OpPolicy(nla) => nla.emit_value(buffer),
92-
Op(v) => NativeEndian::write_u32(buffer, *v),
88+
Op(v) => emit_u32(buffer, *v).unwrap(),
9389
}
9490
}
9591
}

src/ctrl/nlas/oppolicy.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
// SPDX-License-Identifier: MIT
22

33
use crate::constants::*;
4-
use anyhow::Context;
5-
use byteorder::{ByteOrder, NativeEndian};
6-
use netlink_packet_utils::{
7-
nla::{Nla, NlaBuffer, NlasIterator},
8-
parsers::*,
9-
traits::*,
10-
DecodeError,
4+
use netlink_packet_core::{
5+
emit_u32, parse_u32, DecodeError, Emitable, ErrorContext, Nla, NlaBuffer,
6+
NlasIterator, Parseable,
117
};
128
use std::mem::size_of_val;
139

@@ -76,8 +72,8 @@ impl Nla for OppolicyIndexAttr {
7672
fn emit_value(&self, buffer: &mut [u8]) {
7773
use OppolicyIndexAttr::*;
7874
match self {
79-
Do(v) => NativeEndian::write_u32(buffer, *v),
80-
Dump(v) => NativeEndian::write_u32(buffer, *v),
75+
Do(v) => emit_u32(buffer, *v).unwrap(),
76+
Dump(v) => emit_u32(buffer, *v).unwrap(),
8177
}
8278
}
8379
}

src/ctrl/nlas/ops.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
// SPDX-License-Identifier: MIT
22

33
use crate::constants::*;
4-
use anyhow::Context;
5-
use byteorder::{ByteOrder, NativeEndian};
6-
use netlink_packet_utils::{
7-
nla::{Nla, NlaBuffer},
8-
parsers::*,
9-
traits::*,
10-
DecodeError,
4+
use netlink_packet_core::{
5+
emit_u32, parse_u32, DecodeError, Emitable, ErrorContext, Nla, NlaBuffer,
6+
Parseable,
117
};
128
use std::{mem::size_of_val, ops::Deref};
139

@@ -82,8 +78,8 @@ impl Nla for OpAttrs {
8278
fn emit_value(&self, buffer: &mut [u8]) {
8379
use OpAttrs::*;
8480
match self {
85-
Id(v) => NativeEndian::write_u32(buffer, *v),
86-
Flags(v) => NativeEndian::write_u32(buffer, *v),
81+
Id(v) => emit_u32(buffer, *v).unwrap(),
82+
Flags(v) => emit_u32(buffer, *v).unwrap(),
8783
}
8884
}
8985
}

src/ctrl/nlas/policy.rs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
// SPDX-License-Identifier: MIT
22

33
use crate::constants::*;
4-
use anyhow::Context;
5-
use byteorder::{ByteOrder, NativeEndian};
6-
use netlink_packet_utils::{
7-
nla::{Nla, NlaBuffer, NlasIterator},
8-
parsers::*,
9-
traits::*,
10-
DecodeError,
11-
};
12-
use std::{
13-
convert::TryFrom,
14-
mem::{size_of, size_of_val},
4+
use netlink_packet_core::{
5+
emit_i64, emit_u32, emit_u64, parse_i64, parse_u32, parse_u64, DecodeError,
6+
Emitable, ErrorContext, Nla, NlaBuffer, NlasIterator, Parseable,
157
};
8+
use std::{convert::TryFrom, mem::size_of_val};
169

1710
// PolicyAttr
1811

@@ -150,17 +143,17 @@ impl Nla for NlPolicyTypeAttrs {
150143
fn emit_value(&self, buffer: &mut [u8]) {
151144
use NlPolicyTypeAttrs::*;
152145
match self {
153-
Type(v) => NativeEndian::write_u32(buffer, u32::from(*v)),
154-
MinValueSigned(v) => NativeEndian::write_i64(buffer, *v),
155-
MaxValueSigned(v) => NativeEndian::write_i64(buffer, *v),
156-
MaxValueUnsigned(v) => NativeEndian::write_u64(buffer, *v),
157-
MinValueUnsigned(v) => NativeEndian::write_u64(buffer, *v),
158-
MinLength(v) => NativeEndian::write_u32(buffer, *v),
159-
MaxLength(v) => NativeEndian::write_u32(buffer, *v),
160-
PolicyIdx(v) => NativeEndian::write_u32(buffer, *v),
161-
PolicyMaxType(v) => NativeEndian::write_u32(buffer, *v),
162-
Bitfield32Mask(v) => NativeEndian::write_u32(buffer, *v),
163-
Mask(v) => NativeEndian::write_u64(buffer, *v),
146+
Type(v) => emit_u32(buffer, u32::from(*v)).unwrap(),
147+
MinValueSigned(v) => emit_i64(buffer, *v).unwrap(),
148+
MaxValueSigned(v) => emit_i64(buffer, *v).unwrap(),
149+
MaxValueUnsigned(v) => emit_u64(buffer, *v).unwrap(),
150+
MinValueUnsigned(v) => emit_u64(buffer, *v).unwrap(),
151+
MinLength(v) => emit_u32(buffer, *v).unwrap(),
152+
MaxLength(v) => emit_u32(buffer, *v).unwrap(),
153+
PolicyIdx(v) => emit_u32(buffer, *v).unwrap(),
154+
PolicyMaxType(v) => emit_u32(buffer, *v).unwrap(),
155+
Bitfield32Mask(v) => emit_u32(buffer, *v).unwrap(),
156+
Mask(v) => emit_u64(buffer, *v).unwrap(),
164157
}
165158
}
166159
}
@@ -296,11 +289,3 @@ impl TryFrom<u32> for NlaType {
296289
})
297290
}
298291
}
299-
300-
// FIXME: Add this into netlink_packet_utils::parser
301-
fn parse_i64(payload: &[u8]) -> Result<i64, DecodeError> {
302-
if payload.len() != size_of::<i64>() {
303-
return Err(format!("invalid i64: {payload:?}").into());
304-
}
305-
Ok(NativeEndian::read_i64(payload))
306-
}

src/header.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

77
/// Generic Netlink header
88
#[derive(Clone, Copy, Debug, PartialEq, Eq)]

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
//! serialization process including its specific header (if any) and the netlink
2626
//! attributes.
2727
//!
28-
//! To achieve this, you should implement [`netlink_packet_utils::Emitable`]
28+
//! To achieve this, you should implement [`netlink_packet_core::Emitable`]
2929
//! trait for the payload type.
3030
//!
31-
//! For deserialization, [`netlink_packet_utils::ParseableParametrized<[u8],
32-
//! GenlHeader>`](netlink_packet_utils::ParseableParametrized) trait should be
31+
//! For deserialization, [`netlink_packet_core::ParseableParametrized<[u8],
32+
//! GenlHeader>`](netlink_packet_core::ParseableParametrized) trait should be
3333
//! implemented. As mention above, to provide more scalability, we use the
3434
//! simplest buffer type: `[u8]` here. You can turn it into other buffer type
3535
//! easily during deserializing.
@@ -68,7 +68,7 @@
6868
//! the header data structure in your payload type and handle the serialization.
6969
7070
#[macro_use]
71-
extern crate netlink_packet_utils;
71+
extern crate netlink_packet_core;
7272

7373
pub mod buffer;
7474
pub use self::buffer::GenlBuffer;

0 commit comments

Comments
 (0)