Python TUN/TAP Module for Linux

OK, I wrote this module a while ago just to test extending python with c. I've posted it in case anyone has a use for it. It isn't extensively tested.

It allows you to open a low level network device (a tun or tap device) from your python program. Your usual network tools can configure the interface (ifconfig etc.), but packets sent to it end up being readable by your python program, and your program can write data to it which appears on the host's network interface.

This could be used for low level networking experimentation, writing tunnling programs to tunnel ip traffic over unusual paths etc.

If anyone actually uses it, let me know at alex at king dot net dot nz

You can download it at http://alex.king.net.nz/tuntap-0.1.tar.gz

Postscript

Since I wrote that, I discovered you can use the tun device in standard python without the C extension. I _think_ this has only been in recent (2.6 maybe 2.5) python releases.

Here is an extract of some of my current code:

TUNSETIFF   = 0x400454ca
IFF_TUN     = 0x0001

class Link:
    def __init__(self,interfaceName):
       self.netDeviceFD = os.open("/dev/net/tun", os.O_RDWR)
       ifs = ioctl(self.netDeviceFD, TUNSETIFF, struct.pack("16sH", interfaceName, IFF_TUN))
       print "Using interface %s" % ifs[:16].strip("\x00")
       ...
       packet = os.read(self.netDeviceFD,1500)