mirror of
https://github.com/ktims/rs-aggregate.git
synced 2024-11-15 23:27:18 -08:00
Fix bare IP parsing
This commit is contained in:
parent
5345f7b1d2
commit
dd4cc163c2
@ -1,5 +1,5 @@
|
||||
# rs-aggregate
|
||||
rs-aggregate will compress an unsorted list of IP prefixes
|
||||
rs-aggregate will aggregate an unsorted list of IP prefixes
|
||||
|
||||
Intended to be [aggregate6](https://github.com/job/aggregate6) with better performance.
|
||||
|
||||
|
31
src/main.rs
31
src/main.rs
@ -4,7 +4,7 @@ extern crate iprange;
|
||||
use clio::*;
|
||||
use ipnet::{IpNet, Ipv4Net, Ipv6Net};
|
||||
use iprange::IpRange;
|
||||
use std::io::BufRead;
|
||||
use std::{io::BufRead, net::IpAddr};
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
@ -34,7 +34,7 @@ fn simplify_input(mut input: Input) -> (IpBothRange, Errors) {
|
||||
};
|
||||
let mut errors = Errors::new();
|
||||
for line in input.lock().lines() {
|
||||
for net in line.unwrap().split_whitespace() {
|
||||
for net in line.unwrap().split_whitespace().to_owned() {
|
||||
match net.parse() {
|
||||
Ok(ipnet) => match ipnet {
|
||||
IpNet::V4(v4_net) => {
|
||||
@ -46,12 +46,27 @@ fn simplify_input(mut input: Input) -> (IpBothRange, Errors) {
|
||||
()
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("ERROR: {} - {}, ignoring.", net, error.to_string());
|
||||
errors.push(IpParseError {
|
||||
ip: net.to_string(),
|
||||
problem: error.to_string(),
|
||||
});
|
||||
Err(_) => {
|
||||
// First try to add it as a bare IP
|
||||
match net.parse() {
|
||||
Ok(ip) => match ip {
|
||||
IpAddr::V4(v4_ip) => {
|
||||
res.v4.add(Ipv4Net::new(v4_ip, 32).unwrap());
|
||||
()
|
||||
}
|
||||
IpAddr::V6(v6_ip) => {
|
||||
res.v6.add(Ipv6Net::new(v6_ip, 128).unwrap());
|
||||
()
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("ERROR: {} - {}, ignoring.", net, error.to_string());
|
||||
errors.push(IpParseError {
|
||||
ip: net.to_string(),
|
||||
problem: error.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user