feizusechizhubao

#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 device
	65536,            // portion of the packet to capture. 
	// 65536 guarantees that the whole packet will be captured on all the link layers
	PCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode
	-1,             // read timeout
	NULL,             // authentication on the remote machine
	errbuf            // 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 8
typedef 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 device
	65536,            // portion of the packet to capture. 
	// 65536 guarantees that the whole packet will be captured on all the link layers
	PCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode
	-1,             // read timeout
	NULL,             // authentication on the remote machine
	errbuf            // 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 8
typedef 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值