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

📄 utl.c

📁 基于东南大学开发的SEP3203的ARM7中的所有驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
  wordStartP = (UNSIGNED*)(((UNSIGNED)(ptr)) + sizeof(UNSIGNED) - 1);
  wordStartP = (UNSIGNED*)(((UNSIGNED)(wordStartP)) / sizeof(UNSIGNED));
  wordStartP = (UNSIGNED*)(((UNSIGNED)(wordStartP)) * sizeof(UNSIGNED));

  byteStartP = (UINT8 *)ptr;
  byteEndP = (UINT8 *)(wordStartP);

  /* Clear leading unaligned bytes */
  while(byteStartP < byteEndP)
  {
    *byteStartP = clearByte;
    ++byteStartP;
  }

  wordEndP = (UNSIGNED *)((((UNSIGNED)(ptr)) + size) / sizeof(UNSIGNED));
  wordEndP = (UNSIGNED *)(((UNSIGNED)(wordEndP)) * sizeof(UNSIGNED));

  /* Clear the aligned bytes as entire words */
  while(wordStartP < wordEndP)
  {
    *wordStartP = clearWord;
    ++wordStartP;
  }
  
  /* Clear trailing unaligned bytes */
  byteStartP = (UINT8 *)(wordEndP);
  byteEndP = (UINT8 *)(((CHAR HUGE *) ptr) + size);

  while(byteStartP < byteEndP)
  {
    *byteStartP = clearByte;
    ++byteStartP;
  }
}

/*************************************************************************
*                                                                       
*   FUNCTION                                                              
*                                                                       
*       UTL_Checksum                                                     
*                                                                       
*   DESCRIPTION                                                           
*                                                                       
*       Calculate a TCP checksum.                                        
*                                                                       
*   INPUTS                                                                
*                                                                       
*       *buf_ptr                                                         
*       source                                                           
*       dest                                                             
*       protocol                                                         
*                                                                       
*   OUTPUTS                                                               
*                                                                       
*       UINT16                                                           
*                                                                       
*************************************************************************/
UINT16 UTL_Checksum (NET_BUFFER *buf_ptr, UINT32 source,
                     UINT32 dest, UINT8 protocol)
{
    struct pseudotcp     tcp_chk;

    tcp_chk.source  = LONGSWAP(source);
    tcp_chk.dest    = LONGSWAP(dest);
    tcp_chk.z       = 0;
    tcp_chk.proto   = protocol;
    tcp_chk.tcplen  = INTSWAP((UINT16)buf_ptr->mem_total_data_len);

    return(TLS_TCP_Check( (UINT16 *) &tcp_chk, buf_ptr));

} /* UTL_Checksum */

/*************************************************************************
*
*   FUNCTION                                                               
*                                                                        
*       UTL_Rand                                                              
*                                                                        
*   DESCRIPTION                                                            
*                                                                        
*       Function handles generating a random number.                          
*                                                                        
*   INPUTS                                                                 
*                                                                        
*       None                                                                
*                                                                        
*   OUTPUTS                                                                
*                                                                        
*       Returns the Random Number.                                          
*                                                                        
*************************************************************************/
UINT32 UTL_Rand(void)
{
    static UINT32 next = 0;
    
    /* Use the last 4 bytes of the MAC addr as the seed. */
    if (next == 0)
    {
        /* If the first device is the loopback and there is a second
           device then use its' MAC address, since the loopback does not 
           have one. */
        if ( (DEV_Table.dv_head->dev_type == DVT_LOOP) && 
             (DEV_Table.dv_head->dev_next != NU_NULL) )
            next = GET32(&DEV_Table.dv_head->dev_next->dev_mac_addr[2], 0);
        else
            next = GET32(&DEV_Table.dv_head->dev_mac_addr[2], 0);
    }

    next = NU_Retrieve_Clock() + (next * ((next >> 16) + 1));

    return next;

} /* end UTL_Rand() */

/*************************************************************************
*                                                                         
*   FUNCTION                                                               
*                                                                         
*       UTL_Get_Unique_Port_Number                                         
*                                                                         
*   DESCRIPTION                                                            
*                                                                         
*       This function derives a new port number and then searches          
*       both the TCP_Ports and the UDP_Ports to see if it has              
*       already been used.                                                 
*                                                                         
*   INPUTS                                                                 
*                                                                         
*       None                                                               
*                                                                         
*   OUTPUTS                                                                
*                                                                         
*       UINT16                  Port number.                               
*                                                                         
*************************************************************************/
UINT16 UTL_Get_Unique_Port_Number(VOID)
{
 UINT16  i=0;

 while (i==0)
 {
     i = (UINT16) UTL_Rand ();           /* get a unique number */
     i = (UINT16) (i | 2048);            /* make sure >= 0x800 */
     
    /*  search for an un-used port.  Search as long as we have
     *  not searched all ports and we have not found an existing port
     *  of the same number as the new one.
     */
     if((UTL_Is_Unique_Port_Number(NU_PROTO_TCP, i) == NU_FALSE) || 
         (UTL_Is_Unique_Port_Number(NU_PROTO_UDP, i) == NU_FALSE))
         i=0; 
 }

 return(i);
    
} /* end UTL_Get_Unique_Port_Number() */



/*************************************************************************
*                                                                         
*   FUNCTION                                                               
*                                                                         
*       UTL_Is_Unique_Port_Number                                         
*                                                                         
*   DESCRIPTION                                                            
*                                                                         
*       This function searches both the TCP_Ports and the UDP_Ports to 
*       see if it has already been used.                                                 
*                                                                         
*   INPUTS                                                                 
*                                                                         
*       protocol - whether it is TCP or UDP Protocol
*       port - the port number to search for.                                                               
*                                                                         
*   OUTPUTS                                                                
*                                                                         
*       INT   NU_TRUE or NU_FALSE                               
*                                                                         
*************************************************************************/
INT UTL_Is_Unique_Port_Number(UINT16 protocol, UINT16 port)
{
  INT     j;

#if (INCLUDE_TCP == NU_TRUE)
    if (protocol == NU_PROTO_TCP)
    {
        for (j=0; j<TCP_MAX_PORTS; j++)           /* don't check NULL ports */
            if (TCP_Ports[j]!=NU_NULL && port==TCP_Ports[j]->in.port)
                return NU_FALSE;
            

        /* Don't forget to check the TCP listening sockets. They do not
           have a port structure so we must check the socket list. */
            
            
         for (j = 0; j < NSOCKETS; j++)
             if ( (SCK_Sockets[j] != NU_NULL) &&
                (SCK_Sockets[j]->s_protocol == NU_PROTO_TCP) &&
                (SCK_Sockets[j]->s_local_addr.port_num == port) )
                return NU_FALSE;
             
    }
#endif
    
#if (INCLUDE_UDP == NU_TRUE)
    if (protocol == NU_PROTO_UDP)
    {
        
        for (j=0; j < UDP_MAX_PORTS; j++)
            if (UDP_Ports[j] && port == UDP_Ports[j]->up_lport)
                return NU_FALSE;

        for (j = 0; j < NSOCKETS; j++)
            if ( (SCK_Sockets[j] != NU_NULL) &&
               (SCK_Sockets[j]->s_protocol == NU_PROTO_UDP) &&
               (SCK_Sockets[j]->s_local_addr.port_num == port) )
               return NU_FALSE;            
    }
#endif
    
    return NU_TRUE;
} /* end UTL_Is_Unique_Port_Number() */

⌨️ 快捷键说明

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