diff options
Diffstat (limited to 'Godeps/_workspace/src/github.com/jackpal/gateway')
6 files changed, 206 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/github.com/jackpal/gateway/LICENSE b/Godeps/_workspace/src/github.com/jackpal/gateway/LICENSE new file mode 100644 index 000000000..c9efac32e --- /dev/null +++ b/Godeps/_workspace/src/github.com/jackpal/gateway/LICENSE @@ -0,0 +1,27 @@ +// Copyright (c) 2010 Jack Palevich. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Godeps/_workspace/src/github.com/jackpal/gateway/README.md b/Godeps/_workspace/src/github.com/jackpal/gateway/README.md new file mode 100644 index 000000000..49cc0b85c --- /dev/null +++ b/Godeps/_workspace/src/github.com/jackpal/gateway/README.md @@ -0,0 +1,7 @@ +# gateway + +A very simple library for discovering the IP address of the local LAN gateway. + +Provides implementations for Linux, OS X (Darwin) and Windows. + +Pull requests for other OSs happily considered! diff --git a/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_darwin.go b/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_darwin.go new file mode 100644 index 000000000..fc6ef68d9 --- /dev/null +++ b/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_darwin.go @@ -0,0 +1,40 @@ +package gateway + +import ( + "bytes" + "io/ioutil" + "net" + "os/exec" +) + +func DiscoverGateway() (ip net.IP, err error) { + routeCmd := exec.Command("route", "-n", "get", "0.0.0.0") + stdOut, err := routeCmd.StdoutPipe() + if err != nil { + return + } + if err = routeCmd.Start(); err != nil { + return + } + output, err := ioutil.ReadAll(stdOut) + if err != nil { + return + } + + // Darwin route out format is always like this: + // route to: default + // destination: default + // mask: default + // gateway: 192.168.1.1 + outputLines := bytes.Split(output, []byte("\n")) + for _, line := range outputLines { + if bytes.Contains(line, []byte("gateway:")) { + gatewayFields := bytes.Fields(line) + ip = net.ParseIP(string(gatewayFields[1])) + break + } + } + + err = routeCmd.Wait() + return +} diff --git a/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_linux.go b/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_linux.go new file mode 100644 index 000000000..333bde1dc --- /dev/null +++ b/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_linux.go @@ -0,0 +1,75 @@ +package gateway + +import ( + "bytes" + "io/ioutil" + "net" + "os/exec" +) + +func discoverGatewayUsingIp() (ip net.IP, err error) { + routeCmd := exec.Command("ip", "route", "show") + stdOut, err := routeCmd.StdoutPipe() + if err != nil { + return + } + if err = routeCmd.Start(); err != nil { + return + } + output, err := ioutil.ReadAll(stdOut) + if err != nil { + return + } + + // Linux 'ip route show' format looks like this: + // default via 192.168.178.1 dev wlp3s0 metric 303 + // 192.168.178.0/24 dev wlp3s0 proto kernel scope link src 192.168.178.76 metric 303 + outputLines := bytes.Split(output, []byte("\n")) + for _, line := range outputLines { + if bytes.Contains(line, []byte("default")) { + ipFields := bytes.Fields(line) + ip = net.ParseIP(string(ipFields[2])) + break + } + } + err = routeCmd.Wait() + return +} + +func discoverGatewayUsingRoute() (ip net.IP, err error) { + routeCmd := exec.Command("route", "-n") + stdOut, err := routeCmd.StdoutPipe() + if err != nil { + return + } + if err = routeCmd.Start(); err != nil { + return + } + output, err := ioutil.ReadAll(stdOut) + if err != nil { + return + } + + // Linux route out format is always like this: + // Kernel IP routing table + // Destination Gateway Genmask Flags Metric Ref Use Iface + // 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 + outputLines := bytes.Split(output, []byte("\n")) + for _, line := range outputLines { + if bytes.Contains(line, []byte("0.0.0.0")) { + ipFields := bytes.Fields(line) + ip = net.ParseIP(string(ipFields[1])) + break + } + } + err = routeCmd.Wait() + return +} + +func DiscoverGateway() (ip net.IP, err error) { + ip, err = discoverGatewayUsingRoute() + if err != nil { + ip, err = discoverGatewayUsingIp() + } + return +} diff --git a/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_unimplemented.go b/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_unimplemented.go new file mode 100644 index 000000000..35042b910 --- /dev/null +++ b/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_unimplemented.go @@ -0,0 +1,14 @@ +// +build !darwin,!linux,!windows + +package gateway + +import ( + "fmt" + "net" + "runtime" +) + +func DiscoverGateway() (ip net.IP, err error) { + err = fmt.Errorf("DiscoverGateway not implemented for OS %s", runtime.GOOS) + return +} diff --git a/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_windows.go b/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_windows.go new file mode 100644 index 000000000..282c8f685 --- /dev/null +++ b/Godeps/_workspace/src/github.com/jackpal/gateway/gateway_windows.go @@ -0,0 +1,43 @@ +package gateway + +import ( + "bytes" + "io/ioutil" + "net" + "os/exec" +) + +func DiscoverGateway() (ip net.IP, err error) { + routeCmd := exec.Command("route", "print", "0.0.0.0") + stdOut, err := routeCmd.StdoutPipe() + if err != nil { + return + } + if err = routeCmd.Start(); err != nil { + return + } + output, err := ioutil.ReadAll(stdOut) + if err != nil { + return + } + + // Windows route output format is always like this: + // =========================================================================== + // Active Routes: + // Network Destination Netmask Gateway Interface Metric + // 0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.100 20 + // =========================================================================== + // I'm trying to pick the active route, + // then jump 2 lines and pick the third IP + // Not using regex because output is quite standard from Windows XP to 8 (NEEDS TESTING) + outputLines := bytes.Split(output, []byte("\n")) + for idx, line := range outputLines { + if bytes.Contains(line, []byte("Active Routes:")) { + ipFields := bytes.Fields(outputLines[idx+2]) + ip = net.ParseIP(string(ipFields[2])) + break + } + } + err = routeCmd.Wait() + return +} |