152 lines
7.0 KiB
Rust
152 lines
7.0 KiB
Rust
// Create an Ipv4 connection and have it ready to send IPv4 packets
|
|
|
|
use crate::libs::args::Config;
|
|
// use pnet::packet::ethernet::{EtherTypes, EthernetPacket, MutableEthernetPacket};
|
|
use pnet::packet::ipv4;
|
|
use pnet::packet::icmp;
|
|
use pnet::packet::Packet;
|
|
// use pnet_datalink::{
|
|
// self,
|
|
// MacAddr
|
|
// };
|
|
|
|
use pnet_transport::{
|
|
icmp_packet_iter,
|
|
transport_channel,
|
|
// TransportChannelType::Layer4,
|
|
// TransportProtocol::Ipv4,
|
|
TransportChannelType::Layer3,
|
|
TransportReceiver,
|
|
TransportSender,
|
|
};
|
|
use pnet::packet::ip::{
|
|
IpNextHeaderProtocols::Icmp,IpNextHeaderProtocols,
|
|
};
|
|
use std::net::{IpAddr, Ipv4Addr};
|
|
use pnet::packet::icmp::{
|
|
echo_reply::EchoReplyPacket, echo_request::MutableEchoRequestPacket, IcmpTypes,
|
|
};
|
|
|
|
|
|
// Takes a config structure and generate a wrapper Packet, note that it will need to be filled with a payload
|
|
pub fn create_ethernet_packet<'a>(config: &'a Config) -> ipv4::MutableIpv4Packet<'a>{
|
|
|
|
let (mut tx, rx) = match transport_channel(1500, Layer3(pnet_packet::ip::IpNextHeaderProtocols::Ipv4)) {
|
|
// let (mut tx, mut rx) = match transport_channel(1500, Layer4(Ipv4(Icmp))) { // Non Raw
|
|
Ok((tx, rx)) => (tx, rx),
|
|
Err(e) => panic!("An error occurred when creating transport channel: {}", e),
|
|
};
|
|
|
|
// The total length is dependent on the packed sent
|
|
let size_tot: u16 = (config.ihl*4 + config.packet_size).into(); //84; // ihl*4+packet_size
|
|
let binding = vec![0; size_tot as usize];
|
|
let mut ethernet_packet = pnet_packet::ipv4::MutableIpv4Packet::owned(binding).unwrap();
|
|
|
|
// let mut binding = [/*0x44 ,0xd4 ,0x53 ,0x93 ,0x8a ,0x90 ,0x54 ,0xaf ,0x97 ,0x4d ,0x13 ,0xbf ,0x08 ,0x00 ,*/
|
|
// 0x45 ,0x00,
|
|
// 0x00 ,0x54 ,0xcd ,0xdf ,0x40 ,0x00 ,0x40 ,0x01 ,0xe9 ,0x74 ,0xc0 ,0xa8 ,0x01 ,0x03 ,0xc0 ,0xa8,
|
|
// 0x01 ,0x01 ,0x08 ,0x00 ,0xba ,0x4c ,0x00 ,0x02 ,0x00 ,0x02 ,0xed ,0x04 ,0x95 ,0x63 ,0x00 ,0x00,
|
|
// 0x00 ,0x00 ,0xf6 ,0x73 ,0x06 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x10 ,0x11 ,0x12 ,0x13 ,0x14 ,0x15,
|
|
// 0x16 ,0x17 ,0x18 ,0x19 ,0x1a ,0x1b ,0x1c ,0x1d ,0x1e ,0x1f ,0x20 ,0x21 ,0x22 ,0x23 ,0x24 ,0x25,
|
|
// 0x26 ,0x27 ,0x28 ,0x29 ,0x2a ,0x2b ,0x2c ,0x2d ,0x2e ,0x2f ,0x30 ,0x31 ,0x32 ,0x33 ,0x34 ,0x35,
|
|
// 0x36, 0x37];
|
|
// let rethernet_packet = pnet_packet::ipv4::MutableIpv4Packet::new(&mut binding).unwrap();
|
|
|
|
// IPv4
|
|
ethernet_packet.set_total_length(size_tot);
|
|
ethernet_packet.set_version(config.ipv4_version);
|
|
ethernet_packet.set_header_length(config.ihl);
|
|
ethernet_packet.set_dscp(config.dcsp);
|
|
ethernet_packet.set_ecn(config.ecn);
|
|
ethernet_packet.set_ttl(config.ttl);
|
|
ethernet_packet.set_identification(config.identification);
|
|
ethernet_packet.set_flags(config.flags);
|
|
ethernet_packet.set_fragment_offset(config.offset);
|
|
ethernet_packet.set_next_level_protocol(pnet_packet::ip::IpNextHeaderProtocols::Icmp);//pnet_packet::ip::IpNextHeaderProtocol(6));
|
|
|
|
// Get the IP address of the interface
|
|
// let ip_addr = interface.ips.into_iter().next().unwrap();
|
|
//config.src_addr.parse
|
|
ethernet_packet.set_source(config.src_addr.parse::<Ipv4Addr>().expect("Couldn't parse source address"));
|
|
ethernet_packet.set_destination(config.dest_addr.parse::<Ipv4Addr>().expect("Couldn't parse destination address"));
|
|
|
|
ethernet_packet.set_checksum(ipv4::checksum(ðernet_packet.to_immutable()));
|
|
|
|
|
|
// Send the Ethernet frame using the `ethernet::EthernetPacket::send` function
|
|
// println!("{:?} {:?}",rethernet_packet.packet(), rethernet_packet);
|
|
// assert_eq!(rethernet_packet.packet(),[69, 0, 0, 84, 205, 223, 64, 0, 64, 1, 233, 116, 192, 168, 1, 3, 192, 168, 1, 1, 8, 0, 186, 76, 0, 2, 0, 2, 237, 4, 149, 99, 0, 0, 0, 0, 246, 115, 6, 0, 0, 0, 0, 0, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55]);
|
|
|
|
// let res = tx.send_to(rethernet_packet, std::net::IpAddr::V4("192.168.1.1".parse::<Ipv4Addr>().expect("Couldn't parse source address")));
|
|
// println!("{:?}",res);
|
|
|
|
ethernet_packet
|
|
|
|
}
|
|
|
|
// Create a transmiter/receiver interface with the send_to, recv methods
|
|
pub fn create_interface(config: &Config) -> (TransportSender,TransportReceiver) {
|
|
// Get the default network interface // Should allow for selection
|
|
// let interfaces = pnet_datalink::interfaces();
|
|
// let interface = interfaces.into_iter().next().unwrap();
|
|
|
|
let (tx, rx) = match transport_channel(1500, Layer3(pnet_packet::ip::IpNextHeaderProtocols::Ipv4)) {
|
|
// let (mut tx, mut rx) = match transport_channel(1500, Layer4(Ipv4(Icmp))) { // Non Raw
|
|
Ok((tx, rx)) => (tx, rx),
|
|
Err(e) => panic!("An error occurred when creating transport channel: {}", e),
|
|
};
|
|
(tx,rx)
|
|
}
|
|
|
|
pub fn send_packet(packet: ipv4::Ipv4Packet, mut interface: TransportSender, dst: Ipv4Addr) -> Result<usize,std::io::Error>{
|
|
let res = interface.send_to(packet, std::net::IpAddr::V4(dst));
|
|
println!("{:?}",res);
|
|
res
|
|
}
|
|
|
|
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(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),
|
|
};
|
|
|
|
|
|
let mut recv = icmp_packet_iter(&mut rx);
|
|
|
|
// 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 = 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));
|
|
// // let frozen = binding.clone();
|
|
// packet.set_checksum(pnet_packet::icmp::checksum(&packet.to_immutable()));
|
|
// // Get a `Packet` object representing the ping request packet
|
|
// let ping_request_packet = packet.packet();
|
|
// let IDENT_NUM: u16 = 111;
|
|
// Create a new mutable Ethernet packet containaing the ICMP 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),
|
|
// }
|
|
} |