コード例 #1
0
        /// <summary>
        /// Removes a network from the adapter's preferred list
        /// </summary>
        /// <param name="SSID">The SSID of the network to remove</param>
        public bool RemovePreferredNetwork(string SSID)
        {
            if ((SSID == null) || (SSID == string.Empty))
            {
                throw new ArgumentException("Invalid SSID");
            }

            // 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)
                {
                    // There is no list.  Return false.
                    return(false);
                }

                // Find the indicated item and remove it from
                // the list by creating a new list and setting
                // that as the preferred list.
                RAW_DATA rdold = entry.rdStSSIDList;
                WLANConfigurationList prefl = new WLANConfigurationList(rdold);

                // If there are no items in the list, return false.
                if (prefl.NumberOfItems == 0)
                {
                    return(false);
                }

                // Build the new list.
                WLANConfigurationList prefnew = new WLANConfigurationList(prefl.NumberOfItems - 1);

                // Start at the top of the old list and copy items
                // from old to new, until we find the item to be
                // removed.
                int j = 0;
                int i;
                for (i = 0; i < prefl.NumberOfItems; i++)
                {
                    WLANConfiguration item = prefl.Item(i);

                    if (item.SSID == SSID)
                    {
                        // Skip to next item without incrementing
                        // j.
                    }
                    else
                    {
                        prefnew.SetItem(j, item);
                        j++;
                    }
                }

                // Check for whether the item was found in the
                // list or not.  If not, we don't reset the
                // wireless settings and instead return false.
                if (j == i)
                {
                    return(false);
                }

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

                // Must now copy the new preferred list to the entry that
                // we will send 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_ALL_FLAGS | INTF_FLAGS.INTF_PREFLIST);

                if (uret != 0)
                {
                    throw new System.ComponentModel.Win32Exception(uret, "Unable to Set WZC Interface");
                }
            }
            finally
            {
                entry.Dispose();
            }
            return(uret <= 0);
        }
コード例 #2
0
        /// <summary>
        /// Connects to an already-configured wireless network by SSID
        /// </summary>
        /// <param name="SSID"></param>
        /// <returns></returns>
        public bool ConnectToPreferredNetwork(string SSID)
        {
            INTF_ENTRY entry;
            int        uret = WZC.QueryAdapter(this.Name, out entry);

            try
            {
                if (uret != 0)
                {
                    throw new System.ComponentModel.Win32Exception(uret, "Unable to Query WZC Interface");
                }

                // 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.  Return false.
                if (targetItem == null)
                {
                    return(false);
                }

                // If the SSID is already first in the list, we're done.
                if (i > 0)
                {
                    // Now, copy the rest of the items one place down in the
                    // list.
                    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));
                    }

                    // Put the saved target item in index 0 in the new list.
                    prefl.SetItem(0, targetItem);
                }

                uret = WZC.SetAdapter(entry, INTF_FLAGS.INTF_ALL_FLAGS | INTF_FLAGS.INTF_PREFLIST);
                if (uret != 0)
                {
                    throw new System.ComponentModel.Win32Exception(uret, "Unable to Set WZC Interface");
                }
            }
            finally
            {
                entry.Dispose();
            }
            return(true);
        }
コード例 #3
0
        /// <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);
        }
コード例 #4
0
        internal unsafe void RefreshListPreferred(bool nearbyOnly)
        {
            // If the caller wants only the local preferred APs,
            // we check nearby list and, if the AP is not there,
            // we don't add it to our own preferred list.
            AccessPointCollection apc = null;

            if (nearbyOnly)
            {
                apc = m_adapter.NearbyAccessPoints;
            }

            // First step is to get the INTF_ENTRY for the adapter.
            // This includes the list of preferred SSID values.
            INTF_ENTRY ie = INTF_ENTRY.GetEntry(this.m_adapter.Name);

            // The field rdStSSIDList is the preferred list.  It comes
            // in the form of a WZC_802_11_CONFIG_LIST.
            RAW_DATA rd = ie.rdStSSIDList;
            WLANConfigurationList cl = new WLANConfigurationList(rd);

            // Step through the list and add a new AP to the
            // collection for each entry.
            for (int i = 0; i < cl.NumberOfItems; i++)
            {
                WLANConfiguration c = cl.Item(i);

                //Debug.WriteLine(c.SSID);
                //for (int d = 1; d <= c.Data.Length; d++)
                //{
                //    Debug.Write(string.Format("{0:x2}{1}", c.Data[d - 1], (d%8 == 0) ? "\r\n" : " "));
                //}
                //Debug.WriteLine(string.Empty);

                // If we're only showing those which we can hear,
                // see if the current SSID is in the nearby list.
                if (nearbyOnly)
                {
                    // Find the currently active AP with the SSID
                    // to match the one we're working on.
                    AccessPoint activeAP = apc.FindBySSID(c.SSID);
                    int         ss;

                    // If the given SSID is not in range, don't add
                    // an entry to the list.
                    if (activeAP != null)
                    {
                        // Update signal strength.
                        ss = activeAP.SignalStrengthInDecibels;

                        // Copy the signal strength value to the
                        // NDIS_WLAN_BSSID structure for the
                        // preferred list entry.
                        c.Rssi = ss;

                        // Create the AP instance and add it to the
                        // preferred list.
                        AccessPoint ap = new AccessPoint(c);
                        m_aps.Add(ap);
                    }
                }
                else
                {
                    // Create the AP instance and add it to the
                    // preferred list.  The signal strength will
                    // not necessarily be valid.
                    AccessPoint ap = new AccessPoint(c);
                    m_aps.Add(ap);
                }
            }

            // Dispose of INTF_ENTRY
            ie.Dispose();
        }