0222-使用网络层协议

环境

  • 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 791

目标

了解网络层的数据包格式。

main.rs

use pnet::packet::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;
        }

        // 第一个字节,服务版本,头长度,单位是4字节
        println!("version: {}", packet.get_version());
        println!("Internet Header Length: {}", packet.get_header_length());
        // 第二个字节,最初为 Type of Service,因为很少使用,修改成下面的了
        println!("differentiated services code point: {}", packet.get_dscp());
        println!("Explicit Congestion Notification: {}", packet.get_ecn());
        // 第三四个字节,总长度
        println!("Total Length: {}", packet.get_total_length());
        // 第五六个字节,分片标识
        println!("Identification: {}", packet.get_identification());
        // 7 到 12 后面来看

        // 13到16个字节,源 IP 地址
        println!("Source Address: {}", packet.get_source());
        // 17到20个字节,目的 IP 地址
        println!("Destination Address: {}", packet.get_destination());

        // 其它选项或者填充,最大60个字节-已有20,剩余40字节
        println!("options {:?}", packet.get_options_raw());
        println!("length: {}, {:?}", size, &buffer[..size]);
    }
}

抓包结果

使用 ping 命令抓包,ping -I tun0 172.24.51.243

root@jiangbo12490:~# tcpdump -X -i tun0
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on tun0, link-type RAW (Raw IP), snapshot length 262144 bytes

23:00:40.110205 IP 172.24.51.244 > 172.24.51.243: ICMP echo request, id 14785, seq 1, length 64
        0x0000:  4500 0054 2546 4000 4001 554b ac18 33f4  E..T%F@.@.UK..3.
        0x0010:  ac18 33f3 0800 f574 39c1 0001 18e4 7c63  ..3....t9.....|c
        0x0020:  0000 0000 73ae 0100 0000 0000 1011 1213  ....s...........
        0x0030:  1415 1617 1819 1a1b 1c1d 1e1f 2021 2223  .............!"#
        0x0040:  2425 2627 2829 2a2b 2c2d 2e2f 3031 3233  $%&'()*+,-./0123
        0x0050:  3435 3637                                4567

程序输出

version: 4
Internet Header Length: 5
differentiated services code point: 0
Explicit Congestion Notification: 0
Total Length: 84
Identification: 10898
Source Address: 172.24.51.244
Destination Address: 172.24.51.243
options []
length: 84, [69, 0, 0, 84, 42, 146, 64, 0, 64, 1, 79, 255, 172, 24, 51, 244, 172, 24, 51, 243, 8, 0, 110, 107, 108, 80, 0, 1, 238, 228, 124, 99, 0, 0, 0, 0, 230, 39, 13, 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]

可以看到头部长度为 20 字节,还填充了 64 字节的数据。

总结

了解了网络层的 IP 协议,认识了其中几个字节的数据所代表的含义。

附录

posted @   jiangbo4444  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-08-13 【JavaScript】Object 实例属性
点击右上角即可分享
微信分享提示