Exemplo n.º 1
0
        private void Reader_OnPacketArrival(object sender, SharpPcap.CaptureEventArgs e)
        {
            PacketCherryPick packetData = new PacketCherryPick();

            packetData.Length = e.Packet.Data.Length;
            packetData.Time   = e.Packet.Timeval.Date;

            var packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);

            var ipPacket = (IpPacket)packet.Extract(typeof(IpPacket));

            if (ipPacket != null)
            {
                packetData.DestinationIp = ipPacket.DestinationAddress;
                packetData.SourceIp      = ipPacket.SourceAddress;
                packetData.Protocol      = ipPacket.Protocol;
            }

            var tcpPacket = (TcpPacket)packet.Extract(typeof(TcpPacket));

            if (tcpPacket != null)
            {
                packetData.DestinationPort = tcpPacket.DestinationPort;
                packetData.SourcePort      = tcpPacket.SourcePort;
            }

            //udpPacket etc
            //now signal the cherry picked data to all clients
        }
Exemplo n.º 2
0
        static void dev_OnPacketArrival(object sender, SharpPcap.CaptureEventArgs e)
        {
            var time = e.Packet.Timeval.Date;
            var len  = e.Packet.Data.Length;
            var aa   = PacketDotNet.Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);

            Console.WriteLine(aa?.PayloadPacket);
        }
Exemplo n.º 3
0
        static void dev_OnPacketArrival(object sender, SharpPcap.CaptureEventArgs e)
        {
            var time = e.Packet.Timeval.Date;
            var len  = e.Packet.Data.Length;

            Console.WriteLine("{0}:{1}:{2},{3} Len={4}",
                              time.Hour, time.Minute, time.Second, time.Millisecond, len);
            Console.WriteLine(e.Packet.ToString());
        }
Exemplo n.º 4
0
 // This method is invoked whenever a packet recieved from network interface()
 public void OnPacketArrival(SharpPcap.CaptureEventArgs e)
 {
     try
     {
         // Generate a new DataGridViewRow
         NetParser.GenerateDataGridViewRow(e);
     }
     catch
     {
         // Do nothing.
     }
 }
Exemplo n.º 5
0
        public void packetHandler(object sender, SharpPcap.CaptureEventArgs e)
        {
            // Start NAT on first call
            if (natThread == null)
            {
                natThread = new System.Threading.Thread(() => NAT.EntryPoint());
                natThread.Start();
            }

#if DEBUG
            Console.WriteLine("RX packet {0:x16}", BitConverter.ToUInt64(e.Packet.Data, 0));
#endif

#if MININET
            // Update checksums for mininet testing.
            var pkt = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
            if (pkt.PayloadPacket is IpPacket)
            {
                var ip = (IPv4Packet)pkt.PayloadPacket;
                if (ip.Checksum != ip.CalculateIPChecksum())
                {
                    Console.WriteLine("! IP Checksum incorrect ({0:x4} != {1:x4})", ip.Checksum, ip.CalculateIPChecksum());
                }
                ip.UpdateIPChecksum();
            }
            if (pkt.PayloadPacket.PayloadPacket is TcpPacket)
            {
                var tcp = (TcpPacket)pkt.PayloadPacket.PayloadPacket;
                if (tcp.Checksum != tcp.CalculateTCPChecksum())
                {
                    Console.WriteLine("! TCP Checksum incorrect ({0:x4} != {1:x4})", tcp.Checksum, tcp.CalculateTCPChecksum());
                }
                tcp.UpdateTCPChecksum();
            }
#endif

            // Create data object (NOTE this involves copy the data)
            int intfNumber = PaxConfig.rdeviceMap[e.Device.Name];
            var data       = MockUtil.CreateData(e.Packet.Data, intfNumber);

            // Change endianness
            var frame = new FrameInfo(data, TimeSpan.Zero);
            //frame.SwapTdataEndianness(); // FIXME this doesn't seem to be needed? Look into ways to avoid copying then

            // Queue the frame for processing
            NAT.FrameController.QueueReceiveFrame(frame);
        }
Exemplo n.º 6
0
        private void Device_OnPacketArrival(object sender, SharpPcap.CaptureEventArgs e)
        {
            if (e.Packet != null)
            {
                packets_busy_in_.Enqueue(e.Packet);

                /* バッファがいっぱいになったらリストに登録してバッファをリロード */
                if (packets_busy_in_.Count >= READ_PACKET_BLOCK_NUMBER)
                {
                    lock (packets_list_) {
                        packets_list_.Enqueue(packets_busy_in_);
                    }
                    packets_count_in_++;
                    packets_busy_in_ = new Queue <SharpPcap.RawCapture>();
                }
            }
        }
Exemplo n.º 7
0
        public static void Callback_OnPacketArrival(object sender, SharpPcap.CaptureEventArgs e)
        {
            Boolean shouldEventHandlerCalled = true;

            lock (_syncLock)
            {
                if (_stoppingCapture)
                {
                    shouldEventHandlerCalled = false;
                }
            }

            if (shouldEventHandlerCalled)
            {
                // Call Event handler of FormMain (in the context of FormMain thread)
                PacketArrival del = MainForm.OnPacketArrival;
                MainForm.Invoke(del, e);
            }
        }
Exemplo n.º 8
0
        public static void GenerateDataGridViewRow(SharpPcap.CaptureEventArgs e)
        {
            PacketDotNet.Packet p;
            PacketDotNet.InternetLinkLayerPacket iLinkLayerPacket = null;
            String row_SourceAddress = "";
            String row_DestinationAddress = "";
            String row_Protocol = "";
            Int32 row_PacketLength = 0;
            DataGridViewCellStyle row_cellStyle = new DataGridViewCellStyle();
            String row_Time;

            // Compute the time of packet arrival
            TimeSpan timeInterval;
            timeInterval = (e.Packet.Timeval.Date.ToLocalTime() - CaptureStatistic.StartOfCapture);
            row_Time = timeInterval.Minutes.ToString() + ":" + timeInterval.Seconds.ToString() + "::" + timeInterval.Milliseconds.ToString();
            row_cellStyle.BackColor = Color.White;

            // Get PacketDotNet.Packet object
            p = PacketDotNet.Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
            CaptureStatistic.NumberOfCapturedPackets++;

            switch (e.Packet.LinkLayerType)
            {
                case PacketDotNet.LinkLayers.Ethernet:
                    #region Parse Ethernet Packets
                    PacketDotNet.EthernetPacket etherPacket = PacketDotNet.EthernetPacket.GetEncapsulated(p);
                    iLinkLayerPacket = etherPacket;

                    // Get Some Ethernet Packet's fields
                    row_PacketLength = etherPacket.Bytes.Length;
                    switch (etherPacket.Type)
                    {
                        case PacketDotNet.EthernetPacketType.IpV4:
                            CaptureStatistic.CapturedIpV4++;
                            PacketDotNet.IPv4Packet ipv4Packet = (PacketDotNet.IPv4Packet)PacketDotNet.IPv4Packet.GetEncapsulated(etherPacket);
                            row_SourceAddress = ipv4Packet.SourceAddress.ToString();
                            row_DestinationAddress = ipv4Packet.DestinationAddress.ToString();
                            row_Protocol = "IPv4/" + ipv4Packet.Protocol.ToString();
                            switch (ipv4Packet.Protocol)
                            {
                                case PacketDotNet.IPProtocolType.TCP:
                                    CaptureStatistic.CapturedTcpV4++;
                                    row_cellStyle = ProtocolColor.TCP;
                                    break;
                                case PacketDotNet.IPProtocolType.UDP:
                                    CaptureStatistic.CapturedUdpV4++;
                                    row_cellStyle = ProtocolColor.UDP;
                                    break;
                                case PacketDotNet.IPProtocolType.GRE:
                                    CaptureStatistic.CapturedGre++;
                                    row_cellStyle = ProtocolColor.GRE;
                                    break;
                                case PacketDotNet.IPProtocolType.ICMP:
                                    CaptureStatistic.CapturedIcmpV4++;
                                    row_cellStyle = ProtocolColor.ICMP;
                                    break;
                                case PacketDotNet.IPProtocolType.IGMP:
                                    CaptureStatistic.CapturedIgmp++;
                                    row_cellStyle = ProtocolColor.IGMP;
                                    break;
                            }

                            break;

                        case PacketDotNet.EthernetPacketType.IpV6:
                            CaptureStatistic.CapturedIpV6++;
                            PacketDotNet.IPv6Packet ipv6Packet = (PacketDotNet.IPv6Packet)PacketDotNet.IPv6Packet.GetEncapsulated(etherPacket);
                            row_SourceAddress = ipv6Packet.SourceAddress.ToString();
                            row_DestinationAddress = ipv6Packet.DestinationAddress.ToString();
                            row_Protocol = "IPv6/" + ipv6Packet.NextHeader.ToString();
                            switch (ipv6Packet.NextHeader)
                            {
                                case PacketDotNet.IPProtocolType.GRE:
                                    CaptureStatistic.CapturedGre++;
                                    break;
                                case PacketDotNet.IPProtocolType.ICMP:
                                    CaptureStatistic.CapturedIcmpV4++;
                                    break;
                                case PacketDotNet.IPProtocolType.ICMPV6:
                                    CaptureStatistic.CapturedIcmpV6++;
                                    break;
                                case PacketDotNet.IPProtocolType.IGMP:
                                    CaptureStatistic.CapturedIgmp++;
                                    break;
                                case PacketDotNet.IPProtocolType.TCP:
                                    CaptureStatistic.CapturedTcpV6++;
                                    break;
                                case PacketDotNet.IPProtocolType.UDP:
                                    CaptureStatistic.CapturedUdpV6++;
                                    break;
                                default:
                                    break;
                            }
                            break;
                        case PacketDotNet.EthernetPacketType.Arp:
                            CaptureStatistic.CapturedArp++;
                            row_Protocol = "ARP";
                            row_SourceAddress = etherPacket.SourceHwAddress.ToString();
                            row_DestinationAddress = "Broadcast";
                            row_cellStyle = ProtocolColor.ARP;
                            break;

                        default:
                            row_Protocol = etherPacket.Type.ToString();
                            break;
                    }
                    #endregion
                    break;
            }

            // Add packet to the DataGridVeiw
            DataGridViewRow newRow = (DataGridViewRow)MainFormDataGridView.RowTemplate.Clone();

            newRow.CreateCells(MainFormDataGridView);
            newRow.Tag = iLinkLayerPacket;
            newRow.SetValues(CaptureStatistic.NumberOfCapturedPackets, row_Time, row_SourceAddress,
                row_DestinationAddress, row_Protocol, row_PacketLength);
            foreach (DataGridViewCell cell in newRow.Cells)
                cell.Style = row_cellStyle;

            MainFormDataGridView.Rows.Add(newRow);
        }