Struct Client

Source
pub struct Client { /* private fields */ }
Expand description

A client for handling ARP (Address Resolution Protocol) requests and probes.

The Client is responsible for sending ARP requests, caching responses, and handling notifications. It uses a raw packet stream for network communication.

§Example

use async_arp::{Client, ClientConfig};
use std::time::Duration;

let config = ClientConfig {
    interface_name: "eth0".to_string(),
    response_timeout: Duration::from_secs(2),
    cache_timeout: Duration::from_secs(60),
};

let client = Client::new(config).expect("Failed to create ARP client");

Implementations§

Source§

impl Client

Source

pub fn new(config: ClientConfig) -> Result<Self>

Creates a new Client with the given configuration.

This function initializes a raw packet stream, binds it to the specified network interface, and sets up caching and background tasks for listening to ARP responses.

§Errors

Returns an error if the packet stream cannot be created or if binding to the specified network interface fails.

Source

pub async fn probe(&self, input: ProbeInput) -> Result<ProbeOutcome>

Probes for the presence of a device at the given IP address.

This function sends an ARP request to determine whether an IP address is occupied. It returns a ProbeOutcome, indicating whether the address is in use.

§Example
use async_arp::{Client, ClientConfigBuilder, ProbeStatus, ProbeInputBuilder};
use pnet::util::MacAddr;
use std::net::Ipv4Addr;

let probe_input = ProbeInputBuilder::new()
    .with_sender_mac(MacAddr::new(0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E))
    .with_target_ip(Ipv4Addr::new(192, 168, 1, 1))
    .build()
    .expect("Failed to build probe input");
tokio_test::block_on(async {
    let client = Client::new(ClientConfigBuilder::new("eth0").build()).unwrap();
    let outcome = client.probe(probe_input).await.unwrap();
    match outcome.status {
        ProbeStatus::Occupied => println!("IP is in use"),
        ProbeStatus::Free => println!("IP is available"),
}
})
§Errors

Returns an error if sending the ARP request fails.

Source

pub async fn request(&self, input: RequestInput) -> Result<RequestOutcome>

Sends an ARP request and waits for a response.

If the requested IP is already cached, the cached response is returned immediately. Otherwise, a new ARP request is sent, and the client waits for a response within the configured timeout period.

§Example
use pnet::util::MacAddr;
use std::net::Ipv4Addr;
use async_arp::{Client, ClientConfigBuilder, RequestInputBuilder};

let request_input = RequestInputBuilder::new()
    .with_sender_ip(Ipv4Addr::new(192, 168, 1, 100))
    .with_sender_mac(MacAddr::new(0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E))
    .with_target_ip(Ipv4Addr::new(192, 168, 1, 1))
    .with_target_mac(MacAddr::zero())
    .build()
    .expect("Failed to build request input");
tokio_test::block_on(async {
    let client = Client::new(ClientConfigBuilder::new("eth0").build()).unwrap();
    let outcome = client.request(request_input).await.unwrap();

    println!("Received response: {:?}", outcome);
})
§Errors

Returns an error if sending the request fails or if no response is received within the timeout period.

Trait Implementations§

Source§

impl Debug for Client

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.