Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# MTR
[![Build Status](https://travis-ci.org/tonobo/mtr.svg?branch=master)](https://travis-ci.org/tonobo/mtr)

A MTR implementation written in golang, completely without shell-execs.

Expand Down
20 changes: 13 additions & 7 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ var RootCmd = &cobra.Command{
return err
}
if jsonFmt {
go func(ch chan struct{}) {
go func(ch <-chan struct{}) {
for {
<-ch
_, isOpen := <-ch
if !isOpen {
return
}
}
}(ch)
m.Run(ch, COUNT)
m.Run(COUNT)
s, err := pj.Marshal(m)
if err != nil {
return err
Expand All @@ -59,16 +62,19 @@ var RootCmd = &cobra.Command{
fmt.Println("Start:", time.Now())
tm.Clear()
mu := &sync.Mutex{}
go func(ch chan struct{}) {
go func(ch <-chan struct{}) {
for {
mu.Lock()
<-ch
_, isOpen := <-ch
if !isOpen {
mu.Unlock()
return
}
render(m)
mu.Unlock()
}
}(ch)
m.Run(ch, COUNT)
close(ch)
m.Run(COUNT)
mu.Lock()
render(m)
mu.Unlock()
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module github.com/tonobo/mtr

go 1.16

require (
github.com/buger/goterm v0.0.0-20181115115552-c206103e1f37
github.com/fatih/color v1.7.0 // indirect
github.com/hokaccha/go-prettyjson v0.0.0-20180920040306-f579f869bbfe
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd
golang.org/x/sys v0.0.0-20190220154126-629670e5acc5 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect
)
19 changes: 15 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/hokaccha/go-prettyjson v0.0.0-20180920040306-f579f869bbfe h1:MCgzztuoH5LZNr9AkIaicIDvCfACu11KUCCZQnRHDC0=
github.com/hokaccha/go-prettyjson v0.0.0-20180920040306-f579f869bbfe/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/mattn/go-colorable v0.1.0 h1:v2XXALHHh6zHfYTJ+cSkwtyffnaOyR1MXaA91mTrb8o=
github.com/mattn/go-colorable v0.1.0/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
Expand All @@ -12,7 +14,16 @@ github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd h1:HuTn7WObtcDo9uEEU7rEqL0jYthdXAmZ6PP+meazmaU=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20190220154126-629670e5acc5 h1:3Nsfe5Xa1wTt01QxlAFIY5j9ycDtS+d7mhvI8ZY5bn0=
golang.org/x/sys v0.0.0-20190220154126-629670e5acc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ End of functions from https://github.com/client9/shlib
EOF

PROJECT_NAME="mtr"
OWNER=tonobo
OWNER="tonobo"
REPO="mtr"
BINARY=mtr
FORMAT=tar.gz
Expand Down
9 changes: 6 additions & 3 deletions pkg/hop/hop.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (h *HopStatistic) Render(ptrLookup bool) {
l := fmt.Sprintf("%d", h.RingBufferSize)
gm.Printf("%3d:|-- %-20s %5.1f%% %4d %6.1f %6.1f %6.1f %6.1f %"+l+"s\n",
h.TTL,
fmt.Sprintf("%.20s", h.lookupAddr(ptrLookup, 0)),
fmt.Sprintf("%.20s", h.LookupAddr(ptrLookup, 0)),
h.Loss(),
h.Sent,
h.Last.Elapsed.Seconds()*1000,
Expand All @@ -156,8 +156,11 @@ func (h *HopStatistic) Render(ptrLookup bool) {
)
}

func (h *HopStatistic) lookupAddr(ptrLookup bool, index int) string {
addr := "???"
func (h *HopStatistic) LookupAddr(ptrLookup bool, index int) string {
if h.dnsCache == nil {
h.dnsCache = map[string]string{}
}
addr := ""
if h.Targets[index] != "" {
addr = h.Targets[index]
if ptrLookup {
Expand Down
102 changes: 82 additions & 20 deletions pkg/icmp/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,41 @@ func listenForSpecific6(conn *icmp.PacketConn, deadline time.Time, neededPeer st
}

if x.Type.(ipv6.ICMPType) == ipv6.ICMPTypeTimeExceeded {
body := x.Body.(*icmp.TimeExceeded).Data
x, _ := icmp.ParseMessage(ProtocolIPv6ICMP, body[40:])
switch x.Body.(type) {
msg, ok := x.Body.(*icmp.TimeExceeded)
if !ok { // this should never happen
continue
}

// Be defensive regarding the junk we might end up
// getting here. Make sure the ICMP packet contains. an
// IPv6 packet.
_, err := ipv6.ParseHeader(msg.Data)
if err != nil {
continue
}

echoPkt := msg.Data[ipv6.HeaderLen:]

icmpMsg, err := icmp.ParseMessage(ProtocolIPv6ICMP, echoPkt)
if err != nil {
// We cannot error out here because we
// don't know if this packet was the
// one we were expecting or the one we
// were expecting is still in flight.
//
// To make that decision we need to be
// able to parse the packet.
continue
}

switch body := icmpMsg.Body.(type) {
case *icmp.Echo:
echoBody := x.Body.(*icmp.Echo)
if echoBody.Seq == needSeq && echoBody.ID == id {
if body.Seq == needSeq && body.ID == id {
return peer.String(), []byte{}, nil
}

continue

default:
// ignore
}
Expand Down Expand Up @@ -206,22 +232,58 @@ func listenForSpecific4(conn *icmp.PacketConn, deadline time.Time, neededPeer st
continue
}

if typ, ok := x.Type.(ipv4.ICMPType); ok && typ.String() == "time exceeded" {
body := x.Body.(*icmp.TimeExceeded).Data

index := bytes.Index(body, sent[:4])
if index > 0 {
x, _ := icmp.ParseMessage(ProtocolICMP, body[index:])
switch x.Body.(type) {
case *icmp.Echo:
echoBody := x.Body.(*icmp.Echo)
if echoBody.Seq == needSeq && echoBody.ID == pid {
return peer.String(), []byte{}, nil
}
continue
default:
// ignore
if typ, ok := x.Type.(ipv4.ICMPType); ok && typ == ipv4.ICMPTypeTimeExceeded {
msg, ok := x.Body.(*icmp.TimeExceeded)
if !ok { // this should never happen
continue
}

// Be defensive regarding the junk we might end up
// getting here. Make sure the ICMP packet contains:
// 1. an IPv4 packet
hdr, err := ipv4.ParseHeader(msg.Data)
if err != nil {
continue
}

// 2. containing an ICMP packet
if hdr.Protocol != ProtocolICMP {
continue
}

// 3. with a length that makes sense
echoPkt := msg.Data[hdr.Len:]
if len(sent) < 4 || len(echoPkt) < 4 {
continue
}

// The echo packet should match what we sent.
if !bytes.Equal(sent[:4], echoPkt[:4]) {
continue
}

icmpMsg, err := icmp.ParseMessage(ProtocolICMP, echoPkt)
if err != nil {
// We cannot error out here because we
// don't know if this packet was the
// one we were expecting or the one we
// were expecting is still in flight.
//
// To make that decision we need to be
// able to parse the packet.
continue
}

switch body := icmpMsg.Body.(type) {
case *icmp.Echo:
if body.Seq == needSeq && body.ID == pid {
return peer.String(), []byte{}, nil
}

continue

default:
// ignore
}
}

Expand Down
Loading