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
11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ members = [
]

[workspace.package]
version = "0.18.0"
version = "0.19.0"
edition = "2021"
authors = ["shellrow <shellrow@fortnium.com>"]

[workspace.dependencies]
nex-core = { version = "0.19.0", path = "nex-core" }
nex-datalink = { version = "0.19.0", path = "nex-datalink" }
nex-macro = { version = "0.19.0", path = "nex-macro" }
nex-macro-helper = { version = "0.19.0", path = "nex-macro-helper" }
nex-packet = { version = "0.19.0", path = "nex-packet" }
nex-packet-builder = { version = "0.19.0", path = "nex-packet-builder" }
nex-socket = { version = "0.19.0", path = "nex-socket" }
nex-sys = { version = "0.19.0", path = "nex-sys" }
serde = { version = "1" }
libc = "0.2"
rand = "0.8"
netdev = { version = "0.34" }
async-io = "2.4"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ To use `nex`, add it as a dependency in your `Cargo.toml`:

```toml
[dependencies]
nex = "0.18"
nex = "0.19"
```

## Using Specific Sub-crates
Expand Down
4 changes: 2 additions & 2 deletions nex-datalink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ libc = { workspace = true }
netdev = { workspace = true }
serde = { workspace = true, features = ["derive"], optional = true }
pcap = { version = "2.0", optional = true }
nex-core = { path = "../nex-core", version = "0.18.0" }
nex-sys = { path = "../nex-sys", version = "0.18.0" }
nex-core = { workspace = true }
nex-sys = { workspace = true }

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.59.0"
Expand Down
2 changes: 1 addition & 1 deletion nex-macro-helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ categories = ["network-programming"]
license = "MIT"

[dependencies]
nex-core = { path = "../nex-core", version = "0.18.0" }
nex-core = { workspace = true }
2 changes: 1 addition & 1 deletion nex-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ syn = { version = "2.0", features = ["full"] }
regex = "1.11"

[dev-dependencies]
nex-macro-helper = { path = "../nex-macro-helper", version = "0.18.0" }
nex-macro-helper = { workspace = true }
4 changes: 2 additions & 2 deletions nex-packet-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ categories = ["network-programming"]
license = "MIT"

[dependencies]
nex-core = { path = "../nex-core", version = "0.18.0" }
nex-packet = { path = "../nex-packet", version = "0.18.0" }
nex-core = { workspace = true }
nex-packet = { workspace = true }
rand = { workspace = true }
6 changes: 2 additions & 4 deletions nex-packet-builder/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,13 @@ impl PacketBuilder {

if self.packet.len() >= min_offset_ipv4 {
if self.packet.len() < min_offset_ipv4 + dhcp_packet.len() {
self.packet
.resize(min_offset_ipv4 + dhcp_packet.len(), 0);
self.packet.resize(min_offset_ipv4 + dhcp_packet.len(), 0);
}
self.packet[min_offset_ipv4..min_offset_ipv4 + dhcp_packet.len()]
.copy_from_slice(&dhcp_packet);
} else if self.packet.len() >= min_offset_ipv6 {
if self.packet.len() < min_offset_ipv6 + dhcp_packet.len() {
self.packet
.resize(min_offset_ipv6 + dhcp_packet.len(), 0);
self.packet.resize(min_offset_ipv6 + dhcp_packet.len(), 0);
}
self.packet[min_offset_ipv6..min_offset_ipv6 + dhcp_packet.len()]
.copy_from_slice(&dhcp_packet);
Expand Down
34 changes: 20 additions & 14 deletions nex-packet-builder/src/dhcp.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use nex_core::mac::MacAddr;
use nex_packet::dhcp::{DhcpHardwareType, DhcpOperation, MutableDhcpPacket};
use std::net::Ipv4Addr;
use nex_packet::dhcp::DHCP_MIN_PACKET_SIZE;
use nex_packet::dhcp::{DhcpHardwareType, DhcpOperation, MutableDhcpPacket};
use nex_packet::Packet;
use std::net::Ipv4Addr;

#[derive(Clone, Debug)]
pub struct DhcpPacketBuilder {
Expand Down Expand Up @@ -45,9 +45,9 @@ impl DhcpPacketBuilder {
self.operation = DhcpOperation::Request;
self.options.clear();
self.options.extend_from_slice(&[
53, 1, 1, // DHCP Message Type: DHCPDISCOVER (1)
55, 2, 1, 3, // Parameter Request List: Subnet Mask (1), Router (3)
255, // End
53, 1, 1, // DHCP Message Type: DHCPDISCOVER (1)
55, 2, 1, 3, // Parameter Request List: Subnet Mask (1), Router (3)
255, // End
]);
}

Expand All @@ -62,19 +62,26 @@ impl DhcpPacketBuilder {
self.operation = DhcpOperation::Request;
self.options.clear();
self.options.extend_from_slice(&[
53, 1, 3, // DHCP Message Type: DHCPREQUEST (3)
50, 4, // Requested IP Address
53,
1,
3, // DHCP Message Type: DHCPREQUEST (3)
50,
4, // Requested IP Address
requested_ip.octets()[0],
requested_ip.octets()[1],
requested_ip.octets()[2],
requested_ip.octets()[3],
54, 4, // DHCP Server Identifier
54,
4, // DHCP Server Identifier
server_id.octets()[0],
server_id.octets()[1],
server_id.octets()[2],
server_id.octets()[3],
55, 2, 1, 3, // Parameter Request List
255, // End
55,
2,
1,
3, // Parameter Request List
255, // End
]);
}

Expand Down Expand Up @@ -121,10 +128,9 @@ mod tests {
fn test_dhcp_discover_builder() {
let transaction_id = 0x12345678;
let client_mac = MacAddr::new(0x00, 0x11, 0x22, 0x33, 0x44, 0x55);
let builder = DhcpPacketBuilder::new(transaction_id, client_mac)
.with_discover_options();
let builder = DhcpPacketBuilder::new(transaction_id, client_mac).with_discover_options();
let packet = builder.build();

assert!(packet.len() >= DHCP_MIN_PACKET_SIZE);
assert_eq!(packet[0], 1);
assert_eq!(
Expand All @@ -143,7 +149,7 @@ mod tests {
let builder = DhcpPacketBuilder::new(transaction_id, client_mac)
.with_request_options(requested_ip, server_id);
let packet = builder.build();

assert!(packet.len() >= DHCP_MIN_PACKET_SIZE);
assert_eq!(packet[0], 1);
assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions nex-packet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ license = "MIT"
[dependencies]
rand = { workspace = true }
serde = { workspace = true, features = ["derive"], optional = true }
nex-core = { path = "../nex-core", version = "0.18.0" }
nex-macro = { path = "../nex-macro", version = "0.18.0" }
nex-macro-helper = { path = "../nex-macro-helper", version = "0.18.0" }
nex-core = { workspace = true }
nex-macro = { workspace = true }
nex-macro-helper = { workspace = true }

[features]
clippy = []
Expand Down
62 changes: 43 additions & 19 deletions nex-packet/src/dns.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use alloc::string::String;
use alloc::vec::Vec;
use core::{fmt, str};
use std::str::Utf8Error;
use nex_macro::packet;
use nex_macro_helper::packet::{Packet, PacketSize, PrimitiveValues};
use nex_macro_helper::types::{u1, u16be, u32be, u4};
use std::str::Utf8Error;

/// Represents an DNS operation.
/// These identifiers correspond to DNS resource record classes.
Expand Down Expand Up @@ -570,17 +570,17 @@ pub fn parse_name(packet: &DnsPacket, coded_name: &Vec<u8>) -> Result<String, Ut
let start = packet.packet();
let mut name = coded_name.as_slice();
let mut rname = String::new();
let mut offset:usize = 0;
let mut offset: usize = 0;

loop {
let label_len:u16 = name[offset] as u16;
let label_len: u16 = name[offset] as u16;
if label_len == 0 {
break;
}
if (label_len & 0xC0) == 0xC0 {
let offset1 = ((label_len & 0x3F) as usize) << 8;
let offset2 = name[offset + 1] as usize;
offset = offset1 + offset2;
offset = offset1 + offset2;
// now change name
name = start;
continue;
Expand Down Expand Up @@ -658,15 +658,32 @@ impl SrvName {
pub fn new(name: &str) -> Self {
let parts: Vec<&str> = name.split('.').collect();
let (instance, service, protocol, domain) = match parts.as_slice() {
[instance, service, protocol, domain @ ..] if service.starts_with('_') && protocol.starts_with('_') => {
(Some(String::from(*instance)), Some(String::from(*service)), Some(String::from(*protocol)), Some(String::from(domain.join("."))))
},
[service, protocol, domain @ ..] if service.starts_with('_') && protocol.starts_with('_') => {
(None, Some(String::from(*service)), Some(String::from(*protocol)), Some(String::from(domain.join("."))))
},
[instance, service, protocol, domain @ ..] => {
(Some(String::from(*instance)), Some(String::from(*service)), Some(String::from(*protocol)), Some(String::from(domain.join("."))))
},
[instance, service, protocol, domain @ ..]
if service.starts_with('_') && protocol.starts_with('_') =>
{
(
Some(String::from(*instance)),
Some(String::from(*service)),
Some(String::from(*protocol)),
Some(String::from(domain.join("."))),
)
}
[service, protocol, domain @ ..]
if service.starts_with('_') && protocol.starts_with('_') =>
{
(
None,
Some(String::from(*service)),
Some(String::from(*protocol)),
Some(String::from(domain.join("."))),
)
}
[instance, service, protocol, domain @ ..] => (
Some(String::from(*instance)),
Some(String::from(*service)),
Some(String::from(*protocol)),
Some(String::from(domain.join("."))),
),
_ => (None, None, None, None),
};

Expand Down Expand Up @@ -696,7 +713,12 @@ fn test_dns_query_packet() {
assert_eq!(packet.get_authority_rr_count(), 0);
assert_eq!(packet.get_additional_rr_count(), 1);
assert_eq!(packet.get_queries().len(), 1);
assert_eq!(packet.get_queries()[0].get_qname_parsed().unwrap_or(String::new()), "cloudflare.com");
assert_eq!(
packet.get_queries()[0]
.get_qname_parsed()
.unwrap_or(String::new()),
"cloudflare.com"
);
assert_eq!(packet.get_queries()[0].qtype, DnsTypes::A);
assert_eq!(packet.get_queries()[0].qclass, DnsClasses::IN);
assert_eq!(packet.get_responses().len(), 0);
Expand All @@ -721,7 +743,12 @@ fn test_dns_reponse_packet() {
assert_eq!(packet.get_authority_rr_count(), 0);
assert_eq!(packet.get_additional_rr_count(), 1);
assert_eq!(packet.get_queries().len(), 1);
assert_eq!(packet.get_queries()[0].get_qname_parsed().unwrap_or(String::new()), "cloudflare.com");
assert_eq!(
packet.get_queries()[0]
.get_qname_parsed()
.unwrap_or(String::new()),
"cloudflare.com"
);
assert_eq!(packet.get_queries()[0].qtype, DnsTypes::A);
assert_eq!(packet.get_queries()[0].qclass, DnsClasses::IN);
assert_eq!(packet.get_responses().len(), 2);
Expand Down Expand Up @@ -782,9 +809,7 @@ fn test_mdns_response() {
assert_eq!(String::from_utf8(text_rr.get_text()).unwrap(), "version=1");
// RR #3
let srv_name = parse_name(&packet, &responses[2].rname).unwrap_or(String::new());
assert_eq!(
srv_name, "_service._amzn-alexa._tcp.local"
);
assert_eq!(srv_name, "_service._amzn-alexa._tcp.local");
assert_eq!(responses[2].rtype, DnsTypes::SRV);
assert_eq!(responses[2].data_len, 29);
let srv_rr = DnsRrSrvPacket::new(&responses[2].data).unwrap();
Expand All @@ -803,5 +828,4 @@ fn test_mdns_response() {
// RR #4
assert_eq!(responses[3].rtype, DnsTypes::A);
assert_eq!(responses[3].data.as_slice(), [192, 168, 1, 6]);

}
4 changes: 2 additions & 2 deletions nex-socket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ categories = ["network-programming"]
license = "MIT"

[dependencies]
async-io = { workspace = true }
async-io = "2.4"
futures-lite = "2.6"
futures-io = "0.3"
socket2 = { version = "0.5", features = ["all"] }
nex-packet = { path = "../nex-packet", version = "0.18.0" }
nex-packet = { workspace = true }

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.59.0"
Expand Down
12 changes: 6 additions & 6 deletions nex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ categories = ["network-programming"]
license = "MIT"

[dependencies]
nex-core = { path = "../nex-core", version = "0.18.0" }
nex-packet = { path = "../nex-packet", version = "0.18.0" }
nex-datalink = { path = "../nex-datalink", version = "0.18.0" }
nex-socket = { path = "../nex-socket", version = "0.18.0" }
nex-packet-builder = { path = "../nex-packet-builder", version = "0.18.0" }
nex-core = { workspace = true }
nex-packet = { workspace = true }
nex-datalink = { workspace = true }
nex-socket = { workspace = true }
nex-packet-builder = { workspace = true }

[dev-dependencies]
serde_json = "1.0"
async-io = { workspace = true }
async-io = "2.4"
futures = "0.3"

[features]
Expand Down