OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [redboot/] [current/] [src/] [net/] [ping.c] - Blame information for rev 856

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
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, 2003 Free Software Foundation, 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      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY 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        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    gthomas
43
// Contributors: gthomas
44
// Date:         2001-01-22
45
// Purpose:      
46
// Description:  
47
//              
48
// This code is part of RedBoot (tm).
49
//
50
//####DESCRIPTIONEND####
51
//
52
//==========================================================================
53
 
54
#include <redboot.h>
55
#include <net/net.h>
56
 
57
#ifndef CYGPKG_REDBOOT_NETWORKING
58
#error CYGPKG_REDBOOT_NETWORKING required!
59
#else
60
 
61
static void do_ping(int argc, char *argv[]);
62
RedBoot_cmd("ping",
63
            "Network connectivity test",
64
            "[-v] [-n <count>] [-l <length>] [-t <timeout>] [-r <rate>]\n"
65
            "        [-i <IP_addr>] -h <IP_addr>",
66
            do_ping
67
    );
68
 
69
static bool icmp_received;
70
static icmp_header_t hold_hdr;
71
 
72
static void
73
handle_icmp(pktbuf_t *pkt, ip_route_t *src_route)
74
{
75
    icmp_header_t *icmp;
76
    unsigned short cksum;
77
 
78
    icmp = pkt->icmp_hdr;
79
    if (icmp->type == ICMP_TYPE_ECHOREQUEST
80
        && icmp->code == 0
81
        && __sum((word *)icmp, pkt->pkt_bytes, 0) == 0) {
82
 
83
        icmp->type = ICMP_TYPE_ECHOREPLY;
84
        icmp->checksum = 0;
85
        cksum = __sum((word *)icmp, pkt->pkt_bytes, 0);
86
        icmp->checksum = htons(cksum);
87
        __ip_send(pkt, IP_PROTO_ICMP, src_route);
88
    } else if (icmp->type == ICMP_TYPE_ECHOREPLY) {
89
        memcpy(&hold_hdr, icmp, sizeof(*icmp));
90
        icmp_received = true;
91
    }
92
}
93
 
94
static void
95
do_ping(int argc, char *argv[])
96
{
97
    struct option_info opts[7];
98
    long count, timeout, length, rate, start_time, end_time, timer, received, tries;
99
    char *local_ip_addr, *host_ip_addr;
100
    bool local_ip_addr_set, host_ip_addr_set, count_set,
101
        timeout_set, length_set, rate_set, verbose;
102
    struct sockaddr_in local_addr, host_addr;
103
    ip_addr_t hold_addr;
104
    icmp_header_t *icmp;
105
    pktbuf_t *pkt;
106
    ip_header_t *ip;
107
    unsigned short cksum;
108
    ip_route_t dest_ip;
109
 
110
    init_opts(&opts[0], 'n', true, OPTION_ARG_TYPE_NUM,
111
              (void *)&count, (bool *)&count_set, "<count> - number of packets to test");
112
    init_opts(&opts[1], 't', true, OPTION_ARG_TYPE_NUM,
113
              (void *)&timeout, (bool *)&timeout_set, "<timeout> - max #ms per packet [rount trip]");
114
    init_opts(&opts[2], 'i', true, OPTION_ARG_TYPE_STR,
115
              (void *)&local_ip_addr, (bool *)&local_ip_addr_set, "local IP address");
116
    init_opts(&opts[3], 'h', true, OPTION_ARG_TYPE_STR,
117
              (void *)&host_ip_addr, (bool *)&host_ip_addr_set, "host name or IP address");
118
    init_opts(&opts[4], 'l', true, OPTION_ARG_TYPE_NUM,
119
              (void *)&length, (bool *)&length_set, "<length> - size of payload");
120
    init_opts(&opts[5], 'v', false, OPTION_ARG_TYPE_FLG,
121
              (void *)&verbose, (bool *)0, "verbose operation");
122
    init_opts(&opts[6], 'r', true, OPTION_ARG_TYPE_NUM,
123
              (void *)&rate, (bool *)&rate_set, "<rate> - time between packets");
124
    if (!scan_opts(argc, argv, 1, opts, 7, (void **)0, 0, "")) {
125
        diag_printf("PING - Invalid option specified\n");
126
        return;
127
    }
128
    // Set defaults; this has to be done _after_ the scan, since it will
129
    // have destroyed all values not explicitly set.
130
    if (local_ip_addr_set) {
131
        if (!_gethostbyname(local_ip_addr, (in_addr_t *)&local_addr)) {
132
            diag_printf("PING - Invalid local name: %s\n", local_ip_addr);
133
            return;
134
        }
135
    } else {
136
        memcpy((in_addr_t *)&local_addr, __local_ip_addr, sizeof(__local_ip_addr));
137
    }
138
    if (host_ip_addr_set) {
139
        if (!_gethostbyname(host_ip_addr, (in_addr_t *)&host_addr)) {
140
            diag_printf("PING - Invalid host name: %s\n", host_ip_addr);
141
            return;
142
        }
143
        if (__arp_lookup((ip_addr_t *)&host_addr.sin_addr, &dest_ip) < 0) {
144
            diag_printf("PING: Cannot reach server '%s' (%s)\n",
145
                        host_ip_addr, inet_ntoa((in_addr_t *)&host_addr));
146
            return;
147
        }
148
    } else {
149
        diag_printf("PING - host name or IP address required\n");
150
        return;
151
    }
152
#define DEFAULT_LENGTH   64
153
#define DEFAULT_COUNT    10
154
#define DEFAULT_TIMEOUT  1000
155
#define DEFAULT_RATE     1000
156
    if (!rate_set) {
157
        rate = DEFAULT_RATE;
158
    }
159
    if (!length_set) {
160
        length = DEFAULT_LENGTH;
161
    }
162
    if ((length < 64) || (length > 1400)) {
163
        diag_printf("Invalid length specified: %ld\n", length);
164
        return;
165
    }
166
    if (!count_set) {
167
        count = DEFAULT_COUNT;
168
    }
169
    if (!timeout_set) {
170
        timeout = DEFAULT_TIMEOUT;
171
    }
172
    // Note: two prints here because 'inet_ntoa' returns a static pointer
173
    diag_printf("Network PING - from %s",
174
                inet_ntoa((in_addr_t *)&local_addr));
175
    diag_printf(" to %s\n",
176
                inet_ntoa((in_addr_t *)&host_addr));
177
    received = 0;
178
    __icmp_install_listener(handle_icmp);
179
    // Save default "local" address
180
    memcpy(hold_addr, __local_ip_addr, sizeof(hold_addr));
181
    for (tries = 0;  tries < count;  tries++) {
182
        // The network stack uses the global variable '__local_ip_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
            if (_rb_break(1)) {
214
                goto abort;
215
            }
216
            MS_TICKS_DELAY();
217
            __enet_poll();
218
        }
219
        end_time = MS_TICKS();
220
 
221
        timer = MS_TICKS() + rate;
222
        while (MS_TICKS_DELAY() < timer) {
223
            if (_rb_break(1)) {
224
                goto abort;
225
            }
226
            MS_TICKS_DELAY();
227
            __enet_poll();
228
        }
229
 
230
        if (icmp_received) {
231
            received++;
232
            if (verbose) {
233
                diag_printf(" seq: %d, time: %ld (ticks)\n",
234
                            ntohs(hold_hdr.seqnum), end_time-start_time);
235
            }
236
        }
237
    }
238
 abort:
239
    __icmp_remove_listener();
240
    // Clean up
241
    memcpy(__local_ip_addr, &hold_addr, sizeof(__local_ip_addr));
242
    // Report
243
    diag_printf("PING - received %ld of %ld expected\n", received, count);
244
}
245
 
246
#endif //CYGPKG_REDBOOT_NETWORKING

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.