1 //==========================================================================
5 // Network utility - ping
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 // Copyright (C) 2003 Gary Thomas
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
45 // Contributors: gthomas
50 // This code is part of RedBoot (tm).
52 //####DESCRIPTIONEND####
54 //==========================================================================
59 #ifndef CYGPKG_REDBOOT_NETWORKING
60 #error CYGPKG_REDBOOT_NETWORKING required!
63 static void do_ping(int argc, char *argv[]);
65 "Network connectivity test",
66 "[-v] [-n <count>] [-l <length>] [-t <timeout>] [-r <rate>]\n"
67 " [-i <IP_addr>] -h <IP_addr>",
71 static bool icmp_received;
72 static icmp_header_t hold_hdr;
75 handle_icmp(pktbuf_t *pkt, ip_route_t *src_route)
81 if (icmp->type == ICMP_TYPE_ECHOREQUEST
83 && __sum((word *)icmp, pkt->pkt_bytes, 0) == 0) {
85 icmp->type = ICMP_TYPE_ECHOREPLY;
87 cksum = __sum((word *)icmp, pkt->pkt_bytes, 0);
88 icmp->checksum = htons(cksum);
89 __ip_send(pkt, IP_PROTO_ICMP, src_route);
90 } else if (icmp->type == ICMP_TYPE_ECHOREPLY) {
91 memcpy(&hold_hdr, icmp, sizeof(*icmp));
96 #include <cyg/hal/hal_soc.h>
98 do_ping(int argc, char *argv[])
100 struct option_info opts[7];
101 long count, timeout, length, rate, start_time, end_time, timer, received, tries;
102 char *local_ip_addr, *host_ip_addr;
103 bool local_ip_addr_set, host_ip_addr_set, count_set,
104 timeout_set, length_set, rate_set, verbose;
105 struct sockaddr_in local_addr, host_addr;
110 unsigned short cksum;
113 init_opts(&opts[0], 'n', true, OPTION_ARG_TYPE_NUM,
114 &count, &count_set, "<count> - number of packets to test");
115 init_opts(&opts[1], 't', true, OPTION_ARG_TYPE_NUM,
116 &timeout, &timeout_set, "<timeout> - max #ms per packet [rount trip]");
117 init_opts(&opts[2], 'i', true, OPTION_ARG_TYPE_STR,
118 &local_ip_addr, &local_ip_addr_set, "local IP address");
119 init_opts(&opts[3], 'h', true, OPTION_ARG_TYPE_STR,
120 &host_ip_addr, &host_ip_addr_set, "host name or IP address");
121 init_opts(&opts[4], 'l', true, OPTION_ARG_TYPE_NUM,
122 &length, &length_set, "<length> - size of payload");
123 init_opts(&opts[5], 'v', false, OPTION_ARG_TYPE_FLG,
124 &verbose, NULL, "verbose operation");
125 init_opts(&opts[6], 'r', true, OPTION_ARG_TYPE_NUM,
126 &rate, &rate_set, "<rate> - time between packets");
127 if (!scan_opts(argc, argv, 1, opts, 7, NULL, 0, "")) {
128 diag_printf("PING - Invalid option specified\n");
131 // Set defaults; this has to be done _after_ the scan, since it will
132 // have destroyed all values not explicitly set.
133 if (local_ip_addr_set) {
134 if (!_gethostbyname(local_ip_addr, (in_addr_t *)&local_addr)) {
135 diag_printf("PING - Invalid local name: %s\n", local_ip_addr);
139 memcpy((in_addr_t *)&local_addr, __local_ip_addr, sizeof(__local_ip_addr));
141 if (host_ip_addr_set) {
142 if (!_gethostbyname(host_ip_addr, (in_addr_t *)&host_addr)) {
143 diag_printf("PING - Invalid host name: %s\n", host_ip_addr);
146 if (__arp_lookup((ip_addr_t *)&host_addr.sin_addr, &dest_ip) < 0) {
147 diag_printf("PING: Cannot reach server '%s' (%s)\n",
148 host_ip_addr, inet_ntoa((in_addr_t *)&host_addr));
152 diag_printf("PING - host name or IP address required\n");
155 #define DEFAULT_LENGTH 64
156 #define DEFAULT_COUNT 10
157 #define DEFAULT_TIMEOUT 1000
158 #define DEFAULT_RATE 1000
163 length = DEFAULT_LENGTH;
165 if ((length < 64) || (length > 1400)) {
166 diag_printf("Invalid length specified: %ld\n", length);
170 count = DEFAULT_COUNT;
173 timeout = DEFAULT_TIMEOUT;
175 // Note: two prints here because 'inet_ntoa' returns a static pointer
176 diag_printf("Network PING - from %s",
177 inet_ntoa((in_addr_t *)&local_addr));
178 diag_printf(" to %s\n",
179 inet_ntoa((in_addr_t *)&host_addr));
181 __icmp_install_listener(handle_icmp);
182 // Save default "local" address
183 memcpy(hold_addr, __local_ip_addr, sizeof(hold_addr));
184 for (tries = 0; tries < count; tries++) {
185 // The network stack uses the global variable '__local_ip_addr'
186 memcpy(__local_ip_addr, &local_addr, sizeof(__local_ip_addr));
187 // Build 'ping' request
188 if ((pkt = __pktbuf_alloc(ETH_MAX_PKTLEN)) == NULL) {
189 // Give up if no packets - something is wrong
193 icmp = pkt->icmp_hdr;
195 pkt->pkt_bytes = length + sizeof(icmp_header_t);
197 icmp->type = ICMP_TYPE_ECHOREQUEST;
200 icmp->seqnum = htons(tries+1);
201 cksum = __sum((word *)icmp, pkt->pkt_bytes, 0);
202 icmp->checksum = htons(cksum);
204 memcpy(ip->source, (in_addr_t *)&local_addr, sizeof(ip_addr_t));
205 memcpy(ip->destination, (in_addr_t *)&host_addr, sizeof(ip_addr_t));
206 ip->protocol = IP_PROTO_ICMP;
207 ip->length = htons(pkt->pkt_bytes);
209 __ip_send(pkt, IP_PROTO_ICMP, &dest_ip);
212 start_time = MS_TICKS();
213 timer = start_time + timeout;
214 icmp_received = false;
215 while (!icmp_received && (MS_TICKS_DELAY() < timer)) {
219 timer--; /* account for time spent in _rb_break() */
222 end_time = MS_TICKS();
224 timer = MS_TICKS() + rate;
225 while (MS_TICKS_DELAY() < timer) {
229 timer--; /* account for time spent in _rb_break() */
236 diag_printf(" seq: %d, time: %ld (ticks)\n",
237 ntohs(hold_hdr.seqnum), end_time - start_time);
242 __icmp_remove_listener();
244 memcpy(__local_ip_addr, &hold_addr, sizeof(__local_ip_addr));
246 diag_printf("PING - received %ld of %ld expected\n", received, count);
249 #endif //CYGPKG_REDBOOT_NETWORKING