解析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;    
        bpf_u_int32 sigfigs;   
        bpf_u_int32 snaplen;   
        bpf_u_int32 linktype;  
};

Pcap文件头24B各字段说明:

Magic:4B:0×1A 2B 3C 4D:用来识别文件自己和字节顺序。0xa1b2c3d4用来表示按照原来的顺序读取,0xd4c3b2a1表示下面的字节都要交换顺序读取。一般,我们使用0xa1b2c3d4
Major:2B,0×02 00:当前文件主要的版本号
Minor:2B,0×04 00当前文件次要的版本号
ThisZone:4B 时区。GMT和本地时间的相差,用秒来表示。如果本地的时区是GMT,那么这个值就设置为0.这个值一般也设置为0 SigFigs:4B时间戳的精度;全零
SnapLen:4B最大的存储长度(该值设置所抓获的数据包的最大长度,如果所有数据包都要抓获,将该值设置为65535; 例如:想获取数据包的前64字节,可将该值设置为64)
LinkType:4B链路类型
常用类型:
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


Packet 包头和Packet数据组成
字段说明:
Timestamp:时间戳高位,精确到seconds(值是自从January 1, 1970 00:00:00 GMT以来的秒数来记)
Timestamp:时间戳低位,精确到microseconds (数据包被捕获时候的微秒(microseconds)数,是自ts-sec的偏移量)
Caplen:当前数据区的长度,即抓取到的数据帧长度,由此可以得到下一个数据帧的位置。
Len:离线数据长度 网络中实际数据帧的长度,一般不大于caplen,多数情况下和Caplen数值相等。
(例如,实际上有一个包长度是1500 bytes(L en=1500),但是因为在Global Header的 snaplen=1300有限制,所以只能抓取这个包的前1300个字节,这个时候, Caplen = 1300 )
Packet 数据:即 Packet(通常就是链路层的数据帧)具体内容,长度就是Caplen,这个长度的后面,就是当前PCAP文件中存放的下一个Packet数据包,也就 是说:PCAP文件里面并没有规定捕获的Packet数据包之间有什么间隔字符串,下一组数据在文件中的起始位置。我们需要靠第一个Packet包确定。 最后,Packet数据部分的格式其实就是标准的网路协议格式了可以任何网络教材上找得到。



[cpp]  view plain  copy
  1.   
[cpp]  view plain  copy
  1. //  
  2. //  pcap.h  
  3. //  pcaptest  
  4. //  
  5. //  Created by zc on 12-1-24.  
  6. //  Copyright 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.    
  9. #ifndef pcaptest_pcap_h  
  10. #define pcaptest_pcap_h  
  11.    
  12. typedef unsigned int  bpf_u_int32;  
  13. typedef unsigned short  u_short;  
  14. typedef int bpf_int32;  
  15.    
  16. /* 
  17.  Pcap文件头24B各字段说明: 
  18.  Magic:4B:0x1A 2B 3C 4D:用来标示文件的开始 
  19.  Major:2B,0x02 00:当前文件主要的版本号      
  20.  Minor:2B,0x04 00当前文件次要的版本号 
  21.  ThisZone:4B当地的标准时间;全零 
  22.  SigFigs:4B时间戳的精度;全零 
  23.  SnapLen:4B最大的存储长度     
  24.  LinkType:4B链路类型 
  25.  常用类型: 
  26.   0            BSD loopback devices, except for later OpenBSD 
  27.  1            Ethernet, and Linux loopback devices 
  28.  6            802.5 Token Ring 
  29.  7            ARCnet 
  30.  8            SLIP 
  31.  9            PPP 
  32.  */  
  33. typedef struct pcap_file_header {  
  34.     bpf_u_int32 magic;  
  35.     u_short version_major;  
  36.     u_short version_minor;  
  37.     bpf_int32 thiszone;      
  38.     bpf_u_int32 sigfigs;     
  39.     bpf_u_int32 snaplen;     
  40.     bpf_u_int32 linktype;    
  41. }pcap_file_header;  
  42.    
  43. /* 
  44.  Packet 包头和Packet数据组成 
  45.  字段说明: 
  46.  Timestamp:时间戳高位,精确到seconds      
  47.  Timestamp:时间戳低位,精确到microseconds 
  48.  Caplen:当前数据区的长度,即抓取到的数据帧长度,由此可以得到下一个数据帧的位置。 
  49.  Len:离线数据长度:网络中实际数据帧的长度,一般不大于caplen,多数情况下和Caplen数值相等。 
  50.  Packet 数据:即 Packet(通常就是链路层的数据帧)具体内容,长度就是Caplen,这个长度的后面,就是当前PCAP文件中存放的下一个Packet数据包,也就 是说:PCAP文件里面并没有规定捕获的Packet数据包之间有什么间隔字符串,下一组数据在文件中的起始位置。我们需要靠第一个Packet包确定。 
  51.  */  
  52.    
  53. typedef struct  timestamp{  
  54.     bpf_u_int32 timestamp_s;  
  55.     bpf_u_int32 timestamp_ms;  
  56. }timestamp;  
  57.    
  58. typedef struct pcap_header{  
  59.     timestamp ts;  
  60.     bpf_u_int32 capture_len;  
  61.     bpf_u_int32 len;  
  62.    
  63. }pcap_header;  
  64.    
  65.    
  66. void prinfPcapFileHeader(pcap_file_header *pfh);  
  67. void printfPcapHeader(pcap_header *ph);  
  68. void printPcap(void * data,size_t size);  
  69.    
  70. #endif  



[cpp]  view plain  copy
  1. //  
  2. //  pcap.c  
  3. //  pcaptest  
  4. //  
  5. //  Created by zc on 12-1-24.  
  6. //  Copyright 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.    
  9. #include <stdio.h>  
  10. #include "pcap.h"  
  11.    
  12. void prinfPcapFileHeader(pcap_file_header *pfh){  
  13.     if (pfh==NULL) {  
  14.         return;  
  15.     }  
  16.     printf("=====================\n"  
  17.            "magic:0x%0x\n"  
  18.            "version_major:%u\n"  
  19.            "version_minor:%u\n"  
  20.            "thiszone:%d\n"  
  21.            "sigfigs:%u\n"  
  22.            "snaplen:%u\n"  
  23.            "linktype:%u\n"  
  24.            "=====================\n",  
  25.            pfh->magic,  
  26.            pfh->version_major,  
  27.            pfh->version_minor,  
  28.            pfh->thiszone,  
  29.            pfh->sigfigs,  
  30.            pfh->snaplen,  
  31.            pfh->linktype);  
  32. }  
  33.    
  34. void printfPcapHeader(pcap_header *ph){  
  35.     if (ph==NULL) {  
  36.         return;  
  37.     }  
  38.     printf("=====================\n"  
  39.            "ts.timestamp_s:%u\n"  
  40.            "ts.timestamp_ms:%u\n"  
  41.            "capture_len:%u\n"  
  42.            "len:%d\n"  
  43.            "=====================\n",  
  44.            ph->ts.timestamp_s,  
  45.            ph->ts.timestamp_ms,  
  46.            ph->capture_len,  
  47.            ph->len);  
  48.    
  49.    
  50. }  
  51.    
  52. void printPcap(void * data,size_t size){  
  53.     unsigned  short iPos = 0;  
  54.     //int * p = (int *)data;  
  55.     //unsigned short* p = (unsigned short *)data;  
  56.     if (data==NULL) {  
  57.         return;  
  58.     }  
  59.    
  60.     printf("\n==data:0x%x,len:%lu=========",data,size);  
  61.    
  62.     for (iPos=0; iPos < size/sizeof(unsigned short); iPos++) {  
  63.         //printf(" %x ",(int)( * (p+iPos) ));  
  64.         //unsigned short a = ntohs(p[iPos]);  
  65.    
  66.         unsigned short a = ntohs( *((unsigned short *)data + iPos ) );  
  67.         if (iPos%8==0) printf("\n");  
  68.         if (iPos%4==0) printf(" ");  
  69.    
  70.         printf("%04x",a);  
  71.    
  72.    
  73.     }  
  74.     /* 
  75.      for (iPos=0; iPos <= size/sizeof(int); iPos++) { 
  76.         //printf(" %x ",(int)( * (p+iPos) )); 
  77.         int a = ntohl(p[iPos]); 
  78.   
  79.         //int a = ntohl( *((int *)data + iPos ) ); 
  80.         if (iPos %4==0) printf("\n"); 
  81.   
  82.         printf("%08x ",a); 
  83.   
  84.   
  85.     } 
  86.      */  
  87.     printf("\n============\n");  
  88. }  



[cpp]  view plain  copy
  1. //  
  2. //  main.c  
  3. //  pcaptest  
  4. //  
  5. //  Created by zc on 12-1-24.  
  6. //  Copyright 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.    
  9. #include <stdio.h>  
  10. #include <arpa/inet.h>  
  11. #include "pcap.h"  
  12.    
  13. #define PCAP_FILE "ping.pcap"  
  14. #define MAX_ETH_FRAME 1514  
  15. #define ERROR_FILE_OPEN_FAILED -1  
  16. #define ERROR_MEM_ALLOC_FAILED -2  
  17. #define ERROR_PCAP_PARSE_FAILED -3  
  18.    
  19.    
  20. int main (int argc, const char * argv[])  
  21. {  
  22.    
  23.     printf("sizeof:int %lu,unsigned int %lu,char %lu,unsigned char %lu,short:%lu,unsigned short:%lu\n",  
  24.             sizeof(int),sizeof(unsigned int),sizeof(char),sizeof(unsigned char),sizeof(short),sizeof(unsigned short));  
  25.    
  26.     pcap_file_header  pfh;  
  27.     pcap_header  ph;  
  28.     int count=0;  
  29.     void * buff = NULL;  
  30.     int readSize=0;  
  31.     int ret = 0;  
  32.    
  33.     FILE *fp = fopen(PCAP_FILE, "rw");  
  34.    
  35.     if (fp==NULL) {  
  36.         fprintf(stderr, "Open file %s error.",PCAP_FILE);  
  37.         ret = ERROR_FILE_OPEN_FAILED;  
  38.         goto ERROR;  
  39.     }  
  40.    
  41.     fread(&pfh, sizeof(pcap_file_header), 1, fp);     
  42.     prinfPcapFileHeader(&pfh);  
  43.     //fseek(fp, 0, sizeof(pcap_file_header));  
  44.    
  45.     buff = (void *)malloc(MAX_ETH_FRAME);  
  46.     for (count=1; ; count++) {  
  47.         memset(buff,0,MAX_ETH_FRAME);  
  48.         //read pcap header to get a packet  
  49.         //get only a pcap head count .  
  50.         readSize=fread(&ph, sizeof(pcap_header), 1, fp);  
  51.         if (readSize<=0) {  
  52.             break;  
  53.         }  
  54.         printfPcapHeader(&ph);  
  55.    
  56.    
  57.         if (buff==NULL) {  
  58.             fprintf(stderr, "malloc memory failed.\n");  
  59.             ret = ERROR_MEM_ALLOC_FAILED;  
  60.             goto ERROR;  
  61.         }  
  62.    
  63.         //get a packet contents.  
  64.         //read ph.capture_len bytes.  
  65.         readSize=fread(buff,1,ph.capture_len, fp);  
  66.         if (readSize != ph.capture_len) {  
  67.             free(buff);  
  68.             fprintf(stderr, "pcap file parse error.\n");  
  69.             ret = ERROR_PCAP_PARSE_FAILED;  
  70.             goto ERROR;  
  71.         }  
  72.         printPcap(buff, ph.capture_len);  
  73.    
  74.    
  75.         printf("===count:%d,readSize:%d===\n",count,readSize);  
  76.    
  77.         if (feof(fp) || readSize <=0 ) {   
  78.             break;  
  79.         }  
  80.     }  
  81.    
  82. ERROR:  
  83.     //free  
  84.     if (buff) {  
  85.         free(buff);  
  86.         buff=NULL;  
  87.     }   
  88.     if (fp) {  
  89.         fclose(fp);  
  90.         fp=NULL;  
  91.     }     
  92.    
  93.     return ret;  
  94. }  



Makefile


[cpp]  view plain  copy
  1. objects = main.o pcap.o  
  2.    
  3. pcaptest : $(objects)  
  4.     gcc -o pcaptest  $(objects)  
  5.    
  6. main.o:pcap.h  
  7. pcap.o:pcap.h  
  8.    
  9. .PHONY : clean  
  10. clean :  
  11.     rm pcaptest  $(objects)  

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值