Skip to content

Commit

Permalink
Add support for OnSendError and OnRecvError handlers (#44)
Browse files Browse the repository at this point in the history
* Add support for OnSendError and OnRecvError handlers

Signed-off-by: Jeremiah Millay <jmillay@fastly.com>

* do not use handler := pattern for callbacks

Signed-off-by: Jeremiah Millay <jmillay@fastly.com>

---------

Signed-off-by: Jeremiah Millay <jmillay@fastly.com>
  • Loading branch information
floatingstatic authored Jun 8, 2023
1 parent 0ce0e4e commit 23b417c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ type Pinger struct {
// OnDuplicateRecv is called when a packet is received that has already been received.
OnDuplicateRecv func(*Packet)

// OnSendError is called when an error occurs while Pinger attempts to send a packet
OnSendError func(*Packet, error)

// OnRecvError is called when an error occurs while Pinger attempts to receive a packet
OnRecvError func(error)

// Size of packet being sent
Size int

Expand Down Expand Up @@ -645,6 +651,9 @@ func (p *Pinger) recvICMP(
var err error
n, ttl, _, err = conn.ReadFrom(bytes)
if err != nil {
if p.OnRecvError != nil {
p.OnRecvError(err)
}
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
// Read timeout
Expand Down Expand Up @@ -792,6 +801,16 @@ func (p *Pinger) sendICMP(conn packetConn) error {

for {
if _, err := conn.WriteTo(msgBytes, dst); err != nil {
if p.OnSendError != nil {
outPkt := &Packet{
Nbytes: len(msgBytes),
IPAddr: p.ipaddr,
Addr: p.addr,
Seq: p.sequence,
ID: p.id,
}
p.OnSendError(outPkt, err)
}
if neterr, ok := err.(*net.OpError); ok {
if neterr.Err == syscall.ENOBUFS {
continue
Expand Down

0 comments on commit 23b417c

Please sign in to comment.