Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cargo clippy #142

Merged
merged 1 commit into from
Jan 20, 2025
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 src/address/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Nla for AddressAttribute {
IPV4_ADDR_LEN
}
}
Self::Label(ref string) => string.as_bytes().len() + 1,
Self::Label(ref string) => string.len() + 1,

Self::Flags(_) => size_of::<u32>(),

Expand Down
44 changes: 22 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
// SPDX-License-Identifier: MIT

//! The `netlink-packet-route` crate is designed to abstract Netlink route
//! protocol(`rtnetlink`) packet into Rust data types. The goal of this crate is
//! saving netlink user from reading Kernel Netlink codes.
//!
//! This crate grouped Netlink route protocol into these modules:
//! * `link`: NIC interface, similar to to `ip link` command.
//! * `address`: IP address, similar to `ip address` command.
//! * `route`: Route, similar to `ip route` command.
//! * `rule`: Route rule, similar to `ip rule` command.
//! * `tc`: Traffic control, similar to `tc` command.
//! * `neighbour`: Neighbour, similar to `ip neighbour` command.
//! * `neighbour_table`: Neighbour table, similar to `ip ntable` command.
//! * `nsid`: Namespace, similar to `ip netns` command.
//!
//! At the top level of this crate, we also provide:
//! * [AddressFamily]
//!
//! Normally, you should use [`rtnetlink`][rtnetlink_url] instead of using this
//! crate directly.
//!
//! [rtnetlink_url]: https://docs.rs/rtnetlink

pub mod address;
pub mod link;
pub mod neighbour;
Expand Down Expand Up @@ -42,28 +64,6 @@ pub use self::address_family_fallback::AddressFamily;
pub use self::ip::IpProtocol;
pub use self::message::{RouteNetlinkMessage, RouteNetlinkMessageBuffer};

/// The `netlink-packet-route` crate is designed to abstract Netlink route
/// protocol(`rtnetlink`) packet into Rust data types. The goal of this crate is
/// saving netlink user from reading Kernel Netlink codes.
///
/// This crate grouped Netlink route protocol into these modules:
/// * `link`: NIC interface, similar to to `ip link` command.
/// * `address`: IP address, similar to `ip address` command.
/// * `route`: Route, similar to `ip route` command.
/// * `rule`: Route rule, similar to `ip rule` command.
/// * `tc`: Traffic control, similar to `tc` command.
/// * `neighbour`: Neighbour, similar to `ip neighbour` command.
/// * `neighbour_table`: Neighbour table, similar to `ip ntable` command.
/// * `nsid`: Namespace, similar to `ip netns` command.
///
/// At the top level of this crate, we also provide:
/// * [AddressFamily]
///
/// Normally, you should use [`rtnetlink`][rtnetlink_url] instead of using this
/// crate directly.
///
/// [rtnetlink_url]: https://docs.rs/rtnetlink

#[macro_use]
extern crate netlink_packet_utils;

Expand Down
2 changes: 1 addition & 1 deletion src/link/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl Nla for LinkAttribute {
Self::IfName(string)
| Self::Qdisc(string)
| Self::IfAlias(string)
| Self::PhysPortName(string) => string.as_bytes().len() + 1,
| Self::PhysPortName(string) => string.len() + 1,

Self::Mode(_) | Self::Carrier(_) | Self::ProtoDown(_) => 1,

Expand Down
2 changes: 1 addition & 1 deletion src/link/prop_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Nla for Prop {
fn value_len(&self) -> usize {
use self::Prop::*;
match self {
AltIfName(ref string) => string.as_bytes().len() + 1,
AltIfName(ref string) => string.len() + 1,
Other(nla) => nla.value_len()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/link/sriov/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ buffer!(VfInfoBroadcastBuffer(VF_INFO_BROADCAST_LEN) {
addr: (slice, 0..VF_INFO_BROADCAST_LEN),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoBroadcastBuffer<&'a T>>
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfInfoBroadcastBuffer<&T>>
for VfInfoBroadcast
{
fn parse(buf: &VfInfoBroadcastBuffer<&T>) -> Result<Self, DecodeError> {
Expand Down
4 changes: 1 addition & 3 deletions src/link/sriov/guid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ buffer!(VfInfoGuidBuffer(VF_INFO_GUID_LEN) {
guid: (u64, 4..12),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoGuidBuffer<&'a T>>
for VfInfoGuid
{
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfInfoGuidBuffer<&T>> for VfInfoGuid {
fn parse(buf: &VfInfoGuidBuffer<&T>) -> Result<Self, DecodeError> {
Ok(Self::new(buf.vf_id(), buf.guid()))
}
Expand Down
2 changes: 1 addition & 1 deletion src/link/sriov/link_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ buffer!(VfInfoLinkStateBuffer(VF_INFO_LINK_STATE_LEN) {
state: (u32, 4..8),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoLinkStateBuffer<&'a T>>
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfInfoLinkStateBuffer<&T>>
for VfInfoLinkState
{
fn parse(buf: &VfInfoLinkStateBuffer<&T>) -> Result<Self, DecodeError> {
Expand Down
4 changes: 1 addition & 3 deletions src/link/sriov/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ buffer!(VfInfoMacBuffer(VF_INFO_MAC_LEN) {
mac: (slice, 4..VF_INFO_MAC_LEN),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoMacBuffer<&'a T>>
for VfInfoMac
{
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfInfoMacBuffer<&T>> for VfInfoMac {
fn parse(buf: &VfInfoMacBuffer<&T>) -> Result<Self, DecodeError> {
Ok(Self::new(buf.vf_id(), buf.mac()))
}
Expand Down
4 changes: 1 addition & 3 deletions src/link/sriov/rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ buffer!(VfInfoRateBuffer(VF_INFO_RATE_LEN) {
max_tx_rate: (u32, 8..12)
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoRateBuffer<&'a T>>
for VfInfoRate
{
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfInfoRateBuffer<&T>> for VfInfoRate {
fn parse(buf: &VfInfoRateBuffer<&T>) -> Result<Self, DecodeError> {
Ok(Self {
vf_id: buf.vf_id(),
Expand Down
2 changes: 1 addition & 1 deletion src/link/sriov/rss_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ buffer!(VfInfoRssQueryEnBuffer(VF_INFO_RSS_QUERY_EN_LEN) {
setting: (u32, 4..8),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoRssQueryEnBuffer<&'a T>>
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfInfoRssQueryEnBuffer<&T>>
for VfInfoRssQueryEn
{
fn parse(buf: &VfInfoRssQueryEnBuffer<&T>) -> Result<Self, DecodeError> {
Expand Down
2 changes: 1 addition & 1 deletion src/link/sriov/spoofchk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ buffer!(VfInfoSpoofCheckBuffer(VF_INFO_SPOOFCHK_LEN) {
setting: (u32, 4..8),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoSpoofCheckBuffer<&'a T>>
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfInfoSpoofCheckBuffer<&T>>
for VfInfoSpoofCheck
{
fn parse(buf: &VfInfoSpoofCheckBuffer<&T>) -> Result<Self, DecodeError> {
Expand Down
4 changes: 1 addition & 3 deletions src/link/sriov/trust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ buffer!(VfInfoTrustBuffer(VF_INFO_TRUST_LEN) {
setting: (u32, 4..8),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoTrustBuffer<&'a T>>
for VfInfoTrust
{
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfInfoTrustBuffer<&T>> for VfInfoTrust {
fn parse(buf: &VfInfoTrustBuffer<&T>) -> Result<Self, DecodeError> {
Ok(Self::new(
buf.vf_id(),
Expand Down
2 changes: 1 addition & 1 deletion src/link/sriov/tx_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ buffer!(VfInfoTxRateBuffer(VF_INFO_TX_RATE_LEN) {
rate: (u32, 4..8),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoTxRateBuffer<&'a T>>
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfInfoTxRateBuffer<&T>>
for VfInfoTxRate
{
fn parse(buf: &VfInfoTxRateBuffer<&T>) -> Result<Self, DecodeError> {
Expand Down
4 changes: 1 addition & 3 deletions src/link/sriov/vf_vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ buffer!(VfVlanInfoBuffer(VF_VLAN_INFO_LEN) {
protocol: (u16, 12..14),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfVlanInfoBuffer<&'a T>>
for VfVlanInfo
{
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfVlanInfoBuffer<&T>> for VfVlanInfo {
fn parse(buf: &VfVlanInfoBuffer<&T>) -> Result<Self, DecodeError> {
Ok(Self {
vf_id: buf.vf_id(),
Expand Down
4 changes: 1 addition & 3 deletions src/link/sriov/vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ buffer!(VfInfoVlanBuffer(VF_INFO_VLAN_LEN) {
qos: (u32, 8..12)
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoVlanBuffer<&'a T>>
for VfInfoVlan
{
impl<T: AsRef<[u8]> + ?Sized> Parseable<VfInfoVlanBuffer<&T>> for VfInfoVlan {
fn parse(buf: &VfInfoVlanBuffer<&T>) -> Result<Self, DecodeError> {
Ok(Self {
vf_id: buf.vf_id(),
Expand Down
8 changes: 4 additions & 4 deletions src/route/mpls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ impl From<u32> for MplsLabel {

impl From<MplsLabel> for u32 {
fn from(v: MplsLabel) -> u32 {
v.label << MPLS_LS_LABEL_SHIFT
| (v.traffic_class as u32) << MPLS_LS_TC_SHIFT
| (v.bottom_of_stack as u32) << MPLS_LS_S_SHIFT
| (v.ttl as u32) << MPLS_LS_TTL_SHIFT
(v.label << MPLS_LS_LABEL_SHIFT)
| ((v.traffic_class as u32) << MPLS_LS_TC_SHIFT)
| ((v.bottom_of_stack as u32) << MPLS_LS_S_SHIFT)
| ((v.ttl as u32) << MPLS_LS_TTL_SHIFT)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/route/next_hops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ pub struct RouteNextHop {
pub attributes: Vec<RouteAttribute>,
}

impl<'a, T: AsRef<[u8]>>
impl<T: AsRef<[u8]>>
ParseableParametrized<
RouteNextHopBuffer<&'a T>,
RouteNextHopBuffer<&T>,
(AddressFamily, RouteType, RouteLwEnCapType),
> for RouteNextHop
{
Expand Down
2 changes: 1 addition & 1 deletion src/route/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Emitable for RouteRealm {
}

fn emit(&self, buffer: &mut [u8]) {
let all = (self.source as u32) << 16 | self.destination as u32;
let all = ((self.source as u32) << 16) | self.destination as u32;
buffer.copy_from_slice(&all.to_ne_bytes());
}
}
2 changes: 1 addition & 1 deletion src/rule/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Nla for RuleAttribute {
Self::SourcePortRange(v) | Self::DestinationPortRange(v) => {
v.buffer_len()
}
Self::Iifname(s) | Self::Oifname(s) => s.as_bytes().len() + 1,
Self::Iifname(s) | Self::Oifname(s) => s.len() + 1,
Self::Priority(_)
| Self::FwMark(_)
| Self::FwMask(_)
Expand Down
7 changes: 3 additions & 4 deletions src/tc/actions/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ impl Nla for TcActionAttribute {
match self {
Self::Cookie(bytes) => buffer.copy_from_slice(bytes.as_slice()),
Self::Kind(string) => {
buffer[..string.as_bytes().len()]
.copy_from_slice(string.as_bytes());
buffer[string.as_bytes().len()] = 0;
buffer[..string.len()].copy_from_slice(string.as_bytes());
buffer[string.len()] = 0;
}
Self::Options(opt) => opt.as_slice().emit(buffer),
Self::Index(value) | Self::InHwCount(value) => {
Expand Down Expand Up @@ -580,7 +579,7 @@ buffer!(TcfBuffer(TC_TCF_BUF_LEN) {
firstuse: (u64, 24..32),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<TcfBuffer<&'a T>> for Tcf {
impl<T: AsRef<[u8]> + ?Sized> Parseable<TcfBuffer<&T>> for Tcf {
fn parse(buf: &TcfBuffer<&T>) -> Result<Self, DecodeError> {
Ok(Self {
install: buf.install(),
Expand Down
4 changes: 1 addition & 3 deletions src/tc/actions/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ impl Emitable for TcMirror {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<TcMirrorBuffer<&'a T>>
for TcMirror
{
impl<T: AsRef<[u8]> + ?Sized> Parseable<TcMirrorBuffer<&T>> for TcMirror {
fn parse(buf: &TcMirrorBuffer<&T>) -> Result<Self, DecodeError> {
Ok(Self {
generic: TcActionGeneric::parse(&TcActionGenericBuffer::new(
Expand Down
2 changes: 1 addition & 1 deletion src/tc/actions/nat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Emitable for TcNat {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<TcNatBuffer<&'a T>> for TcNat {
impl<T: AsRef<[u8]> + ?Sized> Parseable<TcNatBuffer<&T>> for TcNat {
fn parse(buf: &TcNatBuffer<&T>) -> Result<Self, DecodeError> {
Ok(Self {
generic: TcActionGeneric::parse(&TcActionGenericBuffer::new(
Expand Down
4 changes: 1 addition & 3 deletions src/tc/actions/tunnel_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ impl Emitable for TcTunnelKey {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<TcTunnelKeyBuffer<&'a T>>
for TcTunnelKey
{
impl<T: AsRef<[u8]> + ?Sized> Parseable<TcTunnelKeyBuffer<&T>> for TcTunnelKey {
fn parse(buf: &TcTunnelKeyBuffer<&T>) -> Result<Self, DecodeError> {
Ok(Self {
generic: TcActionGeneric::parse(&TcActionGenericBuffer::new(
Expand Down
7 changes: 3 additions & 4 deletions src/tc/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Nla for TcAttribute {
Self::HwOffload(_) => 1,
Self::Stats2(ref v) => v.as_slice().buffer_len(),
Self::Stats(ref v) => v.buffer_len(),
Self::Kind(ref string) => string.as_bytes().len() + 1,
Self::Kind(ref string) => string.len() + 1,
Self::Options(ref opt) => opt.as_slice().buffer_len(),
Self::DumpInvisible(_) => 0, // The existence of NLA means true
Self::Other(ref attr) => attr.value_len(),
Expand All @@ -78,9 +78,8 @@ impl Nla for TcAttribute {
Self::Stats2(ref stats) => stats.as_slice().emit(buffer),
Self::Stats(ref stats) => stats.emit(buffer),
Self::Kind(ref string) => {
buffer[..string.as_bytes().len()]
.copy_from_slice(string.as_bytes());
buffer[string.as_bytes().len()] = 0;
buffer[..string.len()].copy_from_slice(string.as_bytes());
buffer[string.len()] = 0;
}
Self::Options(ref opt) => opt.as_slice().emit(buffer),
Self::DumpInvisible(_) => (),
Expand Down
Loading