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

📄 igmp.c

📁 基于nucleus操作系统的GPRS无线数据传输终端全套源文件。包括支持ARM7的BSP,操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************
*                                                                       
*        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                       
*                                                                               
*      IGMP.C                                       4.4                         
*                                                                               
* COMPONENT                                                             
*                                                                       
*      IGMP - Internet Group Management Protocol                            
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*      This file contains the IGMP (Internet Group Management Protocol) 
*      module. IGMP provides the means for hosts to notify routers      
*      of the multicasting groups that the host belongs to.             
*                                                                       
* DATA STRUCTURES                                                       
*                                                                       
*      <Data Structure> - <Description>                                 
*                                                                       
* FUNCTIONS                                                             
*                                                                       
*      IGMP_Interpret                  Processes received IGMP packets. 
*      IGMP_Initialize                 Joins the all hosts group on an  
*                                      IP multiccasting capable device. 
*      IGMP_Leave                      Kills any timer events that are  
*                                      pending when membership in a     
*                                      multicast gorup is dropped.      
*      IGMP_Random_Delay               Caclulates a Random delay.       
*      IGMP_Send                       Sends an IGMP group membership   
*                                      report.                          
*      IGMP_Join                       Sends a multicast group          
*                                      membership report.               
*                                                                       
* DEPENDENCIES                                                          
*                                                                       
*      <Dependency>                                                     
*                                                                       
*************************************************************************/
#include "plus/nucleus.h"
#include "net/target.h"
#include "net/inc/externs.h"
#include "net/inc/ip.h"
#include "net/inc/igmp.h"
#include "net/inc/netevent.h"

/* If Multicasting is not desired, then this file should not be compiled 
   into the Nucleus NET library. */
#if INCLUDE_IP_MULTICASTING
/***********************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*      IGMP_Interpret                                                   
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*      This function processes receieved IGMP packets.                  
*                                                                       
* INPUTS                                                                
*                                                                       
*      buf_ptr         Pointer to a buffer containg the IGMP packet.    
*      hlen            Length of the IP header                          
*                                                                       
* OUTPUTS                                                               
*                                                                       
*      NU_SUCCESS                                                       
*      -1              Fail.                                            
*                                                                       
*************************************************************************/
STATUS IGMP_Interpret(NET_BUFFER *buf_ptr, INT hlen)
{
    INT32       igmp_len;
    IGMP_LAYER  *igmp;
    IPLAYER     *ip;
    UINT16      cksum;
    IP_MULTI    *ipm;

    igmp_len = buf_ptr->mem_total_data_len;

    if (igmp_len != sizeof (IGMP_LAYER))
    {
        MEM_Buffer_Chain_Free (&MEM_Buffer_List, &MEM_Buffer_Freelist);
        return -1;
    }

    igmp = (IGMP_LAYER *)buf_ptr->data_ptr;
    ip = (IPLAYER *)(buf_ptr->data_ptr - hlen);

    cksum = GET16(igmp, IGMP_CKSUM_OFFSET);
    PUT16(igmp, IGMP_CKSUM_OFFSET, 0);

    if ( cksum != TLS_IP_Check ((UINT16 *)igmp, sizeof(IGMP_LAYER)) )
    {
        MEM_Buffer_Chain_Free (&MEM_Buffer_List, &MEM_Buffer_Freelist);
        return -1;
    }

    switch (GET8(igmp, IGMP_TYPE_OFFSET))
    {
    
    case IGMP_HOST_MEMBERSHIP_QUERY :
        
        /* All queries should be addressed to the all hosts group. */
        if (GET32(ip, IP_DEST_OFFSET) != LONGSWAP(IGMP_ALL_HOSTS_GROUP))
        {
            MEM_Buffer_Chain_Free (&MEM_Buffer_List, &MEM_Buffer_Freelist);
            return -1;
        }

        /* Step through all of the multicast groups that the receive interface 
           is a member of. */
        for ( ipm = buf_ptr->mem_buf_device->dev_addr.dev_multiaddrs;
              ipm != NU_NULL;
              ipm = ipm->ipm_next)
        {
            /* Don't send a report if for the all hosts group or if a timer is 
               already set up. */
            if ( (ipm->ipm_device == buf_ptr->mem_buf_device) &&
                 (ipm->ipm_timer == 0) &&
                 (ipm->ipm_addr != LONGSWAP(IGMP_ALL_HOSTS_GROUP)) )
            {
                ipm->ipm_timer = IGMP_Random_Delay(ipm);
            }

        }
        break;

    case IGMP_HOST_MEMBERSHIP_REPORT :
        
        /* Make sure the multicast group is valid. */
        if ( !IP_MULTICAST_ADDR(GET32(igmp, IGMP_GROUP_OFFSET)) ||
             (GET32(igmp, IGMP_GROUP_OFFSET) != GET32(ip, IP_DEST_OFFSET)) )
        {
            MEM_Buffer_Chain_Free (&MEM_Buffer_List, &MEM_Buffer_Freelist);
            return -1;
        }

        /* See if the receive interface is a member of the group. */
        ipm = IP_Lookup_Multi(GET32(igmp, IGMP_GROUP_OFFSET), &buf_ptr->mem_buf_device->dev_addr);

        /* If the interface is a member of the group and a report timer 
           currently exists, clear the timer. */
        if (ipm && ipm->ipm_timer)
        {
            ipm->ipm_timer = 0;
            UTL_Timerunset (EV_IGMP_REPORT, (UINT32)ipm, 1);
        }
        break;

    default :
        break;
    }

    /* Free the buffer. */
    MEM_Buffer_Chain_Free (&MEM_Buffer_List, &MEM_Buffer_Freelist);

    return (NU_SUCCESS);

} /* IGMP_Interpret */

/***********************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*      IGMP_Initialize                                                  
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*      This function joins the all hosts group on an IP multicasting    
*      capable device.  All level two conforming hosts are required to  
*      join the all hosts group (224.0.0.1).                            
*                                                                       
* INPUTS                                                                
*                                                                       
*      device          Pointer to the device to init IGMP on.           
*                                                                       
* OUTPUTS                                                               
*                                                                       
*      NU_SUCCESS      Successful completion of the operation.          
*      NU_INVAL        failure                                          
*************************************************************************/
STATUS  IGMP_Initialize(DV_DEVICE_ENTRY *device)
{
    UINT32      all_hosts_group = 0xE0000001UL; /* IP address 224.0.0.1 */

    /* Join the all hosts group on this device. Note that IP_Add_Multi will not
       report the membership in the all hosts group, and a report should not be

⌨️ 快捷键说明

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