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

📄 tun_dev.c

📁 Adhoc无线网络路由协议源码
💻 C
字号:
#include "common.h"#include "utils.h"#include "tun_dev.h"/* * tun_init(): * 	prepare the tun device and return the handle (file descriptor) */int tun_init(){  struct ifreq ifr;  int fd, r;  char cmd[256];  unsigned char dev[IFNAMSIZ];    // change to root --vikas  if(setreuid(0, 0) < 0){    fprintf(stderr, "setreuid to root failed : \n Please run as root.. ");    return(-1);  }  /* FIXME : hardcoded dir name and perms */  umask(0);  mkdir("/dev/net", 0755);  /* MISC_MAJOR and TUN_MINOR char nodes */  mk_node("/dev/net/tun", 10, 200);  // FIXME : should I do a manual insmod here : kmod should do automatically  	if ( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {	 	fprintf(stderr, "tun_init(): Error openeing tun device :\n May be you have not configured tun device \n See /usr/src/linux/Documentation/networking/tuntap.txt\n");		perror("Opening : /dev/net/tun");		return -1;	}	memset(&ifr, 0, sizeof(ifr));	/* Flags: IFF_TUN   - TUN device (no Ethernet headers) 	 *        IFF_TAP   - TAP device  	 *	 *        IFF_NO_PI - Do not provide packet information  	 */ 	ifr.ifr_flags = IFF_TUN | IFF_NO_PI; 	strcpy(ifr.ifr_name, TUN_DEV_NAME);	if ((r = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) {		perror("ioctl(TUNSETIFF) failed");        	close(fd);        	return -1;	}	strcpy(dev, ifr.ifr_name);	/* FIXME : Is there another way instead of using system */	/* turn up the interface	* Is it worth it ?? -vikas */	sprintf(cmd, "/sbin/ifconfig %s %s netmask 255.255.255.255 up",		dev, TUN_DEV_IP);	system(cmd);	#ifdef DEBUG	 fprintf(stderr, "tun_init() : device %s is now up\n", dev);#endif	 	/* no checksum */        if ((r = ioctl(fd, TUNSETNOCSUM, 1)) < 0) {		perror("ioctl(TUNSETNOCSUM) failed");        	close(fd);        	return -1;	}	return (fd);}/* * tun_in(fd, buf): *	read the next packet from fd into buf, return the packet length */int tun_in(int fd, unsigned char *buf){  int r;	r = read(fd, buf, BUFSIZE);#ifdef DEBUG	{ int i;		fprintf(stderr, "IN: (%d) [ ", r);		for (i = 0; i < 20; i++)			fprintf(stderr, "%02x ", buf[i]);		fprintf(stderr, "]\n");	}#endif	return r;}

⌨️ 快捷键说明

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