-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Advanced ICMP options #240
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ func getICMPSequence() uint16 { | |
|
||
func ProbeICMP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry, logger log.Logger) (success bool) { | ||
var ( | ||
socket *icmp.PacketConn | ||
socket net.PacketConn | ||
requestType icmp.Type | ||
replyType icmp.Type | ||
) | ||
|
@@ -62,23 +62,52 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr | |
if ip.IP.To4() == nil { | ||
requestType = ipv6.ICMPTypeEchoRequest | ||
replyType = ipv6.ICMPTypeEchoReply | ||
|
||
socket, err = icmp.ListenPacket("ip6:ipv6-icmp", "::") | ||
if err != nil { | ||
level.Error(logger).Log("msg", "Error listening to socket", "err", err) | ||
return | ||
} | ||
} else { | ||
requestType = ipv4.ICMPTypeEcho | ||
replyType = ipv4.ICMPTypeEchoReply | ||
socket, err = icmp.ListenPacket("ip4:icmp", "0.0.0.0") | ||
} | ||
|
||
if err != nil { | ||
level.Error(logger).Log("msg", "Error listening to socket", "err", err) | ||
return | ||
if !module.ICMP.DontFragment { | ||
socket, err = icmp.ListenPacket("ip4:icmp", "0.0.0.0") | ||
if err != nil { | ||
level.Error(logger).Log("msg", "Error listening to socket", "err", err) | ||
return | ||
} | ||
} else { | ||
s, err := net.ListenPacket("ip4:icmp", "0.0.0.0") | ||
if err != nil { | ||
level.Error(logger).Log("msg", "Error listening to socket", "err", err) | ||
return | ||
} | ||
|
||
rc, err := ipv4.NewRawConn(s) | ||
if err != nil { | ||
level.Error(logger).Log("msg", "cannot construct raw connection", "err", err) | ||
return | ||
} | ||
socket = &dfConn{c: rc} | ||
} | ||
} | ||
|
||
defer socket.Close() | ||
|
||
var data []byte | ||
if module.ICMP.PayloadSize != 0 { | ||
data = make([]byte, module.ICMP.PayloadSize) | ||
copy(data, "Prometheus Blackbox Exporter") | ||
} else { | ||
data = []byte("Prometheus Blackbox Exporter") | ||
} | ||
|
||
body := &icmp.Echo{ | ||
ID: os.Getpid() & 0xffff, | ||
Seq: int(getICMPSequence()), | ||
Data: []byte("Prometheus Blackbox Exporter"), | ||
Data: data, | ||
} | ||
level.Info(logger).Log("msg", "Creating ICMP packet", "seq", body.Seq, "id", body.ID) | ||
wm := icmp.Message{ | ||
|
@@ -106,7 +135,7 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr | |
return | ||
} | ||
|
||
rb := make([]byte, 1500) | ||
rb := make([]byte, 65536) | ||
if err := socket.SetReadDeadline(deadline); err != nil { | ||
level.Error(logger).Log("msg", "Error setting socket deadline", "err", err) | ||
return | ||
|
@@ -136,3 +165,60 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr | |
} | ||
} | ||
} | ||
|
||
type dfConn struct { | ||
c *ipv4.RawConn | ||
} | ||
|
||
func (c *dfConn) ReadFrom(b []byte) (int, net.Addr, error) { | ||
h, p, _, err := c.c.ReadFrom(b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work on Windows, which is a supported platform. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you propose I move about this? Different files for nix and windows? Or error when reading config? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First see if this can be done with Windows, we should be aiming for feature parity |
||
if err != nil { | ||
return 0, nil, err | ||
} | ||
|
||
copy(b, p) | ||
n := len(b) | ||
if len(p) < len(b) { | ||
n = len(p) | ||
} | ||
return n, &net.IPAddr{IP: h.Src}, nil | ||
} | ||
|
||
func (d *dfConn) WriteTo(b []byte, addr net.Addr) (int, error) { | ||
ipAddr, err := net.ResolveIPAddr(addr.Network(), addr.String()) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
dfHeader := &ipv4.Header{ | ||
Version: ipv4.Version, | ||
Len: ipv4.HeaderLen, | ||
Protocol: 1, | ||
TotalLen: ipv4.HeaderLen + len(b), | ||
Flags: ipv4.DontFragment, | ||
TTL: 64, | ||
Dst: ipAddr.IP, | ||
} | ||
|
||
return len(b), d.c.WriteTo(dfHeader, b, nil) | ||
} | ||
|
||
func (d *dfConn) Close() error { | ||
return d.c.Close() | ||
} | ||
|
||
func (d *dfConn) LocalAddr() net.Addr { | ||
return nil | ||
} | ||
|
||
func (d *dfConn) SetDeadline(t time.Time) error { | ||
return d.c.SetDeadline(t) | ||
} | ||
|
||
func (d *dfConn) SetReadDeadline(t time.Time) error { | ||
return d.c.SetReadDeadline(t) | ||
} | ||
|
||
func (d *dfConn) SetWriteDeadline(t time.Time) error { | ||
return d.c.SetWriteDeadline(t) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick, but it might make sense to include the exporter version in here.