Skip to content

Commit

Permalink
mtr: Add ptr lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
tonobo committed Feb 20, 2019
1 parent 57a3d9f commit 4c84fbe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
16 changes: 6 additions & 10 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package cli

import (
"fmt"
"log"
"net"
"sync"
"time"

Expand All @@ -24,6 +22,7 @@ var (
MAX_HOPS = 64
MAX_UNKNOWN_HOPS = 10
RING_BUFFER_SIZE = 50
PTR_LOOKUP = false
jsonFmt = false
srcAddr = "0.0.0.0"
versionFlag bool
Expand All @@ -38,15 +37,11 @@ var RootCmd = &cobra.Command{
fmt.Printf("MTR Version: %s, build date: %s\n", version, date)
return nil
}
ip := args[0]
if net.ParseIP(args[0]) == nil {
addrs, err := net.LookupHost(args[0])
if err != nil || len(addrs) == 0 {
log.Fatalf("invalid host or ip provided: %s", err)
}
ip = addrs[0]
m, ch, err := mtr.NewMTR(args[0], srcAddr, TIMEOUT, INTERVAL, HOP_SLEEP,
MAX_HOPS, MAX_UNKNOWN_HOPS, RING_BUFFER_SIZE, PTR_LOOKUP)
if err != nil {
return err
}
m, ch := mtr.NewMTR(ip, srcAddr, TIMEOUT, INTERVAL, HOP_SLEEP, MAX_HOPS, MAX_UNKNOWN_HOPS, RING_BUFFER_SIZE)
if jsonFmt {
go func(ch chan struct{}) {
for {
Expand Down Expand Up @@ -93,6 +88,7 @@ func init() {
RootCmd.Flags().IntVar(&MAX_UNKNOWN_HOPS, "max-unknown-hops", MAX_UNKNOWN_HOPS, "Maximal hops that do not reply before stopping to look")
RootCmd.Flags().IntVar(&RING_BUFFER_SIZE, "buffer-size", RING_BUFFER_SIZE, "Cached packet buffer size")
RootCmd.Flags().BoolVar(&jsonFmt, "json", jsonFmt, "Print json results")
RootCmd.Flags().BoolVarP(&PTR_LOOKUP, "ptr", "n", PTR_LOOKUP, "Reverse lookup on host")
RootCmd.Flags().BoolVar(&versionFlag, "version", false, "Print version")
RootCmd.Flags().StringVar(&srcAddr, "address", srcAddr, "The address to be bound the outgoing socket")
}
31 changes: 25 additions & 6 deletions pkg/hop/hop.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type HopStatistic struct {
Packets *ring.Ring
RingBufferSize int
pingSeq int
dnsCache map[string]string
}

type packet struct {
Expand Down Expand Up @@ -124,7 +125,10 @@ func (h *HopStatistic) packets() []*packet {
return v
}

func (h *HopStatistic) Render() {
func (h *HopStatistic) Render(ptrLookup bool) {
if h.dnsCache == nil {
h.dnsCache = map[string]string{}
}
packets := make([]byte, h.RingBufferSize)
i := h.RingBufferSize - 1
h.Packets.Do(func(f interface{}) {
Expand All @@ -137,14 +141,10 @@ func (h *HopStatistic) Render() {
}
i--
})
addr := "???"
if h.Target != "" {
addr = h.Target
}
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,
addr,
fmt.Sprintf("%.20s", h.lookupAddr(ptrLookup)),
h.Loss(),
h.Sent,
h.Last.Elapsed.Seconds()*1000,
Expand All @@ -154,3 +154,22 @@ func (h *HopStatistic) Render() {
packets,
)
}

func (h *HopStatistic) lookupAddr(ptrLookup bool) string {
addr := "???"
if h.Target != "" {
addr = h.Target
if ptrLookup {
if key, ok := h.dnsCache[h.Target]; ok {
addr = key
} else {
names, err := net.LookupAddr(h.Target)
if err == nil && len(names) > 0 {
addr = names[0]
}
}
}
h.dnsCache[h.Target] = addr
}
return addr
}
16 changes: 13 additions & 3 deletions pkg/mtr/mtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ type MTR struct {
ringBufferSize int
maxHops int
maxUnknownHops int
ptrLookup bool
}

func NewMTR(addr, srcAddr string, timeout time.Duration, interval time.Duration, hopsleep time.Duration, maxHops, maxUnknownHops, ringBufferSize int) (*MTR, chan struct{}) {
func NewMTR(addr, srcAddr string, timeout time.Duration, interval time.Duration,
hopsleep time.Duration, maxHops, maxUnknownHops, ringBufferSize int, ptr bool) (*MTR, chan struct{}, error) {
if net.ParseIP(addr) == nil {
addrs, err := net.LookupHost(addr)
if err != nil || len(addrs) == 0 {
return nil, nil, fmt.Errorf("invalid host or ip provided: %s", err)
}
addr = addrs[0]
}
return &MTR{
SrcAddress: srcAddr,
interval: interval,
Expand All @@ -38,7 +47,8 @@ func NewMTR(addr, srcAddr string, timeout time.Duration, interval time.Duration,
maxHops: maxHops,
ringBufferSize: ringBufferSize,
maxUnknownHops: maxUnknownHops,
}, make(chan struct{})
ptrLookup: ptr,
}, make(chan struct{}), nil
}

func (m *MTR) registerStatistic(ttl int, r icmp.ICMPReturn) *hop.HopStatistic {
Expand Down Expand Up @@ -69,7 +79,7 @@ func (m *MTR) Render(offset int) {
for i := 1; i <= len(m.Statistic); i++ {
gm.MoveCursor(1, offset+i)
m.mutex.RLock()
m.Statistic[i].Render()
m.Statistic[i].Render(m.ptrLookup)
m.mutex.RUnlock()
}
}
Expand Down

0 comments on commit 4c84fbe

Please sign in to comment.