原文:http://xiexiaohui.com.host2.ugocn.com/index.php/archives/34
实现步骤:
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_short;
typedef unsigned long u_int32;
typedef unsigned short u_int16;
typedef unsigned char u_int8;
//pacp 文件头结构体
struct pcap_file_header
{
bpf_u_int32 magic; /* 0xa1b2c3d4 */
u_short version_major; /* magjor Version 2 */
u_short version_minor; /* magjor Version 4 */
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_*) */
};
//时间戳
struct time_val
{
long tv_sec; /* seconds 含义同 time_t 对象的值 */
long tv_usec; /* and microseconds */
};
//pcap 数据包头结构体
struct pcap_pkthdr
{
struct time_val ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
};
//数据帧头
typedef struct FramHeader_t
评论15
最新资源