如何用winpcap捕获数据帧 (在线等)

CMyMfc 2003-11-27 10:22:01
rt
...全文
62 1 打赏 收藏 转发到动态 举报
写回复
用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;
}
}

4,356

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧