]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/redboot/v2_0/src/net/ping.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / redboot / v2_0 / src / net / ping.c
1 //==========================================================================
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 // Copyright (C) 2003 Gary Thomas
13 //
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.
17 //
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
21 // for more details.
22 //
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.
26 //
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.
33 //
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.
36 //
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####
43 //
44 // Author(s):    gthomas
45 // Contributors: gthomas
46 // Date:         2001-01-22
47 // Purpose:
48 // Description:
49 //
50 // This code is part of RedBoot (tm).
51 //
52 //####DESCRIPTIONEND####
53 //
54 //==========================================================================
55
56 #include <redboot.h>
57 #include <net/net.h>
58
59 #ifndef CYGPKG_REDBOOT_NETWORKING
60 #error CYGPKG_REDBOOT_NETWORKING required!
61 #else
62
63 static void do_ping(int argc, char *argv[]);
64 RedBoot_cmd("ping",
65             "Network connectivity test",
66             "[-v] [-n <count>] [-l <length>] [-t <timeout>] [-r <rate>]\n"
67             "        [-i <IP_addr>] -h <IP_addr>",
68             do_ping
69     );
70
71 static bool icmp_received;
72 static icmp_header_t hold_hdr;
73
74 static void
75 handle_icmp(pktbuf_t *pkt, ip_route_t *src_route)
76 {
77     icmp_header_t *icmp;
78     unsigned short cksum;
79
80     icmp = pkt->icmp_hdr;
81     if (icmp->type == ICMP_TYPE_ECHOREQUEST
82         && icmp->code == 0
83         && __sum((word *)icmp, pkt->pkt_bytes, 0) == 0) {
84
85         icmp->type = ICMP_TYPE_ECHOREPLY;
86         icmp->checksum = 0;
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));
92         icmp_received = true;
93     }
94 }
95
96 #include <cyg/hal/hal_soc.h>
97 static void
98 do_ping(int argc, char *argv[])
99 {
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;
106     ip_addr_t hold_addr;
107     icmp_header_t *icmp;
108     pktbuf_t *pkt;
109     ip_header_t *ip;
110     unsigned short cksum;
111     ip_route_t dest_ip;
112
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");
129         return;
130     }
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);
136             return;
137         }
138     } else {
139         memcpy((in_addr_t *)&local_addr, __local_ip_addr, sizeof(__local_ip_addr));
140     }
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);
144             return;
145         }
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));
149             return;
150         }
151     } else {
152         diag_printf("PING - host name or IP address required\n");
153         return;
154     }
155 #define DEFAULT_LENGTH   64
156 #define DEFAULT_COUNT    10
157 #define DEFAULT_TIMEOUT  1000
158 #define DEFAULT_RATE     1000
159     if (!rate_set) {
160         rate = DEFAULT_RATE;
161     }
162     if (!length_set) {
163         length = DEFAULT_LENGTH;
164     }
165     if ((length < 64) || (length > 1400)) {
166         diag_printf("Invalid length specified: %ld\n", length);
167         return;
168     }
169     if (!count_set) {
170         count = DEFAULT_COUNT;
171     }
172     if (!timeout_set) {
173         timeout = DEFAULT_TIMEOUT;
174     }
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));
180     received = 0;
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
190             break;
191         }
192
193         icmp = pkt->icmp_hdr;
194         ip = pkt->ip_hdr;
195         pkt->pkt_bytes = length + sizeof(icmp_header_t);
196
197         icmp->type = ICMP_TYPE_ECHOREQUEST;
198         icmp->code = 0;
199         icmp->checksum = 0;
200         icmp->seqnum = htons(tries+1);
201         cksum = __sum((word *)icmp, pkt->pkt_bytes, 0);
202         icmp->checksum = htons(cksum);
203
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);
208
209         __ip_send(pkt, IP_PROTO_ICMP, &dest_ip);
210         __pktbuf_free(pkt);
211
212         start_time = MS_TICKS();
213         timer = start_time + timeout;
214         icmp_received = false;
215         while (!icmp_received && (MS_TICKS_DELAY() < timer)) {
216             if (_rb_break(1)) {
217                 goto abort;
218             }
219             timer--; /* account for time spent in _rb_break() */
220             __enet_poll();
221         }
222         end_time = MS_TICKS();
223
224         timer = MS_TICKS() + rate;
225         while (MS_TICKS_DELAY() < timer) {
226             if (_rb_break(1)) {
227                 goto abort;
228             }
229             timer--; /* account for time spent in _rb_break() */
230             __enet_poll();
231         }
232
233         if (icmp_received) {
234             received++;
235             if (verbose) {
236                 diag_printf(" seq: %d, time: %ld (ticks)\n",
237                             ntohs(hold_hdr.seqnum), end_time - start_time);
238             }
239         }
240     }
241  abort:
242     __icmp_remove_listener();
243     // Clean up
244     memcpy(__local_ip_addr, &hold_addr, sizeof(__local_ip_addr));
245     // Report
246     diag_printf("PING - received %ld of %ld expected\n", received, count);
247 }
248
249 #endif //CYGPKG_REDBOOT_NETWORKING