Esempio n. 1
0
        internal AccessPoint(WLANConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            Name               = config.SSID;
            macaddr            = config.MACAddress;
            Privacy            = config.Privacy;
            AuthenticationMode = config.AuthenticationMode;

            // jsm - Defect 352: Exposed keyIndex, keyMaterial, and EapolParams
            KeyIndex    = config.KeyIndex;
            KeyMaterial = config.KeyMaterial;
            EapolParams = config.EapolParams;

            Channel = config.Configuration.Frequency;
            if (Channel > 14)
            {
                Channel = (config.Configuration.Frequency - 2407000) / 5000;
            }
            if (Channel < 0)
            {
                Channel = (int)((config.Configuration.Frequency - 2407) / 5);
            }

            // see if the rssi is in the HIWORD or LOWORD
            uint ssi = (uint)config.Rssi;

            if (((ssi & 0xFFFF0000) > 0) && ((ssi & 0xffff) == 0))
            {
                // hiword
                SignalStrengthInDecibels = config.Rssi >> 16;
            }
            else if (ssi == 0)
            {
                SignalStrengthInDecibels = -99;
            }
            else
            {
                // loword
                SignalStrengthInDecibels = config.Rssi;
            }

            supportedrates     = config.Rates;
            NetworkTypeInUse   = config.NetworkTypeInUse;
            InfrastructureMode = config.InfrastructureMode;
        }
        private WLANConfiguration MakeSSIDEntry(string SSID, bool bInfrastructure, string authKey, int keyIndex, AuthenticationMode authMode, WEPStatus privacyMode, EAPParameters eap)
        {
            WLANConfiguration config = new WLANConfiguration();

            if (eap == null)
            {
                // setup default based on mode
                eap = new EAPParameters();
                if (privacyMode == WEPStatus.WEPEnabled)
                {
                    eap.EapFlags = EAPFlags.Disabled;
                }
                else if ((privacyMode == WEPStatus.AESEnabled) || (privacyMode == WEPStatus.TKIPEnabled))
                {
                    eap.EapType     = EAPType.Default;
                    eap.EapFlags    = EAPFlags.Enabled;
                    eap.Enable8021x = true;
                    eap.AuthData    = IntPtr.Zero;
                    eap.AuthDataLen = 0;
                }
            }

            config.EapolParams        = eap;
            config.MACAddress         = this.GetPhysicalAddress().GetAddressBytes();
            config.SSID               = SSID;
            config.KeyIndex           = keyIndex - 1; // WZC is 0-based, but wireless specs are 1-based
            config.InfrastructureMode = bInfrastructure ? InfrastructureMode.Infrastructure : InfrastructureMode.AdHoc;
            config.AuthenticationMode = authMode;
            config.Privacy            = privacyMode;

            // translate key string into encrypted version
            if (authKey != null)
            {
                if (string.Compare(authKey, "auto", true) != 0)
                {
                    TranslateEncryptionKey(ref config, authKey, authMode, privacyMode);
                }
            }
            return(config);
        }
        /// <summary>
        /// Sets wireless settings associated with a given interface and AP, adding to, rather than replacing the preferred list of APs.  This version of the
        /// method is designed for the case where *all* of  the options are going to be set, where no initial  configuration exists at all and where existing
        /// items in the preferred list should be maintained. After this method executes, if it is successful, the specified SSID will be at the top, highest-
        /// priority, end of the preferred list.
        /// </summary>
        /// <param name="SSID">
        /// Target SSID to connect
        /// </param>
        /// <param name="infrastructureMode">
        /// Is infrastructure
        /// </param>
        /// <param name="authKey">
        /// WEP key or WPA shared key string representing hex string (each two characters are converted to a single byte)
        /// </param>
        /// <param name="keyIndex">
        /// Index of the WEP key.  Valid values are 1-4
        /// </param>
        /// <param name="authMode">
        /// Authentication mode for the connection
        /// </param>
        /// <param name="privacyMode">
        /// Privacy (encryption) mode for the connection
        /// </param>
        /// <param name="eapParams">
        /// Parameters describing how the connection should use EAP to authenticate the user to the network
        /// </param>
        /// <returns>true if succeeded</returns>
        #endregion
        public bool AddPreferredNetwork(string SSID,
                                        bool infrastructureMode,
                                        string authKey,
                                        int keyIndex,
                                        AuthenticationMode authMode,
                                        WEPStatus privacyMode,
                                        EAPParameters eapParams)
        {
            if ((keyIndex <= 0) || (keyIndex > 4))
            {
                throw new ArgumentException("Invalid keyIndex (must be between 1 and 4)");
            }
            if ((SSID == null) || (SSID == string.Empty))
            {
                throw new ArgumentException("Invalid SSID");
            }

            // We may yet need to do some processing on the key,
            // if it is a WPA key.  We need a WZC_WLAN_CONFIG
            // structure to pass to the WZC routine that does
            // this processing, however, so that is done below.

            // Get the current preferred list of SSID values.
            // First, we need to get an INTF_ENTRY for this adapter.
            INTF_ENTRY entry;
            int        uret = WZC.QueryAdapter(this.Name, out entry);

            try
            {
                if (uret != 0)
                {
                    throw new System.ComponentModel.Win32Exception(uret, "No preferred list found");
                }

                // We need to push the indicated item to the top of the
                // preferred list.  Once we do that and call WZCSetInterface
                // the connection will be established to that SSID.
                // The preferred list is in the rdStSSIDList field.
                RAW_DATA rdold = entry.rdStSSIDList;
                WLANConfigurationList prefl = new WLANConfigurationList(rdold);

                // Start at the bottom of the list.  If the current item
                // is the one we want to copy, save it and start copying
                // items down in the list.
                WLANConfiguration targetItem = null;
                int i;
                for (i = (int)prefl.NumberOfItems - 1; i >= 0; i--)
                {
                    targetItem = prefl.Item(i);
                    if (targetItem.SSID == SSID)
                    {
                        break;
                    }
                }

                // If we get no match for our SSID value, the item
                // is *not* in the preferred list, so we can
                // skip removing it.
                if (i >= 0)
                {
                    // Now, copy the items before i on the
                    // list down to cover i.  This leaves
                    // position 0 in the list as a copy of
                    // position 1.  We'll fill in position 0
                    // with the new most-preferred SSID.
                    for (int j = i; j >= 1; j--)
                    {
                        // Copy old list item j-1 to new list item j.
                        prefl.SetItem(j, prefl.Item(j - 1));
                    }
                }
                else
                {
                    // The item was not in the list.  We have
                    // to expand the list and move all of
                    // the original items down one spot.
                    WLANConfigurationList prefl2 = new WLANConfigurationList(prefl.NumberOfItems + 1);
                    for (int j = 0; j < (int)prefl.NumberOfItems; j++)
                    {
                        // Copy from old list to new list.
                        prefl2.SetItem(j + 1, prefl.Item(j));
                    }

                    // Replace the old list with the new one
                    // for the rest of the code.  Entry #0
                    // is unset.
                    prefl = prefl2;
                }

                // Create a new item and put that in the list
                // at item #0, which presently exists but
                // doesn't mean anything (it's either a
                // totally blank item, if the SSID was not
                // in the list before the call, or it's the
                // old first item in the list).

                // Unlike the other SetWirelessSettings versions,
                // we *don't* get the current configuration here;
                // our parameters will set that.
                WLANConfiguration thisConfig = MakeSSIDEntry(SSID, infrastructureMode, authKey, keyIndex, authMode, privacyMode, eapParams);

                // OK, finally, set the item in the preferred
                // list according to the parameters to this
                // call.
                prefl.SetItem(0, thisConfig);

                // Must now copy the new preferred list to the entry that
                // we will sent with WZCSetInterface.
                entry.rdStSSIDList = prefl.rawData;

                // Finally, we are ready to select the new SSID as our
                // primary preferred connection.
                uret = WZC.SetAdapter(entry, INTF_FLAGS.INTF_PREFLIST);
                if (uret != 0)
                {
                    throw new System.ComponentModel.Win32Exception(uret, "Unable to Set WZC Interface");
                }
            }
            finally
            {
                entry.Dispose();
            }

            return(true);
        }