ping

package
v1.99.0-pre Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: BSD-3-Clause Imports: 19 Imported by: 5

Documentation

Overview

Package ping allows sending ICMP echo requests to a host in order to determine network latency.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListenPacketer added in v1.42.0

type ListenPacketer interface {
	ListenPacket(ctx context.Context, typ string, addr string) (net.PacketConn, error)
}

PacketListener defines the interface required to listen to packages on an address.

type Pinger

type Pinger struct {

	// Unprivileged, when set, makes the Pinger use non-privileged
	// datagram-oriented ICMP sockets ("udp4"/"udp6") opened via
	// golang.org/x/net/icmp.ListenPacket instead of raw ICMP sockets
	// ("ip4:icmp"/"ip6:icmp") opened via the configured ListenPacketer.
	//
	// Unprivileged mode is supported on macOS, iOS, and Linux (subject to
	// the /proc/sys/net/ipv4/ping_group_range sysctl). When set, the
	// ListenPacketer passed to New is ignored and the kernel rewrites the
	// outgoing ICMP echo ID to match the socket; replies are matched by
	// sequence number and echo data only.
	//
	// Must be set before the first call to Send.
	Unprivileged bool

	Verbose bool        // verbose logging
	Logf    logger.Logf // optional logging function; if nil, logs to the standard logger
	// contains filtered or unexported fields
}

Pinger represents a set of ICMP echo requests to be sent at a single time.

A new instance should be created for each concurrent set of ping requests; this type should not be reused.

func New

func New(ctx context.Context, logf logger.Logf, lp ListenPacketer) *Pinger

New creates a new Pinger. The Context provided will be used to create network listeners, and to set an absolute deadline (if any) on the net.Conn

func (*Pinger) Close

func (p *Pinger) Close() error

func (*Pinger) Send

func (p *Pinger) Send(ctx context.Context, dest net.Addr, data []byte) (time.Duration, error)

Send sends an ICMP Echo Request packet to the destination, waits for a response, and returns the duration between when the request was sent and when the reply was received.

If provided, "data" is sent with the packet and is compared upon receiving a reply.

Jump to

Keyboard shortcuts

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