Exemplo n.º 1
0
        private void GetNDISAPs()
        {
            // Retrieve a list of NDIS_802_11_BSSID_LIST
            // structures from the driver.  We'll parse that
            // list and populate ourselves based on the data
            // that we find there.
            string name = m_adapter.Name;

            byte[] data = NDISUIO.QueryOID(NDIS_OID.BSSID_LIST, name);
            if (data != null)
            {
                // Figure out how many SSIDs there are.
                NDIS_802_11_BSSID_LIST rawlist = new NDIS_802_11_BSSID_LIST(data, false);

                for (int i = 0; i < rawlist.NumberOfItems; i++)
                {
                    // Get the next raw item from the list.
                    BSSID bssid = rawlist.Item(i);

                    // Using the raw item, create a cooked
                    // SSID item.
                    AccessPoint ssid = new AccessPoint(bssid);

                    // Add the new item to this.
                    m_aps.Add(ssid);
                }
            }
            else
            {
                throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to get BSSID List");
            }
        }
Exemplo n.º 2
0
        public unsafe static bool IoControl(uint code, byte[] indata, byte[] outdata)
        {
            NDISUIO ndis   = new NDISUIO();
            bool    retVal = false;

            try
            {
                try
                {
                    ndis.Open(System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
                }
                catch
                {
                    throw new NetworkInformationException(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
                }

                try
                {
                    ndis.DeviceIoControl(code, indata, outdata);
                }
                catch (Exception)
                {
                    retVal = false;
                }
            }
            finally
            {
                ndis.Dispose();
            }

            return(retVal);
        }
Exemplo n.º 3
0
        internal unsafe void ClearCache()
        {
            string name = m_adapter.Name;

            if (!NDISUIO.SetOID(NDIS_OID.BSSID_LIST_SCAN, name))
            {
                throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to clear adapter's NDIS cache");
            }
        }
Exemplo n.º 4
0
 private void TeardownQueueListener()
 {
     if (m_ndisQueue != null)
     {
         NDISUIO.CancelNotifications();
         m_ndisQueue.Close();
         m_ndisQueue = null;
     }
 }
Exemplo n.º 5
0
        private void SetupQueueListener()
        {
            if (m_ndisQueue != null)
            {
                m_ndisQueue = new P2PMessageQueue(true, null, NDISUIO_DEVICE_NOTIFICATION.Size, 0);
                m_ndisQueue.DataOnQueueChanged += new EventHandler(DataOnQueueChanged);

                NDISUIO.RequestNotifications(m_ndisQueue);
            }
        }
        /// <summary>
        /// Attempts to connect to a specific Access Point
        /// </summary>
        /// <param name="SSID">The Service Set Identified or the Access Point to which a connection should be made</param>
        public virtual void Connect(string SSID)
        {
            if (SSID.Length > 32)
            {
                throw new ArgumentException("SSID Max Length is 32 characters");
            }

            if (!NDISUIO.SetOID(NDIS_OID.SSID, this.Name, Encoding.ASCII.GetBytes(SSID)))
            {
                throw new NotSupportedException();
            }
        }
Exemplo n.º 7
0
        public static void CancelNotifications()
        {
            NDISUIO ndis = new NDISUIO();

            try
            {
                ndis.DeviceIoControl(IOCTL_NDISUIO_CANCEL_NOTIFICATION, null, null);
            }
            finally
            {
                ndis.Dispose();
            }
        }
Exemplo n.º 8
0
        public static void RequestNotifications(P2PMessageQueue rxQueue)
        {
            NDISUIO ndis = new NDISUIO();

            try
            {
                NDISUIO_REQUEST_NOTIFICATION request = new NDISUIO_REQUEST_NOTIFICATION();
                request.hMsgQueue           = rxQueue.Handle;
                request.dwNotificationTypes = ALL_NOTIFICATIONS;

                ndis.DeviceIoControl(IOCTL_NDISUIO_REQUEST_NOTIFICATION, request.GetBytes(), null);
            }
            finally
            {
                ndis.Dispose();
            }
        }
Exemplo n.º 9
0
        public unsafe static bool SetOID(NDIS_OID oid, string adapterName, NDISQueryOid queryOID)
        {
            NDISUIO ndis = new NDISUIO();

            if (queryOID == null)
            {
                queryOID = new NDISQueryOid(0);
            }

            byte[] nameBytes = System.Text.Encoding.Unicode.GetBytes(adapterName + '\0');
            fixed(byte *pName = &nameBytes[0])
            {
                queryOID.ptcDeviceName = pName;

                queryOID.Oid = (uint)oid;

                try
                {
                    ndis.Open(System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
                }
                catch
                {
                    throw new NetworkInformationException(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
                }
                try
                {
                    ndis.DeviceIoControl(IOCTL_NDISUIO_SET_OID_VALUE, queryOID.getBytes(), queryOID.getBytes());
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            ndis.Dispose();

            return(true);
        }
Exemplo n.º 10
0
        public unsafe static byte[] QueryOID(NDIS_OID oid, string adapterName)
        {
            if ((adapterName == null) || (adapterName == string.Empty))
            {
                // no point in even trying to call NDIS if this is the case
                return(null);
            }

            NDISUIO ndis = new NDISUIO();

            NDISQueryOid queryOID;

            byte[] result;

            try
            {
                switch (oid)
                {
                case NDIS_OID.RSSI:
                case NDIS_OID.WEP_STATUS:
                case NDIS_OID.BSSID_LIST_SCAN:
                case NDIS_OID.AUTHENTICATION_MODE:
                case NDIS_OID.INFRASTRUCTURE_MODE:
                case NDIS_OID.NETWORK_TYPE_IN_USE:
                    queryOID = new NDISQueryOid(4);         // The data is a four-byte signed int
                    break;

                case NDIS_OID.BSSID:
                    queryOID = new NDISQueryOid(36);
                    break;

                case NDIS_OID.SUPPORTED_RATES:
                    queryOID = new NDISQueryOid(8);
                    break;

                case NDIS_OID.CONFIGURATION:
                    queryOID = new NDISQueryOid(32);
                    break;

                case NDIS_OID.SSID:
                    queryOID = new NDISQueryOid(36);            // The data is a four-byte length plus 32-byte ASCII string
                    break;

                case NDIS_OID.BSSID_LIST:
                    queryOID = new NDISQueryOid(2000);
                    break;

                default:
                    throw new NotSupportedException(string.Format("'NDIS_OID_{0}' Not supported", oid.ToString()));
                }

                byte[] nameBytes = System.Text.Encoding.Unicode.GetBytes(adapterName + '\0');

                fixed(byte *pName = &nameBytes[0])
                {
                    queryOID.ptcDeviceName = pName;
                    queryOID.Oid           = (uint)oid;

                    try
                    {
                        ndis.Open(System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
                    }
                    catch
                    {
                        throw new NetworkInformationException(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
                    }
                    try
                    {
                        ndis.DeviceIoControl(IOCTL_NDISUIO_QUERY_OID_VALUE, queryOID.getBytes(), queryOID.getBytes());
                    }
                    catch (Exception)
                    {
                        // if it's an attempt to get a list it might be an OOM
                        // are we trying to get a list?
                        if (oid != NDIS_OID.BSSID_LIST)
                        {
                            throw new NetworkInformationException();
                        }

                        // maybe just an OOM - try again
                        GC.Collect();
                        queryOID = new NDISQueryOid(8000);
                        queryOID.ptcDeviceName = pName;
                        queryOID.Oid           = (uint)oid;
                        ndis.DeviceIoControl(IOCTL_NDISUIO_QUERY_OID_VALUE, queryOID.getBytes(), queryOID.getBytes());
                    }
                    finally
                    {
                        ndis.Close();
                    }

                    result = new byte[queryOID.Data.Length];
                    Buffer.BlockCopy(queryOID.Data, 0, result, 0, result.Length);
                }
            }
            finally
            {
                ndis.Dispose();
            }
            return(result);
        }
        /// <summary>
        /// Returns objects that describe the network interfaces on the local computer.
        /// </summary>
        /// <returns>
        /// A System.Net.NetworkInformation.NetworkInterface array that contains objects
        /// that describe the available network interfaces, or an empty array if no interfaces
        /// are detected.
        /// </returns>
        public unsafe static INetworkInterface[] GetAllNetworkInterfaces()
        {
            NetworkInterface[] interfaceList;
            uint size;

            // get buffer size requirement
            NativeMethods.GetInterfaceInfo(null, out size);

            byte[] ifTable = new byte[size];
            // pin the table buffer
            fixed(byte *pifTable = ifTable)
            {
                byte *p = pifTable;

                /* table looks like this:
                 *  typedef struct _IP_INTERFACE_INFO {
                 *    LONG NumAdapters;
                 *    IP_ADAPTER_INDEX_MAP Adapter[1];
                 *  } IP_INTERFACE_INFO, *PIP_INTERFACE_INFO;
                 *
                 *  typedef struct _IP_ADAPTER_INDEX_MAP {
                 *    ULONG Index;
                 *    WCHAR Name [MAX_ADAPTER_NAME];
                 *  } IP_ADAPTER_INDEX_MAP, *PIP_ADAPTER_INDEX_MAP;
                 */

                // get the table data
                NativeMethods.GetInterfaceInfo(pifTable, out size);

                // get interface count
                int interfaceCount = *p;

                interfaceList = new NetworkInterface[interfaceCount];

                p += 4;

                // get each interface
                for (int i = 0; i < interfaceCount; i++)
                {
                    // get interface index
                    int index = (int)*((int *)p);
                    p += 4;

                    // get interface name
                    byte[] nameBytes = new byte[256];
                    Marshal.Copy(new IntPtr(p), nameBytes, 0, nameBytes.Length);
                    string name      = Encoding.Unicode.GetString(nameBytes, 0, nameBytes.Length);
                    int    nullIndex = name.IndexOf('\0');
                    if (nullIndex > 0)
                    {
                        name = name.Substring(0, nullIndex);
                    }
                    p += 256;

                    // check the wireless capabilities
                    try
                    {
                        NDISUIO.QueryOID(NDIS_OID.WEP_STATUS, name);

                        // didn't throw, so it's wireless - determinine if it's WZC compatible
                        INTF_ENTRY entry;
                        if (WZC.QueryAdapter(name, out entry) == NativeMethods.NO_ERROR)
                        {
                            // this is a WZC wireless adapter
                            interfaceList[i] = new WirelessZeroConfigNetworkInterface(index, name);
                        }
                        else
                        {
                            // this is a non-WZC wireless adapter
                            interfaceList[i] = new WirelessNetworkInterface(index, name);
                        }
                        entry.Dispose();
                    }
                    catch
                    {
                        // if it's not wireless, it will throw and end up here
                        interfaceList[i] = new NetworkInterface(index, name);
                    }
                }
            }

            return(interfaceList);
        }