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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [pppd/] [cbcp.c] - Diff between revs 30 and 173

Only display areas with differences | Details | Blame | View Log

Rev 30 Rev 173
/*
/*
 * cbcp - Call Back Configuration Protocol.
 * cbcp - Call Back Configuration Protocol.
 *
 *
 * Copyright (c) 1995 Pedro Roque Marques
 * Copyright (c) 1995 Pedro Roque Marques
 * All rights reserved.
 * All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms are permitted
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * distribution and use acknowledge that the software was developed
 * by Pedro Roque Marques.  The name of the author may not be used to
 * by Pedro Roque Marques.  The name of the author may not be used to
 * endorse or promote products derived from this software without
 * endorse or promote products derived from this software without
 * specific prior written permission.
 * specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
 */
 
 
#ifndef lint
#ifndef lint
/* static char rcsid[] = "$Id: cbcp.c,v 1.2 2001-09-27 12:01:57 chris Exp $"; */
/* static char rcsid[] = "$Id: cbcp.c,v 1.2 2001-09-27 12:01:57 chris Exp $"; */
#endif
#endif
 
 
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/time.h>
#include <syslog.h>
#include <syslog.h>
 
 
#include "pppd.h"
#include "pppd.h"
#include "cbcp.h"
#include "cbcp.h"
#include "fsm.h"
#include "fsm.h"
#include "lcp.h"
#include "lcp.h"
#include "ipcp.h"
#include "ipcp.h"
 
 
/*
/*
 * Protocol entry points.
 * Protocol entry points.
 */
 */
static void cbcp_init      __P((int unit));
static void cbcp_init      __P((int unit));
static void cbcp_open      __P((int unit));
static void cbcp_open      __P((int unit));
static void cbcp_lowerup   __P((int unit));
static void cbcp_lowerup   __P((int unit));
static void cbcp_input     __P((int unit, u_char *pkt, int len));
static void cbcp_input     __P((int unit, u_char *pkt, int len));
static void cbcp_protrej   __P((int unit));
static void cbcp_protrej   __P((int unit));
static int  cbcp_printpkt  __P((u_char *pkt, int len,
static int  cbcp_printpkt  __P((u_char *pkt, int len,
                                void (*printer) __P((void *, char *, ...)),
                                void (*printer) __P((void *, char *, ...)),
                                void *arg));
                                void *arg));
 
 
struct protent cbcp_protent = {
struct protent cbcp_protent = {
    PPP_CBCP,
    PPP_CBCP,
    cbcp_init,
    cbcp_init,
    cbcp_input,
    cbcp_input,
    cbcp_protrej,
    cbcp_protrej,
    cbcp_lowerup,
    cbcp_lowerup,
    NULL,
    NULL,
    cbcp_open,
    cbcp_open,
    NULL,
    NULL,
    cbcp_printpkt,
    cbcp_printpkt,
    NULL,
    NULL,
    0,
    0,
    "CBCP",
    "CBCP",
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
    NULL
};
};
 
 
cbcp_state cbcp[NUM_PPP];
cbcp_state cbcp[NUM_PPP];
 
 
/* internal prototypes */
/* internal prototypes */
 
 
static void cbcp_recvreq __P((cbcp_state *us, char *pckt, int len));
static void cbcp_recvreq __P((cbcp_state *us, char *pckt, int len));
static void cbcp_resp __P((cbcp_state *us));
static void cbcp_resp __P((cbcp_state *us));
static void cbcp_up __P((cbcp_state *us));
static void cbcp_up __P((cbcp_state *us));
static void cbcp_recvack __P((cbcp_state *us, char *pckt, int len));
static void cbcp_recvack __P((cbcp_state *us, char *pckt, int len));
static void cbcp_send __P((cbcp_state *us, u_char code, u_char *buf, int len));
static void cbcp_send __P((cbcp_state *us, u_char code, u_char *buf, int len));
 
 
/* init state */
/* init state */
static void
static void
cbcp_init(iface)
cbcp_init(iface)
    int iface;
    int iface;
{
{
    cbcp_state *us;
    cbcp_state *us;
 
 
    us = &cbcp[iface];
    us = &cbcp[iface];
    memset(us, 0, sizeof(cbcp_state));
    memset(us, 0, sizeof(cbcp_state));
    us->us_unit = iface;
    us->us_unit = iface;
    us->us_type |= (1 << CB_CONF_NO);
    us->us_type |= (1 << CB_CONF_NO);
}
}
 
 
/* lower layer is up */
/* lower layer is up */
static void
static void
cbcp_lowerup(iface)
cbcp_lowerup(iface)
    int iface;
    int iface;
{
{
    cbcp_state *us = &cbcp[iface];
    cbcp_state *us = &cbcp[iface];
 
 
 
 
    syslog(LOG_DEBUG, "cbcp_lowerup");
    syslog(LOG_DEBUG, "cbcp_lowerup");
    syslog(LOG_DEBUG, "want: %d", us->us_type);
    syslog(LOG_DEBUG, "want: %d", us->us_type);
 
 
    if (us->us_type == CB_CONF_USER)
    if (us->us_type == CB_CONF_USER)
        syslog(LOG_DEBUG, "phone no: %s", us->us_number);
        syslog(LOG_DEBUG, "phone no: %s", us->us_number);
}
}
 
 
static void
static void
cbcp_open(unit)
cbcp_open(unit)
    int unit;
    int unit;
{
{
    syslog(LOG_DEBUG, "cbcp_open");
    syslog(LOG_DEBUG, "cbcp_open");
}
}
 
 
/* process an incomming packet */
/* process an incomming packet */
static void
static void
cbcp_input(unit, inpacket, pktlen)
cbcp_input(unit, inpacket, pktlen)
    int unit;
    int unit;
    u_char *inpacket;
    u_char *inpacket;
    int pktlen;
    int pktlen;
{
{
    u_char *inp;
    u_char *inp;
    u_char code, id;
    u_char code, id;
    u_short len;
    u_short len;
 
 
    cbcp_state *us = &cbcp[unit];
    cbcp_state *us = &cbcp[unit];
 
 
    inp = inpacket;
    inp = inpacket;
 
 
    if (pktlen < CBCP_MINLEN) {
    if (pktlen < CBCP_MINLEN) {
        syslog(LOG_ERR, "CBCP packet is too small");
        syslog(LOG_ERR, "CBCP packet is too small");
        return;
        return;
    }
    }
 
 
    GETCHAR(code, inp);
    GETCHAR(code, inp);
    GETCHAR(id, inp);
    GETCHAR(id, inp);
    GETSHORT(len, inp);
    GETSHORT(len, inp);
 
 
#if 0
#if 0
    if (len > pktlen) {
    if (len > pktlen) {
        syslog(LOG_ERR, "CBCP packet: invalid length");
        syslog(LOG_ERR, "CBCP packet: invalid length");
        return;
        return;
    }
    }
#endif
#endif
 
 
    len -= CBCP_MINLEN;
    len -= CBCP_MINLEN;
 
 
    switch(code) {
    switch(code) {
    case CBCP_REQ:
    case CBCP_REQ:
        us->us_id = id;
        us->us_id = id;
        cbcp_recvreq(us, inp, len);
        cbcp_recvreq(us, inp, len);
        break;
        break;
 
 
    case CBCP_RESP:
    case CBCP_RESP:
        syslog(LOG_DEBUG, "CBCP_RESP received");
        syslog(LOG_DEBUG, "CBCP_RESP received");
        break;
        break;
 
 
    case CBCP_ACK:
    case CBCP_ACK:
        if (id != us->us_id)
        if (id != us->us_id)
            syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
            syslog(LOG_DEBUG, "id doesn't match: expected %d recv %d",
                   us->us_id, id);
                   us->us_id, id);
 
 
        cbcp_recvack(us, inp, len);
        cbcp_recvack(us, inp, len);
        break;
        break;
 
 
    default:
    default:
        break;
        break;
    }
    }
}
}
 
 
/* protocol was rejected by foe */
/* protocol was rejected by foe */
void cbcp_protrej(int iface)
void cbcp_protrej(int iface)
{
{
}
}
 
 
char *cbcp_codenames[] = {
char *cbcp_codenames[] = {
    "Request", "Response", "Ack"
    "Request", "Response", "Ack"
};
};
 
 
char *cbcp_optionnames[] = {
char *cbcp_optionnames[] = {
    "NoCallback",
    "NoCallback",
    "UserDefined",
    "UserDefined",
    "AdminDefined",
    "AdminDefined",
    "List"
    "List"
};
};
 
 
/* pretty print a packet */
/* pretty print a packet */
static int
static int
cbcp_printpkt(p, plen, printer, arg)
cbcp_printpkt(p, plen, printer, arg)
    u_char *p;
    u_char *p;
    int plen;
    int plen;
    void (*printer) __P((void *, char *, ...));
    void (*printer) __P((void *, char *, ...));
    void *arg;
    void *arg;
{
{
    int code, opt, id, len, olen, delay;
    int code, opt, id, len, olen, delay;
    u_char *pstart;
    u_char *pstart;
 
 
    if (plen < HEADERLEN)
    if (plen < HEADERLEN)
        return 0;
        return 0;
    pstart = p;
    pstart = p;
    GETCHAR(code, p);
    GETCHAR(code, p);
    GETCHAR(id, p);
    GETCHAR(id, p);
    GETSHORT(len, p);
    GETSHORT(len, p);
    if (len < HEADERLEN || len > plen)
    if (len < HEADERLEN || len > plen)
        return 0;
        return 0;
 
 
    if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
    if (code >= 1 && code <= sizeof(cbcp_codenames) / sizeof(char *))
        printer(arg, " %s", cbcp_codenames[code-1]);
        printer(arg, " %s", cbcp_codenames[code-1]);
    else
    else
        printer(arg, " code=0x%x", code);
        printer(arg, " code=0x%x", code);
 
 
    printer(arg, " id=0x%x", id);
    printer(arg, " id=0x%x", id);
    len -= HEADERLEN;
    len -= HEADERLEN;
 
 
    switch (code) {
    switch (code) {
    case CBCP_REQ:
    case CBCP_REQ:
    case CBCP_RESP:
    case CBCP_RESP:
    case CBCP_ACK:
    case CBCP_ACK:
        while(len >= 2) {
        while(len >= 2) {
            GETCHAR(opt, p);
            GETCHAR(opt, p);
            GETCHAR(olen, p);
            GETCHAR(olen, p);
 
 
            if (olen < 2 || olen > len) {
            if (olen < 2 || olen > len) {
                break;
                break;
            }
            }
 
 
            printer(arg, " <");
            printer(arg, " <");
            len -= olen;
            len -= olen;
 
 
            if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
            if (opt >= 1 && opt <= sizeof(cbcp_optionnames) / sizeof(char *))
                printer(arg, " %s", cbcp_optionnames[opt-1]);
                printer(arg, " %s", cbcp_optionnames[opt-1]);
            else
            else
                printer(arg, " option=0x%x", opt);
                printer(arg, " option=0x%x", opt);
 
 
            if (olen > 2) {
            if (olen > 2) {
                GETCHAR(delay, p);
                GETCHAR(delay, p);
                printer(arg, " delay = %d", delay);
                printer(arg, " delay = %d", delay);
            }
            }
 
 
            if (olen > 3) {
            if (olen > 3) {
                int addrt;
                int addrt;
                char str[256];
                char str[256];
 
 
                GETCHAR(addrt, p);
                GETCHAR(addrt, p);
                memcpy(str, p, olen - 4);
                memcpy(str, p, olen - 4);
                str[olen - 4] = 0;
                str[olen - 4] = 0;
                printer(arg, " number = %s", str);
                printer(arg, " number = %s", str);
            }
            }
            printer(arg, ">");
            printer(arg, ">");
            break;
            break;
        }
        }
 
 
    default:
    default:
        break;
        break;
    }
    }
 
 
    for (; len > 0; --len) {
    for (; len > 0; --len) {
        GETCHAR(code, p);
        GETCHAR(code, p);
        printer(arg, " %.2x", code);
        printer(arg, " %.2x", code);
    }
    }
 
 
    return p - pstart;
    return p - pstart;
}
}
 
 
/* received CBCP request */
/* received CBCP request */
static void
static void
cbcp_recvreq(us, pckt, pcktlen)
cbcp_recvreq(us, pckt, pcktlen)
    cbcp_state *us;
    cbcp_state *us;
    char *pckt;
    char *pckt;
    int pcktlen;
    int pcktlen;
{
{
    u_char type, opt_len, delay, addr_type;
    u_char type, opt_len, delay, addr_type;
    char address[256];
    char address[256];
    int len = pcktlen;
    int len = pcktlen;
 
 
    address[0] = 0;
    address[0] = 0;
 
 
    while (len) {
    while (len) {
        syslog(LOG_DEBUG, "length: %d", len);
        syslog(LOG_DEBUG, "length: %d", len);
 
 
        GETCHAR(type, pckt);
        GETCHAR(type, pckt);
        GETCHAR(opt_len, pckt);
        GETCHAR(opt_len, pckt);
 
 
        if (opt_len > 2)
        if (opt_len > 2)
            GETCHAR(delay, pckt);
            GETCHAR(delay, pckt);
 
 
        us->us_allowed |= (1 << type);
        us->us_allowed |= (1 << type);
 
 
        switch(type) {
        switch(type) {
        case CB_CONF_NO:
        case CB_CONF_NO:
            syslog(LOG_DEBUG, "no callback allowed");
            syslog(LOG_DEBUG, "no callback allowed");
            break;
            break;
 
 
        case CB_CONF_USER:
        case CB_CONF_USER:
            syslog(LOG_DEBUG, "user callback allowed");
            syslog(LOG_DEBUG, "user callback allowed");
            if (opt_len > 4) {
            if (opt_len > 4) {
                GETCHAR(addr_type, pckt);
                GETCHAR(addr_type, pckt);
                memcpy(address, pckt, opt_len - 4);
                memcpy(address, pckt, opt_len - 4);
                address[opt_len - 4] = 0;
                address[opt_len - 4] = 0;
                if (address[0])
                if (address[0])
                    syslog(LOG_DEBUG, "address: %s", address);
                    syslog(LOG_DEBUG, "address: %s", address);
            }
            }
            break;
            break;
 
 
        case CB_CONF_ADMIN:
        case CB_CONF_ADMIN:
            syslog(LOG_DEBUG, "user admin defined allowed");
            syslog(LOG_DEBUG, "user admin defined allowed");
            break;
            break;
 
 
        case CB_CONF_LIST:
        case CB_CONF_LIST:
            break;
            break;
        }
        }
        len -= opt_len;
        len -= opt_len;
    }
    }
 
 
    cbcp_resp(us);
    cbcp_resp(us);
}
}
 
 
static void
static void
cbcp_resp(us)
cbcp_resp(us)
    cbcp_state *us;
    cbcp_state *us;
{
{
    u_char cb_type;
    u_char cb_type;
    u_char buf[256];
    u_char buf[256];
    u_char *bufp = buf;
    u_char *bufp = buf;
    int len = 0;
    int len = 0;
 
 
    cb_type = us->us_allowed & us->us_type;
    cb_type = us->us_allowed & us->us_type;
    syslog(LOG_DEBUG, "cbcp_resp cb_type=%d", cb_type);
    syslog(LOG_DEBUG, "cbcp_resp cb_type=%d", cb_type);
 
 
#if 0
#if 0
    if (!cb_type)
    if (!cb_type)
        lcp_down(us->us_unit);
        lcp_down(us->us_unit);
#endif
#endif
 
 
    if (cb_type & ( 1 << CB_CONF_USER ) ) {
    if (cb_type & ( 1 << CB_CONF_USER ) ) {
        syslog(LOG_DEBUG, "cbcp_resp CONF_USER");
        syslog(LOG_DEBUG, "cbcp_resp CONF_USER");
        PUTCHAR(CB_CONF_USER, bufp);
        PUTCHAR(CB_CONF_USER, bufp);
        len = 3 + 1 + strlen(us->us_number) + 1;
        len = 3 + 1 + strlen(us->us_number) + 1;
        PUTCHAR(len , bufp);
        PUTCHAR(len , bufp);
        PUTCHAR(5, bufp); /* delay */
        PUTCHAR(5, bufp); /* delay */
        PUTCHAR(1, bufp);
        PUTCHAR(1, bufp);
        BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
        BCOPY(us->us_number, bufp, strlen(us->us_number) + 1);
        cbcp_send(us, CBCP_RESP, buf, len);
        cbcp_send(us, CBCP_RESP, buf, len);
        return;
        return;
    }
    }
 
 
    if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
    if (cb_type & ( 1 << CB_CONF_ADMIN ) ) {
        syslog(LOG_DEBUG, "cbcp_resp CONF_ADMIN");
        syslog(LOG_DEBUG, "cbcp_resp CONF_ADMIN");
        PUTCHAR(CB_CONF_ADMIN, bufp);
        PUTCHAR(CB_CONF_ADMIN, bufp);
        len = 3 + 1;
        len = 3 + 1;
        PUTCHAR(len , bufp);
        PUTCHAR(len , bufp);
        PUTCHAR(5, bufp); /* delay */
        PUTCHAR(5, bufp); /* delay */
        PUTCHAR(0, bufp);
        PUTCHAR(0, bufp);
        cbcp_send(us, CBCP_RESP, buf, len);
        cbcp_send(us, CBCP_RESP, buf, len);
        return;
        return;
    }
    }
 
 
    if (cb_type & ( 1 << CB_CONF_NO ) ) {
    if (cb_type & ( 1 << CB_CONF_NO ) ) {
        syslog(LOG_DEBUG, "cbcp_resp CONF_NO");
        syslog(LOG_DEBUG, "cbcp_resp CONF_NO");
        PUTCHAR(CB_CONF_NO, bufp);
        PUTCHAR(CB_CONF_NO, bufp);
        len = 3;
        len = 3;
        PUTCHAR(len , bufp);
        PUTCHAR(len , bufp);
        PUTCHAR(0, bufp);
        PUTCHAR(0, bufp);
        cbcp_send(us, CBCP_RESP, buf, len);
        cbcp_send(us, CBCP_RESP, buf, len);
        (*ipcp_protent.open)(us->us_unit);
        (*ipcp_protent.open)(us->us_unit);
        return;
        return;
    }
    }
}
}
 
 
static void
static void
cbcp_send(us, code, buf, len)
cbcp_send(us, code, buf, len)
    cbcp_state *us;
    cbcp_state *us;
    u_char code;
    u_char code;
    u_char *buf;
    u_char *buf;
    int len;
    int len;
{
{
    u_char *outp;
    u_char *outp;
    int outlen;
    int outlen;
 
 
    outp = outpacket_buf;
    outp = outpacket_buf;
 
 
    outlen = 4 + len;
    outlen = 4 + len;
 
 
    MAKEHEADER(outp, PPP_CBCP);
    MAKEHEADER(outp, PPP_CBCP);
 
 
    PUTCHAR(code, outp);
    PUTCHAR(code, outp);
    PUTCHAR(us->us_id, outp);
    PUTCHAR(us->us_id, outp);
    PUTSHORT(outlen, outp);
    PUTSHORT(outlen, outp);
 
 
    if (len)
    if (len)
        BCOPY(buf, outp, len);
        BCOPY(buf, outp, len);
 
 
    output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
    output(us->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
}
}
 
 
static void
static void
cbcp_recvack(us, pckt, len)
cbcp_recvack(us, pckt, len)
    cbcp_state *us;
    cbcp_state *us;
    char *pckt;
    char *pckt;
    int len;
    int len;
{
{
    u_char type, delay, addr_type;
    u_char type, delay, addr_type;
    int opt_len;
    int opt_len;
    char address[256];
    char address[256];
 
 
    if (len) {
    if (len) {
        GETCHAR(type, pckt);
        GETCHAR(type, pckt);
        GETCHAR(opt_len, pckt);
        GETCHAR(opt_len, pckt);
 
 
        if (opt_len > 2)
        if (opt_len > 2)
            GETCHAR(delay, pckt);
            GETCHAR(delay, pckt);
 
 
        if (opt_len > 4) {
        if (opt_len > 4) {
            GETCHAR(addr_type, pckt);
            GETCHAR(addr_type, pckt);
            memcpy(address, pckt, opt_len - 4);
            memcpy(address, pckt, opt_len - 4);
            address[opt_len - 4] = 0;
            address[opt_len - 4] = 0;
            if (address[0])
            if (address[0])
               syslog(LOG_DEBUG, "peer will call: %s", address);
               syslog(LOG_DEBUG, "peer will call: %s", address);
        }
        }
    }
    }
 
 
    cbcp_up(us);
    cbcp_up(us);
}
}
 
 
extern int persist;
extern int persist;
 
 
/* ok peer will do callback */
/* ok peer will do callback */
static void
static void
cbcp_up(us)
cbcp_up(us)
    cbcp_state *us;
    cbcp_state *us;
{
{
    persist = 0;
    persist = 0;
    lcp_close(0, "Call me back, please");
    lcp_close(0, "Call me back, please");
}
}
 
 

powered by: WebSVN 2.1.0

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