This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new c61fb846d apps/net: add if network conntivity check API
c61fb846d is described below

commit c61fb846d7cefc43d52066db00bec756e99e70a6
Author: meijian <[email protected]>
AuthorDate: Thu Oct 10 16:34:10 2024 +0800

    apps/net: add if network conntivity check API
    
    Perform connectivity testing on the specified network interface card.
    
    Signed-off-by: meijian <[email protected]>
---
 include/netutils/netlib.h                    |  2 +
 netutils/netlib/CMakeLists.txt               |  2 +-
 netutils/netlib/Makefile                     |  2 +-
 netutils/netlib/netlib_checkifconnectivity.c | 89 ++++++++++++++++++++++++++++
 4 files changed, 93 insertions(+), 2 deletions(-)

diff --git a/include/netutils/netlib.h b/include/netutils/netlib.h
index 5be9747b2..2e7f7bc60 100644
--- a/include/netutils/netlib.h
+++ b/include/netutils/netlib.h
@@ -515,6 +515,8 @@ int netlib_check_ifconflict(FAR const char *ifname);
 
 #ifdef CONFIG_NETUTILS_PING
 int netlib_check_ipconnectivity(FAR const char *ip, int timeout, int retry);
+int netlib_check_ifconnectivity(FAR const char *ifname,
+                                int timeout, int retry);
 #endif
 
 #ifdef CONFIG_MM_IOB
diff --git a/netutils/netlib/CMakeLists.txt b/netutils/netlib/CMakeLists.txt
index 632b32857..be87f33fa 100644
--- a/netutils/netlib/CMakeLists.txt
+++ b/netutils/netlib/CMakeLists.txt
@@ -158,7 +158,7 @@ if(CONFIG_NETUTILS_NETLIB)
   endif()
 
   if(CONFIG_NETUTILS_PING)
-    list(APPEND SRCS netlib_checkipconnectivity.c)
+    list(APPEND SRCS netlib_checkipconnectivity.c netlib_checkifconnectivity.c)
   endif()
 
   target_sources(apps PRIVATE ${SRCS})
diff --git a/netutils/netlib/Makefile b/netutils/netlib/Makefile
index 0de8c07e5..a55340c1f 100644
--- a/netutils/netlib/Makefile
+++ b/netutils/netlib/Makefile
@@ -157,7 +157,7 @@ CSRCS += netlib_getiobinfo.c
 endif
 
 ifeq ($(CONFIG_NETUTILS_PING),y)
-CSRCS += netlib_checkipconnectivity.c
+CSRCS += netlib_checkipconnectivity.c netlib_checkifconnectivity.c
 endif
 
 include $(APPDIR)/Application.mk
diff --git a/netutils/netlib/netlib_checkifconnectivity.c 
b/netutils/netlib/netlib_checkifconnectivity.c
new file mode 100644
index 000000000..c08995366
--- /dev/null
+++ b/netutils/netlib/netlib_checkifconnectivity.c
@@ -0,0 +1,89 @@
+/****************************************************************************
+ * apps/netutils/netlib/netlib_checkifconnectivity.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <arpa/inet.h>
+#include <nuttx/net/ip.h>
+
+#include "netutils/netlib.h"
+
+#ifdef CONFIG_NETUTILS_PING
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: netlib_check_ifconnectivity
+ *
+ * Description:
+ *   Check network interface connectivity by pinging the gateway
+ *
+ * Parameters:
+ *   ifname   The name of the interface to use
+ *   timeout  The timeout of ping
+ *   retry    The retry times of ping
+ *
+ * Return:
+ *   nums of gateway reply of ping; a nagtive on failure.
+ *
+ ****************************************************************************/
+
+int netlib_check_ifconnectivity(FAR const char *ifname,
+                                int timeout, int retry)
+{
+  int ret;
+  char destip[INET_ADDRSTRLEN];
+  struct in_addr addr;
+
+  ret = netlib_get_dripv4addr(ifname, &addr);
+  if (ret < 0)
+    {
+      nerr("ERROR: failed to get gateway ip %s\n", ifname);
+      return ret;
+    }
+
+  if (net_ipv4addr_cmp(&addr, INADDR_ANY))
+    {
+      nerr("ERROR: failed to get gateway ip %s\n", ifname);
+      return -EINVAL;
+    }
+
+  inet_ntop(AF_INET, &addr, destip, sizeof(destip));
+  ninfo("ping gateway %s \n", destip);
+
+  return netlib_check_ipconnectivity(destip, timeout, retry);
+}
+
+#endif /* CONFIG_NETUTILS_PING */
\ No newline at end of file

Reply via email to