routing

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package routing provides a very basic but mostly functional implementation of a routing table for IPv4/IPv6 addresses. It uses a routing table pulled from the kernel via netlink to find the correct interface, gateway, and preferred source IP address for packets destined to a particular location.

The routing package is meant to be used with applications that are sending raw packet data, which don't have the benefit of having the kernel route packets for them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindInterfaceByIp

func FindInterfaceByIp(ip net.IP) (*net.Interface, error)

func FindSourceIpForIp

func FindSourceIpForIp(route *Route, ip net.IP) (net.IP, error)

func GetOutboundIPs

func GetOutboundIPs() (net.IP, net.IP, error)

Types

type Route

type Route struct {
	Type             RouteType
	Default          bool
	NetworkInterface *net.Interface
	Destination      string
	Gateway          string
	Flags            string
	Expire           string
	DefaultSourceIP  net.IP
}

func FindRouteForIp

func FindRouteForIp(ip net.IP, routes []*Route) (*Route, error)

func FindRouteWithHwAndIp

func FindRouteWithHwAndIp(hardwareAddr net.HardwareAddr, src net.IP, routes []*Route) (*Route, error)

type RouteType

type RouteType string
const (
	IPv4 RouteType = "IPv4"
	IPv6 RouteType = "IPv6"
)

func (RouteType) String

func (routeType RouteType) String() string

type Router

type Router interface {
	// Route returns where to route a packet based on the packet's source
	// and destination IP address.
	//
	// Callers may pass in nil for src, in which case the src is treated as
	// either 0.0.0.0 or ::, depending on whether dst is a v4 or v6 address.
	//
	// It returns the interface on which to send the packet, the gateway IP
	// to send the packet to (if necessary), the preferred src IP to use (if
	// available).  If the preferred src address is not given in the routing
	// table, the first IP address of the interface is provided.
	//
	// If an error is encountered, iface, geteway, and
	// preferredSrc will be nil, and err will be set.
	Route(dst net.IP) (iface *net.Interface, gateway, preferredSrc net.IP, err error)

	// RouteWithSrc routes based on source information as well as destination
	// information.  Either or both of input/src can be nil.  If both are, this
	// should behave exactly like Route(dst)
	RouteWithSrc(input net.HardwareAddr, src, dst net.IP) (iface *net.Interface, gateway, preferredSrc net.IP, err error)
}

Router shares the same interface described in https://github.com/google/gopacket

func New

func New() (Router, error)

New creates a new router object. The router returned by New currently does not update its routes after construction... care should be taken for long-running programs to call New() regularly to take into account any changes to the routing table which have occurred since the last New() call.

Jump to

Keyboard shortcuts

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