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

83 lines
1.9 KiB

2 years ago
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2 years ago
use crate::util;
2 years ago
use anyhow::{anyhow, Result};
use convert_base::Convert;
/*
Algorithm: O(n)
let i = 0 .. u32:max_value()
# Convert each i to base 256 and we get all the ipv4 addresses
# This is waaaay better than a stupid loop
*/
2 years ago
#[derive(Debug, Copy, Clone)]
2 years ago
pub struct IPv4 {
pub id: u64,
2 years ago
pub ip: [u8; 4],
2 years ago
pub ignore: bool,
}
impl IPv4 {
pub fn new(id: u64) -> Self {
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 i in 0..(4 - ip.len()) {
ip.insert(0, 0);
}
}
// Reverse it so that we start from the top
ip = ip.into_iter().rev().collect();
2 years ago
Self {
id,
ip,
ignore: false,
}
}
pub fn to_ipaddr(self: &mut Self) -> Result<IpAddr> {
2 years ago
if let [a, b, c, d] = self.ip[0..3] {
Ok(IpAddr::V4(Ipv4Addr::new(a, b, c, d)))
} else {
Err(anyhow!("Unable to unpack IPv4 address"))
}
}
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 fn get_all(ignorelist: Option<Vec<u64>>) -> Result<Vec<IPv4>> {
// Ignore those that we know
let ignorelist = ignorelist.unwrap_or(Vec::new());
2 years ago
// Get all of the "ids"
let ids: Vec<u32> = (0..u32::max_value()).collect();
2 years ago
let ips: Vec<IPv4> = ids
.iter()
.map(|&ip| {
// Make IP
let mut ip = IPv4::new(ip as u64);
2 years ago
2 years ago
// Make the IP "ignored" if it is in the ignorelist
2 years ago
ip.ignore = ignorelist.contains(&ip.id);
2 years ago
2 years ago
ip
})
.collect();
2 years ago
Ok(ips)
}