TAP/TUN(二)
#include<assert.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/ioctl.h>
#include<linux/if.h>
#include<linux/if_tun.h>
#include<sys/types.h>
#include<errno.h>
#include<net/route.h>
intTap=0;
intTap1=0;
int interface_up(char*interface_name)
{
int s;
if((s = socket(PF_INET,SOCK_STREAM,0))<0)
{
printf("Error create socket :%m/n", errno);
return-1;
}
struct ifreq ifr;
strcpy(ifr.ifr_name,interface_name);
short flag;
flag = IFF_UP;
if(ioctl(s, SIOCGIFFLAGS,&ifr)<0)
{
printf("Error up %s :%m/n",interface_name, errno);
return-1;
}
ifr.ifr_ifru.ifru_flags |= flag;
if(ioctl(s, SIOCSIFFLAGS,&ifr)<0)
{
printf("Error up %s :%m/n",interface_name, errno);
return-1;
}
return0;
}
int tun_create(char*dev,int flags)
{
struct ifreq ifr;
int fd, err;
assert(dev != NULL);
if((fd = open("/dev/net/tun", O_RDWR))<0){
return fd;
}
memset(&ifr,0,sizeof(ifr));
ifr.ifr_flags |= flags;
if(*dev !='\0'){
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}
if((err=ioctl(fd,TUNSETIFF,(void*)&ifr))<0){
close(fd);
return err;
}
strcpy(dev,ifr.ifr_name);
return fd;
}
int main(int argc,char*argv[])
{
char tun_name[IFNAMSIZ]="tap0";
char tun_name1[IFNAMSIZ]="tap1";
Tap=tun_create(tun_name, IFF_TAP | IFF_NO_PI);
if(Tap<0){
perror("tun_create");
return1;
}
printf("TAP name is %s\n", tun_name);
printf("Tap=%d\n",Tap);
//interface_up(tun_name);
system("ifconfig tap0 10.66.31.185 netmask 255.255.0.0 up");
while(1);
}
程序要一直运行,否则程序退出后创建的TAP设备会被释放


附件列表
TAP/TUN(二)的更多相关文章
- TAP/TUN摘要
TUN适用于IP帧.Tap适用于以太网帧.TAP摸拟一个以太网设备(以arp广播MAC识别),它操作第二层数据包如以太网数据帧.TUN模拟了网络层ip设备(以点对点的方式,使用ip标识),操作第三层数 ...
- 一文总结 Linux 虚拟网络设备 eth, tap/tun, veth-pair
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. Linux 虚 ...
- 利用 Linux tap/tun 虚拟设备写一个 ICMP echo 程序
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 前面两篇文章已 ...
- Linux 网络工具详解之 ip tuntap 和 tunctl 创建 tap/tun 设备
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 在前面一篇文章 ...
- [原创] 详解云计算网络底层技术——虚拟网络设备 tap/tun 原理解析
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 在云计算时代, ...
- 【转】Linux 网络工具详解之 ip tuntap 和 tunctl 创建 tap/tun 设备
原文:https://www.cnblogs.com/bakari/p/10449664.html -------------------------------------------------- ...
- TAP/TUN浅析(一)
参考链接:https://www.ibm.com/developerworks/cn/linux/1310_xiawc_networkdevice/ TAP 设备与 VETH 设备 TUN/T ...
- TAP/TUN浅析
转:http://www.cnblogs.com/yml435/p/5917628.html 参考链接:https://www.ibm.com/developerworks/cn/linux/1310 ...
- [转]Linux-虚拟网络设备-tun/tap
转: 原文:https://blog.csdn.net/sld880311/article/details/77854651 ------------------------------------- ...
随机推荐
- Excel应该这么玩——6、链接:瞬间转移
上一篇中提到通过命名表格来管理基础数据,这样会让数据更规范.如果有很多个基础数据表,需要查找或者修改其中的一个,可以通过名称框中下拉来定位. 但是当表格较多的时候,通过下拉选择的方式就不是很好定位了. ...
- Install Mono on Linux
Debian, Ubuntu, and derivatives Add the Mono Project GPG signing key and the package repository to y ...
- Asp.net在IE10、IE11下事件丢失经验总结
asp.net4.0出生得比IE10早,所以asp.net4.0以前版本不认识IE10 的 User-Agent 标头,导致的后果就是ASP.NET 特定功能失效,例如:页面报错__doPostBac ...
- Java图形化用户界面(GUI)笔记(一)介绍
一.布局 1.组件 以图形化的方式显示在屏幕上的,和用户进行交互的对象,就叫做组件. 组件包括:菜单.标签.文本框(密码框).滚动条.单选.复选.按钮等等. 2.容器 组件不能独立的显示出来,只能将组 ...
- C#控制台项目更改运行文件
这个是极光推送的C# demo,里面有几个文件,要先后运行.这是第一次遇见,所以一下子找不到北,摸索了好一会儿才知会.于是做了这面一个记录. 右击鼠标,查看属性弹出这个这面,然后选择要启动的对象.保存 ...
- php常用函数time
string date( string format [, int timestamp] ) 参数 format 表示时间格式化的方式,可能的方式如下: 格式化方式 说明 Y ...
- 自定义CollectionViewLayout
转自answer-huang的博客 原文出自:Custom Collection View Layouts UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一颗 ...
- QT常见数据类型操作
平常使用QStringList,都是通过at()访问其中的元素,然后试图也通过它修改元素,编译器报错,原来要使用下标访问修改: //accessRecList.at(3)=strSimilarity; ...
- python 列表排序
转自http://www.iplaypython.com/jinjie/jj114.html reverse()方法 将列表中元素反转排序,比如下面这样>>> x = [1,5,2, ...
- maven插件
sql-maven-plugin: http://www.mojohaus.org/sql-maven-plugin/ 常用插件: http://www.trinea.cn/android/maven ...