feizusechizhubao

来源:互联网 发布:哔哩哔哩有mac版吗 编辑:程序博客网 时间:2024/05/17 23:06
#include "pcap.h"#include "remote-ext.h"INT getAdaper(int &i,pcap_if_t *&alldevs,pcap_t *&adhandle,char *&errbuf,pcap_if_t *&d){int inum = 0;printf("Enter the interface number (1-%d):",i);scanf("%d", &inum);if(inum < 1 || inum > i){printf("\nInterface number out of range.\n");/* Free the device list */pcap_freealldevs(alldevs);return -1;}/* Jump to the selected adapter */for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);/* Open the device */if ( (adhandle= pcap_open(d->name,          // name of the device65536,            // portion of the packet to capture. // 65536 guarantees that the whole packet will be captured on all the link layersPCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode-1,             // read timeoutNULL,             // authentication on the remote machineerrbuf            // error buffer) ) == NULL){fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);/* Free the device list */pcap_freealldevs(alldevs);return -1;}printf("\nlistening on %s...\n", d->description);return 1;}typedef struct cp_pcap_t{pcap_if_t *dev;struct pcap_pkthdr *header;const u_char *pkt_data;cp_pcap_t(){dev = NULL;header = NULL;pkt_data = NULL;}}cp_pcap_t;#define MAX_NET_CARD 8typedef struct cpPcap_t{cp_pcap_t pCap[MAX_NET_CARD];int num;cpPcap_t(){num = 0;}}cpPcap_t;main(){pcap_if_t *alldevs;pcap_if_t *d;pcap_if_t *d2;int i=0;pcap_t *adhandle;pcap_t *adhandle2;int res;char errbuf[PCAP_ERRBUF_SIZE];struct tm *ltime;char timestr[16];struct pcap_pkthdr *header;const u_char *pkt_data;struct pcap_pkthdr *header2;const u_char *pkt_data2;            /* Retrieve the device list on the local machine */    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)    {        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);        exit(1);    }        /* Print the list */    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;    }getAdaper(i,alldevs,adhandle,(char *&)errbuf,d);   i = 4;getAdaper(i,alldevs,adhandle2,(char *&)errbuf,d2);    /* At this point, we don't need any more the device list. Free it */    pcap_freealldevs(alldevs);        /* Retrieve the packets */while(1){if((res = pcap_next_ex( adhandle2, &header2, &pkt_data2)) >= 0){if(res != 0)/* Timeout elapsed */{/* convert the timestamp to readable format */ltime=localtime(&header2->ts.tv_sec);strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);printf("2222:%s,%.6d len:%d\n", timestr, header2->ts.tv_usec, header2->len);}}if((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){if(res != 0){ltime=localtime(&header->ts.tv_sec);strftime( timestr, sizeof(timestr), "%H:%M:%S", ltime);printf("1111:%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);}}    }    if(res == -1){        printf("Error reading the packets: %s\n", pcap_geterr(adhandle));        return -1;    }        return 0;}
#include "pcap.h"#include "remote-ext.h"INT getAdaper(int &i,pcap_if_t *&alldevs,pcap_t *&adhandle,char *&errbuf){int inum = 0;printf("Enter the interface number (1-%d):",i);scanf("%d", &inum);if(inum < 1 || inum > i){printf("\nInterface number out of range.\n");/* Free the device list */pcap_freealldevs(alldevs);return -1;}pcap_if_t *d = NULL;/* Jump to the selected adapter */for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);/* Open the device */if ( (adhandle= pcap_open(d->name,          // name of the device65536,            // portion of the packet to capture. // 65536 guarantees that the whole packet will be captured on all the link layersPCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode-1,             // read timeoutNULL,             // authentication on the remote machineerrbuf            // error buffer) ) == NULL){fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);/* Free the device list */pcap_freealldevs(alldevs);return -1;}printf("\nlistening on %s...\n", d->description);return 1;}typedef struct cp_pcap_t{pcap_t *handle;struct pcap_pkthdr *header;const u_char *pkt_data;cp_pcap_t(){handle = NULL;header = NULL;pkt_data = NULL;}}cp_pcap_t;#define MAX_NET_CARD 8typedef struct cpPcap_t{cp_pcap_t pCap[MAX_NET_CARD];int num;cpPcap_t(){num = 0;}}cpPcap_t;main(){pcap_if_t *alldevs,*d;cpPcap_t cpPcap;int i=0;int res;char errbuf[PCAP_ERRBUF_SIZE];struct tm *ltime;char timestr[16];            /* Retrieve the device list on the local machine */    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)    {        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);        exit(1);    }        /* Print the list */    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;    }int j = 0;getAdaper(i,alldevs,cpPcap.pCap[cpPcap.num ++ ].handle,(char *&)errbuf);  /*     i = 4;  getAdaper(i,alldevs,cpPcap.pCap[cpPcap.num ++].handle,(char *&)errbuf);*/     /* At this point, we don't need any more the device list. Free it */    pcap_freealldevs(alldevs);        /* Retrieve the packets */int havPacket = 0;while(1){havPacket = 0;for(int iCardNum = 0;iCardNum < cpPcap.num;iCardNum++){if((res = pcap_next_ex( cpPcap.pCap[iCardNum].handle, &(cpPcap.pCap[iCardNum].header), &(cpPcap.pCap[iCardNum].pkt_data))) >= 0){if(res > 0)/* Timeout elapsed */{/* convert the timestamp to readable format */ltime=localtime(&cpPcap.pCap[iCardNum].header->ts.tv_sec);strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);havPacket++;printf("$$%d:%s,%.6d len:%d\n",iCardNum, timestr, cpPcap.pCap[iCardNum].header->ts.tv_usec, cpPcap.pCap[iCardNum].header->len);}else{continue;}}else if(res == -1){printf("Error reading the packets: %s\n", pcap_geterr(cpPcap.pCap[iCardNum].handle));return -1;}}if(!havPacket){Sleep(20);}}            return 0;}

	
				
		
原创粉丝点击