Fix bare IP parsing

This commit is contained in:
Keenan Tims 2023-03-14 00:40:01 -07:00
parent 5345f7b1d2
commit dd4cc163c2
Signed by: ktims
GPG Key ID: 11230674D69038D4
2 changed files with 24 additions and 9 deletions

View File

@ -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.

View File

@ -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,6 +46,19 @@ fn simplify_input(mut input: Input) -> (IpBothRange, Errors) {
()
}
},
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 {
@ -56,6 +69,8 @@ fn simplify_input(mut input: Input) -> (IpBothRange, Errors) {
}
}
}
}
}
res.v4.simplify();
res.v6.simplify();