diff --git a/src/main.rs b/src/main.rs index ae8391f..804d471 100644 --- a/src/main.rs +++ b/src/main.rs @@ -141,7 +141,8 @@ fn main() { // Get the default network interface let interfaces = pnet_datalink::interfaces(); let interface = interfaces.into_iter().next().unwrap(); - let (mut tx, mut rx) = match transport_channel(1500, Layer3(IpNextHeaderProtocols::Udp)) { + // let (mut tx, mut rx) = match transport_channel(1500, Layer3(IpNextHeaderProtocols::Udp)) { + let (mut tx, mut rx) = match transport_channel(1500, Layer3(pnet_packet::ip::IpNextHeaderProtocols::Ipv4)) { // let (mut tx, mut rx) = match transport_channel(1500, Layer4(Ipv4(Icmp))) { Ok((tx, rx)) => (tx, rx), Err(e) => panic!("An error occurred when creating transport channel: {}", e), @@ -156,8 +157,8 @@ fn main() { // Create a new mutable ICMP packet let mut binding = [4;64]; - let mut packet = icmp::MutableIcmpPacket::new(&mut binding).expect("Couldn't create packet"); - + // let mut packet = icmp::MutableIcmpPacket::new(&mut binding).expect("Couldn't create packet"); + let mut packet = pnet_packet::ipv4::MutableIpv4Packet::new(&mut binding).expect("Couldn't create packet"); // // Set the ICMP packet type and code to the appropriate values for a ping request // packet.set_icmp_type(icmp::IcmpTypes::EchoRequest); // packet.set_icmp_code(pnet::packet::icmp::IcmpCode(0xaa)); @@ -168,15 +169,36 @@ fn main() { let IDENT_NUM: u16 = 111; // Create a new mutable Ethernet packet containaing the ICMP packet let mut binding = [3; 64+32]; - let mut ethernet_packet = MutableEthernetPacket::new(&mut binding).unwrap(); + // let mut ethernet_packet = MutableEthernetPacket::new(&mut binding).unwrap(); + let mut ethernet_packet = pnet_packet::ipv4::MutableIpv4Packet::new(&mut binding).unwrap(); // ethernet_packet.set_icmp_type(); // Set the destination and source MAC addresses - ethernet_packet.set_destination(interface.mac.unwrap().clone()); - ethernet_packet.set_source(interface.mac.unwrap()); + // ethernet_packet.set_destination(interface.mac.unwrap().clone()); + // // ethernet_packet.set_source(interface.mac.unwrap()); + + ethernet_packet.set_version(4); + ethernet_packet.set_header_length(5); + ethernet_packet.set_dscp(0); + ethernet_packet.set_ecn(0); + ethernet_packet.set_ttl(64); + ethernet_packet.set_identification(8); + ethernet_packet.set_flags(1); + ethernet_packet.set_next_level_protocol(pnet_packet::ip::IpNextHeaderProtocols::Ipv4);//pnet_packet::ip::IpNextHeaderProtocol(6)); + + ethernet_packet.set_source("192.168.1.3".parse::().expect("Couldn't set destination")); + ethernet_packet.set_destination("192.168.1.1".parse::().expect("Couldn't set destination")); + + ethernet_packet.set_checksum(ethernet_packet.get_checksum()); + println!("Lgh {}",ethernet_packet.get_total_length()); + ethernet_packet.set_total_length(110); + + + // // ethernet_packet.set_source(interface.mac.unwrap()); + // Set the EtherType to indicate that the payload is an IP packet - ethernet_packet.set_ethertype(pnet::packet::ethernet::EtherType(0x0000)); + // ethernet_packet.set_ethertype(pnet::packet::ethernet::EtherType(0x0000)); // Set the payload of the Ethernet packet to the ping request packet // ethernet_packet.set_payload(ping_request_packet); @@ -186,23 +208,23 @@ fn main() { // Send the Ethernet frame using the `ethernet::EthernetPacket::send` function let dst = "192.168.1.1".parse::().expect(""); - println!("{:?} {:?}",ethernet_packet.packet(), ethernet_packet.payload()); + println!("{:?} {:?}",ethernet_packet.packet(), ethernet_packet); let res = tx.send_to(ethernet_packet, std::net::IpAddr::V4(dst)); println!("{:?}",res); - match recv.next() { - Ok((pkt, addr)) => { - let pong = EchoReplyPacket::new(pkt.packet()).unwrap(); - if pong.get_icmp_type() == IcmpTypes::EchoReply - && pong.get_identifier() == IDENT_NUM - && pong.get_sequence_number() == 1 - { - () - } else { - () - } - } - Err(e) => eprintln!("Error: {} ", e), - } + // match recv.next() { + // Ok((pkt, addr)) => { + // let pong = EchoReplyPacket::new(pkt.packet()).unwrap(); + // if pong.get_icmp_type() == IcmpTypes::EchoReply + // && pong.get_identifier() == IDENT_NUM + // && pong.get_sequence_number() == 1 + // { + // () + // } else { + // () + // } + // } + // Err(e) => eprintln!("Error: {} ", e), + // } } // use std::io; // use std::os::unix::io::AsRawFd;