Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix several failed hops #3

Merged
merged 6 commits into from
Aug 24, 2018
Merged
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
mtr
*.swp
9 changes: 6 additions & 3 deletions cli.go → cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"errors"
Expand All @@ -9,6 +9,7 @@ import (
tm "github.com/buger/goterm"
pj "github.com/hokaccha/go-prettyjson"
"github.com/spf13/cobra"
"github.com/tonobo/mtr/mtr"
)

var (
Expand All @@ -17,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 @@ -28,7 +30,7 @@ var RootCmd = &cobra.Command{
if len(args) != 1 {
return errors.New("No target provided")
}
m, ch := NewMTR(args[0], TIMEOUT, INTERVAL, HOP_SLEEP)
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 Expand Up @@ -60,7 +62,7 @@ var RootCmd = &cobra.Command{
},
}

func render(m *MTR) {
func render(m *mtr.MTR) {
tm.MoveCursor(1, 1)
m.Render(1)
tm.Flush() // Call it every time at the end of rendering
Expand All @@ -72,6 +74,7 @@ func init() {
RootCmd.Flags().DurationVarP(&INTERVAL, "interval", "i", INTERVAL, "Wait time between icmp packets before sending new one")
RootCmd.Flags().DurationVar(&HOP_SLEEP, "hop-sleep", HOP_SLEEP, "Wait time between pinging next hop")
RootCmd.Flags().IntVar(&MAX_HOPS, "max-hops", MAX_HOPS, "Maximal TTL count")
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")
}
44 changes: 23 additions & 21 deletions hop.go → hop/hop.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package hop

import (
"container/ring"
Expand All @@ -8,21 +8,23 @@ import (
"time"

gm "github.com/buger/goterm"
"github.com/tonobo/mtr/imcp"
)

type HopStatistic struct {
dest *net.IPAddr
timeout time.Duration
pid int
Sent int
TTL int
Target string
Last ICMPReturn
Best ICMPReturn
Worst ICMPReturn
SumElapsed time.Duration
Lost int
Packets *ring.Ring
Dest *net.IPAddr
Timeout time.Duration
PID int
Sent int
TTL int
Target string
Last imcp.ICMPReturn
Best imcp.ICMPReturn
Worst imcp.ICMPReturn
SumElapsed time.Duration
Lost int
Packets *ring.Ring
RingBufferSize int
}

type packet struct {
Expand All @@ -31,7 +33,7 @@ type packet struct {
}

func (s *HopStatistic) Next() {
r, _ := Icmp("0.0.0.0", s.dest, s.TTL, s.pid, s.timeout)
r, _ := imcp.SendIMCP("0.0.0.0", s.Dest, s.TTL, s.PID, s.Timeout)
s.Packets = s.Packets.Prev()
s.Packets.Value = r
if s.Target == "" {
Expand Down Expand Up @@ -68,7 +70,7 @@ func (h *HopStatistic) MarshalJSON() ([]byte, error) {
TTL: h.TTL,
Loss: h.Loss(),
Target: h.Target,
PacketBufferSize: RING_BUFFER_SIZE,
PacketBufferSize: h.RingBufferSize,
Last: h.Last.Elapsed.Seconds() * 1000,
Best: h.Best.Elapsed.Seconds() * 1000,
Worst: h.Worst.Elapsed.Seconds() * 1000,
Expand All @@ -90,15 +92,15 @@ func (h *HopStatistic) Loss() float64 {
}

func (h *HopStatistic) packets() []*packet {
v := make([]*packet, RING_BUFFER_SIZE)
v := make([]*packet, h.RingBufferSize)
i := 0
h.Packets.Do(func(f interface{}) {
if f == nil {
v[i] = nil
i++
return
}
x := f.(ICMPReturn)
x := f.(imcp.ICMPReturn)
if x.Success {
v[i] = &packet{
Success: true,
Expand All @@ -116,12 +118,12 @@ func (h *HopStatistic) packets() []*packet {
}

func (h *HopStatistic) Render() {
packets := make([]byte, RING_BUFFER_SIZE)
i := RING_BUFFER_SIZE - 1
packets := make([]byte, h.RingBufferSize)
i := h.RingBufferSize - 1
h.Packets.Do(func(f interface{}) {
if f == nil {
packets[i] = ' '
} else if !f.(ICMPReturn).Success {
} else if !f.(imcp.ICMPReturn).Success {
packets[i] = '?'
} else {
packets[i] = '.'
Expand All @@ -132,7 +134,7 @@ func (h *HopStatistic) Render() {
if h.Target != "" {
addr = h.Target
}
l := fmt.Sprintf("%d", RING_BUFFER_SIZE)
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,
Expand Down
6 changes: 4 additions & 2 deletions icmp.go → imcp/icmp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package imcp

import (
"net"
Expand All @@ -8,13 +8,15 @@ import (
"golang.org/x/net/ipv4"
)

// ICMPReturn contains the info for a returned IMCP
type ICMPReturn struct {
Success bool
Addr string
Elapsed time.Duration
}

func Icmp(localAddr string, dst net.Addr, ttl, pid int, timeout time.Duration) (hop ICMPReturn, err error) {
// SendIMCP sends a IMCP to a given destination
func SendIMCP(localAddr string, dst net.Addr, ttl, pid int, timeout time.Duration) (hop ICMPReturn, err error) {
hop.Success = false
start := time.Now()
c, err := icmp.ListenPacket("ip4:icmp", localAddr)
Expand Down
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package main

import (
"fmt"

"github.com/tonobo/mtr/cli"
)

func main() {
RootCmd.Execute()
err := cli.RootCmd.Execute()
if err != nil {
fmt.Println(err)
}
}
116 changes: 0 additions & 116 deletions mtr.go

This file was deleted.

Loading