# netforge
[](https://crates.io/crates/netforge)
[](#license)
High-performance raw packet I/O engine for internet-scale scanning. Three
backends, auto-selected by system capability, with a unified
`PacketEngine` trait.
| AF_XDP | `xdp-backend` | 25M+ pps | Kernel 5.10+, `CAP_BPF`, libbpf |
| sendmmsg | `sendmmsg-backend` | 10M+ pps | Linux, `CAP_NET_RAW` |
| pnet | `pnet-backend` | 1M pps | Any platform with `CAP_NET_RAW` |
`auto_select()` tries the fastest available backend first and falls
through on construction failure.
## Install
```toml
[dependencies]
netforge = "0.1"
```
Default features compile in `sendmmsg-backend` + `pnet-backend`. Opt
into AF_XDP explicitly:
```toml
netforge = { version = "0.1", features = ["xdp-backend"] }
```
## Usage
```rust,no_run
use netforge::{auto_select, EngineConfig, RawPacket, packet::build_syn_template};
use std::net::Ipv4Addr;
let config = EngineConfig {
source_ip: Ipv4Addr::new(192, 168, 1, 10),
rate_pps: 1_000_000,
interface: Some("eth0".into()),
..Default::default()
};
let engine = auto_select(config)?;
let mut tmpl = build_syn_template(Ipv4Addr::new(192, 168, 1, 10), 49_152);
netforge::packet::stamp_syn(&mut tmpl, Ipv4Addr::new(1, 1, 1, 1), 80, 0);
let sent = engine.tx_batch(&[tmpl])?;
println!("sent {sent} packet(s)");
let stats = engine.stats();
println!("backend={} tx_pps={}", engine.name(), stats.tx_pps);
# Ok::<_, netforge::engine::EngineError>(())
```
## Stateless sequence encoding
The `seq` module provides cryptographically-keyed TCP sequence number
encoding for fully stateless SYN scanning. The full probe identity
`(src_ip, dst_ip, dst_port, src_port, phase)` is encoded into the 32-bit
sequence number using a keyed hash; SYN-ACK responses are verified
against the hash at receive time, with **zero per-connection state**.
```rust
use netforge::{SeqEncoder, StatelessCookie};
use std::net::Ipv4Addr;
let enc = SeqEncoder::with_cookie(StatelessCookie::with_key(0xDEAD_BEEF));
let seq = enc.encode(Ipv4Addr::new(1, 1, 1, 1), 80, 49_152, 0);
// Later, when a SYN-ACK arrives with ack_num = seq + 1:
assert!(enc.verify_synack(seq.wrapping_add(1), Ipv4Addr::new(1, 1, 1, 1), 80, 49_152));
```
## SIMD-friendly checksums
The `checksum` module provides the Internet checksum (RFC 1071) for IP
and TCP headers. The implementation uses a folded-32-bit-sum form that
LLVM auto-vectorises into 32-byte AVX2 chunks on x86_64.
## License
Dual-licensed under either of:
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.