Пример #1
0
        /// <summary>
        /// 解析接收的数据包,形成PacketArrivedEventArgs事件数据类对象,并引发PacketArrival事件
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="len"></param>
        private void Receive(byte[] buf, int len)
        {
            var e = PacketArrivedEventArgs.ParseFrom(buf, len);//新网络数据包信息事件

            switch (e.Protocol.ToUpper())
            {
            case "TCP":
                var te = TcpPacketArrivedEventArgs.ParseTcpFrom(buf, len);
                OnPacketArrival(te);
                return;

            case "UDP":
                var ue = UdpPacketArrivedEventArgs.ParseUdpFrom(buf, len);
                OnPacketArrival(ue);
                return;
            }

            //引发PacketArrival事件
            OnPacketArrival(e);
        }
Пример #2
0
        /// <summary>
        /// 解析UDP包
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public unsafe static UdpPacketArrivedEventArgs ParseUdpFrom(byte[] buf, int len)
        {
            var e = new UdpPacketArrivedEventArgs();//新网络数据包信息事件

            fixed(byte *fixedBuf = buf)
            {
                var head = (UdpHeader *)fixedBuf;//把数据流整和为IPHeader结构

                e.HeaderLength    = (uint)(head->IpHeader.IpVerLen & 0x0F) << 2;
                e.UdpHeaderLength = 8;
                var tempProtocol = head->IpHeader.IpProtocol;

                switch (tempProtocol) //提取协议类型
                {
                case 1:
                    e.Protocol = "ICMP";
                    break;

                case 2:
                    e.Protocol = "IGMP";
                    break;

                case 6:
                    e.Protocol = "TCP";
                    break;

                case 17:
                    e.Protocol = "UDP";
                    break;

                default:
                    e.Protocol = "UNKNOWN";
                    break;
                }

                var tempVersion = (uint)(head->IpHeader.IpVerLen & 0xF0) >> 4;

                e.IpVersion = tempVersion.ToString(CultureInfo.InvariantCulture);

                //以下语句提取出了PacketArrivedEventArgs对象中的其他参数
                var tempIpSrcaddr  = head->IpHeader.IpSrcAddr;
                var tempIpDestaddr = head->IpHeader.IpDestAddr;
                var tempIp         = new IPAddress(tempIpSrcaddr);

                e.OriginationAddress = tempIp.ToString();
                tempIp = new IPAddress(tempIpDestaddr);
                e.DestinationAddress = tempIp.ToString();

                var tempSrcport = *(short *)&fixedBuf[e.HeaderLength];
                var tempDstport = *(short *)&fixedBuf[e.HeaderLength + 2];

                e.OriginationPort = IPAddress.NetworkToHostOrder(tempSrcport).ToString(CultureInfo.InvariantCulture);
                e.DestinationPort = IPAddress.NetworkToHostOrder(tempDstport).ToString(CultureInfo.InvariantCulture);

                e.PacketLength     = (uint)len;
                e.MessageLength    = (uint)len - e.HeaderLength;
                e.UdpMessageLength = e.MessageLength - e.UdpHeaderLength;

                e.ReceiveBuffer = buf;
                //把buf中的IP头赋给PacketArrivedEventArgs中的IPHeaderBuffer
                Array.Copy(buf, 0, e.IpHeaderBuffer, 0, (int)e.HeaderLength);
                //把buf中的包中内容赋给PacketArrivedEventArgs中的MessageBuffer
                Array.Copy(buf, (int)e.HeaderLength, e.MessageBuffer, 0, (int)e.MessageLength);
                //
                Array.Copy(buf, (int)e.HeaderLength, e.UdpHeaderBuffer, 0, (int)e.UdpHeaderLength);
                //
                Array.Copy(buf, (int)(e.HeaderLength + e.UdpHeaderLength), e.UdpMessageBuffer, 0,
                           (int)e.UdpMessageLength);
            }

            return(e);
        }