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

📄 rtab.c

📁 基于东南大学开发的SEP3203的ARM7中的所有驱动
💻 C
📖 第 1 页 / 共 4 页
字号:
            return status;        /* could not get the memory */

        UTL_Zero(pointer, sizeof(RIP2_ENTRY));
        RTAB_Default_Route->rt_rip2 = (RIP2_ENTRY *)pointer;

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

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

        UTL_Zero(pointer, sizeof(RIP2_AUTH_ENTRY));
        RTAB_Default_Route->rt_auth = (RIP2_AUTH_ENTRY *)pointer;
    }

    RTAB_Default_Route->rt_flags = (INT16)(flags | RT_UP | RT_GATEWAY);
    RTAB_Default_Route->rt_refcnt = 0;
    RTAB_Default_Route->rt_use = 0;
    RTAB_Default_Route->rt_clock = 0;
    RTAB_Default_Route->rt_lastsent = 0;
    RTAB_Default_Route->rt_sendcnt = 0;
        
    RTAB_Default_Route->rt_gateway.sck_addr = gw;
    RTAB_Default_Route->rt_gateway.sck_family = NU_FAMILY_IP;
    RTAB_Default_Route->rt_gateway.sck_len = sizeof (RTAB_Default_Route->rt_gateway);
    
    /* The destination IP address of a default route should be set to 0. */
    *(UINT32 *)RTAB_Default_Route->rt_rip2->ip_addr = 0;
    *(UINT32 *)RTAB_Default_Route->rt_rip2->nexthop = gw;
    RTAB_Default_Route->rt_rip2->metric = 1;
    RTAB_Default_Route->rt_rip2->af_id = NU_FAMILY_IP;
    
    RTAB_Default_Route->rt_device = device;

#if (INCLUDE_SNMP == NU_TRUE)

    PUT32(gw_ptr, 0, gw);

    /* Add this route to the SNMP routing table */
    SNMP_ipRouteTableUpdate (SNMP_ADD, (INT)(device->dev_index), 
                  RTAB_Default_Route->rt_rip2->ip_addr, 
                  RTAB_Default_Route->rt_rip2->metric, (UNSIGNED)-1, (UNSIGNED)-1, (UNSIGNED)-1, (UNSIGNED)-1, 
                  gw_ptr, 4, 8, 0, RTAB_Default_Route->rt_rip2->ip_addr, 
                  "0.0");
#endif

    return NU_SUCCESS;

}   /* RTAB_Set_Default_Route */

/*************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       RTAB_Root_Node                                                  
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Returns the Tree top to the calling function.                   
*                                                                       
*   INPUTS                                                                
*                                                                       
*      None
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*      *ROUTE_NODE                                                      
*                                                                       
*************************************************************************/
ROUTE_NODE *RTAB_Root_Node(VOID)
{
    return(Root_Node);
}   /* RTAB_Root_Node */

/*************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       RTAB_Create_Node                                                
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Create a ROUTE_NODE structure that can be inserted into the      
*       RIP2 tree.                                                       
*                                                                       
*   INPUTS                                                                
*                                                                       
*       None
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*       *ROUTE_NODE                                                      
*                                                                       
*************************************************************************/
ROUTE_NODE *RTAB_Create_Node(VOID)
{
    VOID *pointer;
    STATUS status;
    static ROUTE_NODE *rn;

    /* Allocate space for the node. */
    status = NU_Allocate_Memory (&System_Memory, &pointer,
                                    sizeof(ROUTE_NODE), NU_SUSPEND);

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

    rn = (ROUTE_NODE *)pointer;

    /* init the elements in ROUTE_NODE */
    rn->rt_parent       = 0;
    rn->rt_child[LESS]  = 0;
    rn->rt_child[MORE]  = 0;
    rn->rt_clock        = 0L;
    rn->rt_lastsent     = 0L;
    rn->rt_sendcnt      = 0L;
    rn->rt_rip2         = 0;
    rn->rt_auth         = 0;
    rn->rt_device       = 0;
    rn->rt_flags        = 0;
    rn->rt_refcnt       = 0;
    rn->rt_use          = 0;
    memset(&(rn->rt_gateway), 0, sizeof(SCK_SOCKADDR_IP));

    return(rn);
}   /* RTAB_Create_Node */

/*************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       RTAB_Find_Leaf                                                  
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Find a leaf node, one that does not have children.              
*                                                                       
*   INPUTS                                                                
*                                                                       
*       n                   node used in search.                     
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*       *ROUTE_NODE         Pointer to leaf node or NULL.            
*                                                                       
*************************************************************************/
ROUTE_NODE *RTAB_Find_Leaf( ROUTE_NODE *n )
{
    int dir;
    ROUTE_NODE *s = NU_NULL, *r;

    r = Root_Node;

    if( r == NULL_ROUTE_NODE || n == NULL_ROUTE_NODE )
        return( NULL_ROUTE_NODE );

    while( r )
    {
        s = r;              /* save current node */
        dir = RTAB_Compare_Addresses( r, n );
        if( dir == MIDDLE )
            return( r );        /* the nodes equal. */
        else
            r = r->rt_child[dir];   /* move to one of the childern */
    }

    return(s);          /* return found leaf. */
}   /* RTAB_Find_Leaf */

/*************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       RTAB_Compare_Addresses                                          
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Compare two addresses and return result.                        
*                                                                       
*   INPUTS                                                                
*                                                                       
*       r - node 1                                                       
*       n - node 2                                                       
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*       MIDDLE - than are equal.                                         
*       LESS   - node 1 is less than node 2.                             
*       MORE  - node 1 is greater than node 2.                           
*                                                                       
*************************************************************************/
int RTAB_Compare_Addresses( ROUTE_NODE *r, ROUTE_NODE *n)
{
    UINT32 addr1, addr2;
    UINT32 raddr1, raddr2;
    UINT32 mask;

    if( r == NULL_ROUTE_NODE || n == NULL_ROUTE_NODE )
        return( CMP_ERROR );

    /* use the mask from n, for both addresses. */
    /* Formula (n->addr & n->mask) - (r->addr & n->mask) */

    mask = LONGSWAP(IP_ADDR(r->rt_rip2->submask));
    memcpy((char *)&raddr1, n->rt_rip2->ip_addr, 4);
    memcpy((char *)&raddr2, r->rt_rip2->ip_addr, 4);

    addr1 = (raddr1 & mask);
    addr2 = (raddr2 & mask);

    if( addr2 == addr1 )
    {
        if( raddr1 == raddr2 )
            return(MIDDLE);
        else
            if( raddr2 < raddr1 )
            return(LESS);
        else
            return(MORE);
    }
    else
    if( addr2 < addr1 )
        return(LESS);
    else
        return(MORE);

}   /* RTAB_Compare_Addresses */

/*************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       RTAB_Add_Route                                                  
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Add a new route to the routine table.                           
*                                                                       
*   INPUTS                                                                
*                                                                       
*       *device                                                           
*       dest                                                             
*       mask                                                             
*       gw                                                               
*       flags                                                            
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*       NU_SUCCESS or -1                                                 
*                                                                       
*************************************************************************/
STATUS RTAB_Add_Route(DV_DEVICE_ENTRY *device, UINT32 dest, UINT32 mask, 
                      UINT32 gw, INT16 flags)
{
    ROUTE_NODE rn;
    RIP2_ENTRY re;
    RIP2_AUTH_ENTRY ra;
    STATUS st = NU_SUCCESS;

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

    if( (mask == 0) || (device == NU_NULL) )
        return (NU_INVALID_PARM);

    UTL_Zero(&re, sizeof(RIP2_ENTRY));
    UTL_Zero(&ra, sizeof(RIP2_AUTH_ENTRY));
    UTL_Zero(&rn, sizeof(ROUTE_NODE));

    memcpy(re.ip_addr, (char *)&dest, 4);
    memcpy(re.submask, (UINT8 *)&mask, sizeof(mask));   
    

    if( device->dev_metric == 0 )
        re.metric = 1;
    else
        re.metric = device->dev_metric;

    rn.rt_rip2 = &re;
    rn.rt_auth = &ra;

    rn.rt_rip2->af_id = NU_FAMILY_IP;
    rn.rt_flags = flags;
    rn.rt_refcnt = 0;
    rn.rt_use = 0;
    rn.rt_device = device;
    memcpy((UINT8 *)&rn.rt_gateway.sck_addr, (UINT8 *)&gw, sizeof(gw));

    if( RTAB_Insert_Node( &rn ) == -1 )
    {
        st = -1;

        /* Increment the number of routes that could not be added. */
        SNMP_ipRoutingDiscards_Inc;
    }

#if (INCLUDE_SNMP == NU_TRUE)

    else
    {
        /*
           There are two cases. They are for the type of route. Either a
           direct or indirect. If the next hop is a gateway then the route
           type is indirect. A type of 4 is indirect and 3 is direct. Also
           8 is for the routing protocol type. In this case it is RIP.
        */
        if (flags & RT_GATEWAY)
        {

            PUT32(dest_ptr, 0, dest);
            PUT32(gw_ptr, 0, gw);

            /* Add this route to the SNMP routing table */
            SNMP_ipRouteTableUpdate (SNMP_ADD, (INT)(device->dev_index), dest_ptr,
                re.metric, (UNSIGNED)-1, (UNSIGNED)-1, (UNSIGNED)-1, (UNSIGNED)-1, 
                gw_ptr, 4, 8, 0, re.submask, "0.0");
        }

⌨️ 快捷键说明

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