Categorygithub.com/MR5356/go-ping
modulepackage
0.0.0-20240628070247-a45544c66640
Repository: https://github.com/mr5356/go-ping.git
Documentation: pkg.go.dev

# README

pro-bing

PkgGoDev Circle CI

A simple but powerful ICMP echo (ping) library for Go, inspired by go-ping & go-fastping.

Here is a very simple example that sends and receives three packets:

import (
    ping "github.com/MR5356/go-ping"
)

pinger, err := ping.NewPinger("www.google.com")
if err != nil {
	panic(err)
}
pinger.Count = 3
err = pinger.Run() // Blocks until finished.
if err != nil {
	panic(err)
}
stats := pinger.Statistics() // get send/receive/duplicate/rtt stats

Here is an example that emulates the traditional UNIX ping command:

import (
    ping "github.com/MR5356/go-ping"
)

pinger, err := ping.NewPinger("www.google.com")
if err != nil {
	panic(err)
}

// Listen for Ctrl-C.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
	for _ = range c {
		pinger.Stop()
	}
}()

pinger.OnRecv = func(pkt *probing.Packet) {
	fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n",
		pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)
}

pinger.OnDuplicateRecv = func(pkt *probing.Packet) {
	fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)\n",
		pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.TTL)
}

pinger.OnFinish = func(stats *probing.Statistics) {
	fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr)
	fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n",
		stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss)
	fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
		stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)
}

fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr())
err = pinger.Run()
if err != nil {
	panic(err)
}

It sends ICMP Echo Request packet(s) and waits for an Echo Reply in response. If it receives a response, it calls the OnRecv callback unless a packet with that sequence number has already been received, in which case it calls the OnDuplicateRecv callback. When it's finished, it calls the OnFinish callback.

For a full ping example, see cmd/ping/ping.go.

Installation

go get -u github.com/prometheus-community/pro-bing

To install the native Go ping executable:

go get -u github.com/mr5356/go-ping/...
$GOPATH/bin/ping

Supported Operating Systems

Linux

This library attempts to send an "unprivileged" ping via UDP. On Linux, this must be enabled with the following sysctl command:

sudo sysctl -w net.ipv4.ping_group_range="0 2147483647"

If you do not wish to do this, you can call pinger.SetPrivileged(true) in your code and then use setcap on your binary to allow it to bind to raw sockets (or just run it as root):

setcap cap_net_raw=+ep /path/to/your/compiled/binary

See this blog and the Go x/net/icmp package for more details.

This library supports setting the SO_MARK socket option which is equivalent to the -m mark flag in standard ping binaries on linux. Setting this option requires the CAP_NET_ADMIN capability (via setcap or elevated privileges). You can set a mark (ex: 100) with pinger.SetMark(100) in your code.

Setting the "Don't Fragment" bit is supported under Linux which is equivalent to ping -Mdo. You can enable this with pinger.SetDoNotFragment(true).

Windows

You must use pinger.SetPrivileged(true), otherwise you will receive the following error:

socket: The requested protocol has not been configured into the system, or no implementation for it exists.

Despite the method name, this should work without the need to elevate privileges and has been tested on Windows 10. Please note that accessing packet TTL values is not supported due to limitations in the Go x/net/ipv4 and x/net/ipv6 packages.

Plan 9 from Bell Labs

There is no support for Plan 9. This is because the entire x/net/ipv4 and x/net/ipv6 packages are not implemented by the Go programming language.

HTTP

This library also provides support for HTTP probing. Here is a trivial example:

import (
    ping "github.com/MR5356/go-ping"
)

httpCaller := ping.NewHttpCaller("https://www.google.com",
    probing.WithHTTPCallerCallFrequency(time.Second),
    probing.WithHTTPCallerOnResp(func(suite *probing.TraceSuite, info *probing.HTTPCallInfo) {
        fmt.Printf("got resp, status code: %d, latency: %s\n",
            info.StatusCode,
            suite.GetGeneralEnd().Sub(suite.GetGeneralStart()),
        )
    }),
)

// Listen for Ctrl-C.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
    <-c
    httpCaller.Stop()
}()
httpCaller.Run()

Library provides a rich list of options available for a probing. You can check the full list of available options in a generated doc.

Callbacks

HTTPCaller uses net/http/httptrace pkg to provide an API to track specific request event, e.g. tls handshake start. It is highly recommended to check the httptrace library doc to understand the purpose of provided callbacks. Nevertheless, httptrace callbacks are concurrent-unsafe, our implementation provides a concurrent-safe API. In addition to that, each callback contains a TraceSuite object which provides an Extra field which you can use to propagate your data across them and a number of timer fields, which are set prior to the execution of a corresponding callback.

Target RPS & performance

Library provides two options, allowing to manipulate your call load: callFrequency & maxConcurrentCalls. In case you set callFrequency to a value X, but it can't be achieved during the execution - you will need to try increasing a number of maxConcurrentCalls. Moreover, your callbacks might directly influence an execution performance.

For a full documentation, please refer to the generated doc.

Maintainers and Getting Help:

This repo was originally in the personal account of sparrc, but is now maintained by the Prometheus Community.

Contributing

Refer to CONTRIBUTING.md

# Packages

No description provided by the author

# Functions

New returns a new Pinger struct pointer.
NewHttpCaller returns a new HTTPCaller.
NewPinger returns a new Pinger and resolves the address.
WithHTTPCallerBody is a functional parameter for a HTTPCaller which specifies a body that should be set in request.
WithHTTPCallerCallFrequency is a functional parameter for a HTTPCaller which specifies a call frequency.
WithHTTPCallerClient is a functional parameter for a HTTPCaller which specifies a http.Client.
WithHTTPCallerHeaders is a functional parameter for a HTTPCaller which specifies headers that should be set in request.
WithHTTPCallerHost is a functional parameter for a HTTPCaller which allowed to override a host header.
WithHTTPCallerIsValidResponse is a functional parameter for a HTTPCaller which specifies a function that will be used to assess whether a response is valid.
WithHTTPCallerLogger is a functional parameter for a HTTPCaller which specifies a logger.
WithHTTPCallerMaxConcurrentCalls is a functional parameter for a HTTPCaller which specifies a number of maximum concurrent calls.
WithHTTPCallerMethod is a functional parameter for a HTTPCaller which specifies a method that should be set in request.
WithHTTPCallerOnConnDone is a functional parameter for a HTTPCaller which specifies a callback that will be called when connection establishment finished.
WithHTTPCallerOnConnStart is a functional parameter for a HTTPCaller which specifies a callback that will be called when connection establishment started.
WithHTTPCallerOnDNSDone is a functional parameter for a HTTPCaller which specifies a callback that will be called when dns resolving ended.
WithHTTPCallerOnDNSStart is a functional parameter for a HTTPCaller which specifies a callback that will be called when dns resolving starts.
WithHTTPCallerOnFirstByteReceived is a functional parameter for a HTTPCaller which specifies a callback that will be called when first response byte has been received.
WithHTTPCallerOnReq is a functional parameter for a HTTPCaller which specifies a callback that will be called before the start of the http call execution.
WithHTTPCallerOnResp is a functional parameter for a HTTPCaller which specifies a callback that will be called when response is received.
WithHTTPCallerOnTLSDone is a functional parameter for a HTTPCaller which specifies a callback that will be called when tls handshake ended.
WithHTTPCallerOnTLSStart is a functional parameter for a HTTPCaller which specifies a callback that will be called when tls handshake started.
WithHTTPCallerOnWroteRequest is a functional parameter for a HTTPCaller which specifies a callback that will be called when request has been written.
WithHTTPCallerTimeout is a functional parameter for a HTTPCaller which specifies request timeout.

# Variables

No description provided by the author
No description provided by the author

# Structs

HTTPCaller represents a prober performing http calls and collecting relevant statistics.
HTTPCallInfo represents a data set which passed as a function argument to an onResp callback.
No description provided by the author
Packet represents a received and processed ICMP echo packet.
Pinger represents a packet sender/receiver.
Statistics represent the stats of a currently running or finished pinger operation.
No description provided by the author
TraceSuite is a struct that is passed to each callback.

# Interfaces

No description provided by the author

# Type aliases

HTTPCallerOption represents a function type for a functional parameter passed to a NewHttpCaller constructor.