private void SendCaptureFile()
        {
            if (!ValidateUserInput())
            {
                return;
            }

            // Get the network device through which the packets will be sent.
            PcapDevice device = chooseDeviceUserControl.Device;

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

            PcapSendQueue sendQueue = null;

            bool isDeviceOpenedInThisForm = false;

            try
            {
                Cursor = Cursors.WaitCursor;

                if (!device.Opened)
                {
                    device.Open();
                    isDeviceOpenedInThisForm = true;
                }

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

                // Allocate a new send queue
                sendQueue = new PcapSendQueue
                                ((int)((PcapOfflineDevice)offlineDevice).FileSize);

                Packet packet;

                // Go through all packets in the file and add to the queue
                while ((packet = offlineDevice.GetNextPacket()) != null)
                {
                    if (!sendQueue.Add(packet))
                    {
                        MessageBox.Show(
                            "Packet buffer too small, not all the packets will be sent.",
                            "Warning",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Warning);

                        break;
                    }
                }

                if (device.PcapDataLink != offlineDevice.PcapDataLink)
                {
                    DialogResult answer = MessageBox.Show(
                        "The datalink of the capture differs from the one of the" +
                        "\nselected interface. Do you want to continue?",
                        "Question",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question);

                    if (answer == DialogResult.No)
                    {
                        return;
                    }
                }

                int bytesSent = device.SendQueue(sendQueue, false);

                if (bytesSent < sendQueue.CurrentLength)
                {
                    MessageBox.Show(
                        String.Format("An error occurred sending the packets: {0}.\nOnly {1} bytes were sent.", device.LastError, bytesSent),
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // Close the pcap device
                offlineDevice.Close();

                // Free the queue
                if (sendQueue != null)
                {
                    sendQueue.Dispose();
                }

                if (isDeviceOpenedInThisForm)
                {
                    device.Close();
                }

                Cursor = Cursors.Default;
            }
        }
        public static void Main(string[] args)
        {
            string ver = SharpPcap.Version.VersionString;
            /* Print SharpPcap version */
            Console.WriteLine("SharpPcap {0}, Example10.SendQueues.cs", ver);

            Console.WriteLine();
            Console.Write("-- Please enter an input capture file name: ");
            string capFile = Console.ReadLine();

            PcapDevice device;

            try
            {
                //Get an offline file pcap device
                device = SharpPcap.Pcap.GetPcapOfflineDevice( capFile );
                //Open the device for capturing
                device.Open();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            Console.Write("Queueing packets...");

            //Allocate a new send queue
            PcapSendQueue squeue = new PcapSendQueue
                ( (int)((PcapOfflineDevice)device).FileSize );
            Packet packet;

            try
            {
                //Go through all packets in the file and add to the queue
                while( (packet=device.GetNextPacket()) != null )
                {
                    if( !squeue.Add( packet ) )
                    {
                        Console.WriteLine("Warning: packet buffer too small, "+
                            "not all the packets will be sent.");
                        break;
                    }
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            Console.WriteLine("OK");

            Console.WriteLine();
            Console.WriteLine("The following devices are available on this machine:");
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine();

            int i=0;

            List<PcapDevice> devices = SharpPcap.Pcap.GetAllDevices();
            /* Scan the list printing every entry */
            foreach(PcapDevice dev in devices)
            {
                /* Description */
                Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);
                i++;
            }

            Console.WriteLine();
            Console.Write("-- Please choose a device to transmit on: ");
            i = int.Parse( Console.ReadLine() );
            devices[i].Open();
            string resp;

            if(devices[i].PcapDataLink != device.PcapDataLink)
            {
                Console.Write("Warning: the datalink of the capture"+
                    " differs from the one of the selected interface, continue? [YES|no]");
                resp = Console.ReadLine().ToLower();

                if((resp!="")&&( !resp.StartsWith("y")))
                {
                    Console.WriteLine("Cancelled by user!");
                    devices[i].Close();
                    return;
                }
            }
            device.Close();
            device = devices[i];

            Console.Write("This will transmit all queued packets through"+
                " this device, continue? [YES|no]");
            resp = Console.ReadLine().ToLower();

            if((resp!="")&&( !resp.StartsWith("y")))
            {
                Console.WriteLine("Cancelled by user!");
                return;
            }

            try
            {
                Console.Write("Sending packets...");
                int sent = device.SendQueue( squeue, true );
                Console.WriteLine("Done!");
                if( sent < squeue.CurrentLength )
                {
                    Console.WriteLine("An error occurred sending the packets: {0}. "+
                        "Only {1} bytes were sent\n", device.LastError, sent);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine( "Error: "+e.Message );
            }
            //Free the queue
            squeue.Dispose();
            Console.WriteLine("-- Queue is disposed.");
            //Close the pcap device
            device.Close();
            Console.WriteLine("-- Device closed.");
            Console.Write("Hit 'Enter' to exit...");
            Console.ReadLine();
        }
Beispiel #3
0
        public static void Main(string[] args)
        {
            string ver = SharpPcap.Version.VersionString;

            /* Print SharpPcap version */
            Console.WriteLine("SharpPcap {0}, Example10.SendQueues.cs", ver);

            Console.WriteLine();
            Console.Write("-- Please enter an input capture file name: ");
            string capFile = Console.ReadLine();

            PcapDevice device;

            try
            {
                //Get an offline file pcap device
                device = SharpPcap.Pcap.GetPcapOfflineDevice(capFile);
                //Open the device for capturing
                device.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            Console.Write("Queueing packets...");

            //Allocate a new send queue
            PcapSendQueue squeue = new PcapSendQueue
                                       ((int)((PcapOfflineDevice)device).FileSize);
            Packet packet;

            try
            {
                //Go through all packets in the file and add to the queue
                while ((packet = device.GetNextPacket()) != null)
                {
                    if (!squeue.Add(packet))
                    {
                        Console.WriteLine("Warning: packet buffer too small, " +
                                          "not all the packets will be sent.");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            Console.WriteLine("OK");

            Console.WriteLine();
            Console.WriteLine("The following devices are available on this machine:");
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine();

            int i = 0;

            List <PcapDevice> devices = SharpPcap.Pcap.GetAllDevices();

            /* Scan the list printing every entry */
            foreach (PcapDevice dev in devices)
            {
                /* Description */
                Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);
                i++;
            }

            Console.WriteLine();
            Console.Write("-- Please choose a device to transmit on: ");
            i = int.Parse(Console.ReadLine());
            devices[i].Open();
            string resp;

            if (devices[i].PcapDataLink != device.PcapDataLink)
            {
                Console.Write("Warning: the datalink of the capture" +
                              " differs from the one of the selected interface, continue? [YES|no]");
                resp = Console.ReadLine().ToLower();

                if ((resp != "") && (!resp.StartsWith("y")))
                {
                    Console.WriteLine("Cancelled by user!");
                    devices[i].Close();
                    return;
                }
            }
            device.Close();
            device = devices[i];

            Console.Write("This will transmit all queued packets through" +
                          " this device, continue? [YES|no]");
            resp = Console.ReadLine().ToLower();

            if ((resp != "") && (!resp.StartsWith("y")))
            {
                Console.WriteLine("Cancelled by user!");
                return;
            }

            try
            {
                Console.Write("Sending packets...");
                int sent = device.SendQueue(squeue, true);
                Console.WriteLine("Done!");
                if (sent < squeue.CurrentLength)
                {
                    Console.WriteLine("An error occurred sending the packets: {0}. " +
                                      "Only {1} bytes were sent\n", device.LastError, sent);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
            //Free the queue
            squeue.Dispose();
            Console.WriteLine("-- Queue is disposed.");
            //Close the pcap device
            device.Close();
            Console.WriteLine("-- Device closed.");
            Console.Write("Hit 'Enter' to exit...");
            Console.ReadLine();
        }
Beispiel #4
0
        static void Main1(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

            /* Print SharpPcap version */
            Console.WriteLine("SharpPcap {0}, Example9.SendPacket.cs", ver);
            Console.WriteLine();

            /* Retrieve the device list */
            PcapDeviceList devices = SharpPcap.GetAllDevices();

            /*If no device exists, print error */
            if (devices.Count < 1)
            {
                Console.WriteLine("No device found on this machine");
                return;
            }

            Console.WriteLine("The following devices are available on this machine:");
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine();

            int i = 0;

            /* Scan the list printing every entry */
            foreach (PcapDevice dev in devices)
            {
                /* Description */
                Console.WriteLine("{0}) {1}", i, dev.PcapDescription);
                i++;
            }

            Console.WriteLine();
            Console.Write("-- Please choose a device to send a packet on: ");
            i = int.Parse(Console.ReadLine());

            PcapDevice device = devices[i];

            Console.Write("-- This will send a random packet out this interface," +
                          "continue? [YES|no]");
            string resp = Console.ReadLine().ToLower();

            //If user refused, exit program
            if (resp == "n" || resp == "no")
            {
                return;
            }

            PcapSendQueue queue = new PcapSendQueue(10000);

            byte[] bytes;
            bytes = GetRandomPacket();
            queue.Add(GetRandomPacket(), 0, 0);
            Console.WriteLine(queue.CurrentLength);
            queue.Add(GetRandomPacket(), 0, 10);
            Console.WriteLine(queue.CurrentLength);
            queue.Add(GetRandomPacket(), 2, 0);
            Console.WriteLine(queue.CurrentLength);
            queue.Add(GetRandomPacket(), 2, 1);
            Console.WriteLine(queue.CurrentLength);

            //Open the device
            device.PcapOpen();
            Console.WriteLine(device.PcapSendQueue(queue, true));
            Console.WriteLine(device.PcapLastError);
            device.PcapClose();
            queue.Dispose();
            Console.WriteLine(" device closed");
        }