예제 #1
0
        private void OpenCaptureFile()
        {
            if (!capturedPacketsHaveBeenSaved)
            {
                DialogResult userWish = ShowDialogAskingUserToSaveCaptureData(ActionType.OpeningCaptureFile);

                if (userWish == DialogResult.OK)
                {
                    SaveCapturedPacketsToFile();
                }
                else if (userWish == DialogResult.Cancel)
                {
                    return;
                }
            }

            // Nullify the packets counter and clear the structures that keep
            // the captured packets info
            ClearDataStructures();

            capturedPacketsHaveBeenSaved = true;


            // Get an offline file pcap device
            PcapDevice offlineDevice = new PcapOfflineDevice(openCaptureFileDialog.FileName);

            try
            {
                Cursor = Cursors.WaitCursor;

                // Open the device for capturing
                offlineDevice.Open();

                // Register our handler function to the "packet arrival" event
                offlineDevice.OnPacketArrival += new Pcap.PacketArrivalEvent(device_OnPacketArrival);

                // Start capture "INFINTE" number of packets
                // This method will return when EOF reached.
                offlineDevice.Capture(Pcap.INFINITE);

                // Set the MainForm text
                SetMainFormText(String.Format("{0} - Wi-Fi Sniffer", Path.GetFileName(openCaptureFileDialog.FileName)));

                // Set the status labels texts
                SetStatusLabelsTexts(String.Format("File: \"{0}\"", openCaptureFileDialog.FileName), packetsCount == 0 ? "| No packets" : String.Format("| Displayed packets: {0}", packetsCount));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // Close the pcap device
                offlineDevice.Close();

                Cursor = Cursors.Default;
            }
        }