Example #1
0
 protected virtual void OnPacketArrival(PacketArrivedEventArgs e)
 {
     if (PacketArrival != null)
     {
         PacketArrival(this, e);
     }
 }
Example #2
0
        static public void Pack(Object sender, PacketArrivedEventArgs args)
        {
            //Console.Clear();
            Length = Length + args.PacketLength;
            if (args.Protocol == "TCP:")
            {
                TCP++;
            }
            if (args.Protocol == "UDP:")
            {
                UDP++;
            }
            if (args.Protocol == "ICMP:")
            {
                ICMP++;
            }
            if (args.Protocol == "IGMP:")
            {
                IGMP++;
            }
            if (args.Protocol == "UNKNOWN")
            {
                UNKNOWN++;
            }

            //if (args.OriginationPort == Port.ToString() && iPAddress.Contains(IPAddress.Parse(args.OriginationAddress)))
            //    Out = Out + args.PacketLength;
            //if (args.DestinationPort == Port.ToString() && iPAddress.Contains(IPAddress.Parse(args.DestinationAddress)))
            //In = In + args.PacketLength;

            if (iPAddress.Contains(IPAddress.Parse(args.OriginationAddress)))
            {
                AllOut = AllOut + args.PacketLength;
                if (args.OriginationPort == Port.ToString())
                {
                    Out = Out + args.PacketLength;
                }
            }
            else
            {
                AllIn = AllIn + args.PacketLength;
                if (args.DestinationPort == Port.ToString())
                {
                    In = In + args.PacketLength;
                }
            }

            Console.WriteLine("目标IP:" + args.DestinationAddress
                              + "\t目标端口:" + args.DestinationPort
                              + "\t协议版本:" + args.IPVersion
                              + "\t目标地址:" + args.OriginationAddress
                              + "\t目标端口:" + args.OriginationPort
                              + "\tIP包长度:" + args.PacketLength
                              + "\t协议类型:" + args.Protocol);
            //Console.WriteLine("已接收字节数:" + Length);
            // Console.WriteLine(GetSpand(Length));
        }
Example #3
0
            //解析接收的数据包,形成PacketArrivedEventArgs事件数据类对象,并引发OnPacketArrival事件
            unsafe private void Receive(byte[] buf, int len)
            {
                byte      temp_protocol           = 0;
                uint      temp_version            = 0;
                uint      temp_sourceAddress      = 0;
                uint      temp_destinationAddress = 0;
                short     temp_srcport            = 0;
                short     temp_dstport            = 0;
                IPAddress temp_ip;
                PacketArrivedEventArgs e = new PacketArrivedEventArgs();//新网络数据包信息事件

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

                    e.HeaderLength   = (uint)(head->versionAndLength & 0x0F) << 2;
                    e.IPHeaderBuffer = new byte[e.HeaderLength];
                    temp_protocol    = head->protocol;
                    switch (temp_protocol)                              //提取协议类型
                    {
                    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;
                    }
                    temp_version = (uint)(head->versionAndLength & 0xF0) >> 4;//提取IP协议版本
                    e.IPVersion  = temp_version.ToString();
                    //以下语句提取出了PacketArrivedEventArgs对象中的其他参数
                    temp_sourceAddress      = head->sourceAddress;
                    temp_destinationAddress = head->destinationAddress;
                    temp_ip = new IPAddress(temp_sourceAddress);
                    e.OriginationAddress = temp_ip.ToString();
                    temp_ip = new IPAddress(temp_destinationAddress);
                    e.DestinationAddress = temp_ip.ToString();
                    temp_srcport         = *(short *)&fixed_buf[e.HeaderLength];
                    temp_dstport         = *(short *)&fixed_buf[e.HeaderLength + 2];
                    e.OriginationPort    = IPAddress.NetworkToHostOrder(temp_srcport).ToString();
                    e.DestinationPort    = IPAddress.NetworkToHostOrder(temp_dstport).ToString();
                    e.PacketLength       = (uint)len;
                    e.MessageLength      = (uint)len - e.HeaderLength;
                    e.MessageBuffer      = new byte[e.MessageLength];
                    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);
                }

                //引发PacketArrival事件
                OnPacketArrival(e);
            }
        private void HostScannerControlPackageArrived(object sender, PacketArrivedEventArgs e)
        {
            EthernetPacket packet = e.Packet;

            ArpPacket arpPacket = packet.PayloadPacket as ArpPacket;

            if (arpPacket == null || _foundAddresses.Contains(arpPacket.SenderIp))
                return;

            _foundAddresses.Add(arpPacket.SenderIp);

            AddHost(arpPacket);
        }
Example #5
0
        }//黄罡2017.4.29

        //定义封包数据的事件参数类
        private void SaveToPcap(PacketArrivedEventArgs e)
        {
            FileInfo File = new FileInfo(Main.FileSavePath + "\\" + Main.FileName + ".pcap");

            if (File.Length < 50 * 1024 * 1024)
            {
                BuiltPcapFile(e.PacketBuffer, e.PacketLength);
            }
            else
            {
                Main.CreatPcapFile();
                BuiltPcapFile(e.PacketBuffer, e.PacketLength);
            }
        }
Example #6
0
        /// <summary>
        /// Uses the information contained in a PacketDotNet packet to update the
        /// network graph.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void HandlePacketArrived(object sender, PacketArrivedEventArgs e)
        {
            MacFrame frame = e.ArrivedPacket.Extract <MacFrame>();

            switch (frame)
            {
            case DataFrame dataFrame:
                HandleDataFrame(dataFrame);
                break;

            case ManagementFrame managementFrame:

                break;

            default:
                break;
            }
        }
        private void HostScannerControlPackageArrived(object sender, PacketArrivedEventArgs e)
        {
            EthernetPacket packet = e.Packet;

            IpPacket ipPacket = packet.PayloadPacket as IpPacket;

            if (ipPacket == null)
                return;

            TCPPacket tcpPacket = ipPacket.PayloadPacket as TCPPacket;

            if (tcpPacket == null)
                return;

            HttpPacket httpPacket = tcpPacket.PayloadPacket as HttpPacket;

            if (httpPacket == null)
                return;

            if (httpPacket.Content.StartsWith("POST") || httpPacket.Content.StartsWith("GET"))
            {
                AddUrl(httpPacket);
            }
        }
Example #8
0
        /// <summary>
        /// 解析接收的数据包,形成PacketArrivedEventArgs时间数据类对象,并引发PacketArrival事件
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="len"></param>
        unsafe private void Receive(byte[] buf, int len)
        {
            byte      temp_protocol    = 0;
            uint      temp_version     = 0;
            uint      temp_ip_srcaddr  = 0;
            uint      temp_ip_destaddr = 0;
            short     temp_srcport     = 0;
            short     temp_dstport     = 0;
            IPAddress temp_ip;
            PacketArrivedEventArgs e = new PacketArrivedEventArgs();
            udp_hdr *  pUdpheader;  //UDP头结构体指针
            IPHeader * head;        //IP头结构体指针
            tcp_hdr *  pTcpheader;  //TCP头结构体指针
            icmp_hdr * pIcmpheader; //ICMP头结构体指针
            IPHeader2 *Portheader;  //端口指针

            fixed(byte *fixed_buf = buf)
            {
                int lentcp, lenudp, lenicmp, lenip;

                head        = (IPHeader *)fixed_buf;                     //IP头结构体指针
                Portheader  = (IPHeader2 *)fixed_buf;
                pTcpheader  = (tcp_hdr *)(fixed_buf + sizeof(IPHeader)); //TCP头结构体指针
                pUdpheader  = (udp_hdr *)(fixed_buf + sizeof(IPHeader));
                pIcmpheader = (icmp_hdr *)(fixed_buf + sizeof(IPHeader));

                //计算各种包的长度(只有判断是否是该包后才有意义,先计算出来)
                lenip = ntohs(head->ip_totallength);
                try
                {
                    lentcp  = ntohs(head->ip_totallength) - (sizeof(IPHeader) + sizeof(tcp_hdr));
                    lenudp  = ntohs(head->ip_totallength) - (sizeof(IPHeader) + sizeof(udp_hdr));
                    lenicmp = ntohs(head->ip_totallength) - (sizeof(IPHeader) + sizeof(icmp_hdr));
                }
                catch { }

                e.HeaderLength = (uint)(head->ip_verlen & 0x0F) << 2;
                temp_protocol  = head->ip_protocol;
                temp_dstport   = *(short *)&fixed_buf[e.HeaderLength + 2];
                switch (temp_protocol)
                {
                case 1:
                    e.Protocol = "ICMP:"; break;

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

                case 6:
                    e.Protocol        = "TCP:";
                    e.DestinationPort = ntohs(pTcpheader->dport).ToString();
                    break;

                case 17:
                    e.Protocol        = "UDP:";
                    e.DestinationPort = ntohs(pUdpheader->dport).ToString();
                    break;

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

                temp_version         = (uint)(head->ip_verlen & 0xF0) >> 4;
                e.IPVersion          = temp_version.ToString();
                temp_ip_srcaddr      = head->ip_srcaddr;
                temp_ip_destaddr     = head->ip_destaddr;
                temp_ip              = new IPAddress(temp_ip_srcaddr);
                e.OriginationAddress = temp_ip.ToString();
                temp_ip              = new IPAddress(temp_ip_destaddr);
                e.DestinationAddress = temp_ip.ToString();
                temp_srcport         = *(short *)&fixed_buf[e.HeaderLength];
                e.OriginationPort    = ntohs(Portheader->sport).ToString();
                int acb = IPAddress.NetworkToHostOrder(Portheader->sport);

                e.DestinationPort = ntohs(Portheader->dport).ToString();
                int abc = IPAddress.NetworkToHostOrder(Portheader->dport);

                e.PacketLength  = (uint)lenip;
                e.MessageLength = (uint)lenip - e.HeaderLength;
                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);
            }

            //引发PacketArrival事件
            OnPacketArrival(e);
        }
        private void HostScannerControlPackageArrived(object sender, PacketArrivedEventArgs e)
        {
            EthernetPacket packet = e.Packet;

            AddPacket(packet);
        }
Example #10
0
        //解析接收的数据包,形成PacketArrivedEventArgs时间数据类对象,并引发PacketArrival事件
        unsafe private void Receive(byte[] buf, int len)
        {
            byte      temp_protocol    = 0;
            uint      temp_version     = 0;
            uint      temp_ip_srcaddr  = 0;
            uint      temp_ip_destaddr = 0;
            short     temp_srcport     = 0;
            short     temp_dstport     = 0;
            IPAddress temp_ip;

            //return;

            PacketArrivedEventArgs e = new PacketArrivedEventArgs();

            fixed(byte *fixed_buf = buf)
            {
                IPHeader *head = (IPHeader *)fixed_buf;

                e.HeaderLength = (uint)(head->ip_verlen & 0x0F) << 2;

                temp_protocol = head->ip_protocol;
                switch (temp_protocol)
                {
                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;
                }

                temp_version = (uint)(head->ip_verlen & 0xF0) >> 4;
                e.IPVersion  = temp_version.ToString();

                temp_ip_srcaddr      = head->ip_srcaddr;
                temp_ip_destaddr     = head->ip_destaddr;
                temp_ip              = new IPAddress(temp_ip_srcaddr);
                e.OriginationAddress = temp_ip.ToString();
                temp_ip              = new IPAddress(temp_ip_destaddr);
                e.DestinationAddress = temp_ip.ToString();

                temp_srcport      = *(short *)&fixed_buf[e.HeaderLength];
                temp_dstport      = *(short *)&fixed_buf[e.HeaderLength + 2];
                e.OriginationPort = IPAddress.NetworkToHostOrder(temp_srcport).ToString();
                e.DestinationPort = IPAddress.NetworkToHostOrder(temp_dstport).ToString();
                // if(e.DestinationAddress!="211.87.212.116"||int.Parse(e.DestinationPort)>1000)
                // {
                // return;
                // }
                e.PacketLength  = (uint)len;
                e.MessageLength = (uint)len - e.HeaderLength;

                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);
            }

            //引发PacketArrival事件
            OnPacketArrival(e);
        }
Example #11
0
        unsafe private void Receive(byte[] buf, int len)
        {
            byte      temp_protocol    = 0;
            uint      temp_version     = 0;
            uint      temp_ip_srcaddr  = 0;
            uint      temp_ip_destaddr = 0;
            short     temp_srcport     = 0;
            short     temp_dstport     = 0;
            IPAddress temp_ip;

            PacketArrivedEventArgs e = new PacketArrivedEventArgs();

            fixed(byte *fixed_buf = buf)
            {
                IPHeader *head = (IPHeader *)fixed_buf;

                e.HeaderLength = (uint)(head->ip_verlen & 0x0F) << 2;

                //Extract Network Protocal Type
                temp_protocol = head->ip_protocol;
                switch (temp_protocol)
                {
                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;
                }

                //Extract Network Protocal Version
                temp_version = (uint)(head->ip_verlen & 0xF0) >> 4;
                e.IPVersion  = temp_version.ToString();


                temp_ip_srcaddr      = head->ip_srcaddr;
                temp_ip_destaddr     = head->ip_destaddr;
                temp_ip              = new IPAddress(temp_ip_srcaddr);
                e.OriginationAddress = temp_ip.ToString();
                temp_ip              = new IPAddress(temp_ip_destaddr);
                e.DestinationAddress = temp_ip.ToString();

                temp_srcport      = *(short *)&fixed_buf[e.HeaderLength];
                temp_dstport      = *(short *)&fixed_buf[e.HeaderLength + 2];
                e.OriginationPort = IPAddress.NetworkToHostOrder(temp_srcport).ToString();
                e.DestinationPort = IPAddress.NetworkToHostOrder(temp_dstport).ToString();

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

                e.ReceiveBuffer = buf;

                Array.Copy(buf, 0, e.IPHeaderBuffer, 0, (int)e.HeaderLength);

                Array.Copy(buf, (int)e.HeaderLength, e.MessageBuffer, 0, (int)e.MessageLength);
            }

            OnPacketArrival(e);
        }
Example #12
0
        unsafe private void Receive(byte[] buf, int len)
        {
            byte      protocol             = 0;
            uint      version              = 0;
            uint      ipSourceAddress      = 0;
            uint      ipDestinationAddress = 0;
            int       sourcePort           = 0;
            int       destinationPort      = 0;
            IPAddress ip;
            PacketArrivedEventArgs e = new PacketArrivedEventArgs();

            e.ReceiveBuf = buf;
            e.BufLength  = len;
            fixed(byte *FixedBuf = buf)
            {
                IPHeader *head = (IPHeader *)FixedBuf;

                e.IPHeaderLength = (uint)((head->versionAndLength & 0x0f) << 2);
                //一个指向结构体或对象的指针访问其内成员(->指向结构体成员运算符)
                //并将变量versionAndLength的值高四位清0,保留低四位
                //
                protocol = head->protocol;
                switch (protocol)
                {
                case 1:
                    e.Protocol = "ICMP";
                    break;

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

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

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

                case 132:
                    e.Protocol = "SCTP";
                    break;

                default:
                    e.Protocol = "UNKNOWN";
                    break;
                }
                version              = (uint)((head->versionAndLength & 0xf0) >> 4);
                e.Version            = version.ToString();
                ipSourceAddress      = head->sourceAddress;
                ipDestinationAddress = head->destinationAdress;
                ip = new IPAddress(ipSourceAddress);
                e.OriginationAddress = ip.ToString();
                ip = new IPAddress(ipDestinationAddress);
                e.DestinationAddress = ip.ToString();
                sourcePort           = buf[e.IPHeaderLength] * 256 + buf[e.IPHeaderLength + 1];
                destinationPort      = buf[e.IPHeaderLength + 2] * 256 + buf[e.IPHeaderLength + 3];
                e.OriginationPort    = sourcePort.ToString();
                e.DestinationPort    = destinationPort.ToString();
                e.PacketLength       = (uint)len;
                e.MessageLength      = e.PacketLength - e.IPHeaderLength;
                e.PacketBuffer       = buf;
                Array.Copy(buf, (int)e.IPHeaderLength, e.MessageBuffer, 0, (int)e.MessageLength);

                OnPacketArrival(e);//引发PacketArrival事件
            }
        }
Example #13
0
 protected virtual void OnPacketArrival(PacketArrivedEventArgs e)
 {
     PacketArrival?.Invoke(this, e);
 }
Example #14
0
        private void Receive(byte[] buf, int len)
        {
            byte protocol            = 0;
            uint version             = 0;
            int  sourcePort          = 0;
            int  destinationPort     = 0;
            PacketArrivedEventArgs e = new PacketArrivedEventArgs();

            protocol = buf[9];
            switch (protocol)
            {
            case 6:
                e.Protocol = "TCP";
                break;

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

            case 132:
                e.Protocol = "SCTP";
                break;

            default:
                e.Protocol = "UNKNOWN";
                break;
            }
            version         = (uint)((buf[0] & 0xf0) >> 4);
            sourcePort      = buf[20] * 256 + buf[21];
            destinationPort = buf[22] * 256 + buf[23];
            if ((protocol == 6) && (sourcePort == 3868 || destinationPort == 3868))//TCP-diameter  (黄罡)
            {
                e.IPHeaderLength     = (uint)((buf[0] & 0x0f) << 2);
                e.Version            = version.ToString();
                e.OriginationPort    = sourcePort.ToString();
                e.DestinationPort    = destinationPort.ToString();
                e.DestinationAddress = buf[16].ToString() + "." + buf[17].ToString() + "." + buf[18].ToString() + "." + buf[19].ToString();
                e.OriginationAddress = buf[12].ToString() + "." + buf[13].ToString() + "." + buf[14].ToString() + "." + buf[15].ToString();
                e.PacketLength       = (uint)len;
                e.MessageLength      = e.PacketLength - e.IPHeaderLength;
                e.PacketBuffer       = buf;
                OnPacketArrival(e);//引发PacketArrival事件
                SaveToPcap(e);
            }
            else if ((protocol == 17) && (sourcePort == 2123 || destinationPort == 2123))//UDP-GTPv2 (黄罡)
            {
                e.IPHeaderLength     = (uint)((buf[0] & 0x0f) << 2);
                e.Version            = version.ToString();
                e.OriginationPort    = sourcePort.ToString();
                e.DestinationPort    = destinationPort.ToString();
                e.DestinationAddress = buf[16].ToString() + "." + buf[17].ToString() + "." + buf[18].ToString() + "." + buf[19].ToString();
                e.OriginationAddress = buf[12].ToString() + "." + buf[13].ToString() + "." + buf[14].ToString() + "." + buf[15].ToString();
                e.PacketLength       = (uint)len;
                e.MessageLength      = e.PacketLength - e.IPHeaderLength;
                e.PacketBuffer       = buf;
                OnPacketArrival(e);//引发PacketArrival事件
                SaveToPcap(e);
            }
            else if (protocol == 132)
            {
                byte[] sctp = new byte[len - 20];
                Array.Copy(buf, 20, sctp, 0, len - 20);//去除IP头部20字节
                C.DeScChunk(sctp);
                int PPID = C.paylaodProtocolIdentifier;
                if (PPID == 18)//sctp-s1ap
                {
                    e.IPHeaderLength     = (uint)((buf[0] & 0x0f) << 2);
                    e.Version            = version.ToString();
                    e.OriginationPort    = sourcePort.ToString();
                    e.DestinationPort    = destinationPort.ToString();
                    e.DestinationAddress = buf[16].ToString() + "." + buf[17].ToString() + "." + buf[18].ToString() + "." + buf[19].ToString();
                    e.OriginationAddress = buf[12].ToString() + "." + buf[13].ToString() + "." + buf[14].ToString() + "." + buf[15].ToString();
                    e.PacketLength       = (uint)len;
                    e.MessageLength      = e.PacketLength - e.IPHeaderLength;
                    e.PacketBuffer       = buf;
                    OnPacketArrival(e);//引发PacketArrival事件
                    SaveToPcap(e);
                }
                else if ((PPID == 0 || PPID == 46 || PPID == 47 || PPID == 132) && ((sourcePort == 3868) || (destinationPort == 3868) || (sourcePort == 60000) || (destinationPort == 60000)))//sctp-diameter(黄罡)
                {
                    B.DecodeSctpChunk(sctp);
                    if (B.IsDia == 1)
                    {
                        e.IPHeaderLength     = (uint)((buf[0] & 0x0f) << 2);
                        e.Version            = version.ToString();
                        e.OriginationPort    = sourcePort.ToString();
                        e.DestinationPort    = destinationPort.ToString();
                        e.DestinationAddress = buf[16].ToString() + "." + buf[17].ToString() + "." + buf[18].ToString() + "." + buf[19].ToString();
                        e.OriginationAddress = buf[12].ToString() + "." + buf[13].ToString() + "." + buf[14].ToString() + "." + buf[15].ToString();
                        e.PacketLength       = (uint)len;
                        e.MessageLength      = e.PacketLength - e.IPHeaderLength;
                        e.PacketBuffer       = buf;
                        OnPacketArrival(e);//引发PacketArrival事件
                        SaveToPcap(e);
                    }
                }
            }
        }//黄罡2017.4.29
Example #15
0
        //解析接收的数据包,形成PacketArrivedEventArgs时间数据类对象,并引发PacketArrival事件
        private unsafe void Receive(byte[] buf, int len)
        {
            byte temp_protocol = 0;
            uint temp_version = 0;
            uint temp_ip_srcaddr = 0;
            uint temp_ip_destaddr = 0;
            short temp_srcport = 0;
            short temp_dstport = 0;
            IPAddress temp_ip;

            //return;

            PacketArrivedEventArgs e = new PacketArrivedEventArgs();

            fixed (byte* fixed_buf = buf)
            {
                IPHeader* head = (IPHeader*)fixed_buf;
                e.HeaderLength = (uint)(head->ip_verlen & 0x0F) << 2;

                temp_protocol = head->ip_protocol;
                switch (temp_protocol)
                {
                    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;
                }

                temp_version = (uint)(head->ip_verlen & 0xF0) >> 4;
                e.IPVersion = temp_version.ToString();

                temp_ip_srcaddr = head->ip_srcaddr;
                temp_ip_destaddr = head->ip_destaddr;
                temp_ip = new IPAddress(temp_ip_srcaddr);
                e.OriginationAddress = temp_ip.ToString();
                temp_ip = new IPAddress(temp_ip_destaddr);
                e.DestinationAddress = temp_ip.ToString();

                temp_srcport = *(short*)&fixed_buf[e.HeaderLength];
                temp_dstport = *(short*)&fixed_buf[e.HeaderLength + 2];
                e.OriginationPort = IPAddress.NetworkToHostOrder(temp_srcport).ToString();
                e.DestinationPort = IPAddress.NetworkToHostOrder(temp_dstport).ToString();
                // if(e.DestinationAddress!="211.87.212.116"||int.Parse(e.DestinationPort)>1000)
                // {
                // return;
                // }
                e.PacketLength = (uint)len;
                e.MessageLength = (uint)len - e.HeaderLength;

                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);
            }
            //引发PacketArrival事件
            OnPacketArrival(e);
        }
Example #16
0
 public event PacketArrivedEventHandler PacketArrival;//声明时间句柄函数
 private void OnPacketArrival(PacketArrivedEventArgs e)
 {
     PacketArrival?.Invoke(this, e);//触发事件
 }
Example #17
0
        private unsafe void Receive(byte[] buf, int len)
        {
            byte temp_protocol = 0;
            byte temp_flag = 0;
            uint temp_version = 0;
            uint temp_ip_srcaddr = 0;
            uint temp_ip_destaddr = 0;
            short temp_srcport = 0;
            short temp_dstport = 0;
            IPAddress temp_ip;

            PacketArrivedEventArgs e = new PacketArrivedEventArgs();

            fixed (byte* fixed_buf = buf)
            {
                IPHeader* head = (IPHeader*)fixed_buf;
                e.HeaderLength = (uint)(head->ip_verlen & 0x0F) << 2;

                temp_protocol = head->ip_protocol;
                switch (temp_protocol)
                {
                    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;
                }

                temp_version = (uint)(head->ip_verlen & 0xF0) >> 4;
                e.IPVersion = temp_version.ToString();

                temp_ip_srcaddr = head->ip_srcaddr;
                temp_ip_destaddr = head->ip_destaddr;
                temp_ip = new IPAddress(temp_ip_srcaddr);
                e.OriginationAddress = temp_ip.ToString();
                temp_ip = new IPAddress(temp_ip_destaddr);
                e.DestinationAddress = temp_ip.ToString();

                temp_srcport = *(short*)&fixed_buf[e.HeaderLength];
                temp_dstport = *(short*)&fixed_buf[e.HeaderLength + 2];
                temp_flag = *(byte*)&fixed_buf[e.HeaderLength + 13];
                e.Flag = temp_flag.ToString();
                e.OriginationPort = IPAddress.NetworkToHostOrder(temp_srcport).ToString();
                e.DestinationPort = IPAddress.NetworkToHostOrder(temp_dstport).ToString();

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

                e.ReceiveBuffer = buf;
                Array.Copy(buf, 0, e.IPHeaderBuffer, 0, (int)e.HeaderLength);
                Array.Copy(buf, (int)e.HeaderLength, e.MessageBuffer, 0, (int)e.MessageLength);
            }

            OnPacketArrival(e);
        }
Example #18
0
 protected virtual void OnPacketArrival(PacketArrivedEventArgs e)
 {
     if (PacketArrival != null)
     {
         PacketArrival(this, e);
     }
 }