Skip to content

Commit

Permalink
Add a max unknown hops
Browse files Browse the repository at this point in the history
This is to prevent continue discovery if more than 2 hops do not reply
  • Loading branch information
Maartje Eyskens committed Aug 24, 2018
1 parent 5f0248c commit c349816
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
3 changes: 2 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
INTERVAL = 100 * time.Millisecond
HOP_SLEEP = time.Nanosecond
MAX_HOPS = 64
MAX_UNKNOWN_HOPS = 10
RING_BUFFER_SIZE = 50
jsonFmt = false
)
Expand All @@ -29,7 +30,7 @@ var RootCmd = &cobra.Command{
if len(args) != 1 {
return errors.New("No target provided")
}
m, ch := mtr.NewMTR(args[0], TIMEOUT, INTERVAL, HOP_SLEEP, MAX_HOPS, RING_BUFFER_SIZE)
m, ch := mtr.NewMTR(args[0], TIMEOUT, INTERVAL, HOP_SLEEP, MAX_HOPS, MAX_UNKNOWN_HOPS, RING_BUFFER_SIZE)
if jsonFmt {
go func(ch chan struct{}) {
for {
Expand Down
30 changes: 14 additions & 16 deletions mtr/mtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ type MTR struct {
Statistic map[int]*hop.HopStatistic `json:"statistic"`
ringBufferSize int
maxHops int
maxUnknownHops int
}

func NewMTR(addr string, timeout time.Duration, interval time.Duration, hopsleep time.Duration, maxHops, ringBufferSize int) (*MTR, chan struct{}) {
func NewMTR(addr string, timeout time.Duration, interval time.Duration, hopsleep time.Duration, maxHops, maxUnknownHops, ringBufferSize int) (*MTR, chan struct{}) {
return &MTR{
interval: interval,
timeout: timeout,
Expand All @@ -34,6 +35,7 @@ func NewMTR(addr string, timeout time.Duration, interval time.Duration, hopsleep
Statistic: map[int]*hop.HopStatistic{},
maxHops: maxHops,
ringBufferSize: ringBufferSize,
maxUnknownHops: maxUnknownHops,
}, make(chan struct{})
}

Expand Down Expand Up @@ -89,27 +91,15 @@ func (m *MTR) Run(ch chan struct{}, count int) {
m.ping(ch, count-1)
}

// discover discovers all hops on the route
func (m *MTR) discover(ch chan struct{}) {
ipAddr := net.IPAddr{IP: net.ParseIP(m.Address)}
pid := os.Getpid() & 0xffff
ttlDoubleBump := false
unknownHopsCount := 0
for ttl := 1; ttl < m.maxHops; ttl++ {
time.Sleep(m.hopsleep)
hopReturn, err := imcp.SendIMCP("0.0.0.0", &ipAddr, ttl, pid, m.timeout)
if err != nil || !hopReturn.Success {
if ttlDoubleBump {
break
}
m.mutex.Lock()
s := m.registerStatistic(ttl, hopReturn)
s.Dest = &ipAddr
s.PID = pid
m.mutex.Unlock()
ch <- struct{}{}
ttlDoubleBump = true
continue
}
ttlDoubleBump = false

m.mutex.Lock()
s := m.registerStatistic(ttl, hopReturn)
s.Dest = &ipAddr
Expand All @@ -119,5 +109,13 @@ func (m *MTR) discover(ch chan struct{}) {
if hopReturn.Addr == m.Address {
break
}
if err != nil || !hopReturn.Success {
unknownHopsCount++
if unknownHopsCount > m.maxUnknownHops {
break
}
continue
}
unknownHopsCount = 0
}
}

0 comments on commit c349816

Please sign in to comment.