- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我很难解决这个问题 - 我正在尝试编写一个程序来与 Linux 隧道驱动程序交互。在最基本的层面上,我只想创建一个能够通过网络隧道传输数据的应用程序。但是,对于如何正确设置隧道驱动程序以实现此目的,我完全不知所措。
我在 Ubuntu 9.04 上开发,我加载了隧道驱动程序内核模块。
存在设备/dev/net/tun
,但是没有/dev/tunX
设备。我无法使用 ifconfig
创建这些设备 - 例如,每当我运行 /sbin/ifconfig tun0 up
时,我都会收到以下错误:
tun0: ERROR while getting interface flags: No such device.
如果我尝试查看 /dev/net/tun
设备,会出现以下错误:
cat: /dev/net/tun: File descriptor in bad state.
试图通过一个小程序打开/dev/tunX
,基本上,一个简单的
tun_fd = open( "/dev/tun0", O_RDWR )
返回 -1:应用程序以 root 身份运行,但仍然无法打开此隧道设备。可以打开 /dev/net/tun
,但这似乎不会生成一个新的 /dev/tunX
设备来代替使用。
因此,总而言之 - 如何编写一个希望使用 Linux 隧道驱动程序的应用程序?任何见解将不胜感激。
谢谢;~罗伯特
最佳答案
没有/dev/tunX
设备文件。相反,您打开 /dev/net/tun
并通过 ioctl()
将其配置为“指向”tun0
。为了展示基本过程,我将使用命令行工具 ip tun tap
创建 TUN 接口(interface),然后展示要从该 TUN 设备读取的 C 代码。因此,要通过命令行创建 tun 接口(interface):
ip addr show # my eth0 inet address is 10.0.2.15/24 as Im running on a VirtualBox vm with Ubuntu 18.04 guest
sudo ip tuntap add mode tun dev tun0
sudo ip addr add 10.0.3.0/24 dev tun0 # give it an address (that does not conflict with existing IP)
sudo ip link set dev tun0 up # bring the if up
ip route get 10.0.3.50 # check that packets to 10.0.3.x are going through tun0
# 10.0.3.50 dev tun0 src 10.0.3.0 uid 1000
ping 10.0.3.50 # leave this running in another shell to be able to see the effect of the next example, nobody is responding to the ping
tun0
已创建,所有发往目标 IP 地址 10.0.3.x 的数据包都将路由到 tun0
。
要从用户空间程序向此接口(interface)读取/写入数据包,您需要使用ioctl()
与/dev/net/tun
设备文件进行交互。下面是一个示例,它将读取到达 tun0
接口(interface)的数据包并打印大小:
#include <fcntl.h> /* O_RDWR */
#include <string.h> /* memset(), memcpy() */
#include <stdio.h> /* perror(), printf(), fprintf() */
#include <stdlib.h> /* exit(), malloc(), free() */
#include <sys/ioctl.h> /* ioctl() */
#include <unistd.h> /* read(), close() */
/* includes for struct ifreq, etc */
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
int tun_open(char *devname)
{
struct ifreq ifr;
int fd, err;
if ( (fd = open("/dev/net/tun", O_RDWR)) == -1 ) {
perror("open /dev/net/tun");exit(1);
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN;
strncpy(ifr.ifr_name, devname, IFNAMSIZ); // devname = "tun0" or "tun1", etc
/* ioctl will use ifr.if_name as the name of TUN
* interface to open: "tun0", etc. */
if ( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) == -1 ) {
perror("ioctl TUNSETIFF");close(fd);exit(1);
}
/* After the ioctl call the fd is "connected" to tun device specified
* by devname ("tun0", "tun1", etc)*/
return fd;
}
int main(int argc, char *argv[])
{
int fd, nbytes;
char buf[1600];
fd = tun_open("tun0"); /* devname = ifr.if_name = "tun0" */
printf("Device tun0 opened\n");
while(1) {
nbytes = read(fd, buf, sizeof(buf));
printf("Read %d bytes from tun0\n", nbytes);
}
return 0;
}
如果您正在运行 ping 10.0.3.1
或 ping 10.0.3.40
,您将定期看到 Read 88 bytes from tun0
。
您还可以通过 nc -u 10.0.3.3 2222
并键入 text + Enter 来测试使用 netcat UDP。
如果未打印任何内容,则很可能是分配给 tun0 的 id 地址/ip 范围不可访问/不可路由/不可寻址。确保 ip route get 10.0.3.4
显示 10.0.3.4 dev tun0
表明 linux 内核知道到 10.0.3.4 的数据包应该发送到 tun0 设备。
删除 tun0
做
sudo ip link set dev tun0 down
sudo ip tuntap del mode tun dev tun0
关于c - 如何与 Linux tun 驱动程序交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1003684/
我已经在我的 Windows 7 机器上安装了 TAP-WIN32 Adapter V9。我想用 TUN 从接口(interface)读取 IP 数据包。我遵循了 C# 示例 http://www.v
TAP/TUN 设备的“目标地址”的目的是什么? Pytun让您轻松设置抽头/调谐设备的参数: tun = TapTunDevice(name='mytun') tun.addr = '10.66.6
我将尝试使用 openvpn 连接到 HideMyAss VPN 构建 docker 镜像。查看 Dockerfile: FROM ubuntu:16.04 RUN apt-get update RU
我使用 VpnService 创建了一个 TUN 设备.为什么 TUN 接口(interface)在我设备的其他网络接口(interface)中具有最高优先级? 更新 #1 这就是我配置 TUN 设备
将 IP 数据包(从用户空间)发送到 tun 设备和使用原始套接字有什么区别? 为了通过用户空间传输 IP 数据包。为什么我会使用一种方法而不是另一种方法? 原始套接字: s = socket(AF_
我正在关注http://backreference.org/2010/03/26/tuntap-interface-tutorial/ 当我以 root 身份运行以下代码时,它成功获取了一个 fd(通
我正在尝试拦截通过 TUN 接口(interface)的数据包。我想将原始数据包信息转换为可读信息,以便稍后使用。 我使用以下代码: int main(){ char tun_name[IFN
我在这里引用示例:https://www.kernel.org/doc/Documentation/networking/tuntap.txt (这很旧),但此处的链接表明同样的事情也应该有效:( h
我目前正在探索不同的网络接口(interface)并遇到了 tun/tap。我对创建套接字的一部分感到困惑。 我们能否在分配给 tun/tap 接口(interface)的 IP 地址上创建具有已知端
我想创建一个 TUN 界面我在 adb 中键入命令,如下所示 mkdir -p /dev/net busybox mknod /dev/net/tun1 c 10 200 chmod 666 /dev
我很难解决这个问题 - 我正在尝试编写一个程序来与 Linux 隧道驱动程序交互。在最基本的层面上,我只想创建一个能够通过网络隧道传输数据的应用程序。但是,对于如何正确设置隧道驱动程序以实现此目的,我
我在理解 tun/tap 设备的实际工作方式时遇到了一点问题,这是来自 wiki 的内容 How does Virtual network device actually work ? Virtual
我不清楚 tun/tap 如何界面正在工作。从维基百科,我得到了这个: 操作系统通过 TUN/TAP 设备发送的数据包被传递到用户空间程序,该程序将自身附加到设备上。用户空间程序也可以将数据包传递到
短版 .是否有 Linux API 允许从文件描述符中读取(类似于 read())而不实际从操作系统缓冲区中删除数据?某种方式将 read() 拆分为等价于 front() (读取而不删除)和 pop
我喜欢用 C++ 程序创建 TUN/TAP 接口(interface)。我在网上找到了一个直接的教程 http://backreference.org/2010/03/26/tuntap-interf
我正在尝试通过 TUN device 以编程方式构建和发送 IP 数据包. 我已经设置了 TUN 设备和正确的路由: # ip tuntap add mode tun tun0 # ip link s
我正在尝试使用 AX.25 封装创建 TUN 网络设备。有效的是:- 创 build 备- 将其封装设置为 ax25 什么不起作用是设置它的硬件地址。这在 ax.25 通信中至关重要,因为它用于唯一寻
我已使用 连接到现有的 Tap 设备 fd = open(...) 现在我想逐包读取它。如果我使用 read(fd,buffer,sizeof(buffer)); 我不会准确读取 1 个数据包。 我怎
我正在尝试使我用来通过 VPN 连接到我大学的网络的工具成为可能。问题是,我只能生成一个 tun/tap 设备。该工具本身不支持使用特定的网络接口(interface),但它支持使用 HTTP 代理。
我正在尝试使用 tun/tap 将 TCP 数据包读入结构, 因此 IFF_TUN 标志设置为使用 tun 设备(无以太网头)。 我有这样的结构(我不关心字节序问题): Tcp header : st
我是一名优秀的程序员,十分优秀!