summaryrefslogtreecommitdiff
path: root/internal/failover/ping.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/failover/ping.go')
-rw-r--r--internal/failover/ping.go32
1 files changed, 26 insertions, 6 deletions
diff --git a/internal/failover/ping.go b/internal/failover/ping.go
index 59dbcc9..b37685c 100644
--- a/internal/failover/ping.go
+++ b/internal/failover/ping.go
@@ -8,16 +8,22 @@ import (
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
+ "golang.org/x/net/ipv6"
)
-// mtuOverhead defines the total MTU overhead for an ICMP ECHO message: 20 bytes IP header + 8 bytes ICMP header
-var mtuOverhead = 28
+var (
+ // mtuV4Overhead defines the total MTU overhead for an ICMP ECHO message: 20 bytes IP header + 8 bytes ICMP header
+ mtuV4Overhead = 28
+ // mtuV6Overhead defines the total MTU v6 overhead for an ICMP ECHO message: 40 bytes IP header + 8 bytes ICMP header
+ mtuV6Overhead = 48
+)
// Pinger sends pings
type Pinger struct {
listener net.PacketConn
buffer []byte
gateway net.Addr
+ v4 bool
}
// Read reads from the ping listener with deadline `deadline`
@@ -33,12 +39,18 @@ func (p Pinger) Read(deadline time.Time) error {
if err != nil {
return err
}
- got, err := icmp.ParseMessage(ipv4.ICMPTypeEchoReply.Protocol(), r[:n])
+ var t icmp.Type
+ if p.v4 {
+ t = ipv4.ICMPTypeEchoReply
+ } else {
+ t = ipv6.ICMPTypeEchoReply
+ }
+ got, err := icmp.ParseMessage(t.Protocol(), r[:n])
if err != nil {
return err
}
switch got.Type {
- case ipv4.ICMPTypeEchoReply:
+ case t:
return nil
default:
return fmt.Errorf("not a ping echo reply, got: %+v", got)
@@ -49,10 +61,18 @@ func (p Pinger) Read(deadline time.Time) error {
func (p Pinger) Send(seq int) error {
errorMessage := fmt.Sprintf("failed sending ping, seq %d", seq)
// Make a new ICMP message
+ var t icmp.Type
+ if p.v4 {
+ t = ipv4.ICMPTypeEcho
+ } else {
+ t = ipv6.ICMPTypeEchoRequest
+ }
m := icmp.Message{
- Type: ipv4.ICMPTypeEcho, Code: 0,
+ Type: t,
+ Code: 0,
Body: &icmp.Echo{
- ID: os.Getpid() & 0xffff, Seq: seq,
+ ID: os.Getpid() & 0xffff,
+ Seq: seq,
Data: p.buffer,
},
}