1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// ping.c
|
4 |
|
|
//
|
5 |
|
|
// Network utility - ping
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//==========================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): gthomas
|
44 |
|
|
// Contributors: gthomas
|
45 |
|
|
// Date: 2001-01-22
|
46 |
|
|
// Purpose:
|
47 |
|
|
// Description:
|
48 |
|
|
//
|
49 |
|
|
// This code is part of RedBoot (tm).
|
50 |
|
|
//
|
51 |
|
|
//####DESCRIPTIONEND####
|
52 |
|
|
//
|
53 |
|
|
//==========================================================================
|
54 |
|
|
|
55 |
|
|
#include <redboot.h>
|
56 |
|
|
#include <net/net.h>
|
57 |
|
|
|
58 |
|
|
#ifndef CYGPKG_REDBOOT_NETWORKING
|
59 |
|
|
#error CYGPKG_REDBOOT_NETWORKING required!
|
60 |
|
|
#else
|
61 |
|
|
|
62 |
|
|
static void do_ping(int argc, char *argv[]);
|
63 |
|
|
RedBoot_cmd("ping",
|
64 |
|
|
"Network connectivity test",
|
65 |
|
|
"[-v] [-n <count>] [-l <length>] [-t <timeout>] [-r <rate>]\n"
|
66 |
|
|
" [-i <IP_addr>] -h <IP_addr>",
|
67 |
|
|
do_ping
|
68 |
|
|
);
|
69 |
|
|
|
70 |
|
|
static bool icmp_received;
|
71 |
|
|
static icmp_header_t hold_hdr;
|
72 |
|
|
|
73 |
|
|
static void
|
74 |
|
|
handle_icmp(pktbuf_t *pkt, ip_route_t *src_route)
|
75 |
|
|
{
|
76 |
|
|
icmp_header_t *icmp;
|
77 |
|
|
unsigned short cksum;
|
78 |
|
|
|
79 |
|
|
icmp = pkt->icmp_hdr;
|
80 |
|
|
if (icmp->type == ICMP_TYPE_ECHOREQUEST
|
81 |
|
|
&& icmp->code == 0
|
82 |
|
|
&& __sum((word *)icmp, pkt->pkt_bytes, 0) == 0) {
|
83 |
|
|
|
84 |
|
|
icmp->type = ICMP_TYPE_ECHOREPLY;
|
85 |
|
|
icmp->checksum = 0;
|
86 |
|
|
cksum = __sum((word *)icmp, pkt->pkt_bytes, 0);
|
87 |
|
|
icmp->checksum = htons(cksum);
|
88 |
|
|
__ip_send(pkt, IP_PROTO_ICMP, src_route);
|
89 |
|
|
} else if (icmp->type == ICMP_TYPE_ECHOREPLY) {
|
90 |
|
|
memcpy(&hold_hdr, icmp, sizeof(*icmp));
|
91 |
|
|
icmp_received = true;
|
92 |
|
|
}
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
static void
|
96 |
|
|
do_ping(int argc, char *argv[])
|
97 |
|
|
{
|
98 |
|
|
struct option_info opts[7];
|
99 |
|
|
long count, timeout, length, rate, start_time, end_time, timer, received, tries;
|
100 |
|
|
char *local_ip_addr, *host_ip_addr;
|
101 |
|
|
bool local_ip_addr_set, host_ip_addr_set, count_set,
|
102 |
|
|
timeout_set, length_set, rate_set, verbose;
|
103 |
|
|
struct sockaddr_in local_addr, host_addr;
|
104 |
|
|
ip_addr_t hold_addr;
|
105 |
|
|
icmp_header_t *icmp;
|
106 |
|
|
pktbuf_t *pkt;
|
107 |
|
|
ip_header_t *ip;
|
108 |
|
|
unsigned short cksum;
|
109 |
|
|
ip_route_t dest_ip;
|
110 |
|
|
|
111 |
|
|
init_opts(&opts[0], 'n', true, OPTION_ARG_TYPE_NUM,
|
112 |
|
|
(void **)&count, (bool *)&count_set, "<count> - number of packets to test");
|
113 |
|
|
init_opts(&opts[1], 't', true, OPTION_ARG_TYPE_NUM,
|
114 |
|
|
(void **)&timeout, (bool *)&timeout_set, "<timeout> - max #ms per packet [rount trip]");
|
115 |
|
|
init_opts(&opts[2], 'i', true, OPTION_ARG_TYPE_STR,
|
116 |
|
|
(void **)&local_ip_addr, (bool *)&local_ip_addr_set, "local IP address");
|
117 |
|
|
init_opts(&opts[3], 'h', true, OPTION_ARG_TYPE_STR,
|
118 |
|
|
(void **)&host_ip_addr, (bool *)&host_ip_addr_set, "host name or IP address");
|
119 |
|
|
init_opts(&opts[4], 'l', true, OPTION_ARG_TYPE_NUM,
|
120 |
|
|
(void **)&length, (bool *)&length_set, "<length> - size of payload");
|
121 |
|
|
init_opts(&opts[5], 'v', false, OPTION_ARG_TYPE_FLG,
|
122 |
|
|
(void **)&verbose, (bool *)0, "verbose operation");
|
123 |
|
|
init_opts(&opts[6], 'r', true, OPTION_ARG_TYPE_NUM,
|
124 |
|
|
(void **)&rate, (bool *)&rate_set, "<rate> - time between packets");
|
125 |
|
|
if (!scan_opts(argc, argv, 1, opts, 7, (void **)0, 0, "")) {
|
126 |
|
|
diag_printf("PING - Invalid option specified\n");
|
127 |
|
|
return;
|
128 |
|
|
}
|
129 |
|
|
// Set defaults; this has to be done _after_ the scan, since it will
|
130 |
|
|
// have destroyed all values not explicitly set.
|
131 |
|
|
if (local_ip_addr_set) {
|
132 |
|
|
if (!_gethostbyname(local_ip_addr, (in_addr_t *)&local_addr)) {
|
133 |
|
|
diag_printf("PING - Invalid local name: %s\n", local_ip_addr);
|
134 |
|
|
return;
|
135 |
|
|
}
|
136 |
|
|
} else {
|
137 |
|
|
memcpy((in_addr_t *)&local_addr, __local_ip_addr, sizeof(__local_ip_addr));
|
138 |
|
|
}
|
139 |
|
|
if (host_ip_addr_set) {
|
140 |
|
|
if (!_gethostbyname(host_ip_addr, (in_addr_t *)&host_addr)) {
|
141 |
|
|
diag_printf("PING - Invalid host name: %s\n", host_ip_addr);
|
142 |
|
|
return;
|
143 |
|
|
}
|
144 |
|
|
if (__arp_lookup((ip_addr_t *)&host_addr.sin_addr, &dest_ip) < 0) {
|
145 |
|
|
diag_printf("PING: Cannot reach server '%s' (%s)\n",
|
146 |
|
|
host_ip_addr, inet_ntoa((in_addr_t *)&host_addr));
|
147 |
|
|
return;
|
148 |
|
|
}
|
149 |
|
|
} else {
|
150 |
|
|
diag_printf("PING - host name or IP address required\n");
|
151 |
|
|
return;
|
152 |
|
|
}
|
153 |
|
|
#define DEFAULT_LENGTH 64
|
154 |
|
|
#define DEFAULT_COUNT 10
|
155 |
|
|
#define DEFAULT_TIMEOUT 1000
|
156 |
|
|
#define DEFAULT_RATE 1000
|
157 |
|
|
if (!rate_set) {
|
158 |
|
|
rate = DEFAULT_RATE;
|
159 |
|
|
}
|
160 |
|
|
if (!length_set) {
|
161 |
|
|
length = DEFAULT_LENGTH;
|
162 |
|
|
}
|
163 |
|
|
if ((length < 64) || (length > 1400)) {
|
164 |
|
|
diag_printf("Invalid length specified: %ld\n", length);
|
165 |
|
|
return;
|
166 |
|
|
}
|
167 |
|
|
if (!count_set) {
|
168 |
|
|
count = DEFAULT_COUNT;
|
169 |
|
|
}
|
170 |
|
|
if (!timeout_set) {
|
171 |
|
|
timeout = DEFAULT_TIMEOUT;
|
172 |
|
|
}
|
173 |
|
|
// Note: two prints here because 'inet_ntoa' returns a static pointer
|
174 |
|
|
diag_printf("Network PING - from %s",
|
175 |
|
|
inet_ntoa((in_addr_t *)&local_addr));
|
176 |
|
|
diag_printf(" to %s\n",
|
177 |
|
|
inet_ntoa((in_addr_t *)&host_addr));
|
178 |
|
|
received = 0;
|
179 |
|
|
__icmp_install_listener(handle_icmp);
|
180 |
|
|
for (tries = 0; tries < count; tries++) {
|
181 |
|
|
// The network stack uses the global variable '__local_ip_addr'
|
182 |
|
|
memcpy(hold_addr, __local_ip_addr, sizeof(hold_addr));
|
183 |
|
|
memcpy(__local_ip_addr, &local_addr, sizeof(__local_ip_addr));
|
184 |
|
|
// Build 'ping' request
|
185 |
|
|
if ((pkt = __pktbuf_alloc(ETH_MAX_PKTLEN)) == NULL) {
|
186 |
|
|
// Give up if no packets - something is wrong
|
187 |
|
|
break;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
icmp = pkt->icmp_hdr;
|
191 |
|
|
ip = pkt->ip_hdr;
|
192 |
|
|
pkt->pkt_bytes = length + sizeof(icmp_header_t);
|
193 |
|
|
|
194 |
|
|
icmp->type = ICMP_TYPE_ECHOREQUEST;
|
195 |
|
|
icmp->code = 0;
|
196 |
|
|
icmp->checksum = 0;
|
197 |
|
|
icmp->seqnum = htons(tries+1);
|
198 |
|
|
cksum = __sum((word *)icmp, pkt->pkt_bytes, 0);
|
199 |
|
|
icmp->checksum = htons(cksum);
|
200 |
|
|
|
201 |
|
|
memcpy(ip->source, (in_addr_t *)&local_addr, sizeof(ip_addr_t));
|
202 |
|
|
memcpy(ip->destination, (in_addr_t *)&host_addr, sizeof(ip_addr_t));
|
203 |
|
|
ip->protocol = IP_PROTO_ICMP;
|
204 |
|
|
ip->length = htons(pkt->pkt_bytes);
|
205 |
|
|
|
206 |
|
|
__ip_send(pkt, IP_PROTO_ICMP, &dest_ip);
|
207 |
|
|
__pktbuf_free(pkt);
|
208 |
|
|
|
209 |
|
|
start_time = MS_TICKS();
|
210 |
|
|
timer = start_time + timeout;
|
211 |
|
|
icmp_received = false;
|
212 |
|
|
while (!icmp_received && (MS_TICKS_DELAY() < timer)) {
|
213 |
|
|
__enet_poll();
|
214 |
|
|
}
|
215 |
|
|
end_time = MS_TICKS();
|
216 |
|
|
|
217 |
|
|
timer = MS_TICKS() + rate;
|
218 |
|
|
while (MS_TICKS_DELAY() < timer) {
|
219 |
|
|
__enet_poll();
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
// Clean up
|
223 |
|
|
memcpy(__local_ip_addr, &hold_addr, sizeof(__local_ip_addr));
|
224 |
|
|
|
225 |
|
|
if (icmp_received) {
|
226 |
|
|
received++;
|
227 |
|
|
if (verbose) {
|
228 |
|
|
diag_printf(" seq: %ld, time: %ld (ticks)\n",
|
229 |
|
|
ntohs(hold_hdr.seqnum), end_time-start_time);
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
}
|
233 |
|
|
__icmp_remove_listener();
|
234 |
|
|
// Report
|
235 |
|
|
diag_printf("PING - received %ld of %ld expected\n", received, count);
|
236 |
|
|
}
|
237 |
|
|
|
238 |
|
|
#endif //CYGPKG_REDBOOT_NETWORKING
|