⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ppp.c

📁 PPP协议C语言源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
                strcpy((char *)cp2+1, netp->hw.userid);
                cp2 += *cp2 + 1;
                *cp2 = (unsigned char)strlen(netp->hw.passwd);
                strcpy((char *)cp2+1, netp->hw.passwd);
                cp2 += *cp2 + 1;
                goto write;
            }
            else
#endif



#if REQAUTH == PROTchap
           /* If host requested CHAP, we send challenge */
            if (netp->hw.opt4 & (AUTHhchp|AUTHhmsc))
            {
                if (netp->hw.opt4 & AUTHhwat)
                    return;
#if AUTHENT & 1
old_chap:
#endif
                netp->hw.opt4 |= (char)AUTHhwat;
                PH(mess)->protocol = NC2(PROTchap);
                *cp = CHAPchallenge;           /* Create challenge */
                *cp2++ = 8;                         /* Length == 8 */
                netp->hw.ul2 = TimeMS();            /* Value == Time() */
                *cp2++ = strlen(netp->hw.userid);   /* Element: 0 */
                Nmemcpy((char *)cp2, (char *)&netp->hw.ul2, 4);
                cp2 += 4;                           /*          1 - 4 */
                *cp2++ = (netp->hw.ul2 >> 3);       /*          5 */
                *cp2++ = (netp->hw.ul2 >> 10);      /*          6 */
                *cp2++ = (netp->hw.ul2 >> 15);      /*          7 */
#if AUTH_ALG == CHAPalg_MD5
                strcpy((char *)cp2, netp->hw.userid);    /* Add name */
                cp2 += strlen(netp->hw.userid);
#endif
                goto write;
            }
            else
#endif




#if AUTHENT & 6 || REQAUTH == PROTpap
            if (netp->hw.opt4 & AUTHpini)
                return;
            else
#endif
            if ((netp->state & IPCPtxREQ) == 0) {
                netp->state |= IPCPtxREQ;
                netp->state &= ~IPCPrxACK;
                PH(mess)->protocol = NC2(PROTipcp);
                *cp = IPCPconf_req;
                *cp2++ = IPCPopt_IPAD;
                *cp2++ = 2 + Iid_SZ;
                Nmemcpy(cp2, netp->haddr.c, Iid_SZ);
                cp2 += Iid_SZ;
#if VJ
                if (TXslots[mess->netno]) {
                    *cp2++ = IPCPopt_IPCP;
                    *cp2++ = 6;
                    *cp2++ = 0;
                    *cp2++ = 0x2d;
                    *cp2++ = TXslots[mess->netno];
                    *cp2++ = TXixcomp[mess->netno];
                }
#endif
                goto write;
            }
        }
        else if ((netp->state & LCPtxREQ) == 0) {
            netp->state |= LCPtxREQ;
            netp->state &= ~LCPrxACK;
            PH(mess)->protocol = NC2(PROTlcp);
            *cp = LCPconf_req;

           /* Loop though possible configure options (i1 = type) */
            for (i1=0; i1 < 16; i1++) {
               /* if the option is selected, include it in conf-req */
                if (netp->hw.locopts & (1<<i1)) {
                    *cp2++ = i1;
                    switch (i1) {
                    default:
                        cp2 -= 1;   /* Not supported; Undo operation */
                        break;
#if PPP_MRU
                    case LCPopt_MRU:
                        *cp2++ = 4;
                        *cp2++ = netp->maxblo >> 8;
                        *cp2++ = netp->maxblo;
                        break;
#endif
#if ASYNC
                    case LCPopt_ASYC:
                        *cp2++ = 6;
                        Nmemcpy(cp2, (char *)&netp->hw.ul1, 4);
                        cp2 += 4;
                        break;
#endif
#if REQAUTH
                    case LCPopt_AUTH:
#if REQAUTH == PROTchap
                        *cp2++ = 5;
#else
                        *cp2++ = 4;
#endif
                        *cp2++ = (unsigned char)(REQAUTH>>8);
                        *cp2++ = (unsigned char)REQAUTH;
#if REQAUTH == PROTchap
                        *cp2++ = AUTH_ALG;
#endif
                        break;
#endif
#if defined(LQRP) && QUALITY
                    case LCPopt_LQPT:
                        *cp2++ = 8;
                        *cp2++ = QUALITY >> 8;
                        *cp2++ = QUALITY;
                        Nmemcpy(cp2, (unsigned char *)netp->peerLQRPms, 4);
                        cp2 += 4;
                        break;
#endif
#if MAGICNUM
                    case LCPopt_MNUM:
                        *cp2++ = 6;
                        netp->hw.mnum = TimeMS();
                        Nmemcpy(cp2, (char *)&netp->hw.mnum, 4);
                        cp2 += 4;
                        break;
#endif
#if COMPRESSION & 1
                    case LCPopt_PCMP:
                    case LCPopt_ACMP:
                        *cp2++ = 2;
                        break;
#endif
                    }
                }
            }
/* MP is not included in for loop because it's options are > 16 */
#if MP
            if (netp->hw.opt5 & MPmrru) {
               /* This option also initiates MP if accepted */
                *cp2++ = LCPopt_MRRU;
                *cp2++ = 4;
                *cp2++ = (MAXBUF - MESSH_SZ - LHDRSZ) >> 8;
                *cp2++ = MAXBUF - MESSH_SZ - LHDRSZ;
            }
            if (netp->hw.opt5 & MPsnhf) {
                *cp2++ = LCPopt_SNHF;
                *cp2++ = 2;
            }
            if (netp->hw.opt5 & MPendd) {
               /*
                ** The endpoint descriminator used by yfnet is the locally
                ** assigned localhostname.  To use a different class, change
                ** the following two lines to provide mpFindBundle with a new
                ** class, address length and address.  In addition, the
                ** packet must be formatted with the following:
                **
                **      one octet for the class
                **      one octet for the address length (0 to 20)
                **      the address (in 0 to 20 octets)
                **
                ** The pointer, cp2, must point to the the octet after the
                ** last octet in the packet upon completion.
                */
                i1 = (int)strlen(localhostname);
                if (mpFindBundle(mess->netno, ENDDcf_LCAS, i1, localhostname) >= 0) {
                    *cp2++ = LCPopt_ENDD;
                    *cp2++ = i1 + 3;
                    *cp2++ = ENDDcf_LCAS;
                    Nmemcpy((char *)cp2, localhostname, i1);
                    cp2 += i1;
                }
            }
#endif

write:
            cp[1] = ++netp->hw.opt1;                /* Set ID field */
            i1 = cp2 - cp;                          /* Get length */
            cp[2] = i1 >> 8, cp[3] = i1;            /* Set length field */
            netp->hw.opt7--;                        /* Restart counter-- */
            mess->mlen = i1 + MESSH_SZ + LHDRSZ;    /* Buffer tx length */
            pppWrite(mess);                         /* TX negotiation */
#if REQAUTH == PROTchap && AUTHENT & 1 && 0
/*
** This isn't included unless there is trouble when both host's are
** authenticating with the remote host using PAP and us using CHAP.
** This is very unlikely to occur.  If you notice that PAP is being
** performed to the exclusion of CHAP, preprocess this in.
*/
            if ((netp->state & LCPopen) == LCPopen &&
                    netp->hw.opt4 & (AUTHhchp|AUTHhmsc) &&
                    netp->hw.opt4 & AUTHhwat == 0)
                goto old_chap;
#endif
        }
    }
}


/*
** * * * * * *
** pppForceUP()   Bring the link up if it's down
**
** static int pppForceUp(int netno);
**
** PARAMETERS:
**   (in) netno    The yfnet network number (the interface to bring up)
**
** RETURNS:
**   1             PPP is open
**   0             PPP is working to be open
**
** DESCRIPTION:
**   Thnis function forces PPP to bring itself up rather than being
**   passive.  It enacts an active open.  Do not call if a passive open
**   is being stimulated.
**
** * * * * * *
*/
static int pppForceUp(int netno)
{
    struct NET *netp;
    MESS *mess;

    netp = &nets[netno];
    if (netp->state == PPPopen)
        return 1;   /* PPP is already up */

    if (netp->state == PPPclsd || netp->state == PPPclsng) {
#if DIALD
        if (netconf[netp->confix].flags & DIAL) {
            if (!IS_MODEM_DONE(netp) ||
                !IS_MODEM_DIALOUT(netp) ||
                netp->state == PPPclsng)
            {
                netp->state = PPPclsd;
                MODEM_DIALOUT(netno);
                YIELD();
                return 0;
            }
        }
#endif
        pppStart(netno, 1); /* Make PPP ready for starting actively */
        if ((mess = Ngetbuf()) != 0) {
            mess->netno = netno;
            pppNegotiate(mess);
            mess->id = bALLOC;
            Nrelbuf(mess);
        }
        else
            netp->state = LCPtxREQ; /* Wait for time-out to resend */

        YIELD();
    }
    return 0;   /* PPP is trying to get up */
}


/*
** * * * * * *
** pppForceDown()   Bring the link down if it's up
**
** static int pppForceDown(int netno);
**
** PARAMETERS:
**   (in) netno    The yfnet network number (the interface to bring up)
**
** RETURNS:
**   1             PPP is down
**   0             PPP is working to be down
**
** DESCRIPTION:
**   This function forces PPP to bring itself down regardless.
**
** * * * * * *
*/
static int pppForceDown(int netno)
{
    struct NET *netp;

    netp = &nets[netno];
    if (netp->state == PPPclsng)
        return 0;

    if (netp->state == PPPclsd) {
#if DIALD
        if (netconf[netp->confix].flags & DIAL) {
           /* Unless we're passively waiting, dial down */
            if (IS_MODEM_DONE(netp) || !IS_MODEM_DIALIN(netp)) {
                MODEM_DIALDOWN(netno);
                return 0;
            }
        }
        else
#endif
        {
            WAITNOMORE(SIG_WN(netno));
        }
        return 1;   /* PPP is already down or going down */
    }

   /* If the link is open, propegate a close */
#if NTRACE
    Nprintf("PPP: Closing network %d\n", netno);
#endif
    netp->state = PPPclsng;
    netp->hw.opt7 = MAXTERM;
    pppDQ(netno);

    return 0;   /* PPP is trying to get down */
}


/*
** * * * * * *
** pppTimeout()  State machine driver for PPP.
**
** int pppTimeout(int netno);
**
** PARAMETERS:
**   (in) netno                Interface index into nets structure
**
** RETURNS:
**  -1                         PPP closed
**   0                         PPP working to be open/closed
**   1                         PPP open
**
** DESCRIPTION:
**   This function drives the state machine for PPP.
**
** * * * * * *
*/
int pppTimeout(int netno)
{
    char *cp;
    unsigned long ul1, ul2;
    int prevstate;
    MESS *mess;
    struct NET *netp;

    netp = &nets[netno];
    ul1 = TimeMS();
#if ECHO_TOUTMS
   /* Send an echo-req if necessary */
    if ((netp->state & LCPopen) == LCPopen) {
        if ((ul1-netp->hw.echo_time) > ECHO_TOUTMS) {
            if (netp->hw.opt6 > ECHO_RETRIES) {
#if NTRACE >= 3
                Nprintf("PPP: Echo request counter expired\n");
#endif
                goto term;
            }
            if ((mess = Ngetbuf()) == 0)
                return 0;
            netp->hw.opt6++;
            netp->hw.echo_time = ul1;
            mess->netno = netno;
            PH(mess)->protocol = NC2(PROTlcp);          /* Protocol */
            cp = (char *)mess + MESSH_SZ + LHDRSZ;
            *cp++ = LCPecho_req;                        /* Code */
            *cp++ = ++netp->hw.opt1;                    /* ID */
            *cp++ = 0; *cp++ = 8;                       /* Length */
#if MAGICNUM
            if (netp->hw.locopts & (1<<LCPopt_MNUM))    /* MagicNum */
                Nmemcpy(cp, (char *)&netp->hw.mnum, 4);
            else
#endif
                memset(cp, 0, 4);
            mess->mlen = MESSH_SZ + LHDRSZ + 8;
            pppWrite(mess);
            mess->id = bALLOC;
            Nrelbuf(mess);
        }
    }
#endif

    if (netp->state == PPPhold)
        return 0;

   /* If link is open, check for idle if necessary and return 1 */
    if (netp->state == PPPopen) {
#if IDLE_TOUT
        if (((ul1-netp->hw.idle_time)>>10) > IDLE_TOUT) {
#if NTRACE >= 3
            Nprintf("PPP: Idle timeout expired\n");
#endif
            goto term;
        }
#endif
        return 1;       /* Link is open */
    }

   /* If PPP is closed, do dialing if necessary and return -1 */
    if (netp->state == PPPclsd) {
#if DIALD
       /* If there is dialing to do */
        if (!IS_MODEM_DONE(netp) && !IS_MODEM_NONE(netp)) {
           /* If still working */
            if ((prevstate = MODEM_PROCESS(netno)) == 0)

⌨️ 快捷键说明

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