netroute

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2025 License: BSD-3-Clause Imports: 11 Imported by: 77 modules, 109 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Netroute

Build Status

A cross-platform implementation of the gopacket/routing.Router interface.

This library is derived from gopacket for linux, x/net/route for mac, and iphlpapi.dll for windows.

Table of Contents

Install

go get github.com/libp2p/go-netroute

Usage

To be used for querying the local OS routing table.

import (
    netroute "github.com/libp2p/go-netroute"
)

func main() {
    r, err := netroute.New()
    if err != nil {
        panic(err)
    }
    iface, gw, src, err := r.Route(net.IPv4(127, 0, 0, 1))
    fmt.Printf("%v, %v, %v, %v\n", iface, gw, src, err)
}

Documentation

See the gopacket interface for thoughts on design, and godoc for API documentation.

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.

Small note: If editing the README, please conform to the standard-readme specification.

License

BSD © Will Scott, and the Gopacket authors (i.e. Google)

Documentation

Overview

Originally found in https://github.com/google/gopacket/blob/master/routing/routing.go

  • Route selection modified to choose most selective route to break ties when route priority is insufficient.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Router added in v0.3.0

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 implements simple IPv4/IPv6 routing based on the kernel's routing table. This routing library has very few features and may actually route incorrectly in some cases, but it should work the majority of the time.

This interface was copied from https://github.com/google/gopacket

Copyright 2012 Google, Inc. All rights reserved.

Use of this source code is governed by a BSD-style license that can be found in the LICENSE file in the root of the source tree.

func New

func New() (Router, error)

Jump to

Keyboard shortcuts

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