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
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ members = [
]

[workspace.package]
version = "0.23.2"
edition = "2021"
version = "0.24.0"
edition = "2024"
authors = ["shellrow <shellrow@foctal.com>"]

[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" }
Expand Down
2 changes: 1 addition & 1 deletion examples/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/async_datalink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
94 changes: 53 additions & 41 deletions examples/async_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}
}

Expand Down Expand Up @@ -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");
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/async_icmp_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down
92 changes: 52 additions & 40 deletions examples/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}

Expand Down Expand Up @@ -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");
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/icmp_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Usage: icmp_socket <TARGET IP> <INTERFACE>

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;
Expand Down
6 changes: 3 additions & 3 deletions examples/mutable_chaining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion examples/ndp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion nex-core/src/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion nex-datalink/src/async_io/bpf.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion nex-datalink/src/async_io/wpcap.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion nex-datalink/src/bindings/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions nex-datalink/src/bindings/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Loading