Sunday, December 22, 2013

Python, how to open TUN/TAP device

Just a note in case I forget
def open(n):
    TUNSETIFF = 0x400454ca
    IFF_TUN   = 0x0001
    IFF_TAP   = 0x0002
    TUNMODE = IFF_TAP
    MODE = 0
    DEBUG = 0
    f = os.open("/dev/net/tun", os.O_RDWR)
    ifs = ioctl(f, TUNSETIFF, struct.pack("16sH", "tap%d" % n, TUNMODE))
    #ifname = ifs[:16].strip("\x00")
    return f
And then as usual
f1 = open(1)
f2 = open(2)

p = os.read(f1, 65000)
os.write(f2, p)

os.close(f1)
os.close(f2)

No comments:

Post a Comment