testvnet2.c

来自「嵌入式系统的网络仿真」· C语言 代码 · 共 48 行

C
48
字号
//---------------------------------------------------------------------//test open device file /dev/net/vnet//test hif net device//test ioctl VNETSETIFF, and ifr_name=NULL//if ifr_name==NULL, then should use 'ifconfig vnet0 inet ......'//----------------------------------------------------------------------#include <net/if.h>//#include <linux/if_tun.h>#include "if_vnet.h"#include <sys/ioctl.h>void main(void){int tapif_fd;struct ifreq ifr;char buf[1024];tapif_fd = open("/dev/net/vnet", 2);if(tapif_fd<0){ 	perror("open");exit(-1);};printf("open vnet ok!\n");memset(&ifr, 0, sizeof(ifr));//no need to set name, do it in vnet_hostif_create//I think when you want more vnet, then you should set //tht name for vnet.//IFNAMSIZ=16//strncpy(ifr.ifr_name, "vnet1", IFNAMSIZ);ifr.ifr_flags = IFF_VNET;if (ioctl(tapif_fd, VNETSETIFF, (void *) &ifr) < 0) {    perror(buf);    exit(-1);}printf("ioctl vnet ok\n");sleep(2);snprintf(buf, sizeof(buf), "ifconfig vnet0 inet %d.%d.%d.%d",           10,0,0,1);  printf("tapif_init: system(\"%s\");\n", buf);system(buf);while(1){ sleep(30);};//printf("input a key....\n");//getc();}

⌨️ 快捷键说明

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