summaryrefslogtreecommitdiffstats
path: root/src/scanner.rs
blob: c51dffca55365f849f3ea9cdf562094db8404c9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::net::IpAddr;

use ipnet::IpNet;
use tokio::{sync::mpsc::Sender, task::JoinSet};

use crate::Result;

pub async fn scan(tx: Sender<IpAddr>, ip_net: Option<IpNet>) -> Result<()> {
    let ip_net = match ip_net {
        Some(net) if !net.addr().is_unspecified() => net,
        Some(net) => IpNet::new(local_ip_address::local_ip()?, net.prefix_len())?,
        None => IpNet::new(local_ip_address::local_ip()?, 24)?,
    };

    let mut join_set = JoinSet::new();
    for ip_addr in ip_net.hosts() {
        let tx = tx.clone();

        join_set.spawn(async move { tx.send(ip_addr).await });
    }

    while let Some(res) = join_set.join_next().await {
        res??;
    }

    Ok(())
}