Crate async_arp

Source
Expand description

§Example: Probing Hosts in a Network Range with Retry Capabilities

This example demonstrates probing all hosts in a given network range (e.g., an IPv4 subnet) with retry capabilities, using ClientSpinner to batch the probes. This approach allows for retrying failed probes across the entire network range. To run this example locally, make sure to specify the network interface (e.g., eth0 or wlan0) as a parameter.

use common;
use async_arp::{Client, ClientConfigBuilder, ClientSpinner, ProbeStatus, Result};
use clap::Parser;
use std::io::Write;
use std::time::{Duration, Instant};

mod common;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
    let args = common::Args::parse();
    let interface = common::interface_from(&args.iface);
    let net = common::net_from(&interface).unwrap();

    let client = Client::new(
        ClientConfigBuilder::new(&args.iface)
            .with_response_timeout(Duration::from_millis(500))
            .build(),
    )?;
    let spinner = ClientSpinner::new(client).with_retries(9);

    let start = Instant::now();
    let outcomes = spinner
        .probe_batch(&common::generate_probe_inputs(net, interface))
        .await;

    let occupied = outcomes?
        .into_iter()
        .filter(|outcome| outcome.status == ProbeStatus::Occupied);
    let scan_duration = start.elapsed();

    {
        let mut stdout = std::io::stdout().lock();
        writeln!(stdout, "Found hosts:").unwrap();
        for outcome in occupied {
            writeln!(stdout, "{:?}", outcome).unwrap();
        }
        writeln!(stdout, "Scan took {:?}", scan_duration).unwrap();
    }

    Ok(())
}

§Example 2: Using Client for Single Probe Requests

When more granular control over the execution order is needed, or if only a single probe or request is desired, you can use Client instead of ClientSpinner. This approach is useful for cases when you want to handle each probe individually or when finer control is needed over how the futures are driven to completion.

use common;
use async_arp::{Client, ClientConfigBuilder, ProbeStatus};
use clap::Parser;
use std::io::Write;
use std::time::{Duration, Instant};

mod common;

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let args = common::Args::parse();
    let interface = common::interface_from(&args.iface);
    let net = common::net_from(&interface).unwrap();

    let client = Client::new(
        ClientConfigBuilder::new(&args.iface)
            .with_response_timeout(Duration::from_millis(500))
            .build(),
    )
    .unwrap();
    let inputs = common::generate_probe_inputs(net, interface);

    let start = Instant::now();
    let futures = inputs.into_iter().map(|input| client.probe(input));
    let outcomes = futures::future::join_all(futures).await;
    let scan_duration = start.elapsed();

    let occupied = outcomes
        .into_iter()
        .filter_map(|outcome| outcome.ok())
        .filter(|outcome| outcome.status == ProbeStatus::Occupied);

    {
        let mut stdout = std::io::stdout().lock();
        writeln!(stdout, "Found hosts:").unwrap();
        for outcome in occupied {
            writeln!(stdout, "{:?}", outcome).unwrap();
        }
        writeln!(stdout, "Scan took {:?}", scan_duration).unwrap();
    }
}

§Advanced ARP Requests (Diagnostic Purposes)

In a similar fashion, more advanced ARP requests (e.g., for diagnostic purposes) can be sent using either ClientSpinner::request_batch or Client::request. The former allows for batching multiple ARP requests, while the latter provides more control over individual requests. Both approaches enable sending specific ARP requests tailored to your network diagnostic needs.

Structs§

Client
A client for handling ARP (Address Resolution Protocol) requests and probes.
ClientConfig
ClientConfigBuilder
ClientSpinner
A struct responsible for performing batch requests and probes with retry logic.
ProbeInput
ProbeInputBuilder
ProbeOutcome
RequestInput
RequestInputBuilder
RequestOutcome

Enums§

Error
InputBuildError
ProbeStatus

Type Aliases§

OpaqueError
Result