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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [ppp/] [current/] [src/] [cbcp.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      src/cbcp.c
4
//
5
//==========================================================================
6
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
7
// -------------------------------------------                              
8
// This file is part of eCos, the Embedded Configurable Operating System.   
9
// Copyright (C) 2003 Free Software Foundation, Inc.                        
10
//
11
// eCos is free software; you can redistribute it and/or modify it under    
12
// the terms of the GNU General Public License as published by the Free     
13
// Software Foundation; either version 2 or (at your option) any later      
14
// version.                                                                 
15
//
16
// eCos is distributed in the hope that it will be useful, but WITHOUT      
17
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
18
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
19
// for more details.                                                        
20
//
21
// You should have received a copy of the GNU General Public License        
22
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
23
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
24
//
25
// As a special exception, if other files instantiate templates or use      
26
// macros or inline functions from this file, or you compile this file      
27
// and link it with other works to produce a work based on this file,       
28
// this file does not by itself cause the resulting work to be covered by   
29
// the GNU General Public License. However the source code for this file    
30
// must still be made available in accordance with section (3) of the GNU   
31
// General Public License v2.                                               
32
//
33
// This exception does not invalidate any other reasons why a work based    
34
// on this file might be covered by the GNU General Public License.         
35
// -------------------------------------------                              
36
// ####ECOSGPLCOPYRIGHTEND####                                              
37
// ####BSDALTCOPYRIGHTBEGIN####                                             
38
// -------------------------------------------                              
39
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
40
// or other sources, and if so are covered by the appropriate copyright     
41
// and license included herein.                                             
42
// -------------------------------------------                              
43
// ####BSDALTCOPYRIGHTEND####                                               
44
//==========================================================================
45
 
46
/*
47
 * cbcp - Call Back Configuration Protocol.
48
 *
49
 * Copyright (c) 1995 Pedro Roque Marques
50
 * All rights reserved.
51
 *
52
 * Redistribution and use in source and binary forms are permitted
53
 * provided that the above copyright notice and this paragraph are
54
 * duplicated in all such forms and that any documentation,
55
 * advertising materials, and other materials related to such
56
 * distribution and use acknowledge that the software was developed
57
 * by Pedro Roque Marques.  The name of the author may not be used to
58
 * endorse or promote products derived from this software without
59
 * specific prior written permission.
60
 *
61
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
62
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
63
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
64
 */
65
 
66
#ifndef lint
67
//static char rcsid[] = "$FreeBSD: src/usr.sbin/pppd/cbcp.c,v 1.4 1999/08/28 01:19:00 peter Exp $";
68
#endif
69
 
70
#include <stdio.h>
71
#include <string.h>
72
#include <sys/types.h>
73
#include <sys/time.h>
74
#include <cyg/ppp/syslog.h>
75
#include "cyg/ppp/pppd.h"
76
#include "cyg/ppp/cbcp.h"
77
#include "cyg/ppp/fsm.h"
78
#include "cyg/ppp/lcp.h"
79
#include "cyg/ppp/ipcp.h"
80
 
81
/*
82
 * Protocol entry points.
83
 */
84
static void cbcp_init      __P((int unit));
85
static void cbcp_open      __P((int unit));
86
static void cbcp_lowerup   __P((int unit));
87
static void cbcp_input     __P((int unit, u_char *pkt, int len));
88
static void cbcp_protrej   __P((int unit));
89
static int  cbcp_printpkt  __P((u_char *pkt, int len,
90
                                void (*printer) __P((void *, char *, ...)),
91
                                void *arg));
92
 
93
struct protent cbcp_protent = {
94
    PPP_CBCP,
95
    cbcp_init,
96
    cbcp_input,
97
    cbcp_protrej,
98
    cbcp_lowerup,
99
    NULL,
100
    cbcp_open,
101
    NULL,
102
    cbcp_printpkt,
103
    NULL,
104
    0,
105
    "CBCP",
106
    NULL,
107
    NULL,
108
    NULL
109
};
110
 
111
cbcp_state cbcp[NUM_PPP];
112
 
113
/* internal prototypes */
114
 
115
static void cbcp_recvreq __P((cbcp_state *us, u_char *pckt, int len));
116
static void cbcp_resp __P((cbcp_state *us));
117
static void cbcp_up __P((cbcp_state *us));
118
static void cbcp_recvack __P((cbcp_state *us, u_char *pckt, int len));
119
static void cbcp_send __P((cbcp_state *us, u_char code, u_char *buf, int len));
120
 
121
/* init state */
122
static void
123
cbcp_init(iface)
124
    int iface;
125
{
126
    cbcp_state *us;
127
 
128
    us = &cbcp[iface];
129
    memset(us, 0, sizeof(cbcp_state));
130
    us->us_unit = iface;
131
    us->us_type |= (1 << CB_CONF_NO);
132
}
133
 
134
/* lower layer is up */
135
static void
136
cbcp_lowerup(iface)
137
    int iface;
138
{
139
    cbcp_state *us = &cbcp[iface];
140
 
141
    syslog(LOG_DEBUG, "cbcp_lowerup");
142
    syslog(LOG_DEBUG, "want: %d", us->us_type);
143
 
144
    if (us->us_type == CB_CONF_USER)
145
        syslog(LOG_DEBUG, "phone no: %s", us->us_number);
146
}
147
 
148
static void
149
cbcp_open(unit)
150
    int unit;
151
{
152
    syslog(LOG_DEBUG, "cbcp_open");
153
}
154
 
155
/* process an incomming packet */
156
static void
157
cbcp_input(unit, inpacket, pktlen)
158
    int unit;
159
    u_char *inpacket;
160
    int pktlen;
161
{
162
    u_char *inp;
163
    u_char code, id;
164
    u_short len;
165
 
166
    cbcp_state *us = &cbcp[unit];
167
 
168
    inp = inpacket;
169
 
170
    if (pktlen < CBCP_MINLEN) {
171
        syslog(LOG_ERR, "CBCP packet is too small");
172
        return;
173
    }
174
 
175
    GETCHAR(code, inp);
176
    GETCHAR(id, inp);
177
    GETSHORT(len, inp);
178
 
179
#if 0
180
    if (len > pktlen) {
181
        syslog(LOG_ERR, "CBCP packet: invalid length");
182
        return;
183
    }
184
#endif
185
 
186
    len -= CBCP_MINLEN;
187
 
188
    switch(code) {
189
    case CBCP_REQ:
190
        us->us_id = id;
191
        cbcp_recvreq(us, inp, len);
192
        break;
193
 
194
    case CBCP_RESP:
195
        syslog(LOG_DEBUG, "CBCP_RESP received");
196
        break;
197
 
198
    case CBCP_ACK:
199
        if (id != us->us_id)
200
            syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
201
                   us->us_id, id);
202
 
203
        cbcp_recvack(us, inp, len);
204
        break;
205
 
206
    default:
207
        break;
208
    }
209
}
210
 
211
/* protocol was rejected by foe */
212
void cbcp_protrej(int iface)
213
{
214
}
215
 
216
char *cbcp_codenames[] = {
217
    "Request", "Response", "Ack"
218
};
219
 
220
char *cbcp_optionnames[] = {
221
    "NoCallback",
222
    "UserDefined",
223
    "AdminDefined",
224
    "List"
225
};
226
 
227
/* pretty print a packet */
228
static int
229
cbcp_printpkt(p, plen, printer, arg)
230
    u_char *p;
231
    int plen;
232
    void (*printer) __P((void *, char *, ...));
233
    void *arg;
234
{
235
    int code, opt, id, len, olen, delay;
236
    u_char *pstart;
237
 
238
    if (plen < HEADERLEN)
239
        return 0;
240
    pstart = p;
241
    GETCHAR(code, p);
242
    GETCHAR(id, p);
243
    GETSHORT(len, p);
244
    if (len < HEADERLEN || len > plen)
245
        return 0;
246
 
247
    if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
248
        printer(arg, " %s", cbcp_codenames[code-1]);
249
    else
250
        printer(arg, " code=0x%x", code);
251
 
252
    printer(arg, " id=0x%x", id);
253
    len -= HEADERLEN;
254
 
255
    switch (code) {
256
    case CBCP_REQ:
257
    case CBCP_RESP:
258
    case CBCP_ACK:
259
        while(len >= 2) {
260
            GETCHAR(opt, p);
261
            GETCHAR(olen, p);
262
 
263
            if (olen < 2 || olen > len) {
264
                break;
265
            }
266
 
267
            printer(arg, " <");
268
            len -= olen;
269
 
270
            if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
271
                printer(arg, " %s", cbcp_optionnames[opt-1]);
272
            else
273
                printer(arg, " option=0x%x", opt);
274
 
275
            if (olen > 2) {
276
                GETCHAR(delay, p);
277
                printer(arg, " delay = %d", delay);
278
            }
279
 
280
            if (olen > 3) {
281
                int addrt;
282
                char str[256];
283
 
284
                GETCHAR(addrt, p);
285
                memcpy(str, p, olen - 4);
286
                str[olen - 4] = 0;
287
                printer(arg, " number = %s", str);
288
            }
289
            printer(arg, ">");
290
            break;
291
        }
292
 
293
    default:
294
        break;
295
    }
296
 
297
    for (; len > 0; --len) {
298
        GETCHAR(code, p);
299
        printer(arg, " %.2x", code);
300
    }
301
 
302
    return p - pstart;
303
}
304
 
305
/* received CBCP request */
306
static void
307
cbcp_recvreq(us, pckt, pcktlen)
308
    cbcp_state *us;
309
    u_char *pckt;
310
    int pcktlen;
311
{
312
    u_char type, opt_len, delay, addr_type;
313
    char address[256];
314
    int len = pcktlen;
315
 
316
    address[0] = 0;
317
 
318
    while (len) {
319
        syslog(LOG_DEBUG, "length: %d", len);
320
 
321
        GETCHAR(type, pckt);
322
        GETCHAR(opt_len, pckt);
323
 
324
        if (opt_len > 2)
325
            GETCHAR(delay, pckt);
326
 
327
        us->us_allowed |= (1 << type);
328
 
329
        switch(type) {
330
        case CB_CONF_NO:
331
            syslog(LOG_DEBUG, "no callback allowed");
332
            break;
333
 
334
        case CB_CONF_USER:
335
            syslog(LOG_DEBUG, "user callback allowed");
336
            if (opt_len > 4) {
337
                GETCHAR(addr_type, pckt);
338
                memcpy(address, pckt, opt_len - 4);
339
                address[opt_len - 4] = 0;
340
                if (address[0])
341
                    syslog(LOG_DEBUG, "address: %s", address);
342
            }
343
            break;
344
 
345
        case CB_CONF_ADMIN:
346
            syslog(LOG_DEBUG, "user admin defined allowed");
347
            break;
348
 
349
        case CB_CONF_LIST:
350
            break;
351
        }
352
        len -= opt_len;
353
    }
354
 
355
    cbcp_resp(us);
356
}
357
 
358
static void
359
cbcp_resp(us)
360
    cbcp_state *us;
361
{
362
    u_char cb_type;
363
    u_char buf[256];
364
    u_char *bufp = buf;
365
    int len = 0;
366
 
367
    cb_type = us->us_allowed & us->us_type;
368
    syslog(LOG_DEBUG, "cbcp_resp cb_type=%d", cb_type);
369
 
370
#if 0
371
    if (!cb_type)
372
        lcp_down(us->us_unit);
373
#endif
374
 
375
    if (cb_type & ( 1 << CB_CONF_USER ) ) {
376
        syslog(LOG_DEBUG, "cbcp_resp CONF_USER");
377
        PUTCHAR(CB_CONF_USER, bufp);
378
        len = 3 + 1 + strlen(us->us_number) + 1;
379
        PUTCHAR(len , bufp);
380
        PUTCHAR(5, bufp); /* delay */
381
        PUTCHAR(1, bufp);
382
        BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
383
        cbcp_send(us, CBCP_RESP, buf, len);
384
        return;
385
    }
386
 
387
    if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
388
        syslog(LOG_DEBUG, "cbcp_resp CONF_ADMIN");
389
        PUTCHAR(CB_CONF_ADMIN, bufp);
390
        len = 3 + 1;
391
        PUTCHAR(len , bufp);
392
        PUTCHAR(5, bufp); /* delay */
393
        PUTCHAR(0, bufp);
394
        cbcp_send(us, CBCP_RESP, buf, len);
395
        return;
396
    }
397
 
398
    if (cb_type & ( 1 << CB_CONF_NO ) ) {
399
        syslog(LOG_DEBUG, "cbcp_resp CONF_NO");
400
        PUTCHAR(CB_CONF_NO, bufp);
401
        len = 3;
402
        PUTCHAR(len , bufp);
403
        PUTCHAR(0, bufp);
404
        cbcp_send(us, CBCP_RESP, buf, len);
405
        (*ipcp_protent.open)(us->us_unit);
406
        return;
407
    }
408
}
409
 
410
static void
411
cbcp_send(us, code, buf, len)
412
    cbcp_state *us;
413
    u_char code;
414
    u_char *buf;
415
    int len;
416
{
417
    u_char *outp;
418
    int outlen;
419
 
420
    outp = outpacket_buf;
421
 
422
    outlen = 4 + len;
423
 
424
    MAKEHEADER(outp, PPP_CBCP);
425
 
426
    PUTCHAR(code, outp);
427
    PUTCHAR(us->us_id, outp);
428
    PUTSHORT(outlen, outp);
429
 
430
    if (len)
431
        BCOPY(buf, outp, len);
432
 
433
    output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
434
}
435
 
436
static void
437
cbcp_recvack(us, pckt, len)
438
    cbcp_state *us;
439
    u_char *pckt;
440
    int len;
441
{
442
    u_char type, delay, addr_type;
443
    int opt_len;
444
    char address[256];
445
 
446
    if (len) {
447
        GETCHAR(type, pckt);
448
        GETCHAR(opt_len, pckt);
449
 
450
        if (opt_len > 2)
451
            GETCHAR(delay, pckt);
452
 
453
        if (opt_len > 4) {
454
            GETCHAR(addr_type, pckt);
455
            memcpy(address, pckt, opt_len - 4);
456
            address[opt_len - 4] = 0;
457
            if (address[0])
458
                syslog(LOG_DEBUG, "peer will call: %s", address);
459
        }
460
    }
461
 
462
    cbcp_up(us);
463
}
464
 
465
extern int persist;
466
 
467
/* ok peer will do callback */
468
static void
469
cbcp_up(us)
470
    cbcp_state *us;
471
{
472
    persist = 0;
473
    lcp_close(0, "Call me back, please");
474
}

powered by: WebSVN 2.1.0

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