2008-04-17 21:40:47| 分类: 默认分类|字号 订阅
#include <iostream.h>
#include <pcap.h>
#include <winsock2.h>
#pragma comment(lib,"wpcap")
#pragma comment(lib,"wsock32")
#include <conio.h>
struct ETherNet{
u_char dhost[6];
u_char shost[6];
u_short ether_type;
};
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 bit的版本信息 + 4 bits的头长
u_char tos; // TOS类型
u_short tlen; // 总长度
u_short identification; // Identification
u_short flags_fo; // Flags (3 bits) + Fragment offset (13 bits)
u_char ttl; // 生存期
u_char proto; // 后面的协议信息
u_short crc; // 校验和
ip_address saddr; // 源IP
ip_address daddr; // 目的IP
u_int op_pad; // Option + Padding
}ip_header;
/* UDP header*/
typedef struct udp_header{
u_short sport; // Source port
u_short dport; // Destination port
u_short len; // 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 main(){
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0,inum;
u_int netmask;
pcap_t *adhandle;
char packet_filter[100];
struct bpf_program fcode;
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
printf("查找网络适配器出错: %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(" (找不到相关描述!)\n");
}
if(i==0)
{
printf("\n找不到网络适配器,请确认已经安装WINPCAP.\n");
return -1;
}
printf("请输入选择的网络适配器编号 (1-%d):",i);
scanf("%d", &inum); //输入要选择打开的网卡号
if(inum < 1 || inum > i) //判断号的合法性
{
printf("\n非法的设备编号!.\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, // 混杂模式
3000, // 读超时为1秒
errbuf // 错误信息缓冲区
) ) == NULL)
{
printf("\n不能打开指定的网络适配器. %s 不被WINPCAP支持!\n");
/* 释放资源 */
pcap_freealldevs(alldevs);
return -1;
}
/* Check the link layer. We support only Ethernet for simplicity. */
if(pcap_datalink(adhandle) != DLT_EN10MB)
{
printf("\nThis program works only on Ethernet networks.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
if(d->addresses != NULL)
/* 获得第一个接口地址的掩码 */
{
netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
printf("%d.%d.%d.%d\n",
((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_un_b.s_b1,
((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_un_b.s_b2,
((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_un_b.s_b3,
((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_un_b.s_b4);
}
else
/* 如果这个接口没有地址那么我们假设他为C类地址 */
netmask=0xffffff;
printf("\n 请输入过滤条件(以'$'结束):\n");
i=0;
while((packet_filter[i++]=getchar())!='$');
packet_filter[i-1]='\0';
printf("\n%s",packet_filter);
//compile the filter
if(pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 ){
fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
//set the filter
if(pcap_setfilter(adhandle, &fcode)<0){
printf("\nError setting the filter.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
printf("\n开始在\n %s \n上侦听...\n", d->description);
/*释放资源*/
pcap_freealldevs(alldevs);
pcap_loop(adhandle, 0, packet_handler, NULL);
// getch();
return 0;
}
/* 对每一个到来的数据包调用该函数 */
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;
/* convert the timestamp to readable format */
ltime=localtime(&header->ts.tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
/* print timestamp and length of the packet */
printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);
ETherNet *EH;
EH=(ETherNet *)(pkt_data);
printf("%s\n",EH->dhost);
/* 找到IP头的位置 */
ih = (ip_header *) (pkt_data +
14); //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 );
/* print ip addresses and udp ports */
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);
}