From e3fcc2a15572c30c928b6b3d7d750de67b12ad60 Mon Sep 17 00:00:00 2001 From: Harshal Sheth Date: Sat, 7 Nov 2015 19:20:00 -0500 Subject: [PATCH] Moved linux specific code to the syscalls_linux.go file. Also removed unneeded constants from the syscalls_other.go file. --- if.go | 22 ++-------------------- syscalls_linux.go | 27 +++++++++++++++++++++++++++ syscalls_other.go | 18 +++++------------- waterutil/doc.go | 6 +++--- 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/if.go b/if.go index 229d141..9f3a97e 100644 --- a/if.go +++ b/if.go @@ -15,32 +15,14 @@ type Interface struct { // If ifName is empty, a default name (tap0, tap1, ... ) will be assigned. // ifName should not exceed 16 bytes. func NewTAP(ifName string) (ifce *Interface, err error) { - file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) - if err != nil { - return nil, err - } - name, err := createInterface(file.Fd(), ifName, cIFF_TAP|cIFF_NO_PI) - if err != nil { - return nil, err - } - ifce = &Interface{isTAP: true, file: file, name: name} - return + return newTAP(ifName) } // Create a new TUN interface whose name is ifName. // If ifName is empty, a default name (tap0, tap1, ... ) will be assigned. // ifName should not exceed 16 bytes. func NewTUN(ifName string) (ifce *Interface, err error) { - file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) - if err != nil { - return nil, err - } - name, err := createInterface(file.Fd(), ifName, cIFF_TUN|cIFF_NO_PI) - if err != nil { - return nil, err - } - ifce = &Interface{isTAP: false, file: file, name: name} - return + return newTUN(ifName) } // Returns true if ifce is a TUN interface, otherwise returns false; diff --git a/syscalls_linux.go b/syscalls_linux.go index 6bf6c57..e69c3d9 100644 --- a/syscalls_linux.go +++ b/syscalls_linux.go @@ -3,6 +3,7 @@ package water import ( + "os" "strings" "syscall" "unsafe" @@ -20,6 +21,32 @@ type ifReq struct { pad [0x28 - 0x10 - 2]byte } +func newTAP(ifName string) (ifce *Interface, err error) { + file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) + if err != nil { + return nil, err + } + name, err := createInterface(file.Fd(), ifName, cIFF_TAP|cIFF_NO_PI) + if err != nil { + return nil, err + } + ifce = &Interface{isTAP: true, file: file, name: name} + return +} + +func newTUN(ifName string) (ifce *Interface, err error) { + file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) + if err != nil { + return nil, err + } + name, err := createInterface(file.Fd(), ifName, cIFF_TUN|cIFF_NO_PI) + if err != nil { + return nil, err + } + ifce = &Interface{isTAP: false, file: file, name: name} + return +} + func createInterface(fd uintptr, ifName string, flags uint16) (createdIFName string, err error) { var req ifReq req.Flags = flags diff --git a/syscalls_other.go b/syscalls_other.go index d291ead..a38ce85 100644 --- a/syscalls_other.go +++ b/syscalls_other.go @@ -2,18 +2,10 @@ package water -const ( - cIFF_TUN = 0 - cIFF_TAP = 0 - cIFF_NO_PI = 0 -) - -type ifReq struct { - Name [0]byte - Flags uint16 - pad [0]byte +func newTAP(ifName string) (ifce *Interface, err error) { + panic("water: tap interface not implemented on this platform") } -func createInterface(fd uintptr, ifName string, flags uint16) (createdIFName string, err error) { - panic("water: createInterface not implemented on this platform") -} \ No newline at end of file +func newTUN(ifName string) (ifce *Interface, err error) { + panic("water: tap interface not implemented on this platform") +} diff --git a/waterutil/doc.go b/waterutil/doc.go index 14574a0..1a7cea1 100644 --- a/waterutil/doc.go +++ b/waterutil/doc.go @@ -8,10 +8,10 @@ TAP - MAC Frame: +----------------------------------------------------------------------------- | Octet |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|... +----------------------------------------------------------------------------- - | Field | MAC Destination | MAC Source |EType| Payload + | Field | MAC Destination | MAC Source |EType| Payload +----------------------------------------------------------------------------- - Single-Tagged -- Octets [12,13] == {0x81, 0x00} + Single-Tagged -- Octets [12,13] == {0x81, 0x00} +----------------------------------------------------------------------------- | Octet |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|... +----------------------------------------------------------------------------- @@ -22,7 +22,7 @@ TAP - MAC Frame: +----------------------------------------------------------------------------- | Octet |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|... +----------------------------------------------------------------------------- - | Field | MAC Destination | MAC Source | Outer Tag | Inner Tag | Payload + | Field | MAC Destination | MAC Source | Outer Tag | Inner Tag | Payload +----------------------------------------------------------------------------- TUN - IPv4 Packet: