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

📄 vj.c

📁 PPP协议C语言源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
   /* Sequence, ACK, URG: reserved for special use Echoed terminal traffic */
    case BITurg|BITwin|BITseq:                  /* 0x0b */
   /*
    ** sequence, ACK, window, URG: reserved for special use
    ** Non-Echoed terminal traffic 
    ** Unidirectional data trasfer
    */
    case BITurg|BITwin|BITack|BITseq:           /* 0x0f */
        goto uncompr;   /* send uncompressed if special case */
   /* Sequence, ACK: special short notation used */
    case BITack|BITseq:                         /* 0x0c */
        if (deltaS == deltaA && deltaS ==
            NC2(((unsigned short *)ihdrp2)[TLEN]) - hlen) {
            changeflag = BITurg|BITwin|BITseq;  /* 0x0b */
            cp = changes;
        }
        break;
   /* Sequence: special short notation used */
    case BITseq:                                /* 0x08 */
        if (deltaS == NC2(((unsigned short *)ihdrp2)[TLEN]) - hlen) {
            changeflag = BITurg|BITwin|BITack|BITseq;   /* 0x0f */
            cp = changes;
        }
    default:
        break;
    }

   /* Encode fragment ID and push flag */
    us1 = NC2(((unsigned short *)ihdrp)[FRID]) -
          NC2(((unsigned short *)ihdrp2)[FRID]);
    if (us1 != 1) {
        ENCODEZ(us1, cp);
        changeflag |= BITid;                /* 0x20 */
    }
    if (thdrp[FLAGS] & S_PSH)
        changeflag |= BITpsh;               /* 0x10 */
    Nmemcpy(CONNstatep->TCPIPhdr, ihdrp, hlen);

   /*
    ** Change the packet.  This can't be done in with the orginal
    ** message buffer because the original packet may still be
    ** in the wait-for-ack queue.
    */
    if ((mp2 = Ngetbuf()) == 0)         /* Get another buffer */
        goto uncompr;

    mp2->netno = mess->netno;           /* Copy over network number */
    mp2->confix = mess->confix;         /* Index of host in netconf[] */
    mp2->offset = MESSH_SZ + LHDRSZ;    /* point to message */
    mp2->id = bRELEASE;                 /* Buffer can be released */
    i1 = cp - changes;
    us1 = ((unsigned short *)thdrp)[T_CHKSUM];
    cp = (unsigned char *)mp2 + MESSH_SZ + LHDRSZ;
    if (TXixcomp[netno] == 0 || LINEstatep->TXid != CONNstatep->id) {
        LINEstatep->TXid = CONNstatep->id;
        *cp++ = changeflag | BITcon;    /* 0x40 */
        *cp++ = CONNstatep->id;
        mp2->mlen = mess->mlen + 4 + i1 - hlen; 
    }
    else {
        *cp++ = changeflag;
        mp2->mlen = mess->mlen + 3 + i1 - hlen; 
    }
    *cp++ = (NC2(us1)) >> 8;
    *cp++ = NC2(us1);

    Nmemcpy((char *)cp, (char *)changes, i1);
    thlen = MESSH_SZ + LHDRSZ + hlen;
    Nmemcpy((char *)cp+i1, (char *)mess+thlen, mess->mlen-thlen);
    *mpp = mp2;                         /* buffer to send */

   /* Handle transmission of packet */
    mess->offset = boTXDONE;            /* Indicate it was trasmitted */
    if (mess->id <= bWACK) {
        if (mess->id == bRELEASE) {     /* If old buffer can be released */
            mess->id = bALLOC;          /*  then release it */
            Nrelbuf(mess);
        }
    }
    else
    {
        WAITNOMORE(SIG_WN(netno));
    }

    return NC2(PROTcomp);               /* PACKET WAS COMPRESSED 0x002d */

/* We can't compress, but we save the TCP/IP header */
uncompr:
    Nmemcpy(CONNstatep->TCPIPhdr, ihdrp, hlen);
    ihdrp[PROT] = LINEstatep->TXid = CONNstatep->id;
    return NC2(PROTuncomp);             /* 0x002f */

/* Packet was not compressible, send as TYPE_IP */
nocompr:
    return NC2(PROTip);                 /* 0x0021 */
}


/*
** * * * * * *
** vjScreen()   Attempt to uncompress a PPP frame
**
** int vjScreen(MESS *mess, unsigned short protoc)
**
** PARAMETERS:
**   (in/out) MESS *mess        A pointer to a frame to decompress
**   (in) unsigned short protoc The protocol field of the frame
**
** RETURNS:
**   0                          Success
**  -1                          Failure
**
** DESCRIPTION:
**   This function will input a PPP frame and attempt to decompress it
**   using Van Jacobson decompression.  If it can be done (or if the
**   header can be used to start a decompression sequence), 0 is
**   returned and the frame is potentially modified.  Otherwise
**   -1 is returned and the frame is unmodified.
**
** * * * * * *
**
** MODIFICATION HISTORY:
**
**   15-JUL-1999  BSK  Chg: Taken from ppp.c
**
** * * * * * *
*/
int vjScreen(MESS *mess, unsigned short protoc)
{
    int netno;
    unsigned int ui1, ui2, hlen, ihlen;
    unsigned short us1;
    unsigned char changeflag, *cp, *cp2;
    unsigned char *ihdrp;
    unsigned char *thdrp;
    struct LINEstate *LINEstatep;
    struct CONNstate *CONNstatep;
    union {unsigned char c[4]; short s[2]; long l;} UL3;

    netno = mess->netno;
    LINEstatep = &LINEstate[netno];

   /* Handle uncompressed packet; just save the headers */
    if (protoc == NC2(PROTuncomp)) {
        ihdrp = (unsigned char *)mess + MESSH_SZ + LHDRSZ;
        hlen = (ihdrp[VERLEN] & 0xf) * 4;
        thdrp = (unsigned char *)ihdrp + hlen;
        hlen += (thdrp[HDRLEN] >> 4) << 2;
        ui1 = *((unsigned char *)mess + MESSH_SZ + LHDRSZ + 9);
        *((char *)mess + MESSH_SZ + LHDRSZ + 9) = 6;
        if (ui1 > RXslots[netno]) {
            LINEstatep->flags |= 0x01;
            goto ret9;
        }
        LINEstatep->RXid = (unsigned char)ui1;
        CONNstatep = &LINEstatep->RXstate[ui1];
        LINEstatep->flags &= ~0x01;
        Nmemcpy(CONNstatep->TCPIPhdr, ihdrp, hlen);
        ((unsigned short *)CONNstatep->TCPIPhdr)[CHKSUM] = 0;
        return 0;
    }

   /* This is code for a truly compressed packet */
    cp2 = cp = (unsigned char *)mess + MESSH_SZ + LHDRSZ;
    changeflag = *cp++;
    if (changeflag & BITcon) {            /* 0x40 */
        if (*cp > RXslots[netno])
            goto ret9;
        LINEstatep->flags &= ~0x01;
        LINEstatep->RXid = *cp++;
    }
    else {
        if (LINEstatep->flags & 0x01)
            goto ret9;
    }

    CONNstatep = &LINEstatep->RXstate[LINEstatep->RXid];
    ihdrp = (unsigned char *)CONNstatep->TCPIPhdr;
    ihlen = (ihdrp[VERLEN] & 0xf) * 4;
    thdrp = (unsigned char *)(CONNstatep->TCPIPhdr + ihlen);
    hlen = ihlen + ((thdrp[HDRLEN] >> 4) << 2);
    thdrp[2*T_CHKSUM] = *cp++;
    thdrp[2*T_CHKSUM+1] = *cp++;
    thdrp[FLAGS] = BITpsh;                /* 0x10 */
    if (changeflag & BITpsh)              /* 0x10 */
        thdrp[FLAGS] |= S_PSH;

    switch (changeflag & (BITurg|BITwin|BITack|BITseq)) {   /* 0x0f */
    case BITurg|BITwin|BITseq:                          /* 0x0b */
        ui1 = NC2(((unsigned short *)ihdrp)[TLEN]) - hlen;
        GETLONGp(UL3, thdrp, ACKNO);
        UL3.l += ui1;
        PUTLONGp(UL3, thdrp, ACKNO);
        GETLONGp(UL3, thdrp, SEQNO);
        UL3.l += ui1;
        PUTLONGp(UL3, thdrp, SEQNO);
        break;
    case BITurg|BITwin|BITack|BITseq:                   /* 0x0f */
        ui1 = NC2(((unsigned short *)ihdrp)[TLEN]) - hlen;
        GETLONGp(UL3, thdrp, SEQNO);
        UL3.l += ui1;
        PUTLONGp(UL3, thdrp, SEQNO);
        break;
    default:
        if (changeflag & BITurg) {         /* 0x01 */
            thdrp[FLAGS] |= S_URG;
            DECODE(((unsigned short *)thdrp)[URGP], cp);
        }
        if (changeflag & BITwin) {        /* 0x02 */
            DECODE(us1, cp);
            us1 += NC2(((unsigned short *)thdrp)[WINDOW]);
            ((unsigned short *)thdrp)[WINDOW] = NC2(us1);
        }
        if (changeflag & BITack) {        /* 0x04 */
            DECODE(us1, cp);
            GETLONGp(UL3, thdrp, ACKNO);
            UL3.l += us1;
            PUTLONGp(UL3, thdrp, ACKNO);
        }
        if (changeflag & BITseq) {        /* 0x08 */
            DECODE(us1, cp);
            GETLONGp(UL3, thdrp, SEQNO);
            UL3.l += us1;
            PUTLONGp(UL3, thdrp, SEQNO);
        }
    }
    if (changeflag & BITid) {             /* 0x20 */
        DECODE(us1, cp);
        us1 += NC2(((unsigned short *)ihdrp)[FRID]);
        ((unsigned short *)ihdrp)[FRID] = NC2(us1);
    }
    else {
        us1 = NC2(((unsigned short *)ihdrp)[FRID]) + 1;
        ((unsigned short *)ihdrp)[FRID] = NC2(us1);
    }

   /*
    ** Copy the message up so we'll have space for the TCP/IP header.  This
    ** is sadly quite slow, but ANSI C does not have a copy routine for this
    ** situation.   In any case, compression is not recommended for slow
    ** processors or long packets.
    */
    ui1 = (unsigned int)(cp - cp2);
    ui2 = hlen - ui1;
    if (ui2 >= 128)
        goto ret9;
    ui1 = mess->mlen - MESSH_SZ - LHDRSZ - ui1;
    cp = (unsigned char *)mess + mess->mlen - 1;
    mess->mlen += ui2;
    if (mess->mlen >= MAXBUF)
        goto ret9;
    cp2 = (unsigned char *)mess + mess->mlen - 1;
    ui2 = ui1 + ihlen;
    while (ui1--)
        *cp2-- = *cp--;

   /* Store new IP length, copy in the TCP/IP header, refresh checksum */
    ((unsigned short *)ihdrp)[TLEN] = NC2(ui2 + ihlen);
    ihdrp = (unsigned char *)mess + MESSH_SZ + LHDRSZ;
    Nmemcpy(ihdrp, CONNstatep->TCPIPhdr, hlen);
    ((unsigned short *)ihdrp)[CHKSUM] =
        ~Nchksum((unsigned short *)ihdrp, ihlen/2);
    return 0;

ret9:
    return -1;
}

⌨️ 快捷键说明

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