Struct cross_socket::packet::arp::ArpPacket
source · pub struct ArpPacket {
pub hardware_type: ArpHardwareType,
pub protocol_type: EtherType,
pub operation: ArpOperation,
pub sender_hw_addr: MacAddr,
pub sender_proto_addr: Ipv4Addr,
pub target_hw_addr: MacAddr,
pub target_proto_addr: Ipv4Addr,
pub payload: Vec<u8>,
}
Expand description
Represents an ARP packet.
Fields§
§hardware_type: ArpHardwareType
§protocol_type: EtherType
§operation: ArpOperation
§sender_hw_addr: MacAddr
§sender_proto_addr: Ipv4Addr
§target_hw_addr: MacAddr
§target_proto_addr: Ipv4Addr
§payload: Vec<u8>
Implementations§
source§impl ArpPacket
impl ArpPacket
sourcepub fn from_bytes(packet: &[u8]) -> ArpPacket
pub fn from_bytes(packet: &[u8]) -> ArpPacket
Examples found in repository?
examples/datalink_socket/arp.rs (line 43)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let interface: Interface = cross_socket::interface::get_default_interface().unwrap();
// Create new socket
let mut socket: DataLinkSocket = DataLinkSocket::new(interface, false).unwrap();
// Create packet info for ARP request
let mut packet_info = PacketInfo::new();
packet_info.src_mac = socket.interface.mac_addr.clone().unwrap();
packet_info.dst_mac = MacAddr::zero();
packet_info.ether_type = cross_socket::packet::ethernet::EtherType::Arp;
packet_info.src_ip = IpAddr::V4(socket.interface.ipv4[0].addr);
packet_info.dst_ip = socket.interface.gateway.clone().unwrap().ip_addr;
// Build ARP packet
let arp_packet = builder::build_arp_packet(packet_info);
// Send ARP request to default gateway
match socket.send_to(&arp_packet) {
Ok(packet_len) => {
println!("Sent {} bytes", packet_len);
}
Err(e) => {
println!("Error: {}", e);
}
}
let src_mac = socket.interface.mac_addr.clone().unwrap();
// Receive packets
println!("Waiting for ARP reply... ");
loop {
match socket.receive() {
Ok(packet) => {
let ethernet_packet = ethernet::EthernetPacket::from_bytes(&packet);
if ethernet_packet.ethertype != cross_socket::packet::ethernet::EtherType::Arp {
continue;
}
let arp_packet = cross_socket::packet::arp::ArpPacket::from_bytes(ðernet_packet.payload);
if arp_packet.sender_hw_addr.address() != src_mac.address() {
println!("Received {} bytes from {}", packet.len(), arp_packet.sender_hw_addr.address());
println!("Packet: {:?}", arp_packet);
break;
}
}
Err(e) => {
println!("Error: {}", e);
}
}
}
}
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for ArpPacket
impl Send for ArpPacket
impl Sync for ArpPacket
impl Unpin for ArpPacket
impl UnwindSafe for ArpPacket
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more