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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [ppp/] [current/] [tests/] [isp.c] - Blame information for rev 838

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

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      tests/isp.c
4
//
5
//      Test of PPP and CHAT connection to an ISP
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 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
// ####BSDALTCOPYRIGHTBEGIN####                                             
40
// -------------------------------------------                              
41
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
42
// or other sources, and if so are covered by the appropriate copyright     
43
// and license included herein.                                             
44
// -------------------------------------------                              
45
// ####BSDALTCOPYRIGHTEND####                                               
46
//==========================================================================
47
//#####DESCRIPTIONBEGIN####
48
//
49
// Author(s):    nickg
50
// Contributors: gthomas (ping code), nickg
51
// Date:         2003-06-01
52
// Purpose:      
53
// Description:  This test uses CHAT to set up a modem, dial, talk to an ISP
54
//               and then ping some well known addresses. It then brings the
55
//               link down.
56
//              
57
//
58
//####DESCRIPTIONEND####
59
//
60
//==========================================================================
61
 
62
// PPP test code
63
 
64
#include <network.h>
65
 
66
#include <pkgconf/system.h>
67
#include <pkgconf/net.h>
68
#include <pkgconf/ppp.h>
69
 
70
#include <cyg/ppp/ppp.h>
71
 
72
#include <cyg/io/io.h>
73
#include <cyg/io/serialio.h>
74
 
75
#include <cyg/infra/testcase.h>
76
 
77
//==========================================================================
78
 
79
#include <arpa/inet.h>
80
 
81
// Fill in the blanks if necessary
82
#ifndef TNR_OFF
83
# define TNR_OFF()
84
#endif
85
#ifndef TNR_ON
86
# define TNR_ON()
87
#endif
88
#ifndef TNR_INIT
89
# define TNR_INIT()
90
#endif
91
#ifndef TNR_PRINT_ACTIVITY
92
# define TNR_PRINT_ACTIVITY()
93
#endif
94
 
95
#define NUM_PINGS 16
96
#define MAX_PACKET 4096
97
#define MIN_PACKET   64
98
#define MAX_SEND   4000
99
 
100
#define PACKET_ADD  ((MAX_SEND - MIN_PACKET)/NUM_PINGS)
101
#define nPACKET_ADD  1 
102
 
103
static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];
104
 
105
#define UNIQUEID 0x1234
106
 
107
void
108
pexit(char *s)
109
{
110
    CYG_TEST_FAIL_FINISH(s);
111
}
112
 
113
// Compute INET checksum
114
int
115
inet_cksum(u_short *addr, int len)
116
{
117
    register int nleft = len;
118
    register u_short *w = addr;
119
    register u_short answer;
120
    register u_int sum = 0;
121
    u_short odd_byte = 0;
122
 
123
    /*
124
     *  Our algorithm is simple, using a 32 bit accumulator (sum),
125
     *  we add sequential 16 bit words to it, and at the end, fold
126
     *  back all the carry bits from the top 16 bits into the lower
127
     *  16 bits.
128
     */
129
    while( nleft > 1 )  {
130
        sum += *w++;
131
        nleft -= 2;
132
    }
133
 
134
    /* mop up an odd byte, if necessary */
135
    if( nleft == 1 ) {
136
        *(u_char *)(&odd_byte) = *(u_char *)w;
137
        sum += odd_byte;
138
    }
139
 
140
    /*
141
     * add back carry outs from top 16 bits to low 16 bits
142
     */
143
    sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
144
    sum += (sum >> 16);                     /* add carry */
145
    answer = ~sum;                          /* truncate to 16 bits */
146
    return (answer);
147
}
148
 
149
static int
150
show_icmp(unsigned char *pkt, int len,
151
          struct sockaddr_in *from, struct sockaddr_in *to)
152
{
153
    cyg_tick_count_t *tp, tv;
154
    struct ip *ip;
155
    struct icmp *icmp;
156
    tv = cyg_current_time();
157
    ip = (struct ip *)pkt;
158
    if ((len < sizeof(*ip)) || ip->ip_v != IPVERSION) {
159
        diag_printf("%s: Short packet or not IP! - Len: %d, Version: %d\n",
160
                    inet_ntoa(from->sin_addr), len, ip->ip_v);
161
        return 0;
162
    }
163
    icmp = (struct icmp *)(pkt + sizeof(*ip));
164
    len -= (sizeof(*ip) + 8);
165
    tp = (cyg_tick_count_t *)&icmp->icmp_data;
166
    if (icmp->icmp_type != ICMP_ECHOREPLY) {
167
        diag_printf("%s: Invalid ICMP - type: %d\n",
168
                    inet_ntoa(from->sin_addr), icmp->icmp_type);
169
        return 0;
170
    }
171
    if (icmp->icmp_id != UNIQUEID) {
172
        diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
173
                    inet_ntoa(from->sin_addr), UNIQUEID, icmp->icmp_id);
174
    }
175
    diag_printf("%d bytes from %s: ", len, inet_ntoa(from->sin_addr));
176
    diag_printf("icmp_seq=%d", icmp->icmp_seq);
177
    diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
178
    return (from->sin_addr.s_addr == to->sin_addr.s_addr);
179
}
180
 
181
static void
182
ping_host(int s, struct sockaddr_in *host)
183
{
184
    struct icmp *icmp = (struct icmp *)pkt1;
185
    int icmp_len = MIN_PACKET;
186
    int seq, ok_recv, bogus_recv;
187
    cyg_tick_count_t *tp;
188
    long *dp;
189
    struct sockaddr_in from;
190
    int i, len, fromlen;
191
 
192
    ok_recv = 0;
193
    bogus_recv = 0;
194
    diag_printf("PING server %s\n", inet_ntoa(host->sin_addr));
195
    for (seq = 0;  seq < NUM_PINGS;  seq++, icmp_len += PACKET_ADD ) {
196
        TNR_ON();
197
        // Build ICMP packet
198
        icmp->icmp_type = ICMP_ECHO;
199
        icmp->icmp_code = 0;
200
        icmp->icmp_cksum = 0;
201
        icmp->icmp_seq = seq;
202
        icmp->icmp_id = 0x1234;
203
        // Set up ping data
204
        tp = (cyg_tick_count_t *)&icmp->icmp_data;
205
        *tp++ = cyg_current_time();
206
        dp = (long *)tp;
207
        for (i = sizeof(*tp);  i < icmp_len;  i += sizeof(*dp)) {
208
            *dp++ = i;
209
        }
210
        // Add checksum
211
        icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
212
        // Send it off
213
        if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
214
            TNR_OFF();
215
            perror("sendto");
216
            continue;
217
        }
218
        // Wait for a response
219
        fromlen = sizeof(from);
220
        len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
221
        TNR_OFF();
222
        if (len < 0) {
223
            perror("recvfrom");
224
            icmp_len = MIN_PACKET - PACKET_ADD; // just in case - long routes
225
        } else {
226
            if (show_icmp(pkt2, len, &from, host)) {
227
                ok_recv++;
228
            } else {
229
                bogus_recv++;
230
            }
231
        }
232
    }
233
    TNR_OFF();
234
    diag_printf("Sent %d packets, received %d OK, %d bad\n", NUM_PINGS, ok_recv, bogus_recv);
235
}
236
 
237
static void do_ping(char *addr)
238
{
239
    struct protoent *p;
240
    struct timeval tv;
241
    struct sockaddr_in host;
242
    int s;
243
 
244
    if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
245
        pexit("getprotobyname");
246
        return;
247
    }
248
    s = socket(AF_INET, SOCK_RAW, p->p_proto);
249
    if (s < 0) {
250
        pexit("socket");
251
        return;
252
    }
253
    tv.tv_sec = 1;
254
    tv.tv_usec = 0;
255
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
256
    // Set up host address
257
    host.sin_family = AF_INET;
258
    host.sin_len = sizeof(host);
259
    inet_aton(addr, &host.sin_addr);
260
    host.sin_port = 0;
261
 
262
    ping_host(s, &host);
263
 
264
}
265
 
266
//==========================================================================
267
 
268
 
269
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
270
static char stack[STACK_SIZE];
271
static cyg_thread thread_data;
272
static cyg_handle_t thread_handle;
273
 
274
//==========================================================================
275
/*
276
 
277
Captured session:
278
 
279
 
280
AT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0
281
OK
282
ATD08440416662
283
CONNECT 42666/ARQ/V90/LAPM/V42BIS
284
anchor-du-10.access.demon.net
285
 
286
 
287
login: xxxxxx
288
Password: yyyyyy
289
Protocol: ppp
290
xxxxxx: IP Address: XXX.XXX.XXX.XXX  Running PPP on 1223
291
No operational problems reported.
292
Finger status@gate.demon.co.uk for more info - Last change 16:30 Jun 08
293
HELLO
294
 
295
 
296
 
297
OK
298
ATH
299
OK
300
 
301
*/
302
 
303
static char *script[] =
304
{
305
    "ABORT"             ,       "BUSY"                                  ,
306
    "ABORT"             ,       "NO CARRIER"                            ,
307
    "ABORT"             ,       "ERROR"                                 ,
308
    ""                  ,       "ATZ"                                   ,
309
    "OK"                ,       "AT S7=45 S0=0 L1 V1 X4 &C1 E1 Q0"      ,
310
    "OK"                ,       "ATD" CYGPKG_PPP_DEFAULT_DIALUP_NUMBER  ,
311
    "ogin:--ogin:"      ,       CYGPKG_PPP_AUTH_DEFAULT_USER            ,
312
    "assword:"          ,       CYGPKG_PPP_AUTH_DEFAULT_PASSWD          ,
313
    "otocol:"           ,       "ppp"                                   ,
314
    "HELLO"             ,       "\\c"                                   ,
315
 
316
};
317
 
318
//==========================================================================
319
 
320
void
321
isp_test(cyg_addrword_t p)
322
{
323
    CYG_TEST_INIT();
324
 
325
    diag_printf("Start ISP test\n");
326
 
327
    init_all_network_interfaces();
328
 
329
    {
330
        cyg_ppp_options_t options;
331
        cyg_ppp_handle_t ppp_handle;
332
 
333
        cyg_ppp_options_init( &options );
334
 
335
//        options.debug = 1;
336
//        options.kdebugflag = 1;
337
 
338
        options.modem = 1;
339
        options.script = &script[0];
340
 
341
        ppp_handle = cyg_ppp_up( CYGPKG_PPP_TEST_DEVICE, &options );
342
 
343
        CYG_TEST_INFO( "Waiting for PPP to come up");
344
 
345
        cyg_ppp_wait_up( ppp_handle );
346
 
347
        CYG_TEST_INFO( "Delaying for a while...");
348
 
349
        cyg_thread_delay(10*100);
350
 
351
        CYG_TEST_INFO( "Pinging");
352
 
353
        do_ping("195.173.57.10");       // anchor-du-10.access.demon.net
354
        do_ping("194.154.160.254");     // fluffy.ecoscentric.com
355
        do_ping("66.187.233.205");      // sources.redhat.com
356
 
357
        CYG_TEST_INFO( "Bringing PPP down");
358
 
359
        cyg_ppp_down( ppp_handle );
360
 
361
        CYG_TEST_INFO( "Waiting for PPP to go down");
362
 
363
        cyg_ppp_wait_down( ppp_handle );
364
    }
365
 
366
    CYG_TEST_PASS_FINISH("ISP test OK");
367
}
368
 
369
//==========================================================================
370
 
371
void
372
cyg_start(void)
373
{
374
    // Create a main thread, so we can run the scheduler and have time 'pass'
375
    cyg_thread_create(10,                // Priority - just a number
376
                      isp_test,          // entry
377
                      0,                 // entry parameter
378
                      "ISP test",        // Name
379
                      &stack[0],         // Stack
380
                      STACK_SIZE,        // Size
381
                      &thread_handle,    // Handle
382
                      &thread_data       // Thread data structure
383
            );
384
    cyg_thread_resume(thread_handle);  // Start it
385
    cyg_scheduler_start();
386
}
387
 
388
//==========================================================================

powered by: WebSVN 2.1.0

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