Пример #1
0
        static void Main(string[] args)
        {
            // New cache controller.
            byte[] router = CacheController.GetCachedAddress("10.0.0.1");

            Console.WriteLine(AdapterController.GetGateway());

            // Make sure we got something.
            // Byte array length should be the same as a mac address length in bytes.
            if (router != null)
            {
                Console.WriteLine(BytesToString(router));
            }
            else
            {
                Console.WriteLine("Router has no cached address?");
            }

            // Get device list from local machine
            IList <LivePacketDevice> allDevices;

            try
            {
                allDevices = LivePacketDevice.AllLocalMachine;
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.Message);
                allDevices = new List <LivePacketDevice>();
            }

            if (allDevices.Count == 0)
            {
                Console.WriteLine("No interfaces found. Make sure libpcap/WinPcap is properly installed on the local machine.");
                return;
            }

            // Print the list
            Console.WriteLine("\nInterface Device List:");
            for (int i = 0; i != allDevices.Count; i++)
            {
                LivePacketDevice device = allDevices[i];
                Console.Write((i + 1) + ". " + device.Name + "  " + string.Join(", ", device.Addresses));
                if (args.Length > 0)
                {
                    if (args[0] == "-d")
                    {
                        DevicePrint(allDevices[i]);
                    }
                }
                if (device.Description != null)
                {
                    Console.WriteLine(" (" + device.Description + ")");
                }
                else
                {
                    Console.WriteLine(" (No description available)");
                }
            }

            int deviceIndex = 0;

            do
            {
                Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
                string deviceIndexString = Console.ReadLine();
                if (!int.TryParse(deviceIndexString, out deviceIndex) ||
                    deviceIndex < 1 || deviceIndex > allDevices.Count)
                {
                    deviceIndex = 0;
                }
            } while (deviceIndex == 0);

            ThreadStart childref = new ThreadStart(PacketCounter);

            Console.WriteLine("In Main: Creating the Child thread");
            Thread childThread = new Thread(childref);

            childThread.Start();

            // Take the selected adapter
            PacketDevice selectedDevice = allDevices[deviceIndex - 1];

            adapterId = selectedDevice.Name.Split('_')[1];

            // open the device
            using (PacketCommunicator communicator =
                       selectedDevice.Open(65536,
                                           PacketDeviceOpenAttributes.Promiscuous,
                                           1000))
            {
                Console.WriteLine("Listening on " + selectedDevice.Description + "...");

                // Start the capture
                communicator.SetFilter("arp");
                communicator.ReceivePackets(0, PacketHandlerArp);
            }
        }