예제 #1
0
        /// <summary>Initializes the Ethernet device by opening the TAP device and
        /// starting the ISource thread.</summary>
        /// <param name="tap">The name of the TAP device on the host.</param>
        public Ethernet(string device_name)
        {
            for (int i = 0; i < 15; i++)
            {
                try {
                    _tap = Ipop.Tap.TapDevice.GetTapDevice(device_name);
                    break;
                } catch {}
                Thread.Sleep(2);
                ProtocolLog.WriteIf(ProtocolLog.Exceptions,
                                    "Unable to set up the tap, trying again...");
            }

            if (_tap == null)
            {
                ProtocolLog.WriteIf(ProtocolLog.Exceptions, "Unable to set up the tap.");
                Environment.Exit(1);
            }

            Address = _tap.Address;

            _running     = true;
            _read_thread = new Thread(ReadLoop);
            _read_thread.IsBackground = true;
            _read_thread.Start();
        }
예제 #2
0
        public static IEnumerable <TapDevice> GetTapDevices()
        {
            var guid           = GetClassGuid();
            var propertyBuffer = new StringBuilder();
            var deviceInfoData = new SetupApi.SP_DEVINFO_DATA();
            var deviceInfoSet  = SetupApi.SetupDiGetClassDevsA(ref guid, 0, IntPtr.Zero,
                                                               SetupApi.DIGCF_PRESENT);

            if (deviceInfoSet == IntPtr.Add(IntPtr.Zero, -1))

            {
            }

            var cont = true;

            for (var i = 0; cont; i++)
            {
                deviceInfoData.cbSize = (uint)Marshal.SizeOf(typeof(SetupApi.SP_DEVINFO_DATA));
                ;
                //is devices exist for class
                deviceInfoData.DevInst   = 0;
                deviceInfoData.ClassGuid = guid;
                deviceInfoData.Reserved  = (IntPtr)0;

                var res = SetupApi.SetupDiEnumDeviceInfo(deviceInfoSet,
                                                         (uint)i, ref deviceInfoData);
                if (!res && deviceInfoData.DevInst == 0)
                {
                    cont = false;
                    break;
                }


                propertyBuffer.Capacity = SetupApi.MAX_DEV_LEN;

                uint index        = 0;
                uint requiredSize = 0;
                res = SetupApi.SetupDiGetDeviceRegistryProperty(deviceInfoSet,
                                                                ref deviceInfoData, SetupApi.SPDRP_DEVICEDESC, out index,
                                                                propertyBuffer, SetupApi.MAX_DEV_LEN,
                                                                out requiredSize);
                if (!res)
                {
                    var error = SetupApi.GetLastError();
                    SetupApi.SetupDiDestroyDeviceInfoList(deviceInfoSet);
                    throw new Win32Exception(error);
                }

                var device = new TapDevice
                {
                    Name    = propertyBuffer.ToString(),
                    DevInst = deviceInfoData.DevInst
                };
                index        = 0;
                requiredSize = 0;
                res          = SetupApi.SetupDiGetDeviceRegistryProperty(deviceInfoSet,
                                                                         ref deviceInfoData, SetupApi.SPDRP_HARDWAREID, out index,
                                                                         propertyBuffer, SetupApi.MAX_DEV_LEN,
                                                                         out requiredSize);
                if (!res)
                {
                    var error = SetupApi.GetLastError();
                    SetupApi.SetupDiDestroyDeviceInfoList(deviceInfoSet);
                    throw new Win32Exception(error);
                }
                device.HardwareId = propertyBuffer.ToString();
                if (device.HardwareId == HardwareID)
                {
                    yield return(device);
                }
            }
        }