百度空间 | 百度首页 
 
查看文章
 
get pack2
2009-11-03 15:06

//start

// yjyfir.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#define HAVE_REMOTE
#include "pcap.h"

#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock.h>
#endif

#include <pcap.h>

#pragma comment(lib, "Packet.lib")
#pragma comment(lib, "wpcap.lib")
void mylis(u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data);
int main()
{
pcap_if_t * alldevs;
pcap_if_t * d;

char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)<0){
   printf("%s\n",errbuf);
   exit(1);
}
int cas=0;
for (d=alldevs;d!=NULL;d=d->next){
   printf("case: %d: %s\n",cas++,d->description);
}
int id;
printf("input which card:");
scanf("%d",&id);
int i;
for (d=alldevs,i=0;d!=NULL && i!=id;i++,d=d->next);
printf("choose: %s\n",d->description);
pcap_t * handle = pcap_open(d->name,65536, PCAP_OPENFLAG_PROMISCUOUS,1000,NULL,errbuf);
if (handle==NULL){
   printf("open error: %s\n",errbuf);
}

if (alldevs==NULL){
   printf("NO netcard found!\n");
}
pcap_freealldevs(alldevs);
int cnt=5;//number of packs;
//pcap_loop(handle,cnt, mylis,NULL);
int res;
struct pcap_pkthdr *pkt_header;
const u_char *pkt_data;
while ((res=pcap_next_ex(handle,&pkt_header ,&pkt_data ))>=0){
   if (res==0){
    continue;
   }
   mylis(NULL,pkt_header ,pkt_data );
}
return 0;
}
void mylis(u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data){
struct tm * ltime;
char timestr[100];
time_t local;
local=pkt_header->ts.tv_sec;
ltime=localtime(&local);
strftime(timestr,sizeof(timestr),"%H : %M:%S",ltime);
printf("%s: len:%d nl: %d\n",timestr,pkt_header->len,pkt_header->caplen);
int i;
for (i=0;i<pkt_header->len;i++){
   printf("%c",pkt_data[i]);
}
printf("\n");
}

//end


类别:net 协议 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu