Пример #1
0
        public NetworkInterface(NetworkInfoModel aInfo)
        {
            Name = aInfo.Name;
            Info = aInfo;

            if (aInfo.IPAddress.Equals(IPAddress.Any) || aInfo.IPAddress.Equals(IPAddress.IPv6Any) ||
                aInfo.IPAddress.Equals(IPAddress.None) || aInfo.IPAddress.Equals(IPAddress.IPv6None))
            {
                Status = EStatus.eUnavailable;
            }
            else
            {
                Status = EStatus.eAvailable;
            }
        }
Пример #2
0
        public static void RefreshWifiInfo(Context aContext)
        {
            WifiManager      wifi     = (WifiManager)aContext.GetSystemService(Context.WifiService);
            NetworkInfoModel newModel = null;

            if (wifi != null && wifi.IsWifiEnabled)
            {
                Android.Net.Wifi.WifiInfo networkInfo = wifi.ConnectionInfo;// as Android.Net.Wifi.WifiInfo;
                newModel = new NetworkInfoModel(
                    networkInfo.SSID,
                    networkInfo.MacAddress,
                    ENetworkInterfaceType.eEthernet,
                    new System.Net.IPAddress(networkInfo.IpAddress),
                    null,
                    ENetworkInterfaceComponent.eIPv4,
                    string.Empty,
                    wifi.WifiState == Android.Net.WifiState.Enabled ? EOperationalStatus.eUp : EOperationalStatus.eDown,
                    //networkInfo.SupplicantState == Android.Net.Wifi.SupplicantState.Completed ? EOperationalStatus.eUp : EOperationalStatus.eDown,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    new List <System.Net.IPAddress>()
                    );
                networkInfo.Dispose();
                wifi.Dispose();
                UserLog.WriteLine("RefreshWifiInfo(): " + newModel.Name + ", " + newModel.IPAddress + ", " + newModel.OperationalStatus);
            }
            else
            {
                UserLog.WriteLine("RefreshWifiInfo(): No Network");
            }
            sWifiInterface = newModel;
        }
Пример #3
0
        public static List <NetworkInfoModel> GetAllNetworkInterfaces()
        {
            List <NetworkInfoModel> adaptors = new List <NetworkInfoModel>();

            System.Net.NetworkInformation.NetworkInterface[] nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

            foreach (System.Net.NetworkInformation.NetworkInterface adapter in nics)
            {
                string adapterName        = adapter.Name;
                string adapterDescription = adapter.Description;
                string macAddress         = adapter.GetPhysicalAddress().ToString();

                ENetworkInterfaceComponent networkInterfaceComponent = (adapter.Supports(NetworkInterfaceComponent.IPv4))
                    ? ENetworkInterfaceComponent.eIPv4
                    : ENetworkInterfaceComponent.eUnknown;

                ENetworkInterfaceType networkInterfaceType = (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
                    ? ENetworkInterfaceType.eLoopBack
                    : ENetworkInterfaceType.eUnknown;

                // ignore loopback adapters, non IPv4 adapters
                if (networkInterfaceType == ENetworkInterfaceType.eLoopBack)
                {
                    UserLog.WriteLine(adapter.Description + " is loopback - ignored");
                    continue;
                }

                if (networkInterfaceComponent != ENetworkInterfaceComponent.eIPv4)
                {
                    UserLog.WriteLine(adapter.Description + " does not support IPv4 - ignored");
                    continue;
                }

                bool?supportsMulticast = null;
                bool?isRecieveOnly     = null;
                try
                {
                    supportsMulticast = adapter.SupportsMulticast;
                    isRecieveOnly     = adapter.IsReceiveOnly;
                }
                catch (PlatformNotSupportedException)
                {
                    // Multicast, IsReceiveOnly support only defined in Windows XP and greater
                }

                EOperationalStatus    operationalStatus = (EOperationalStatus)((int)adapter.OperationalStatus);
                IPInterfaceProperties properties        = adapter.GetIPProperties();

                bool?isDHCPEnabled = null;
                bool?isAutoPrivateAddressEnabled = null;
                int? mtu = null;

                try
                {
                    IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
                    if (ipv4 != null)
                    {
                        isDHCPEnabled = ipv4.IsDhcpEnabled;
                        isAutoPrivateAddressEnabled = ipv4.IsAutomaticPrivateAddressingEnabled;
                        mtu = ipv4.Mtu;
                    }
                }
                catch { } // throws an error in mono 2.6

                List <IPAddress> gatewayAddress = new List <IPAddress>();
                foreach (GatewayIPAddressInformation gateway in properties.GatewayAddresses)
                {
                    gatewayAddress.Add(gateway.Address);
                }

                int i = 0;
                UnicastIPAddressInformationCollection uniCast = properties.UnicastAddresses;
                foreach (UnicastIPAddressInformation ip in uniCast)
                {
                    // only take ipv4 address of adapter (may have ipv4 and ipv6)
                    if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        NetworkInfoModel adaptor = new NetworkInfoModel(adapterDescription
                                                                        , adapterName + ((i == 0) ? "" : " (" + i.ToString() + ")")
                                                                        , networkInterfaceType
                                                                        , ip.Address
                                                                        , supportsMulticast
                                                                        , networkInterfaceComponent
                                                                        , macAddress
                                                                        , operationalStatus
                                                                        , isRecieveOnly
                                                                        , isDHCPEnabled
                                                                        , isAutoPrivateAddressEnabled
                                                                        , mtu
                                                                        , properties.IsDnsEnabled
                                                                        , properties.IsDynamicDnsEnabled
                                                                        , properties.DnsSuffix
                                                                        , gatewayAddress);

                        adaptors.Add(adaptor);
                        ++i;
                        UserLog.WriteLine(adapterName + " (" + ip.Address.ToString() + ") is supported");
                        Trace.WriteLine(Trace.kCore, adapterName + " [" + adapterDescription + "] (" + ip.Address.ToString() + ") is supported");
                    }
                }
            }

            return(adaptors);
        }