fix(windows): SOCKET_ADDRESS carries lpSockaddr, and MinGW spells the oper status IfOperStatusUp
AuthorPico <picobot@scorpioplayer.com>
Date2026-11-12 11:45:59
Commit1e64f08f
Parents8969a32f
Changed files
src/plugin_netcheck.c+15−7■■■■■
tests/run.sh+2−1■■■
2 files+17−8
Diff
src/plugin_netcheck.c+15−7■■■■■
index 6d0d630..e04be41 100644
--- a/src/plugin_netcheck.c
+++ b/src/plugin_netcheck.c
@@ -395,28 +395,34 @@ static void collect_ifaces_windows(void)
395395 for (a = adapters; a; a = a->Next) {
396396 char ascii[64];
397397 const wchar_t *friendly = a->FriendlyName ? a->FriendlyName : NULL;
398- int up = (a->OperStatus == MIF_OPER_UP);
398+ int up = (a->OperStatus == IfOperStatusUp);
399399
400400 if (friendly) wide_to_ascii(friendly, ascii, sizeof ascii);
401401 else if (a->AdapterName) str_copy(ascii, sizeof ascii, a->AdapterName);
402402 else str_copy(ascii, sizeof ascii, "adapter");
403403
404404 for (u = a->FirstUnicastAddress; u; u = u->Next) {
405+ const struct sockaddr *sa = u->Address.lpSockaddr;
405406 struct sockaddr_storage nm;
406407 struct sockaddr *nmp = NULL;
407408
409+ /* SOCKET_ADDRESS is {lpSockaddr, iSockaddrLength}, not an inline
410+ * sockaddr, so the family has to be read through the pointer and
411+ * guarded by the length the OS reported. */
412+ if (!sa || u->Address.iSockaddrLength < (int)sizeof(struct sockaddr))
413+ continue;
414+
408415 /* OnLinkPrefixLength is a CIDR length, not a dotted mask. Expand it
409416 * into a dotted netmask so the table has something familiar to show. */
410417 memset(&nm, 0, sizeof nm);
411- if (u->Address.sa_family == AF_INET && u->OnLinkPrefixLength < 32) {
418+ if (sa->sa_family == AF_INET && u->OnLinkPrefixLength < 32) {
412419 struct sockaddr_in *in4 = (struct sockaddr_in *)&nm;
413420 in4->sin_family = AF_INET;
414421 in4->sin_addr.s_addr =
415422 htonl(0xFFFFFFFFUL << (32 - u->OnLinkPrefixLength));
416423 nmp = (struct sockaddr *)in4;
417424 }
418- iface_add(ascii, (const struct sockaddr *)&u->Address, nmp, NULL,
419- up, up);
425+ iface_add(ascii, sa, nmp, NULL, up, up);
420426 }
421427 }
422428 free(adapters);
@@ -727,8 +733,10 @@ static void discover_nameservers(void)
727733 for (a = adapters; a; a = a->Next)
728734 for (d = a->FirstDnsServerAddress; d; d = d->Next) {
729735 char txt[64];
730- if (addr_text((const struct sockaddr *)&d->Address,
731- txt, sizeof txt))
736+ const struct sockaddr *sa = d->Address.lpSockaddr;
737+ if (!sa || d->Address.iSockaddrLength < (int)sizeof(struct sockaddr))
738+ continue;
739+ if (addr_text(sa, txt, sizeof txt))
732740 ns_add(txt);
733741 }
734742 free(adapters);
@@ -1216,7 +1224,7 @@ static void action_tcp(const char *host_in, const char *port_s)
12161224 else if (rc != 0 || !res) { status = 0; samples[nsamp++] = -2; }
12171225 else {
12181226 for (i = 0; i < MAX_SAMPLES; i++) {
1219- long long r;
1227+ long long r = -4; /* no address family could be opened */
12201228 for (rp = res; rp; rp = rp->ai_next) {
12211229 r = tcp_connect_ms(rp, TCP_MS, addr, sizeof addr);
12221230 if (r >= 0 || r == -2) break; /* try the next family only on socket failure */
tests/run.sh+2−1■■■
index e05f07f..a4a7daa 100644
--- a/tests/run.sh
+++ b/tests/run.sh
@@ -411,8 +411,9 @@ grep -q 'OnLinkPrefixLength' src/plugin_netcheck.c \
411411 && pass "the CIDR prefix is expanded to a netmask" || fail "no prefix expansion"
412412 grep -q 'GAA_FLAG_SKIP_DNS_SERVER' src/plugin_netcheck.c \
413413 && pass "the adapter query skips dns" || fail "no dns skip flag"
414-grep -q 'MIF_OPER_UP' src/plugin_netcheck.c \
414+grep -q 'IfOperStatusUp' src/plugin_netcheck.c \
415415 && pass "adapter state is read from OperStatus" || fail "no OperStatus"
416+
416417 grep -q 'IFF_UP' src/plugin_netcheck.c \
417418 && pass "posix iface state is read from ifa_flags" || fail "no IFF_UP"
418419 grep -q '#include "ui_html.h"' src/plugin_netcheck.c \
419420
src/plugin_netcheck.c+15−7■■■■■
index 6d0d630..e04be41 100644index 6d0d630..e04be41 100644
--- a/src/plugin_netcheck.c--- a/src/plugin_netcheck.c
+++ b/src/plugin_netcheck.c+++ b/src/plugin_netcheck.c
@@ -395,28 +395,34 @@ static void collect_ifaces_windows(void)@@ -395,28 +395,34 @@ static void collect_ifaces_windows(void)
395 for (a = adapters; a; a = a->Next) {395 for (a = adapters; a; a = a->Next) {
396 char ascii[64];396 char ascii[64];
397 const wchar_t *friendly = a->FriendlyName ? a->FriendlyName : NULL;397 const wchar_t *friendly = a->FriendlyName ? a->FriendlyName : NULL;
398- int up = (a->OperStatus == MIF_OPER_UP);398+ int up = (a->OperStatus == IfOperStatusUp);
399 399
400 if (friendly) wide_to_ascii(friendly, ascii, sizeof ascii);400 if (friendly) wide_to_ascii(friendly, ascii, sizeof ascii);
401 else if (a->AdapterName) str_copy(ascii, sizeof ascii, a->AdapterName);401 else if (a->AdapterName) str_copy(ascii, sizeof ascii, a->AdapterName);
402 else str_copy(ascii, sizeof ascii, "adapter");402 else str_copy(ascii, sizeof ascii, "adapter");
403 403
404 for (u = a->FirstUnicastAddress; u; u = u->Next) {404 for (u = a->FirstUnicastAddress; u; u = u->Next) {
405+ const struct sockaddr *sa = u->Address.lpSockaddr;
405 struct sockaddr_storage nm;406 struct sockaddr_storage nm;
406 struct sockaddr *nmp = NULL;407 struct sockaddr *nmp = NULL;
407 408
409+ /* SOCKET_ADDRESS is {lpSockaddr, iSockaddrLength}, not an inline
410+ * sockaddr, so the family has to be read through the pointer and
411+ * guarded by the length the OS reported. */
412+ if (!sa || u->Address.iSockaddrLength < (int)sizeof(struct sockaddr))
413+ continue;
414+
408 /* OnLinkPrefixLength is a CIDR length, not a dotted mask. Expand it415 /* OnLinkPrefixLength is a CIDR length, not a dotted mask. Expand it
409 * into a dotted netmask so the table has something familiar to show. */416 * into a dotted netmask so the table has something familiar to show. */
410 memset(&nm, 0, sizeof nm);417 memset(&nm, 0, sizeof nm);
411- if (u->Address.sa_family == AF_INET && u->OnLinkPrefixLength < 32) {418+ if (sa->sa_family == AF_INET && u->OnLinkPrefixLength < 32) {
412 struct sockaddr_in *in4 = (struct sockaddr_in *)&nm;419 struct sockaddr_in *in4 = (struct sockaddr_in *)&nm;
413 in4->sin_family = AF_INET;420 in4->sin_family = AF_INET;
414 in4->sin_addr.s_addr =421 in4->sin_addr.s_addr =
415 htonl(0xFFFFFFFFUL << (32 - u->OnLinkPrefixLength));422 htonl(0xFFFFFFFFUL << (32 - u->OnLinkPrefixLength));
416 nmp = (struct sockaddr *)in4;423 nmp = (struct sockaddr *)in4;
417 }424 }
418- iface_add(ascii, (const struct sockaddr *)&u->Address, nmp, NULL,425+ iface_add(ascii, sa, nmp, NULL, up, up);
419- up, up);
420 }426 }
421 }427 }
422 free(adapters);428 free(adapters);
@@ -727,8 +733,10 @@ static void discover_nameservers(void)@@ -727,8 +733,10 @@ static void discover_nameservers(void)
727 for (a = adapters; a; a = a->Next)733 for (a = adapters; a; a = a->Next)
728 for (d = a->FirstDnsServerAddress; d; d = d->Next) {734 for (d = a->FirstDnsServerAddress; d; d = d->Next) {
729 char txt[64];735 char txt[64];
730- if (addr_text((const struct sockaddr *)&d->Address,736+ const struct sockaddr *sa = d->Address.lpSockaddr;
731- txt, sizeof txt))737+ if (!sa || d->Address.iSockaddrLength < (int)sizeof(struct sockaddr))
738+ continue;
739+ if (addr_text(sa, txt, sizeof txt))
732 ns_add(txt);740 ns_add(txt);
733 }741 }
734 free(adapters);742 free(adapters);
@@ -1216,7 +1224,7 @@ static void action_tcp(const char *host_in, const char *port_s)@@ -1216,7 +1224,7 @@ static void action_tcp(const char *host_in, const char *port_s)
1216 else if (rc != 0 || !res) { status = 0; samples[nsamp++] = -2; }1224 else if (rc != 0 || !res) { status = 0; samples[nsamp++] = -2; }
1217 else {1225 else {
1218 for (i = 0; i < MAX_SAMPLES; i++) {1226 for (i = 0; i < MAX_SAMPLES; i++) {
1219- long long r;1227+ long long r = -4; /* no address family could be opened */
1220 for (rp = res; rp; rp = rp->ai_next) {1228 for (rp = res; rp; rp = rp->ai_next) {
1221 r = tcp_connect_ms(rp, TCP_MS, addr, sizeof addr);1229 r = tcp_connect_ms(rp, TCP_MS, addr, sizeof addr);
1222 if (r >= 0 || r == -2) break; /* try the next family only on socket failure */1230 if (r >= 0 || r == -2) break; /* try the next family only on socket failure */
tests/run.sh+2−1■■■
index e05f07f..a4a7daa 100644index e05f07f..a4a7daa 100644
--- a/tests/run.sh--- a/tests/run.sh
+++ b/tests/run.sh+++ b/tests/run.sh
@@ -411,8 +411,9 @@ grep -q 'OnLinkPrefixLength' src/plugin_netcheck.c \@@ -411,8 +411,9 @@ grep -q 'OnLinkPrefixLength' src/plugin_netcheck.c \
411 && pass "the CIDR prefix is expanded to a netmask" || fail "no prefix expansion"411 && pass "the CIDR prefix is expanded to a netmask" || fail "no prefix expansion"
412 grep -q 'GAA_FLAG_SKIP_DNS_SERVER' src/plugin_netcheck.c \412 grep -q 'GAA_FLAG_SKIP_DNS_SERVER' src/plugin_netcheck.c \
413 && pass "the adapter query skips dns" || fail "no dns skip flag"413 && pass "the adapter query skips dns" || fail "no dns skip flag"
414-grep -q 'MIF_OPER_UP' src/plugin_netcheck.c \414+grep -q 'IfOperStatusUp' src/plugin_netcheck.c \
415 && pass "adapter state is read from OperStatus" || fail "no OperStatus"415 && pass "adapter state is read from OperStatus" || fail "no OperStatus"
416+
416 grep -q 'IFF_UP' src/plugin_netcheck.c \417 grep -q 'IFF_UP' src/plugin_netcheck.c \
417 && pass "posix iface state is read from ifa_flags" || fail "no IFF_UP"418 && pass "posix iface state is read from ifa_flags" || fail "no IFF_UP"
418 grep -q '#include "ui_html.h"' src/plugin_netcheck.c \419 grep -q '#include "ui_html.h"' src/plugin_netcheck.c \
419420