Code Monkey home page Code Monkey logo

go-netroute's Introduction

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)

go-netroute's People

Contributors

7sunarni avatar dependabot[bot] avatar fhs avatar libp2p-mgmt-read-write[bot] avatar maceip avatar marten-seemann avatar snyh avatar stebalien avatar web-flow avatar web3-bot avatar willscott avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-netroute's Issues

Cannot find the route, in the case of many Iface

In Linux:

Gateway selection procedure:

go-netroute/common.go

Lines 142 to 155 in 263be50

for _, rt := range routes {
if rt.InputIface != 0 && rt.InputIface != inputIndex {
continue
}
if src != nil && rt.Src != nil && !rt.Src.Contains(src) {
continue
}
if rt.Dst != nil && !rt.Dst.Contains(dst) {
continue
}
if rt.IsMoreSpecThan(mostSpecificRt) {
mostSpecificRt = rt
}
}

But I find that I have a route that is all nill and has priority 0 (I don't know why):

image

I try to insert the following code to skip this route:

                if rt.Dst == nil && rt.Src == nil && rt.PrefSrc == nil && rt.Gateway == nil {
			continue
		}

It works.

wrong route rule selecting

On some android devices, the route table is showing below (dump as slice of rtInfo)

ALL V4 ROUTE: 
&{Src:<nil> Dst:172.17.0.0/16 Gateway:<nil> PrefSrc:172.17.0.1 InputIface:0 OutputIface:7 Priority:0}
&{Src:<nil> Dst:127.0.0.0/8 Gateway:<nil> PrefSrc:127.0.0.1 InputIface:0 OutputIface:1 Priority:0}
&{Src:<nil> Dst:127.0.0.1/32 Gateway:<nil> PrefSrc:127.0.0.1 InputIface:0 OutputIface:1 Priority:0}
&{Src:<nil> Dst:127.255.255.255/32 Gateway:<nil> PrefSrc:127.0.0.1 InputIface:0 OutputIface:1 Priority:0}
&{Src:<nil> Dst:172.17.0.1/32 Gateway:<nil> PrefSrc:172.17.0.1 InputIface:0 OutputIface:7 Priority:0}
&{Src:<nil> Dst:172.17.255.255/32 Gateway:<nil> PrefSrc:172.17.0.1 InputIface:0 OutputIface:7 Priority:0}
&{Src:<nil> Dst:192.168.1.145/32 Gateway:<nil> PrefSrc:192.168.1.145 InputIface:0 OutputIface:3 Priority:0}
&{Src:<nil> Dst:192.168.1.255/32 Gateway:<nil> PrefSrc:192.168.1.145 InputIface:0 OutputIface:3 Priority:0}
&{Src:<nil> Dst:<nil> Gateway:192.168.1.1 PrefSrc:192.168.1.145 InputIface:0 OutputIface:3 Priority:600}
&{Src:<nil> Dst:192.168.1.0/24 Gateway:<nil> PrefSrc:192.168.1.145 InputIface:0 OutputIface:3 Priority:600}

ALL V6 ROUTE:
&{Src:<nil> Dst:fe80::42:4fff:fe18:4c60/128 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:7 Priority:0}
&{Src:<nil> Dst:fc03:1136:3849:fb4:6747:c508:ffef:0/128 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:741 Priority:0}
&{Src:<nil> Dst:240e:36a:140b:5800::169/128 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:3 Priority:0}
&{Src:<nil> Dst:240e:36a:140b:5800:5fb7:95ba:2f1e:8679/128 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:3 Priority:0}
&{Src:<nil> Dst:::1/128 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:0}
&{Src:<nil> Dst:fe80::cd13:d340:2662:fa5f/128 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:741 Priority:0}
&{Src:<nil> Dst:fe80::1b61:8f5b:8d0c:47dd/128 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:3 Priority:0}
&{Src:<nil> Dst:fe80::/64 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:741 Priority:256}
&{Src:<nil> Dst:ff00::/8 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:3 Priority:256}
&{Src:<nil> Dst:fc03:1136:3800::/40 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:741 Priority:256}
&{Src:<nil> Dst:::1/128 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:256}
&{Src:<nil> Dst:ff00::/8 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:7 Priority:256}
&{Src:<nil> Dst:ff00::/8 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:741 Priority:256}
&{Src:<nil> Dst:fe80::/64 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:7 Priority:256}
&{Src:<nil> Dst:240e:36a:140b:5800::/64 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:3 Priority:600}
&{Src:<nil> Dst:<nil> Gateway:fe80::3ecd:57ff:fef8:e384 PrefSrc:<nil> InputIface:0 OutputIface:3 Priority:600}
&{Src:<nil> Dst:240e:36a:140b:5800::169/128 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:3 Priority:600}
&{Src:<nil> Dst:fe80::/64 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:3 Priority:1024}
&{Src:<nil> Dst:240e:36a:140b:5800::/64 Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:36 Priority:1024}
&{Src:<nil> Dst:<nil> Gateway:fe80::3ecd:57ff:fef8:e384 PrefSrc:<nil> InputIface:0 OutputIface:36 Priority:1024}
&{Src:<nil> Dst:<nil> Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:4294967295}
&{Src:<nil> Dst:<nil> Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:4294967295}
&{Src:<nil> Dst:<nil> Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:4294967295}
&{Src:<nil> Dst:<nil> Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:4294967295}
&{Src:<nil> Dst:<nil> Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:4294967295}
&{Src:<nil> Dst:<nil> Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:4294967295}
&{Src:<nil> Dst:<nil> Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:4294967295}
&{Src:<nil> Dst:<nil> Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:4294967295}
&{Src:<nil> Dst:<nil> Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:4294967295}
&{Src:<nil> Dst:<nil> Gateway:<nil> PrefSrc:<nil> InputIface:0 OutputIface:1 Priority:4294967295}

when query route of :: the result is ::1 which is wrong. (it should be address of OutputIface 3)

Because any of the rule's dst is not contain the ::, the rules are compared by candSpec and curSpec. But the system's rules have many "empty dst" rules like above.

the go-netroute will choose the laste mpty dst rule because all they mask size is zero and so they are equal.

We should compare rule priority if other metrics are the same.

MAC Address Lookup Failed on Windows

		if bytes.Equal(iface.HardwareAddr, ifRow.PhysAddr[:]) {
			return &iface
		}

Code above had error because ifRow.PhysAddr has code:

PhysAddr        [MAXLEN_PHYSADDR]byte

and MAXLEN_PHYSADDR is set by const MAXLEN_PHYSADDR = 8.

In my case, the last two bytes needs to be removed, thus:

iface.HardwareAddr, ifRow.PhysAddr[:6]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.