Esempio n. 1
0
        //Captures packets while issuing concurrent calls to update the GUI
        /// <summary>
        /// Capture packets and stores their information
        /// </summary>
        private void CapturePackets()
        {
            // Take the selected adapter
            PacketDevice selectedDevice = allDevices[this.deviceIndex];
            int          prevInd        = 0;

            // Open the device
            using (PacketCommunicator communicator =
                       selectedDevice.Open(65536,                                  // portion of the packet to capture
                                                                                   // 65536 guarantees that the whole packet will be captured on all the link layers
                                           PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                                           1000))                                  // read timeout
            {
                communicator.SetFilter(filter);
                while (captFlag)
                {
                    PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out Packet packet);
                    byte[] packetInfo;

                    switch (result)
                    {
                    case PacketCommunicatorReceiveResult.Timeout:
                        // Timeout elapsed
                        break;

                    case PacketCommunicatorReceiveResult.Ok:
                        IpV4Datagram ipv4 = packet.Ethernet.IpV4;
                        IpV4Protocol i    = ipv4.Protocol;

                        CougarPacket cp = new CougarPacket(packet.Timestamp.ToString("hh:mm:ss.fff"),
                                                           ++packetNumber,
                                                           packet.Length,
                                                           ipv4.Source.ToString(),
                                                           ipv4.Destination.ToString());

                        packetInfo = Encoding.ASCII.GetBytes(cp.ToString() + "\n");
                        packetBytes.Add(packetInfo);

                        this.Invoke((MethodInvoker)(() =>
                        {
                            packetView.Items.Add(new ListViewItem(cp.toPropertyArray()));

                            ++prevInd;
                            if (chkAutoScroll.Checked && prevInd > 12)
                            {
                                packetView.Items[packetView.Items.Count - 1].EnsureVisible();
                                prevInd = 0;
                            }
                        }));
                        break;

                    default:
                        throw new InvalidOperationException("The result " + result + " should never be reached here");
                    }
                }
            }
        }
        private void PrintPacket(Packet packet) // main packet handler method (should probably be renamed to the original HandleLoadedPacket name to make more sense)
        {
            /*if (packet.DataLink.Kind.ToString().Equals("Ethernet"))
             * {
             *  //Console.WriteLine(packet.Ethernet.EtherType);
             *  if (packet.Ethernet.EtherType.ToString().Equals("IpV4"))
             *  {
             *      Console.WriteLine(packet.Ethernet.IpV4.Protocol);
             *  }
             * }*/


            // if (packet.Ethernet.IsValid)
            {
                ++packetNumber;
                CougarPacket cp = CougarPacket.DetermineCorrectPacketFormat(packet, sensorAddress, packetNumber);  // create new cougarpacket wtih proper protocol information related to this particular packet
                packets.Add(packet);
                cougarpackets.Add(cp);
                //if (cp.SourceAddress.ToString() != "0.0.0.0")
                this.Invoke((MethodInvoker)(() =>                               // this is used to access the main form from within a separate thread (i.e. this capture thread)
                {
                    packetView.Items.Add(new ListViewItem(cp.ToPropertyArray)); // add packet info to listview (must be string array)
                    packetBytes.Add(Encoding.ASCII.GetBytes(cp.ToString() + "\n"));
                    if (adjusted == false)                                      // these next few lines are for resizing the listview items.  should only be called once after 10 packets have shown up
                    {
                        if ((packetNumber % 10 == 0))
                        {
                            packetView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                            packetView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                            adjusted = true;
                        }
                    }
                    if (chkAutoScroll.Checked)
                    {
                        packetView.Items[packetView.Items.Count - 1].EnsureVisible();
                    }
                }));
            }
        }