diff options
Diffstat (limited to 'src/tun_linux.go')
-rw-r--r-- | src/tun_linux.go | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/tun_linux.go b/src/tun_linux.go index accc6c6..a728a48 100644 --- a/src/tun_linux.go +++ b/src/tun_linux.go @@ -56,6 +56,10 @@ type NativeTun struct { events chan TUNEvent // device related events } +func (tun *NativeTun) File() *os.File { + return tun.fd +} + func (tun *NativeTun) RoutineNetlinkListener() { sock := int(C.bind_rtmgrp()) if sock < 0 { @@ -222,7 +226,7 @@ func (tun *NativeTun) MTU() (int, error) { val := binary.LittleEndian.Uint32(ifr[16:20]) if val >= (1 << 31) { - return int(val-(1<<31)) - (1 << 31), nil + return int(toInt32(val)), nil } return int(val), nil } @@ -248,6 +252,29 @@ func (tun *NativeTun) Close() error { return nil } +func CreateTUNFromFile(name string, fd *os.File) (TUNDevice, error) { + device := &NativeTun{ + fd: fd, + name: name, + events: make(chan TUNEvent, 5), + errors: make(chan error, 5), + } + + // start event listener + + var err error + device.index, err = getIFIndex(device.name) + if err != nil { + return nil, err + } + + go device.RoutineNetlinkListener() + + // set default MTU + + return device, device.setMTU(DefaultMTU) +} + func CreateTUN(name string) (TUNDevice, error) { // open clone device |