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

📄 igmp_main.cpp

📁 igmp for switch in vxworks
💻 CPP
📖 第 1 页 / 共 5 页
字号:
							add[i].groupaddress = 0;
							usCurrentGroup--;
							SendIgmpPkt (IGMPLeaveGroup, groupaddress, 0,
								0);
						}
						return OK;

					}
				}
			}
			return OK;
		}
		else if (*(IgmpPkt + 38) == 0x11)
		{
			/*发IGMP 查询报文 */
			for (i = 1; i <= MAX_UPLINK_PORT; i++)
			{
				if (i == usPort)
					continue;
				SendIgmpPkt (0x11, 0, i, 10);
			}
		}

		else
		{
			return OK;
		}

	}
#endif

/***********************************************************************************/
	/*ParameterNotUse (UINT (usLength)); */

	/*有效性检查 */
	if (nIGMPTaskId == 0)
	{
		if (bIgmpDebug)
		{
			printf ("IGMP Task does not exist,Receive ERROR!\n");
		}
		ulIGMPDropPktAll++;
		/*free ((void *) IgmpPkt);
		   /*device_driver_free ((void *) IgmpPkt); 
		   IgmpPkt = NULL; */
		return ERROR;
	}

	if (usPort > MAX_UPLINK_PORT)
	{
		if (bIgmpDebug)
		{
			printf ("IGMP Port out of range!\n");
		}
		ulIGMPDropPktAll++;
		/*free ((void *) IgmpPkt);
		   /*device_driver_free ((void *) IgmpPkt); 
		   IgmpPkt = NULL; */
		return ERROR;
	}

	/*统计队列长度 */
	MsgNum = msgQNumMsgs (IGMPMsgQId);
	usCurrentMsgQLength = USHORT (MsgNum);
	if (usCurrentMsgQLength > usRealMaxMsgQLength)
	{
		usRealMaxMsgQLength = usCurrentMsgQLength;
	}

	if (MsgNum >= sMaxIGMPMsgQLength)	/*队列已经满了 */
	{
		if (bIgmpDebug)
		{
			printf ("MessageQueue is Full,Receive ERROR!\n");
		}
		ulIGMPDropPktAll++;
		/*free ((void *) IgmpPkt);
		   /*device_driver_free ((void *) IgmpPkt);
		   IgmpPkt = NULL; */
		return ERROR;
	}

	semTake (semIgmpReceive, WAIT_FOREVER);

	/*开始对收到的包进行解码 */

	if (htons (*((USHORT *) (IgmpPkt + 12))) != 0x8100)	/*无VLAN TAG */
	{
		ReceiveIgmpMsg.VlanTag = 0;
		if (htons (*((USHORT *) (IgmpPkt + 12))) == 0x8864)	/*有PPPoE封装 */
		{
			/*printf("Rx IGMP with PPPoE\n"); */
			PPPoEFlag = 1;
			IpHeaderInPkt = (ipheader *) (IgmpPkt + 22);
			usSessionId = htons (*((USHORT *) (IgmpPkt + 16)));
		}
		else				/*无PPPoE封装 */
		{
			PPPoEFlag = 0;
			IpHeaderInPkt = (ipheader *) (IgmpPkt + 14);
			usSessionId = 0;
		}
		IpHeaderLength = IpHeaderInPkt->headerlength * 4;
		IpProtocolInPkt = IpHeaderInPkt->protocol;
		if (IpProtocolInPkt != 2)	/*不是IGMP包 */
		{
			if (bIgmpDebug)
			{
				printf ("Not IGMP Pkt,Receive ERROR!\n");
			}
			ulIGMPDropPktAll++;
			ulIGMPReceiveBadPktAll++;
			semGive (semIgmpReceive);
			/*printf ("Not IGMP Pkt,Receive ERROR!\n"); */
			/*free ((void *) IgmpPkt);
			   /*device_driver_free ((void *) IgmpPkt); 
			   IgmpPkt = NULL; */
			return ERROR;
		}

		if (IpHeaderInPkt->ttl != 1)	/*IGMP包TTL必须等于1,参见RFC2236 */
		{
			if (bIgmpDebug)
			{
				printf ("TTL != 1,Receive ERROR!\n");
			}
			ulIGMPDropPktAll++;
			ulIGMPReceiveBadPktAll++;
			semGive (semIgmpReceive);
			/*printf ("TTL != 1,Receive ERROR!\n"); */
			/*free ((void *) IgmpPkt);
			   /*device_driver_free ((void *) IgmpPkt);
			   IgmpPkt = NULL; */
			return ERROR;
		}

		if (!VerifyChecksum ((UCHAR *) IpHeaderInPkt, IpHeaderLength, 10))	/*校验和检查 */
		{
			if (bIgmpDebug)
			{
				printf ("Bad IP Checksum,Receive ERROR!\n");
				for (int iii = 0; iii < 64; iii++)
					printf ("%0x - ", *(IgmpPkt + iii));
				printf ("\n");
			}
			ulIGMPDropPktAll++;
			ulIGMPReceiveBadPktAll++;
			semGive (semIgmpReceive);
			/*printf ("Bad IP Checksum,Receive ERROR!\n"); */
			/*free ((void *) IgmpPkt);
			   /*device_driver_free ((void *) IgmpPkt); 
			   IgmpPkt = NULL; */
			return ERROR;
		}

		/*if(ntohl (IpHeaderInPkt->daddr) == 0xe0000016) 
		   IGMPV3Flag = 1; */

		IgmpHeaderInPkt =
			(igmpheader *) (IgmpPkt + IpHeaderLength + 14 + PPPoEFlag * 8);

		if (!VerifyChecksum ((UCHAR *) IgmpHeaderInPkt, sizeof (igmpheader),
				2))
		{
			if (IgmpHeaderInPkt->ucType != IGMPV3MembershipReport)
			{
				if (bIgmpDebug)
				{
					printf ("Bad IGMP Checksum,Receive ERROR!\n");
					for (int iii = 0; iii < 64; iii++)
						printf ("%0x - ", *(IgmpPkt + iii));
					printf ("\n");
				}

				ulIGMPDropPktAll++;
				ulIGMPReceiveBadPktAll++;
				semGive (semIgmpReceive);
				/*free ((void *) IgmpPkt);
				   /*device_driver_free ((void *) IgmpPkt); 
				   IgmpPkt = NULL; */
				return ERROR;
			}

		}

		if (bEnableSecurityOption)
		{
			if ((IgmpHeaderInPkt->ucType == IGMPV2MembershipReport) ||
				(IgmpHeaderInPkt->ucType == IGMPLeaveGroup) ||
				(IgmpHeaderInPkt->ucType == IGMPV3MembershipReport))
			{
				if (IpHeaderLength <= 5)
				{
					if (bIgmpDebug)
					{
						printf ("No Security Option,Receive ERROR!\n");
					}
					ulIGMPDropPktAll++;
					ulIGMPReceiveBadPktAll++;
					semGive (semIgmpReceive);
					/*free ((void *) IgmpPkt);
					   /*device_driver_free ((void *) IgmpPkt); 
					   IgmpPkt = NULL; */
					return ERROR;
				}
				else
				{
					if (htonl (*((UINT *) (IgmpPkt + 34 + PPPoEFlag * 8))) != 0x94040000)	/*Security Option */
					{
						if (bIgmpDebug)
						{
							printf ("Bad Security Option,Receive ERROR!\n");
						}
						ulIGMPDropPktAll++;
						ulIGMPReceiveBadPktAll++;
						semGive (semIgmpReceive);
						/*free ((void *) IgmpPkt);
						   /*device_driver_free ((void *) IgmpPkt); 
						   IgmpPkt = NULL; */
						return ERROR;
					}
				}
			}
		}
		ReceiveIgmpMsg.ucIGMPType = IgmpHeaderInPkt->ucType;
		ReceiveIgmpMsg.unSourceAddress = ntohl (IpHeaderInPkt->saddr);
		ReceiveIgmpMsg.unDestinationAddress = ntohl (IpHeaderInPkt->daddr);
		p = (char *) IgmpHeaderInPkt;
		user = (UCHAR *) (p + 8);
		ReceiveIgmpMsg.user = *user;

		if (IgmpHeaderInPkt->ucType == IGMPV3MembershipReport)
		{
			ReceiveIgmpMsg.unGroupAddress =
				htonl (*((UINT *) (IgmpPkt + 50 + PPPoEFlag * 8)));
		}
		else
		{
			ReceiveIgmpMsg.unGroupAddress =
				ntohl (IgmpHeaderInPkt->unGroupAddress);
		}
		ReceiveIgmpMsg.ucMaxResponseTime =
			(UCHAR) (IgmpHeaderInPkt->ucMaxRespTime / 10);
		ReceiveIgmpMsg.usPort = usPort;
		ReceiveIgmpMsg.usSessionId = usSessionId;
	}
	else					/*带有VLAN TAG */
	{
		ReceiveIgmpMsg.VlanTag = htons (*((USHORT *) (IgmpPkt + 14)));
		if (htons (*((USHORT *) (IgmpPkt + 16))) == 0x8864)	/*有PPPoE封装 */
		{
			//printf("PPPoE w 802.1q\n");
			PPPoEFlag = 1;
			IpHeaderInPkt = (ipheader *) (IgmpPkt + 26);
			usSessionId = htons (*((USHORT *) (IgmpPkt + 20)));
		}
		else				/*无PPPoE封装 */
		{
			PPPoEFlag = 0;
			IpHeaderInPkt = (ipheader *) (IgmpPkt + 18);
			usSessionId = 0;
		}

		IpHeaderLength = IpHeaderInPkt->headerlength * 4;
		IpProtocolInPkt = IpHeaderInPkt->protocol;
		if (IpProtocolInPkt != 2)
		{
			if (bIgmpDebug)
			{
				printf ("Not IGMP Pkt,Receive ERROR!\n");
			}
			ulIGMPDropPktAll++;
			ulIGMPReceiveBadPktAll++;
			semGive (semIgmpReceive);
			/*free ((void *) IgmpPkt);
			   /*device_driver_free ((void *) IgmpPkt);
			   IgmpPkt = NULL; */
			return ERROR;
		}

		if (IpHeaderInPkt->ttl != 1)
		{
			printf ("TTL != 1,Receive ERROR!\n");
			ulIGMPDropPktAll++;
			ulIGMPReceiveBadPktAll++;
			semGive (semIgmpReceive);
			/*free ((void *) IgmpPkt);
			   /*device_driver_free ((void *) IgmpPkt); 
			   IgmpPkt = NULL; */
			return ERROR;
		}
		/*
		   if(PPPoEFlag)
		   {
		   IgmpHeaderInPkt = (igmpheader *) (IgmpPkt + IpHeaderLength * 4 + 26);
		   }
		   else
		   { */
		IgmpHeaderInPkt =
			(igmpheader *) (IgmpPkt + IpHeaderLength + 18 + PPPoEFlag * 8);

		/*} */
		if (bEnableSecurityOption)
		{
			if ((IgmpHeaderInPkt->ucType == IGMPV2MembershipReport) ||
				(IgmpHeaderInPkt->ucType == IGMPLeaveGroup) ||
				(IgmpHeaderInPkt->ucType == IGMPV3MembershipReport))
			{
				if (IpHeaderLength <= 5)
				{
					if (bIgmpDebug)
					{
						printf ("No Security Option,Receive ERROR!\n");
					}
					ulIGMPDropPktAll++;
					ulIGMPReceiveBadPktAll++;
					semGive (semIgmpReceive);
					/*free ((void *) IgmpPkt);
					   /*device_driver_free ((void *) IgmpPkt); 
					   IgmpPkt = NULL; */
					return ERROR;
				}
				else
				{
					if (htonl (*((UINT *) (IgmpPkt + 38 + PPPoEFlag * 8))) != 0x94040000)	/*Security Option */
					{
						if (bIgmpDebug)
						{
							printf ("Bad Security Option,Receive ERROR!\n");
						}
						ulIGMPDropPktAll++;
						ulIGMPReceiveBadPktAll++;
						semGive (semIgmpReceive);
						/*free ((void *) IgmpPkt);
						   /*device_driver_free ((void *) IgmpPkt); 
						   IgmpPkt = NULL; */
						return ERROR;
					}
				}
			}
		}

		ReceiveIgmpMsg.ucIGMPType = IgmpHeaderInPkt->ucType;
		//printf("type = 0x%x\n",IgmpHeaderInPkt->ucType);
		ReceiveIgmpMsg.unSourceAddress = ntohl (IpHeaderInPkt->saddr);
		ReceiveIgmpMsg.unDestinationAddress = ntohl (IpHeaderInPkt->daddr);
		if (IgmpHeaderInPkt->ucType == IGMPV3MembershipReport)
		{
			ReceiveIgmpMsg.unGroupAddress =
				htonl (*((UINT *) (IgmpPkt + 54 + PPPoEFlag * 8)));
		}
		else
		{
			ReceiveIgmpMsg.unGroupAddress =
				ntohl (IgmpHeaderInPkt->unGroupAddress);
		}
		ReceiveIgmpMsg.ucMaxResponseTime =
			(UCHAR) (IgmpHeaderInPkt->ucMaxRespTime / 10);
		ReceiveIgmpMsg.usPort = usPort;
		ReceiveIgmpMsg.usSessionId = usSessionId;
	}
	timetick1 = tickGet ();

	/*发送到消息队列 */
	timetick2 = tickGet ();
	ReceiveIgmpMsg.tick = tickGet ();
	nReturnValue =
		msgQSend (IGMPMsgQId, (char *) &ReceiveIgmpMsg, sizeof (IGMPMsg),
		NO_WAIT, MSG_PRI_NORMAL);
	if (nReturnValue == OK)
	{
		ulIGMPSendToQueueAll++;
	}
	semGive (semIgmpReceive);
	timetick3 = tickGet ();
	return (nReturnValue);
}

/***************************************************
函数名称:IgmpReceive
函数功能:IGMP Proxy对外接口函数,负责将IGMP包送入消息队列
输入参数:usPort 接收数据包的源端口号
	 	 IgmpPkt 指向数据包的指针
	 	 usLength 数据包的长度
返回值:	 0  表示成功
	  	-1 表示失败
  备注:   将输入参数unPVC改为unPort ,这样可以去掉与pvc的转换过程   aibin  2005-01-14
***************************************************/
int IgmpReceive_bak (USHORT usPort, char *IgmpPkt, USHORT usLength)
{
	IGMPMsg ReceiveIgmpMsg;
	ipheader *IpHeaderInPkt;
	igmpheader *IgmpHeaderInPkt;
	USHORT IpHeaderLength;
	UINT groupaddress;

	/*UINT DestinationAddressInPkt; */
	UCHAR IpProtocolInPkt;
	UINT MsgNum;
	int nReturnValue;
	UCHAR *user;			/*线卡上的用户端口aibin 2005-3-24 */
	USHORT usSessionId;
	char *p;
	char *p1;
	int *pp;
	int i;
	USHORT PPPoEFlag = 0;
	USHORT IGMPV3Flag = 0;

	/*USHORT usPort = TransformPVCToPort (unPVC);  modified by aibin 2005-01-14 */

	if (bIgmpDebug)
	{
		printf ("\n\rreceive from Slot %d:\r\n", usPort);
		for (i = 0; i < usLength; i++)
		{
			printf ("%02x-", *(IgmpPkt + i));
			if ((i > 1) && (i % 30 == 0))
				printf ("\n\r");
		}
		printf ("\r\n");
	}
	/*协议报文来自自己发出的,丢弃 */
	if (memcmp (IgmpPkt + 6, (char *) gEtherInfo.etherMac, 6) == 0)
	{
		if (bIgmpDebug)
		{
			printf ("Error! Igmp Packet comes from myself!\r\n");
		}
		/*free ((void *) IgmpPkt);
		   /*device_driver_free ((void *) IgmpPkt); 
		  IgmpPkt = NULL; */
		return ERROR;
	}

/*
if (htonl (*((ULONG *) (IgmpPkt + 26))) == gEtherInfo.ipAddr)
{
	return ERROR;
}

⌨️ 快捷键说明

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