Giter Site home page Giter Site logo

Packets not sending about libpnet HOT 2 OPEN

zubairdroid avatar zubairdroid commented on June 3, 2024
Packets not sending

from libpnet.

Comments (2)

zubairdroid avatar zubairdroid commented on June 3, 2024
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(&etherbuff, 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.

Paul-weqe avatar Paul-weqe commented on June 3, 2024

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(&etherbuff, Some(interface.clone()));

}

Of course, please edit this to your specifications. Good luck coding !!!

from libpnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.