I/O with a Tun interface
First, the TUN/TAP interface is documented in the Linux kernel documentation, which is worth a read if you haven't already seen it. The examples are in C, of course, but hopefully still useful.
I see references to the second arguement being
TUNSETIFF
. All I know is that it has to be numeric. What is it asking for?
This is a C library constant, the value of which you can determine with code like the following:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <linux/if.h>#include <linux/if_tun.h>int main(int argc, char **argv) { printf("TUNSETIFF: %d\n", TUNSETIFF); return 0;}
Which on my system returns:
TUNSETIFF: 1074025674
From what I've gathered, the third arguement is supposed to be flags of some sort, but I have not managed to find info on them. Presumably, one flag would be for selecting if it should be a Tun or Tap tunnel. Any info on this?
The third argument is a pointer to a structure that has anifr_name
attribute, containing the device name, and an ifr_flags
attribute containing the flags. The kernel documentation provides thefollowing sample code:
/* Flags: IFF_TUN - TUN device (no Ethernet headers) * IFF_TAP - TAP device * * IFF_NO_PI - Do not provide packet information */ ifr.ifr_flags = IFF_TUN; if( *dev ) strncpy(ifr.ifr_name, dev, IFNAMSIZ); if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){ close(fd); return err; }
You can find the values for the different flags (IFF_TUN
, IFF_TAP
)using the same code from above that we used to find the value ofTUNSETIFF
.
If you are writing code in Perl, you would need a way to create thecorresponding C-compatible structure. My Perl is too rusty to suggestthe proper solution off the top of my head.
However, there appear to be a number of Perl modules available thatsimplify the whole process: