-
-
Save ironpillow/d64f644fc42a05c2bcb5e4a3dff84815 to your computer and use it in GitHub Desktop.
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
fn main() { | |
mioco::start(move || { | |
let icmp_proto = Layer3(IpNextHeaderProtocols::Icmp); | |
let (mut tx, mut rx) = match transport_channel(4096, icmp_proto) { | |
Ok((tx, rx)) => (tx, rx), | |
Err(e) => panic!("error when creating transport channel: {}", e), | |
}; | |
for i in 0..3 { | |
mioco::spawn(move || { | |
let mut packet = [0u8; 84]; | |
let mut ip_packet = build_ipv4_header(&mut packet[..]); | |
{ | |
build_icmp_packet(ip_packet.payload_mut(), i, 1000 + i + (i * 2)); | |
} | |
let l = ip_packet.get_total_length(); | |
let dst = IpAddr::V4(ip_packet.get_destination()); | |
match tx.send_to(ip_packet, dst) { | |
Ok(n) => assert_eq!(n, l as usize), | |
Err(e) => println!("failed to send packet: {}", e), | |
} | |
let mut iter = pnet::transport::ipv4_packet_iter(&mut rx); | |
match iter.next() { | |
Ok((p, _)) => { | |
let payload = p.payload(); | |
handle_icmp_packet("en0", p.get_source(), p.get_destination(), payload); | |
} | |
Err(e) => panic!("error receiving: {}", e), | |
} | |
}); | |
} | |
}) | |
.unwrap(); | |
} |
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
fn main() { | |
let number_of_pings = 3; | |
let time_between_pings = 1; | |
let ip_src = Ipv4Addr::from_str("10.20.0.155").unwrap(); | |
let ips = ["8.8.8.8", "199.59.150.7", "184.173.147.39", "108.160.172.238"]; | |
let icmp_proto = Layer3(IpNextHeaderProtocols::Icmp); | |
let (mut tx, mut rx) = match transport_channel(4096, icmp_proto) { | |
Ok((tx, rx)) => (tx, rx), | |
Err(e) => panic!("error when creating transport channel: {}", e), | |
}; | |
// listen | |
thread::spawn(move || { | |
let mut iter = pnet::transport::ipv4_packet_iter(&mut rx); | |
loop { | |
match iter.next() { | |
Ok((p, _)) => { | |
let payload = p.payload(); | |
let src_str = format!("{}", p.get_source()); | |
if p.get_destination() == ip_src && ips.contains(&src_str.as_str()) { | |
handle_icmp_packet("en0", p.get_source(), p.get_destination(), payload); | |
} | |
} | |
Err(e) => panic!("error receiving: {}", e), | |
} | |
} | |
}); | |
for (idx, ip) in ips.iter().enumerate() { | |
let id = 1000 + ((idx as u16) * 1000); | |
for i in 0..number_of_pings { | |
let mut packet = [0u8; 84]; | |
let ip_dst = Ipv4Addr::from_str(ip).unwrap(); | |
let mut ip_packet = build_ipv4_header(&mut packet[..], ip_src, ip_dst); | |
build_icmp_packet(ip_packet.payload_mut(), i, id); | |
let l = ip_packet.get_total_length(); | |
let dst = IpAddr::V4(ip_packet.get_destination()); | |
match tx.send_to(ip_packet, dst) { | |
Ok(n) => assert_eq!(n, l as usize), | |
Err(e) => println!("failed to send packet: {}", e), | |
} | |
thread::sleep(Duration::new(time_between_pings, 0)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment