go_mtr

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

Go Mtr

Go implementation of mtr. mtr combines the functionality of the 'traceroute' and 'ping' programs in a single network diagnostic tool.

As mtr starts, it investigates the network connection between the host mtr runs on and a user-specified destination host. After it determines the address of each network hop between the machines, it sends a sequence of ICMP ECHO requests to each one to determine the quality of the link to each machine. As it does this, it prints running statistics about each machine.

Getting started

  • command use case
cd ./cmd
go run root.go --help
  • code use case
package main

import (
	"fmt"
	"github.com/wisdomatom/go-mtr"
	"time"
)

func main() {
	tracer, err := go_mtr.NewTrace(go_mtr.Config{
		ICMP:        true,
		UDP:         false,
		MaxUnReply:  8,
		NextHopWait: time.Millisecond * 200,
	})
	if err != nil {
		panic(err)
	}
	t, err := go_mtr.GetTrace(&go_mtr.Trace{
		SrcAddr: go_mtr.GetOutbondIP(),
		DstAddr: "8.8.8.8",
		SrcPort: 65533,
		DstPort: 65535,
		MaxTTL:  30,
		Retry:   2,
	})
	if err != nil {
		panic(err)
	}
	res := tracer.BatchTrace([]go_mtr.Trace{*t}, 1)
	for _, r := range res {
		fmt.Println(r.Marshal())
		fmt.Println(r.MarshalAggregate())
	}
}

Documentation

Index

Constants

View Source
const (
	ICMPEcho        = "ICMPEcho"
	ICMPTimeExceed  = "ICMPTimeExceed"
	ICMPUnreachable = "ICMPUnreachable"
)

Variables

This section is empty.

Functions

func Error

func Error(errCh chan error, err error)

func GetOutbondIP

func GetOutbondIP() string

GetOutbondIP return default local ip which used to route packages outbond

func IsIpv4

func IsIpv4(ip string) bool

Types

type Config

type Config struct {
	ICMP            bool
	TCP             bool
	UDP             bool
	MaxUnReply      int
	NextHopWait     time.Duration
	RcvGoroutineNum int
	ErrCh           chan error
	BatchSize       int
}

type ConstructPacket

type ConstructPacket struct {
	Trace
	TTL     uint8
	Id      uint16
	Seq     uint16
	SrcPort uint16
	DstPort uint16
}

type Constructor

type Constructor interface {
	Packet(req ConstructPacket) ([]byte, error)
}

type DeConstructor

type DeConstructor interface {
	DeConstruct(pkg []byte) (*ICMPRcv, error)
}

type Detector

type Detector interface {
	Probe(req SendProbe) error
	SteamProbe(reqCh chan *SendProbe)
	Close()
}

type ICMPRcv

type ICMPRcv struct {
	RcvType   string
	RcvAt     time.Time
	Src       string
	Dst       string
	TTLSrc    string
	Id        uint16
	TTL       uint8
	Seq       uint16
	Proto     uint8
	SrcPort   uint16
	DstPort   uint16
	Reachable bool
}

type Receiver

type Receiver interface {
	Receive() (chan *ICMPRcv, error)
	Close()
}

type SendProbe

type SendProbe struct {
	Trace
	WriteTimeout time.Duration
	Msg          []byte
}

type Trace

type Trace struct {
	IsIpv4      bool          `json:"-"`
	SrcAddr     string        `json:"src_addr"`
	DstAddr     string        `json:"dst_addr"`
	SrcSockAddr unix.Sockaddr `json:"-"`
	DstSockAddr unix.Sockaddr `json:"-"`
	SrcPort     uint16        `json:"src_port"`
	DstPort     uint16        `json:"dst_port"`
	MaxTTL      uint8         `json:"max_ttl"`
	Retry       int           `json:"retry"`
}

func GetTrace

func GetTrace(t *Trace) (*Trace, error)

type TraceDebugInfo

type TraceDebugInfo struct {
	PacketSend uint32 `json:"packet_send"`
	PacketRcv  uint32 `json:"packet_rcv"`
}

type TraceRes

type TraceRes struct {
	SrcTTL     string        `json:"src_ttl"`
	Latency    time.Duration `json:"latency"`
	TTL        uint8         `json:"ttl"`
	Reached    bool          `json:"reached"`
	PacketLoss float32       `json:"packet_loss"`
}

type TraceResult

type TraceResult struct {
	Trace
	Id         uint16     `json:"id"`
	Key        string     `json:"key"`
	StartAt    time.Time  `json:"start_at"`
	Done       bool       `json:"done"`
	AvgPktLoss float32    `json:"avg_pkt_loss"`
	Res        []TraceRes `json:"res"`
	// contains filtered or unexported fields
}

func (TraceResult) Aggregate

func (t TraceResult) Aggregate() TraceResult

func (TraceResult) JsonString added in v0.0.2

func (t TraceResult) JsonString() string

func (TraceResult) JsonStringAggregate added in v0.0.2

func (t TraceResult) JsonStringAggregate() string

func (TraceResult) Marshal

func (t TraceResult) Marshal() string

func (TraceResult) MarshalAggregate

func (t TraceResult) MarshalAggregate() string

type Tracer

type Tracer interface {
	BatchTrace(batch []Trace, startTTL uint8) ([]*TraceResult, error)
	DebugInfo() TraceDebugInfo
}

func NewTrace

func NewTrace(conf Config) (Tracer, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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