Esempio n. 1
0
        /// <summary>
        /// Caution: Use the singlton instance unless you know why you need to call this.
        /// One use is for multiple filters on the same physical device. To apply multiple
        /// filters open the same physical device multiple times, one for each
        /// filter by calling this routine and picking the same device out of each list.
        /// </summary>
        /// <returns>
        /// A <see cref="CaptureDeviceList"/>
        /// </returns>
        public static CaptureDeviceList New()
        {
            var newCaptureDevice = new CaptureDeviceList();

            // windows
            if ((Environment.OSVersion.Platform == PlatformID.Win32NT) ||
                (Environment.OSVersion.Platform == PlatformID.Win32Windows))
            {
                newCaptureDevice.nPcapDeviceList = NPcap.NPcapDeviceList.New();
            }
            else // not windows
            {
                newCaptureDevice.libPcapDeviceList = LibPcap.LibPcapLiveDeviceList.New();
            }

            // refresh the device list to flush the original devices and pull the
            // new ones into the newCaptureDevice
            newCaptureDevice.Refresh();

            return(newCaptureDevice);
        }
Esempio n. 2
0
        private bool initializeCaptureDeviceList()
        {
            DialogResult res = DialogResult.No;
            try
            {
                pcap_devices = CaptureDeviceList.Instance;
            }
            catch (PcapException pex)
            {
            }
            if (System.Environment.OSVersion.Platform == PlatformID.Win32NT && (pcap_devices==null || pcap_devices.Count < 1) )
            {
                res = MessageBox.Show(Resources.message_no_capture_devices_startNPF, "XBSlink error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                if (res == DialogResult.Yes)
                {
                    startNPFdriver();
                    try
                    {
                        pcap_devices = CaptureDeviceList.New();
                    }
                    catch (PcapException pex)
                    {
                    }
                }
            }

            if (pcap_devices!=null)
                foreach (LibPcapLiveDevice dev in pcap_devices)
                    comboBox_captureDevice.Items.Add(dev.Interface.FriendlyName + " (" + dev.Interface.Description + ")");

            if (comboBox_captureDevice.Items.Count > 0)
                comboBox_captureDevice.SelectedIndex = 0;
            else
            {
                if (System.Environment.OSVersion.Platform == PlatformID.Unix)
                    MessageBox.Show(Resources.message_no_capture_devices_unix, "XBSlink error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show(Resources.message_no_capture_devices, "XBSlink error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
Esempio n. 3
0
        public void setLocalAddr(TextBox addresses)
        {
            //first get out IP address and store it for later use
            string str = "";

            stationConsole.Text += "Determine Local Addr" + Environment.NewLine;
            System.Net.NetworkInformation.NetworkInterface[] nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            List <string> ipAddr = new List <string>();

            foreach (NetworkInterface adapter in nics)
            {
                NetworkInterfaceType networkType = adapter.NetworkInterfaceType;
                if (networkType.Equals(NetworkInterfaceType.Ethernet))
                {
                    foreach (var x in adapter.GetIPProperties().UnicastAddresses)
                    {
                        if (x.Address.AddressFamily == AddressFamily.InterNetwork && x.IsDnsEligible)
                        {
                            Console.WriteLine(" IPAddress ........ : {0:x}", x.Address.ToString());

                            ipAddr.Add(x.Address.ToString());
                        }
                    }

                    address = adapter.GetPhysicalAddress();
                    byte[] bytes = address.GetAddressBytes();
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        // Display the physical address in hexadecimal.
                        addresses.Text += bytes[i].ToString("X2");
                        Console.Write("{0}", bytes[i].ToString("X2"));
                        // Insert a hyphen after each byte, unless we are at the end of the
                        // address.
                        if (i != bytes.Length - 1)
                        {
                            Console.Write("-");
                            addresses.Text += "-";
                        }
                    }
                    addresses.Text += Environment.NewLine;
                }
            }


            int count = ipAddr.Count();

            for (int i = 0; i < count; i++)
            {
                byte[] addrBytes = IPAddress.Parse(ipAddr[i]).GetAddressBytes();

                if (addrBytes[0] == 0xC0)
                {
                    localAddr       = IPAddress.Parse(ipAddr[i]);
                    addresses.Text += localAddr;
                    break;
                }
                else
                {
                    stationConsole.Text += "ERROR Unable to determine self IP address!!!" + Environment.NewLine;
                }
            }

            // Print SharpPcap version
            string ver = SharpPcap.Version.VersionString;

            stationConsole.Text += "SharpPcap {0}, Example1.IfList.cs" + ver + Environment.NewLine;

            // Retrieve the device list
            devices = SharpPcap.CaptureDeviceList.Instance;

            // If no devices were found print an error
            if (devices.Count < 1)
            {
                stationConsole.Text += "No devices were found on this machine" + Environment.NewLine;
                return;
            }
            else
            {
                stationConsole.Text += "Device count = " + devices.Count + Environment.NewLine;
            }

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

            // Print out the available network devices
            //foreach (ICaptureDevice dev in devices)
            //    Console.WriteLine("{0}\n", dev.ToString());



            // Open the device for capturing
            int            readTimeoutMilliseconds = 1000;
            ICaptureDevice device;

            filter = "port 20000";
            string localInterfaceUsed = address.ToString();

            for (ushort i = 0; i < devices.Count; i++)
            {
                device = devices[i];
                device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
                var    macAddress = device.MacAddress;
                string hwAddr     = macAddress.ToString();
                if (hwAddr.Equals(localInterfaceUsed))
                {
                    transmitDevice = device;
                }
            }
        }