ping

package module
v0.0.0-...-95db3ca Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

README

go-jing

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Fatalf(format string, v ...interface{})
	Errorf(format string, v ...interface{})
	Warnf(format string, v ...interface{})
	Infof(format string, v ...interface{})
	Debugf(format string, v ...interface{})
}

type NoopLogger

type NoopLogger struct {
}

func (NoopLogger) Debugf

func (l NoopLogger) Debugf(format string, v ...interface{})

func (NoopLogger) Errorf

func (l NoopLogger) Errorf(format string, v ...interface{})

func (NoopLogger) Fatalf

func (l NoopLogger) Fatalf(format string, v ...interface{})

func (NoopLogger) Infof

func (l NoopLogger) Infof(format string, v ...interface{})

func (NoopLogger) Warnf

func (l NoopLogger) Warnf(format string, v ...interface{})

type Packet

type Packet struct {
	// Rtt is the round-trip time it took to ping.
	Rtt time.Duration

	// IPAddr is the address of the host being pinged.
	IPAddr *net.IPAddr

	// Addr is the string address of the host being pinged.
	Addr string

	// Nbytes is the number of bytes in the message.
	Nbytes int

	// Seq is the ICMP sequence number
	Seq int

	// TTL is the TTL on the packet
	Ttl int
}

Packet represents a received and processed ICMP echo packet.

type Pinger

type Pinger struct {
	// Interval is the wait time between each packet send. Default is 1s.
	Interval time.Duration

	// Timeout specifies a timeout before ping exits, regardless of how many
	// packets have been received.
	Timeout time.Duration

	// Count tells pinger to stop after sending (and receiving) Count echo
	// packets. If this option is not specified, pinger will operate until
	// interrupted.
	Count int

	// Debug runs in debug mode
	Debug bool

	// Number of packets sent
	PacketsSent int

	// Number of packets received
	PacketsRecv int

	// Number of duplicate packets received
	PacketsRecvDuplicates int

	// If true, keep a record of rtts of all received packets.
	// Set to false to avoid memory bloat for long running pings.
	RecordRtts bool

	// OnSetup is called when Pinger has finished setting up the listening socket.
	OnSetup func()

	// OnSend is called when Pinger sends a packet
	OnSend func(*Packet)

	// OnRecv is called when Pinger receives and processes a packet
	OnRecv func(*Packet)

	// OnFinish is called when Pinger exits
	OnFinish func(*Statistics)

	// OnDuplicateRecv is called when a packet is received that has already been received
	OnDuplicateRecv func(*Packet)

	// Tracker: Used to uniquely identify packets
	Tracker uint64

	// Source is the source IP address
	Source string

	Size int
	// contains filtered or unexported fields
}

Pinger represents a packet sender/receiver.

func New

func New(addr string) *Pinger

New returns a new Pinger struct pointer

func NewPinger

func NewPinger(addr string) (*Pinger, error)

NewPinger returns a new Pinger and resolves the address.

func (*Pinger) Addr

func (p *Pinger) Addr() string

Addr returns the string ip address of the target host.

func (*Pinger) IPAddr

func (p *Pinger) IPAddr() *net.IPAddr

IPAddr returns the IP address of the target host.

func (*Pinger) Privileged

func (p *Pinger) Privileged() bool

Privileged returns whether pinger is running in privileged mode.

func (*Pinger) Resolve

func (p *Pinger) Resolve() error

Resolve does the DNS lookup for the Pinger address and sets IP protocol.

func (*Pinger) Run

func (p *Pinger) Run() error

Run runs the pinger. This is a blocking function that will exit when it's done. If Count or Interval are not specified, it will continuously until it is interrupted.

func (*Pinger) SetAddr

func (p *Pinger) SetAddr(addr string) error

SetAddr resolves and sets the ip address of the target host, addr can be a DNS name like "www.google.com" or IP like "127.0.0.1".

func (*Pinger) SetIPAddr

func (p *Pinger) SetIPAddr(ipaddr *net.IPAddr)

SetIPAddr sets the ip address of the target host.

func (*Pinger) SetLogger

func (p *Pinger) SetLogger(logger Logger)

SetLogger sets the logger to be used to log events from the pinger.

func (*Pinger) SetNetwork

func (p *Pinger) SetNetwork(n string)

SetNetwork allows configuration of DNS resolution. * "ip" will automatically select IPv4 or IPv6. * "ip4" will select IPv4. * "ip6" will select IPv6.

func (*Pinger) SetPrivileged

func (p *Pinger) SetPrivileged(privileged bool)

SetPrivileged sets the type of ping pinger will send. false means pinger will send an "unprivileged" UDP ping. true means pinger will send a "privileged" raw ICMP ping. NOTE: setting to true requires that it be run with super-user privileges.

func (*Pinger) Statistics

func (p *Pinger) Statistics() *Statistics

Statistics returns the statistics of the pinger. This can be run while the pinger is running or after it is finished. OnFinish calls this function to get its finished statistics.

func (*Pinger) Stop

func (p *Pinger) Stop()

type Statistics

type Statistics struct {
	// PacketsRecv is the number of packets received.
	PacketsRecv int

	// PacketsSent is the number of packets sent.
	PacketsSent int

	// PacketsLoss is the number of packets lost.
	PacketLoss float64

	// IPAddr is the address of the host being pinged.
	IPAddr *net.IPAddr

	// Addr is the string address of the host being pinged.
	Addr string

	// Rtts is all of the round-trip times sent via this pinger.
	Rtts []time.Duration

	// MinRtt is minimum round-trip time sent via this pinger.
	MinRtt time.Duration

	// MaxRtt is maximum round-trip time sent via this pinger.
	MaxRtt time.Duration

	// AvgRtt is average round-trip time sent via this pinger.
	AvgRtt time.Duration

	// StdDevRtt is standard deviation round-trip times sent via this pinger.
	StdDevRtt time.Duration

	// PacketsRecvDuplicates is the number of duplicate responses there were to a sent packet.
	PacketsRecvDuplicates int
}

Statistics represent the stats of a currently running or finished pinger operation.

type StdLogger

type StdLogger struct {
	Logger *log.Logger
}

func (StdLogger) Debugf

func (l StdLogger) Debugf(format string, v ...interface{})

func (StdLogger) Errorf

func (l StdLogger) Errorf(format string, v ...interface{})

func (StdLogger) Fatalf

func (l StdLogger) Fatalf(format string, v ...interface{})

func (StdLogger) Infof

func (l StdLogger) Infof(format string, v ...interface{})

func (StdLogger) Warnf

func (l StdLogger) Warnf(format string, v ...interface{})

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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