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
impl Client
Sourcepub fn new(config: ClientConfig) -> Result<Self>
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.
Sourcepub async fn probe(&self, input: ProbeInput) -> Result<ProbeOutcome>
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.
Sourcepub async fn request(&self, input: RequestInput) -> Result<RequestOutcome>
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.