icmp

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2018 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IPing

type IPing struct {
	// contains filtered or unexported fields
}

IPing provide ability to send and receive ICMP packets

func NewPing

func NewPing(stopChan chan bool) *IPing

NewPing new a ping

func (*IPing) Ping

func (p *IPing) Ping(ipAddr *net.IPAddr, timeout time.Duration) (latency time.Duration, err error)

Ping ipAddr with timeout

func (*IPing) Start

func (p *IPing) Start() (err error)

Start listen

func (*IPing) Trace

func (p *IPing) Trace(ipAddr *net.IPAddr, ttl int, timeout time.Duration) (time.Duration, net.Addr, error)

Trace ipAddr with timeout

Example
stop := make(chan bool, 2)
ping := NewPing(stop)
if err := ping.Start(); err != nil {
	log.Fatalf("start ping error, %s", err)
}

addr, _ := net.ResolveIPAddr("ip", "example.com")
ttl := 1
for {
	if latency, from, err := ping.Trace(addr, ttl, 2*time.Second); err != nil {
		if _, ok := err.(*errors.ErrTTLExceed); !ok {
			log.Println("timeout")
			break
		}
		log.Printf("%d %+v, %+v", ttl, from, latency)
		ttl++
	} else {
		log.Printf("%d %+v, %+v", ttl, from, latency)
		break
	}
}
Output:

Jump to

Keyboard shortcuts

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