// TODO: #2485: Temporarily made GetFixedInfo() public to make things build.
        // This function needs to be switched back to private since it has no correspondent in the Unix world.
        public static Interop.IpHlpApi.FIXED_INFO GetFixedInfo()
        {
            Interop.IpHlpApi.FIXED_INFO fixedInfo = new Interop.IpHlpApi.FIXED_INFO();

            IReadOnlyList <HostName> hostNamesList = global::Windows.Networking.Connectivity.NetworkInformation.GetHostNames();

            foreach (HostName entry in hostNamesList)
            {
                // The first DomainName entry in the GetHostNames() list
                // is the fdqn of the machine itself.
                if (entry.Type == HostNameType.DomainName)
                {
                    string host = entry.ToString();
                    int    dot  = host.IndexOf('.');
                    if (dot != -1)
                    {
                        // The machine is domain joined.
                        fixedInfo.hostName   = host.Substring(0, dot);
                        fixedInfo.domainName = host.Substring(dot + 1);
                    }
                    else
                    {
                        // The machine is not domain joined.
                        fixedInfo.hostName   = host;
                        fixedInfo.domainName = string.Empty;
                    }

                    break;
                }
            }

            return(fixedInfo);
        }
Beispiel #2
0
        private static Interop.IpHlpApi.FIXED_INFO GetFixedInfo()
        {
            uint size = 0;
            SafeLocalAllocHandle buffer = null;

            Interop.IpHlpApi.FIXED_INFO fixedInfo = new Interop.IpHlpApi.FIXED_INFO();

            // First we need to get the size of the buffer
            uint result = Interop.IpHlpApi.GetNetworkParams(SafeLocalAllocHandle.InvalidHandle, ref size);

            while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW)
            {
                // Now we allocate the buffer and read the network parameters.
                using (buffer = Interop.mincore_obsolete.LocalAlloc(0, (UIntPtr)size))
                {
                    if (buffer.IsInvalid)
                    {
                        throw new OutOfMemoryException();
                    }

                    result = Interop.IpHlpApi.GetNetworkParams(buffer, ref size);
                    if (result == Interop.IpHlpApi.ERROR_SUCCESS)
                    {
                        fixedInfo = Marshal.PtrToStructure <Interop.IpHlpApi.FIXED_INFO>(buffer.DangerousGetHandle());
                    }
                }
            }

            // If the result include there being no information, we'll still throw
            if (result != Interop.IpHlpApi.ERROR_SUCCESS)
            {
                throw new NetworkInformationException((int)result);
            }
            return(fixedInfo);
        }
Beispiel #3
0
        private static Interop.IpHlpApi.FIXED_INFO GetFixedInfo()
        {
            uint size = 0;

            Interop.IpHlpApi.FIXED_INFO fixedInfo = default;

            // First we need to get the size of the buffer
            uint result = Interop.IpHlpApi.GetNetworkParams(IntPtr.Zero, ref size);

            while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW)
            {
                IntPtr buffer = Marshal.AllocHGlobal((int)size);
                try
                {
                    result = Interop.IpHlpApi.GetNetworkParams(buffer, ref size);
                    if (result == Interop.IpHlpApi.ERROR_SUCCESS)
                    {
                        fixedInfo = Marshal.PtrToStructure <Interop.IpHlpApi.FIXED_INFO>(buffer);
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(buffer);
                }
            }

            // If the result include there being no information, we'll still throw
            if (result != Interop.IpHlpApi.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            return(fixedInfo);
        }
        // TODO: #2485: Temporarily made GetFixedInfo() public to make things build.
        // This function needs to be switched back to private since it has no correspondent in the Unix world.
        public static Interop.IpHlpApi.FIXED_INFO GetFixedInfo()
        {
            Interop.IpHlpApi.FIXED_INFO fixedInfo = new Interop.IpHlpApi.FIXED_INFO();

            IReadOnlyList<HostName> hostNamesList = Windows.Networking.Connectivity.NetworkInformation.GetHostNames();

            foreach (HostName entry in hostNamesList)
            {
                // The first DomainName entry in the GetHostNames() list
                // is the fdqn of the machine itself.
                if (entry.Type == HostNameType.DomainName)
                {
                    string host = entry.ToString();
                    int dot = host.IndexOf('.');
                    if (dot != -1)
                    {
                        // The machine is domain joined.
                        fixedInfo.hostName = host.Substring(0, dot);
                        fixedInfo.domainName = host.Substring(dot+1);
                    }
                    else
                    {
                        // The machine is not domain joined.
                        fixedInfo.hostName = host;
                        fixedInfo.domainName = string.Empty;
                    }
                    
                    break;
                }
            }

            return fixedInfo;
        }
        // TODO: #2485: Temporarily made GetFixedInfo() public to make things build.
        // This function needs to be switched back to private since it has no correspondent in the Unix world.
        public static Interop.IpHlpApi.FIXED_INFO GetFixedInfo()
        {
            uint size = 0;
            SafeLocalAllocHandle buffer = null;
            Interop.IpHlpApi.FIXED_INFO fixedInfo = new Interop.IpHlpApi.FIXED_INFO();

            // First we need to get the size of the buffer
            uint result = Interop.IpHlpApi.GetNetworkParams(SafeLocalAllocHandle.InvalidHandle, ref size);

            while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW)
            {
                // Now we allocate the buffer and read the network parameters.
                using (buffer = Interop.mincore_obsolete.LocalAlloc(0, (UIntPtr)size))
                {
                    if (buffer.IsInvalid)
                    {
                        throw new OutOfMemoryException();
                    }

                    result = Interop.IpHlpApi.GetNetworkParams(buffer, ref size);
                    if (result == Interop.IpHlpApi.ERROR_SUCCESS)
                    {
                        fixedInfo = Marshal.PtrToStructure<Interop.IpHlpApi.FIXED_INFO>(buffer.DangerousGetHandle());
                    }
                }
            }

            // If the result include there being no information, we'll still throw
            if (result != Interop.IpHlpApi.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }
            
            return fixedInfo;
        }
        public static FixedNetworkInformation GetFixedInformation()
        {
            var              info   = new FixedNetworkInformation();
            uint             size   = 0;
            DisposableIntPtr buffer = null;

            Interop.IpHlpApi.FIXED_INFO fixedInfo = new Interop.IpHlpApi.FIXED_INFO();

            uint result = Interop.IpHlpApi.GetNetworkParams(IntPtr.Zero, ref size);

            while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW)
            {
                using (buffer = DisposableIntPtr.Alloc((int)size))
                {
                    if (buffer.IsValid)
                    {
                        result = Interop.IpHlpApi.GetNetworkParams(buffer.Ptr, ref size);
                        if (result == Interop.IpHlpApi.ERROR_SUCCESS)
                        {
                            fixedInfo = Marshal.PtrToStructure <Interop.IpHlpApi.FIXED_INFO>(buffer.Ptr);
                        }
                        else
                        {
                            throw new Win32Exception((int)result);
                        }

                        var dnsAddresses = new List <IPAddress>();
                        Interop.IpHlpApi.IP_ADDR_STRING addr = fixedInfo.DnsServerList;
                        IPAddress ip;

                        if (IPAddress.TryParse(addr.IpAddress, out ip))
                        {
                            dnsAddresses.Add(ip);

                            while (addr.Next != IntPtr.Zero)
                            {
                                addr = Marshal.PtrToStructure <Interop.IpHlpApi.IP_ADDR_STRING>(addr.Next);
                                if (IPAddress.TryParse(addr.IpAddress, out ip))
                                {
                                    dnsAddresses.Add(ip);
                                }
                            }
                        }

                        info.HostName     = fixedInfo.hostName;
                        info.DomainName   = fixedInfo.domainName;
                        info.DnsAddresses = dnsAddresses.ToArray();

                        return(info);
                    }
                    else
                    {
                        throw new OutOfMemoryException();
                    }
                }
            }

            return(info);
        }
Beispiel #7
0
        internal static NetworkInterface[] GetNetworkInterfaces()
        {
            Contract.Ensures(Contract.Result <NetworkInterface[]>() != null);
            AddressFamily        family     = AddressFamily.Unspecified;
            uint                 bufferSize = 0;
            SafeLocalAllocHandle buffer     = null;

            // TODO: #2485: This will probably require changes in the PAL for HostInformation.
            Interop.IpHlpApi.FIXED_INFO   fixedInfo     = HostInformationPal.GetFixedInfo();
            List <SystemNetworkInterface> interfaceList = new List <SystemNetworkInterface>();

            Interop.IpHlpApi.GetAdaptersAddressesFlags flags =
                Interop.IpHlpApi.GetAdaptersAddressesFlags.IncludeGateways
                | Interop.IpHlpApi.GetAdaptersAddressesFlags.IncludeWins;

            // Figure out the right buffer size for the adapter information.
            uint result = Interop.IpHlpApi.GetAdaptersAddresses(
                family, (uint)flags, IntPtr.Zero, SafeLocalAllocHandle.Zero, ref bufferSize);

            while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW)
            {
                // Allocate the buffer and get the adapter info.
                using (buffer = SafeLocalAllocHandle.LocalAlloc((int)bufferSize))
                {
                    result = Interop.IpHlpApi.GetAdaptersAddresses(
                        family, (uint)flags, IntPtr.Zero, buffer, ref bufferSize);

                    // If succeeded, we're going to add each new interface.
                    if (result == Interop.IpHlpApi.ERROR_SUCCESS)
                    {
                        // Linked list of interfaces.
                        IntPtr ptr = buffer.DangerousGetHandle();
                        while (ptr != IntPtr.Zero)
                        {
                            // Traverse the list, marshal in the native structures, and create new NetworkInterfaces.
                            Interop.IpHlpApi.IpAdapterAddresses adapterAddresses = Marshal.PtrToStructure <Interop.IpHlpApi.IpAdapterAddresses>(ptr);
                            interfaceList.Add(new SystemNetworkInterface(fixedInfo, adapterAddresses));

                            ptr = adapterAddresses.next;
                        }
                    }
                }
            }

            // If we don't have any interfaces detected, return empty.
            if (result == Interop.IpHlpApi.ERROR_NO_DATA || result == Interop.IpHlpApi.ERROR_INVALID_PARAMETER)
            {
                return(new SystemNetworkInterface[0]);
            }

            // Otherwise we throw on an error.
            if (result != Interop.IpHlpApi.ERROR_SUCCESS)
            {
                throw new NetworkInformationException((int)result);
            }

            return(interfaceList.ToArray());
        }
        internal SystemIPv4InterfaceProperties(Interop.IpHlpApi.FIXED_INFO fixedInfo, Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses)
        {
            _index          = ipAdapterAddresses.index;
            _routingEnabled = fixedInfo.enableRouting;
            _dhcpEnabled    = ((ipAdapterAddresses.flags & Interop.IpHlpApi.AdapterFlags.DhcpEnabled) != 0);
            _haveWins       = (ipAdapterAddresses.firstWinsServerAddress != IntPtr.Zero);

            _mtu = ipAdapterAddresses.mtu;

            GetPerAdapterInfo(ipAdapterAddresses.index);
        }
Beispiel #9
0
 private static void EnsureFixedInfo()
 {
     if (!Volatile.Read(ref s_fixedInfoInitialized))
     {
         lock (s_syncObject)
         {
             if (!s_fixedInfoInitialized)
             {
                 s_fixedInfo = GetFixedInfo();
                 Volatile.Write(ref s_fixedInfoInitialized, true);
             }
         }
     }
 }
 private static void EnsureFixedInfo()
 {
     if (!Volatile.Read(ref s_fixedInfoInitialized))
     {
         lock (s_syncObject)
         {
             if (!s_fixedInfoInitialized)
             {
                 s_fixedInfo = GetFixedInfo();
                 Volatile.Write(ref s_fixedInfoInitialized, true);
             }
         }
     }
 }
Beispiel #11
0
        internal SystemIPInterfaceProperties(Interop.IpHlpApi.FIXED_INFO fixedInfo, Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses)
        {
            _adapterFlags      = ipAdapterAddresses.flags;
            _dnsSuffix         = ipAdapterAddresses.dnsSuffix;
            _dnsEnabled        = fixedInfo.enableDns;
            _dynamicDnsEnabled = ((ipAdapterAddresses.flags & Interop.IpHlpApi.AdapterFlags.DnsEnabled) > 0);

            _multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection(
                Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress));
            _dnsAddresses     = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress);
            _anycastAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection(
                ipAdapterAddresses.firstAnycastAddress);
            _unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection(
                ipAdapterAddresses.firstUnicastAddress);
            _winsServersAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(
                ipAdapterAddresses.firstWinsServerAddress);
            _gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection(
                Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress));

            _dhcpServers = new InternalIPAddressCollection();
            if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero)
            {
                _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress());
            }

            if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero)
            {
                _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress());
            }

            if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv4Enabled) != 0)
            {
                _ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses);
            }

            if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv6Enabled) != 0)
            {
                _ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index,
                                                                    ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices);
            }
        }
Beispiel #12
0
        internal SystemNetworkInterface(Interop.IpHlpApi.FIXED_INFO fixedInfo, Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses)
        {
            // Store the common API information.
            _id          = ipAdapterAddresses.AdapterName;
            _name        = ipAdapterAddresses.friendlyName;
            _description = ipAdapterAddresses.description;
            _index       = ipAdapterAddresses.index;

            _physicalAddress = ipAdapterAddresses.address;
            _addressLength   = ipAdapterAddresses.addressLength;

            _type       = ipAdapterAddresses.type;
            _operStatus = ipAdapterAddresses.operStatus;
            _speed      = (long)ipAdapterAddresses.receiveLinkSpeed;

            // API specific info.
            _ipv6Index = ipAdapterAddresses.ipv6Index;

            _adapterFlags        = ipAdapterAddresses.flags;
            _interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses);
        }