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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ jobs:
uv sync --frozen --no-install-project

- name: Build and Install
run: uvx maturin develop
run: uv run maturin develop

- name: Test Rust
run: cargo test
run: uv run cargo test

- name: Test Python
run: uv run pytest tests/python
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ stackforge.egg-info/
# IDE
.vscode/
.idea/

helper_files/
# Tools
.bin/
61 changes: 52 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ shared-version = true
tag-name = "v{{version}}"

[workspace.package]
version = "0.1.1"
version = "0.2.0"
edition = "2024"
license = "GPL-3.0-only"
authors = ["Stackforge Contributors"]
Expand Down
1 change: 1 addition & 0 deletions crates/stackforge-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ smallvec.workspace = true
pnet_datalink = "0.35.0"
default-net = "0.22.0"
rand = { version = "0.9.2", optional = true }
pcap-file = "2"

[features]
default = ["rand"]
Expand Down
6 changes: 6 additions & 0 deletions crates/stackforge-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ pub enum PacketError {
Io(String),
}

impl From<std::io::Error> for PacketError {
fn from(err: std::io::Error) -> Self {
PacketError::Io(err.to_string())
}
}

impl PacketError {
/// Create a buffer too short error.
pub fn buffer_too_short(expected: usize, actual: usize) -> Self {
Expand Down
41 changes: 39 additions & 2 deletions crates/stackforge-core/src/layer/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,16 +902,53 @@ pub struct ArpBuilder {
pdst: ProtocolAddr,
}

/// Get the default interface's MAC address and IPv4 address.
fn get_local_mac_and_ip() -> (MacAddress, Ipv4Addr) {
let default_mac = MacAddress::ZERO;
let default_ip = Ipv4Addr::new(0, 0, 0, 0);

let default_iface = match default_net::get_default_interface() {
Ok(iface) => iface,
Err(_) => return (default_mac, default_ip),
};

let interfaces = pnet_datalink::interfaces();
let iface = match interfaces.iter().find(|i| i.name == default_iface.name) {
Some(i) => i,
None => return (default_mac, default_ip),
};

let mac = iface
.mac
.map(|m| MacAddress::new(m.octets()))
.unwrap_or(default_mac);

let ip = iface
.ips
.iter()
.find_map(|ip_net| {
if let IpAddr::V4(v4) = ip_net.ip() {
Some(v4)
} else {
None
}
})
.unwrap_or(default_ip);

(mac, ip)
}

impl Default for ArpBuilder {
fn default() -> Self {
let (local_mac, local_ip) = get_local_mac_and_ip();
Self {
hwtype: hardware_type::ETHERNET,
ptype: protocol_type::IPV4,
hwlen: None,
plen: None,
op: opcode::REQUEST,
hwsrc: HardwareAddr::Mac(MacAddress::ZERO),
psrc: ProtocolAddr::Ipv4(Ipv4Addr::new(0, 0, 0, 0)),
hwsrc: HardwareAddr::Mac(local_mac),
psrc: ProtocolAddr::Ipv4(local_ip),
hwdst: HardwareAddr::Mac(MacAddress::ZERO),
pdst: ProtocolAddr::Ipv4(Ipv4Addr::new(0, 0, 0, 0)),
}
Expand Down
Loading