Skip to content

Commit 14f1554

Browse files
committed
netutils/ping: improve ping and ping6 for error handling
dns look for both IPv4/v6 when do dns_query, so when do ping, it should try both ipv4 and ipv6 address before return "ping_gethostip" error. Signed-off-by: Jerry Ma <masc2008@gmail.com>
1 parent 76e02c0 commit 14f1554

File tree

8 files changed

+213
-147
lines changed

8 files changed

+213
-147
lines changed

include/netutils/icmp_ping.h

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
****************************************************************************/
2929

3030
#include <netinet/in.h>
31+
#include "icmp_pub.h"
3132

3233
/****************************************************************************
3334
* Pre-processor Definitions
@@ -42,7 +43,6 @@
4243

4344
/* Negative odd number represent error(unrecoverable) */
4445

45-
#define ICMP_E_HOSTIP -1 /* extra: not used */
4646
#define ICMP_E_MEMORY -3 /* extra: not used */
4747
#define ICMP_E_SOCKET -5 /* extra: error code */
4848
#define ICMP_E_SENDTO -7 /* extra: error code */
@@ -67,53 +67,8 @@
6767
* Public Types
6868
****************************************************************************/
6969

70-
struct ping_result_s;
71-
72-
struct ping_info_s
73-
{
74-
FAR const char *hostname; /* Host name to ping */
75-
#ifdef CONFIG_NET_BINDTODEVICE
76-
FAR const char *devname; /* Device name to bind */
77-
#endif
78-
uint16_t count; /* Number of pings requested */
79-
uint16_t datalen; /* Number of bytes to be sent */
80-
uint16_t delay; /* Deciseconds to delay between pings */
81-
uint16_t timeout; /* Deciseconds to wait response before timeout */
82-
FAR void *priv; /* Private context for callback */
83-
void (*callback)(FAR const struct ping_result_s *result);
84-
};
85-
86-
struct ping_result_s
87-
{
88-
int code; /* Notice code ICMP_I/E/W_XXX */
89-
long extra; /* Extra information for code */
90-
struct in_addr dest; /* Target address to ping */
91-
uint16_t nrequests; /* Number of ICMP ECHO requests sent */
92-
uint16_t nreplies; /* Number of matching ICMP ECHO replies received */
93-
uint16_t outsize; /* Bytes(include ICMP header) to be sent */
94-
uint16_t id; /* ICMP_ECHO id */
95-
uint16_t seqno; /* ICMP_ECHO seqno */
96-
FAR const struct ping_info_s *info;
97-
};
98-
9970
/****************************************************************************
10071
* Public Function Prototypes
10172
****************************************************************************/
10273

103-
#undef EXTERN
104-
#if defined(__cplusplus)
105-
#define EXTERN extern "C"
106-
extern "C"
107-
{
108-
#else
109-
#define EXTERN extern
110-
#endif
111-
112-
void icmp_ping(FAR const struct ping_info_s *info);
113-
114-
#undef EXTERN
115-
#ifdef __cplusplus
116-
}
117-
#endif
118-
11974
#endif /* __APPS_INCLUDE_NETUTILS_ICMP_PING_H */

include/netutils/icmp_pub.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/****************************************************************************
2+
* apps/include/netutils/icmp_pub.h
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
#ifndef __APPS_INCLUDE_NETUTILS_ICMP_PUB_H
24+
#define __APPS_INCLUDE_NETUTILS_ICMP_PUB_H
25+
26+
#define ICMP_E_HOSTIP -1 /* extra: not used */
27+
28+
struct ping_result_s;
29+
30+
struct ping_info_s
31+
{
32+
FAR const char *hostname; /* Host name to ping */
33+
#ifdef CONFIG_NET_BINDTODEVICE
34+
FAR const char *devname; /* Device name to bind */
35+
#endif
36+
uint16_t flag; /* v4 or v6 */
37+
uint16_t count; /* Number of pings requested */
38+
uint16_t datalen; /* Number of bytes to be sent */
39+
uint16_t delay; /* Deciseconds to delay between pings */
40+
uint16_t timeout; /* Deciseconds to wait response before timeout */
41+
FAR void *priv; /* Private context for callback */
42+
void (*callback)(FAR const struct ping_result_s *result);
43+
};
44+
45+
struct ping_result_s
46+
{
47+
int code; /* Notice code ICMPv6_I/E/W_XXX */
48+
long extra; /* Extra information for code */
49+
union
50+
{
51+
#ifdef CONFIG_SYSTEM_PING
52+
struct in_addr v4; /* Target address to ping */
53+
#endif
54+
#ifdef CONFIG_SYSTEM_PING6
55+
struct in6_addr v6; /* Target address to ping */
56+
#endif
57+
} dest;
58+
uint16_t nrequests; /* Number of ICMP ECHO requests sent */
59+
uint16_t nreplies; /* Number of matching ICMP ECHO replies received */
60+
uint16_t outsize; /* Bytes(include ICMP header) to be sent */
61+
uint16_t id; /* ICMPv6_ECHO id */
62+
uint16_t seqno; /* ICMPv6_ECHO seqno */
63+
FAR const struct ping_info_s *info;
64+
};
65+
66+
#undef EXTERN
67+
#if defined(__cplusplus)
68+
#define EXTERN extern "C"
69+
extern "C"
70+
{
71+
#else
72+
#define EXTERN extern
73+
#endif
74+
75+
void icmp_ping(FAR const struct ping_info_s *info);
76+
void icmp6_ping(FAR const struct ping_info_s *info);
77+
78+
#undef EXTERN
79+
#ifdef __cplusplus
80+
}
81+
#endif
82+
#endif /* __APPS_INCLUDE_NETUTILS_ICMP_PUB_H */

include/netutils/icmpv6_ping.h

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
****************************************************************************/
2929

3030
#include <netinet/in.h>
31+
#include "icmp_pub.h"
3132

3233
/****************************************************************************
3334
* Pre-processor Definitions
@@ -42,7 +43,6 @@
4243

4344
/* Negative odd number represent error(unrecoverable) */
4445

45-
#define ICMPv6_E_HOSTIP -1 /* extra: not used */
4646
#define ICMPv6_E_MEMORY -3 /* extra: not used */
4747
#define ICMPv6_E_SOCKET -5 /* extra: error code */
4848
#define ICMPv6_E_SENDTO -7 /* extra: error code */
@@ -66,53 +66,8 @@
6666
* Public Types
6767
****************************************************************************/
6868

69-
struct ping6_result_s;
70-
71-
struct ping6_info_s
72-
{
73-
FAR const char *hostname; /* Host name to ping */
74-
#ifdef CONFIG_NET_BINDTODEVICE
75-
FAR const char *devname; /* Device name to bind */
76-
#endif
77-
uint16_t count; /* Number of pings requested */
78-
uint16_t datalen; /* Number of bytes to be sent */
79-
uint16_t delay; /* Deciseconds to delay between pings */
80-
uint16_t timeout; /* Deciseconds to wait response before timeout */
81-
FAR void *priv; /* Private context for callback */
82-
void (*callback)(FAR const struct ping6_result_s *result);
83-
};
84-
85-
struct ping6_result_s
86-
{
87-
int code; /* Notice code ICMPv6_I/E/W_XXX */
88-
long extra; /* Extra information for code */
89-
struct in6_addr dest; /* Target address to ping */
90-
uint16_t nrequests; /* Number of ICMP ECHO requests sent */
91-
uint16_t nreplies; /* Number of matching ICMP ECHO replies received */
92-
uint16_t outsize; /* Bytes(include ICMP header) to be sent */
93-
uint16_t id; /* ICMPv6_ECHO id */
94-
uint16_t seqno; /* ICMPv6_ECHO seqno */
95-
FAR const struct ping6_info_s *info;
96-
};
97-
9869
/****************************************************************************
9970
* Public Function Prototypes
10071
****************************************************************************/
10172

102-
#undef EXTERN
103-
#if defined(__cplusplus)
104-
#define EXTERN extern "C"
105-
extern "C"
106-
{
107-
#else
108-
#define EXTERN extern
109-
#endif
110-
111-
void icmp6_ping(FAR const struct ping6_info_s *info);
112-
113-
#undef EXTERN
114-
#ifdef __cplusplus
115-
}
116-
#endif
117-
11873
#endif /* __APPS_INCLUDE_NETUTILS_ICMP_PING_H */

include/netutils/ping_pub.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/****************************************************************************
2+
* apps/include/netutils/ping_pub.h
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
#ifndef __APPS_INCLUDE_NETUTILS_PING_PUB_H
24+
#define __APPS_INCLUDE_NETUTILS_PING_PUB_H
25+
26+
struct ping_priv_s
27+
{
28+
int code; /* Notice code ICMP_I/E/W_XXX */
29+
long tmin; /* Minimum round trip time */
30+
long tmax; /* Maximum round trip time */
31+
long long tsum; /* Sum of all times, for doing average */
32+
long long tsum2; /* Sum2 is the sum of the squares of sum ,for doing mean deviation */
33+
};
34+
35+
#undef EXTERN
36+
#if defined(__cplusplus)
37+
#define EXTERN extern "C"
38+
extern "C"
39+
{
40+
#else
41+
#define EXTERN extern
42+
#endif
43+
44+
void ping_result(FAR const struct ping_result_s *result);
45+
void ping6_result(FAR const struct ping_result_s *result);
46+
47+
#undef EXTERN
48+
#ifdef __cplusplus
49+
}
50+
#endif
51+
52+
#endif /* __APPS_INCLUDE_NETUTILS_PING_PUB_H */

netutils/ping/icmp_ping.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void icmp_ping(FAR const struct ping_info_s *info)
242242
result.info = info;
243243
result.id = ping_newid();
244244
result.outsize = ICMP_IOBUFFER_SIZE(info->datalen);
245-
if (ping_gethostip(info->hostname, &result.dest) < 0)
245+
if (ping_gethostip(info->hostname, &result.dest.v4) < 0)
246246
{
247247
icmp_callback(&result, ICMP_E_HOSTIP, 0);
248248
return;
@@ -300,7 +300,7 @@ void icmp_ping(FAR const struct ping_info_s *info)
300300
memset(&priv->destaddr, 0, sizeof(struct sockaddr_in));
301301
priv->destaddr.sin_family = AF_INET;
302302
priv->destaddr.sin_port = 0;
303-
priv->destaddr.sin_addr.s_addr = result.dest.s_addr;
303+
priv->destaddr.sin_addr.s_addr = result.dest.v4.s_addr;
304304

305305
icmp_callback(&result, ICMP_I_BEGIN, 0);
306306

netutils/ping/icmpv6_ping.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static int ping6_gethostip(FAR const char *hostname,
149149
* Name: icmp6_callback
150150
****************************************************************************/
151151

152-
static void icmp6_callback(FAR struct ping6_result_s *result,
152+
static void icmp6_callback(FAR struct ping_result_s *result,
153153
int code, long extra)
154154
{
155155
result->code = code;
@@ -165,9 +165,9 @@ static void icmp6_callback(FAR struct ping6_result_s *result,
165165
* Name: icmp6_ping
166166
****************************************************************************/
167167

168-
void icmp6_ping(FAR const struct ping6_info_s *info)
168+
void icmp6_ping(FAR const struct ping_info_s *info)
169169
{
170-
struct ping6_result_s result;
170+
struct ping_result_s result;
171171
struct sockaddr_in6 destaddr;
172172
struct sockaddr_in6 fromaddr;
173173
struct icmp6_filter filter;
@@ -197,9 +197,9 @@ void icmp6_ping(FAR const struct ping6_info_s *info)
197197
result.info = info;
198198
result.id = ping6_newid();
199199
result.outsize = ICMPv6_IOBUFFER_SIZE(info->datalen);
200-
if (ping6_gethostip(info->hostname, &result.dest) < 0)
200+
if (ping6_gethostip(info->hostname, &result.dest.v6) < 0)
201201
{
202-
icmp6_callback(&result, ICMPv6_E_HOSTIP, 0);
202+
icmp6_callback(&result, ICMP_E_HOSTIP, 0);
203203
return;
204204
}
205205

0 commit comments

Comments
 (0)