环境
- Time 2022-11-20
- WSL-Ubuntu 22.04
- Rust 1.65.0
- pnet 0.31.0
- tun-tap 0.1.3
前言
说明
参考:https://docs.rs/pnet/latest/pnet/index.html
RFC 792
目标
查看 ping 请求报文的格式,ping 基于 IP 报文。
ping 命令
ping -I tun0 172.24.49.106
root@jiangbo12490:~# ping -I tun0 172.24.49.106
PING 172.24.49.106 (172.24.49.106) from 172.24.49.244 tun0: 56(84) bytes of data.
^C
--- 172.24.49.106 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
main.rs
use pnet::packet::icmp::echo_request::EchoRequestPacket;
use pnet::packet::icmp::{IcmpPacket, IcmpTypes};
use pnet::packet::{ip::IpNextHeaderProtocols, ipv4::Ipv4Packet};
use tun_tap::{Iface, Mode};
fn main() -> std::io::Result<()> {
let iface = Iface::without_packet_info("tun0", Mode::Tun)?;
let mut buffer = vec![0; 1500];
loop {
let size = iface.recv(&mut buffer)?;
let packet = Ipv4Packet::new(&buffer).unwrap();
if packet.get_version() == 6 {
println!("IPv6 packet, continue");
continue;
}
if packet.get_next_level_protocol() != IpNextHeaderProtocols::Icmp {
println!("not icmp packet, continue");
continue;
}
// 因为头部长度的单位是 4 字节
let length = packet.get_header_length() as usize * 4;
let packet = IcmpPacket::new(&buffer[length..]).unwrap();
// 类型 8 表示 EchoRequest,0 表示回应
println!("type {:?}", packet.get_icmp_type());
// ping 程序的请求和响应都为 0
println!("code {:?}", packet.get_icmp_code());
// 校验和
println!("checksum {:?}", packet.get_checksum());
if packet.get_icmp_type() != IcmpTypes::EchoRequest {
println!("not icmp request, continue");
}
let packet = EchoRequestPacket::new(&buffer[length..]).unwrap();
// 类型 8 表示 EchoRequest,0 表示回应
println!("type {:?}", packet.get_icmp_type());
// 标识符
println!("identifier {:?}", packet.get_identifier());
// 序列号
println!("sequence_number {:?}", packet.get_sequence_number());
println!("length: {}, {:?}", size, &buffer[..size]);
}
}
程序输出
type IcmpType(8)
code IcmpCode(0)
checksum 48637
type IcmpType(8)
identifier 24612
sequence_number 1
length: 84, [69, 0, 0, 84, 194, 81, 64, 0, 64, 1, 188, 200, 172, 24, 49, 244, 172, 24, 49, 106, 8, 0, 189, 253, 96, 36, 0, 1, 241, 40, 126, 99, 0, 0, 0, 0, 170, 125, 1, 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]
总结
了解 ping 请求报文的格式,其中包含 type,code,checksum,identifier,sequence_number 等。