netbuf.c

来自「UCOS下添加TCP/IP和PPP协议,实现TCP,UDP点到点等功能.」· C语言 代码 · 共 1,024 行 · 第 1/3 页

C
1,024
字号
        /* Trim partial buffers. */
        if (n0) {
            if (len) {
                st += len;
                cLen -= len;
                if (dst) {
                    memcpy(dst, n0->data, len);
                }
                n0->data += len;
                n0->len -= len;
            }
            n0->chainLen = cLen;
        }
        *nb = n0;
    } else {
        len = -len;
        n0 = *nb;
        cLen = n0->chainLen;
        if (cLen > len) {
            n1 = nSplit(n0, cLen - len);
        } else {
            n1 = n0;
            n0 = NULL;
        }
        st = nTrim(dst, &n1, len);
        *nb = n0;
    }
    
    return st;
}

/*
 * nTrimQ - Trim bytes from the front of a buffer queue.
 * Note: The queue needs to be protected from collisions for the duration
 * of this call.
 * Return the number of bytes trimmed.
 */
int nTrimQ(char *dst, NBufQHdr *qh, u_int len)
{
    int st = 0;
    
    if (qh && qh->qHead && len) {
        NBuf *n0;
        int trimmed;
        
        /* Trim entire chains. */
#ifdef XXX
        OS_ENTER_CRITICAL();
#endif
        while (qh->qHead && len >= qh->qHead->chainLen) {
            if ((qh->qHead = (n0 = qh->qHead)->nextChain) == NULL)
                qh->qTail = NULL;
            qh->qLen--;
#ifdef XXX
            OS_EXIT_CRITICAL();
#endif
            
            n0->nextChain = NULL;
            trimmed = nTrim(dst, &n0, len);
            if (dst)
                dst += trimmed;
            len -= trimmed;
            st += trimmed;
#ifdef XXX
            OS_ENTER_CRITICAL();
#endif
        }
        
        /* If more to go, trim from next chain. */
        if (len && qh->qHead) {
            /* 
             * XXX LONG CRITICAL SECTION!!!  Could we pop this off the queue,
             * trim it, and then replace the remainder?  Do we need a semaphore? 
             */
            trimmed = nTrim(dst, &qh->qHead, len);
            st += trimmed;
        }
#ifdef XXX
        OS_EXIT_CRITICAL();
#endif
    }
    
    return st;
}


/*
 * nCat - Concatenate nBuf chain n2 to n1.  The chain length of n2 is added
 * to n1.
 * Return the new chain.
 */
NBuf *nCat(NBuf *n1, NBuf *n2)
{
    NBuf *nNext;
    
    if (!n1 || !n2)
        ;
    else {
        for (nNext = n1; nNext->nextBuf; nNext = nNext->nextBuf)
            ;
        nNext->nextBuf = n2;
#if STATS_SUPPORT > 0
        if ((n1->chainLen += n2->chainLen) > nBufStats.maxChainLen.val)
            nBufStats.maxChainLen.val = n1->chainLen;
#else
        n1->chainLen += n2->chainLen;
#endif
    }
    
    return n1;
}


/*
 * nChainLen - Determine the chain length of the mBuf chain and update the
 * chain length field of the top buffer.
 * Return the chain length.
 */
u_int nChainLen(NBuf *n)
{
    u_int chainLen = 0;
    NBuf *nNext;
    
    for (nNext = n; nNext; nNext = nNext->nextBuf)
        chainLen += nNext->len;
    n->chainLen = chainLen;
#if STATS_SUPPORT > 0
    if (chainLen > nBufStats.maxChainLen.val)
        nBufStats.maxChainLen.val = chainLen;
#endif

    return chainLen;
}


/*
 * nEnqSort - Insert a new chain into the queue in sorted order.
 * The sort function is designed to handle wrapping 32 bit values.
 * Return the number of chains in the queue on success, an error code
 * on error.
 */
int nEnqSort(NBufQHdr *qh, NBuf *nb, u_int32 sort) 
{
    int st;
    
    OS_ENTER_CRITICAL();
    if (!qh || !nb)
        st = -1;
    else if (!qh->qHead) {
        qh->qHead = qh->qTail = nb;
        nb->nextChain = NULL;
        st = qh->qLen = 1;
    } else {
        NBuf *n0;
        /*** NOTE: Potentially long critical section. ***/
        for(n0 = qh->qHead; 
            n0->nextChain && (long)(sort - nb->sortOrder) >= 0;
            n0 = n0->nextChain)
            ;
        nb->nextChain = n0->nextChain;
        n0->nextChain = nb;
        st = ++qh->qLen;
    }
    OS_EXIT_CRITICAL();
    
    return st;
}
        
#if DEBUG_SUPPORT > 0
/*
 * nDumpChain - Dump the details of the given buffer chain to the trace log.
 */
void nDumpChain(NBuf *n)
{
    int bufNum, len, dLen;
    u_char *dPtr;
    
    trace(LOG_INFO, "Buffer chain len=%u", n->chainLen);
    for (bufNum = 0; n; bufNum++) {
        dPtr = nBUFTOPTR(n, u_char *);
        for (len = n->len; len > 0;) {
            dLen = MIN(len, 32);
//            trace(LOG_INFO, "Buf %d[%d]:%.*H", bufNum, dLen, dLen * 2, dPtr);
            trace(LOG_INFO, "Buf %d[%d]:%s", bufNum, dLen, dPtr);
            len -= dLen;
            dPtr += dLen;
        }
        n = n->nextBuf;
    }
}
#endif

#if BYTE_ORDER == BIG_ENDIAN
#define CS_B0 0
#define CS_B1 1
#else
#define CS_B0 1
#define CS_B1 0
#endif

/*
 * inChkSum - Compute the internet ones complement 16 bit checksum for a given
 * length of a network buffer chain starting at offset off0.
 * Return the checksum in network byte order.
 */
#define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
#define REDUCE {l_util.l = sum; sum = (u_long)l_util.s[CS_B0] + (u_long)l_util.s[CS_B1]; ADDCARRY(sum);}

u_short _inChkSum(NBuf *nb, u_short len, u_short off0, u_short start_sum)
{
    register u_short *w;
    register long sum = start_sum;
    register int bufLen = 0;
    register NBuf *n0 = nb;
    int byte_swapped = 0;

    union {
        char    c[2];
        u_short s;
    } s_util;
    union {
        u_short s[2];
        long    l;
    } l_util;

    /* Ensure that there is enough data for the offset. */
    if (nb->len <= off0) {
        return -1;
    }
    
    /*
     * Adjust buffer start for the offset.
     */
    nb->len -= off0;
    nb->data += off0;
#if 0  // TODO: find out what this is for ????
    byte_swapped=off0&1;
#endif
    for (;n0 && len; n0 = n0->nextBuf) {
        if (n0->len <= 0)
            continue;
        w = nBUFTOPTR(n0, u_short *);
        if (bufLen == -1) {
            /*
             * The first byte of this nBuf is the continuation
             * of a word spanning between this nBuf and the
             * last nBuf.
             *
             * s_util.c[0] is already saved when scanning previous 
             * nBuf.
             */
            s_util.c[CS_B1] = *(char *)w;
            sum += s_util.s;
            w = (u_short *)((char *)w + 1);
            bufLen = n0->len - 1;
            len--;
        } else
            bufLen = n0->len;
        if (len < bufLen)
            bufLen = len;
        len -= bufLen;
        /*
         * Force to even boundary.
         */
        if ((1 & (int) w) && (bufLen > 0)) {
            REDUCE;
            sum <<= 8;
            s_util.c[CS_B0] = *(u_char *)w;
            w = (u_short *)((char *)w + 1);
            bufLen--;
            byte_swapped = 1;
        }
        /*
         * Unroll the loop to make overhead from
         * branches &c small.
         */
        while ((bufLen -= 32) >= 0) {
            sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
            sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
            sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
            sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
            w += 16;
        }
        bufLen += 32;
        while ((bufLen -= 8) >= 0) {
            sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
            w += 4;
        }
        bufLen += 8;
        if (bufLen == 0 && byte_swapped == 0)
            continue;
        REDUCE;
        while ((bufLen -= 2) >= 0) {
            sum += *w++;
        }
        if (byte_swapped) {
            REDUCE;
            sum <<= 8;
            byte_swapped = 0;
            if (bufLen == -1) {
                s_util.c[CS_B1] = *(char *)w;
                sum += s_util.s;
                bufLen = 0;
            } else
                bufLen = -1;
        } else if (bufLen == -1)
            s_util.c[CS_B0] = *(char *)w;
    }
    /*
     * Reset buffer start for the offset.
     */
    nb->len += off0;
    nb->data -= off0;
    
    if (len) {
        IPDEBUG((LOG_ERR, TL_IP, "inChkSum: out of data"));
    }
    if (bufLen == -1) {
        /* The last nBuf has odd # of bytes. Follow the
           standard (the odd byte may be shifted left by 8 bits
           or not as determined by endian-ness of the machine) */
        s_util.c[CS_B1] = 0;
        sum += s_util.s;
    }
    REDUCE;
    return ((u_short)(~sum) & 0xffff);
}

u_short inChkSum(NBuf *nb, u_short len, u_short off0)
{
    return _inChkSum(nb, len, off0, 0);
}


/**********************************/
/*** LOCAL FUNCTION DEFINITIONS ***/
/**********************************/

#pragma warning (pop)

⌨️ 快捷键说明

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