From d2ac4607bbdbdcf1de3864784b3464275857a4ba Mon Sep 17 00:00:00 2001 From: Harshal Sheth Date: Sat, 15 Aug 2015 17:05:24 -0400 Subject: [PATCH 1/3] Multi-platform support --- syscalls_other.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 syscalls_other.go diff --git a/syscalls_other.go b/syscalls_other.go new file mode 100644 index 0000000..d291ead --- /dev/null +++ b/syscalls_other.go @@ -0,0 +1,19 @@ +// +build !linux + +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 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 From e3fcc2a15572c30c928b6b3d7d750de67b12ad60 Mon Sep 17 00:00:00 2001 From: Harshal Sheth Date: Sat, 7 Nov 2015 19:20:00 -0500 Subject: [PATCH 2/3] 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: From 606f940f3eac74cddaddd5b705510631cce07c2b Mon Sep 17 00:00:00 2001 From: Harshal Sheth Date: Sat, 7 Nov 2015 19:21:28 -0500 Subject: [PATCH 3/3] Fixed small typo file in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dc3df84..8ebd252 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ * exposes standard interfaces; plays well with standard packages like `io`, `bufio`, etc.. * does not handle memory management (allocating/destructing slice). It's up to user to decide how to deal with buffers; whether to use GC. -`water/waterutil` has some useful functions to interpret MAC farme headers and IP packet headers. It also contains some constants such as protocol numbers and ethernet frame types. +`water/waterutil` has some useful functions to interpret MAC frame headers and IP packet headers. It also contains some constants such as protocol numbers and ethernet frame types. ## Installation ```