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

📄 route.c

📁 代码在ti的c67系列单片机上实现了完整的TCPIP协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
//--------------------------------------------------------------------------
// Ip Stack
//--------------------------------------------------------------------------
// Route.C
//
// Route Object
//
// Author: Michael A. Denio
// Copyright 1999 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <stkmain.h>
#include "route.h"

//--------------------------------------------------------------------
// RtNew - Create a route entry structure
//
// Static function used only by route API
//--------------------------------------------------------------------
static HANDLE RtNew()
{
    RT  *prt;

    if( !(prt = mmAlloc(sizeof(RT))) )
    {
        DbgPrintf(DBG_WARN,"RtNew: OOM");
        ExecLowResource();
        return(0);
    }

    // Initialize type
    prt->Type = HTYPE_RT;

    // Initialize refcount
    prt->RefCount = 1;

    // Initialize defaults
    prt->pNextExp     = 0;
    prt->pNext        = 0;
    prt->dwTimeout    = 0;
    prt->Flags        = 0;
    prt->FailCode     = 0;
    prt->hNode        = 0;
    prt->hIF          = 0;
    prt->hLLI         = 0;
    prt->ProtMTU      = ROUTE_DEFAULT_MTU;

    return( (HANDLE)prt );
}

//--------------------------------------------------------------------
// RtFree - Destroy Route
//
// Static function used only by route API
//--------------------------------------------------------------------
static void RtFree( RT *prt )
{
    // Kill type for debug
    prt->Type = 0;

    // Free the LLI
    if( prt->hLLI )
        LLIFree( prt->hLLI );

    // Remove from Timeout list
    _RtExpListRemove( prt );

    // Remove from Node list
    _RtNodeRemove( prt );

    // Free the handle if we're done with it
    mmFree( prt );
}

//--------------------------------------------------------------------
// RtDeRef - DeReference a Route
//
// Decrement the reference count, and free if it reaches ZERO.
//--------------------------------------------------------------------
void RtDeRef( HANDLE hRt )
{
    RT     *prt = (RT *)hRt;

#ifdef _STRONG_CHECKING
    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtFree: HTYPE %04x",prt->Type);
        return;
    }
#endif

    // Standard DeRef
    // (Zap the type if route should be deleted)
    if( prt->RefCount != 65535 )
    {
        if( prt->RefCount > 1 )
            prt->RefCount--;                // Deref one count
        else
        {
            // Post a report if needed
            // If the route is still active (has a node), we'll report it.
            // Otherwise, we'll assume it was a time-out.
            if( prt->hNode )
                RTCReport(MSG_RTC_REMOVED, prt->dwIPAddr, prt->dwIPMask);
            RtFree( prt );
        }
    }
}

#ifdef _STRONG_CHECKING

//--------------------------------------------------------------------
// RtGetIPAddr - Get IP Addr
//
// Get the destination Ip Address associated with
// the route.
//--------------------------------------------------------------------
IPN RtGetIPAddr( HANDLE hRt )
{
    RT *prt = (RT *)hRt;

    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtGetIpAddr: HTYPE %04x",prt->Type);
        return(0);
    }
    return( prt->dwIPAddr );
}

//--------------------------------------------------------------------
// RtGetIPMask - Get IP Mask
//
// Get the destination Ip Mask associated with
// the route.
//--------------------------------------------------------------------
IPN RtGetIPMask( HANDLE hRt )
{
    RT *prt = (RT *)hRt;

    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtGetIpMask: HTYPE %04x",prt->Type);
        return(0);
    }
    return( prt->dwIPMask );
}

//--------------------------------------------------------------------
// RtGetFlags
//
// Get the Route Status Flags
//--------------------------------------------------------------------
uint RtGetFlags( HANDLE hRt )
{
    RT *prt = (RT *)hRt;

    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtGetFlags: HTYPE %04x",prt->Type);
        return(0);
    }
    return( prt->Flags );
}

//--------------------------------------------------------------------
// RtGetIF
//
// Get Interface Handle
//--------------------------------------------------------------------
HANDLE RtGetIF( HANDLE hRt )
{
    RT *prt = (RT *)hRt;

    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtGetIF: HTYPE %04x",prt->Type);
        return( 0 );
    }
    return( prt->hIF );
}

//--------------------------------------------------------------------
// RtGetMTU
//
// Get MTU of route
//--------------------------------------------------------------------
uint RtGetMTU( HANDLE hRt )
{
    RT *prt = (RT *)hRt;

    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtGetMTU: HTYPE %04x",prt->Type);
        return( 0 );
    }
    return( prt->ProtMTU );
}

//--------------------------------------------------------------------
// RtGetGateIP
//
// Get Gate IP Address
//--------------------------------------------------------------------
IPN RtGetGateIP( HANDLE hRt )
{
    RT *prt = (RT *)hRt;

    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtGetGateIP: HTYPE %04x",prt->Type);
        return( 0 );
    }
    return( prt->dwIPGate );
}

//--------------------------------------------------------------------
// RtGetLLI
//
// Get LLI
//--------------------------------------------------------------------
HANDLE RtGetLLI( HANDLE hRt )
{
    RT *prt = (RT *)hRt;

    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtGetLLI: HTYPE %04x",prt->Type);
        return( 0 );
    }
    return( prt->hLLI );
}

//--------------------------------------------------------------------
// RtGetFailure
//
// Get Failure Code
//--------------------------------------------------------------------
uint RtGetFailure( HANDLE hRt )
{
    RT *prt = (RT *)hRt;

    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtGetFailure: HTYPE %04x",prt->Type);
        return( 0 );
    }
    return( prt->FailCode );
}

//--------------------------------------------------------------------
// RtSetMTU
//
// Set the MTU of route
//--------------------------------------------------------------------
void RtSetMTU( HANDLE hRt, uint mtu )
{
    RT *prt = (RT *)hRt;

    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtSetMTU: HTYPE %04x",prt->Type);
        return;
    }
    prt->ProtMTU = mtu;
}

//--------------------------------------------------------------------
// RtSetLLI
//
// Set LLI Entry
//--------------------------------------------------------------------
void RtSetLLI( HANDLE hRt, HANDLE hLLI )
{
    RT *prt = (RT *)hRt;

    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtSetLLI: HTYPE %04x",prt->Type);
        return;
    }

    // Make sure we don't already have an entry
    if( prt->hLLI )
    {
        DbgPrintf(DBG_ERROR,"RtSetLLI: Double Set");
        return;
    }

    prt->hLLI = hLLI;
}

#endif

//---------------------------------------------------------------------
// RtSetFailure - Fail Route
//
// Flags:
//     FLG_RTF_REPORT      Report failure
//
// Failure Code:
//     NULL               - No Failure
//     RTC_HOSTDOWN       - Host is down
//     RTC_HOSTUNREACH    - Host unreachable
//     RTC_NETUNREACH     - Network unreachable
//---------------------------------------------------------------------
void RtSetFailure( HANDLE hRt, uint CallFlags, uint FailCode )
{
    RT *prt = (RT *)hRt;

#ifdef _STRONG_CHECKING
    if( prt->Type != HTYPE_RT )
    {
        DbgPrintf(DBG_ERROR,"RtSetFailure: HTYPE %04x",prt->Type);
        return;
    }
#endif

    // First, update the failure code and UP flag
    if( (prt->FailCode = FailCode) != 0 )
        prt->Flags &= ~FLG_RTE_UP;
    else
        prt->Flags |= FLG_RTE_UP;

    // Post a report if requested
    if( CallFlags & FLG_RTF_REPORT )
    {
        // Send Routing Report

⌨️ 快捷键说明

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