Skip to content

Commit debde5c

Browse files
committed
netlib: replace DNS ping with gateway ping for connectivity check
Refactor netlib_check_ipconnectivity() to use gateway/router ping instead of DNS server ping for network connectivity checks. Add IPv6 support. Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
1 parent 5478167 commit debde5c

5 files changed

Lines changed: 363 additions & 34 deletions

File tree

include/netutils/netlib.h

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,49 @@ int netlib_getifstatistics(FAR const char *ifname,
561561
int netlib_check_ifconflict(FAR const char *ifname);
562562
#endif
563563

564-
#ifdef CONFIG_NETUTILS_PING
564+
/****************************************************************************
565+
* Name: netlib_check_ipconnectivity
566+
*
567+
* Description:
568+
* Check network connectivity by pinging a remote IP address.
569+
* If ip is NULL, ping the gateway of each network interface,
570+
* and optionally the routers from the routing table. If ping
571+
* is disabled, just check the status of the IP network card.
572+
*
573+
* Parameters:
574+
* ip The ipv4 address to check, or NULL to ping gateways
575+
* timeout The max timeout of each ping
576+
* retry The retry times of ping
577+
*
578+
* Return:
579+
* nums of remote reply of ping; a negative or ZERO on failure
580+
*
581+
****************************************************************************/
582+
565583
int netlib_check_ipconnectivity(FAR const char *ip, int timeout, int retry);
584+
585+
#ifdef CONFIG_NETUTILS_PING
586+
587+
/****************************************************************************
588+
* Name: netlib_check_ifconnectivity
589+
*
590+
* Description:
591+
* Check network connectivity by pinging the default gateway
592+
* of the specified network interface.
593+
*
594+
* Parameters:
595+
* ifname The name of the interface to use
596+
* timeout The timeout of ping
597+
* retry The retry times of ping
598+
*
599+
* Return:
600+
* nums of gateway reply of ping; a negative on failure.
601+
*
602+
****************************************************************************/
603+
566604
int netlib_check_ifconnectivity(FAR const char *ifname,
567605
int timeout, int retry);
568606
#else
569-
#define netlib_check_ipconnectivity(i, t, r) 1
570607
#define netlib_check_ifconnectivity(i, t, r) 1
571608
#endif
572609

netutils/netlib/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,10 @@ if(CONFIG_NETUTILS_NETLIB)
158158
list(APPEND SRCS netlib_getiobinfo.c)
159159
endif()
160160

161+
list(APPEND SRCS netlib_checkipconnectivity.c)
162+
161163
if(CONFIG_NETUTILS_PING)
162-
list(APPEND SRCS netlib_checkipconnectivity.c netlib_checkifconnectivity.c)
164+
list(APPEND SRCS netlib_checkifconnectivity.c)
163165
endif()
164166

165167
list(APPEND SRCS netlib_checkhttpconnectivity.c)

netutils/netlib/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ ifeq ($(CONFIG_MM_IOB),y)
157157
CSRCS += netlib_getiobinfo.c
158158
endif
159159

160+
CSRCS += netlib_checkipconnectivity.c
161+
160162
ifeq ($(CONFIG_NETUTILS_PING),y)
161-
CSRCS += netlib_checkipconnectivity.c netlib_checkifconnectivity.c
163+
CSRCS += netlib_checkifconnectivity.c
162164
endif
163165

164166
CSRCS += netlib_checkhttpconnectivity.c

netutils/netlib/netlib_checkifconnectivity.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@
4848
* Name: netlib_check_ifconnectivity
4949
*
5050
* Description:
51-
* Check network interface connectivity by pinging the gateway
51+
* Check network connectivity by pinging the default gateway
52+
* of the specified network interface.
5253
*
5354
* Parameters:
5455
* ifname The name of the interface to use
5556
* timeout The timeout of ping
5657
* retry The retry times of ping
5758
*
5859
* Return:
59-
* nums of gateway reply of ping; a nagtive on failure.
60+
* nums of gateway reply of ping; a negative on failure.
6061
*
6162
****************************************************************************/
6263

@@ -86,4 +87,4 @@ int netlib_check_ifconnectivity(FAR const char *ifname,
8687
return netlib_check_ipconnectivity(destip, timeout, retry);
8788
}
8889

89-
#endif /* CONFIG_NETUTILS_PING */
90+
#endif /* CONFIG_NETUTILS_PING */

0 commit comments

Comments
 (0)