diff --git a/Cargo.toml b/Cargo.toml index 7422655..b8dbd8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,16 +10,16 @@ members = [ ] [workspace.package] -version = "0.23.2" -edition = "2021" +version = "0.24.0" +edition = "2024" authors = ["shellrow "] [workspace.dependencies] -nex-core = { version = "0.23.2", path = "nex-core" } -nex-datalink = { version = "0.23.2", path = "nex-datalink" } -nex-packet = { version = "0.23.2", path = "nex-packet" } -nex-sys = { version = "0.23.2", path = "nex-sys" } -nex-socket = { version = "0.23.2", path = "nex-socket" } +nex-core = { version = "0.24.0", path = "nex-core" } +nex-datalink = { version = "0.24.0", path = "nex-datalink" } +nex-packet = { version = "0.24.0", path = "nex-packet" } +nex-sys = { version = "0.24.0", path = "nex-sys" } +nex-socket = { version = "0.24.0", path = "nex-socket" } serde = { version = "1" } libc = "0.2" netdev = { version = "0.39" } diff --git a/examples/arp.rs b/examples/arp.rs index 64df91a..2cc1b84 100644 --- a/examples/arp.rs +++ b/examples/arp.rs @@ -8,7 +8,7 @@ use nex::datalink; use nex::datalink::Channel::Ethernet; -use nex::net::interface::{get_interfaces, Interface}; +use nex::net::interface::{Interface, get_interfaces}; use nex::net::mac::MacAddr; use nex::packet::builder::ethernet::EthernetPacketBuilder; use nex::packet::ethernet::EtherType; diff --git a/examples/async_datalink.rs b/examples/async_datalink.rs index 0b60f40..77805c9 100644 --- a/examples/async_datalink.rs +++ b/examples/async_datalink.rs @@ -13,8 +13,8 @@ use nex::packet::frame::{Frame, ParseOption}; use nex::packet::icmp::IcmpType; use nex::packet::icmpv6::Icmpv6Type; use nex_core::interface::Interface; -use nex_datalink::async_io::{async_channel, AsyncChannel}; use nex_datalink::Config; +use nex_datalink::async_io::{AsyncChannel, async_channel}; use nex_packet::ip::IpNextProtocol; use nex_packet::ipv4::Ipv4Flags; use nex_packet::packet::Packet; diff --git a/examples/async_dump.rs b/examples/async_dump.rs index ff58247..7084615 100644 --- a/examples/async_dump.rs +++ b/examples/async_dump.rs @@ -14,8 +14,8 @@ use nex::packet::ipv6::Ipv6Packet; use nex::packet::packet::Packet; use nex::packet::tcp::TcpPacket; use nex::packet::udp::UdpPacket; -use nex_datalink::async_io::{async_channel, AsyncChannel}; use nex_datalink::Config; +use nex_datalink::async_io::{AsyncChannel, async_channel}; use nex_packet::ethernet::EthernetHeader; use nex_packet::{icmp, icmpv6}; use std::net::IpAddr; @@ -96,43 +96,52 @@ fn handle_ethernet_frame(ethernet: EthernetPacket) { } fn handle_arp_packet(packet: Bytes) { - if let Some(arp) = ArpPacket::from_bytes(packet) { - println!( - "ARP packet: {}({}) > {}({}); operation: {:?}", - arp.header.sender_hw_addr, - arp.header.sender_proto_addr, - arp.header.target_hw_addr, - arp.header.target_proto_addr, - arp.header.operation - ); - } else { - println!("Malformed ARP Packet"); + match ArpPacket::from_bytes(packet) { + Some(arp) => { + println!( + "ARP packet: {}({}) > {}({}); operation: {:?}", + arp.header.sender_hw_addr, + arp.header.sender_proto_addr, + arp.header.target_hw_addr, + arp.header.target_proto_addr, + arp.header.operation + ); + } + _ => { + println!("Malformed ARP Packet"); + } } } fn handle_ipv4_packet(packet: Bytes) { - if let Some(ipv4) = Ipv4Packet::from_bytes(packet) { - handle_transport_protocol( - IpAddr::V4(ipv4.header.source), - IpAddr::V4(ipv4.header.destination), - ipv4.header.next_level_protocol, - ipv4.payload, - ); - } else { - println!("Malformed IPv4 Packet"); + match Ipv4Packet::from_bytes(packet) { + Some(ipv4) => { + handle_transport_protocol( + IpAddr::V4(ipv4.header.source), + IpAddr::V4(ipv4.header.destination), + ipv4.header.next_level_protocol, + ipv4.payload, + ); + } + _ => { + println!("Malformed IPv4 Packet"); + } } } fn handle_ipv6_packet(packet: Bytes) { - if let Some(ipv6) = Ipv6Packet::from_bytes(packet) { - handle_transport_protocol( - IpAddr::V6(ipv6.header.source), - IpAddr::V6(ipv6.header.destination), - ipv6.header.next_header, - ipv6.payload, - ); - } else { - println!("Malformed IPv6 Packet"); + match Ipv6Packet::from_bytes(packet) { + Some(ipv6) => { + handle_transport_protocol( + IpAddr::V6(ipv6.header.source), + IpAddr::V6(ipv6.header.destination), + ipv6.header.next_header, + ipv6.payload, + ); + } + _ => { + println!("Malformed IPv6 Packet"); + } } } @@ -162,17 +171,20 @@ fn handle_transport_protocol( } fn handle_tcp_packet(source: IpAddr, destination: IpAddr, packet: Bytes) { - if let Some(tcp) = TcpPacket::from_bytes(packet) { - println!( - "TCP Packet: {}:{} > {}:{}; length: {}", - source, - tcp.header.source, - destination, - tcp.header.destination, - tcp.total_len(), - ); - } else { - println!("Malformed TCP Packet"); + match TcpPacket::from_bytes(packet) { + Some(tcp) => { + println!( + "TCP Packet: {}:{} > {}:{}; length: {}", + source, + tcp.header.source, + destination, + tcp.header.destination, + tcp.total_len(), + ); + } + _ => { + println!("Malformed TCP Packet"); + } } } diff --git a/examples/async_icmp_socket.rs b/examples/async_icmp_socket.rs index 93fa3cc..fb55e85 100644 --- a/examples/async_icmp_socket.rs +++ b/examples/async_icmp_socket.rs @@ -4,14 +4,14 @@ //! Example: async_icmp_socket 192.168.1 use bytes::Bytes; -use nex::net::interface::{get_interfaces, Interface}; +use nex::net::interface::{Interface, get_interfaces}; use nex_packet::builder::icmp::IcmpPacketBuilder; use nex_packet::icmp::echo_reply::EchoReplyPacket; use nex_packet::icmp::{self, IcmpPacket, IcmpType}; use nex_packet::ipv4::Ipv4Packet; use nex_packet::packet::Packet; use nex_socket::icmp::{AsyncIcmpSocket, IcmpConfig, IcmpKind}; -use rand::{thread_rng, Rng}; +use rand::{Rng, thread_rng}; use std::collections::HashMap; use std::env; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; @@ -87,7 +87,7 @@ async fn main() -> std::io::Result<()> { let mut handles = Vec::new(); for i in 1u8..=254 { let addr = Ipv4Addr::new(parts[0], parts[1], parts[2], i); - let id: u16 = thread_rng().gen(); + let id: u16 = thread_rng().r#gen(); let seq: u16 = 1; let socket = socket.clone(); let replies = replies.clone(); diff --git a/examples/dump.rs b/examples/dump.rs index 8075bc1..1169d44 100644 --- a/examples/dump.rs +++ b/examples/dump.rs @@ -119,43 +119,52 @@ fn handle_ethernet_frame(ethernet: EthernetPacket) { } fn handle_arp_packet(packet: Bytes) { - if let Some(arp) = ArpPacket::from_bytes(packet) { - println!( - "ARP packet: {}({}) > {}({}); operation: {:?}", - arp.header.sender_hw_addr, - arp.header.sender_proto_addr, - arp.header.target_hw_addr, - arp.header.target_proto_addr, - arp.header.operation - ); - } else { - println!("Malformed ARP Packet"); + match ArpPacket::from_bytes(packet) { + Some(arp) => { + println!( + "ARP packet: {}({}) > {}({}); operation: {:?}", + arp.header.sender_hw_addr, + arp.header.sender_proto_addr, + arp.header.target_hw_addr, + arp.header.target_proto_addr, + arp.header.operation + ); + } + _ => { + println!("Malformed ARP Packet"); + } } } fn handle_ipv4_packet(packet: Bytes) { - if let Some(ipv4) = Ipv4Packet::from_bytes(packet) { - handle_transport_protocol( - IpAddr::V4(ipv4.header.source), - IpAddr::V4(ipv4.header.destination), - ipv4.header.next_level_protocol, - ipv4.payload, - ); - } else { - println!("Malformed IPv4 Packet"); + match Ipv4Packet::from_bytes(packet) { + Some(ipv4) => { + handle_transport_protocol( + IpAddr::V4(ipv4.header.source), + IpAddr::V4(ipv4.header.destination), + ipv4.header.next_level_protocol, + ipv4.payload, + ); + } + _ => { + println!("Malformed IPv4 Packet"); + } } } fn handle_ipv6_packet(packet: Bytes) { - if let Some(ipv6) = Ipv6Packet::from_bytes(packet) { - handle_transport_protocol( - IpAddr::V6(ipv6.header.source), - IpAddr::V6(ipv6.header.destination), - ipv6.header.next_header, - ipv6.payload, - ); - } else { - println!("Malformed IPv6 Packet"); + match Ipv6Packet::from_bytes(packet) { + Some(ipv6) => { + handle_transport_protocol( + IpAddr::V6(ipv6.header.source), + IpAddr::V6(ipv6.header.destination), + ipv6.header.next_header, + ipv6.payload, + ); + } + _ => { + println!("Malformed IPv6 Packet"); + } } } @@ -185,17 +194,20 @@ fn handle_transport_protocol( } fn handle_tcp_packet(source: IpAddr, destination: IpAddr, packet: Bytes) { - if let Some(tcp) = TcpPacket::from_bytes(packet) { - println!( - "TCP Packet: {}:{} > {}:{}; length: {}", - source, - tcp.header.source, - destination, - tcp.header.destination, - tcp.total_len(), - ); - } else { - println!("Malformed TCP Packet"); + match TcpPacket::from_bytes(packet) { + Some(tcp) => { + println!( + "TCP Packet: {}:{} > {}:{}; length: {}", + source, + tcp.header.source, + destination, + tcp.header.destination, + tcp.total_len(), + ); + } + _ => { + println!("Malformed TCP Packet"); + } } } diff --git a/examples/icmp_socket.rs b/examples/icmp_socket.rs index a666057..375ff54 100644 --- a/examples/icmp_socket.rs +++ b/examples/icmp_socket.rs @@ -3,7 +3,7 @@ //! Usage: icmp_socket use bytes::Bytes; -use nex::net::interface::{get_interfaces, Interface}; +use nex::net::interface::{Interface, get_interfaces}; use nex_packet::builder::icmp::IcmpPacketBuilder; use nex_packet::builder::icmpv6::Icmpv6PacketBuilder; use nex_packet::icmp::IcmpPacket; diff --git a/examples/mutable_chaining.rs b/examples/mutable_chaining.rs index db53b4b..3fb826e 100644 --- a/examples/mutable_chaining.rs +++ b/examples/mutable_chaining.rs @@ -2,12 +2,12 @@ use nex::net::mac::MacAddr; use nex::packet::ethernet::{ - EtherType, EthernetPacket, MutableEthernetPacket, ETHERNET_HEADER_LEN, + ETHERNET_HEADER_LEN, EtherType, EthernetPacket, MutableEthernetPacket, }; use nex::packet::ip::IpNextProtocol; -use nex::packet::ipv4::{self, Ipv4Packet, MutableIpv4Packet, IPV4_HEADER_LEN}; +use nex::packet::ipv4::{self, IPV4_HEADER_LEN, Ipv4Packet, MutableIpv4Packet}; use nex::packet::packet::{MutablePacket, Packet}; -use nex::packet::udp::{self, MutableUdpPacket, UdpPacket, UDP_HEADER_LEN}; +use nex::packet::udp::{self, MutableUdpPacket, UDP_HEADER_LEN, UdpPacket}; use std::net::Ipv4Addr; fn main() { diff --git a/examples/ndp.rs b/examples/ndp.rs index bc376d2..060bd24 100644 --- a/examples/ndp.rs +++ b/examples/ndp.rs @@ -8,7 +8,7 @@ use nex::datalink; use nex::datalink::Channel::Ethernet; -use nex::net::interface::{get_interfaces, Interface}; +use nex::net::interface::{Interface, get_interfaces}; use nex::net::mac::MacAddr; use nex::packet::builder::ethernet::EthernetPacketBuilder; use nex::packet::builder::ipv6::Ipv6PacketBuilder; diff --git a/nex-core/src/ip.rs b/nex-core/src/ip.rs index 0aecf41..42893ff 100644 --- a/nex-core/src/ip.rs +++ b/nex-core/src/ip.rs @@ -56,7 +56,7 @@ pub fn is_global_ipv6(ipv6_addr: &Ipv6Addr) -> bool { // Drone Remote ID Protocol Entity Tags (DETs) Prefix (`2001:30::/28`)` || matches!(ipv6_addr.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x3F) )) - // 6to4 (`2002::/16`) – it's not explicitly documented as globally reachable, + // 6to4 (`2002::/16`) - it's not explicitly documented as globally reachable, // IANA says N/A. || matches!(ipv6_addr.segments(), [0x2002, _, _, _, _, _, _, _]) || is_documentation_ipv6(ipv6_addr) diff --git a/nex-datalink/src/async_io/bpf.rs b/nex-datalink/src/async_io/bpf.rs index bf351ad..c06039b 100644 --- a/nex-datalink/src/async_io/bpf.rs +++ b/nex-datalink/src/async_io/bpf.rs @@ -1,8 +1,8 @@ //! Asynchronous raw datalink support for BSD BPF devices. +use crate::Config; use crate::async_io::{AsyncChannel, AsyncRawSender}; use crate::bindings::bpf; -use crate::Config; use futures_core::stream::Stream; use nex_core::interface::Interface; use nex_sys; diff --git a/nex-datalink/src/async_io/wpcap.rs b/nex-datalink/src/async_io/wpcap.rs index 37ae0bd..465e5c5 100644 --- a/nex-datalink/src/async_io/wpcap.rs +++ b/nex-datalink/src/async_io/wpcap.rs @@ -1,8 +1,8 @@ //! Asynchronous raw datalink support for Windows using the Npcap / WinPcap library. +use crate::Config; use crate::async_io::{AsyncChannel, AsyncRawSender}; use crate::bindings::{bpf, windows}; -use crate::Config; use futures_core::stream::Stream; use nex_core::interface::Interface; use std::cmp; diff --git a/nex-datalink/src/bindings/bpf.rs b/nex-datalink/src/bindings/bpf.rs index 6f1fd73..dc53470 100644 --- a/nex-datalink/src/bindings/bpf.rs +++ b/nex-datalink/src/bindings/bpf.rs @@ -142,6 +142,6 @@ pub struct bpf_hdr { } #[cfg(not(windows))] -extern "C" { +unsafe extern "C" { pub fn ioctl(d: libc::c_int, request: libc::c_ulong, ...) -> libc::c_int; } diff --git a/nex-datalink/src/bindings/windows.rs b/nex-datalink/src/bindings/windows.rs index ef5f803..940ccf7 100644 --- a/nex-datalink/src/bindings/windows.rs +++ b/nex-datalink/src/bindings/windows.rs @@ -2,9 +2,9 @@ #![allow(non_snake_case)] #![allow(dead_code)] -use windows_sys::core::PCWSTR; use windows_sys::Win32::Foundation::{BOOLEAN, HANDLE}; use windows_sys::Win32::System::IO::OVERLAPPED; +use windows_sys::core::PCWSTR; #[repr(C)] pub struct _ADAPTER; @@ -51,7 +51,7 @@ pub fn to_npf_name(name: &str) -> String { #[link(name = "Packet")] #[allow(improper_ctypes)] -extern "C" { +unsafe extern "C" { // from Packet32.h pub fn PacketSendPacket(AdapterObject: LPADAPTER, pPacket: LPPACKET, Sync: BOOLEAN) -> BOOLEAN; pub fn PacketReceivePacket( diff --git a/nex-packet/src/arp.rs b/nex-packet/src/arp.rs index e3bf32a..ec0126c 100644 --- a/nex-packet/src/arp.rs +++ b/nex-packet/src/arp.rs @@ -1,7 +1,7 @@ //! ARP packet abstraction. use crate::{ - ethernet::{EtherType, ETHERNET_HEADER_LEN}, + ethernet::{ETHERNET_HEADER_LEN, EtherType}, packet::{MutablePacket, Packet}, }; diff --git a/nex-packet/src/builder/ndp.rs b/nex-packet/src/builder/ndp.rs index 24fce2f..d40a072 100644 --- a/nex-packet/src/builder/ndp.rs +++ b/nex-packet/src/builder/ndp.rs @@ -1,5 +1,5 @@ use crate::icmpv6::ndp::{NdpOptionPacket, NdpOptionTypes, NeighborSolicitPacket}; -use crate::icmpv6::{self, checksum, Icmpv6Header, Icmpv6Packet, Icmpv6Type}; +use crate::icmpv6::{self, Icmpv6Header, Icmpv6Packet, Icmpv6Type, checksum}; use crate::packet::Packet; use bytes::Bytes; use nex_core::mac::MacAddr; diff --git a/nex-packet/src/builder/udp.rs b/nex-packet/src/builder/udp.rs index 9b219f4..eed3c8b 100644 --- a/nex-packet/src/builder/udp.rs +++ b/nex-packet/src/builder/udp.rs @@ -1,7 +1,7 @@ use std::net::IpAddr; use crate::packet::Packet; -use crate::udp::{UdpHeader, UdpPacket, UDP_HEADER_LEN}; +use crate::udp::{UDP_HEADER_LEN, UdpHeader, UdpPacket}; use bytes::Bytes; /// Builder for constructing UDP packets diff --git a/nex-packet/src/dns.rs b/nex-packet/src/dns.rs index 33d214f..ae1d3fd 100644 --- a/nex-packet/src/dns.rs +++ b/nex-packet/src/dns.rs @@ -1056,10 +1056,13 @@ impl Packet for DnsPacket { fn parse_responses(count: usize, buf: &mut &[u8]) -> Option> { let mut packets = Vec::with_capacity(count); for _ in 0..count { - if let Some(pkt) = DnsResponsePacket::from_buf_mut(buf) { - packets.push(pkt); - } else { - break; + match DnsResponsePacket::from_buf_mut(buf) { + Some(pkt) => { + packets.push(pkt); + } + _ => { + break; + } } } Some(packets) diff --git a/nex-packet/src/gre.rs b/nex-packet/src/gre.rs index 0bab76a..2100a7b 100644 --- a/nex-packet/src/gre.rs +++ b/nex-packet/src/gre.rs @@ -2,7 +2,7 @@ use crate::packet::{GenericMutablePacket, Packet}; use bytes::{Buf, Bytes}; -use nex_core::bitfield::{u1, u16be, u3, u32be, u5}; +use nex_core::bitfield::{u1, u3, u5, u16be, u32be}; /// GRE (Generic Routing Encapsulation) Packet. /// diff --git a/nex-packet/src/icmpv6.rs b/nex-packet/src/icmpv6.rs index da71766..bf911c5 100644 --- a/nex-packet/src/icmpv6.rs +++ b/nex-packet/src/icmpv6.rs @@ -611,7 +611,7 @@ pub mod ndp { use bytes::Bytes; use nex_core::bitfield::{self, u24be, u32be}; - use crate::icmpv6::{Icmpv6Code, Icmpv6Header, Icmpv6Packet, Icmpv6Type, ICMPV6_HEADER_LEN}; + use crate::icmpv6::{ICMPV6_HEADER_LEN, Icmpv6Code, Icmpv6Header, Icmpv6Packet, Icmpv6Type}; use crate::packet::Packet; use std::net::Ipv6Addr; @@ -647,15 +647,15 @@ pub mod ndp { } } - /// Neighbor Discovery Option Types [RFC 4861 § 4.6] + /// Neighbor Discovery Option Types [RFC 4861 Section 4.6] /// - /// [RFC 4861 § 4.6]: https://tools.ietf.org/html/rfc4861#section-4.6 + /// [RFC 4861 Section 4.6]: https://tools.ietf.org/html/rfc4861#section-4.6 #[allow(non_snake_case)] #[allow(non_upper_case_globals)] pub mod NdpOptionTypes { use super::NdpOptionType; - /// Source Link-Layer Address Option [RFC 4861 § 4.6.1] + /// Source Link-Layer Address Option [RFC 4861 Section 4.6.1] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -663,10 +663,10 @@ pub mod ndp { /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// ``` /// - /// [RFC 4861 § 4.6.1]: https://tools.ietf.org/html/rfc4861#section-4.6.1 + /// [RFC 4861 Section 4.6.1]: https://tools.ietf.org/html/rfc4861#section-4.6.1 pub const SourceLLAddr: NdpOptionType = NdpOptionType(1); - /// Target Link-Layer Address Option [RFC 4861 § 4.6.1] + /// Target Link-Layer Address Option [RFC 4861 Section 4.6.1] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -674,10 +674,10 @@ pub mod ndp { /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// ``` /// - /// [RFC 4861 § 4.6.1]: https://tools.ietf.org/html/rfc4861#section-4.6.1 + /// [RFC 4861 Section 4.6.1]: https://tools.ietf.org/html/rfc4861#section-4.6.1 pub const TargetLLAddr: NdpOptionType = NdpOptionType(2); - /// Prefix Information Option [RFC 4861 § 4.6.2] + /// Prefix Information Option [RFC 4861 Section 4.6.2] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -699,10 +699,10 @@ pub mod ndp { /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// ``` /// - /// [RFC 4861 § 4.6.2]: https://tools.ietf.org/html/rfc4861#section-4.6.2 + /// [RFC 4861 Section 4.6.2]: https://tools.ietf.org/html/rfc4861#section-4.6.2 pub const PrefixInformation: NdpOptionType = NdpOptionType(3); - /// Redirected Header Option [RFC 4861 § 4.6.3] + /// Redirected Header Option [RFC 4861 Section 4.6.3] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -716,10 +716,10 @@ pub mod ndp { /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// ``` /// - /// [RFC 4861 § 4.6.3]: https://tools.ietf.org/html/rfc4861#section-4.6.3 + /// [RFC 4861 Section 4.6.3]: https://tools.ietf.org/html/rfc4861#section-4.6.3 pub const RedirectedHeader: NdpOptionType = NdpOptionType(4); - /// MTU Option [RFC 4861 § 4.6.4] + /// MTU Option [RFC 4861 Section 4.6.4] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -729,11 +729,11 @@ pub mod ndp { /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// ``` /// - /// [RFC 4861 § 4.6.4]: https://tools.ietf.org/html/rfc4861#section-4.6.4 + /// [RFC 4861 Section 4.6.4]: https://tools.ietf.org/html/rfc4861#section-4.6.4 pub const MTU: NdpOptionType = NdpOptionType(5); } - /// Neighbor Discovery Option [RFC 4861 § 4.6] + /// Neighbor Discovery Option [RFC 4861 Section 4.6] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -743,7 +743,7 @@ pub mod ndp { /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// ``` /// - /// [RFC 4861 § 4.6]: https://tools.ietf.org/html/rfc4861#section-4.6 + /// [RFC 4861 Section 4.6]: https://tools.ietf.org/html/rfc4861#section-4.6 #[derive(Clone, Debug, PartialEq, Eq)] pub struct NdpOptionPacket { pub option_type: NdpOptionType, @@ -817,17 +817,13 @@ pub mod ndp { pub fn option_payload_length(&self) -> usize { //let len = option.get_length(); let len = self.payload.len(); - if len > 0 { - ((len * 8) - 2) as usize - } else { - 0 - } + if len > 0 { ((len * 8) - 2) as usize } else { 0 } } } /// Calculate a length of a `NdpOption`'s payload. - /// Router Solicitation Message [RFC 4861 § 4.1] + /// Router Solicitation Message [RFC 4861 Section 4.1] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -838,7 +834,7 @@ pub mod ndp { /// | Options ... /// ``` /// - /// [RFC 4861 § 4.1]: https://tools.ietf.org/html/rfc4861#section-4.1 + /// [RFC 4861 Section 4.1]: https://tools.ietf.org/html/rfc4861#section-4.1 #[derive(Clone, Debug, PartialEq, Eq)] pub struct RouterSolicitPacket { pub header: Icmpv6Header, @@ -998,7 +994,7 @@ pub mod ndp { pub const OtherConf: u8 = 0b01000000; } - /// Router Advertisement Message Format [RFC 4861 § 4.2] + /// Router Advertisement Message Format [RFC 4861 Section 4.2] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -1014,7 +1010,7 @@ pub mod ndp { /// +-+-+-+-+-+-+-+-+-+-+-+- /// ``` /// - /// [RFC 4861 § 4.2]: https://tools.ietf.org/html/rfc4861#section-4.2 + /// [RFC 4861 Section 4.2]: https://tools.ietf.org/html/rfc4861#section-4.2 #[derive(Clone, Debug, PartialEq, Eq)] pub struct RouterAdvertPacket { pub header: Icmpv6Header, @@ -1190,7 +1186,7 @@ pub mod ndp { } } - /// Neighbor Solicitation Message Format [RFC 4861 § 4.3] + /// Neighbor Solicitation Message Format [RFC 4861 Section 4.3] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -1210,7 +1206,7 @@ pub mod ndp { /// +-+-+-+-+-+-+-+-+-+-+-+- /// ``` /// - /// [RFC 4861 § 4.3]: https://tools.ietf.org/html/rfc4861#section-4.3 + /// [RFC 4861 Section 4.3]: https://tools.ietf.org/html/rfc4861#section-4.3 #[derive(Clone, Debug, PartialEq, Eq)] pub struct NeighborSolicitPacket { pub header: Icmpv6Header, @@ -1397,7 +1393,7 @@ pub mod ndp { pub const Override: u8 = 0b00100000; } - /// Neighbor Advertisement Message Format [RFC 4861 § 4.4] + /// Neighbor Advertisement Message Format [RFC 4861 Section 4.4] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -1417,7 +1413,7 @@ pub mod ndp { /// +-+-+-+-+-+-+-+-+-+-+-+- /// ``` /// - /// [RFC 4861 § 4.4]: https://tools.ietf.org/html/rfc4861#section-4.4 + /// [RFC 4861 Section 4.4]: https://tools.ietf.org/html/rfc4861#section-4.4 #[derive(Clone, Debug, PartialEq, Eq)] pub struct NeighborAdvertPacket { pub header: Icmpv6Header, @@ -1606,7 +1602,7 @@ pub mod ndp { } } - /// Redirect Message Format [RFC 4861 § 4.5] + /// Redirect Message Format [RFC 4861 Section 4.5] /// /// ```text /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -1634,7 +1630,7 @@ pub mod ndp { /// +-+-+-+-+-+-+-+-+-+-+-+- /// ``` /// - /// [RFC 4861 § 4.5]: https://tools.ietf.org/html/rfc4861#section-4.5 + /// [RFC 4861 Section 4.5]: https://tools.ietf.org/html/rfc4861#section-4.5 #[derive(Clone, Debug, PartialEq, Eq)] pub struct RedirectPacket { pub header: Icmpv6Header, @@ -2446,7 +2442,7 @@ pub mod echo_reply { mod echo_tests { use super::*; use crate::icmpv6::{ - echo_reply::EchoReplyPacket, echo_request::EchoRequestPacket, Icmpv6Code, Icmpv6Type, + Icmpv6Code, Icmpv6Type, echo_reply::EchoReplyPacket, echo_request::EchoRequestPacket, }; #[test] diff --git a/nex-packet/src/packet.rs b/nex-packet/src/packet.rs index 665aa39..9bf1296 100644 --- a/nex-packet/src/packet.rs +++ b/nex-packet/src/packet.rs @@ -143,12 +143,13 @@ impl<'a, P: Packet> GenericMutablePacket<'a, P> { } fn lengths(&self) -> (usize, usize) { - if let Some(packet) = P::from_buf(self.packet()) { - let header_len = packet.header_len(); - let payload_len = packet.payload_len(); - (header_len, payload_len) - } else { - (self.buffer.len(), 0) + match P::from_buf(self.packet()) { + Some(packet) => { + let header_len = packet.header_len(); + let payload_len = packet.payload_len(); + (header_len, payload_len) + } + _ => (self.buffer.len(), 0), } } } diff --git a/nex-packet/src/tcp.rs b/nex-packet/src/tcp.rs index d0e62bc..12f160e 100644 --- a/nex-packet/src/tcp.rs +++ b/nex-packet/src/tcp.rs @@ -9,7 +9,7 @@ use std::net::Ipv6Addr; use std::net::{IpAddr, Ipv4Addr}; use bytes::{Buf, BufMut, Bytes, BytesMut}; -use nex_core::bitfield::{u16be, u32be, u4}; +use nex_core::bitfield::{u4, u16be, u32be}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -242,29 +242,29 @@ impl TcpOptionKind { #[allow(non_snake_case)] #[allow(non_upper_case_globals)] pub mod TcpFlags { - /// CWR – Congestion Window Reduced (CWR) flag is set by the sending + /// CWR - Congestion Window Reduced (CWR) flag is set by the sending /// host to indicate that it received a TCP segment with the ECE flag set /// and had responded in congestion control mechanism. pub const CWR: u8 = 0b10000000; - /// ECE – ECN-Echo has a dual role, depending on the value of the + /// ECE - ECN-Echo has a dual role, depending on the value of the /// SYN flag. It indicates: /// If the SYN flag is set (1), that the TCP peer is ECN capable. /// If the SYN flag is clear (0), that a packet with Congestion Experienced /// flag set (ECN=11) in IP header received during normal transmission. pub const ECE: u8 = 0b01000000; - /// URG – indicates that the Urgent pointer field is significant. + /// URG - indicates that the Urgent pointer field is significant. pub const URG: u8 = 0b00100000; - /// ACK – indicates that the Acknowledgment field is significant. + /// ACK - indicates that the Acknowledgment field is significant. /// All packets after the initial SYN packet sent by the client should have this flag set. pub const ACK: u8 = 0b00010000; - /// PSH – Push function. Asks to push the buffered data to the receiving application. + /// PSH - Push function. Asks to push the buffered data to the receiving application. pub const PSH: u8 = 0b00001000; - /// RST – Reset the connection. + /// RST - Reset the connection. pub const RST: u8 = 0b00000100; - /// SYN – Synchronize sequence numbers. Only the first packet sent from each end + /// SYN - Synchronize sequence numbers. Only the first packet sent from each end /// should have this flag set. pub const SYN: u8 = 0b00000010; - /// FIN – No more data from sender. + /// FIN - No more data from sender. pub const FIN: u8 = 0b00000001; } diff --git a/nex-packet/src/util.rs b/nex-packet/src/util.rs index d488143..32ed338 100644 --- a/nex-packet/src/util.rs +++ b/nex-packet/src/util.rs @@ -4,8 +4,8 @@ use crate::ip::IpNextProtocol; use nex_core::bitfield::u16be; use core::convert::TryInto; -use core::u16; use core::u8; +use core::u16; use std::net::{Ipv4Addr, Ipv6Addr}; /// Convert a value to a byte array. diff --git a/nex-socket/src/icmp/async_impl.rs b/nex-socket/src/icmp/async_impl.rs index e56bf06..a0d121e 100644 --- a/nex-socket/src/icmp/async_impl.rs +++ b/nex-socket/src/icmp/async_impl.rs @@ -1,5 +1,5 @@ -use crate::icmp::{IcmpConfig, IcmpKind, IcmpSocketType}; use crate::SocketFamily; +use crate::icmp::{IcmpConfig, IcmpKind, IcmpSocketType}; use socket2::{Domain, Protocol, Socket, Type as SockType}; use std::io; use std::net::{SocketAddr, UdpSocket as StdUdpSocket}; diff --git a/nex-socket/src/icmp/sync_impl.rs b/nex-socket/src/icmp/sync_impl.rs index 0c028c0..46e4105 100644 --- a/nex-socket/src/icmp/sync_impl.rs +++ b/nex-socket/src/icmp/sync_impl.rs @@ -1,5 +1,5 @@ -use crate::icmp::{IcmpConfig, IcmpKind, IcmpSocketType}; use crate::SocketFamily; +use crate::icmp::{IcmpConfig, IcmpKind, IcmpSocketType}; use socket2::{Domain, Protocol, Socket, Type as SockType}; use std::io; use std::net::{SocketAddr, UdpSocket}; diff --git a/nex-socket/src/tcp/sync_impl.rs b/nex-socket/src/tcp/sync_impl.rs index e648b72..3d88526 100644 --- a/nex-socket/src/tcp/sync_impl.rs +++ b/nex-socket/src/tcp/sync_impl.rs @@ -9,7 +9,7 @@ use crate::tcp::TcpConfig; use std::os::fd::AsRawFd; #[cfg(unix)] -use nix::poll::{poll, PollFd, PollFlags}; +use nix::poll::{PollFd, PollFlags, poll}; /// Low level synchronous TCP socket. #[derive(Debug)] @@ -164,7 +164,7 @@ impl TcpSocket { use std::mem::size_of; use std::os::windows::io::AsRawSocket; use windows_sys::Win32::Networking::WinSock::{ - getsockopt, WSAPoll, POLLWRNORM, SOCKET, SOCKET_ERROR, SOL_SOCKET, SO_ERROR, WSAPOLLFD, + POLLWRNORM, SO_ERROR, SOCKET, SOCKET_ERROR, SOL_SOCKET, WSAPOLLFD, WSAPoll, getsockopt, }; let sock = self.socket.as_raw_socket() as SOCKET; diff --git a/nex-sys/src/unix.rs b/nex-sys/src/unix.rs index a6c7e97..4aaab23 100644 --- a/nex-sys/src/unix.rs +++ b/nex-sys/src/unix.rs @@ -30,7 +30,9 @@ pub const AF_INET6: libc::c_int = libc::AF_INET6; pub use libc::{IFF_BROADCAST, IFF_LOOPBACK, IFF_MULTICAST, IFF_POINTOPOINT, IFF_UP}; pub unsafe fn close(sock: CSocket) { - let _ = libc::close(sock); + unsafe { + let _ = libc::close(sock); + } } fn ntohs(u: u16) -> u16 { @@ -116,7 +118,7 @@ pub unsafe fn sendto( addr: *const SockAddr, addrlen: SockLen, ) -> CouldFail { - libc::sendto(socket, buf, len, flags, addr, addrlen) + unsafe { libc::sendto(socket, buf, len, flags, addr, addrlen) } } pub unsafe fn recvfrom( @@ -127,7 +129,7 @@ pub unsafe fn recvfrom( addr: *mut SockAddr, addrlen: *mut SockLen, ) -> CouldFail { - libc::recvfrom(socket, buf, len, flags, addr, addrlen) + unsafe { libc::recvfrom(socket, buf, len, flags, addr, addrlen) } } #[inline] diff --git a/nex-sys/src/windows.rs b/nex-sys/src/windows.rs index 472aaa0..069e610 100644 --- a/nex-sys/src/windows.rs +++ b/nex-sys/src/windows.rs @@ -23,7 +23,9 @@ pub type InAddr = ws::IN_ADDR; pub type In6Addr = ws::IN6_ADDR; pub unsafe fn close(sock: CSocket) { - let _ = ws::closesocket(sock); + unsafe { + let _ = ws::closesocket(sock); + } } pub unsafe fn sendto( @@ -34,7 +36,7 @@ pub unsafe fn sendto( to: *const SockAddr, tolen: SockLen, ) -> CouldFail { - ws::sendto(socket, buf as *const u8, len, flags, to, tolen) + unsafe { ws::sendto(socket, buf as *const u8, len, flags, to, tolen) } } pub unsafe fn recvfrom( @@ -45,7 +47,7 @@ pub unsafe fn recvfrom( addr: *mut SockAddr, addrlen: *mut SockLen, ) -> CouldFail { - ws::recvfrom(socket, buf as *mut u8, len, flags, addr, addrlen) + unsafe { ws::recvfrom(socket, buf as *mut u8, len, flags, addr, addrlen) } } #[inline]