pppmisc.c

来自「PPPoE协议在Psos中的实现源代码」· C语言 代码 · 共 396 行

C
396
字号
/************************************************************************/
/*                                                                      */
/*   MODULE: misc.c                                                     */
/*   PRODUCT: pNA+, OpEN TCP?IP PPP Driver                              */
/*   PURPOSE: miscellaneous support routines                            */
/*   DATE:    28 February 1996                                          */
/*                                                                      */
/*----------------------------------------------------------------------*/
/*                                                                      */
/*              Copyright 1996, Integrated Systems Inc.                 */
/*                      ALL RIGHTS RESERVED                             */
/*                                                                      */
/*   This computer program is the property of Integrated Systems Inc.   */
/*   Santa Clara, California, U.S.A. and may not be copied              */
/*   in any form or by any means, whether in part or in whole,          */
/*   except under license expressly granted by Integrated Systems Inc.  */
/*                                                                      */
/*   All copies of this program, whether in part or in whole, and       */
/*   whether modified or not, must display this and all other           */
/*   embedded copyright and ownership notices in full.                  */
/*                                                                      */
/*----------------------------------------------------------------------*/
/*                                                                      */
/* Global Procedures:                                                   */
/*                                                                      */
/************************************************************************/
#include <psos.h>
#include <bsp.h>
#include "bspfuncs.h"
#include "syslog.h"
#include <configs.h>
#include "ppp.h"
#include "pppmisc.h"

extern NODE_CT NodeCfg;
#define SEC2TICKS(sec) (NodeCfg.psosct->kc_ticks2sec * sec)

int memused = 0;
struct callout TimeoutList;
extern struct callout *CalloutPool;
extern struct passwdentry passwdtab[];
extern struct secretentry secrettab[];

extern void (*ppp_error_callout)(unsigned long unit, unsigned long error);

/************************************************************************/
/* timeout: Add a new timeout request to the list.                      */
/*  INPUTS: func - function to call when timeout                        */
/*          arg  - argument to the function                             */
/*          time - timeout period in seconds                            */
/* RETURNS:                                                             */
/* OUTPUTS:                                                             */
/* NOTE(S): This adds a new timeout to the existing list.  A pSOS+      */
/*          timer is armed for the first timeout request on the list.   */
/*          Everytime a pSOS+ timer goes off, all timeout requests are  */
/*          checked.  Then a new pSOS+ timer is armed for the new first */
/*          request on the list, if any.                                */
/************************************************************************/
void ppp_timeout(void (*func)(), caddr_t arg, int time)
{
    struct callout *tp;
    struct callout *tp1;
    unsigned long wait = 0;

    /*--------------------------------------------------------------*/
    /* Get a callout from the pool.                                 */
    /*--------------------------------------------------------------*/
    if ((tp = (struct callout *) DEQUE(&CalloutPool)) == 0)
    {
        syslog(LOG_ERR, "Out of memory in timeout()!");
        return;
    }
    tp->c_arg = arg;
    tp->c_func = func;
    tp1 = TimeoutList.next;
    while (tp1 != &TimeoutList)  /*find the approciate insert node of the doubly linked list*/
    {
        wait += tp1->wait;
        if (time <= wait)
            break;
        tp1 = tp1->next;
    }
    tp1->prev->next = tp;  /*insert the new node*/
    tp->prev = tp1->prev;
    tp->next = tp1;
    tp1->prev = tp;
    if (time > wait)
        tp->wait = time - wait;
    else
    {
        tp->wait = time - (wait - tp1->wait);
        tp1->wait -= tp->wait;
    }
}


/************************************************************************/
/* untimeout: remove a timeout request from the list                    */
/*  INPUTS: func - function to call                                     */
/*          arg  - argument for the function                            */
/* RETURNS:                                                             */
/* OUTPUTS:                                                             */
/* NOTE(S):                                                             */
/************************************************************************/
void ppp_untimeout(void (*func)(), caddr_t arg)
{
    struct callout *tp;

    tp = TimeoutList.next;
    while (tp != &TimeoutList)
    {
        if (tp->c_arg == arg && tp->c_func == func)
        {
            tp->prev->next = tp->next;
            tp->next->prev = tp->prev;
            if (tp->next != &TimeoutList)
                tp->next->wait += tp->wait;
            break;
        }
        tp = tp->next;
    }
    if (tp != &TimeoutList)
    {
        ENQUE(&CalloutPool, tp);
    }
}

/************************************************************************/
/*   alrm : Called when a pSOS+ timer goes off.                         */
/*  INPUTS:                                                             */
/* RETURNS:                                                             */
/* OUTPUTS:                                                             */
/* NOTE(S):                                                             */
/*Check all of the timeout requests,if time equals to 0,perform it!     */ 
/************************************************************************/
void alrm(void)
{
    void (*func)(caddr_t);
    caddr_t arg;
    struct callout *tp, *tp1;

    tp = TimeoutList.next;
    if (tp == &TimeoutList)
        return;
    tp->wait--;
    while (tp != &TimeoutList && tp->wait == 0)
    {
        func = tp->c_func;
        syslog(LOG_INFO, "timer went off %x", func);
        arg = tp->c_arg;
        tp->next->prev = &TimeoutList;
        TimeoutList.next = tp->next;
        tp1 = tp->next;
        ENQUE(&CalloutPool, tp);
        (*func)(arg);
        tp = tp1;
    }
}

#if 0 /*JRC*/
/*
 * bcmp: Byte compare routine
 *
 */

bcmp(char *m1, char *m2, long len)
{
    for (;len; len--)
    {
        if (*m1 != *m2)
            return(1);
        m1++;
        m2++;
    }
    return(0);
}
#endif

/*
 * LOGIN
 */
int
check_passwd(long unit, char * user, long userlen, char * passwd,
             long passwdlen, char ** msg, int * msglen)
{
    struct passwdentry *pp;

    pp = passwdtab;

    for (;pp->user;pp++)
    {
        if ((strlen(pp->user) != userlen) ||
            (strlen(pp->passwd) != passwdlen))
            continue;
        if (bcmp(pp->user, user, userlen))
            continue;
        if (bcmp(pp->passwd, passwd, passwdlen))
            continue;
        *msg = pp->msg;
        *msglen = strlen(pp->msg);
        return(2);
    }
    *msg = "Login Fail";
    *msglen = strlen(*msg);
    return(3);
}

/*
 * LOGOUT:
 */
LOGOUT(long unit)
{
}


/* 
 * GETSECRET:
 */
int
get_secret(int unit, char *name, char * hostname, char * secret,
           long * secret_len, int dummy)
{
    struct secretentry *sp;

    sp = secrettab;
    for (; sp->hostname; sp++)
    {
        if (strcmp(sp->hostname, name))
            continue;
        strcpy(secret, sp->secret);
        *secret_len = strlen(sp->secret);
        return(2);
    }
    return(0);
}

#if 0 /*JRC*/
bcopy(char *m1, char *m2, int len)
{

    for (;len;len--)
    {
        *m1++ = *m2++;
    }
}
#endif


char *pstrchr(char *s, char c)
{
    while(*s && *s != c)
        s++;
    return( *s ? s : (char *) 0);
}

char *pmalloc(size)
int size;
{
    void *seg_addr;

    memused ++;

    if (rn_getseg(0, (unsigned long) size, RN_NOWAIT, 0, &seg_addr))
    {
        k_fatal(0x10000, 0);
        return (char *)0;
    }

    return (char *)seg_addr;
}

void pfree(bp)
char *bp;
{
    memused --;

    if (bp)
        rn_retseg(0, (void *)bp);
}

void pfree1(bp)
char *bp;
{
    unsigned long msg[4];


    if (bp) {
        msg[0] = MSG_FREE;
        msg[1] = (unsigned long) bp;
        q_send(Qid, msg);
        ev_send(Tid, PEV_QEVENT);
    }
}

/*
 * ppp_report_error: Call error callout routine for the channel
*/
void ppp_report_error(long unit, unsigned long error)
{

    /* Is there a error callout  */
    if (ppp_error_callout)
        ppp_error_callout((unsigned long) unit, error);
}

/************************************************************************/
/* insque: Insert an element into the list at the end.                  */
/*                                                                      */
/*      INPUTS: head: Pointer to head of the list                       */
/*              bp: Pointer to the element                              */
/*                                                                      */
/*     RETURNS:                                                         */
/*     OUTPUTS:                                                         */
/*     NOTE(S):                                                         */
/*                                                                      */
/************************************************************************/
void ppp_insque(struct list **head, struct list *bp)
{
    unsigned long m;

    m = splx(MAX_ILEV);  /*define MAX_ILEV 6 in bspfuncs.h*/

    if (*head == 0)
    {
        *head = bp;
        bp->next = bp;
        bp->prev = bp;
    }
    else
    {
        (*head)->prev->next = bp;
        bp->prev = (*head)->prev;
        (*head)->prev = bp;
        bp->next = *head;
    }
    splx(m);
}


/************************************************************************/
/* remque: Remove a node from the queue at the front.                   */
/*                                                                      */
/*      INPUTS: var1:                                                   */
/*                                                                      */
/*     RETURNS: buffer pointer or 0                                     */
/*     OUTPUTS:                                                         */
/*     NOTE(S):                                                         */
/*                                                                      */
/************************************************************************/
struct list *ppp_remque(struct list **head)
{
    struct list *bp;
    unsigned long m;

    m = splx(MAX_ILEV);

    bp = *head;
    if (!bp)
    {
        splx(m);
        return((struct list *) 0);
    }

    if (bp == bp->next)
    {
        *head = (struct list *) 0;
    }
    else
    {
        bp->prev->next = bp->next;
        bp->next->prev = bp->prev;
        *head = bp->next;
    }
    splx(m);
    return(bp);
}

#if 0 /*JRC*/
/************************************************************************/
/* bzero: Zero out the memory                                           */
/*                                                                      */
/*      INPUTS: cp: Pointer to the buffer                               */
/*              size: Size of the buffer                                */
/*                                                                      */
/*     RETURNS: NONE                                                    */
/*     OUTPUTS:                                                         */
/*     NOTE(S):                                                         */
/*                                                                      */
/************************************************************************/
void bzero(char *cp, long size)
{
    for (; size > 0; size--)
        *cp++ = 0;
}
#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?