Chinaunix首页 | 论坛 | 博客
  • 博客访问: 163204
  • 博文数量: 67
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 622
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-19 19:12
文章分类

全部博文(67)

分类: C/C++

2014-11-19 19:58:34

pcap文件格式是bpf保存原始数据包的格式,很多软件都在使用,比如tcpdump、wireshark等等,
了解pcap格式可以加深对原始数据包的了解,自己也可以手工构造任意的数据包进行测试。
 
pcap文件的格式为:
  文件头    24字节
  数据包头 + 数据包  数据包头为16字节,后面紧跟数据包
  数据包头 + 数据包  ......
 
pcap.h里定义了文件头的格式
struct pcap_file_header {
        bpf_u_int32 magic;
        u_short version_major;
        u_short version_minor;
        bpf_int32 thiszone;     /* gmt to local correction */
        bpf_u_int32 sigfigs;    /* accuracy of timestamps */
        bpf_u_int32 snaplen;    /* max length saved portion of each pkt */
        bpf_u_int32 linktype;   /* data link type (LINKTYPE_*) */
};
各字段的含义:
 1、magic:   4字节 pcap文件标识 目前为“d4 c3 b2 a1”
 2、major:   2字节 主版本号     #define PCAP_VERSION_MAJOR 2
 3、minor:   2字节 次版本号     #define PCAP_VERSION_MINOR 4
 4、thiszone:4字节 时区修正     并未使用,目前全为0
 5、sigfigs: 4字节 精确时间戳   并未使用,目前全为0
 6、snaplen: 4字节 抓包最大长度 如果要抓全,设为0x0000ffff(65535),
          tcpdump -s 0就是设置这个参数,缺省为68字节
 7、linktype:4字节 链路类型    一般都是1:ethernet


       以下是数据值与链路层类型的对应表
      0            BSD       loopback devices, except for later OpenBSD
      1            Ethernet, and Linux loopback devices   以太网类型,大多数的数据包为这种类型。
      6            802.5 Token Ring
      7            ARCnet
      8            SLIP
      9            PPP
      10          FDDI
      100        LLC/SNAP-encapsulated ATM
      101        raw IP, with no link
      102        BSD/OS SLIP
      103        BSD/OS PPP
      104        Cisco HDLC
      105        802.11
      108        later OpenBSD loopback devices (with the AF_value in network byte order)
      113               special Linux cooked capture
      114               LocalTalk

|    magic    |major  | minor |   thiszone  |   sigfigs   |   snaplen   |  linktype   |
| d4 c3 b2 a1 | 02 00 | 04 00 | 00 00 00 00 | 00 00 00 00 | ff ff 00 00 | 01 00 00 00 |
 
数据包头的格式
struct pcap_pkthdr {
        struct timeval ts;      /* time stamp */
        bpf_u_int32 caplen;     /* length of portion present */
        bpf_u_int32 len;        /* length this packet (off wire) */
};
struct timeval {
        long            tv_sec;         /* seconds (XXX should be time_t) */
        suseconds_t     tv_usec;        /* and microseconds */
};
 ts:    8字节 抓包时间 4字节表示秒数,4字节表示微秒数
 caplen:4字节 保存下来的包长度(最多是snaplen,比如68字节)
 len:   4字节 数据包的真实长度,如果文件中保存的不是完整数据包,可能比caplen大
 
了解了pcap文件格式,就可以自己手工构造任意数据包了,可以以录好的包为基础,
用十六进制编辑器打开进行修改。

阅读(2195) | 评论(0) | 转发(0) |
0

上一篇:libpcap中文说明

下一篇:freeswitch运行

给主人留下些什么吧!~~