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

📄 test.c.3

📁 实现一个网卡到内存空间的零拷贝程序
💻 3
字号:
/* 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		5800#define FROM_PORT	5000#define TO_IP		"192.16.199.38"#define FROM_IP		"192.16.199.42"#define FROM_ETHER	{ 0x00, 0x01, 0x02, 0x08, 0xA6, 0xF2 }#define TO_ETHER	{ 0x00, 0x50, 0x04, 0x63, 0xDB, 0xEF }#define NET_DEVICE	"eth0"#define DATA_LEN	1400#define COUNT		10000#define RAWTX_DEVICE "/user/jorisvr/jstage/rawtx_device"static int rawtx_fd;static unsigned char * dbuffer;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("Mapping buffer pool...\n");	dbuffer = mmap(NULL, RAWTX_BUFPOOL_SIZE, PROT_READ|PROT_WRITE,	               MAP_SHARED, rawtx_fd, 0);	if (dbuffer == MAP_FAILED) {		perror("mmap");		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...\n");	if (ioctl(rawtx_fd, RAWTX_IOCTL_SETPROTO, RAWTX_PROTO_UDPIP) < 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);	}}void send_packet(unsigned long bufofs){	int err;	if ((err = ioctl(rawtx_fd, RAWTX_IOCTL_SENDBUFFER, bufofs)) != 0) {		if (err > 0)			printf("Packet drop!!\n");		else {			perror("ioctl sendbuffer");			exit(1);		}	}}static void get_buffer(unsigned long *ofs, struct rawtx_buffer **buf,                       unsigned char **data_buf){	if (ioctl(rawtx_fd, RAWTX_IOCTL_GETBUFFER, ofs)) {		perror("ioctl getbuffer");		exit(1);	}	*buf = (struct rawtx_buffer *) (dbuffer + *ofs);	*data_buf = dbuffer + (*buf)->data_ofs;}int main(void){	unsigned long bufofs;	struct rawtx_buffer *buf;	unsigned char *data_buf;	int i;	init_rawtx();	printf("Sending packets...\n");	/* Tell ttcp to start the timer. */	get_buffer(&bufofs, &buf, &data_buf);	buf->data_len = 4;	send_packet(bufofs);	for (i = 0; i < COUNT; i++) {		get_buffer(&bufofs, &buf, &data_buf);		*(unsigned int *)(data_buf) = i;		strcpy(data_buf + 8, "...Hallo allemaal hier");		buf->data_len = DATA_LEN;		send_packet(bufofs);	}	/* Tell ttcp that we're done. */	get_buffer(&bufofs, &buf, &data_buf);	buf->data_len = 4;	send_packet(bufofs);	get_buffer(&bufofs, &buf, &data_buf);	buf->data_len = 4;	send_packet(bufofs);	get_buffer(&bufofs, &buf, &data_buf);	buf->data_len = 4;	send_packet(bufofs);	get_buffer(&bufofs, &buf, &data_buf);	buf->data_len = 4;	send_packet(bufofs);	close(rawtx_fd);	return 0;}

⌨️ 快捷键说明

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