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

📄 rip2.c

📁 基于东南大学开发的SEP3203的ARM7中的所有驱动
💻 C
📖 第 1 页 / 共 5 页
字号:
*                                                                         
*   INPUTS                                                                
*                                                                         
*       None
*                                                                         
*   OUTPUTS                                                               
*                                                                         
*       None
*                                                                         
*************************************************************************/
VOID RIP2_Task_Entry(UNSIGNED argc, VOID *argv)
{
    struct addr_struct fromaddr;
    UINT32 curtime, last_time = 0;
    UINT32 last_del_time = 0;
    STATUS status;
    SIGNED bytes_received;
    INT16 flen;
    UINT32 wtime;
    INT entry_count;
    RIP2_PACKET *rpkt;
    ROUTE_NODE *Top_Node;
    FD_SET readfs;
    RIP2_AUTH *auth = NU_NULL;
    RIP2_LIST_NODE      *node;

#if (NU_DEBUG_RIP2 == NU_TRUE)
    INT8 temp[10];
#endif
    
    if (argc)
        auth = argv;
    
    /* The sending socket can do both broadcast and multicast sends, */
    /* you just have to enable them through setsockopt. */
    
    for ( node = RIP2_List.r2_head;
    node;
    node = node->r2_next)
    {
        RIP2_Request_Routing_Table(node);
    }
    
    for( ;; )
    {
        NU_FD_Init(&readfs);
        NU_FD_Set(RIP2_Socket, &readfs);
        
        /* Wake up every n seconds and look around. */
        wtime = (SCK_Ticks_Per_Second * 15);
        
        curtime = NU_Retrieve_Clock();
        
        /* init last_time value */
        if( last_time == 0L )
            last_time = curtime;
        if( last_del_time == 0L )
            last_del_time = curtime;
        
        /* see if it is time to look for old routes. */
        if( (curtime - last_del_time) >= (UINT32)DELETE_INTERVAL )
        {
            last_del_time = curtime;
            
            /* This function will only gather MAX_DELETES per call. */
            delete_num = 0;
            Top_Node = RTAB_Root_Node();
            if (Top_Node )
                RIP2_Gather_Old(Top_Node, curtime, 1);
            if( delete_num )
                RIP2_Delete_Old();
        }
        
        if( (curtime - last_time) >=
            (UINT32)(RIP2_BTIME * SCK_Ticks_Per_Second) )
        {
            last_time = curtime;
            
            RIP2_Broadcast();
        }
        
        /* The first parameter in this call is normally NSOCKETS. This is an 
        optimization so that the select call does not search the entire
        socket list. Instead it will only search through the sockets up to 
        the RIP2_Socket. The RIP2_Socket should be among the first created. 
        */
        status = NU_Select((RIP2_Socket + 1), &readfs, NU_NULL,
            NU_NULL, (UNSIGNED)wtime);
        
        if( status == NU_NO_DATA )
            continue;
        
        if(NU_FD_Check(RIP2_Socket, &readfs) == NU_FALSE)
            continue;
        
        /* If the select returns NU_SUCCESS then a packet is */
        /* waiting to be read at the socket. */
        
        /*  Go get the router packets.  */
        fromaddr.family = NU_FAMILY_IP;
        fromaddr.port = 0;
        bytes_received = NU_Recv_From(RIP2_Socket, RIP2_Recv_Buffer,
            (INT16)520, (INT16)0,
            &fromaddr, &flen);
        
        /*  If we got an error, it is bad.  */
        if (bytes_received < 0)
        {
            NERRS_Log_Error(NERR_RECOVERABLE, __FILE__, __LINE__);
            break;
        }
        
        rpkt = (RIP2_PACKET *)RIP2_Recv_Buffer;
        rpkt->af_id = INTSWAP(rpkt->af_id);
        
        bytes_received -= 4;        /* remove header size */
        entry_count = ((INT)bytes_received / RIP_PKT_SIZE);
        
#if (NU_DEBUG_RIP2 == NU_TRUE)
        
        /* sprintf(debug_string, "bytes_received = %d  entry_count = %d\n\r",
        bytes_received, entry_count); */
        strcpy(debug_string, "bytes_received = ");
        NU_ITOA(bytes_received, temp, 10);
        strcat(debug_string, temp);
        strcat(debug_string, " ");
        strcat(debug_string, "entry_count = ");
        NU_ITOA(entry_count, temp, 10);
        strcat(debug_string, temp);
        strcat(debug_string, "\n\r");
        
        ATI_DEBUG_PRINT(debug_string);
        
        /* sprintf(debug_string, "fromaddr       = %d.%d.%d.%d:%d",
        fromaddr.id.is_ip_addrs[0],
        fromaddr.id.is_ip_addrs[1],
        fromaddr.id.is_ip_addrs[2],
        fromaddr.id.is_ip_addrs[3],
        fromaddr.port); */
        strcpy(debug_string, "fromaddr       = ");
        NU_ITOA(fromaddr.id.is_ip_addrs[0], temp, 10);
        strcat(debug_string, temp);
        strcat(debug_string, ".");  
        NU_ITOA(fromaddr.id.is_ip_addrs[1], temp, 10);
        strcat(debug_string, temp);
        strcat(debug_string, ".");
        NU_ITOA(fromaddr.id.is_ip_addrs[2], temp, 10);
        strcat(debug_string, temp);
        strcat(debug_string, ".");
        NU_ITOA(fromaddr.id.is_ip_addrs[3], temp, 10);
        strcat(debug_string, temp);
        strcat(debug_string, ":");
        NU_ITOA(fromaddr.port, temp, 10);
        strcat(debug_string, temp);
        
        
        ATI_DEBUG_PRINT(debug_string);
        
        if( rpkt->command == RIP2_REQUEST )
            /* sprintf(debug_string, "   and it's a REQUEST packet\n\r"); */
            strcpy(debug_string, "   and it's a REQUEST packet\n\r");
        else
            /* sprintf(debug_string, "   and it's a RESPONSE packet\n\r"); */
            strcpy(debug_string, "   and it's a RESPONSE packet\n\r");
        
        ATI_DEBUG_PRINT(debug_string);
        
#endif
        
        node = RIP2_Find_Entry(fromaddr.id.is_ip_addrs);
        
        if( node == NU_NULL )
            continue;
        
        if( rpkt->command == RIP2_REQUEST)
        {
            if( rpkt->af_id == 0 && entry_count == 1 &&
                LONGSWAP(rpkt->metric) == RT_INFINITY )
            {
                
#if (NU_DEBUG_RIP2 == NU_TRUE)
                /* sprintf(debug_string, "Sending complete table.\n\r"); */
                strcpy(debug_string, "Sending complete table.\n\r");
                ATI_DEBUG_PRINT(debug_string);
#endif
                /* If we are in silent mode and we receive a */
                /* request for the route table, the port number */
                /* must be something other than RIP_PORT */
                /* see RFC 1058, section 3.4.1 */
                /* normally it is a program that is monitoring */
                /* the RIP program. */
                if( node->r2_sendmode == SEND_NONE &&
                    fromaddr.port != RIP_PORT)
                {
                    RIP2_Send_Table(node, rpkt, auth, &fromaddr);
                }
                else if( node->r2_sendmode != SEND_NONE &&
                    fromaddr.port == RIP_PORT )
                {
                    RIP2_Send_Table(node, rpkt, auth, &fromaddr);
                }
            }
            else
            {
#if (NU_DEBUG_RIP2 == NU_TRUE)
                /* sprintf(debug_string, "Sending only requested entries.\n\r"); */
                strcpy(debug_string, "Sending only requested entries.\n\r");
                ATI_DEBUG_PRINT(debug_string);
#endif
                /* If we are in silent mode and we receive a */
                /* request for the route entries, the port number */
                /* must be something other than RIP_PORT */
                /* see RFC 1058, section 3.4.1 */
                /* normally it is a program that is monitoring */
                /* the RIP program. */
                if( node->r2_sendmode == SEND_NONE && 
                    fromaddr.port != RIP_PORT)
                {
                    RIP2_Send_Updates(rpkt, node, entry_count, &fromaddr);
                }
                
                else
                    if( node->r2_sendmode != SEND_NONE &&
                        fromaddr.port == RIP_PORT)
                    {
                        RIP2_Send_Updates(rpkt, node, entry_count, &fromaddr);
                    }
            }
        }
        
        else
        {
#if (NU_DEBUG_RIP2 == NU_TRUE)
            /* sprintf(debug_string, "Received update entries.\n\r"); */
            strcpy(debug_string, "Received update entries.\n\r");
            ATI_DEBUG_PRINT(debug_string);
#endif
            
            /* All valid responses must come from a host that sent */
            /* the response from port number RIP_PORT. */
            /* see RFC 1058, section 3.4.2 */
            if( fromaddr.port == RIP_PORT &&
                RIP2_IsFromMe(node->r2_device, &fromaddr) == 0)
            {
                if( RIP2_Update_Table(node, rpkt, entry_count, auth) )
                    break;
            }
        }
    }
    
    /* close the connection */
    if (NU_Close_Socket(RIP2_Socket) != NU_SUCCESS)
    {
        NERRS_Log_Error( NERR_RECOVERABLE, __FILE__, __LINE__);
    }
    
    SCK_Suspend_Task(NU_Current_Task_Pointer());
    
}   /* NU_Rip2 */

/*************************************************************************
*                                                                         
*   FUNCTION                                                              
*                                                                         
*       RIP2_Update_Table                                                 
*                                                                         
*   DESCRIPTION                                                           
*                                                                         
*       Update the routing table with packet from other routers.          
*                                                                         
*   INPUTS                                                                
*                                                                         
*       *node                                                
*       *rpkt                                                   
*       pkt_cnt                                                         
*       *pw                                                       
*                                                                         
*   OUTPUTS                                                               
*                                                                         
*       0 for success                                                     
*       Negative number for error                                         
*                                                                         
*************************************************************************/
INT RIP2_Update_Table( RIP2_LIST_NODE *node, RIP2_PACKET *rpkt, INT pkt_cnt, 
                      RIP2_AUTH *pw)
{
    INT i;
    INT ret;
    UINT8 default_mask[4];
    INT16 c, b;
    ROUTE_NODE n;
    RIP2_ENTRY re, *ep;
    RIP2_AUTH_ENTRY ae;
    RIP2_HEADER *rh;
    
    default_mask[0]  = (UINT8) ((node->r2_device->dev_addr.dev_netmask >> 24)
        & 0x000000FF);
    default_mask[1]  = (UINT8) ((node->r2_device->dev_addr.dev_netmask >> 16)
        & 0x000000FF);
    default_mask[2]  = (UINT8) ((node->r2_device->dev_addr.dev_netmask >> 8)
        & 0x000000FF);
    default_mask[3]  = (UINT8) (node->r2_device->dev_addr.dev_netmask
        & 0x000000FF);
    
    /* point to header information */
    rh = (RIP2_HEADER *)rpkt;
    n.rt_auth = &ae;
    n.rt_rip2 = &re;
    
#if (NU_DEBUG_RIP2 == NU_TRUE)

⌨️ 快捷键说明

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