From 849c5da2f03cf8304f240fd1aaec048e99eb4a7a Mon Sep 17 00:00:00 2001 From: shellrow Date: Tue, 8 Jul 2025 23:31:19 +0900 Subject: [PATCH 1/2] correct typo in method name --- examples/async_icmp_socket.rs | 2 +- examples/icmp_ping.rs | 4 ++-- examples/icmp_socket.rs | 4 ++-- examples/tcp_ping.rs | 2 +- examples/udp_ping.rs | 2 +- nex-packet/src/builder/icmp.rs | 2 +- nex-packet/src/builder/icmpv6.rs | 2 +- nex-packet/src/builder/tcp.rs | 2 +- nex-packet/src/builder/udp.rs | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/async_icmp_socket.rs b/examples/async_icmp_socket.rs index ac55cd4..9dd4503 100644 --- a/examples/async_icmp_socket.rs +++ b/examples/async_icmp_socket.rs @@ -66,7 +66,7 @@ async fn main() -> std::io::Result<()> { .icmp_code(icmp::echo_request::IcmpCodes::NoCode) .echo_fields(id, seq) .payload(Bytes::from_static(b"ping")) - .culculate_checksum() + .calculate_checksum() .to_bytes(); let target = SocketAddr::new(IpAddr::V4(addr), 0); let _ = socket.send_to(&pkt, target).await; diff --git a/examples/icmp_ping.rs b/examples/icmp_ping.rs index e8b5707..3ff0f7c 100644 --- a/examples/icmp_ping.rs +++ b/examples/icmp_ping.rs @@ -70,7 +70,7 @@ fn main() { .icmp_code(icmp::echo_request::IcmpCodes::NoCode) .echo_fields(0x1234, 0x1) .payload(Bytes::from_static(b"hello")) - .culculate_checksum() + .calculate_checksum() .build() .to_bytes(), (IpAddr::V6(src), IpAddr::V6(dst)) => Icmpv6PacketBuilder::new(src, dst) @@ -78,7 +78,7 @@ fn main() { .icmpv6_code(icmpv6::echo_request::Icmpv6Codes::NoCode) .echo_fields(0x1234, 0x1) .payload(Bytes::from_static(b"hello")) - .culculate_checksum() + .calculate_checksum() .build() .to_bytes(), _ => panic!("Source and destination IP version mismatch"), diff --git a/examples/icmp_socket.rs b/examples/icmp_socket.rs index 486e197..2dab536 100644 --- a/examples/icmp_socket.rs +++ b/examples/icmp_socket.rs @@ -37,14 +37,14 @@ fn main() -> std::io::Result<()> { .icmp_code(icmp::echo_request::IcmpCodes::NoCode) .echo_fields(0x1234, 1) .payload(Bytes::from_static(b"hello")) - .culculate_checksum() + .calculate_checksum() .to_bytes(), (IpAddr::V6(src), IpAddr::V6(dst)) => Icmpv6PacketBuilder::new(src, dst) .icmpv6_type(nex_packet::icmpv6::Icmpv6Type::EchoRequest) .icmpv6_code(icmpv6::echo_request::Icmpv6Codes::NoCode) .echo_fields(0x1234, 1) .payload(Bytes::from_static(b"hello")) - .culculate_checksum() + .calculate_checksum() .to_bytes(), _ => unreachable!(), }; diff --git a/examples/tcp_ping.rs b/examples/tcp_ping.rs index 67d4c24..7db328b 100644 --- a/examples/tcp_ping.rs +++ b/examples/tcp_ping.rs @@ -118,7 +118,7 @@ fn main() { TcpOptionPacket::nop(), TcpOptionPacket::wscale(7), ]) - .culculate_checksum(&src_ip, &dst_ip) + .calculate_checksum(&src_ip, &dst_ip) .build(); let ip_packet: Bytes; diff --git a/examples/udp_ping.rs b/examples/udp_ping.rs index 27f75cb..1ec5912 100644 --- a/examples/udp_ping.rs +++ b/examples/udp_ping.rs @@ -68,7 +68,7 @@ fn main() { let udp_packet = UdpPacketBuilder::new() .source(SRC_PORT) .destination(DST_PORT) - .culculate_checksum(&src_ip, &target_ip) + .calculate_checksum(&src_ip, &target_ip) .build(); let ip_packet: Bytes = match (src_ip, target_ip) { diff --git a/nex-packet/src/builder/icmp.rs b/nex-packet/src/builder/icmp.rs index 7e18991..02af80b 100644 --- a/nex-packet/src/builder/icmp.rs +++ b/nex-packet/src/builder/icmp.rs @@ -59,7 +59,7 @@ impl IcmpPacketBuilder { self } - pub fn culculate_checksum(mut self) -> Self { + pub fn calculate_checksum(mut self) -> Self { // Calculate the checksum and set it in the header self.packet.header.checksum = checksum(&self.packet); self diff --git a/nex-packet/src/builder/icmpv6.rs b/nex-packet/src/builder/icmpv6.rs index a62f0ca..cf2c5f8 100644 --- a/nex-packet/src/builder/icmpv6.rs +++ b/nex-packet/src/builder/icmpv6.rs @@ -58,7 +58,7 @@ impl Icmpv6PacketBuilder { self } - pub fn culculate_checksum(mut self) -> Self { + pub fn calculate_checksum(mut self) -> Self { // Calculate the checksum and set it in the header self.packet.header.checksum = checksum(&self.packet, &self.source, &self.destination); self diff --git a/nex-packet/src/builder/tcp.rs b/nex-packet/src/builder/tcp.rs index 639e2c9..3392100 100644 --- a/nex-packet/src/builder/tcp.rs +++ b/nex-packet/src/builder/tcp.rs @@ -83,7 +83,7 @@ impl TcpPacketBuilder { self } - pub fn culculate_checksum(mut self, src_ip: &IpAddr, dst_ip: &IpAddr) -> Self { + pub fn calculate_checksum(mut self, src_ip: &IpAddr, dst_ip: &IpAddr) -> Self { // Calculate the checksum and set it in the header self.packet.header.checksum = crate::tcp::checksum(&self.packet, src_ip, dst_ip); self diff --git a/nex-packet/src/builder/udp.rs b/nex-packet/src/builder/udp.rs index dddb1e2..1db3e81 100644 --- a/nex-packet/src/builder/udp.rs +++ b/nex-packet/src/builder/udp.rs @@ -50,7 +50,7 @@ impl UdpPacketBuilder { self } - pub fn culculate_checksum(mut self, src_ip: &IpAddr, dst_ip: &IpAddr) -> Self { + pub fn calculate_checksum(mut self, src_ip: &IpAddr, dst_ip: &IpAddr) -> Self { // Calculate the checksum and set it in the header self.packet.header.checksum = crate::udp::checksum(&self.packet, src_ip, dst_ip); self From ce2c8dc25dcaf39d849f4147ec5ae04b6ba9fe33 Mon Sep 17 00:00:00 2001 From: shellrow Date: Wed, 9 Jul 2025 00:14:56 +0900 Subject: [PATCH 2/2] Add missing serde support --- nex-packet/Cargo.toml | 2 +- nex-packet/src/builder/dhcp.rs | 6 +++--- nex-packet/src/dhcp.rs | 20 ++++++++++++++------ nex-packet/src/dns.rs | 7 +++++++ nex-packet/src/frame.rs | 3 ++- nex-packet/src/icmp.rs | 1 + nex-packet/src/ipv6.rs | 4 ++++ nex-packet/src/tcp.rs | 1 + 8 files changed, 33 insertions(+), 11 deletions(-) diff --git a/nex-packet/Cargo.toml b/nex-packet/Cargo.toml index 5a141e1..1f989b0 100644 --- a/nex-packet/Cargo.toml +++ b/nex-packet/Cargo.toml @@ -17,4 +17,4 @@ serde = { workspace = true, features = ["derive"], optional = true } rand = { workspace = true } [features] -serde = ["dep:serde", "nex-core/serde"] +serde = ["dep:serde", "nex-core/serde", "bytes/serde"] diff --git a/nex-packet/src/builder/dhcp.rs b/nex-packet/src/builder/dhcp.rs index a71910e..5152c8b 100644 --- a/nex-packet/src/builder/dhcp.rs +++ b/nex-packet/src/builder/dhcp.rs @@ -27,9 +27,9 @@ impl DhcpPacketBuilder { siaddr: Ipv4Addr::UNSPECIFIED, giaddr: Ipv4Addr::UNSPECIFIED, chaddr, - chaddr_pad: [0u8; 10], - sname: [0u8; 64], - file: [0u8; 128], + chaddr_pad: [0u8; 10].to_vec(), + sname: [0u8; 64].to_vec(), + file: [0u8; 128].to_vec(), }; Self { packet: DhcpPacket { diff --git a/nex-packet/src/dhcp.rs b/nex-packet/src/dhcp.rs index ea004ef..d9f38e2 100644 --- a/nex-packet/src/dhcp.rs +++ b/nex-packet/src/dhcp.rs @@ -4,6 +4,9 @@ use std::net::Ipv4Addr; use crate::packet::Packet; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /// Minimum size of an DHCP packet. /// Options field is not included in this size. pub const DHCP_MIN_PACKET_SIZE: usize = 236; @@ -11,6 +14,7 @@ pub const DHCP_MIN_PACKET_SIZE: usize = 236; // DHCP Operation Codes #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum DhcpOperation { Request = 1, Reply = 2, @@ -38,6 +42,7 @@ impl DhcpOperation { // DHCP Hardware Types #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum DhcpHardwareType { Ethernet = 1, ExperimentalEthernet = 2, @@ -255,9 +260,12 @@ pub struct DhcpHeader { pub siaddr: Ipv4Addr, pub giaddr: Ipv4Addr, pub chaddr: MacAddr, - pub chaddr_pad: [u8; 10], - pub sname: [u8; 64], - pub file: [u8; 128], + /// Client hardware address padding (must be exactly 10 bytes during parsing/building) + pub chaddr_pad: Vec, + /// Optional server host name (must be exactly 64 bytes during parsing/building) + pub sname: Vec, + /// Boot file name (must be exactly 128 bytes during parsing/building) + pub file: Vec, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -313,9 +321,9 @@ impl Packet for DhcpPacket { siaddr, giaddr, chaddr, - chaddr_pad, - sname, - file, + chaddr_pad: chaddr_pad.to_vec(), + sname: sname.to_vec(), + file: file.to_vec(), }; Some(Self { diff --git a/nex-packet/src/dns.rs b/nex-packet/src/dns.rs index fad4787..c827010 100644 --- a/nex-packet/src/dns.rs +++ b/nex-packet/src/dns.rs @@ -4,11 +4,15 @@ use bytes::{BufMut, Bytes, BytesMut}; use nex_core::bitfield::{u1, u16be, u32be}; use crate::packet::Packet; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /// Represents an DNS operation. /// These identifiers correspond to DNS resource record classes. /// #[repr(u16)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum DnsClass { IN = 1, // Internet CS = 2, // CSNET (Obsolete) @@ -52,6 +56,7 @@ impl DnsClass { #[allow(non_camel_case_types)] #[repr(u16)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum DnsType { A = 1, NS = 2, @@ -435,6 +440,7 @@ impl DnsType { /// Represents an DNS operation code. /// #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum OpCode { Query, InverseQuery, @@ -488,6 +494,7 @@ impl OpCode { /// Represents an DNS return code. /// #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum RetCode { NoError, FormErr, diff --git a/nex-packet/src/frame.rs b/nex-packet/src/frame.rs index 1e644d9..334635e 100644 --- a/nex-packet/src/frame.rs +++ b/nex-packet/src/frame.rs @@ -3,7 +3,8 @@ use nex_core::mac::MacAddr; use crate::{arp::{ArpHeader, ArpPacket}, ethernet::{EtherType, EthernetHeader, EthernetPacket}, icmp::{IcmpHeader, IcmpPacket}, icmpv6::{Icmpv6Header, Icmpv6Packet}, ip::IpNextProtocol, ipv4::{Ipv4Header, Ipv4Packet}, ipv6::{Ipv6Header, Ipv6Packet}, packet::Packet, tcp::{TcpHeader, TcpPacket}, udp::{UdpHeader, UdpPacket}}; - +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] diff --git a/nex-packet/src/icmp.rs b/nex-packet/src/icmp.rs index 1f8972f..84ab883 100644 --- a/nex-packet/src/icmp.rs +++ b/nex-packet/src/icmp.rs @@ -164,6 +164,7 @@ impl IcmpCode { } #[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct IcmpHeader { pub icmp_type: IcmpType, pub icmp_code: IcmpCode, diff --git a/nex-packet/src/ipv6.rs b/nex-packet/src/ipv6.rs index 58fa680..43fb3bd 100644 --- a/nex-packet/src/ipv6.rs +++ b/nex-packet/src/ipv6.rs @@ -3,9 +3,13 @@ use bytes::{Bytes, BytesMut, BufMut}; use crate::packet::Packet; use crate::ip::IpNextProtocol; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + pub const IPV6_HEADER_LEN: usize = 40; #[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Ipv6Header { pub version: u8, // 4 bits pub traffic_class: u8, // 8 bits diff --git a/nex-packet/src/tcp.rs b/nex-packet/src/tcp.rs index 5ba8592..2d692b9 100644 --- a/nex-packet/src/tcp.rs +++ b/nex-packet/src/tcp.rs @@ -311,6 +311,7 @@ impl TcpOptionHeader { /// A TCP option. #[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TcpOptionPacket { kind: TcpOptionKind, length: Option,