Skip to content

Commit 675debc

Browse files
Meissi-jianzs39
authored andcommitted
apps/net: add ip address connectivity check API
The system checks connectivity with the input IP address; if no IP address is provided, it defaults to checking DNS server connectivity. Signed-off-by: meijian <meijian@xiaomi.com>
1 parent 9da2bc0 commit 675debc

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

include/netutils/netlib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,10 @@ int netlib_getifstatistics(FAR const char *ifname,
508508
int netlib_check_ifconflict(FAR const char *ifname);
509509
#endif
510510

511+
#ifdef CONFIG_NETUTILS_PING
512+
int netlib_check_ipconnectivity(FAR const char *ip, int timeout, int retry);
513+
#endif
514+
511515
#ifdef CONFIG_MM_IOB
512516
int netlib_get_iobinfo(FAR struct iob_stats_s *iob);
513517
#endif

netutils/netlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ if(CONFIG_NETUTILS_NETLIB)
153153
list(APPEND SRCS netlib_getiobinfo.c)
154154
endif()
155155

156+
if(CONFIG_NETUTILS_PING)
157+
list(APPEND SRCS netlib_checkipconnectivity.c)
158+
endif()
159+
156160
target_sources(apps PRIVATE ${SRCS})
157161

158162
endif()

netutils/netlib/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,8 @@ ifeq ($(CONFIG_MM_IOB),y)
152152
CSRCS += netlib_getiobinfo.c
153153
endif
154154

155+
ifeq ($(CONFIG_NETUTILS_PING),y)
156+
CSRCS += netlib_checkipconnectivity.c
157+
endif
158+
155159
include $(APPDIR)/Application.mk
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/****************************************************************************
2+
* apps/netutils/netlib/netlib_checkipconnectivity.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 <sys/socket.h>
28+
#include <sys/ioctl.h>
29+
#include <stdint.h>
30+
#include <stdbool.h>
31+
#include <unistd.h>
32+
#include <string.h>
33+
#include <errno.h>
34+
#include <debug.h>
35+
36+
#include <arpa/inet.h>
37+
#include <nuttx/net/ip.h>
38+
39+
#include "netutils/icmp_ping.h"
40+
#include "netutils/netlib.h"
41+
42+
#ifdef CONFIG_NETUTILS_PING
43+
44+
/****************************************************************************
45+
* Private Data
46+
****************************************************************************/
47+
48+
#define PING_IPADDR_DATALEN 80
49+
50+
/****************************************************************************
51+
* Private Functions
52+
****************************************************************************/
53+
54+
/****************************************************************************
55+
* Name: ping_ipaddr_callback
56+
*
57+
* Description:
58+
* ping ipaddr callback
59+
*
60+
* Parameters:
61+
* result The result of ping
62+
*
63+
****************************************************************************/
64+
65+
static void ping_ipaddr_callback(FAR const struct ping_result_s *result)
66+
{
67+
FAR int *count = result->info->priv;
68+
69+
if (result->code == ICMP_I_FINISH)
70+
{
71+
*count = result->nreplies;
72+
}
73+
}
74+
75+
/****************************************************************************
76+
* Public Functions
77+
****************************************************************************/
78+
79+
/****************************************************************************
80+
* Name: netlib_check_ipconnectivity
81+
*
82+
* Description:
83+
* Get the network dns status
84+
*
85+
* Parameters:
86+
* ip The ipv4 address to check
87+
* timeout The max timeout of each ping
88+
* retry The retry times of ping
89+
*
90+
* Return:
91+
* nums of remote reply of ping; a nagtive on failure
92+
*
93+
****************************************************************************/
94+
95+
int netlib_check_ipconnectivity(FAR const char *ip, int timeout, int retry)
96+
{
97+
int replies = 0;
98+
#ifdef CONFIG_NETDB_DNSSERVER_IPv4ADDR
99+
char ip_str[INET_ADDRSTRLEN];
100+
struct in_addr addr;
101+
#endif
102+
struct ping_info_s info;
103+
104+
info.count = retry;
105+
info.datalen = PING_IPADDR_DATALEN;
106+
info.delay = 0;
107+
info.timeout = timeout;
108+
info.callback = ping_ipaddr_callback;
109+
info.priv = &replies;
110+
111+
/* if ip is NULL we use default DNS server */
112+
113+
if (!ip)
114+
{
115+
#ifdef CONFIG_NETDB_DNSSERVER_IPv4ADDR
116+
addr.s_addr = HTONL(CONFIG_NETDB_DNSSERVER_IPv4ADDR);
117+
inet_ntop(AF_INET, &addr, ip_str, INET_ADDRSTRLEN);
118+
info.hostname = ip_str;
119+
#else
120+
nerr("ERROR: ip is null and no dnsserver configured\n");
121+
return -EINVAL;
122+
#endif
123+
}
124+
else
125+
{
126+
info.hostname = ip;
127+
}
128+
129+
ninfo("ping ipaddr test %s \n", info.hostname);
130+
icmp_ping(&info);
131+
132+
return replies;
133+
}
134+
135+
#endif /* CONFIG_NETUTILS_PING */

0 commit comments

Comments
 (0)