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

📄 test.c.4

📁 实现一个网卡到内存空间的零拷贝程序
💻 4
字号:
/* Testje */#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <sys/ioctl.h>#include <sys/mman.h>#include <netinet/in.h>#include <arpa/inet.h>#include "rawtx.h"#define ETHER_ADDR_FORMAT "%02X:%02X:%02X:%02X:%02X:%02X"#define ETHER_ADDR_DATA(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]#define TO_PORT		5001#define FROM_PORT	5000#define TO_IP		"192.168.2.1"#define FROM_IP		"192.168.2.2"#define FROM_ETHER	{ 0x00, 0x01, 0x02, 0x08, 0xA6, 0xF2 }#define TO_ETHER	{ 0x00, 0x50, 0x04, 0xD0, 0x03, 0x40 }#define NET_DEVICE	"eth1"#define DATA_LEN	1400#define COUNT		10000#define QLEN		1#define RAWTX_DEVICE "/tmp/rawtx"static int rawtx_fd;void init_rawtx(void){	unsigned char sether[6] = FROM_ETHER, dether[6] = TO_ETHER;	struct sockaddr_in addr;	printf("Opening rawtx device...\n");	rawtx_fd = open(RAWTX_DEVICE, O_RDWR);	if (rawtx_fd < 0) {		perror("open");		exit(1);	}	printf("Setting device %s...\n", NET_DEVICE);	if (ioctl(rawtx_fd, RAWTX_IOCTL_SETDEVICE, NET_DEVICE) < 0) {		perror("ioctl setdevice");		exit(1);	}	printf("Setting protocol to UDP/IP-NOSUM...\n");	if (ioctl(rawtx_fd, RAWTX_IOCTL_SETPROTO, RAWTX_PROTO_UDPIP_NOSUM) < 0) {		perror("ioctl setproto");		exit(1);	}	printf("Setting destination address " ETHER_ADDR_FORMAT "...\n",	       ETHER_ADDR_DATA(dether));	if (ioctl(rawtx_fd, RAWTX_IOCTL_SETDADDR, &dether) < 0) {		perror("ioctl setdaddr");		exit(1);	}	printf("Setting source address %s:%d...\n", FROM_IP, FROM_PORT);	addr.sin_family = AF_INET;	addr.sin_port = htons(FROM_PORT);	inet_aton(FROM_IP, &addr.sin_addr);	if (ioctl(rawtx_fd, RAWTX_IOCTL_SETSRCADDR, &addr) < 0) {		perror("ioctl setsrcaddr");		exit(1);	}	printf("Setting destination address %s:%d...\n", TO_IP, TO_PORT);	addr.sin_family = AF_INET;	addr.sin_port = htons(TO_PORT);	inet_aton(TO_IP, &addr.sin_addr);	if (ioctl(rawtx_fd, RAWTX_IOCTL_SETDSTADDR, &addr) < 0) {		perror("ioctl setdstaddr");		exit(1);	}	printf("Setting queue length %d...\n", QLEN);	if (ioctl(rawtx_fd, RAWTX_IOCTL_SETQUEUELEN, QLEN) < 0) {		perror("ioctl setqueuelen");		exit(1);	}}int main(void){	unsigned char data_buf[10240];	unsigned char *p;	int i;	init_rawtx();	printf("Sending packets...\n");	/* Tell ttcp to start the timer. */	write(rawtx_fd, data_buf, 4);	for (i = 0; i < COUNT; i++) {		p = data_buf + (i%5) * 1500;		*(unsigned int *)(p) = i;		strcpy(p + 8, "...Hallo allemaal hier");		write(rawtx_fd, p, DATA_LEN);	}	/* Tell ttcp that we're done. */	write(rawtx_fd, data_buf, 4);	write(rawtx_fd, data_buf, 4);	write(rawtx_fd, data_buf, 4);	write(rawtx_fd, data_buf, 4);	close(rawtx_fd);	return 0;}

⌨️ 快捷键说明

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