From 42f2e5a7459110df483be4e9418b396d365734d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 30 Apr 2026 11:00:22 +0800 Subject: [PATCH] Netif (macOS): improves reliability --- src/common/impl/netif_apple.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/common/impl/netif_apple.c b/src/common/impl/netif_apple.c index 1b12a8888..85614acb2 100644 --- a/src/common/impl/netif_apple.c +++ b/src/common/impl/netif_apple.c @@ -1,6 +1,5 @@ #include "common/netif.h" #include "common/io.h" -#include "common/mallocHelper.h" #include #include @@ -98,7 +97,16 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) { return false; } - while (recv(pfRoute, &rtmsg, sizeof(rtmsg), 0) > 0 && !(rtmsg.hdr.rtm_seq == 1 && rtmsg.hdr.rtm_pid == (pid_t) pid)); + bool gotResponse = false; + while (recv(pfRoute, &rtmsg, sizeof(rtmsg), 0) > 0) { + if (rtmsg.hdr.rtm_seq == 1 && rtmsg.hdr.rtm_pid == (pid_t) pid) { + gotResponse = true; + break; + } + } + if (!gotResponse) { + return false; + } #ifndef __sun // On Solaris, the RTF_GATEWAY flag is not set for default routes for some reason if ((rtmsg.hdr.rtm_flags & (RTF_UP | RTF_GATEWAY)) == (RTF_UP | RTF_GATEWAY)) @@ -109,6 +117,7 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) { #ifndef __sun && sdl->sdl_len #endif + && sdl->sdl_family == AF_LINK ) { if (sdl->sdl_nlen > IF_NAMESIZE) { return false; @@ -173,7 +182,16 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) { return false; } - while (recv(pfRoute, &rtmsg, sizeof(rtmsg), 0) > 0 && !(rtmsg.hdr.rtm_seq == 2 && rtmsg.hdr.rtm_pid == (pid_t) pid)); + bool gotResponse = false; + while (recv(pfRoute, &rtmsg, sizeof(rtmsg), 0) > 0) { + if (rtmsg.hdr.rtm_seq == 2 && rtmsg.hdr.rtm_pid == (pid_t) pid) { + gotResponse = true; + break; + } + } + if (!gotResponse) { + return false; + } #ifndef __sun // On Solaris, the RTF_GATEWAY flag is not set for default routes for some reason if ((rtmsg.hdr.rtm_flags & (RTF_UP | RTF_GATEWAY)) == (RTF_UP | RTF_GATEWAY)) @@ -184,6 +202,7 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) { #ifndef __sun && sdl->sdl_len #endif + && sdl->sdl_family == AF_LINK ) { if (sdl->sdl_nlen > IF_NAMESIZE) { return false;