社区
通信技术
帖子详情
如何用winpcap捕获数据帧 (在线等)
CMyMfc
2003-11-27 10:22:01
rt
...全文
62
1
打赏
收藏
如何用winpcap捕获数据帧 (在线等)
rt
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
1 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
lzynk
2003-11-27
打赏
举报
回复
#include"pcap.h"
#include<iostream>
using namespace std;
typedef struct _ethernet_header
{
unsigned char DestinationAddress[6];
unsigned char SourceAddress[6];
unsigned short type;
}
Ethernet_Header;
typedef struct _ip_header
{
unsigned char version_length;
unsigned char tos;
unsigned short TotalLength;
unsigned short ident;
unsigned short fragement_offset;
unsigned char ttl;
unsigned char protocol;
unsigned short checksum;
unsigned long SourceIP;
unsigned long DestinationIP;
}
IP_Header;
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
int main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i = 0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
printf("Interface list:\n");
for (d = alldevs; d; d = d->next)
{
printf("%d:", ++i);
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_live(d->name,
65536, 1, 1000, errbuf)) == NULL)
{
cout << "\nUnable to open the adapter. \n";
pcap_freealldevs(alldevs);
return -1;
}
cout << "\nlistening on " << d->description << endl;
pcap_freealldevs(alldevs);
pcap_loop(adhandle, 0, packet_handler, NULL);
return 0;
}
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm* ltime;
char timestr[16];
Ethernet_Header* EthernetHeader;
IP_Header* IpHeader;
unsigned short type;
unsigned char protocol;
unsigned long IP;
ltime = localtime(&header->ts.tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
EthernetHeader = (Ethernet_Header*)pkt_data;
cout << "时间: " << timestr;
type = ntohs(EthernetHeader->type);
switch (type)
{
case 2048:
printf("\t类型: IP");
break;
case 2054:
printf("\t类型: ARP\n");
break;
case 32821:
printf("\t类型: RARP\n");
break;
case 34983:
printf("\t类型: Huawei\n");
break;
default:
if (type <= 1500)
{
printf("\t类型: 802.3\n");
}
else
printf("\t类型: Other(%d)\n", type);
}
if (type == 2048)
{
IpHeader = (IP_Header*)(pkt_data + 14);
protocol = IpHeader->protocol;
switch (protocol)
{
case 1:
printf(":ICMP");
break;
case 2:
printf(":IGMP");
break;
case 6:
printf(":TCP");
break;
case 17:
printf(":UDP");
break;
case 89:
printf(":OSPF");
break;
default:
printf(":Other(%d)", protocol);
}
IP = IpHeader->SourceIP;
cout << " \t发送: " << (IP&0xFF) << "." << ((IP&0xFFFF) >> 8) << "." << ((IP&0xFFFFFF) >> 16) << "." << (IP >> 24);
IP = IpHeader->DestinationIP;
cout << " \t接受: " << (IP&0xFF) << "." << ((IP&0xFFFF) >> 8) << "." << ((IP&0xFFFFFF) >> 16) << "." << (IP >> 24) << endl;
}
}
基于
Win
Pcap
的IP数据报
捕获
与分析.zip
利用
Win
Pcap
编写数据报
捕获
程序,要求能够解析以太网帧的源地址、目的地址和类型/长度域。 本程序编译环境是:Visual Studio 2012;系统环境是:
Win
dows 8(64 位); 详细介绍参考:...
USTB-计算机网络实践-
Win
Pcap
实现ARP数据分组的发送和解析
①根据 ARP/ICMP/TCP 协议数据的结构,封装成
数据帧
发送给另 一台计算机(可以是手机);②
捕获
网络中包含 ARP/ICMP/TCP 协议数据的数 180 据帧,解析协议数据的内容,并在标准输出中显示报文首部字段的内容,同 时...
论文研究-
捕获
网络数据包的方法.pdf
讨论了原始套接字、Lib
Pcap
、
Win
Pcap
与J
Pcap
捕获
网络数据包的原理,介绍了在
Win
32平台下用原始套接字
捕获
IP数据报,用
Win
Pcap
和Java
Pcap
捕获
以太帧的方法,并实现了以原始套接字、
Win
Pcap
和Java
Pcap
对用户访问ASP,...
IP数据包的
捕获
与分析
自己做的一个IP数据包的
捕获
与分析程序,自带实验报告。C++实现,利用
win
pcap
实现抓包,程序有日志文件记录操作。包括以太网DLC帧头分析、IP数据报报头分析和UDP、ICMP、IGMP、TCP的具体分析。没有图形界面
网络实验需要的程序
Win
Pcap
+WpdPack+J
pcap
无论是在
win
dows操作系统下还是在linux操作系统下,要想
捕获
网络上的数据包,必须要对网卡进行控制,因为本机的数据报从网络上来到本机是通过网卡然后再保存到本地缓冲区上的。网络层上有各种各样的数据包,它们以...
通信技术
4,356
社区成员
28,926
社区内容
发帖
与我相关
我的任务
通信技术
通信技术相关讨论
复制链接
扫一扫
分享
社区描述
通信技术相关讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章