Esempio n. 1
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);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Запускает точку доступа с указанным адаптером для ICS
        /// </summary>
        /// <param name="sharedConnectionGuid">адаптер для ICS</param>
        /// <returns>true, если точка доступа успешно запущена; false, если возникла ошибка. Вызовите GetLastError(), чтобы получить результат ошибки</returns>
        public bool Start(SharableConnection sharedConnection)
        {
            try
            {
                Stop();

                _wlanManager.StartHostedNetwork();

                System.Threading.Thread.Sleep(1000);

                if (sharedConnection != null)
                {
                    if (sharedConnection.Guid != Guid.Empty)
                    {
                        if (_icsManager.SharingInstalled)
                        {
                            _icsManager.DisableIcsOnAll();

                            var privateConnectionGuid = _wlanManager.HostedNetworkInterfaceGuid;

                            if (privateConnectionGuid == Guid.Empty)
                            {
                                // If the GUID for the Hosted Network Adapter isn't return properly,
                                // then retrieve it by the DeviceName.

                                privateConnectionGuid = (from c in _icsManager.Connections
                                                         where c.Props.DeviceName.ToLowerInvariant().Contains("microsoft virtual wifi miniport adapter") ||                                                      // Windows 7
                                                         c.Props.DeviceName.ToLowerInvariant().Contains("microsoft hosted network virtual adapter")                                                            // Windows 8
                                                         select c.Guid).FirstOrDefault();
                                // Note: For some reason the DeviceName can have different names, currently it checks for the ones that I have identified thus far.

                                if (privateConnectionGuid == Guid.Empty)
                                {
                                    // Device still now found, so throw exception so the message gets raised up to the client.
                                    throw new Exception("Virtual Wifi device not found!\n\nNeither \"Microsoft Hosted Network Virtual Adapter\" or \"Microsoft Virtual Wifi Miniport Adapter\" were found.");
                                }
                            }

                            _icsManager.EnableIcs(sharedConnection.Guid, privateConnectionGuid);

                            _currentSharedConnection = sharedConnection;
                        }
                    }
                }
                else
                {
                    _currentSharedConnection = null;
                }

                return(true);
            }
            catch (Exception ex)
            {
                _lastErrorMessage = ex.Message;
                return(false);
            }
        }
Esempio n. 3
0
 void testPrivateConnected()
 {
     if (state_ == getawayState.Starting)
     {
         return;
     }
     if (state_ == getawayState.StartingIP)
     {
         return;
     }
     if (state_ == getawayState.Started)
     {
         return;
     }
     privateManager.updateConnectionGuid();
     if (privateManager.connectionGuid != null)
     {
         if (privateManager.Started)
         {
             privateManager.networkStop();
         }
         else
         {
             Trace.TraceInformation("Getaway starting");
             state_ = getawayState.Starting;
             if (publicConnected_)
             {
                 try {
                     if (this.icsManager.SharingInstalled)
                     {
                         this.icsManager.setPrivateConnection(privateManager.connectionGuid);
                         privateManager.startNetwork();
                         icsManager.EnableIcs();
                         state_ = getawayState.StartingIP;
                         return;
                     }
                 } catch (Exception e) {
                     Trace.TraceInformation("Getaway start failed: {0}", e.Message);
                 }
             }
             state_ = getawayState.StartFailed;
         }
     }
 }
        private void ShareConnection(IcsConnection icsConnection)
        {
            // disable sharing first
            DisableSharing(false);

            if (icsConnection == null)
            {
                return;
            }

#if TRACE
            LogExceptions.LogTrace(true, "ShareConnection -> " + icsConnection.Name);
#endif
            Exception failedToSharException = null;

            // then enabled the sharing
            for (int i = 1; i < 10; i++)
            {
                try
                {
                    if (_wlanManager.HostedNetworkState != WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_active)
                    {
                        Thread.Sleep(500);
                        continue;
                    }

                    var privateConnectionGuid = _wlanManager.HostedNetworkInterfaceGuid;

                    _icsManager.EnableIcs(icsConnection.Guid, privateConnectionGuid);

                    // the connected network guid to internet
                    SharedConnection = icsConnection;

                    failedToSharException = null;
#if TRACE
                    LogExceptions.LogTrace(true, "ShareConnection.EnableIcs on try no:" + i);
#endif
                    return;
                }
                catch (Exception ex)
                {
#if DEBUG
                    LogExceptions.Log(ex);
#endif
                    failedToSharException = ex;
                    Thread.Sleep(500);
                }
            }

            // if we have reached here that means the connection is failed to share with many tries
            if (failedToSharException != null)
            {
#if TRACE
                LogExceptions.LogTrace(true, "ShareConnection is failed -> " + icsConnection.Name,
                                       failedToSharException.ToString());
#endif
                RaiseOnFailedToEnableSharing(icsConnection, failedToSharException);
            }
            else
            {
#if TRACE
                LogExceptions.LogTrace(true, "ShareConnection is failed -> " + icsConnection.Name);
#endif
            }
        }