Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to convert MutableArpPacket to &[u8] , so that it can be set_payload to MutableEthernetPacket #614

Open
swithun-liu opened this issue Mar 11, 2023 · 5 comments

Comments

@swithun-liu
Copy link

        let target_mac = MacAddr::new(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
        let target_ip = Ipv4Addr::new(192, 168, 1, 1);

        let source_mac = MacAddr::new(0, 0, 0, 0, 0, 0);
        let source_ip = Ipv4Addr::new(0, 0, 0, 0);

        let mut arp_buffer = [0u8; 42];
        let mut arp_packet = pnet::packet::arp::MutableArpPacket::new(&mut arp_buffer[..]).unwrap();
        arp_packet.set_hardware_type(pnet::packet::arp::ArpHardwareTypes::Ethernet);
        arp_packet.set_protocol_type(pnet::packet::ethernet::EtherTypes::Ipv4);
        arp_packet.set_hw_addr_len(6);
        arp_packet.set_proto_addr_len(4);
        arp_packet.set_operation(pnet::packet::arp::ArpOperations::Request);
        arp_packet.set_sender_hw_addr(source_mac);
        arp_packet.set_sender_proto_addr(source_ip);
        arp_packet.set_target_hw_addr(target_mac);
        arp_packet.set_target_proto_addr(target_ip);

        let mut ether_buffer = [0u8; 42];
        let mut ether_packet = pnet::packet::ethernet::MutableEthernetPacket::new(&mut ether_buffer).unwrap();

        ether_packet.set_destination(target_mac);
        ether_packet.set_source(source_mac);
        ether_packet.set_ethertype(pnet::packet::ethernet::EtherTypes::Arp);
        ether_packet.set_payload( ^^^^^^^^^ here, how to handle arp_packet ^^^^^^^^^^);
@ZeroNoFun
Copy link
Contributor

ether_packet.set_payload(arp_packet.packet());

or

ether_packet.set_payload(&arp_buffer);

should do the trick

@swithun-liu
Copy link
Author

swithun-liu commented Mar 15, 2023

 let test_arp = pnet::packet::arp::Arp {
            hardware_type: pnet::packet::arp::ArpHardwareTypes::Ethernet,
            protocol_type: pnet::packet::ethernet::EtherTypes::Ipv4,
            hw_addr_len: 6,
            proto_addr_len: 4,
            operation: pnet::packet::arp::ArpOperations::Request,
            sender_hw_addr: *source_mac.as_bytes(),
            sender_proto_addr: source_ip,
            target_hw_addr: target_mac,
            target_proto_addr: target_ip,
            payload: vec![],
        };

        let test_eth = pnet::packet::ethernet::Ethernet {
            destination: target_mac,
            source: source_mac,
            ethertype: pnet::packet::ethernet::EtherTypes::Arp,
            payload:  ,
        };

image

the new version cannot find MutableArpPacket, it just has Arp, like above
and I tried ether_packet.set_payload(arp_packet.packet()); it says packet() is not a function, it's a private variable

and I tried ether_packet.set_payload(&arp_buffer); it panic, i dont known why

but the same question,how to convert Arp to vec, it's too hard

i just want to find all device's ip in LAN

@ZeroNoFun
Copy link
Contributor

ZeroNoFun commented Mar 15, 2023

and I tried ether_packet.set_payload(&arp_buffer); it panic, i dont known why

it's panicking probably because ether_buffer is too small. Try to make it a little larger something like

let mut ether_buffer = [0u8; 100];

And see if it still panics.

EDIT:

the new version cannot find MutableArpPacket, it just has Arp, like above

Types like MutableArpPacket, ArpPacket e.t.c are generated with macro magic stuff. This is probably why your IDE couldn't find these types.

@stappersg
Copy link
Contributor

i just want to find all device's ip in LAN

stappers@alpaca:~
$ ip neigh show
172.24.0.1 dev eth0 lladdr 24:65:11:a9:b5:43 REACHABLE
172.24.0.15 dev eth0 lladdr 00:e1:6d:8f:6b:16 STALE
172.24.0.21 dev eth0  FAILED
172.24.0.25 dev eth0 lladdr 00:1e:06:49:14:e9 REACHABLE
172.24.0.53 dev eth0 lladdr 02:03:0a:02:32:38 STALE
172.24.0.44 dev eth0 lladdr b4:f1:da:da:e0:08 STALE
172.24.0.17 dev eth0 lladdr 00:04:13:84:06:87 STALE
172.24.0.45 dev eth0 lladdr b4:f1:da:da:e0:08 STALE
172.24.0.4 dev eth0 lladdr e0:46:9a:4e:64:53 STALE
172.24.0.43 dev eth0 lladdr 00:d4:9e:10:e0:be STALE
172.24.0.36 dev eth0 lladdr 48:d2:24:a5:cd:5e STALE
172.24.0.40 dev eth0 lladdr e4:b9:7a:90:02:8f STALE
172.24.0.37 dev eth0 lladdr e4:b9:7a:90:02:8f REACHABLE
172.24.0.41 dev eth0 lladdr e8:78:29:c1:fc:8c STALE
2a02:a46d:659e:1:ca34:3411:4890:f618 dev eth0 lladdr 00:d4:9e:10:e0:be STALE
fe80::21e:6ff:fe49:14e9 dev eth0 lladdr 00:1e:06:49:14:e9 STALE
fe80::17e7:fcd:18bc:2996 dev eth0 lladdr e4:b9:7a:90:02:8f STALE
fe80::8e5d:a7b8:9e96:d4fb dev eth0 lladdr e8:78:29:c1:fc:8c STALE
fe80::b6f1:daff:feda:e008 dev eth0 lladdr b4:f1:da:da:e0:08 STALE
fe80::2b0:d0ff:feed:7357 dev eth0 lladdr 00:b0:d0:ed:73:57 router STALE
fe80::204:13ff:fe84:687 dev eth0 lladdr 00:04:13:84:06:87 STALE
fe80::2665:11ff:fea9:b543 dev eth0 lladdr 24:65:11:a9:b5:43 router STALE
fe80::bb8b:86a5:4354:6206 dev eth0 lladdr 00:d4:9e:10:e0:be STALE
stappers@alpaca:~
$ /usr/sbin/arp -an   # for old fart sake
? (172.24.0.1) at 24:65:11:a9:b5:43 [ether] on eth0
? (172.24.0.15) at 00:e1:6d:8f:6b:16 [ether] on eth0
? (172.24.0.21) at <incomplete> on eth0
? (172.24.0.25) at 00:1e:06:49:14:e9 [ether] on eth0
? (172.24.0.53) at 02:03:0a:02:32:38 [ether] on eth0
? (172.24.0.44) at b4:f1:da:da:e0:08 [ether] on eth0
? (172.24.0.17) at 00:04:13:84:06:87 [ether] on eth0
? (172.24.0.45) at b4:f1:da:da:e0:08 [ether] on eth0
? (172.24.0.4) at e0:46:9a:4e:64:53 [ether] on eth0
? (172.24.0.43) at 00:d4:9e:10:e0:be [ether] on eth0
? (172.24.0.36) at 48:d2:24:a5:cd:5e [ether] on eth0
? (172.24.0.40) at e4:b9:7a:90:02:8f [ether] on eth0
? (172.24.0.37) at e4:b9:7a:90:02:8f [ether] on eth0
? (172.24.0.41) at e8:78:29:c1:fc:8c [ether] on eth0
stappers@alpaca:~
$ 

Thing I'm trying to tell: "dealing with arp packages" and "having an arp cache" are two seperate things.

@swithun-liu are you sure you are on the right track? (I think you are (because learning) and I think that you should rethink your path to your goal.)

@swithun-liu
Copy link
Author

@stappersg I want to broadcast ff-ff-ff-ff-ff-ff to in Lan, and then the devices send back response, then I will get it's ip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants