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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [tests/] [ping_lo_test.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      tests/ping_lo_test.c
4
//
5
//      Simple test of PING (ICMP) and networking support
6
//
7
//==========================================================================
8
// ####BSDALTCOPYRIGHTBEGIN####                                             
9
// -------------------------------------------                              
10
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
11
// or other sources, and if so are covered by the appropriate copyright     
12
// and license included herein.                                             
13
// -------------------------------------------                              
14
// ####BSDALTCOPYRIGHTEND####                                               
15
//==========================================================================
16
//#####DESCRIPTIONBEGIN####
17
//
18
// Author(s):    gthomas, sorin@netappi.com
19
// Contributors: gthomas, sorin@netappi.com, andrew.lunn@ascom.ch
20
// Date:         2000-01-10
21
// Purpose:      
22
// Description:  
23
//              
24
//
25
//####DESCRIPTIONEND####
26
//
27
//==========================================================================
28
 
29
// PING test code
30
 
31
#include <network.h>
32
#ifdef CYGPKG_NET_INET6
33
#include <netinet/ip6.h>
34
#include <netinet/icmp6.h>
35
#endif
36
 
37
#include <cyg/infra/testcase.h>
38
 
39
#ifndef CYGPKG_LIBC_STDIO
40
#define perror(s) diag_printf(#s ": %s\n", strerror(errno))
41
#endif
42
 
43
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
44
static char stack[STACK_SIZE];
45
static cyg_thread thread_data;
46
static cyg_handle_t thread_handle;
47
 
48
#define NUM_PINGS 16
49
#define MAX_PACKET 4096
50
static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];
51
 
52
#define UNIQUEID 0x1234
53
 
54
void
55
pexit(char *s)
56
{
57
    CYG_TEST_FAIL_FINISH( s );
58
}
59
 
60
// Compute INET checksum
61
int
62
inet_cksum(u_short *addr, int len)
63
{
64
    register int nleft = len;
65
    register u_short *w = addr;
66
    register u_short answer;
67
    register u_int sum = 0;
68
    u_short odd_byte = 0;
69
 
70
    /*
71
     *  Our algorithm is simple, using a 32 bit accumulator (sum),
72
     *  we add sequential 16 bit words to it, and at the end, fold
73
     *  back all the carry bits from the top 16 bits into the lower
74
     *  16 bits.
75
     */
76
    while( nleft > 1 )  {
77
        sum += *w++;
78
        nleft -= 2;
79
    }
80
 
81
    /* mop up an odd byte, if necessary */
82
    if( nleft == 1 ) {
83
        *(u_char *)(&odd_byte) = *(u_char *)w;
84
        sum += odd_byte;
85
    }
86
 
87
    /*
88
     * add back carry outs from top 16 bits to low 16 bits
89
     */
90
    sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
91
    sum += (sum >> 16);                     /* add carry */
92
    answer = ~sum;                          /* truncate to 16 bits */
93
    return (answer);
94
}
95
 
96
static int
97
show_icmp(unsigned char *pkt, int len,
98
          struct sockaddr_in *from, struct sockaddr_in *to)
99
{
100
    cyg_tick_count_t *tp, tv;
101
    struct ip *ip;
102
    struct icmp *icmp;
103
    tv = cyg_current_time();
104
    ip = (struct ip *)pkt;
105
    if ((len < sizeof(*ip)) || ip->ip_v != IPVERSION) {
106
        diag_printf("%s: Short packet or not IP! - Len: %d, Version: %d\n",
107
                    inet_ntoa(from->sin_addr), len, ip->ip_v);
108
        return 0;
109
    }
110
    icmp = (struct icmp *)(pkt + sizeof(*ip));
111
    len -= (sizeof(*ip) + 8);
112
    tp = (cyg_tick_count_t *)&icmp->icmp_data;
113
    if (icmp->icmp_type != ICMP_ECHOREPLY) {
114
        diag_printf("%s: Invalid ICMP - type: %d\n",
115
                    inet_ntoa(from->sin_addr), icmp->icmp_type);
116
        return 0;
117
    }
118
    if (icmp->icmp_id != UNIQUEID) {
119
        diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
120
                    inet_ntoa(from->sin_addr), UNIQUEID, icmp->icmp_id);
121
    }
122
    diag_printf("%d bytes from %s: ", len, inet_ntoa(from->sin_addr));
123
    diag_printf("icmp_seq=%d", icmp->icmp_seq);
124
    diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
125
    return (from->sin_addr.s_addr == to->sin_addr.s_addr);
126
}
127
 
128
static void
129
ping_host(int s, struct sockaddr_in *host)
130
{
131
    struct icmp *icmp = (struct icmp *)pkt1;
132
    int icmp_len = 64;
133
    int seq, ok_recv, bogus_recv;
134
    cyg_tick_count_t *tp;
135
    long *dp;
136
    struct sockaddr_in from;
137
    int i, len;
138
    socklen_t fromlen;
139
 
140
    ok_recv = 0;
141
    bogus_recv = 0;
142
    diag_printf("PING server %s\n", inet_ntoa(host->sin_addr));
143
    for (seq = 0;  seq < NUM_PINGS;  seq++) {
144
        // Build ICMP packet
145
        icmp->icmp_type = ICMP_ECHO;
146
        icmp->icmp_code = 0;
147
        icmp->icmp_cksum = 0;
148
        icmp->icmp_seq = seq;
149
        icmp->icmp_id = UNIQUEID;
150
        // Set up ping data
151
        tp = (cyg_tick_count_t *)&icmp->icmp_data;
152
        *tp++ = cyg_current_time();
153
        dp = (long *)tp;
154
        for (i = sizeof(*tp);  i < icmp_len;  i += sizeof(*dp)) {
155
            *dp++ = i;
156
        }
157
        // Add checksum
158
        icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
159
        // Send it off
160
        if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
161
            perror("sendto");
162
            continue;
163
        }
164
        // Wait for a response
165
        fromlen = sizeof(from);
166
        len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
167
        if (len < 0) {
168
            perror("recvfrom");
169
        } else {
170
            if (show_icmp(pkt2, len, &from, host)) {
171
                ok_recv++;
172
            } else {
173
                bogus_recv++;
174
            }
175
        }
176
    }
177
    diag_printf("Sent %d packets, received %d OK, %d bad\n", NUM_PINGS, ok_recv, bogus_recv);
178
}
179
 
180
static void
181
ping_test_loopback( int lo )
182
{
183
    struct protoent *p;
184
    struct timeval tv;
185
    struct sockaddr_in host;
186
    int s;
187
 
188
    if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
189
        perror("getprotobyname");
190
        return;
191
    }
192
    s = socket(AF_INET, SOCK_RAW, p->p_proto);
193
    if (s < 0) {
194
        perror("socket");
195
        return;
196
    }
197
    tv.tv_sec = 1;
198
    tv.tv_usec = 0;
199
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
200
    // Set up host address
201
    host.sin_family = AF_INET;
202
    host.sin_len = sizeof(host);
203
    host.sin_addr.s_addr = htonl(INADDR_LOOPBACK + (0x100 * lo));
204
    host.sin_port = 0;
205
    ping_host(s, &host);
206
    // Now try a bogus host
207
    host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) + 32);
208
    ping_host(s, &host);
209
}
210
 
211
#ifdef CYGPKG_NET_INET6
212
static int
213
show6_icmp(unsigned char *pkt, int len,
214
          const struct sockaddr_in6 *from, const struct sockaddr_in6 *to)
215
{
216
    cyg_tick_count_t *tp, tv;
217
    struct icmp6_hdr *icmp;
218
    char fromnamebuf[128];
219
    char tonamebuf[128];
220
    int error;
221
 
222
    error = getnameinfo((struct sockaddr *)from,sizeof(*from),
223
                        fromnamebuf, sizeof(fromnamebuf),
224
                        NULL, 0,
225
                        NI_NUMERICHOST);
226
    if (error) {
227
      perror ("getnameinfo(from)");
228
      return 0;
229
    }
230
 
231
    error = getnameinfo((struct sockaddr *)to,sizeof(*to),
232
                        tonamebuf, sizeof(tonamebuf),
233
                        NULL, 0,
234
                        NI_NUMERICHOST);
235
    if (error) {
236
      perror ("getnameinfo(to)");
237
      return 0;
238
    }
239
 
240
    tv = cyg_current_time();
241
    icmp = (struct icmp6_hdr *)pkt;
242
    tp = (cyg_tick_count_t *)&icmp->icmp6_data8[4];
243
 
244
    if (icmp->icmp6_type != ICMP6_ECHO_REPLY) {
245
        return 0;
246
    }
247
    if (icmp->icmp6_id != UNIQUEID) {
248
        diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
249
                    fromnamebuf, UNIQUEID, icmp->icmp6_id);
250
    }
251
    diag_printf("%d bytes from %s: ", len, fromnamebuf);
252
    diag_printf("icmp_seq=%d", icmp->icmp6_seq);
253
    diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
254
    return (!memcmp(&from->sin6_addr, &to->sin6_addr,sizeof(from->sin6_addr)));
255
}
256
 
257
static void
258
ping6_host(int s, const struct sockaddr_in6 *host)
259
{
260
    struct icmp6_hdr *icmp = (struct icmp6_hdr *)pkt1;
261
    int icmp_len = 64;
262
    int seq, ok_recv, bogus_recv;
263
    cyg_tick_count_t *tp;
264
    long *dp;
265
    struct sockaddr_in6 from;
266
    int i, len, fromlen;
267
    char namebuf[128];
268
    int error;
269
    int echo_responce;
270
 
271
    ok_recv = 0;
272
    bogus_recv = 0;
273
    error = getnameinfo((struct sockaddr *)host,sizeof(*host),
274
                        namebuf, sizeof(namebuf),
275
                        NULL, 0,
276
                        NI_NUMERICHOST);
277
    if (error) {
278
      perror ("getnameinfo");
279
    } else {
280
      diag_printf("PING6 server %s\n", namebuf);
281
    }
282
    for (seq = 0;  seq < NUM_PINGS;  seq++) {
283
        // Build ICMP packet
284
        icmp->icmp6_type = ICMP6_ECHO_REQUEST;
285
        icmp->icmp6_code = 0;
286
        icmp->icmp6_cksum = 0;
287
        icmp->icmp6_seq = seq;
288
        icmp->icmp6_id = UNIQUEID;
289
 
290
        // Set up ping data
291
        tp = (cyg_tick_count_t *)&icmp->icmp6_data8[4];
292
        *tp++ = cyg_current_time();
293
        dp = (long *)tp;
294
        for (i = sizeof(*tp);  i < icmp_len;  i += sizeof(*dp)) {
295
            *dp++ = i;
296
        }
297
        // Add checksum
298
        icmp->icmp6_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
299
        // Send it off
300
        if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
301
            perror("sendto");
302
            continue;
303
        }
304
        // Wait for a response. We get our own ECHO_REQUEST and the responce
305
        echo_responce = 0;
306
        while (!echo_responce) {
307
          fromlen = sizeof(from);
308
          len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
309
          if (len < 0) {
310
            perror("recvfrom");
311
            echo_responce=1;
312
          } else {
313
            if (show6_icmp(pkt2, len, &from, host)) {
314
              ok_recv++;
315
              echo_responce=1;
316
            }
317
          }
318
        }
319
    }
320
    diag_printf("Sent %d packets, received %d OK, %d bad\n", NUM_PINGS, ok_recv, bogus_recv);
321
}
322
 
323
static void
324
ping6_test_loopback( int lo )
325
{
326
    struct protoent *p;
327
    struct timeval tv;
328
    struct sockaddr_in6 host;
329
    int s;
330
 
331
    if ((p = getprotobyname("ipv6-icmp")) == (struct protoent *)0) {
332
        perror("getprotobyname");
333
        return;
334
    }
335
    s = socket(AF_INET6, SOCK_RAW, p->p_proto);
336
    if (s < 0) {
337
        perror("socket");
338
        return;
339
    }
340
    tv.tv_sec = 1;
341
    tv.tv_usec = 0;
342
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
343
    // Set up host address
344
    host.sin6_family = AF_INET6;
345
    host.sin6_len = sizeof(host);
346
    host.sin6_addr = in6addr_loopback;
347
    host.sin6_port = 0;
348
    ping6_host(s, &host);
349
    // Now try a bogus host
350
    host.sin6_addr.s6_addr[15] = host.sin6_addr.s6_addr[15] + 32;
351
    ping6_host(s, &host);
352
}
353
#endif
354
 
355
void
356
net_test(cyg_addrword_t p)
357
{
358
    int i;
359
    diag_printf("Start PING test\n");
360
 
361
    init_all_network_interfaces();
362
#if NLOOP > 0
363
    for ( i = 0; i < NLOOP; i++ )
364
        ping_test_loopback( i );
365
    for ( i = 0; i < NLOOP; i++ )
366
        ping_test_loopback( i );
367
#ifdef CYGPKG_NET_INET6
368
    for ( i = 0; i < NLOOP; i++ )
369
        ping6_test_loopback( i );
370
#endif
371
    CYG_TEST_PASS_FINISH( "Done pinging loopback" );
372
#endif
373
    CYG_TEST_NA( "No loopback devs" );
374
}
375
 
376
void
377
cyg_start(void)
378
{
379
    CYG_TEST_INIT();
380
 
381
    // Create a main thread, so we can run the scheduler and have time 'pass'
382
    cyg_thread_create(CYGPKG_NET_THREAD_PRIORITY-4,// Priority - just a number
383
                      net_test,                 // entry
384
                      0,                        // entry parameter
385
                      "Loopback ping  test",    // Name
386
                      &stack[0],                // Stack
387
                      STACK_SIZE,               // Size
388
                      &thread_handle,           // Handle
389
                      &thread_data              // Thread data structure
390
            );
391
    cyg_thread_resume(thread_handle);           // Start it
392
    cyg_scheduler_start();
393
}

powered by: WebSVN 2.1.0

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