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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [common/] [v2_0/] [tests/] [ping_test.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      tests/ping_test.c
4
//
5
//      Simple test of PING (ICMP) and networking support
6
//
7
//==========================================================================
8
//####BSDCOPYRIGHTBEGIN####
9
//
10
// -------------------------------------------
11
//
12
// Portions of this software may have been derived from OpenBSD or other sources,
13
// and are covered by the appropriate copyright disclaimers included herein.
14
//
15
// -------------------------------------------
16
//
17
//####BSDCOPYRIGHTEND####
18
//==========================================================================
19
//#####DESCRIPTIONBEGIN####
20
//
21
// Author(s):    gthomas
22
// Contributors: gthomas
23
// Date:         2000-01-10
24
// Purpose:      
25
// Description:  
26
//              
27
//
28
//####DESCRIPTIONEND####
29
//
30
//==========================================================================
31
 
32
// PING test code
33
 
34
#include <network.h>
35
 
36
#include <pkgconf/system.h>
37
#include <pkgconf/net.h>
38
 
39
#include <cyg/infra/testcase.h>
40
 
41
#ifdef CYGBLD_DEVS_ETH_DEVICE_H    // Get the device config if it exists
42
#include CYGBLD_DEVS_ETH_DEVICE_H  // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
43
#endif
44
 
45
#ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
46
# ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
47
#  include CYGTST_DEVS_ETH_TEST_NET_REALTIME
48
# endif
49
#endif
50
 
51
// Fill in the blanks if necessary
52
#ifndef TNR_OFF
53
# define TNR_OFF()
54
#endif
55
#ifndef TNR_ON
56
# define TNR_ON()
57
#endif
58
#ifndef TNR_INIT
59
# define TNR_INIT()
60
#endif
61
#ifndef TNR_PRINT_ACTIVITY
62
# define TNR_PRINT_ACTIVITY()
63
#endif
64
 
65
 
66
 
67
#ifndef CYGPKG_LIBC_STDIO
68
#define perror(s) diag_printf(#s ": %s\n", strerror(errno))
69
#endif
70
 
71
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
72
static char stack[STACK_SIZE];
73
static cyg_thread thread_data;
74
static cyg_handle_t thread_handle;
75
 
76
#define NUM_PINGS 16
77
#define MAX_PACKET 4096
78
#define MIN_PACKET   64
79
#define MAX_SEND   4000
80
 
81
#define PACKET_ADD  ((MAX_SEND - MIN_PACKET)/NUM_PINGS)
82
#define nPACKET_ADD  1 
83
 
84
static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];
85
 
86
#define UNIQUEID 0x1234
87
 
88
void
89
pexit(char *s)
90
{
91
    CYG_TEST_FAIL_FINISH(s);
92
}
93
 
94
// Compute INET checksum
95
int
96
inet_cksum(u_short *addr, int len)
97
{
98
    register int nleft = len;
99
    register u_short *w = addr;
100
    register u_short answer;
101
    register u_int sum = 0;
102
    u_short odd_byte = 0;
103
 
104
    /*
105
     *  Our algorithm is simple, using a 32 bit accumulator (sum),
106
     *  we add sequential 16 bit words to it, and at the end, fold
107
     *  back all the carry bits from the top 16 bits into the lower
108
     *  16 bits.
109
     */
110
    while( nleft > 1 )  {
111
        sum += *w++;
112
        nleft -= 2;
113
    }
114
 
115
    /* mop up an odd byte, if necessary */
116
    if( nleft == 1 ) {
117
        *(u_char *)(&odd_byte) = *(u_char *)w;
118
        sum += odd_byte;
119
    }
120
 
121
    /*
122
     * add back carry outs from top 16 bits to low 16 bits
123
     */
124
    sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
125
    sum += (sum >> 16);                     /* add carry */
126
    answer = ~sum;                          /* truncate to 16 bits */
127
    return (answer);
128
}
129
 
130
static int
131
show_icmp(unsigned char *pkt, int len,
132
          struct sockaddr_in *from, struct sockaddr_in *to)
133
{
134
    cyg_tick_count_t *tp, tv;
135
    struct ip *ip;
136
    struct icmp *icmp;
137
    tv = cyg_current_time();
138
    ip = (struct ip *)pkt;
139
    if ((len < sizeof(*ip)) || ip->ip_v != IPVERSION) {
140
        diag_printf("%s: Short packet or not IP! - Len: %d, Version: %d\n",
141
                    inet_ntoa(from->sin_addr), len, ip->ip_v);
142
        return 0;
143
    }
144
    icmp = (struct icmp *)(pkt + sizeof(*ip));
145
    len -= (sizeof(*ip) + 8);
146
    tp = (cyg_tick_count_t *)&icmp->icmp_data;
147
    if (icmp->icmp_type != ICMP_ECHOREPLY) {
148
        diag_printf("%s: Invalid ICMP - type: %d\n",
149
                    inet_ntoa(from->sin_addr), icmp->icmp_type);
150
        return 0;
151
    }
152
    if (icmp->icmp_id != UNIQUEID) {
153
        diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
154
                    inet_ntoa(from->sin_addr), UNIQUEID, icmp->icmp_id);
155
    }
156
    diag_printf("%d bytes from %s: ", len, inet_ntoa(from->sin_addr));
157
    diag_printf("icmp_seq=%d", icmp->icmp_seq);
158
    diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
159
    return (from->sin_addr.s_addr == to->sin_addr.s_addr);
160
}
161
 
162
static void
163
ping_host(int s, struct sockaddr_in *host)
164
{
165
    struct icmp *icmp = (struct icmp *)pkt1;
166
    int icmp_len = MIN_PACKET;
167
    int seq, ok_recv, bogus_recv;
168
    cyg_tick_count_t *tp;
169
    long *dp;
170
    struct sockaddr_in from;
171
    int i, len, fromlen;
172
 
173
    ok_recv = 0;
174
    bogus_recv = 0;
175
    diag_printf("PING server %s\n", inet_ntoa(host->sin_addr));
176
    for (seq = 0;  seq < NUM_PINGS;  seq++, icmp_len += PACKET_ADD ) {
177
        TNR_ON();
178
        // Build ICMP packet
179
        icmp->icmp_type = ICMP_ECHO;
180
        icmp->icmp_code = 0;
181
        icmp->icmp_cksum = 0;
182
        icmp->icmp_seq = seq;
183
        icmp->icmp_id = 0x1234;
184
        // Set up ping data
185
        tp = (cyg_tick_count_t *)&icmp->icmp_data;
186
        *tp++ = cyg_current_time();
187
        dp = (long *)tp;
188
        for (i = sizeof(*tp);  i < icmp_len;  i += sizeof(*dp)) {
189
            *dp++ = i;
190
        }
191
        // Add checksum
192
        icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
193
        // Send it off
194
        if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
195
            TNR_OFF();
196
            perror("sendto");
197
            continue;
198
        }
199
        // Wait for a response
200
        fromlen = sizeof(from);
201
        len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
202
        TNR_OFF();
203
        if (len < 0) {
204
            perror("recvfrom");
205
            icmp_len = MIN_PACKET - PACKET_ADD; // just in case - long routes
206
        } else {
207
            if (show_icmp(pkt2, len, &from, host)) {
208
                ok_recv++;
209
            } else {
210
                bogus_recv++;
211
            }
212
        }
213
    }
214
    TNR_OFF();
215
    diag_printf("Sent %d packets, received %d OK, %d bad\n", NUM_PINGS, ok_recv, bogus_recv);
216
}
217
 
218
static void
219
ping_test(struct bootp *bp)
220
{
221
    struct protoent *p;
222
    struct timeval tv;
223
    struct sockaddr_in host;
224
    int s;
225
 
226
    if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
227
        pexit("getprotobyname");
228
        return;
229
    }
230
    s = socket(AF_INET, SOCK_RAW, p->p_proto);
231
    if (s < 0) {
232
        pexit("socket");
233
        return;
234
    }
235
    tv.tv_sec = 1;
236
    tv.tv_usec = 0;
237
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
238
    // Set up host address
239
    host.sin_family = AF_INET;
240
    host.sin_len = sizeof(host);
241
    host.sin_addr = bp->bp_siaddr;
242
    host.sin_port = 0;
243
    ping_host(s, &host);
244
    // Now try a bogus host
245
    host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) + 32);
246
    ping_host(s, &host);
247
}
248
 
249
void
250
net_test(cyg_addrword_t p)
251
{
252
    diag_printf("Start PING test\n");
253
    TNR_INIT();
254
    init_all_network_interfaces();
255
#ifdef CYGHWR_NET_DRIVER_ETH0
256
    if (eth0_up) {
257
        ping_test(&eth0_bootp_data);
258
    }
259
#endif
260
#ifdef CYGHWR_NET_DRIVER_ETH1
261
    if (eth1_up) {
262
        ping_test(&eth1_bootp_data);
263
    }
264
#endif
265
    TNR_PRINT_ACTIVITY();
266
    CYG_TEST_PASS_FINISH("Ping test OK");
267
}
268
 
269
void
270
cyg_start(void)
271
{
272
    // Create a main thread, so we can run the scheduler and have time 'pass'
273
    cyg_thread_create(10,                // Priority - just a number
274
                      net_test,          // entry
275
                      0,                 // entry parameter
276
                      "Network test",    // Name
277
                      &stack[0],         // Stack
278
                      STACK_SIZE,        // Size
279
                      &thread_handle,    // Handle
280
                      &thread_data       // Thread data structure
281
            );
282
    cyg_thread_resume(thread_handle);  // Start it
283
    cyg_scheduler_start();
284
}

powered by: WebSVN 2.1.0

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