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

📄 pppdebug.c

📁 PPP协议C语言源代码
💻 C
字号:

#include <string.h>
#include <net.h>
#include <local.h>
#include <support.h>
#include "ppp.h"

/* The maximum number of bytes printed for a packet (24 per line) */
#define MAX_PPP_PRINT 120

#if NTRACE >= 5

#define STXT_SZ 35

static char *_code[] = { /* LCP or IPCP */
    "Vend-Ext", "Conf-Req", "Conf-Ack", "Conf-Nak", "Conf-Rej", "Term-Req",
    "Term-Ack", "Code-Rej", "Prot-Rej", "Echo-Req", "Echo-Rep", "Disc-Req"
};
static char *_pcode[] = {/* PAP */
    "", "Auth-Req", "Auth-Ack", "Auth-Ack"
};
static char *_ccode[] = {/* CHAP or MS-CHAP */
    "", "Challenge", "Response", "Success", "Failure"
};

/*
** * * * * * *
** setStateText()    Print the PPP state into a string
**
** static void setStateText(unsigned char state, char auth, char *s);
**
** PARAMETERS:
**   (in) unsigned char state   The PPP state value
**   (in) char auth             The PPP authentication state value
**   (in/out) char *s           The character string to format into
**
** DESCRIPTION:
**  This function will initialize the character string parameter with an
**  English representation of the state.
**
** * * * * * *
*/
static void setStateText(unsigned char state, char auth, char *s)
{
    unsigned short i, j;

    if (state == PPPclsd) {
        strcpy(s, "Closed/Stopped");
        return;
    }
    else if (state == PPPclsng) {
        strcpy(s, "Closing/Stopping");
        return;
    }

    memset(s, 0x20, STXT_SZ);   /* Use spaces instead of null */

    j = i = 0;
    if ((state & PPPopen) == PPPopen)
        i = 0xf;
    else if ((state & LCPopen) == LCPopen)
        if (auth) {
            j = 0;
            strncpy(s, "[Peer", 5);
            for (i = (auth & 0xff); i; i>>=4) {
                switch (i & 0x7) {
                    case 1: strncpy(s+j+6, "PAP]",4), j+=10; break;
                    case 2: strncpy(s+j+6, "CHAP]",5), j+=11; break;
                    case 4: strncpy(s+j+6, "MS-CHAP]",8), j+=14;
                    default: break;
                }
                strncpy(s+j, "[Host", 5);
            }
            strcpy(s+j, "");
            return;
        } else
            strncpy(s, "IPCP", 4), i = (state & 0xf0)>>4, j = 5;
    else
        strncpy(s, "LCP", 3), i = state, j = 4;

    switch (i) {    /* Null is added at the end */
        case 0:   strcpy(s+j, "Stopped"); break;
        case 0x1: strcpy(s+j, "Req-Sent"); break;
        case 0x3: strcpy(s+j, "Ack-Rcvd"); break;
        case 0xc: strcpy(s+j, "Ack-Sent"); break;
        case 0xd: strcpy(s+j, "(Ack,Req)-Sent"); break;
        case 0xf: strcpy(s+j, "Open"); break;
        default:  strcpy(s+j, "Invalid"); break;
    }
}

static char *cil_a = " %s [ID %02x][Length %04x]";
#define cil(c,i,l) Nprintf(cil_a, c, i, l)
#endif

/*
** * * * * * *
** pppDebug()   Print a PPP frame in English
**
** void pppDebug(int X, MESS *mess);
**
** PARAMETERS:
**  (in) int X                  1 for outgoing, 0 for incoming
**  (in) MESS *mess             The PPP frame
**
** DESCRIPTION:
**  NTRACE >= 3
**      Print the PPP frame with some status information.
**  NTRACE >= 5
**      Print the PPP frame in English.
**
** * * * * * *
*/
void pppDebug(int X, MESS *mess)
{
    struct NET *netp;
    unsigned short prot, i1, i2;
    unsigned char *cp, code;
#if NTRACE >= 5
    unsigned short size;
    unsigned char id;
    char StateText[STXT_SZ];
#endif

    (void)X;
    (void)mess;

    netp = &nets[mess->netno];

    cp = (unsigned char *)mess + MESSH_SZ + LHDRSZ;
    code = *cp;

    prot = NC2(PH(mess)->protocol);
#if NTRACE >= 5
    id = cp[1];
    size = (int)cp[3] + (int)(cp[2]<<8);
    setStateText(netp->state, netp->hw.opt4, StateText);
    Nprintf("%s TimeMS %lu, NetNo %d,", (X)?"PPP RX:":"PPP TX:",
        TimeMS(), mess->netno);
    Nprintf(" State %s (%02x)[%d]", StateText, netp->state, netp->hw.opt7);
#else
    Nprintf("%s T%lu N%d P%04x C%d S%02x[%d]", (X)?"PPP RX:":"PPP TX:",
        TimeMS(), mess->netno, prot, (unsigned char)code, netp->state,
        netp->hw.opt7);
#endif

    if ((i2 = mess->mlen - MESSH_SZ - LHDRSZ) > MAX_PPP_PRINT) {
        i2 = MAX_PPP_PRINT;
        Nprintf("  DATA LENGTH %d TRUNCATED TO %d.", i2, MAX_PPP_PRINT);
    }
    for (i1=0; i1 < i2;) {
        if ((i1 % 24) == 0)
            Nprintf("\n ");
        Nprintf(" %02x", cp[i1++]);
    }

#if NTRACE < 5
    Nputchr('\n');
    return;
#else
    Nprintf("\n  ");
    cp += 4;
    if (prot == PROTlcp) {
        Nprintf("LCP");
        if (code > LCPdisc_req)
            goto unknowncode;
        cil(_code[code], id, size);
        switch(code) {
            case LCPvend_ext:
                break;
            case LCPconf_req:
            case LCPconf_ack:
            case LCPconf_nak:
            case LCPconf_rej:
                while ((size-4) > 0) {
                    Nprintf("\n   ");
                    switch(cp[0]) {
                        case LCPopt_MRU:
                            Nprintf("[MRU %02x%02x]", cp[2], cp[3]);
                            break;
                        case LCPopt_ASYC:
                            Nprintf("[ASYNC %02x%02x%02x%02x]",
                            cp[2],cp[3],cp[4],cp[5]);
                            break;
                        case LCPopt_AUTH:
                            Nprintf("[%02x%02x Authentication", cp[2], cp[3]);
                            if (cp[1] != 4)
                                Nprintf(", %02x Algorithm", cp[4]);
                            Nprintf("]");
                            break;
                        case LCPopt_LQPT:
                            Nprintf("[LQR %02x%02x]", cp[2], cp[3]);
                            break;
                        case LCPopt_MNUM:
                            Nprintf("[Magic Number %02x%02x%02x%02x]",
                                cp[2],cp[3],cp[4],cp[5]);
                            break;
                        case LCPopt_PCMP:
                            Nprintf("[Protocol Compression]");
                            break;
                        case LCPopt_ACMP:
                            Nprintf("[Address Control Compression]");
                            break;
                        case LCPopt_MRRU:
                            Nprintf("[MP MRRU %02x%02x]", cp[2], cp[3]);
                            break;
                        case LCPopt_SNHF:
                            Nprintf("[MP Short Header]");
                            break;
                        case LCPopt_ENDD:
                            Nprintf("[MP Endpoint Descriminator: '");
                            for (i1 = 3; i1 < cp[1]; i1++)
                                if (cp[i1] < 0x20 || cp[i1] > 0x7e)
                                    Nputchr('.');
                                else
                                    Nprintf("%c", cp[i1]);
                            Nprintf("']");
                            break;
                        default:
                            Nprintf("[Unknown Option %02x]", cp[0]);
                            break;
                    }
                    if (cp[1] == 0)
                        goto badsize;
                    size -= cp[1];
                    cp += cp[1];
                }
                break;
            case LCPterm_req:
            case LCPterm_ack:
               goto message;
            case LCPcode_rej:
            case LCPprot_rej:
                if (code == LCPprot_rej)
                    Nprintf("\n    [Protocol %02x%02x]",
                        *cp, *(cp+1));
            case LCPecho_req:
            case LCPecho_rep:
            case LCPdisc_req:
            default:
                break;
        }
    }
    else if (prot == PROTpap) {
        Nprintf("PAP");
        if (code == 0 || code > PAPauth_nak)
            goto unknowncode;
        cil(_pcode[code], id, size);
        switch (code) {
            case PAPauth_req:
                Nprintf("\n    [ID length %02x][ID ",cp[0]);
                for (i1=1; i1 < cp[0]+1; i1++)
                    Nprintf("%c",cp[i1]);
                Nprintf("]\n    [PW length %02x][PW ",cp[i1]);
                for (cp += i1, i1=1; i1 < cp[0]+1; i1++)
                    Nprintf("%c",cp[i1]);
                Nputchr(']');
                break;
            case PAPauth_ack:
            case PAPauth_nak:
                size--, cp++;
                goto message;
            default:
                break;
        }
    }
    else if (prot == PROTchap) {
        Nprintf("CHAP");
        if (code == 0 || code > CHAPfailure)
            goto unknowncode;
        cil(_ccode[code], id, size);
        switch (code) {
            case CHAPchallenge:
            case CHAPresponse:
                Nprintf("\n    [Value size %02x]\n    [Value", cp[0]);
                for (i1 = 0; i1 < cp[0]; i1++) {
                    if (i1 && i1 % 17 == 0)
                        Nprintf("\n          ");
                    Nprintf(" %02x",cp[i1+1]);
                }
                Nprintf("]\n    [Name ");
                for (i1++; i1 < size-4; i1++)
                    Nprintf("%c",cp[i1]);
                Nputchr(']');
                break;
            case CHAPsuccess:
            case CHAPfailure:
                goto message;
        }
    }
    else if (prot == PROTipcp) {
        Nprintf("IPCP");
        if (code > 0 && code <= IPCPcode_rej)
            cil(_code[code], id, size);
        switch(code) {
            case IPCPconf_req:
            case IPCPconf_ack:
            case IPCPconf_nak:
            case IPCPconf_rej:
                while ((size-4) > 0) {
                    Nprintf("\n   ");
                    switch(cp[0]) {
                        case XIPCPopt_IPAD:
                            Nprintf("[IP Address (bad)]");
                            break;
                        case IPCPopt_IPCP:
                            Nprintf("[IP Compression Protocol %02x%02x ",
                                cp[2], cp[3]);
                            Nprintf("(Maximum Slots %02x)(Compress Slot ID %02x)]",
                                cp[4], cp[5]);
                            break;
                        case IPCPopt_IPAD:
                            Nprintf("[IP Address %d.%d.%d.%d]",
                                cp[2], cp[3], cp[4], cp[5]);
                            break;
                        default:
                            Nprintf("[Unknown Option %02x]", cp[0]);
                            break;
                    }
                    if (cp[1] == 0) {
badsize:
                        Nprintf("[SIZE VALUE IS 0!]\n");
                        return;
                    }
                    size -= cp[1];
                    cp += cp[1];
                }
                break;
            case IPCPterm_req:
            case IPCPterm_ack:
message:
                Nprintf("\n    [Message: ");
                for (i1 = 0; i1 < size-4; i1++)
                    if (cp[i1] < 0x20 || cp[i1] > 0x7e)
                        Nputchr('.');
                    else
                        Nprintf("%c", cp[i1]);
                Nputchr(']');
            case IPCPcode_rej:
                break;
            default:
unknowncode:
                cil("Unknown code", id, size);
                break;
        }
    }
    else
        Nprintf("Unknown Protocol %04x", prot);

    Nprintf("\n");
#endif
}

⌨️ 快捷键说明

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