2

我之前在 Linux 上使用过 tun 设备,需要在 Windows 上移植我的代码。我已经在 Windows 上安装了 OpenVPN 的 tun/tap 驱动程序,但我无法弄清楚如何在 Windows C 程序中为该 tun 设备创建设备句柄。

基本上我需要帮助创建 tun 设备并获取句柄。

我在 Linux 上使用的代码如下(类似于 kernel.org 上的示例)

int mktun (char * dev, int flags, struct ifreq * ifr) {

    int fd, stat;
    char * clonedev = "/dev/net/tun";


    /* Get the file descriptor from the tun clone to use as input to ioctl() */
    if ( (fd = open(clonedev, O_RDWR) ) < 0 )
        return fd;

    /* Now prepare the structure ifreq for ioctl() */
    memset(ifr, 0, sizeof(*ifr));                   /* reset memory to 0 */
    ifr->ifr_flags = flags;             /* set the flags IFF_TUN or IFF_TAP and IFF_NO_PI */

    if (*dev)
        strcpy(ifr->ifr_name, dev);

    /* Now we try and create a device */
    if ( (stat = ioctl(fd, TUNSETIFF, (void *) ifr) ) < 0 ) {
        perror("ioctl()");
        close(fd);
        return stat;
    }

    /* Now write back the name of the interface to dev just to be sure */
    strcpy(dev, ifr->ifr_name);

    /* Now return the file descriptor that can be used to talk to the tun interface */
    return fd;
}
4

0 回答 0