分析数据包(Microsoft Visual Studio 2010)
// Fenxi1.cpp : 定义控制台应用程序的入口点。
//
代码如下:
#include "stdafx.h"
#include "pcap.h"
#include "bittypes.h"
#pragma comment(lib,"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 bits) + 首部长度 (4 bits)
u_char tos; // 服务类型(Type of service)
u_short tlen; // 总长(Total length)
u_short identification; // 标识(Identification)
u_short flags_fo; // 标志位(Flags) (3 bits) + 段偏移量(Fragment offset) (13 bits)
u_char ttl; // 存活时间(Time to live)
u_char proto; // 协议(Protocol)
u_short crc; // 首部校验和(Header checksum)
ip_address saddr; // 源地址(Source address)
ip_address daddr; // 目的地址(Destination address)
u_int op_pad; // 选项与填充(Option + Padding)
}ip_header;
/* UDP 首部*/
typedef struct udp_header{
u_short sport; // 源端口(Source port)
u_short dport; // 目的端口(Destination port)
u_short len; // UDP数据包长度(Datagram length)
u_short crc; // 校验和(Checksum)
}udp_header;
/* 回调函数原型 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
int _tmain(int argc, _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 netmask;
char packet_filter[] = "ip and udp";
struct bpf_program fcode;
/* 获得设备列表 */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
/* 打印列表 */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
if(i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 跳转到已选设备 */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* 打开适配器 */
if ( (adhandle= pcap_open(d->name, // 设备名
65536, // 要捕捉的数据包的部分
// 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
PCAP_OPENFLAG_PROMISCUOUS, // 混杂模式
1000, // 读取超时时间
NULL, // 远程机器验证
errbuf // 错误缓冲池
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 检查数据链路层,为了简单,我们只考虑以太网 */
if(pcap_datalink(adhandle) != DLT_EN10MB)
{
fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
if(d->addresses != NULL)
/* 获得接口第一个地址的掩码 */
netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
else
/* 如果接口没有地址,那么我们假设一个C类的掩码 */
netmask=0xffffff;
//编译过滤器
if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
{
fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
//设置过滤器
if (pcap_setfilter(adhandle, &fcode)<0)
{
fprintf(stderr,"\nError setting the filter.\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}
printf("蒋教寿:1306404018");
printf("\nlistening on %s...\n", d->description);
/* 释放设备列表 */
pcap_freealldevs(alldevs);
/* 开始捕捉 */
pcap_loop(adhandle, 0, packet_handler, NULL);
return 0;
}
/* 回调函数,当收到每一个数据包时会被libpcap所调用 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];
ip_header *ih;
udp_header *uh;
u_int ip_len;
u_short sport,dport;
time_t local_tv_sec;
/* 将时间戳转换成可识别的格式 */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
/* 打印数据包的时间戳和长度 */
printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);
/* 获得IP数据包头部的位置 */
ih = (ip_header *) (pkt_data +
14); //以太网头部长度
/* 获得UDP首部的位置 */
ip_len = (ih->ver_ihl & 0xf) * 4;
uh = (udp_header *) ((u_char*)ih + ip_len);
/* 将网络字节序列转换成主机字节序列 */
sport = ntohs( uh->sport );
dport = ntohs( uh->dport );
/* 打印IP地址和UDP端口 */
printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
ih->daddr.byte1,
ih->daddr.byte2,
ih->daddr.byte3,
ih->daddr.byte4,
dport);
}
项目-->**属性(alt+F7)
配置属性-->C/C++-->常规-->附加包含目录-->(是把头文件所在的文件路径(Include)添加到附加目录中C:\WpdPack\Include)
项目-->**属性(alt+F7)
配置属性-->链接器-->常规-->附加库目录-->(是把Packet.lib;wpcap.lib所在目录(Lib)添加到附加库目录中C:\WpdPack\Lib)
项目-->**属性(alt+F7)
配置属性-->链接器-->输入-->附加依赖项-->补充“;Packet.lib;wpcap.lib”
项目-->**属性(alt+F7)
配置属性-->C/C++-->预处理器-->预处理器定义-->补充“;HAVE_REMOTE”
加头部如下
#include "pcap.h"
#include "bittypes.h"
#pragma comment(lib,"ws2_32.lib")
下面是获取的结果图:
转载于:https://blog.51cto.com/12044896/1870651
分析数据包(Microsoft Visual Studio 2010)相关推荐
- Microsoft Visual Studio 2010 Service Pack 1
官方页面 http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=75568aa6-8107-475d-948a-ef22627e ...
- 【Microsoft Visual Studio 2010完成CLR窗体应用程序】计算器和画图板的设计
计算器和画图板的设计 目录: 计算器设计 画图板设计 整体总结和分析 前言: 本次应课程要求要用C++设计计算器和画图程序,我会使用Microsoft Visual Studio 2010来完成该窗体 ...
- Microsoft Visual Studio 2010 下载及学习资料大全
Microsoft Visual Studio 2010 下载 http://www.microsoft.com/downloads/details.aspx?familyid=06A32B1C-80 ...
- Microsoft Visual Studio 2010 破解下载!
1.Microsoft Visual Studio 2010下载 专业版(Professional)http://download.microsoft.com/download/4/0/E/40EFE ...
- Download Microsoft Visual Studio 2010 Ultimate Trial - ISO from Official Microsoft Download Center
Download Microsoft Visual Studio 2010 Ultimate Trial - ISO from Official Microsoft Download Center M ...
- C语言的环境搭建(Microsoft Visual Studio 2010的介绍与安装)
C语言的环境搭建 一.常用的环境 : 1.Microsoft Visual C++ (简称Visual C++.MSVC.VC++或VC)微软公司的C++开发工具,具有集成开发环境,可提供编辑C语言, ...
- Microsoft Visual Studio 2010(VS2010)秘钥
Microsoft Visual Studio 2010(VS2010)正式版 CDKEY / SN: YCFHQ-9DWCY-DKV88-T2TMH-G7BHP 企业版.旗舰版
- Microsoft Visual Studio 2010 Service Pack 1 离线下载版
家里网太慢了,终于找到离线安装的链接了.如下是iso和直接下载安装的版本(稍后奉上): Microsoft Visual Studio 2010 Service Pack 1 官方离线下载版(ISO) ...
- Microsoft Visual Studio 2010
VS 2010的全称是Microsoft Visual Studio 2010,它是由微软公司所推出的一款开发环境,它不仅仅可以支持C#.C++ .VB等许多种开发语言,而且还可以用于创建Window ...
- Microsoft Visual Studio 2010安装包、创建工程(一)
对于每一个从事电子/计算机行业的员工,C语言都是一门必修课,那么什么C语言呢?C语言是一门计算机语言,所谓计算机语言就是人和计算机交流的语言.C语言是计算机语言之一,它还有C++.JAVA.Pytho ...
最新文章
- SQL Server 2000安装指南及数据创建
- matlab simulink 电气连接
- Centos7.x/redhat7.x修改网卡名称
- ffmpeg-简单AES加解密记录
- 实现MySQL高可用群集配置_配置高可用性的MySQL服务器负载均衡群集
- android ios logo原型,iOS关于logo和LaunchImage处理
- 郑州it java_郑州Java网站开发
- php登录 cookie,使用cookie进行简单的PHP登录
- Eclipse Code Template 设置自动加注释
- mysql 外键约束条件_MySQL-条件外键约束-问答-阿里云开发者社区-阿里云
- proteus中的标签及总线的使用方法
- 致移动开发的后10年
- 自定义 SpringBoot Banner 图案
- java项目开发团队协作重要性_Java1班项目实战 | 团队协作,我们是认真的!
- Java笔记--基础篇
- js时间与当前时间比较
- info.plist文件常用属性含义
- 血气方刚的年轻小伙竟去做家政小哥,是怎样成功逆袭转行的
- 【论文笔记】Semi-Supervised Active Learning with Temporal Output Discrepancy(ICCV 2021)
- 【干货】240+套年终总结汇报PPT模板,让你的PPT逼格满满!(附下载)
热门文章
- WordPress数据库error establishing a database connection错误
- 从零基础入门Tensorflow2.0 ----七、35. 文本生成之---1.数据处理
- ArcGIS相关学习视频链接
- (转载)形态学腐蚀、膨胀——Matlab DIP(瓦)ch9形态学图像处理
- 《剑指offer》面试题21——包含min函数的栈(C++)
- IDL | 实验一、IDL编码与运行
- Server Tomcat v8.5 Server at localhost was unable to start within 45 seconds. If the server requires
- java数组验证哥德巴赫猜想_验证哥德巴赫猜想的JAVA代码
- Glide 4.x添加自定义组件原理
- mes系统与Oracle对接,Oracle ERP系统与MES系统的接口分析及实现