tracelib

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2020 License: MIT Imports: 13 Imported by: 1

README

tracelib

Traceroute implementation in go including mutli-round trace (returns min/max/avg/lost) and AS number detection both for IPv4 and IPv6. Also expremental implementation of much faster traceroute present (it sends all packets with all possible TTLs at once and total tracroute time is always the same as MaxRTT), look at examples/parallel for more info.

Usage example of regular traceroute (only IPs without hostnames and AS numbers):

hops, err := tracelib.RunTrace("google.com", "0.0.0.0", time.Second, 64, nil)
for i, hop := range hops {
	fmt.Printf("%d. %v(%s)/AS%d %v (final:%v timeout:%v error:%v)\n",
      i+1, hop.Host, hop.Addr, hop.AS, hop.RTT, hop.Final, hop.Timeout, hop.Error)
}

Multiply traces with hostnames and AS numbers:

dnscache := tracelib.NewLookupCache()
rawHops, err := tracelib.RunMultiTrace("homebeat.live", "0.0.0.0", time.Second, 64, dnscache, 5)

hops := tracelib.AggregateMulti(rawHops)

for i, hop := range hops {
	isd := fmt.Sprintf("%d. ", i+1)
	isp := strings.Repeat(" ", len(isd))

	for j, h := range hop {
		prefix := isd
        if j > 0 { prefix = isp }

		fmt.Printf("%s%v(%s)/AS%d %v/%v/%v (final:%v lost %d of %d)\n",
          prefix, h.Host, h.Addr, h.AS, h.MinRTT, h.AvgRTT, h.MaxRTT, h.Final, h.Lost, h.Total)
	}
}

Documentation

Index

Constants

View Source
const (
	// ProtocolICMP icmp protocol id
	ProtocolICMP = 1
	// ProtocolICMP6 icmp protocol id
	ProtocolICMP6 = 58

	// MaxTimeouts sets number of hops without replay before trace termination
	MaxTimeouts = 3
)

Variables

This section is empty.

Functions

func AggregateMulti

func AggregateMulti(hops [][]Hop) [][]MHop

AggregateMulti process result of RunMultiTrace and create aggregated result

func RunMultiTrace

func RunMultiTrace(host string, source string, source6 string, maxrtt time.Duration, maxttl int, DNScache *LookupCache, rounds int, cb Callback) ([][]Hop, error)

RunMultiTrace preforms traceroute to specified host testing each hop several times

func RunPTrace

func RunPTrace(host string, source string, source6 string, maxrtt time.Duration, maxttl int, DNScache *LookupCache, rounds int, icmpID int, delay time.Duration) ([][]Hop, error)

RunPTrace preforms traceroute to specified host by sending all packets at once

Types

type Callback

type Callback func(info Hop, hopnum int, round int)

Callback function called after every hop received

type Hop

type Hop struct {
	Addr    net.Addr
	Host    string
	AS      int64
	RTT     time.Duration
	Final   bool
	Timeout bool
	Down    bool
	Error   error
}

Hop represents each hop of trace

func RunTrace

func RunTrace(host string, source string, source6 string, maxrtt time.Duration, maxttl int, DNScache *LookupCache, cb Callback) ([]Hop, error)

RunTrace preforms traceroute to specified host

type LookupCache

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

LookupCache used to prevent AS-DNS requests for same hosts

func NewLookupCache

func NewLookupCache() *LookupCache

NewLookupCache constructor for LookupCache

func (*LookupCache) LookupAS

func (cache *LookupCache) LookupAS(ip string) int64

LookupAS returns AS number for IP using origin.asn.cymru.com service

func (*LookupCache) LookupHost

func (cache *LookupCache) LookupHost(ip string) string

LookupHost returns AS number for IP using origin.asn.cymru.com service

type MHop

type MHop struct {
	Addr   net.Addr
	Host   string
	AS     int64
	MinRTT time.Duration
	MaxRTT time.Duration
	AvgRTT time.Duration
	Total  int
	Lost   int
	Down   int
	Final  bool
}

MHop represents aggregated result of hop of multiply traces

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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