title: How to use tun device in Windows C program
body: <p>I have used tun devices on Linux before and need to port my code on Windows. I have installed tun/tap driver from OpenVPN on Windows, but I am not able to figure out how to create a device handle for that tun device in Windows C program.</p>
  
  <p>Basically I need help creating the tun device and getting the handle.</p>
  
  <p>The code I use on Linux is as follows (similar to the example on kernel.org)</p>
  
  <pre><code>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) ) &lt; 0 )
          return fd;
  
      /* Now prepare the structure ifreq for ioctl() */
      memset(ifr, 0, sizeof(*ifr));                   /* reset memory to 0 */
      ifr-&gt;ifr_flags = flags;             /* set the flags IFF_TUN or IFF_TAP and IFF_NO_PI */
  
      if (*dev)
          strcpy(ifr-&gt;ifr_name, dev);
  
      /* Now we try and create a device */
      if ( (stat = ioctl(fd, TUNSETIFF, (void *) ifr) ) &lt; 0 ) {
          perror("ioctl()");
          close(fd);
          return stat;
      }
  
      /* Now write back the name of the interface to dev just to be sure */
      strcpy(dev, ifr-&gt;ifr_name);
  
      /* Now return the file descriptor that can be used to talk to the tun interface */
      return fd;
  }
  </code></pre>