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
129 changes: 119 additions & 10 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 @@ -43,7 +43,7 @@ rusqlite = { version = "0.31", features = ["bundled"] }

# Memory-mapped I/O (Sprint 6.6)
memmap2 = "0.9"
bincode = "1.3"
rkyv = "0.8.14"

# CSV export (promote to workspace)
csv = "1.3"
Expand Down
1 change: 1 addition & 0 deletions crates/prtip-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ rand = { workspace = true }
sysinfo = { workspace = true }
flate2 = "1.0"
dirs = "5.0"
rkyv = { workspace = true }

[dev-dependencies]
tokio = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/prtip-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ pub use resource_monitor::{
pub use retry::{retry_with_backoff, RetryConfig};
pub use service_db::{ServiceMatch, ServiceProbe, ServiceProbeDb};
pub use templates::{ScanTemplate, TemplateManager};
pub use types::{PortRange, PortState, Protocol, ScanResult, ScanTarget, ScanType, TimingTemplate};
pub use types::{PortRange, PortState, Protocol, ScanResult, ScanResultRkyv, ScanTarget, ScanType, TimingTemplate};
51 changes: 50 additions & 1 deletion crates/prtip-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::error::{Error, Result};
use chrono::{DateTime, Utc};
use ipnetwork::IpNetwork;
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::net::IpAddr;
Expand Down Expand Up @@ -315,7 +316,8 @@ impl Iterator for PortRangeIterator {
}

/// State of a scanned port
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize)]
#[rkyv(derive(Debug))]
pub enum PortState {
/// Port is open and accepting connections
Open,
Expand Down Expand Up @@ -475,6 +477,53 @@ impl fmt::Display for TimingTemplate {
}
}

/// Serializable representation of ScanResult for rkyv
#[derive(Debug, Clone, Archive, RkyvSerialize, RkyvDeserialize)]
#[rkyv(derive(Debug))]
pub struct ScanResultRkyv {
target_ip: IpAddr,
port: u16,
state: PortState,
response_time_nanos: u128,
timestamp_nanos: i64,
banner: Option<String>,
service: Option<String>,
version: Option<String>,
raw_response: Option<Vec<u8>>,
}

impl From<&ScanResult> for ScanResultRkyv {
fn from(result: &ScanResult) -> Self {
Self {
target_ip: result.target_ip,
port: result.port,
state: result.state,
response_time_nanos: result.response_time.as_nanos(),
timestamp_nanos: result.timestamp.timestamp_nanos_opt().unwrap_or(0),
banner: result.banner.clone(),
service: result.service.clone(),
version: result.version.clone(),
raw_response: result.raw_response.clone(),
}
}
}

impl From<ScanResultRkyv> for ScanResult {
fn from(rkyv: ScanResultRkyv) -> Self {
Self {
target_ip: rkyv.target_ip,
port: rkyv.port,
state: rkyv.state,
response_time: Duration::from_nanos(rkyv.response_time_nanos as u64),
timestamp: DateTime::from_timestamp_nanos(rkyv.timestamp_nanos),
banner: rkyv.banner,
service: rkyv.service,
version: rkyv.version,
raw_response: rkyv.raw_response,
}
}
}

/// Result of scanning a single port
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScanResult {
Expand Down
2 changes: 1 addition & 1 deletion crates/prtip-scanner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pcap-file = "2.0"

# Memory-mapped I/O
memmap2 = { workspace = true }
bincode = { workspace = true }
rkyv = { workspace = true, features = ["alloc"] }

[dev-dependencies]
tokio = { workspace = true }
Expand Down
23 changes: 19 additions & 4 deletions crates/prtip-scanner/src/output/mmap_reader.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Memory-mapped result reader for zero-copy access to scan results

use memmap2::Mmap;
use prtip_core::ScanResult;
use prtip_core::{ScanResult, ScanResultRkyv};
use std::fs::File;
use std::io;
use std::path::Path;

const HEADER_SIZE: usize = 64;
const ENTRY_SIZE: usize = 512;
const LENGTH_PREFIX_SIZE: usize = 8; // 8 bytes for length to maintain alignment

/// Memory-mapped result reader
pub struct MmapResultReader {
Expand Down Expand Up @@ -75,10 +76,24 @@ impl MmapResultReader {
}

let offset = HEADER_SIZE + (index * self.entry_size);
let entry_bytes = &self.mmap[offset..offset + self.entry_size];

// Read length prefix (8 bytes)
let len_bytes: [u8; 8] = self.mmap[offset..offset + LENGTH_PREFIX_SIZE].try_into().ok()?;
let len = u64::from_le_bytes(len_bytes) as usize;

if len == 0 || len + LENGTH_PREFIX_SIZE > self.entry_size {
return None;
}

// Deserialize the entry (bincode handles trailing zeros)
bincode::deserialize(entry_bytes).ok()
// Copy data to an aligned buffer (rkyv requires alignment)
let entry_bytes = &self.mmap[offset + LENGTH_PREFIX_SIZE..offset + LENGTH_PREFIX_SIZE + len];
let aligned_data: Vec<u8> = entry_bytes.to_vec();

// Deserialize using rkyv
match rkyv::from_bytes::<ScanResultRkyv, rkyv::rancor::Error>(&aligned_data) {
Ok(rkyv_result) => Some(rkyv_result.into()),
Err(_) => None,
}
}

/// Create an iterator over all entries
Expand Down
Loading