分析数据包(Microsoft Visual Studio 2010)

//Fenxi1。cpp:定义控制台应用程序的入口点。
//
代码如下:
# include“stdafx。h pcap
# include。h bittypes
# include。h“
# pragma评论(自由,“ws2_32.lib”)
typedef struct ip_address {
,,,u_char byte1;
,,,u_char byte2;
,,,u_char byte3;
,,,u_char byte4;
} ip_address;

/* IPv4首部*/
typedef struct ip_header {
,,,u_char ver_ihl;,,,,,,,//版本(4位)+首部长度(4位)
,,,u_char 服务条款,,,,,,,,,,,,//服务类型(类型的服务)
,,,u_short tlen;,,,,,,,,,,//总长(总长度)
,,,u_short识别;//标识(识别)
,,,u_short flags_fo;,,,,,,//标志位(旗帜)(3位)+段偏移量(片偏移)(13位)
,,,u_char ttl;,,,,,,,,,,,//存活时间(生存时间)
,,,u_char 原型,,,,,,,,,,//协议(协议)
,,,u_short crc,,,,,,,,,,,,//首部校验和(头校验和)
,,,ip_address saddr;,,,,,//源地址(源地址)
,,,ip_address daddr;,,,,,//目的地址(目的地地址)
,,,u_int ,op_pad;,,,,,,,,//选项与填充(选项+填充)
} ip_header;

/* UDP首部*/
typedef struct udp_header {
,,,u_short运动,,,,,,,,,,//源端口(源端口)
,,,u_short dport;,,,,,,,,,//目的端口(目的港)
,,,u_short len;,,,,,,,,,,,//UDP数据包长度(数据报的长度)
,,,u_short crc,,,,,,,,,,,,//校验和(校验和)
} udp_header;

/*回调函数原型*/
空白packet_handler (u_char *参数,const struct pcap_pkthdr *头,const u_char * pkt_data);

int _tmain (int命令行参数个数,_TCHAR * argv [])
{
pcap_if_t * alldevs;
pcap_if_t * d;
int inum;
int i=0;
pcap_t * adhandle;
char errbuf [PCAP_ERRBUF_SIZE];
u_int子网掩码;
char packet_filter []=癷p和udp”;
struct bpf_program fcode;

,,,/*获得设备列表*/
,,,如果(pcap_findalldevs_ex (PCAP_SRC_IF_STRING、零和alldevs errbuf)==1)
,,,{
,,,,,,,在pcap_findalldevs流(stderr,“错误:% s \ n”, errbuf);
,,,,,,,退出(1),
,,,}
,,,
,,/*打印列表*/
,,,(d=alldevs;d;d=d→下)
,,,{
,,,,,,,printf (" % d。% s”+ + i d→名称),
,,,,,,,如果(d→描述)
,,,,,,,,,,,printf (" (% s) \ n”, d→描述),
,,,,,,,其他
,,,,,,,,,,,printf("(没有描述)\ n ");
,,,}

,,,如果(i==0)
,,,{
,,,,,,,printf (" \ nNo接口发现!确保安装WinPcap。\ n”);
,,,,,,,返回1;
,,,}
,,,
,,printf("输入接口(1 - % d)数量:”,i);
,,,scanf (“% d”, inum);
,,,
,,如果(inum & lt;1 | | inum祝辞我)
,,,{
,,,,,,,printf (" \ nInterface号码范围。\ n”);
,,,,,,,/*释放设备列表*/
,,,,,,,pcap_freealldevs (alldevs);
,,,,,,,返回1;
,,,}

,,,/*跳转到已选设备*/
,,,d=alldevs,我=0;i,,,
,,/*打开适配器*/
,,,如果(adhandle=pcap_open (d→名字,,//设备名
,,,,,,,,,,,,,,,,,,,,,,,,,,,,65536年,,,,,null

分析数据包(Microsoft Visual Studio 2010)