ping

package
v0.0.0-...-d9fbfd9 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2018 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Overview

Package ping provides an api for efficiently pinging lots of hosts using ICMP echos.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoIDs = socket.ErrNoIDs

ErrNoIDs is returned when there are no icmp ids left to use

Either you are trying to ping the same host with more than 2^16 connections or you are on windows and are running more than 2^16 connections total

View Source
var ErrNotRunning = conn.ErrNotRunning

ErrNotRunning is returned if a ping is set to a closed connection.

View Source
var ErrTimedOut = socket.ErrTimedOut

ErrTimedOut is returned when a ping times out

Functions

func HostFlood

func HostFlood(ctx context.Context, host string, reResolveEvery int, handler HandleFunc, count int, timeout time.Duration) error

HostFlood performs HostFlood using the default socket.

func HostInterval

func HostInterval(ctx context.Context, host string, reResolveEvery int, handler HandleFunc, count int, interval, timeout time.Duration) error

HostInterval performs HostInterval using the default socket.

func IPFlood

func IPFlood(ctx context.Context, dst *net.IPAddr, handler HandleFunc, count int, timeout time.Duration) error

IPFlood performs IPFlood using the default socket.

func IPInterval

func IPInterval(ctx context.Context, dst *net.IPAddr, handler HandleFunc, count int, interval, timeout time.Duration) error

IPInterval performs IPInterval using the default socket

func SetWorkers

func SetWorkers(n int)

SetWorkers sets the workers on the default socket

Types

type HandleFunc

type HandleFunc func(*Ping, error)

HandleFunc is a function to handle responses and errors

type HostConn

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

HostConn is an ICMP connection based on hostname

Pings run from a HostConn can be configured to periodically re-resolve

func NewHostConn

func NewHostConn(host string, reResolveEvery int, handle HandleFunc, timeout time.Duration) *HostConn

NewHostConn returns a new HostConn

func (*HostConn) Close

func (h *HostConn) Close() error

Close closes the host connection. Further attempts to send pings via this connection will panic.

func (*HostConn) Drain

func (h *HostConn) Drain()

Drain will block until all pending pings have been handled, either by reply or timeout

func (*HostConn) SendPing

func (h *HostConn) SendPing()

SendPing sends a ping

type IPConn

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

IPConn holds a connection to a destination ip address

func NewIPConn

func NewIPConn(dst *net.IPAddr, handle HandleFunc, timeout time.Duration) (*IPConn, error)

NewIPConn creates a new connection

func (*IPConn) Close

func (c *IPConn) Close() error

Close closes an IPConn. SendPing after Close will panic.

func (*IPConn) Drain

func (c *IPConn) Drain()

Drain blocks until all outstanding pings have been handled.

func (*IPConn) ID

func (c *IPConn) ID() int

ID returns the ICMP ID associated with this connection

func (*IPConn) SendPing

func (c *IPConn) SendPing()

SendPing sends a ping.

Errors sending will be sent to the handler.

type Ping

type Ping struct {
	// Host is the hostname that was pinged
	Host string
	// Src is the source IP. This is probably 0.0.0.0 for sent packets, but a
	// specific IP on the sending host for recieved packets
	Src *net.IPAddr
	// Dst is the destination IP.
	// This will be nil for recieved packets on windows. The reason is that
	// the recieve function does not provide the source address
	// on windows ICMP messages are mathed only by the 16 bit ICMP id.
	Dst *net.IPAddr
	// ID is the ICMP ID
	ID int
	// Seq is the ICMP Sequence
	Seq int
	// Count is the count of this ICMP
	Count int
	// Sent is the time the echo was sent
	Sent time.Time
	// Recieved is the time the echo was recieved.
	Recieved time.Time
	// TimeOut is timeout duration
	TimeOut time.Duration
	// TTL is the ttl on the recieved packet.
	// This is not supported on windows and will always be zero
	TTL int
	// Len is the length of the recieved packet
	Len int
}

Ping is an ICMP packet that has been received

func HostOnce

func HostOnce(host string, timeout time.Duration) (*Ping, error)

HostOnce performs HostOnce on the default socket.

func IPOnce

func IPOnce(dst *net.IPAddr, timeout time.Duration) (*Ping, error)

IPOnce performs IPOnce on the default socket.

func (*Ping) RTT

func (p *Ping) RTT() time.Duration

RTT returns the RTT of the ping

func (*Ping) TimeOutTime

func (p *Ping) TimeOutTime() time.Time

TimeOutTime returns the time this ping times out

type Socket

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

Socket is a raw socket connection (one for ipv4 and one for ipv6).

Sockets are only actively listening when there are one or more open connections

Sockets must be created via NewSocket

In most cases using DefaultSocket() is appropriate

func DefaultSocket

func DefaultSocket() *Socket

DefaultSocket is the default global socket

func NewSocket

func NewSocket() *Socket

NewSocket returns a new Socket

func (*Socket) HostFlood

func (s *Socket) HostFlood(ctx context.Context, host string, reResolveEvery int, handler HandleFunc, count int, timeout time.Duration) error

HostFlood works like HostInterval, but instead of sending on an interval, the next ping is sent as soon as the previous ping is handled.

func (*Socket) HostInterval

func (s *Socket) HostInterval(ctx context.Context, host string, reResolveEvery int, handler HandleFunc, count int, interval, timeout time.Duration) error

HostInterval sends a ping each interval up to count pings or until ctx is canceled.

If an interval of zero is specified, it will send pings as fast as possible. When there are 2^16 pending pings which have not received a reply, or timed out sending will block. This may be a limiting factor in how quickly pings can be sent.

If a timeout of zero is specifed, pings will never time out.

If a count of zero is specified, interval will continue to send pings until ctx is canceled.

func (*Socket) HostOnce

func (s *Socket) HostOnce(host string, timeout time.Duration) (*Ping, error)

HostOnce sends a single echo request and returns, it blocks until a reply is recieved or the ping times out

Zero is no timeout and IPOnce will block forever if a reply is never recieved

It is not recommended to use IPOnce in a loop, use Interval, or create a Conn and call SendPing() in a loop

func (*Socket) IPFlood

func (s *Socket) IPFlood(ctx context.Context, dst *net.IPAddr, handler HandleFunc, count int, timeout time.Duration) error

IPFlood continuously sends pings, sending the next ping as soon as the previous one is replied or times out.

func (*Socket) IPInterval

func (s *Socket) IPInterval(ctx context.Context, dst *net.IPAddr, handler HandleFunc, count int, interval, timeout time.Duration) error

IPInterval sends a ping each interval up to count pings or until ctx is canceled.

If an interval of zero is specified, it will send pings as fast as possible. When there are 2^16 pending pings which have not received a reply, or timed out sending will block. This may be a limiting factor in how quickly pings can be sent.

If a timeout of zero is specifed, pings will never time out.

If a count of zero is specified, interval will continue to send pings until ctx is canceled.

func (*Socket) IPOnce

func (s *Socket) IPOnce(dst *net.IPAddr, timeout time.Duration) (*Ping, error)

IPOnce sends a single echo request and returns, it blocks until a reply is recieved or the ping times out

Zero is no timeout and IPOnce will block forever if a reply is never recieved

It is not recommended to use IPOnce in a loop, use Interval, or create a Conn and call SendPing() in a loop

func (*Socket) NewHostConn

func (s *Socket) NewHostConn(host string, reResolveEvery int, handle HandleFunc, timeout time.Duration) *HostConn

NewHostConn returns a new HostConn

func (*Socket) NewIPConn

func (s *Socket) NewIPConn(dst *net.IPAddr, handle HandleFunc, timeout time.Duration) (*IPConn, error)

NewIPConn creates a new connection

func (*Socket) SetWorkers

func (s *Socket) SetWorkers(n int)

SetWorkers sets a number of workers to process incoming packets and run handlers

Consider increasing this if your handlers take a long time to return.

A higher number of workers will prevent dropping replies, at the cost of more memory and cpu usage.

This change will only take effect once all open connections are closed

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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