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

📄 rtab.c

📁 基于东南大学开发的SEP3203的ARM7中的所有驱动
💻 C
📖 第 1 页 / 共 4 页
字号:
/*************************************************************************
*                                                                       
*        Copyright (c) 1993 - 2001 Accelerated Technology, Inc.         
*                                                                       
* PROPRIETARY RIGHTS of Accelerated Technology are involved in the      
* subject matter of this material.  All manufacturing, reproduction,    
* use, and sales rights pertaining to this subject matter are governed  
* by the license agreement.  The recipient of this software implicitly  
* accepts the terms of the license.                                     
*                                                                       
*************************************************************************/

/*************************************************************************
*                                                                       
*   FILE NAME                                       VERSION                       
*                                                                               
*       RTAB.C                                        4.4                        
*                                                                               
*   COMPONENT                                                             
*                                                                       
*       Routing                                                          
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       This module contains the functions for accessing the routing     
*       table.                                                           
*                                                                       
*   DATA STRUCTURES                                                       
*                                                                       
*       *Root_Node
*       RTAB_Default_Route
*                                                                       
*   FUNCTIONS                                                             
*                                                                       
*       RTAB_Add_Route                                                   
*       RTAB_Compare_Addresses                                           
*       RTAB_Create_Node                                                 
*       RTAB_Delete_Node                                                 
*       RTAB_Find_Leaf                                                   
*       RTAB_Find_Route                                                  
*       RTAB_Free                                                        
*       RTAB_Get_Default_Route                                           
*       RTAB_Init                                                        
*       RTAB_Insert_Node                                                 
*       RTAB_Root_Node                                                   
*       RTAB_Set_Default_Route                                           
*       RTAB_Update_Address                                              
*       RTAB_Redirect                                                    
*                                                                       
*   DEPENDENCIES                                                          
*                                                                       
*       nucleus.h
*       target.h
*       externs.h
*       ip.h
*       socketd.h
*       nerrs.h
*       dev.h
*       rtab.h
*                                                                       
*************************************************************************/

#include "plus/nucleus.h"
#include "net/target.h"
#include "net/inc/externs.h"
#include "net/inc/ip.h"
#include "net/inc/socketd.h"    /* socket interface structures */
#include "net/inc/nerrs.h"
#include "net/inc/dev.h"
#include "net/inc/rtab.h"

#if (INCLUDE_SNMP == NU_TRUE)
#include SNMP_GLUE
#endif

/* The top or root of the route table tree. */
ROUTE_NODE *Root_Node; 

/* This is the default route to return when no other route can be found. */
ROUTE_NODE *RTAB_Default_Route;

ROUTE_NODE *RTAB_Root_Node( VOID );
ROUTE_NODE *RTAB_Create_Node( VOID );
INT RTAB_Compare_Addresses( ROUTE_NODE *, ROUTE_NODE * );
INT RTAB_Update_Address( ROUTE_NODE *, ROUTE_NODE * );

extern VOID Hex_Dump_Buffer(UINT8 *, UINT8 *, UINT16);

/*************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       RTAB_Find_Route                                                 
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Finds a route to a destination IP address and return the dev.   
*                                                                       
*   INPUTS                                                                
*                                                                       
*      *de                                                               
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*      *ROUTE_NODER         Returns null if route not found and      
*                           there is no default route.              
*                                                                       
*************************************************************************/
ROUTE_NODE *RTAB_Find_Route( SCK_SOCKADDR_IP *de )
{
    INT             dir = NU_IGNORE_VALUE;
    RIP2_ENTRY      re;
    ROUTE_NODE      *n, c;
    UINT32          ip_addr;

    if (Root_Node == NULL_ROUTE_NODE)
        return( (ROUTE_NODE *)0 );      /* no nodes in tree */

    c.rt_rip2 = &re;
    c.rt_auth = 0;
    memcpy(c.rt_rip2->ip_addr, (CHAR *)&de->sck_addr, 4);

    /* Start at the root node */
    n = Root_Node;

    /* Walk the tree searching for an exact match.  It does not matter whether
     * the target is a host or network route at this point.
     */
    while (n)
    {
        /* Check if the two ip addresses are equal or if we need to 
         * traverse the left or right side of the tree.
         */
        dir = RTAB_Compare_Addresses (n, &c);

        /* If the two ip addresses are equal. */
        if (dir == MIDDLE)
        {
            /* Check to make sure this route is still valid before
             * passing it back up. It must be UP and if rip2 in
             * there its metric must not be infinity. 
             */
            if ( (!(n->rt_flags & RT_UP)) ||
                ((n->rt_rip2) && (n->rt_rip2->metric == RT_INFINITY)))

                /* Set dir to something other than MIDDLE in order
                 * to invalidate this route in the scope of this
                 * function. 
                 */
                dir = NU_IGNORE_VALUE;

            break;
        }
        /* Otherwise, traverse the dir child of the node */
        else
            n = n->rt_child[dir];
    }

    /* If we did not find an exact match, check to see if there is a 
     * network route for this ip address.
     */
    if (dir != MIDDLE)
    {
        /* did not find a host route now search for a network route. */

        /* Save a copy of the unmodified address. */
        ip_addr = *(UINT32 *)c.rt_rip2->ip_addr;

        /* Start at the root node */
        n = Root_Node;

        /* Traverse the tree for a network route. */
        while (n)
        {
            /* By anding the target ip address with the current node's
             * subnet mask, we are able to determine whether we have 
             * a network route in the system for the ip address.
             */
            *(UINT32 *)c.rt_rip2->ip_addr = ip_addr
                & LONGSWAP((IP_ADDR(n->rt_rip2->submask)));

            /* Check to see if we have a network route for this node */
            dir = RTAB_Compare_Addresses( n, &c);

            /* We found a network route for this node */
            if ( dir == MIDDLE )
            {
                /* Check to make sure this route is still valid before
                 * passing it back up. It must be UP and if rip2 in
                 * there its metric must not be infinity. 
                 */
                if ( (!(n->rt_flags & RT_UP)) ||
                    ((n->rt_rip2) && (n->rt_rip2->metric == RT_INFINITY)))

                    /* Set dir to something other than MIDDLE in order
                     * to invalidate this route in the scope of this
                     * function. 
                     */
                    dir = NU_IGNORE_VALUE;

                break;
            }

            /* Otherwise, traverse the dir child of the node */
            else
                n = n->rt_child[dir];
        }
    }

    /* If a route was not found then return the default route. */
    if (dir != MIDDLE)
        n = RTAB_Default_Route;

    if (n)
        n->rt_refcnt++;

    return(n);
}   /* RTAB_Find_Route */

/*************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       RTAB_Free                                                       
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Initialize the default_route structure                          
*                                                                       
*   INPUTS                                                                
*                                                                       
*       *rt                                                               
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*       None
*                                                                       
*************************************************************************/
VOID RTAB_Free(ROUTE_NODE *rt)
{
    if (rt && (rt->rt_refcnt > 0))
        rt->rt_refcnt--;
    else
        NERRS_Log_Error (NERR_RECOVERABLE, __FILE__, __LINE__);
}

/*************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       RTAB_Init                                                       
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Initialize the default_route structure                          
*                                                                       
*   INPUTS                                                                
*                                                                       
*       None
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*       None
*                                                                       
*************************************************************************/
VOID RTAB_Init(VOID)
{
    RTAB_Default_Route = NU_NULL;
    Root_Node = NULL_ROUTE_NODE; 
}   /* RTAB_Init */

/*************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       RTAB_Set_Default_Route                                          
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Sets the default route, called by the NU_Rip2 function.         
*                                                                       
*   INPUTS                                                                
*                                                                       
*      *device                                                           
*      gw                                                               
*      flags                                                            
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*      None
*                                                                       
*************************************************************************/
STATUS RTAB_Set_Default_Route(DV_DEVICE_ENTRY *device, UINT32 gw, UINT16 flags)
{
    VOID        *pointer;
    STATUS      status;

#if (INCLUDE_SNMP == NU_TRUE)
    UINT8       gw_ptr[4];
#endif

    if (!device)
        return NU_INVALID_PARM;

    if (!RTAB_Default_Route)
    {
        status = NU_Allocate_Memory(&System_Memory, (VOID **) &pointer,
                                sizeof(ROUTE_NODE), (UNSIGNED)NU_NO_SUSPEND);

        if (status != NU_SUCCESS)
            return status;        /* could not get the memory */

        UTL_Zero(pointer, sizeof(ROUTE_NODE));
        RTAB_Default_Route = (ROUTE_NODE *)pointer;

        status = NU_Allocate_Memory(&System_Memory, (VOID **) &pointer,
                                sizeof(RIP2_ENTRY), (UNSIGNED)NU_NO_SUSPEND);

        if (status != NU_SUCCESS)

⌨️ 快捷键说明

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