gateway

package module
v1.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2024 License: BSD-3-Clause Imports: 13 Imported by: 0

README

gateway

A simple library for discovering the IP address of the default gateway.

Example:

package main

import (
    "fmt"

    "github.com/jackpal/gateway"
)

func main() {
    gateway, err := gateway.DiscoverGateway()
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println("Gateway:", gateway.String())
   }
}

Provides implementations for:

  • Darwin (macOS)
  • Dragonfly
  • FreeBSD
  • Linux
  • NetBSD
  • OpenBSD
  • Solaris
  • Windows

Other platforms use an implementation that always returns an error.

Pull requests for other OSs happily considered!

Versions

v1.0.13
  • Add tools/check-cross-compile.sh to check that the code compiles for various OSs.
  • Fix compilation errors exposed by tools/check-cross-compile.sh.
v1.0.12
  • If there are multiple default gateways, Windows now returns the gateway with the lowest metric.
  • Fix solaris build break. (In theory, IDK how to test this easily.)
  • Upgrade to golang 1.21
  • Upgrade golang.org/x/net version, makes dependabot happy. Probably was not any actual security issue because gateway doesn't use any of the APIs of golang.org/x/net that had security issues.
v1.0.11
  • Implement DiscoverInterface for BSD-like OSes.
v1.0.10
  • Fix non-BSD-based builds.
v1.0.9
  • Add go.mod and go.sum files.
  • Use "golang.org/x/net/route" to implement all BSD variants.
    • As a side effect this adds support for Dragonfly and NetBSD.
  • Add example to README.
  • Remove broken continuous integration.
v1.0.8
  • Add support for OpenBSD
  • Linux parser now supports gateways with an IP address of 0.0.0.0
  • Fall back to netstat on darwin systems if route fails.
  • Simplify Linux /proc/net/route parsers.

Documentation

Overview

Code generated by go generate

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiscoverGateway

func DiscoverGateway() (ip net.IP, err error)

DiscoverGateway is the OS independent function to get the default gateway

Example
gateway, err := DiscoverGateway()
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println("Gateway:", gateway.String())
}
Output:

func DiscoverInterface added in v1.0.14

func DiscoverInterface() (ip net.IP, err error)

DiscoverInterface is the OS independent function to call to get the default network interface IP that uses the default gateway

Types

type ErrCantParse added in v1.0.14

type ErrCantParse struct{}

ErrCantParse is returned if the route table is garbage.

func (*ErrCantParse) Error added in v1.0.14

func (*ErrCantParse) Error() string

type ErrInvalidRouteFileFormat added in v1.0.14

type ErrInvalidRouteFileFormat struct {
	// contains filtered or unexported fields
}

ErrInvalidRouteFileFormat is returned if the format of /proc/net/route is unexpected on Linux systems. Please raise an issue.

func (*ErrInvalidRouteFileFormat) Error added in v1.0.14

func (e *ErrInvalidRouteFileFormat) Error() string

type ErrNoGateway added in v1.0.14

type ErrNoGateway struct{}

ErrNoGateway is returned if a valid gateway entry was not found in the route table.

func (*ErrNoGateway) Error added in v1.0.14

func (*ErrNoGateway) Error() string

type ErrNotImplemented added in v1.0.14

type ErrNotImplemented struct{}

ErrNotImplemented is returned if your operating system is not supported by this package. Please raise an issue to request support.

func (*ErrNotImplemented) Error added in v1.0.14

func (*ErrNotImplemented) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL