xdb-parse
A parser to xdb files like ip2region A high-performance Rust library for parsing and querying IP database files (xdb format) like ip2region. Supports both IPv4 and IPv6 address lookup with efficient binary search algorithms.
Features
- Dual Protocol Support: Full support for both IPv4 and IPv6 address lookup
- High Performance: Optimized binary search algorithm for fast IP queries
- Memory Efficient: Load database files once and query multiple times
- Thread Safe: Designed for concurrent usage in multi-threaded environments
- Flexible Input: Accepts IP addresses in multiple formats (string, numeric)
- Zero Dependencies: Minimal external dependencies for reliability
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
Basic Usage
use ;
use Result;
IPv6 Support
use ;
use Result;
Examples
Example 1: Basic IP Lookup
See examples/search_ip.rs:
use Instant;
use Result;
use ;
Example 2: Multi-threaded Usage
The library is designed for thread-safe concurrent usage:
use ;
use ;
use Result;
Example 3: Performance Benchmarking
See benches/search.rs for performance testing:
use ;
use ;
criterion_group!;
criterion_main!;
API Reference
Core Functions
load_file(path: PathBuf) -> Result<Vec<u8>, XdbError>
Loads an xdb database file into memory.
Parameters:
path: Path to the xdb file
Returns: Byte vector containing the database data
search_ip(ip: &str, data: &[u8]) -> Result<String, XdbError>
Searches for an IP address in the loaded database.
Parameters:
ip: IP address to search (supports multiple formats)data: Database data loaded byload_file
Returns: Location information as string
Supported IP Formats
The library accepts IP addresses in multiple formats:
// Standard IPv4 format
search_ip?;
// Standard IPv6 format
search_ip?;
// Numeric format (IPv4)
search_ip?; // Equivalent to 192.168.1.1
// Numeric format (IPv6)
search_ip?;
Database File Format
The library supports the xdb format used by ip2region:
- Header: 256 bytes containing metadata
- Vector Index: 256×256 index blocks for fast lookup
- Segment Data: IP range segments with location information
File Structure
┌─────────────────┐
│ Header │ 256 bytes
├─────────────────┤
│ Vector Index │ 256×256×8 bytes
├─────────────────┤
│ Segment Data │ Variable length
└─────────────────┘
Performance
The library uses an optimized binary search algorithm:
- IPv4 Lookup: ~5 microseconds per query
- IPv6 Lookup: ~250 microseconds per query
- Memory Usage: Database loaded once, shared across threads
Error Handling
The library uses thiserror for comprehensive error handling:
use XdbError;
match search_ip
Running Examples
To run the provided examples:
# Run the basic search example
# Run benchmarks
# Run tests
License
This project is licensed under the same terms as Rust itself.
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
Acknowledgments
- Inspired by the ip2region project
- Uses efficient binary search algorithms for fast IP lookup
- Designed for high-performance applications