r-lanlib
A Rust library crate for performing network scanning operations on any local area network (LAN). This is the Rust version of the go-lanscan package.
Features
- ARP Scanning: Discover devices on your network using Address Resolution Protocol
- SYN Scanning: Detect open ports on discovered devices using TCP SYN packets
- Full Scanning: Combined ARP and SYN scanning in a single operation
- Vendor Detection: Identify device manufacturers using MAC address lookup
- Hostname Resolution: Resolve hostnames for discovered devices
- Async Communication: Channel-based communication for real-time scan results
- Flexible Targeting: Support for CIDR blocks, IP ranges, and port ranges
Requirements
- Root privileges required: This library performs raw packet operations that require elevated permissions
- Rust 1.89.0+ with Rust 2024 edition support
Installation
Quick Start
See the working examples in examples/:
arp-scanner.rs- ARP device discoverysyn-scanner.rs- Port scanning on known devicesfull-scanner.rs- Combined ARP + SYN scanning
Run them from the workspace root:
API Reference
Core Modules
network
Provides helpers for selecting network interfaces:
get_default_interface()- Get the default network interface, returnsResult<NetworkInterface>get_interface(name)- Get a specific interface by name, returnsResult<NetworkInterface>get_available_port()- Find an available port for scanning, returnsResult<u16>
routing
Provides OS-level routing table inspection:
get_default_gateway()- Detect the default gateway IP address by parsing the system routing table (netstat -rnon macOS,ip route showon Linux). ReturnsOption<Ipv4Addr>—Noneif the gateway cannot be determined or the platform is unsupported.
oui
OUI (Organizationally Unique Identifier) lookup for resolving MAC address prefixes to vendor/organization names:
oui::default(project_name, max_age)- Initialize the built-in IEEE OUI database. Downloads and caches five IEEE CSV data files locally under the OS-appropriate data directory forproject_name. Re-downloads automatically when the cached files are older thanmax_age. ReturnsResult<Arc<dyn Oui>>.oui::traits::Oui- Trait for custom OUI implementations. Implement this to supply your own vendor database to the scanners.oui::db::OuiDb- The default implementation backed by locally cached IEEE CSV files. Supports MA-L (24-bit), MA-M (28-bit), and MA-S/IAB (36-bit) prefixes, resolving the most-specific match first.oui::types::OuiData- Holds theorganizationstring for a matched prefix.
wire
Low-level packet I/O:
wire::default(interface)- Create aWirefor reading and sending packets- Various packet builders for ARP, SYN, RST packets (in the
packetmodule)
scanners
Main scanning implementations:
ARPScanner- Discover devices using ARPSYNScanner- Scan ports on known devicesFullScanner- Combined ARP + SYN scanning
targets
Target specification utilities:
ips::IPTargets- Define IP ranges and CIDR blocksports::PortTargets- Define port ranges and individual ports
Data Structures
Device
Represents a discovered network device:
Port
Represents a network port:
PortSet
Wrapper around HashSet<Port> with convenience methods:
;
ScanMessage
Messages sent over the notification channel:
Target Specification
IP Targets
// CIDR blocks
new;
// IP ranges
new;
// Individual IPs
new;
Port Targets
// Port ranges
new;
// Individual ports
new;
// Mixed specification
new;
Configuration Options
Scanner Timeouts
idle_timeout- How long to wait for responses before concluding scan- Default: 10 seconds (10,000ms)
- Recommended: 5-30 seconds depending on network size and latency
Packet Send Throttle
throttle- Delay inserted between sending each packet- Default: 200 microseconds (
Duration::from_micros(200)) - Higher values reduce packet loss on congested or high-latency networks at the cost of increased total scan time
Scanner Features
include_vendor- Enable MAC address vendor lookup (requiresouito be set)oui- Supply anArc<dyn Oui>database for vendor lookups. Useoui::default(project_name, max_age)for the built-in IEEE database, or provide a custom implementation. WhenNone, vendor lookup is skipped even ifinclude_vendoristrue.include_host_names- Resolve hostnames via reverse DNS lookupsource_port- Source port for scan packets (auto-selected if not specified)throttle- Delay between sending packets (default: 200µs); increase for more accurate scans on lossy or congested networks
Performance Tuning
- Concurrent scanning: Multiple threads handle packet I/O for optimal throughput
- Memory efficiency: Zero-copy packet processing where possible
- Throttle control: Configurable per-packet send delay via
throttlebuilder field (default200µs); higher values reduce packet loss at the cost of scan speed - Timeout optimization: Adaptive timeouts based on network response times
Security Considerations
- Requires root privileges for raw socket access on Unix-like systems
- Network scanning may be restricted by network policies and firewalls
- Built-in rate limiting prevents network congestion and reduces detection risk
- Minimal network footprint: Optimized packet sizes and timing
- Memory safety: Rust's ownership system prevents buffer overflows and memory corruption
- Use responsibly and only on networks you own or have permission to scan
- Logging: All scan activities can be logged for audit purposes
Ethical Usage Guidelines
- Always obtain proper authorization before scanning
- Respect network resources and avoid aggressive scanning
- Be aware that scanning activities may be logged by network security systems
- Consider the impact on network performance during large-scale scans
Error Handling
The library uses RLanLibError for comprehensive error reporting:
License
This project is dual-licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Related Projects
- go-lanscan - Original Go implementation
- r-lancli - Command-line interface using this library
- r-lanterm - Terminal UI application using this library