Initial commit
This commit is contained in:
parent
7e42cf3a88
commit
b0f15f37ee
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "rping"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
|
@ -0,0 +1,106 @@
|
|||
use std::env;
|
||||
use std::io::{self};
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
|
||||
|
||||
fn main() {
|
||||
// Parse the command line arguments.
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() == 1 || args.contains(&"--help".to_string()) {
|
||||
eprintln!("Usage: cargo run --dest <destination IP address> [--request <ping request>] [--data <data>] [--payload <payload>]");
|
||||
std::process::exit(1);
|
||||
}
|
||||
// if args.len() < 2 {
|
||||
// eprintln!("Error: expected at least one argument (the destination IP address)");
|
||||
// std::process::exit(1);
|
||||
// }
|
||||
let dest_ip = args[1].parse::<Ipv4Addr>().unwrap();
|
||||
let mut ping_request = None;
|
||||
let mut data = None;
|
||||
let mut payload = None;
|
||||
for (i, arg) in args[1..].iter().enumerate() {
|
||||
if arg == "--request" || arg == "-r" {
|
||||
ping_request = Some(args[i + 1].parse::<u16>().unwrap());
|
||||
} else if arg == "--data" || arg == "-d" {
|
||||
data = Some(args[i + 1].parse::<u16>().unwrap());
|
||||
} else if arg == "--payload" || arg == "-p" {
|
||||
payload = Some(args[i + 1].as_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
let ping_request = ping_request.unwrap_or(0x0800); // Default ping request value
|
||||
let data = data.unwrap_or(0x0000); // Default data value
|
||||
let payload = payload.unwrap_or(b""); // Default payload value
|
||||
|
||||
// Create a UDP socket.
|
||||
let socket = UdpSocket::bind("0.0.0.0:8080").unwrap();
|
||||
|
||||
// Set the socket to be non-blocking.
|
||||
socket.set_nonblocking(true).unwrap();
|
||||
|
||||
// Set the Time-To-Live (TTL) value for the socket.
|
||||
socket.set_ttl(64).unwrap();
|
||||
|
||||
// Create the ping request message.
|
||||
let mut buf = [0u8; 64];
|
||||
let ping_request = [
|
||||
(ping_request >> 8) as u8, (ping_request & 0xff) as u8, // Type and Code fields
|
||||
0x00, 0x00, // Checksum (filled in later)
|
||||
0x00, 0x00, 0x00, 0x00, // Identifier and Sequence Number fields
|
||||
];
|
||||
buf[..ping_request.len()].copy_from_slice(&ping_request);
|
||||
|
||||
// Add the data to the ping request message.
|
||||
let data = [
|
||||
(data >> 8) as u8, (data & 0xff) as u8,
|
||||
];
|
||||
buf[ping_request.len()..(ping_request.len() + data.len())].copy_from_slice(&data);
|
||||
buf[ping_request.len() + data.len()..(ping_request.len() + data.len()+payload.len())].copy_from_slice(&payload);
|
||||
|
||||
// Calculate the checksum of the ping request message.
|
||||
let checksum = {
|
||||
let mut sum = 0;
|
||||
for i in 0..(ping_request.len() / 2) {
|
||||
sum += (ping_request[2 * i] as u16) << 8 | ping_request[2 * i + 1] as u16;
|
||||
}
|
||||
!(sum as u16)
|
||||
};
|
||||
buf[2] = (checksum >> 8) as u8;
|
||||
buf[3] = (checksum & 0xff) as u8;
|
||||
|
||||
// Send the ping request to the specified destination IP address.
|
||||
let dest_addr = SocketAddr::new(IpAddr::V4(dest_ip), 8080);
|
||||
socket.send_to(&buf, dest_addr).unwrap();
|
||||
|
||||
// Wait for a response from the destination.
|
||||
let mut buf = [0; 64];
|
||||
let timeout = std::time::Duration::from_secs(1);
|
||||
socket.set_read_timeout(Some(timeout)).unwrap();
|
||||
let (bytes_recv, _src_addr) = match socket.recv_from(&mut buf) {
|
||||
Ok(res) => res,
|
||||
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => {
|
||||
println!("No response from destination");
|
||||
println!("Error {:?}", err);
|
||||
std::process::exit(0);
|
||||
}
|
||||
Err(err) => panic!("Error receiving data: {}", err),
|
||||
};
|
||||
|
||||
// Print the response from the destination.
|
||||
// print!("{} bytes from {}: icmp_seq={} ",
|
||||
// Parse the response from the destination.
|
||||
let icmp_type = buf[0];
|
||||
let icmp_code = buf[1];
|
||||
let icmp_checksum = ((buf[2] as u16) << 8) | (buf[3] as u16);
|
||||
let icmp_id = ((buf[4] as u16) << 8) | (buf[5] as u16);
|
||||
let icmp_seq = ((buf[6] as u16) << 8) | (buf[7] as u16);
|
||||
let data = &buf[0..bytes_recv];
|
||||
|
||||
// Print the parsed response.
|
||||
println!("icmp_type={} icmp_code={} icmp_checksum={:04x}", icmp_type, icmp_code, icmp_checksum);
|
||||
println!("icmp_id={} icmp_seq={}", icmp_id, icmp_seq);
|
||||
print!("data=");
|
||||
for b in data {
|
||||
print!("{:02x}", b);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
Loading…
Reference in New Issue