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 examples/async_icmp_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions examples/icmp_ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ 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)
.icmpv6_type(Icmpv6Type::EchoRequest)
.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"),
Expand Down
4 changes: 2 additions & 2 deletions examples/icmp_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
};
Expand Down
2 changes: 1 addition & 1 deletion examples/tcp_ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/udp_ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion nex-packet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
6 changes: 3 additions & 3 deletions nex-packet/src/builder/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion nex-packet/src/builder/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nex-packet/src/builder/icmpv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nex-packet/src/builder/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nex-packet/src/builder/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions nex-packet/src/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ 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;

// 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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<u8>,
/// Optional server host name (must be exactly 64 bytes during parsing/building)
pub sname: Vec<u8>,
/// Boot file name (must be exactly 128 bytes during parsing/building)
pub file: Vec<u8>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions nex-packet/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// <https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-2>
#[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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -435,6 +440,7 @@ impl DnsType {
/// Represents an DNS operation code.
/// <https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-5>
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum OpCode {
Query,
InverseQuery,
Expand Down Expand Up @@ -488,6 +494,7 @@ impl OpCode {
/// Represents an DNS return code.
/// <https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6>
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum RetCode {
NoError,
FormErr,
Expand Down
3 changes: 2 additions & 1 deletion nex-packet/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
1 change: 1 addition & 0 deletions nex-packet/src/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions nex-packet/src/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions nex-packet/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
Expand Down