如何用C++实现java读取pcap文件.pcap包文件

5120人阅读
Linux(11)
C/C++(11)
pcap文件的格式为:
&&文件头 &&&24字节
& 数据报头 + 数据报&&数据包头为16字节,后面紧跟数据报
&&数据报头 + 数据报& ......
pcap.h里定义了文件头的格式
struct pcap_file_header {
&&&&&&& bpf_u_int32
&&&&&&& u_short version_
&&&&&&& u_short version_
&&&&&&& bpf_int32&&&&
&&&&&&& bpf_u_int32&&&
&&&&&&& bpf_u_int32&&&
&&&&&&& bpf_u_int32&&
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, :00 GMT以来的秒数来记)
Timestamp:时间戳低位,精确到microseconds (数据包被捕获时候的微秒(microseconds)数,是自ts-sec的偏移量)
Caplen:当前数据区的长度,即抓取到的数据帧长度,由此可以得到下一个数据帧的位置。
Len:离线数据长度:网络中实际数据帧的长度,一般不大于caplen,多数情况下和Caplen数值相等。
(例如,实际上有一个包长度是1500 bytes(Len=1500),但是因为在Global Header的snaplen=1300有限制,所以只能抓取这个包的前1300个字节,这个时候,Caplen&= 1300 )
Packet&数据:即 Packet(通常就是链路层的数据帧)具体内容,长度就是Caplen,这个长度的后面,就是当前PCAP文件中存放的下一个Packet数据包,也就 是说:PCAP文件里面并没有规定捕获的Packet数据包之间有什么间隔字符串,下一组数据在文件中的起始位置。我们需要靠第一个Packet包确定。 最后,Packet数据部分的格式其实就是标准的网路协议格式了可以任何网络教材上找得到。
Created by zc on 12-1-24.
Copyright 2012年 __MyCompanyName__. All rights reserved.
#ifndef pcaptest_pcap_h
#define pcaptest_pcap_h
typedef unsigned int
bpf_u_int32;
typedef unsigned short
typedef int bpf_int32;
Pcap文件头24B各字段说明:
Magic:4B:0x1A 2B 3C 4D:用来标示文件的开始
Major:2B,0x02 00:当前文件主要的版本号
Minor:2B,0x04 00当前文件次要的版本号
ThisZone:4B当地的标准时间;全零
SigFigs:4B时间戳的精度;全零
SnapLen:4B最大的存储长度
LinkType:4B链路类型
常用类型:
BSD loopback devices, except for later OpenBSD
Ethernet, and Linux loopback devices
802.5 Token Ring
typedef struct pcap_file_header {
bpf_u_int32
u_short version_
u_short version_
bpf_u_int32
bpf_u_int32
bpf_u_int32
}pcap_file_
Packet 包头和Packet数据组成
字段说明:
Timestamp:时间戳高位,精确到seconds
Timestamp:时间戳低位,精确到microseconds
Caplen:当前数据区的长度,即抓取到的数据帧长度,由此可以得到下一个数据帧的位置。
Len:离线数据长度:网络中实际数据帧的长度,一般不大于caplen,多数情况下和Caplen数值相等。
Packet 数据:即 Packet(通常就是链路层的数据帧)具体内容,长度就是Caplen,这个长度的后面,就是当前PCAP文件中存放的下一个Packet数据包,也就 是说:PCAP文件里面并没有规定捕获的Packet数据包之间有什么间隔字符串,下一组数据在文件中的起始位置。我们需要靠第一个Packet包确定。
typedef struct
timestamp{
bpf_u_int32 timestamp_s;
bpf_u_int32 timestamp_
typedef struct pcap_header{
bpf_u_int32 capture_
bpf_u_int32
void prinfPcapFileHeader(pcap_file_header *pfh);
void printfPcapHeader(pcap_header *ph);
void printPcap(void * data,size_t size);
Created by zc on 12-1-24.
Copyright 2012年 __MyCompanyName__. All rights reserved.
#include &stdio.h&
#include &pcap.h&
void prinfPcapFileHeader(pcap_file_header *pfh){
if (pfh==NULL) {
printf(&=====================\n&
&magic:0x%0x\n&
&version_major:%u\n&
&version_minor:%u\n&
&thiszone:%d\n&
&sigfigs:%u\n&
&snaplen:%u\n&
&linktype:%u\n&
&=====================\n&,
pfh-&magic,
pfh-&version_major,
pfh-&version_minor,
pfh-&thiszone,
pfh-&sigfigs,
pfh-&snaplen,
pfh-&linktype);
void printfPcapHeader(pcap_header *ph){
if (ph==NULL) {
printf(&=====================\n&
&ts.timestamp_s:%u\n&
&ts.timestamp_ms:%u\n&
&capture_len:%u\n&
&len:%d\n&
&=====================\n&,
ph-&ts.timestamp_s,
ph-&ts.timestamp_ms,
ph-&capture_len,
void printPcap(void * data,size_t size){
short iPos = 0;
//int * p = (int *)
//unsigned short* p = (unsigned short *)
if (data==NULL) {
printf(&\n==data:0x%x,len:%lu=========&,data,size);
for (iPos=0; iPos & size/sizeof(unsigned short); iPos++) {
//printf(& %x &,(int)( * (p+iPos) ));
//unsigned short a = ntohs(p[iPos]);
unsigned short a = ntohs( *((unsigned short *)data + iPos ) );
if (iPos%8==0) printf(&\n&);
if (iPos%4==0) printf(& &);
printf(&%04x&,a);
for (iPos=0; iPos &= size/sizeof(int); iPos++) {
//printf(& %x &,(int)( * (p+iPos) ));
int a = ntohl(p[iPos]);
//int a = ntohl( *((int *)data + iPos ) );
if (iPos %4==0) printf(&\n&);
printf(&%08x &,a);
printf(&\n============\n&);
Created by zc on 12-1-24.
Copyright 2012年 __MyCompanyName__. All rights reserved.
#include &stdio.h&
#include &arpa/inet.h&
#include &pcap.h&
#define PCAP_FILE &ping.pcap&
#define MAX_ETH_FRAME 1514
#define ERROR_FILE_OPEN_FAILED -1
#define ERROR_MEM_ALLOC_FAILED -2
#define ERROR_PCAP_PARSE_FAILED -3
int main (int argc, const char * argv[])
printf(&sizeof:int %lu,unsigned int %lu,char %lu,unsigned char %lu,short:%lu,unsigned short:%lu\n&,
sizeof(int),sizeof(unsigned int),sizeof(char),sizeof(unsigned char),sizeof(short),sizeof(unsigned short));
pcap_file_
int count=0;
void * buff = NULL;
int readSize=0;
int ret = 0;
FILE *fp = fopen(PCAP_FILE, &rw&);
if (fp==NULL) {
fprintf(stderr, &Open file %s error.&,PCAP_FILE);
ret = ERROR_FILE_OPEN_FAILED;
goto ERROR;
fread(&pfh, sizeof(pcap_file_header), 1, fp);
prinfPcapFileHeader(&pfh);
//fseek(fp, 0, sizeof(pcap_file_header));
buff = (void *)malloc(MAX_ETH_FRAME);
for (count=1; ; count++) {
memset(buff,0,MAX_ETH_FRAME);
//read pcap header to get a packet
//get only a pcap head count .
readSize=fread(&ph, sizeof(pcap_header), 1, fp);
if (readSize&=0) {
printfPcapHeader(&ph);
if (buff==NULL) {
fprintf(stderr, &malloc memory failed.\n&);
ret = ERROR_MEM_ALLOC_FAILED;
goto ERROR;
//get a packet contents.
//read ph.capture_len bytes.
readSize=fread(buff,1,ph.capture_len, fp);
if (readSize != ph.capture_len) {
free(buff);
fprintf(stderr, &pcap file parse error.\n&);
ret = ERROR_PCAP_PARSE_FAILED;
goto ERROR;
printPcap(buff, ph.capture_len);
printf(&===count:%d,readSize:%d===\n&,count,readSize);
if (feof(fp) || readSize &=0 ) {
if (buff) {
free(buff);
buff=NULL;
fclose(fp);
objects = main.o pcap.o
pcaptest : $(objects)
gcc -o pcaptest
$(objects)
main.o:pcap.h
pcap.o:pcap.h
.PHONY : clean
rm pcaptest
$(objects)
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:17939次
排名:千里之外
转载:19篇
(1)(2)(1)(3)(6)(3)(1)(4)本帖子已过去太久远了,不再提供回复功能。求助 如何读取.pcap包文件?
求助 如何读取.pcap包文件?
RT : & & 由Wireshark抓包软件从网络环境上抓包,保存为pakt.pcap包文件。 如何用C/C++读取 该文件 ,将里面的包内容读出来(一次读取一个包)放到测试程序中解包分析?? 已经知道pcap文件的文件头格式为: typedef struct PCAP_FILE_HEADER { DWORD WORD versionM WORD versionM DWORD thisZ DWORD DWORD snapL DWORD linkT }PcapFileHeader, *PPcapFileH 数据包的包头格式: typedef struct PCAP_PKTHDR { TimeV DWORD capL& & & & & & //包总长 DWORD actualL //可能会比caplen的值大 实际长 }PcapPkthdr, *PPcapP 如何读取该pakt.pcap文件?
发表评论:
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&[转]&pcap分析IP包 - 开源中国社区
当前访客身份:游客 [
当前位置:
发布于 日 20时,
&无详细内容&
代码片段(1)
get_ip_code.c&~&8KB&&&&
/* 文件名字:get_ip_code.c */
#include "pcap.h"
-----------------------------------------------------------------------------------------------------------------------
Libpcap头文件 ;
下面是以太网协议格式
-----------------------------------------------------------------------------------------------------------------------
struct ether_header
u_int8_t ether_dhost[6];
/* 目的以太网地址 */
u_int8_t ether_shost[6];
/* 源以太网地址 */
u_int16_t ether_
/* 以太网类型 */
/* 下面是IP地址格式 */
typedef u_int32_t in_addr_t;
struct in_addr
in_addr_t s_
-----------------------------------------------------------------------------------------------------------------------
下面是IP协议格式
-----------------------------------------------------------------------------------------------------------------------
struct ip_header
#ifdef WORDS_BIGENDIAN
u_int8_t ip_version: 4,
/* IP协议版本 */
ip_header_length: 4; /* IP协议首部长度 */
u_int8_t ip_header_length: 4, ip_version: 4;
u_int8_t ip_
/* TOS服务质量 */
u_int16_t ip_
/* 总长度 */
u_int16_t ip_
/* 标识 */
u_int16_t ip_
/* 偏移 */
u_int8_t ip_
/* 生存时间 */
u_int8_t ip_
/* 协议类型 */
u_int16_t ip_
/* 校验和 */
struct in_addr ip_souce_
/* 源IP地址 */
struct in_addr ip_destination_
/* 目的IP地址 */
=======================================================================================================================
下面是实现IP数据包分析的函数定义
=======================================================================================================================
void ip_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)
struct ip_header *ip_
/* IP协议变量 */
u_int header_
/* 长度 */
/* 偏移 */
/* 服务质量 */
/* 校验和 */
ip_protocol = (struct ip_header*)(packet_content + 14);
/* 获得IP协议数据内容,去掉以太网头 */
checksum = ntohs(ip_protocol-&ip_checksum);
/* 获得校验和 */
header_length = ip_protocol-&ip_header_length *4;
/* 获得长度 */
tos = ip_protocol-&ip_
/* 获得TOS */
offset = ntohs(ip_protocol-&ip_off);
/* 获得偏移量 */
printf("----------- IP Protocol
(Network Layer)
-----------\n");
printf("IP Version:%d\n", ip_protocol-&ip_version);
printf("Header length:%d\n", header_length);
printf("TOS:%d\n", tos);
printf("Total length:%d\n", ntohs(ip_protocol-&ip_length));
/* 获得总长度 */
printf("Identification:%d\n", ntohs(ip_protocol-&ip_id));
/* 获得标识 */
printf("Offset:%d\n", (offset &0x1fff) *8);
printf("TTL:%d\n", ip_protocol-&ip_ttl);
/* 获得TTL */
printf("Protocol:%d\n", ip_protocol-&ip_protocol);
/* 获得协议类型 */
switch (ip_protocol-&ip_protocol) /* 判断协议类型的值 */
printf("The Transport Layer Protocol is TCP\n");
/* 如果协议类型为6,表示上层协议为TCP协议 */
printf("The Transport Layer Protocol is UDP\n");
/* 如果协议类型为17,表示上层协议为UDP协议 */
printf("The Transport Layer Protocol is ICMP\n");
/* 如果协议类型为1,表示传上层协议为ICMP协议 */
printf("Header checksum:%d\n", checksum);
printf("Source address:%s\n", inet_ntoa(ip_protocol-&ip_souce_address));
/* 获得源IP地址 */
printf("Destination address:%s\n", inet_ntoa(ip_protocol-&ip_destination_address));
/* 获得目的IP地址 */
=======================================================================================================================
下面是回调函数,实现捕获和分析以太网协议
=======================================================================================================================
void ethernet_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)
u_short ethernet_
/* 以太网类型 */
struct ether_header *ethernet_
/* 以太网协议变量 */
u_char *mac_
static int packet_number = 1;
printf("**************************************************\n");
printf("The %d IP
packet is captured.\n", packet_number);
printf("-------------
Ehternet Protocol (Link Layer)
------------\n");
ethernet_protocol = (struct ether_header*)packet_
/* 获得以太网协议数据内容 */
printf("Ethernet type is :\n");
ethernet_type = ntohs(ethernet_protocol-&ether_type);
/* 获得以太网类型 */
printf("%04x\n", ethernet_type);
switch (ethernet_type) /* 判断以太网类型的值 */
case 0x0800:
printf("The network layer is IP protocol\n");
/* 如果为0x0800,表示上层协议为IP协议 */
case 0x0806:
printf("The network layer is ARP protocol\n");
/* 如果为0x0806,表示上层协议为ARP协议 */
case 0x8035:
printf("The network layer is RARP potocol\n");
/* 如果为0x8035,表示上层协议为RARP协议 */
printf("Mac Source Address is : \n");
mac_string = ethernet_protocol-&ether_
printf("%02x:%02x:%02x:%02x:%02x:%02x\n", *mac_string, *(mac_string + 1), *(mac_string + 2), *(mac_string + 3), *(mac_string + 4), *(mac_string + 5));
/* 获得源以太网地址 */
printf("Mac Destination Address is : \n");
mac_string = ethernet_protocol-&ether_
printf("%02x:%02x:%02x:%02x:%02x:%02x\n", *mac_string, *(mac_string + 1), *(mac_string + 2), *(mac_string + 3), *(mac_string + 4), *(mac_string + 5));
/* 获得目的以太网地址 */
switch (ethernet_type)
case 0x0800:
ip_protocol_packet_callback(argument, packet_header, packet_content);
* 如果上层协议是IP协议,就调用分析IP协议的函数对IP协议进行分析。注意,此时传递的参数,跟回调函数的参数是一样的,表示代表同一个数据包
printf("**************************************************\n");
packet_number++;
void main()
pcap_t *pcap_
/* Libpcap句柄 */
char error_content[PCAP_ERRBUF_SIZE];
/* 错误信息 */
char *net_
/* 网络接口 */
struct bpf_program bpf_
/* bpf过滤规则 */
char bpf_filter_string[] = "ip";
/* 过滤规则字符串,此时表示本程序只是捕获IP协议的网络数据包 */
bpf_u_int32 net_
/* 网络掩码 */
bpf_u_int32 net_
/* 网络地址 */
net_interface = pcap_lookupdev(error_content);
/* 获得网络接口 */
pcap_lookupnet(net_interface, &net_ip, &net_mask, error_content);
/* 获得网路地址和掩码 */
pcap_handle = pcap_open_live(net_interface, BUFSIZ, 1, 0, error_content);
/* 打开网络接口 */
pcap_compile(pcap_handle, &bpf_filter, bpf_filter_string, 0, net_ip);
/* 编译BPF过滤规则 */
pcap_setfilter(pcap_handle, &bpf_filter);
/* 设置过滤规则 */
if (pcap_datalink(pcap_handle) != DLT_EN10MB)
pcap_loop(pcap_handle,
- 1, ethernet_protocol_packet_callback, NULL);
/* 注册回调函数,循环捕获数据包 */
pcap_close(pcap_handle);
/* 关闭Libpcap操作 */
开源中国-程序员在线工具:
开源从代码分享开始
wokaokeji的其它代码posts - 4,&
comments - 10,&
trackbacks - 0
原文:./index.php/archives/34
转载请注明出处。来自 hello xiexh ()
这是进公司写的一个练手程序,程序功能为解析由Wireshark生成的pcap文件。
实现步骤:
1)用Wireshark软件抓包得到test.pcap文件
2)程序:分析pcap文件头 -& 分析pcap_pkt头 -& 分析帧头 -& 分析ip头 -& 分析tcp头 -& 分析http信息
#include&stdio.h&
#include&string.h&
#include&stdlib.h&
#include&netinet/in.h&
#include&time.h&
#define BUFSIZE 10240
#define STRSIZE 1024
typedef long
bpf_int32;
typedef unsigned long
bpf_u_int32;
typedef unsigned short &u_
typedef unsigned long
typedef unsigned short u_int16;
typedef unsigned char
//pacp文件头结构体
struct pcap_file_header
bpf_u_int32 & & & /* 0xa1b2c3d4 */
u_short version_ & /* magjor Version 2 */
u_short version_ & /* magjor Version 4 */
bpf_int32 & & &/* gmt to local correction */
bpf_u_int32 & & /* accuracy of timestamps */
bpf_u_int32 & & /* max length saved portion of each pkt */
bpf_u_int32 & &/* data link type (LINKTYPE_*) */
struct time_val
long tv_ & & & & /* seconds 含义同 time_t 对象的值 */
long tv_ & & & &/* and microseconds */
//pcap数据包头结构体
struct pcap_pkthdr
struct time_ &/* time stamp */
bpf_u_int32 /* length of portion present */
bpf_u_int32 & &/* length this packet (off wire) */
//数据帧头
typedef struct FramHeader_t
//Pcap捕获的数据帧头
u_int8 DstMAC[6];
//目的MAC地址
u_int8 SrcMAC[6];
//源MAC地址
u_short FrameT & &//帧类型
} FramHeader_t;
//IP数据报头
typedef struct IPHeader_t
//IP数据报头
Ver_HL & & & //版本+报头长度
TOS; & & & & & &//服务类型
TotalL & & & //总长度
Flag_S & //标志+片偏移
TTL; & & & & & &//生存周期
P & & & //协议类型
C & & & //头部校验和
//源IP地址
//目的IP地址
} IPHeader_t;
//TCP数据报头
typedef struct TCPHeader_t
//TCP数据报头
//目的端口
//数据报头的长度(4 bit) + 保留(4 bit)
//标识TCP不同的控制消息
//窗口大小
UrgentP &//紧急指针
}TCPHeader_t;
void match_http(FILE *fp, char *head_str, char *tail_str, char *buf, int total_len);
//查找 http 信息函数
int main()
struct pcap_file_header *file_
struct pcap_pkthdr *ptk_
IPHeader_t *ip_
TCPHeader_t *tcp_
FILE *fp, *
int & pkt_offset, i=0;
ip_len, http_len, ip_
src_port, dst_port, tcp_
buf[BUFSIZE], my_time[STRSIZE];
char src_ip[STRSIZE], dst_ip[STRSIZE];
char &host[STRSIZE], uri[BUFSIZE];
file_header = (struct pcap_file_header *)malloc(sizeof(struct pcap_file_header));
ptk_header &= (struct pcap_pkthdr *)malloc(sizeof(struct pcap_pkthdr));
= (IPHeader_t *)malloc(sizeof(IPHeader_t));
tcp_header = (TCPHeader_t *)malloc(sizeof(TCPHeader_t));
memset(buf, 0, sizeof(buf));
if((fp = fopen(&test.pcap&,&r&)) == NULL)
printf(&error: can not open pcap file\n&);
if((output = fopen(&output.txt&,&w+&)) == NULL)
printf(&error: can not open output file\n&);
//开始读数据包
pkt_offset = 24; //pcap文件头结构 24个字节
while(fseek(fp, pkt_offset, SEEK_SET) == 0)
//遍历数据包
//pcap_pkt_header 16 byte
if(fread(ptk_header, 16, 1, fp) != 1) //读pcap数据包头结构
printf(&\nread end of pcap file\n&);
pkt_offset += 16 + ptk_header-& & //下一个数据包的偏移值
strftime(my_time, sizeof(my_time), &%Y-%m-%d %T&, localtime(&(ptk_header-&ts.tv_sec)));
//获取时间
printf(&%d: %s\n&, i, my_time);
//数据帧头 14字节
fseek(fp, 14, SEEK_CUR); //忽略数据帧头
//IP数据报头 20字节
if(fread(ip_header, sizeof(IPHeader_t), 1, fp) != 1)
printf(&%d: can not read ip_header\n&, i);
inet_ntop(AF_INET, (void *)&(ip_header-&SrcIP), src_ip, 16);
inet_ntop(AF_INET, (void *)&(ip_header-&DstIP), dst_ip, 16);
= ip_header-&P
= ip_header-&TotalL //IP数据报总长度
printf(&%d: &src=%s\n&, i, src_ip);
if(ip_proto != 0&06)
//判断是否是 TCP 协议
//TCP头 20字节
if(fread(tcp_header, sizeof(TCPHeader_t), 1, fp) != 1)
printf(&%d: can not read ip_header\n&, i);
src_port = ntohs(tcp_header-&SrcPort);
dst_port = ntohs(tcp_header-&DstPort);
tcp_flags = tcp_header-&F
printf(&%d: &src=%x\n&, i, tcp_flags);
if(tcp_flags == 0&18)
// (PSH, ACK) 3路握手成功后
if(dst_port == 80)
// HTTP GET请求
http_len = ip_len & 40;
//http 报文长度
match_http(fp, &Host: &, &\r\n&, host, http_len);
//查找 host 值
match_http(fp, &GET &, &HTTP&, uri, http_len);
//查找 uri 值
sprintf(buf, &%d: &%s &src=%s:%d &dst=%s:%d &%s%s\r\n&, i, my_time, src_ip, src_port, dst_ip, dst_port, host, uri);
//printf(&%s&, buf);
if(fwrite(buf, strlen(buf), 1, output) != 1)
printf(&output file can not write&);
} // end while
fclose(fp);
fclose(output);
//查找 HTTP 信息
void match_http(FILE *fp, char *head_str, char *tail_str, char *buf, int total_len)
head_len, tail_len, val_
head_tmp[STRSIZE], tail_tmp[STRSIZE];
memset(head_tmp, 0, sizeof(head_tmp));
memset(tail_tmp, 0, sizeof(tail_tmp));
head_len = strlen(head_str);
tail_len = strlen(tail_str);
//查找 head_str
http_offset = ftell(fp);
//记录下HTTP报文初始文件偏移
while((head_tmp[0] = fgetc(fp)) != EOF) //逐个字节遍历
if((ftell(fp) & http_offset) & total_len)
//遍历完成
sprintf(buf, &can not find %s \r\n&, head_str);
if(head_tmp[0] == *head_str)
//匹配到第一个字符
for(i=1; i&head_ i++)
//匹配 head_str 的其他字符
head_tmp[i]=fgetc(fp);
if(head_tmp[i] != *(head_str+i))
if(i == head_len)
//匹配 head_str 成功,停止遍历
// printf(&head_tmp=%s \n&, head_tmp);
//查找 tail_str
val_len = 0;
while((tail_tmp[0] = fgetc(fp)) != EOF) //遍历
if((ftell(fp) & http_offset) & total_len)
//遍历完成
sprintf(buf, &can not find %s \r\n&, tail_str);
buf[val_len++] = tail_tmp[0];
//用buf 存储 value 直到查找到 tail_str
if(tail_tmp[0] == *tail_str)
//匹配到第一个字符
for(i=1; i&tail_ i++)
//匹配 head_str 的其他字符
tail_tmp[i]=fgetc(fp);
if(tail_tmp[i] != *(tail_str+i))
if(i == tail_len)
//匹配 head_str 成功,停止遍历
buf[val_len-1] = 0; //清除多余的一个字符
// printf(&val=%s\n&, buf);
fseek(fp, http_offset, SEEK_SET); //将文件指针 回到初始偏移
阅读(...) 评论() &}

我要回帖

更多关于 java读取pcap文件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信