Example #1
0
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            SSIDText.Text = Settings.SSID;
            KeyText.Text  = Settings.Key;

            WlanMgr = new WlanManager();
            if (!WlanMgr.IsHostedNetworkSupported)
            {
                UnsupportedDevices();
                return;
            }

            if (WlanMgr.IsHostedNetworkActive)
            {
                IPhlpapi.FlushIpNetTable(WlanMgr.HostedNetworkGuid);
                WlanMgr.ForceStop();
            }

            NetIList.DropDown += UpdateInterfaceList;
            NetIList.SelectionChangeCommitted += OnInterfaceSelected;
            DataGridList.CellContentClick     += OnCellContentClick;
            DataGridList.CellEndEdit          += OnCellEndEdit;
            ShareButton.MouseClick            += OnShareButtonClick;

            WlanMgr.HostedNetworkStateChange      += OnHostedNetworkStateChange;
            WlanMgr.HostedNetworkPeerStateChanged += OnHostedNetworkPeerStateChanged;
            WlanMgr.WlanSoftwareRadioStateChanged += OnWlanSoftwareRadioStateChanged;
            OnHostedNetworkStateChange(WlanMgr.HostedNetworkState);

            System.Threading.Tasks.Task.Run(() => UpdateHostedNetworkStatistic());
            System.Threading.Tasks.Task.Run(() => UpdatePeerListInfo());
        }
Example #2
0
        void btnSet_Click(object sender, EventArgs e)
        {
            try {
                string err = WlanManager.Validate(txtSSID.Text, txtPwd.Text);
                if (err != null)
                {
                    this.Err("错误");
                    return;
                }

                _wlnMgr.SetConnectionSettings(txtSSID.Text, (int)numMaxClients.Value);
                _wlnMgr.SetSecondaryKey(txtPwd.Text);
                _wlnMgr.SetEnable(true);
                if (cmbSharings.SelectedValue != null)
                {
                    if (!_wlnMgr.IsHostedNetworkStarted)
                    {
                        _wlnMgr.StartHostedNetwork();
                    }
                    _icsMgr.EnableIcs((Guid)cmbSharings.SelectedValue, _wlnMgr.HostedNetworkInterfaceGuid);
                }
                this.Info("设置已生效!");
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
Example #3
0
        /// <summary>
        /// Инициализирует экземпляр класса NetShareHost
        /// </summary>
        public NetShareHost()
        {
            _wlanManager = new WlanManager();
            _icsManager  = new IcsManager();

            _wlanManager.StationJoin          += _wlanManager_StationStateChange;
            _wlanManager.StationLeave         += _wlanManager_StationStateChange;
            _wlanManager.HostedNetworkStarted += _wlanManager_HostedNetworkStarted;
            _wlanManager.HostedNetworkStopped += _wlanManager_HostedNetworkStopped;
        }
Example #4
0
        public static void GetPrePopulatedNetworkMethods(out string wlanSSID, out string gateway, out string gatewayMAC, out string dnsSuffix, out string ip, out string location)
        {
            wlanSSID   = string.Empty;
            gateway    = string.Empty;
            gatewayMAC = string.Empty;
            dnsSuffix  = string.Empty;
            ip         = string.Empty;
            location   = string.Empty;

            wlanSSID = WlanManager.GetCurrentSSID();

            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (ni.OperationalStatus != System.Net.NetworkInformation.OperationalStatus.Up)
                {
                    continue;
                }

                dnsSuffix = ni.GetIPProperties().DnsSuffix;

                foreach (GatewayIPAddressInformation gi in ni.GetIPProperties().GatewayAddresses)
                {
                    gateway    = gi.Address.ToString();
                    gatewayMAC = ARPRequest.GetMacAddress(gi.Address);
                    break;
                }
            }

            string      hostName = Dns.GetHostName();
            IPHostEntry ipEntry  = Dns.GetHostEntry(hostName);

            foreach (var item in ipEntry.AddressList)
            {
                if (item.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    ip = item.ToString();
                    break;
                }
            }

            if (Windows7Helper.IsWindows7)
            {
                location = LocationManager.Instance.GetCurrentLocationString();
            }
        }
Example #5
0
        public FrmMain()
        {
            InitializeComponent();

            dgvClients.AutoGenerateColumns = false;

            _wlnMgr     = new WlanManager();
            _icsMgr     = new IcsManager();
            _frmSharing = new FrmSharingManager();

            //监听虚拟路由状态的改变
            _wlnMgr.StationJoin += StationChanged;
            _wlnMgr.StationJoin += (_s, _e) => {
                ntfyDock.ShowBalloonTip(500, "", "发现客户端加入连接", ToolTipIcon.Info);
            };
            _wlnMgr.StationLeave         += StationChanged;
            _wlnMgr.StationStateChange   += StationChanged;
            _wlnMgr.HostedNetworkStarted += HostedNetworkStateChanged;
            _wlnMgr.HostedNetworkStarted += StationChanged;
            _wlnMgr.HostedNetworkStopped += HostedNetworkStateChanged;
            _wlnMgr.HostedNetworkStopped += StationChanged;
        }
Example #6
0
 public VirtualRouterHost()
 {
     this.wlanManager = new WlanManager();
     this.icsManager  = new IcsManager();
 }
Example #7
0
        private void InternalRedetectNetwork()
        {
            Logger.Log("RedetectNetwork called");

            OnRedetectNetworkStatusChanged(NetworkChangeStatus.Detecting);

            bool error = false;

            try
            {
                semaphore.WaitOne();

                bool atLeastOneFound = false;

                bool stopAfterFirstMatch = SettingsManager.Instance.GetApplicationSetting(SettingsManager.App_StopAfterFirstNetworkMatch, SettingsManager.Default_StopAfterFirstNetworkMatch);

                var allActiveInterfaces = NetworkInterface.GetAllNetworkInterfaces().Where(p => p.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up);

                foreach (var nc in SettingsManager.Instance.GetNetworkConfigurations(true))
                {
                    if (nc.IsUnknownNetwork)
                    {
                        continue;
                    }

                    #region Network
                    foreach (NetworkInterface ni in allActiveInterfaces)
                    {
                        if (nc.IsNetworkInterfaceDependend && nc.NetworkInterfaceId != ni.Id)
                        {
                            continue;
                        }

                        bool found = false;

                        switch (nc.Method)
                        {
                        case NetworkConfigurationMethod.DNSSuffix:
                            if (ni.GetIPProperties().DnsSuffix.ToUpper().Contains(nc.Query.ToUpper()))
                            {
                                ActivateNetworkConfiguration(nc);
                                found           = true;
                                atLeastOneFound = true;
                            }
                            break;

                        case NetworkConfigurationMethod.Gateway:
                            foreach (GatewayIPAddressInformation gi in ni.GetIPProperties().GatewayAddresses)
                            {
                                if (gi.Address.ToString().ToUpper().Contains(nc.Query.ToUpper()))
                                {
                                    ActivateNetworkConfiguration(nc);
                                    found           = true;
                                    atLeastOneFound = true;
                                    break;
                                }
                            }
                            break;

                        case NetworkConfigurationMethod.GatewayMAC:
                            foreach (GatewayIPAddressInformation gi in ni.GetIPProperties().GatewayAddresses)
                            {
                                var mac = ARPRequest.GetMacAddress(gi.Address);
                                if (mac.ToUpper().Contains(nc.Query.ToUpper()))
                                {
                                    ActivateNetworkConfiguration(nc);
                                    found           = true;
                                    atLeastOneFound = true;
                                    break;
                                }
                            }
                            break;

                        case NetworkConfigurationMethod.WLANSSID:
                            string wlanSSID = WlanManager.GetCurrentSSID();
                            if (wlanSSID.ToUpper().Contains(nc.Query.ToUpper()))
                            {
                                ActivateNetworkConfiguration(nc);
                                found           = true;
                                atLeastOneFound = true;
                            }
                            break;

                        case NetworkConfigurationMethod.WLANAvailable:
                            if (WlanManager.IsSSIDAvailable(nc.Query))
                            {
                                ActivateNetworkConfiguration(nc);
                                found           = true;
                                atLeastOneFound = true;
                            }
                            break;

                        case NetworkConfigurationMethod.IP:
                            string      hostName = Dns.GetHostName();
                            IPHostEntry ipEntry  = Dns.GetHostEntry(hostName);
                            foreach (var item in ipEntry.AddressList)
                            {
                                if (item.ToString().ToUpper().Contains(nc.Query.ToUpper()))
                                {
                                    ActivateNetworkConfiguration(nc);
                                    found           = true;
                                    atLeastOneFound = true;
                                    break;
                                }
                            }
                            break;

                        case NetworkConfigurationMethod.ServerAvailable:
                            try
                            {
                                var repl = new Ping().Send(nc.Query, 5000);
                                if (repl.Status == IPStatus.Success)
                                {
                                    ActivateNetworkConfiguration(nc);
                                    found           = true;
                                    atLeastOneFound = true;
                                }
                            }
                            catch (PingException) { }
                            break;

                        default:
                            break;
                        }

                        if (found)
                        {
                            break;
                        }
                    }
                    #endregion

                    #region DockingState
                    // no network matched, try docking state
                    if (!atLeastOneFound)
                    {
                        if (nc.Method != NetworkConfigurationMethod.DockingStationState)
                        {
                            continue;
                        }

                        var val = RegistryHelper.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\IDConfigDB\CurrentDockInfo", "DockingState");
                        if (val != null)
                        {
                            //0 = Workstation or Server, 1 = Undocked Laptop, 2 = Docked Laptop
                            var dockState = val.ToString();
                            if (nc.Query == "1" && dockState == "2")
                            {
                                ActivateNetworkConfiguration(nc);
                                atLeastOneFound = true;
                            }
                        }
                    }
                    #endregion

                    #region Location
                    // no network matched, try location
                    if (!atLeastOneFound)
                    {
                        if (nc.Method != NetworkConfigurationMethod.Location)
                        {
                            continue;
                        }

                        if (locationManager == null)
                        {
                            continue;
                        }

                        var queryArray      = nc.Query.Split(',');
                        var currentLocation = locationManager.GetCurrentLocation();
                        if (currentLocation == null)
                        {
                            continue;
                        }

                        int allQueries   = queryArray.Length;
                        int queriesFound = 0;
                        foreach (var query in queryArray)
                        {
                            foreach (var locPart in currentLocation)
                            {
                                if (locPart.Trim().ToUpper().Contains(query.Trim().ToUpper()))
                                {
                                    queriesFound++;
                                    break;
                                }
                            }
                        }

                        if (allQueries == queriesFound)
                        {
                            ActivateNetworkConfiguration(nc);
                            atLeastOneFound = true;
                        }
                    }
                    #endregion

                    if (atLeastOneFound && stopAfterFirstMatch)
                    {
                        break;
                    }
                }

                // nothing found, activate "Unknown Network"
                if (!atLeastOneFound)
                {
                    var unknown = SettingsManager.Instance.GetNetworkConfiguration(SwitcherActionBase.DeactivateNetworkId);
                    ActivateNetworkConfiguration(unknown);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.Message, ex);
                error = true;
            }
            finally
            {
                semaphore.Release();

                Logger.Log("RedetectNetwork completed");

                if (error)
                {
                    OnRedetectNetworkStatusChanged(NetworkChangeStatus.Error);
                }
                else
                {
                    OnRedetectNetworkStatusChanged(NetworkChangeStatus.Completed);
                }
            }
        }