Skip to content

Commit 4b3070c

Browse files
Meissi-jianxiaoxiang781216
authored andcommitted
apps/net: add ip conflict check API
Supports checking for IP conflicts on a NIC. Signed-off-by: meijian <meijian@xiaomi.com>
1 parent 837c27d commit 4b3070c

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

include/netutils/netlib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@ int netlib_getifstatistics(FAR const char *ifname,
499499
FAR struct netdev_statistics_s *stat);
500500
#endif
501501

502+
/* Network check support */
503+
504+
#ifdef CONFIG_NET_ARP_ACD
505+
int netlib_check_ifconflict(FAR const char *ifname);
506+
#endif
507+
502508
#undef EXTERN
503509
#ifdef __cplusplus
504510
}

netutils/netlib/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ if(CONFIG_NETUTILS_NETLIB)
143143
endif()
144144
endif()
145145

146+
# Network check support
147+
148+
if(CONFIG_NET_ARP_ACD)
149+
list(APPEND SRCS netlib_checkifconflict.c)
150+
endif()
151+
146152
target_sources(apps PRIVATE ${SRCS})
147153

148154
endif()

netutils/netlib/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,10 @@ CSRCS += netlib_ipmsfilter.c
142142
endif
143143
endif
144144

145+
# Network check support
146+
147+
ifeq ($(CONFIG_NET_ARP_ACD),y)
148+
CSRCS += netlib_checkifconflict.c
149+
endif
150+
145151
include $(APPDIR)/Application.mk
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/****************************************************************************
2+
* apps/netutils/netlib/netlib_checkifconflict.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
27+
#include <stdint.h>
28+
#include <stdbool.h>
29+
#include <unistd.h>
30+
#include <string.h>
31+
#include <errno.h>
32+
#include <debug.h>
33+
34+
#include "netutils/netlib.h"
35+
36+
#ifdef CONFIG_NET_ARP_ACD
37+
38+
/****************************************************************************
39+
* Pre-processor Definitions
40+
****************************************************************************/
41+
42+
/* Determines the size of an intermediate buffer that must be large enough
43+
* to handle the longest line generated by this logic.
44+
*/
45+
46+
#define PROCFS_LINELEN 80
47+
#define PROCFS_NET_PATH "/proc/net/"
48+
49+
/****************************************************************************
50+
* Public Functions
51+
****************************************************************************/
52+
53+
/****************************************************************************
54+
* Name: netlib_check_ifconflict
55+
*
56+
* Description:
57+
* Check the network driver ip conflict
58+
*
59+
* Parameters:
60+
* ifname The name of the interface to use
61+
*
62+
* Return:
63+
* 0 on no conflict; a nagtive on failure; 1 on conflict
64+
*
65+
****************************************************************************/
66+
67+
int netlib_check_ifconflict(FAR const char *ifname)
68+
{
69+
int conflict = 0;
70+
char path[32];
71+
char line[PROCFS_LINELEN];
72+
FAR FILE *stream;
73+
74+
if (ifname == NULL)
75+
{
76+
fprintf(stderr, "ERROR: ifname is NULL \n");
77+
return -EINVAL;
78+
}
79+
80+
snprintf(path, sizeof(path), "%s%s", PROCFS_NET_PATH, ifname);
81+
ninfo("get dev statistics from %s \n", path);
82+
83+
stream = fopen(path, "r");
84+
if (stream == NULL)
85+
{
86+
fprintf(stderr, "ERROR: Failed to open path:%s \n", path);
87+
return -errno;
88+
}
89+
90+
while (fgets(line, sizeof(line), stream) != NULL)
91+
{
92+
if (strstr(line, "conflict!") != NULL)
93+
{
94+
conflict = 1;
95+
break;
96+
}
97+
}
98+
99+
fclose(stream);
100+
return conflict;
101+
}
102+
103+
#endif /* CONFIG_NET_ARP_ACD */

0 commit comments

Comments
 (0)