Comments (2)
use std::net::Ipv4Addr;
use pnet::{packet::{arp::{ArpHardwareType, ArpOperation}, ethernet::EtherType}, util::MacAddr};
fn main() {
// debug options
std::env::set_var("RUST_BACKTRACE", "1");
// get all the interfaces and everything
let interfac = pnet::datalink::interfaces()[3].clone();
println!("the interface name is {}",interfac.name);
let (mut tx, mut _rx) = match pnet::datalink::channel(&interfac, Default::default()) {
Ok(pnet::datalink::Channel::Ethernet(tx, rx)) => (tx, rx),
Ok(_) => panic!("Unhandled channel type"),
Err(e) => panic!("An error occurred when creating the datalink channel: {}", e)
};
// this is the place to make packets
let mut etherbuff =[0u8;64];
let mut arpbuff = [0u8;32];
// the arp packet
let mut my_arp_packet = pnet::packet::arp::MutableArpPacket::new(&mut arpbuff).expect("could not create arp packet");
my_arp_packet.set_operation(ArpOperation::new(1)); // 1 for sending two for a request
my_arp_packet.set_hardware_type(ArpHardwareType::new(6)); // 6 for wifi , 1 for ethernet
my_arp_packet.set_protocol_type(EtherType::new(0x0800)); //network protocol type for the request, ipv4 here
my_arp_packet.set_hw_addr_len(6); // the length in octets for the length address , 6 for wifi and eth
my_arp_packet.set_proto_addr_len(4);// four octets for ipv4 address
my_arp_packet.set_sender_hw_addr(MacAddr::new(0x9c, 0x29, 0x76, 0x7a, 0x70, 0x7e)); // ny mac
my_arp_packet.set_sender_proto_addr(Ipv4Addr::new(192, 168, 0, 174)); // my ip , used to fill arp caches without sending an arp request
//my_arp_packet.set_target_hw_addr(MacAddr::broadcast()); // broadcast
my_arp_packet.set_target_proto_addr(Ipv4Addr::new(192, 168, 0, 199)); // who's ever ip adder you want
// the ethernet packet
let mut my_ethernet_packet=pnet::packet::ethernet::MutableEthernetPacket::new(&mut etherbuff).unwrap();
my_ethernet_packet.set_destination(MacAddr::broadcast()); // send to everyone
my_ethernet_packet.set_source(MacAddr::new(0x9c,0x29,0x76,0x7a,0x70,0x7e)); // my mac
my_ethernet_packet.set_ethertype(EtherType::new(2));// for ethernet
my_ethernet_packet.set_payload(&arpbuff);
// handle the errors and send the thing
match tx.send_to(ðerbuff, None) {
Some(ho)=> match ho {
Ok(_the_val) => println!("it transfered i think"),
Err(the_error) => println!("{}",the_error.to_string())
} ,
None => println!("hmmm")
};
}
from libpnet.
Hi @zubairdroid I don't know if you found an answer but if you didn't, the EtherType is probably what you got wrong. The EtherType should be Arp.
I would recommend that you use present enum values in libpnet such as EtherTypes::Arp
instead of EtherType::new(2)
and ArpOperations::Request
instead of ArpOperation::new(1)
. This will make your work easier instead of typing them out manually. So this, for , this is how I would format your arp packet(some adjustments have been made, so you can fix those)
use pnet::{packet::{arp::{ArpHardwareTypes, ArpOperations}, ethernet::EtherTypes, Packet}, util::MacAddr};
fn main(){
// custom code...
let mut etherbuff = [0u8; 42];
let mut arpbuff = [0u8; 28];
let (mut tx, _rx) = match pnet::datalink::channel(&interface, Default::default()) {
Ok(pnet::datalink::Channel::Ethernet(tx, rx)) => (tx, rx),
Ok(_) => panic!("Unhandled channel type"),
Err(e) => panic!("An error occurred when creating the datalink channel: {}", e)
};
let mut my_arp_packet = pnet::packet::arp::MutableArpPacket::new(&mut arpbuff).expect("could not create arp packet");
my_arp_packet.set_operation(ArpOperations::Request);
my_arp_packet.set_hardware_type(ArpHardwareTypes::Ethernet);
my_arp_packet.set_protocol_type(EtherTypes::Ipv4);
my_arp_packet.set_hw_addr_len(6);
my_arp_packet.set_proto_addr_len(4);
my_arp_packet.set_sender_hw_addr(interface.mac.unwrap());
my_arp_packet.set_sender_proto_addr(Ipv4Addr::new(192, 168, 100, 16));
my_arp_packet.set_target_proto_addr(Ipv4Addr::new(192, 168, 100, 14));
let mut my_ethernet_packet=pnet::packet::ethernet::MutableEthernetPacket::new(&mut etherbuff).unwrap();
my_ethernet_packet.set_destination(MacAddr::broadcast());
my_ethernet_packet.set_source(MacAddr::new(0x9c,0x29,0x76,0x7a,0x70,0x7e));
my_ethernet_packet.set_ethertype(EtherTypes::Arp);// for ethernet
my_ethernet_packet.set_payload(my_arp_packet.packet());
tx.send_to(ðerbuff, Some(interface.clone()));
}
Of course, please edit this to your specifications. Good luck coding !!!
from libpnet.
Related Issues (20)
- Crashed in `bpf.rs:416:44` HOT 2
- Proposal: Create User-Friendly Tutorial for Installing Packet.lib on Windows HOT 1
- Misaligned pointer dereference in safe functions `backend::channel` and `sockaddr_to_network_addr`
- Passing libpnet `Packet` struct between functions is difficult HOT 4
- tx.send_to(&[0u8], None) fail
- ICMP Destination unreachable Next-hop MTU is not exposed
- Lacks Mac OS Apple Silicon support? HOT 3
- Errors on android HOT 4
- How to send icmp packets? HOT 2
- Datalink Layer Level-2 Passthru example?
- Support for 802.11/WLAN packets
- Dhcp example HOT 1
- next_with_timeout() with zero timeout waits indefinitely HOT 1
- a problem with the packet.lib configuration!help me. HOT 3
- Potential multiplication overflow when calculating length of NdpOptionPacket
- [Question] getting random ip addresses on get_source/destination HOT 1
- macOS kernel panic caused by `send_to` HOT 1
- what happen ? unresolve IPv4Packet HOT 3
- Can't finish compiling on nightly version of rust
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libpnet.