⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 get_packet.c

📁 对网卡大量发包,测试网卡性能的代码!一般对网卡同一端口平均发送5次,求平均值就可以测出网卡的性能
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pcap.h>

#define ETH_HEAD 14

typedef struct IP
{
	unsigned char ver_h_len;
	unsigned char tos;
	unsigned short total_len;
	unsigned short identifier;
	unsigned short flag;
	unsigned char ttl;
	unsigned char protocol;
	unsigned short check;
	unsigned char src_ip[4];
	unsigned char dest_ip[4];
}ip_hdr;/* ip header */


int main(int argc,char **argv){
	pcap_t *pd;/* package identifier */
	char *device = NULL;/* Network device like 'eth0' */
        char stbuf[10] ={0};
        char * pstbuf = &stbuf[0];
	char ebuf[PCAP_ERRBUF_SIZE];/* error buffer */
	char *ptr = NULL;
	ip_hdr *ip;
	struct pcap_pkthdr hdr;
	unsigned char version,length;
	unsigned long counter = 0;
	char *content;
	
	if(argc < 2){
		printf("Usage: ./get_packet XXX (XXX represent the last segment of the source ip).\n");
		exit(1);
	}
	unsigned int ip4 = atoi(argv[1]);
	if(ip4 > 255){
		printf("%d,invalid ip segment!\n",ip4);
		exit(1);
	}
//	device = pcap_lookupdev(ebuf);/* get device name */
//	printf("DEV is %s\n",device);
        strcpy(pstbuf,"eth4");
        device = pstbuf; 
        

	pd = pcap_open_live(device,1800,1,500,ebuf);/* get package identifier */
	while(1){
		if(( ptr = (char *)pcap_next(pd,&hdr)) != NULL){
			ip = (ip_hdr *)(ptr + ETH_HEAD);
			if((ip->src_ip[3] == ip4) || (ip->dest_ip[3] == ip4)){
				counter ++;
				printf("%ld  ",counter);
				printf("packet length: %d\n",ntohs(ip->total_len));
			}
		}
		
		/* deal with the packages */
	}

	pcap_close(pd);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -