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

Subversion Repositories openrisc

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

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

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

powered by: WebSVN 2.1.0

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