gpt4 book ai didi

c - tun 程序未创 build 备节点

转载 作者:行者123 更新时间:2023-12-02 01:58:08 25 4
gpt4 key购买 nike

我正在关注http://backreference.org/2010/03/26/tuntap-interface-tutorial/

当我以 root 身份运行以下代码时,它成功获取了一个 fd(通常为 3),但它没有创建/dev/tun77 设备。

应该吗?

#include <stdio.h>
#include <stdlib.h>
#include <net/if.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <linux/if_tun.h>
#include <linux/ioctl.h>

#define IFNAMSIZ 16

int tun_alloc(char *dev, int flags) {

struct ifreq ifr;
int fd, err;
char *clonedev = "/dev/net/tun";

/* Arguments taken by the function:
*
* char *dev: the name of an interface (or '\0'). MUST have enough
* space to hold the interface name if '\0' is passed
* int flags: interface flags (eg, IFF_TUN etc.)
*/

/* open the clone device */
if( (fd = open(clonedev, O_RDWR)) < 0 ) {
return fd;
}

/* preparation of the struct ifr, of type "struct ifreq" */
memset(&ifr, 0, sizeof(ifr));

ifr.ifr_flags = flags; /* IFF_TUN or IFF_TAP, plus maybe IFF_NO_PI */

if (*dev) {
/* if a device name was specified, put it in the structure; otherwise,
* the kernel will try to allocate the "next" device of the
* specified type */
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}

/* try to create the device */
if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
close(fd);
return err;
}

/* if the operation was successful, write back the name of the
* interface to the variable "dev", so the caller can know
* it. Note that the caller MUST reserve space in *dev (see calling
* code below) */
strcpy(dev, ifr.ifr_name);

/* this is the special file descriptor that the caller will use to talk
* with the virtual interface */
return fd;
}

int main(void) {
char tun_name[IFNAMSIZ];
int nread, tun_fd;
char buffer[2048];
/* Connect to the device */
strcpy(tun_name, "tun77");
tun_fd = tun_alloc(tun_name, IFF_TUN | IFF_NO_PI); /* tun interface */

if (tun_fd < 0){
perror("Allocating interface");
exit(1);
} else {
printf("connected to %s on fd: %i\n", tun_name, tun_fd);
}

/* Now read data coming from the kernel */
while (1) {
/* Note that "buffer" should be at least the MTU size of the interface, eg 1500 bytes */
nread = read(tun_fd, buffer, sizeof(buffer));
if (nread < 0) {
perror("Reading from interface");
close(tun_fd);
exit(1);
}

/* Do whatever with the data */
printf("Read %d bytes from device %s\n", nread, tun_name);
}
return EXIT_SUCCESS;
}

最佳答案

瓦尔德纳在 http://backreference.org/2010/03/26/tuntap-interface-tutorial/ 上回答了这个问题与:

都不是。 Linux 中的网络接口(interface)不会出现在/dev 下;您将看到的唯一内容是/dev/net/tun,这是创建 tun/tap 接口(interface)的第一步应该打开的设备。

如果您运行示例代码,您将能够在程序运行时查看并配置您使用“ip link”创建的接口(interface);当程序终止时,界面消失。

或者,如所解释的,可以使接口(interface)持久化,在这种情况下,它将在程序终止后继续存在。

无论如何,/dev 下不会创建任何设备(除了已经提到的/dev/net/tun)。

关于c - tun 程序未创 build 备节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com