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

📄 ip.c

📁 代码在ti的c67系列单片机上实现了完整的TCPIP协议栈
💻 C
字号:
//--------------------------------------------------------------------------
// Ip Stack
//--------------------------------------------------------------------------
// IP.C
//
// Routines related to IP
//
//
// Author: Michael A. Denio
// Copyright 1999 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <stkmain.h>
#include "ip.h"

uint _IPExecuting = 0;

IPSTATS ips;

static HANDLE hRtCache = 0;
static IPN    dwIPCache;

//--------------------------------------------------------------------
// IPMsg()
//
// Sevices intialization and resource messages
//--------------------------------------------------------------------
void IPMsg( uint Msg )
{
    static HANDLE hTimer;

    switch( Msg )
    {
    // System Initialization
    case MSG_EXEC_SYSTEM_INIT:
        // Timer is a prime to keep it staggered from others
        hTimer = TimerNew( &IPMsg, TIMER_TICKS_IP, MSG_IP_TIMER );
        mmZeroInit( &ips, sizeof( IPSTATS ) );
        _IPExecuting  = 1;
        break;

    // System Shutdown
    case MSG_EXEC_SYSTEM_SHUTDOWN:
        _IPExecuting = 0;
        // Flush the IP Reassembly queue
        while( _IPReasmPtr )
            IPReasmFree( _IPReasmPtr );

        // DeRef the cached route
        if( hRtCache )
        {
            RtDeRef( hRtCache );
            hRtCache = 0;
        }
        if( hTimer )
            TimerFree( hTimer );
        break;

    // Half Second Timer
    case MSG_IP_TIMER:
        if( _IPReasmPtr )
            IPReasmTimeout();
        break;
    }
}

//--------------------------------------------------------------------
// void IPChecksum( UINT8 *pbHdr )
//
// Checksums an IP header
//--------------------------------------------------------------------
void IPChecksum( IPHDR *pIpHdr )
{
    int     tmp1;
    UINT16  *pw;
    UINT32  dwTSum;

    // Get header size in UINT16s
    tmp1 = (pIpHdr->VerLen & 0xF) * 2;

    // Checksum field is NULL in checksum calculations
    pIpHdr->Checksum = 0;

    // Checksum the header
    pw = (UINT16 *)pIpHdr;
    dwTSum = 0;
    for( ; tmp1; tmp1-- )
        dwTSum += (UINT32)*pw++;
    dwTSum = (dwTSum&0xFFFF) + (dwTSum>>16);
    dwTSum = (dwTSum&0xFFFF) + (dwTSum>>16);
    dwTSum = ~dwTSum;

    // Note checksum is Net/Host byte order independent
    pIpHdr->Checksum = (UINT16)dwTSum;
}

//--------------------------------------------------------------------
// IPGetRoute( uint RtCallFlags, IPN dwIPDst, uint wOpt )
//
// Returns a route to the final IP destination.
//--------------------------------------------------------------------
HANDLE IPGetRoute( uint RtCallFlags, IPN dwIPDst, uint wOpt )
{
    HANDLE hRt, hRtGate;
    uint wRtFlags;

    // First, try the cached route
    if( hRtCache && dwIPCache == dwIPDst )
    {
        ips.dwCacheHit++;
        RtRef( hRtCache );
        return( hRtCache );
    }

    // Find the route
    if( !(hRt = RtFind( RtCallFlags, dwIPDst )) )
        return(0);

    // If this is a GATEWAY route, we need to get the route to
    // the GateIP
    if( ((wRtFlags=RtGetFlags( hRt )) & FLG_RTE_GATEWAY) )
    {
        // Get a route to the Gateway IP
        dwIPDst = RtGetGateIP( hRt );

        // If we're using SSRR, no gateways are allowed
        if( wOpt == IPOPT_SSRR )
        {
            RtDeRef( hRt );
            return(0);
        }

        // First, try the cached route
        if( hRtCache && dwIPCache == dwIPDst )
        {
            ips.dwCacheHit++;
            RtRef( hRtCache );
            RtDeRef( hRt );
            return( hRtCache );
        }

        // Find the route to the gateway
        // Note that this time, only HOST routes are allowed
        if( !(hRtGate = RtFind( RtCallFlags|FLG_RTF_HOST, dwIPDst )) )
        {
            RtDeRef( hRt );
            return(0);
        }

        // We no longer need the original route
        RtDeRef( hRt );

        // Switch to the Gateway route
        hRt = hRtGate;
    }

    // We've officially missed the cache
    ips.dwCacheMiss++;

    // As this is not the cached route, if it is a host route, we'll
    // make it the new cache route.
    if( wRtFlags & FLG_RTE_HOST )
    {
        if( hRtCache )
            RtDeRef( hRtCache );
        hRtCache  = hRt;
        dwIPCache = dwIPDst;
        RtRef( hRt );
    }

    // Return whatever route we found
    return( hRt );
}

//--------------------------------------------------------------------
// IPRtChange( HANDLE hRt )
//
// Called to notify IP that a route has changed.
//--------------------------------------------------------------------
void IPRtChange( HANDLE hRt )
{
    // If the changed route is our cached route, then toss it
    if( hRtCache == hRt )
    {
        hRtCache = 0;
        RtDeRef( hRt );
    }
}

⌨️ 快捷键说明

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