Example #1
0
        public static void Main(string[] args)
        {
            string ver = SharpPcap.Version.VersionString;
            /* Print SharpPcap version */
            Console.WriteLine("SharpPcap {0}, Example1.IfList.cs", ver);

            /* Retrieve the device list */
            PcapDeviceList devices = new PcapDeviceList();

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

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

            /* Scan the list printing every entry */
            foreach(PcapDevice dev in devices)
                Console.WriteLine("{0}\n",dev.ToString());

            Console.Write("Hit 'Enter' to exit...");
            Console.ReadLine();
        }
Example #2
0
        private static void sniff()
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

            /* Print SharpPcap version */
            Console.WriteLine("SharpPcap {0}, L2PacketDecrypt", 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 = "port 2106 or port 7777";

            //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 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();
        }
Example #3
0
        static void Main(string[] args)
        {
            string ver = Tamir.IPLib.Version.GetVersionString();

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

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

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

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

            int i = 0;

            /* Scan the list printing every entry */
            foreach (PcapDevice dev in devices)
            {
                /* Description */
                debugStream.WriteLine("{0}) {1}", i, dev.PcapDescription);
                debugStream.WriteLine();
                /* Name */
                debugStream.WriteLine("\tName:\t\t{0}", dev.PcapName);
                /* Is Loopback */
                debugStream.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 */
                    debugStream.WriteLine("\tIP Address:\t\t{0}", netDev.IpAddress);
                    debugStream.WriteLine("\tSubnet Mask:\t\t{0}", netDev.SubnetMask);
                    debugStream.WriteLine("\tMAC Address:\t\t{0}", netDev.MacAddress);
                    debugStream.WriteLine("\tDefault Gateway:\t{0}", netDev.DefaultGateway);
                    debugStream.WriteLine("\tPrimary WINS:\t\t{0}", netDev.WinsServerPrimary);
                    debugStream.WriteLine("\tSecondary WINS:\t\t{0}", netDev.WinsServerSecondary);
                    debugStream.WriteLine("\tDHCP Enabled:\t\t{0}", netDev.DhcpEnabled);
                    debugStream.WriteLine("\tDHCP Server:\t\t{0}", netDev.DhcpServer);
                    debugStream.WriteLine("\tDHCP Lease Obtained:\t{0}", netDev.DhcpLeaseObtained);
                    debugStream.WriteLine("\tDHCP Lease Expires:\t{0}", netDev.DhcpLeaseExpires);
                    debugStream.WriteLine("\tAdmin Status:\t{0}", netDev.AdminStatus);
                    debugStream.WriteLine("\tMedia State:\t{0}", netDev.MediaState);
                }
                debugStream.WriteLine();
                i++;
            }
            debugStream.Flush();
            debugStream.Close();
        }
Example #4
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();
        }
        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();
        }