Skip to content

Check ping windows iphlpapi - #498

Merged
sni merged 4 commits into
mainfrom
check-ping-windows-iphlpapi
Nov 19, 2026
Merged

sni merged 4 commits into
mainfrom
check-ping-windows-iphlpapi

Conversation

@inqrphl

@inqrphl inqrphl commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

AI GENERATED

===============================

Replace the ping.exe call and localized output parser on Windows with the
unprivileged ICMP echo API from iphlpapi.dll:

  • IPv4 uses IcmpCreateFile/IcmpSendEcho, IPv6 uses
    Icmp6CreateFile/Icmp6SendEcho2.
  • No child process and no locale-dependent output parsing.
  • Send one echo per second, using the per-packet timeout from -t or the
    4 second default. The overall runtime is capped by the check deadline.
  • Keep packet results indexed so timeouts and out-of-order completions
    retain their packet position.
  • Write rta and pl with %.3f. IPv4 replies provide ttl; the IPv6 reply
    structure has no ttl field.
  • Reject -4 together with -6 and non-positive packet counts.

The ping.exe parser is unchanged for Linux, macOS and FreeBSD. Add a
build-tagged Windows implementation and a non-Windows stub.

Add tests for the ICMP reply layouts, IP status mapping, result
aggregation, indexed reply collection and IcmpCreateFile availability.
The live check_ping test now accepts the %.3f packet loss output.

================================

check_ping: clamp packet count to maximum of 1000

this is to prevent big slice allocation if a user wants absurdly high packet number.

sending 1000 packets should take around 16 minutes already.

inqrphl and others added 3 commits November 11, 2026 15:40
Replace the ping.exe call and localized output parser on Windows with the
unprivileged ICMP echo API from iphlpapi.dll:

- IPv4 uses IcmpCreateFile/IcmpSendEcho, IPv6 uses
  Icmp6CreateFile/Icmp6SendEcho2.
- No child process and no locale-dependent output parsing.
- Send one echo per second, using the per-packet timeout from -t or the
  4 second default. The overall runtime is capped by the check deadline.
- Keep packet results indexed so timeouts and out-of-order completions
  retain their packet position.
- Write rta and pl with %.3f. IPv4 replies provide ttl; the IPv6 reply
  structure has no ttl field.
- Reject -4 together with -6 and non-positive packet counts.

The ping.exe parser is unchanged for Linux, macOS and FreeBSD. Add a
build-tagged Windows implementation and a non-Windows stub.

Add tests for the ICMP reply layouts, IP status mapping, result
aggregation, indexed reply collection and IcmpCreateFile availability.
The live check_ping test now accepts the %.3f packet loss output.
this is to prevent big slice allocation if a user wants absurdly high packet number.

sending 1000 packets should take around 16 minutes already.
@inqrphl

inqrphl commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

There is a catch when testing this. If you are building on windows, and the folder that you are building it from has a low mandatory level, likely from some kind of harness sandboxing on your workspace (deepseek harness had it)

icacls repositories\snclient:
  Mandatory Label\Low Mandatory Level:(OI)(CI)(NW)

It infect new files you create in the directory with that mark, including snclient executable you built inside. They inherit the "Low mandatory level".

Windows then does not allow usage of iphlpapi calls of that executable. Moving the file does not remove the mark. You have to either copy it elsewhere, or remove the access right mark from it.

icacls .\snclient.windows.amd64.exe
.\snclient.windows.amd64.exe 
                             Mandatory Label\Low Mandatory Level:(I)(NW)
Successfully processed 1 files; Failed processing 0 files

This probably wont happen in the CI build, but still is something to be careful about. If it is built normally, it gets "Medium Mandatory Label" and ping works.

res = snc.RunCheck("check_ping", []string{"host=10.99.99.99", "packets=2", "-t", "1"})
assert.Equalf(t, CheckExitCritical, res.State, "state critical")
assert.Regexpf(t, `^CRITICAL - Packet loss = 100(|\.0)% \|'rta'=U 'pl'=100%;30;80;0`, string(res.BuildPluginOutput()), "output matches")
assert.Regexpf(t, `^CRITICAL - Packet loss = 100(\.0+)?% \|'rta'=U 'pl'=100%;30;80;0`, string(res.BuildPluginOutput()), "output matches")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packet loss is now printed with snprintf %3.f format

err error
}

type indexedPingReply struct {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type is used to in the channel.

I want to save individual ping requests in the order that I sent them to the channel, not in the possibly reorganized order that they come out.

func collectPingReplies(replies <-chan indexedPingReply, sent, packets int, err error) []pingReply {
results := make([]pingReply, packets)
for i := range results {
results[i] = pingReply{status: ipReqTimedOut}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default allocate results slice with timeouts, will get overriden with actual results if they come


timeout := perPacket
if deadline, ok := ctx.Deadline(); ok {
if remaining := time.Until(deadline); remaining < timeout {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remaining to the check context timeout.

If that is smaller than per ping packet timeout, update it

0, // Event
0, // ApcRoutine
0, // ApcContext
uintptr(unsafe.Pointer(&source)),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

procIcmp6SendEcho2 accepts source and destination addresses without specifying length.

likely since they are of known windows.RawSockaddrInet6 type.

what is weird is that it needs a source address? In this case it just sets it as "::" and leaves the windows routing stack pick where it gets sent from.

But why would you need to set it as something else here? It can bind to different ipv6 addresses if the node has it. The same would go for ipv4 if we used IcmpSendEcho2Ex .

But there is no need to support that in snclient, too in depth for monitoring agent.

@inqrphl

inqrphl commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

This should fix #337 and invalidate #495 by not using ping.exe at all.

@inqrphl
inqrphl marked this pull request as ready for review November 11, 2026 15:29
@inqrphl
inqrphl requested a review from sni November 11, 2026 15:29
@sni
sni merged commit a4264da into main Nov 19, 2026
151 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants