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