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 4b3070c08 apps/net: add ip conflict check API
4b3070c08 is described below
commit 4b3070c08148f245f0dc43c31b6583467f04a17b
Author: meijian <[email protected]>
AuthorDate: Sun Sep 29 16:26:14 2024 +0800
apps/net: add ip conflict check API
Supports checking for IP conflicts on a NIC.
Signed-off-by: meijian <[email protected]>
---
include/netutils/netlib.h | 6 ++
netutils/netlib/CMakeLists.txt | 6 ++
netutils/netlib/Makefile | 6 ++
netutils/netlib/netlib_checkifconflict.c | 103 +++++++++++++++++++++++++++++++
4 files changed, 121 insertions(+)
diff --git a/include/netutils/netlib.h b/include/netutils/netlib.h
index 469bd0461..9f8f744cd 100644
--- a/include/netutils/netlib.h
+++ b/include/netutils/netlib.h
@@ -499,6 +499,12 @@ int netlib_getifstatistics(FAR const char *ifname,
FAR struct netdev_statistics_s *stat);
#endif
+/* Network check support */
+
+#ifdef CONFIG_NET_ARP_ACD
+int netlib_check_ifconflict(FAR const char *ifname);
+#endif
+
#undef EXTERN
#ifdef __cplusplus
}
diff --git a/netutils/netlib/CMakeLists.txt b/netutils/netlib/CMakeLists.txt
index 66bc927a0..560a71834 100644
--- a/netutils/netlib/CMakeLists.txt
+++ b/netutils/netlib/CMakeLists.txt
@@ -143,6 +143,12 @@ if(CONFIG_NETUTILS_NETLIB)
endif()
endif()
+ # Network check support
+
+ if(CONFIG_NET_ARP_ACD)
+ list(APPEND SRCS netlib_checkifconflict.c)
+ endif()
+
target_sources(apps PRIVATE ${SRCS})
endif()
diff --git a/netutils/netlib/Makefile b/netutils/netlib/Makefile
index c590970e5..237cc76de 100644
--- a/netutils/netlib/Makefile
+++ b/netutils/netlib/Makefile
@@ -142,4 +142,10 @@ CSRCS += netlib_ipmsfilter.c
endif
endif
+# Network check support
+
+ifeq ($(CONFIG_NET_ARP_ACD),y)
+CSRCS += netlib_checkifconflict.c
+endif
+
include $(APPDIR)/Application.mk
diff --git a/netutils/netlib/netlib_checkifconflict.c
b/netutils/netlib/netlib_checkifconflict.c
new file mode 100644
index 000000000..5aebddd8e
--- /dev/null
+++ b/netutils/netlib/netlib_checkifconflict.c
@@ -0,0 +1,103 @@
+/****************************************************************************
+ * apps/netutils/netlib/netlib_checkifconflict.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 <stdint.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <debug.h>
+
+#include "netutils/netlib.h"
+
+#ifdef CONFIG_NET_ARP_ACD
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Determines the size of an intermediate buffer that must be large enough
+ * to handle the longest line generated by this logic.
+ */
+
+#define PROCFS_LINELEN 80
+#define PROCFS_NET_PATH "/proc/net/"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: netlib_check_ifconflict
+ *
+ * Description:
+ * Check the network driver ip conflict
+ *
+ * Parameters:
+ * ifname The name of the interface to use
+ *
+ * Return:
+ * 0 on no conflict; a nagtive on failure; 1 on conflict
+ *
+ ****************************************************************************/
+
+int netlib_check_ifconflict(FAR const char *ifname)
+{
+ int conflict = 0;
+ char path[32];
+ char line[PROCFS_LINELEN];
+ FAR FILE *stream;
+
+ if (ifname == NULL)
+ {
+ fprintf(stderr, "ERROR: ifname is NULL \n");
+ return -EINVAL;
+ }
+
+ snprintf(path, sizeof(path), "%s%s", PROCFS_NET_PATH, ifname);
+ ninfo("get dev statistics from %s \n", path);
+
+ stream = fopen(path, "r");
+ if (stream == NULL)
+ {
+ fprintf(stderr, "ERROR: Failed to open path:%s \n", path);
+ return -errno;
+ }
+
+ while (fgets(line, sizeof(line), stream) != NULL)
+ {
+ if (strstr(line, "conflict!") != NULL)
+ {
+ conflict = 1;
+ break;
+ }
+ }
+
+ fclose(stream);
+ return conflict;
+}
+
+#endif /* CONFIG_NET_ARP_ACD */