Scan a IPv4 range for a certain port
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wwmap/src/ipv4.rs

107 lines
2.7 KiB

2 years ago
use crate::util;
2 years ago
use anyhow::{anyhow, Result};
use cidr_utils::cidr::Ipv4Cidr;
2 years ago
use convert_base::Convert;
2 years ago
use std::convert::TryInto;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2 years ago
#[derive(Debug, Copy, Clone)]
2 years ago
pub struct IPv4 {
pub id: u64, // u32
2 years ago
pub ip: [u8; 4],
}
impl IPv4 {
pub fn new(id: u64) -> Self {
if id > u32::max_value() as u64 {
panic!(
"IPv4 id is above the IPv4 range! id={id} > {}",
u32::max_value()
);
}
let mut base = Convert::new(10, 256);
2 years ago
let id_vec = util::number_to_vec(id); // push all digits into a vec
let mut ip = base.convert::<u8, u8>(&id_vec);
// In case we are missing some digits
if ip.len() < 4 {
2 years ago
for _ in 0..(4 - ip.len()) {
ip.insert(0, 0);
}
}
// Reverse it so that we start from the top
2 years ago
// ip = ip.into_iter().rev().collect();
// convert to array
2 years ago
let ip: [u8; 4] = ip
.try_into()
.unwrap_or_else(|_: Vec<u8>| panic!("Unable to convert Vec to [u8; 4] for IPv4!"));
2 years ago
Self { id, ip }
}
pub fn to_ipaddr(self: &mut Self) -> Result<IpAddr> {
2 years ago
if let [a, b, c, d] = self.ip[0..4] {
Ok(IpAddr::V4(Ipv4Addr::new(a, b, c, d)))
} else {
2 years ago
Err(anyhow!("Unable to unpack IPv4 address: {:?}", self.ip))
}
}
2 years ago
pub fn to_socketaddr(self: &mut Self, port: u16) -> Result<SocketAddr> {
let ip_addr = self.to_ipaddr()?;
Ok(SocketAddr::new(ip_addr, port))
}
}
2 years ago
pub struct IPv4Range {
2 years ago
pub id_start: u32,
pub id_end: u32,
pub id_ignore: Vec<u32>,
}
2 years ago
impl IPv4Range {
2 years ago
pub fn new(from: u32, to: u32, id_ignore: Option<Vec<u32>>) -> Self {
2 years ago
let to = to.clamp(0, u32::max_value());
2 years ago
let id_ignore = id_ignore.unwrap_or(Vec::new());
if from >= to {
2 years ago
panic!("Range size must be >= 1! from={} >= to={}", from, to);
2 years ago
}
2 years ago
Self {
id_start: from,
id_end: to,
2 years ago
id_ignore,
2 years ago
}
}
2 years ago
pub fn from_cidr(cidr_string: String, id_ignore: Option<Vec<u32>>) -> Self {
let cidr = Ipv4Cidr::from_str(cidr_string).unwrap();
let (from, to) = (cidr.first(), cidr.last());
Self::new(from, to, id_ignore)
}
2 years ago
}
2 years ago
impl Iterator for IPv4Range {
2 years ago
type Item = u32;
fn next(&mut self) -> Option<u32> {
if self.id_start == self.id_end {
None
} else {
let res = Some(self.id_start);
self.id_start += 1;
if self.id_ignore.contains(&res.unwrap()) {
return self.next();
}
res
}
}
}