Пример #1
0
        public static void Main(string[] args)
        {
            string verWinPCap = null;
            int    count      = 0;

            verWinPCap = Tamir
                         .IPLib
                         .Version
                         .GetVersionString();
            PcapDeviceList getNetConnections = SharpPcap.GetAllDevices();

            Console.WriteLine("WinPCap Version: {0}", verWinPCap);
            Console.WriteLine("Connected devices:\r\n");
            foreach (PcapDevice net in getNetConnections)
            {
                Console.WriteLine("{0}) {1}", count, net.PcapDescription);
                Console.WriteLine("\tName:\t{0}", net.PcapName);
                Console.WriteLine("\tMode:\t\t\t{0}", net.PcapMode);
                Console.WriteLine("\tIP Address: \t\t{0}", net.PcapIpAddress);
                Console.WriteLine("\tLoopback: \t\t{0}", net.PcapLoopback);
                Console.WriteLine();
                count++;
            }
            Console.Write("Press any <RETURN> to exit");
            Console.Read();
        }
Пример #2
0
        public static void Main3(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

            /* Print SharpPcap version */
            Console.WriteLine("SharpPcap {0}, ArpTest.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 for sending the ARP request: ");
            i = int.Parse(Console.ReadLine());

            string device = devices[i].PcapName;

            String ip = "";

            while (true)
            {
                Console.Write("-- Please enter IP address to be resolved by ARP: ");
                ip = Console.ReadLine();
                if (IPUtil.IsIP(ip))
                {
                    break;
                }
                Console.WriteLine("Bad IP address format, please try again");
            }

            //Create a new ARP resolver
            //(for more info, see:
            //http://www.tamirgal.com/home/SourceView.aspx?Item=SharpPcap&File=ARP.cs)
            ARP arper = new ARP(device);

            //print the resolved address
            Console.WriteLine(ip + " is at: " + arper.Resolve(ip));
        }
Пример #3
0
        private void Start()
        {
            PcapDeviceList _PcapDeviceList = SharpPcap.GetAllDevices();
            PcapDevice     _PcapDevice     = _PcapDeviceList[0];

            _PcapDevice.PcapOnPacketArrival += new SharpPcap.PacketArrivalEvent(_PcapDevice_PcapOnPacketArrival);
            _PcapDevice.PcapOpen(false);
            _PcapDevice.PcapSetFilter("tcp and ip");
            _PcapDevice.PcapCapture(-1);
        }
Пример #4
0
        private void Form1_Load2(object sender, System.EventArgs e)
        {
            //Run();
            devices = SharpPcap.GetAllDevices();
            foreach (PcapDevice device in devices)
            {
                device.PcapOpen();
            }
            Thread th = new Thread(new ThreadStart(Run));

            th.Start();
        }
Пример #5
0
        private void PacketCapture_Load(object sender, EventArgs e)
        {
            try
            {
                this.promiscuousCheckbox.Enabled = true;
                this.DumpToFileCheckbox.Enabled  = true;
                this.StopCaptureButton.Enabled   = false;
                this.AmberPicture.Visible        = true;
                this.GreenPicture.Visible        = false;
                this.RedPicture.Visible          = false;
                this.updater     = this.UpdateUI;
                this.stopUpdater = this.PcapStopped;
                this.devices     = SharpPcap.GetAllDevices();
                foreach (PcapDevice device in this.devices)
                {
                    this.comboBox1.Items.Add(device.PcapDescription);
                }
                if (this.devices.Count > 0)
                {
                    this.comboBox1.SelectedIndex = 1;
                }
                this.webBrowser1.DocumentStream = new MemoryStream(Encoding.Default.GetBytes(Resources.Filtering));
            }
            catch (Exception exc)
            {
                this.Enabled = false;
                if (exc is BadImageFormatException)
                {
                    Logging.Info(
                        "Terminals Packet Capture is not configured to work with this system (Bad Image Format Exception)",
                        exc);
                    MessageBox.Show(
                        "Terminals Packet Capture is not configured to work with this system (Bad Image Format Exception)");
                }
                else if (exc is DllNotFoundException)
                {
                    Logging.Info("WinpPcap was not installed", exc);
                    ExternalLinks.ShowWinPCapPage();
                }
                else
                {
                    Logging.Info("WinpPcap was not installed correctly", exc);
                }
            }

            this.PacketCapture_Resize(null, null);
        }
Пример #6
0
        public static void Run(string[] args)
        {
            string S    = "Hello";
            int    lLen = EthernetFields_Fields.ETH_HEADER_LEN;
            //bytes = System.Convert.ToByte(S);
            const int MIN_PKT_LEN = 42;

            byte[] data  = System.Text.Encoding.ASCII.GetBytes("HELLO");
            byte[] bytes = new byte[MIN_PKT_LEN + data.Length];
            Array.Copy(data, 0, bytes, MIN_PKT_LEN, data.Length);

            PcapDeviceList devices = SharpPcap.GetAllDevices();
            PcapDevice     device  = devices[2];
            NetworkDevice  netdev  = (NetworkDevice)device;

            UDPPacket packet = new UDPPacket(lLen, bytes);

            //Ethernet Fields
            packet.DestinationHwAddress = "001122334455";
            packet.SourceHwAddress      = netdev.MacAddress;
            packet.EthernetProtocol     = EthernetProtocols_Fields.IP;


            //IP Fields
            packet.DestinationAddress = "58.100.187.167";

            packet.SourceAddress  = netdev.IpAddress;
            packet.IPProtocol     = IPProtocols_Fields.UDP;
            packet.TimeToLive     = 20;
            packet.Id             = 100;
            packet.Version        = 4;
            packet.IPTotalLength  = bytes.Length - lLen;
            packet.IPHeaderLength = IPFields_Fields.IP_HEADER_LEN;

            //UDP Fields
            packet.DestinationPort = 9898;
            packet.SourcePort      = 80;
            packet.ComputeIPChecksum();
            packet.ComputeUDPChecksum();

            device.PcapOpen();
            device.PcapSendPacket(packet);
            device.PcapClose();
        }
Пример #7
0
 private void PacketCapture_Load(object sender, EventArgs e)
 {
     try {
         promiscuousCheckbox.Enabled = true;
         DumpToFileCheckbox.Enabled  = true;
         StopCaptureButton.Enabled   = false;
         AmberPicture.Visible        = true;
         GreenPicture.Visible        = false;
         RedPicture.Visible          = false;
         updater     = new MethodInvoker(UpdateUI);
         stopUpdater = new MethodInvoker(PcapStopped);
         devices     = SharpPcap.GetAllDevices();
         foreach (PcapDevice device in devices)
         {
             comboBox1.Items.Add(device.PcapDescription);
         }
         if (devices.Count > 0)
         {
             comboBox1.SelectedIndex = 1;
         }
         this.webBrowser1.DocumentStream = new System.IO.MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(Terminals.Properties.Resources.Filtering));
     } catch (Exception exc) {
         this.Enabled = false;
         if (exc is System.BadImageFormatException)
         {
             Terminals.Logging.Log.Debug("Terminals Packet Capture is not configured to work with this system (Bad Image Format Exception)", exc);
             System.Windows.Forms.MessageBox.Show("Terminals Packet Capture is not configured to work with this system (Bad Image Format Exception)");
         }
         else if (exc is System.DllNotFoundException)
         {
             Terminals.Logging.Log.Debug("WinpPcap was not installed", exc);
             if (System.Windows.Forms.MessageBox.Show("It appears that WinPcap is not installed.  In order to use this feature within Terminals you must first install that product.  Do you wish to visit the download location right now?", "Download WinPcap?", MessageBoxButtons.OKCancel) == DialogResult.OK)
             {
                 System.Diagnostics.Process.Start("http://www.winpcap.org/install/default.htm");
             }
         }
         else
         {
             Terminals.Logging.Log.Debug("WinpPcap was not installed correctly", exc);
         }
     }
     this.PacketCapture_Resize(null, null);
 }
Пример #8
0
 public DevicesForm()
 {
     InitializeComponent();
     try
     {
         this.deviceList = SharpPcap.GetAllDevices();
         foreach (PcapDevice pd in deviceList)
         {
             this.comboBoxDevices.Items.Add(pd.PcapDescription);
         }
         this.comboBoxDevices.SelectedIndex = 0;
     }
     catch (DllNotFoundException)
     {
         MessageBox.Show("It seem's you haven't installed WinPcap.\n"
                         + "You can get it from http://www.winpcap.org .", "Missing DLL", MessageBoxButtons.OK, MessageBoxIcon.Error);
         Environment.Exit(1);
     }
 }
Пример #9
0
        public static void Main(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

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

            /* 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();
            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);
                Console.WriteLine();
                /* Name */
                Console.WriteLine("\tName:\t{0}", dev.PcapName);
                /* IP Address */
                Console.WriteLine("\tIP Address: \t\t{0}", dev.PcapIpAddress);
                /* Is Loopback */
                Console.WriteLine("\tLoopback: \t\t{0}", dev.PcapLoopback);

                Console.WriteLine();
                i++;
            }
            Console.Write("Hit 'Enter' to exit...");
            Console.ReadLine();
        }
Пример #10
0
        private void Initialize()
        {
            try
            {
                // Try to find a Networkinterface
                this.deviceList = SharpPcap.GetAllDevices();
            }
            catch (DllNotFoundException)
            {
                MessageBox.Show("Es sieht so aus das du kein WinPcap installiert hast. besorg es dir von hier: http://www.winpcap.org", "Fehlende DLL-Datei", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(1);
            }
            catch (Exception e)
            {
#if DEBUG
                Program.debugStream.WriteLine(e.Message);
                Program.debugStream.WriteLine(e.StackTrace);
                Program.debugStream.WriteLine(e.Source);
#endif
            }

            if (this.deviceList.Count < 1)
            {
                MessageBox.Show("Keine Netzwerk Schnittstellen gefunden =(");
                return;
            }
            try
            {
                this.beep = new SoundPlayer(Environment.CurrentDirectory + "\\data\\ok.wav");
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show(Environment.CurrentDirectory + "\\data\\ok.wav nicht gefunden");
            }
            if (!OptionsForm.Instance.MuteSounds)
            {
                this.beep.Play();
            }

            this.startSniffers();
        }
Пример #11
0
        public static void Main1(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

            /* Print SharpPcap version */
            Console.WriteLine("SharpPcap {0}, SendTcpSynExample.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 for sending: ");
            i = int.Parse(Console.ReadLine());

            PcapDevice device = devices[i];

            SendTcpSyn((NetworkDevice)device);
        }
Пример #12
0
        public static void Main(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

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

            /* 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();
            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 gather statistics on: ");
            i = int.Parse(Console.ReadLine());

            PcapDevice device = devices[i];

            //Register our handler function to the 'pcap statistics' event
            device.PcapOnPcapStatistics +=
                new Tamir.IPLib.SharpPcap.PcapStatisticsEvent(device_PcapOnPcapStatistics);

            //Open the device for capturing
            //true -- means promiscuous mode
            //1000 -- means stats will be collected 1000ms
            device.PcapOpen(true, 1000);

            //Handle TCP packets only
            device.PcapSetFilter("tcp");

            //Set device to statistics mode
            device.PcapMode = PcapMode.Statistics;

            Console.WriteLine();
            Console.WriteLine("-- Gathering statistics on \"{0}\", hit 'Enter' to stop...",
                              device.PcapDescription);

            //Start the capturing process
            device.PcapStartCapture();

            //Wait for 'Enter' from the user.
            Console.ReadLine();

            //Stop the capturing process
            device.PcapStopCapture();

            //Close the pcap device
            device.PcapClose();
            Console.WriteLine("Capture stopped, device closed.");
            Console.Write("Hit 'Enter' to exit...");
            Console.ReadLine();
        }
Пример #13
0
 public PcapDeviceList GetInterfaces()
 {
     /* Retrieve the device list */
     return(SharpPcap.GetAllDevices());
 }
Пример #14
0
        public static void Main(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

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

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

            if (devices.Count < 1)
            {
                Console.WriteLine("No device found on this machine");
                return;
            }

            Console.WriteLine();
            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);
                Console.WriteLine();
                /* Name */
                Console.WriteLine("\tName:\t\t{0}", dev.PcapName);
                /* Is Loopback */
                Console.WriteLine("\tLoopback:\t\t{0}", dev.PcapLoopback);

                /*
                 *      If the device is a physical network device,
                 *      lets print some advanced info
                 */
                if (dev is NetworkDevice)
                {                //Then..
                    /* Cast to NetworkDevice */
                    NetworkDevice netDev = (NetworkDevice)dev;
                    /* Print advanced info */
                    Console.WriteLine("\tIP Address:\t\t{0}", netDev.IpAddress);
                    Console.WriteLine("\tSubnet Mask:\t\t{0}", netDev.SubnetMask);
                    Console.WriteLine("\tMAC Address:\t\t{0}", netDev.MacAddress);
                    Console.WriteLine("\tDefault Gateway:\t{0}", netDev.DefaultGateway);
                    Console.WriteLine("\tPrimary WINS:\t\t{0}", netDev.WinsServerPrimary);
                    Console.WriteLine("\tSecondary WINS:\t\t{0}", netDev.WinsServerSecondary);
                    Console.WriteLine("\tDHCP Enabled:\t\t{0}", netDev.DhcpEnabled);
                    Console.WriteLine("\tDHCP Server:\t\t{0}", netDev.DhcpServer);
                    Console.WriteLine("\tDHCP Lease Obtained:\t{0}", netDev.DhcpLeaseObtained);
                    Console.WriteLine("\tDHCP Lease Expires:\t{0}", netDev.DhcpLeaseExpires);
                    Console.WriteLine("\tAdmin Status:\t{0}", netDev.AdminStatus);
                    Console.WriteLine("\tMedia State:\t{0}", netDev.MediaState);
                }
                Console.WriteLine();
                i++;
            }
            Console.Write("Hit 'Enter' to exit...");
            Console.ReadLine();
        }
Пример #15
0
        public static void Main(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

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

            /* 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();
            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 capture: ");
            i = int.Parse(Console.ReadLine());

            PcapDevice device = devices[i];

            //Register our handler function to the 'packet arrival' event
            device.PcapOnPacketArrival +=
                new SharpPcap.PacketArrivalEvent(device_PcapOnPacketArrival);

            //Open the device for capturing
            //true -- means promiscuous mode
            //1000 -- means a read wait of 1000ms
            device.PcapOpen(true, 1000);

            Console.WriteLine();
            Console.WriteLine("-- Listenning on {0}, hit 'Enter' to stop...",
                              device.PcapDescription);

            //Start the capturing process
            device.PcapStartCapture();

            //Wait for 'Enter' from the user.
            Console.ReadLine();

            //Stop the capturing process
            device.PcapStopCapture();

            Console.WriteLine("-- Capture stopped.");

            //Close the pcap device
            device.PcapClose();
        }
Пример #16
0
        public static void Main(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

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

            /* 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();
            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 capture: ");
            i = int.Parse(Console.ReadLine());

            PcapDevice device = devices[i];

            //Open the device for capturing
            //true -- means promiscuous mode
            //1000 -- means a read wait of 1000ms
            device.PcapOpen(true, 1000);

            Console.WriteLine();
            Console.WriteLine("-- Listenning on {0}...",
                              device.PcapDescription);

            Packet packet;

            //Keep capture packets using PcapGetNextPacket()
            while ((packet = device.PcapGetNextPacket()) != null)
            {
                // Prints the time and length of each received packet
                DateTime time = packet.PcapHeader.Date;
                int      len  = packet.PcapHeader.PacketLength;
                Console.WriteLine("{0}:{1}:{2},{3} Len={4}",
                                  time.Hour, time.Minute, time.Second, time.Millisecond, len);
            }

            //Close the pcap device
            device.PcapClose();
            Console.WriteLine("-- Timeout (1000ms) elapsed, capture stopped, device closed.");
            Console.Write("Hit 'Enter' to exit...");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

            /* Print SharpPcap version */
            Console.WriteLine("SharpPcap {0}, Example12.PacketManipulation.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("{0}) {1}", i, "Read packets from offline pcap file");

            Console.WriteLine();
            Console.Write("-- Please choose a device to capture: ");
            int choice = int.Parse(Console.ReadLine());

            PcapDevice device = null;

            if (choice == i)
            {
                Console.Write("-- Please enter an input capture file name: ");
                string capFile = Console.ReadLine();
                device = SharpPcap.GetPcapOfflineDevice(capFile);
            }
            else
            {
                device = devices[choice];
            }


            //Register our handler function to the 'packet arrival' event
            device.PcapOnPacketArrival +=
                new Tamir.IPLib.SharpPcap.PacketArrivalEvent(device_PcapOnPacketArrival);

            //Open the device for capturing
            //true -- means promiscuous mode
            //1000 -- means a read wait of 1000ms
            device.PcapOpen(true, 1000);

            Console.WriteLine();
            Console.WriteLine
                ("-- Listenning on {0}, hit 'Ctrl-C' to exit...",
                device.PcapDescription);

            //Start capture 'INFINTE' number of packets
            device.PcapCapture(SharpPcap.INFINITE);

            //Close the pcap device
            //(Note: this line will never be called since
            // we're capturing infinite number of packets
            device.PcapClose();
        }
Пример #18
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");
        }
Пример #19
0
 static NetWorkController()
 {
     _devicelist = SharpPcap.GetAllDevices();
 }
Пример #20
0
        public static void Main(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

            /* Print SharpPcap version */
            Console.WriteLine("SharpPcap {0}, Example6.DumpTCP.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 capture: ");
            i = int.Parse(Console.ReadLine());

            PcapDevice device = devices[i];

            //Register our handler function to the 'packet arrival' event
            device.PcapOnPacketArrival +=
                new SharpPcap.PacketArrivalEvent(device_PcapOnPacketArrival);

            //Open the device for capturing
            //true -- means promiscuous mode
            //1000 -- means a read wait of 1000ms
            device.PcapOpen(true, 1000);

            //tcpdump filter to capture only TCP/IP packets
            string filter = "ip and tcp";

            //Associate the filter with this capture
            device.PcapSetFilter(filter);

            Console.WriteLine();
            Console.WriteLine
                ("-- The following tcpdump filter will be applied: \"{0}\"",
                filter);
            Console.WriteLine
                ("-- Listenning on {0}, hit 'Ctrl-C' to exit...",
                device.PcapDescription);

            //Start capture 'INFINTE' number of packets
            device.PcapCapture(SharpPcap.INFINITE);

            //Close the pcap device
            //(Note: this line will never be called since
            // we're capturing infinite number of packets
            device.PcapClose();
        }
Пример #21
0
        public static void Main(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 != "") && (!resp.StartsWith("y")))
            {
                Console.WriteLine("Cancelled by user!");
                return;
            }

            //Open the device
            device.PcapOpen();

            //Generate a random packet
            byte[] bytes = GetRandomPacket();

            try
            {
                //Send the packet out the network device
                device.PcapSendPacket(bytes);
                Console.WriteLine("-- Packet sent successfuly.");
            }
            catch (Exception e)
            {
                Console.WriteLine("-- " + e.Message);
            }

            //Close the pcap device
            device.PcapClose();
            Console.WriteLine("-- Device closed.");
            Console.Write("Hit 'Enter' to exit...");
            Console.ReadLine();
        }
Пример #22
0
        public static void Main(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

            /* 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.GetPcapOfflineDevice(capFile);
                //Open the device for capturing
                device.PcapOpen();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }

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

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

            try
            {
                //Go through all packets in the file and add to the queue
                while ((packet = device.PcapGetNextPacket()) != 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;

            PcapDeviceList devices = SharpPcap.GetAllDevices();

            /* 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 transmit on: ");
            i = int.Parse(Console.ReadLine());
            devices[i].PcapOpen();
            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].PcapClose();
                    return;
                }
            }
            device.PcapClose();
            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.PcapSendQueue(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.PcapLastError, sent);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
            //Free the queue
            squeue.Dispose();
            Console.WriteLine("-- Queue is disposed.");
            //Close the pcap device
            device.PcapClose();
            Console.WriteLine("-- Device closed.");
            Console.Write("Hit 'Enter' to exit...");
            Console.ReadLine();
        }