You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello everyone, I hope you are doing well. I'm new to Rust, and I'm trying to write a tool that provides me with the MAC address of any active node within my local network based on their respective IP address. I've written this code, but I'm having trouble sending the ARP packet. I don't quite understand why, but when I run the code, the program panics. Does anyone know how I could correct this code to make it work correctly?
The error is occurring on line 49: ts.send_to(eth_bytes, IpAddr::V4(target_ip)).expect("Failed to send ARP request");
Right at the ARP packet sending. I'd be very grateful for any assistance. Thank you so much in advanced.
use pnet::packet::arp::{Arp,ArpHardwareType,ArpOperations};use pnet::packet::ethernet::{EtherType,MutableEthernetPacket,EtherTypes,EthernetPacket};use pnet::packet::ip::IpNextHeaderProtocols;use pnet::util::MacAddr;use std::net::Ipv4Addr;use pnet::transport::{transport_channel,TransportChannelType};use std::net::IpAddr;use pnet::datalink::{self};use pnet::datalink::Channel::EthernetasOtherEthernet;fnmain(){let sender_mac = MacAddr(0x00,0x00,0x00,0x00,0x00,0x00);// Your MAC addresslet sender_ip = Ipv4Addr::new(0,0,0,0);// Your IP addresslet target_ip = Ipv4Addr::new(198,120,1,52);// Destination IP addresslet hardware_type = ArpHardwareType::new(1);// 1 is typically the value for Ethernetlet arp_packet = Arp{
hardware_type,protocol_type:EtherType(0x0800),// IPv4hw_addr_len:6,// Length of MAC addressproto_addr_len:4,// Length of IP addressoperation:ArpOperations::Request,sender_hw_addr: sender_mac,sender_proto_addr: sender_ip,target_hw_addr:MacAddr(0,0,0,0,0,0),// Can be all zeros for a requesttarget_proto_addr: target_ip,payload:Vec::new(),// You can add additional data if needed};// Create an Ethernet packet containing the ARP requestletmut ethernet_buffer = [0u8;42];// Size of an Ethernet packet with ARPletmut ethernet_packet = MutableEthernetPacket::new(&mut ethernet_buffer).unwrap();
ethernet_packet.set_destination(MacAddr(0xFF,0xFF,0xFF,0xFF,0xFF,0xFF));// Broadcast
ethernet_packet.set_source(sender_mac);
ethernet_packet.set_ethertype(EtherTypes::Arp);
ethernet_packet.set_payload(&arp_packet.payload);// Create a transport channellet(mut ts, _tr) = transport_channel(65535,TransportChannelType::Layer3(IpNextHeaderProtocols::Ipv4)).expect("Failed to create transport channel");// Convert the Ethernet packet to a byte vectorlet eth_bytes = ethernet_packet.to_immutable();// Send the packet through the transport channel
ts.send_to(eth_bytes,IpAddr::V4(target_ip)).expect("Failed to send ARP request");// 🍕HERE I HAVE THE PROBLEM (LINE 49)🍳// Get the list of available network interfaceslet interfaces = datalink::interfaces();// Choose the network interface you want to uselet interface = interfaces
.iter().find(|iface| iface.is_up() && !iface.is_loopback()).expect("No suitable network interface found");let(_,mut package_receiver) =
match datalink::channel(&interface,Default::default()){Ok(OtherEthernet(tx, rx)) => (tx, rx),
_ => panic!("Issues with the data link sender")};loop{match package_receiver.next(){Ok(packet) => {// Receive the packetlet ethernet = EthernetPacket::new(packet).unwrap();// Process the received Ethernet packetprintln!("Received Ethernet packet: {:?}", ethernet);}
_ => eprint!("Packet NOT received!"),}}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello everyone, I hope you are doing well. I'm new to Rust, and I'm trying to write a tool that provides me with the MAC address of any active node within my local network based on their respective IP address. I've written this code, but I'm having trouble sending the ARP packet. I don't quite understand why, but when I run the code, the program panics. Does anyone know how I could correct this code to make it work correctly?
The error is occurring on line 49:
ts.send_to(eth_bytes, IpAddr::V4(target_ip)).expect("Failed to send ARP request");
Right at the ARP packet sending. I'd be very grateful for any assistance. Thank you so much in advanced.
Beta Was this translation helpful? Give feedback.
All reactions