Ejemplo n.º 1
0
        public void ShowPackets(ArrayList TcpIP_RawPackages)
        {
            //Flow flow = new Flow(); //处理流量的类
            RawCapture ep;
            Packet     packet;
            double     len;
            TcpPacket  tcpPacket;

            #region Declare Variable 声明变量
            for (int i = 0; i < TcpIP_RawPackages.Count; i++)
            {
                // 处理
                ep        = ((RawCapture)TcpIP_RawPackages[i]);
                packet    = Packet.ParsePacket(ep.LinkLayerType, ep.Data);
                len       = ep.Data.Length;
                tcpPacket = (TcpPacket)packet.Extract(typeof(TcpPacket));
                if (tcpPacket != null)//if (tcpPacket != null || udpPacket != null)
                {
                    IpPacket ipPacket = (IpPacket)(tcpPacket.ParentPacket);
                    // 目的、源的IP地址和端口
                    IPAddress srcIp = ipPacket.SourceAddress;
                    IPAddress dstIp = ipPacket.DestinationAddress;

                    int          srcPort = tcpPacket.SourcePort;
                    int          dstPort = tcpPacket.DestinationPort;
                    ListViewItem lvItem1 = new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[0].Text = "TCP";
                    lvItem1.SubItems.Add(srcIp.ToString());
                    lvItem1.SubItems.Add(dstIp.ToString());
                    lvItem1.SubItems.Add(len.ToString("#.###"));
                    listView1.Items.Add(lvItem1);
                    int  port;          // 本机端口
                    bool isUp, isIn;    // 是否上传,是否内网

                    // 判断是否为上传
                    if (srcIp.Equals(hostIP))
                    {
                        port = srcPort;
                        isUp = true;
                        isIn = IsInnerNet(dstIp);
                    }
                    else
                    {
                        port = dstPort;
                        isUp = false;
                        isIn = IsInnerNet(srcIp);
                    }

                    if (!isIn && isUp)
                    {
                        total_up += (len / 1024 / 1024);
                    }
                    if (!isIn && !isUp)
                    {
                        total_down += (len / 1024 / 1024);
                    }
                    if (!isIn)
                    {
                        TotalFlow += (len / 1024);
                    }
                }
            }
            #endregion
            if (TotalFlow > 1024)
            {
                total_flow_lable.Text = (TotalFlow / 1024).ToString("#.###");
                flow_unit.Text        = "MB";
            }
            else
            {
                total_flow_lable.Text = TotalFlow.ToString("#.###");
            }

            if (TotalFlow > threshold * 1024)
            {
                ExcessFlow excessFlow = new ExcessFlow(data_flow_warn);//ShowPackets OutText
                this.BeginInvoke(excessFlow, new object[] { threshold });
            }
        }
Ejemplo n.º 2
0
        public void ShowPackets(ArrayList TcpIP_RawPackages)
        {
            //Flow flow = new Flow(); //处理流量的类
            RawCapture ep;
            Packet packet;
            double len;
            TcpPacket tcpPacket;
            #region Declare Variable 声明变量
            for (int i = 0; i < TcpIP_RawPackages.Count; i++)
            {
                // 处理
                ep = ((RawCapture)TcpIP_RawPackages[i]);
                packet = Packet.ParsePacket(ep.LinkLayerType, ep.Data);
                len = ep.Data.Length;
                tcpPacket = (TcpPacket)packet.Extract(typeof(TcpPacket));
                if (tcpPacket != null)//if (tcpPacket != null || udpPacket != null)
                {
                    IpPacket ipPacket = (IpPacket)(tcpPacket.ParentPacket);
                    // 目的、源的IP地址和端口
                    IPAddress srcIp = ipPacket.SourceAddress;
                    IPAddress dstIp = ipPacket.DestinationAddress;

                    int srcPort = tcpPacket.SourcePort;
                    int dstPort = tcpPacket.DestinationPort;
                    ListViewItem lvItem1 = new ListViewItem();
                    lvItem1.SubItems.Clear();
                    lvItem1.SubItems[0].Text="TCP";
                    lvItem1.SubItems.Add(srcIp.ToString());
                    lvItem1.SubItems.Add(dstIp.ToString());
                    lvItem1.SubItems.Add(len.ToString("#.###"));
                    listView1.Items.Add(lvItem1);
                    int port;           // 本机端口
                    bool isUp, isIn;    // 是否上传,是否内网

                    // 判断是否为上传
                    if (srcIp.Equals(hostIP))
                    {
                        port = srcPort;
                        isUp = true;
                        isIn = IsInnerNet(dstIp);
                    }
                    else
                    {
                        port = dstPort;
                        isUp = false;
                        isIn = IsInnerNet(srcIp);
                    }

                    if (!isIn && isUp) total_up += (len/1024/1024);
                    if (!isIn && !isUp) total_down += (len/1024/1024);
                    if(!isIn) TotalFlow += (len /1024);
                }

            }
            #endregion
            if (TotalFlow > 1024)
            {
                total_flow_lable.Text = (TotalFlow / 1024).ToString("#.###");
                flow_unit.Text = "MB";
            }
            else
                total_flow_lable.Text = TotalFlow.ToString("#.###");

            if (TotalFlow > threshold * 1024)
            {
                ExcessFlow excessFlow = new ExcessFlow(data_flow_warn);//ShowPackets OutText
                this.BeginInvoke(excessFlow, new object[] { threshold });
            }
        }