【问题标题】:Error creating tun device (Invalid argument)创建 tun 设备时出错(参数无效)
【发布时间】:2015-02-25 09:51:42
【问题描述】:

我在这里引用了这个例子:https://www.kernel.org/doc/Documentation/networking/tuntap.txt(这很老了),但这里的链接表明同样的事情也应该起作用:(http://backreference.org/2010/03/26/tuntap-interface-tutorial/)。 否则,我发现的最新文档很少。

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

int main(void) {
    char *clonedev = "/dev/net/tun"; 
    int fd, err;
    char * dev_name = "tun0";
    struct ifreq ifr;
   if( (fd = open(clonedev, O_RDWR)) < 0 ) {
        exit(1);
   }
   memset(&ifr, 0, sizeof(ifr));
   strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);

   printf("Opened %s \n", ifr.ifr_name);
    if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
        printf("Failed to creat tun device (errno is %d)\n",
             errno);
        perror("ioctl");
        close(fd);
        exit(1);
   }
   printf("device created");
}

输出:

Opened tun0 
Failed to creat tun device (errno is 22)
ioctl: Invalid argument

编辑:Grepping OpenVPN 源代码显示相同的用法:

src/openvpn/tun.c:1681:      if (ioctl (tt->fd, TUNSETIFF, (void *) &ifr) < 0)

【问题讨论】:

    标签: c linux networking


    【解决方案1】:

    你没有这样的东西,你的两个链接都有:

    ifr.ifr_flags = IFF_TUN; 
    

    似乎是获取无效参数错误的一种非常合理的方法。

    【讨论】:

      【解决方案2】:

      您在示例中错过了这一行

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

      所以,在 ioctl() 之前错过了这个

      ifr.ifr_flags = IFF_TUN; 
      

      ifr.ifr_flags = IFF_TAP; 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        相关资源
        最近更新 更多