internal SystemIPv4InterfaceProperties(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
 {
     this.index = ipAdapterInfo.index;
     this.routingEnabled = fixedInfo.EnableRouting;
     this.dhcpEnabled = ipAdapterInfo.dhcpEnabled;
     this.haveWins = ipAdapterInfo.haveWins;
     this.gatewayAddresses = ipAdapterInfo.gatewayList.ToIPGatewayAddressCollection();
     this.dhcpAddresses = ipAdapterInfo.dhcpServer.ToIPAddressCollection();
     IPAddressCollection addresss = ipAdapterInfo.primaryWinsServer.ToIPAddressCollection();
     IPAddressCollection addresss2 = ipAdapterInfo.secondaryWinsServer.ToIPAddressCollection();
     this.winsServerAddresses = new IPAddressCollection();
     foreach (IPAddress address in addresss)
     {
         this.winsServerAddresses.InternalAdd(address);
     }
     foreach (IPAddress address2 in addresss2)
     {
         this.winsServerAddresses.InternalAdd(address2);
     }
     SystemIPv4InterfaceStatistics statistics = new SystemIPv4InterfaceStatistics((long) this.index);
     this.mtu = (uint) statistics.Mtu;
     if (ComNetOS.IsWin2K)
     {
         this.GetPerAdapterInfo(ipAdapterInfo.index);
     }
     else
     {
         this.dnsAddresses = fixedInfo.DnsAddresses;
     }
 }
 internal bool Update(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
 {
     try
     {
         foreach (IPExtendedAddress address in ipAdapterInfo.ipAddressList.ToIPExtendedAddressArrayList())
         {
             foreach (SystemUnicastIPAddressInformation information in this.unicastAddresses)
             {
                 if (address.address.Equals(information.Address))
                 {
                     information.ipv4Mask = address.mask;
                 }
             }
         }
         this.ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterInfo);
         if ((this.dnsAddresses == null) || (this.dnsAddresses.Count == 0))
         {
             this.dnsAddresses = this.ipv4Properties.DnsAddresses;
         }
     }
     catch (NetworkInformationException exception)
     {
         if (((exception.ErrorCode != 0x57L) && (exception.ErrorCode != 13L)) && (((exception.ErrorCode != 0xe8L) && (exception.ErrorCode != 1L)) && (exception.ErrorCode != 2L)))
         {
             throw;
         }
         return(false);
     }
     return(true);
 }
Exemplo n.º 3
0
        internal SystemIPv4InterfaceProperties(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
        {
            this.index            = ipAdapterInfo.index;
            this.routingEnabled   = fixedInfo.EnableRouting;
            this.dhcpEnabled      = ipAdapterInfo.dhcpEnabled;
            this.haveWins         = ipAdapterInfo.haveWins;
            this.gatewayAddresses = ipAdapterInfo.gatewayList.ToIPGatewayAddressCollection();
            this.dhcpAddresses    = ipAdapterInfo.dhcpServer.ToIPAddressCollection();
            IPAddressCollection addresss  = ipAdapterInfo.primaryWinsServer.ToIPAddressCollection();
            IPAddressCollection addresss2 = ipAdapterInfo.secondaryWinsServer.ToIPAddressCollection();

            this.winsServerAddresses = new IPAddressCollection();
            foreach (IPAddress address in addresss)
            {
                this.winsServerAddresses.InternalAdd(address);
            }
            foreach (IPAddress address2 in addresss2)
            {
                this.winsServerAddresses.InternalAdd(address2);
            }
            SystemIPv4InterfaceStatistics statistics = new SystemIPv4InterfaceStatistics((long)this.index);

            this.mtu = (uint)statistics.Mtu;
            if (ComNetOS.IsWin2K)
            {
                this.GetPerAdapterInfo(ipAdapterInfo.index);
            }
            else
            {
                this.dnsAddresses = fixedInfo.DnsAddresses;
            }
        }
        private static NetworkInterface[] GetNetworkInterfaces(AddressFamily family)
        {
            IpHelperErrors.CheckFamilyUnspecified(family);
            if (ComNetOS.IsPostWin2K)
            {
                return(PostWin2KGetNetworkInterfaces(family));
            }
            FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo();

            if ((family != AddressFamily.Unspecified) && (family != AddressFamily.InterNetwork))
            {
                throw new PlatformNotSupportedException(SR.GetString("WinXPRequired"));
            }
            SafeLocalFree pAdapterInfo = null;
            uint          pOutBufLen   = 0;
            ArrayList     list         = new ArrayList();
            uint          adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(SafeLocalFree.Zero, ref pOutBufLen);

            while (adaptersInfo == 0x6f)
            {
                try
                {
                    pAdapterInfo = SafeLocalFree.LocalAlloc((int)pOutBufLen);
                    adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(pAdapterInfo, ref pOutBufLen);
                    if (adaptersInfo == 0)
                    {
                        IpAdapterInfo ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(pAdapterInfo.DangerousGetHandle(), typeof(IpAdapterInfo));
                        list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterInfo));
                        while (ipAdapterInfo.Next != IntPtr.Zero)
                        {
                            ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(ipAdapterInfo.Next, typeof(IpAdapterInfo));
                            list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterInfo));
                        }
                    }
                    continue;
                }
                finally
                {
                    if (pAdapterInfo != null)
                    {
                        pAdapterInfo.Close();
                    }
                }
            }
            if (adaptersInfo == 0xe8)
            {
                return(new SystemNetworkInterface[0]);
            }
            if (adaptersInfo != 0)
            {
                throw new NetworkInformationException((int)adaptersInfo);
            }
            SystemNetworkInterface[] interfaceArray = new SystemNetworkInterface[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                interfaceArray[i] = (SystemNetworkInterface)list[i];
            }
            return(interfaceArray);
        }
 internal SystemUnicastIPAddressInformation(IpAdapterInfo ipAdapterInfo, IPExtendedAddress address)
 {
     this.innerInfo = new SystemIPAddressInformation(address.address);
     DateTime time = new DateTime(0x7b2, 1, 1);
     time = time.AddSeconds((double) ipAdapterInfo.leaseExpires);
     TimeSpan span = (TimeSpan) (time - DateTime.UtcNow);
     this.dhcpLeaseLifetime = (long) span.TotalSeconds;
     this.ipv4Mask = address.mask;
 }
        internal SystemUnicastIPAddressInformation(IpAdapterInfo ipAdapterInfo, IPExtendedAddress address)
        {
            this.innerInfo = new SystemIPAddressInformation(address.address);
            DateTime time = new DateTime(0x7b2, 1, 1);

            time = time.AddSeconds((double)ipAdapterInfo.leaseExpires);
            TimeSpan span = (TimeSpan)(time - DateTime.UtcNow);

            this.dhcpLeaseLifetime = (long)span.TotalSeconds;
            this.ipv4Mask          = address.mask;
        }
        internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
        {
            this.id              = ipAdapterInfo.adapterName;
            this.name            = string.Empty;
            this.description     = ipAdapterInfo.description;
            this.index           = ipAdapterInfo.index;
            this.physicalAddress = ipAdapterInfo.address;
            this.addressLength   = ipAdapterInfo.addressLength;
            if (ComNetOS.IsWin2K && !ComNetOS.IsPostWin2K)
            {
                this.name = this.ReadAdapterName(this.id);
            }
            if (this.name.Length == 0)
            {
                this.name = this.description;
            }
            SystemIPv4InterfaceStatistics statistics = new SystemIPv4InterfaceStatistics((long)this.index);

            this.operStatus = statistics.OperationalStatus;
            switch (ipAdapterInfo.type)
            {
            case OldInterfaceType.Ppp:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ppp;
                break;

            case OldInterfaceType.Loopback:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Loopback;
                break;

            case OldInterfaceType.Slip:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Slip;
                break;

            case OldInterfaceType.Fddi:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Fddi;
                break;

            case OldInterfaceType.Ethernet:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ethernet;
                break;

            case OldInterfaceType.TokenRing:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.TokenRing;
                break;

            default:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Unknown;
                break;
            }
            this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterInfo);
        }
        internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
        {
            this.id = ipAdapterInfo.adapterName;
            this.name = string.Empty;
            this.description = ipAdapterInfo.description;
            this.index = ipAdapterInfo.index;
            this.physicalAddress = ipAdapterInfo.address;
            this.addressLength = ipAdapterInfo.addressLength;
            if (ComNetOS.IsWin2K && !ComNetOS.IsPostWin2K)
            {
                this.name = this.ReadAdapterName(this.id);
            }
            if (this.name.Length == 0)
            {
                this.name = this.description;
            }
            SystemIPv4InterfaceStatistics statistics = new SystemIPv4InterfaceStatistics((long) this.index);
            this.operStatus = statistics.OperationalStatus;
            switch (ipAdapterInfo.type)
            {
                case OldInterfaceType.Ppp:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ppp;
                    break;

                case OldInterfaceType.Loopback:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Loopback;
                    break;

                case OldInterfaceType.Slip:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Slip;
                    break;

                case OldInterfaceType.Fddi:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Fddi;
                    break;

                case OldInterfaceType.Ethernet:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ethernet;
                    break;

                case OldInterfaceType.TokenRing:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.TokenRing;
                    break;

                default:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Unknown;
                    break;
            }
            this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterInfo);
        }
 internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
 {
     this.dnsEnabled         = fixedInfo.EnableDns;
     this.name               = ipAdapterInfo.adapterName;
     this.index              = ipAdapterInfo.index;
     this.multicastAddresses = new MulticastIPAddressInformationCollection();
     this.anycastAddresses   = new IPAddressInformationCollection();
     if (this.index > 0)
     {
         this.versionSupported |= IPVersion.IPv4;
     }
     if (ComNetOS.IsWin2K)
     {
         this.ReadRegDnsSuffix();
     }
     this.unicastAddresses = new UnicastIPAddressInformationCollection();
     foreach (IPExtendedAddress address in ipAdapterInfo.ipAddressList.ToIPExtendedAddressArrayList())
     {
         this.unicastAddresses.InternalAdd(new SystemUnicastIPAddressInformation(ipAdapterInfo, address));
     }
     try
     {
         this.ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterInfo);
         if ((this.dnsAddresses == null) || (this.dnsAddresses.Count == 0))
         {
             this.dnsAddresses = this.ipv4Properties.DnsAddresses;
         }
     }
     catch (NetworkInformationException exception)
     {
         if (exception.ErrorCode != 0x57L)
         {
             throw;
         }
     }
 }
 internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
 {
     this.dnsEnabled = fixedInfo.EnableDns;
     this.name = ipAdapterInfo.adapterName;
     this.index = ipAdapterInfo.index;
     this.multicastAddresses = new MulticastIPAddressInformationCollection();
     this.anycastAddresses = new IPAddressInformationCollection();
     if (this.index > 0)
     {
         this.versionSupported |= IPVersion.IPv4;
     }
     if (ComNetOS.IsWin2K)
     {
         this.ReadRegDnsSuffix();
     }
     this.unicastAddresses = new UnicastIPAddressInformationCollection();
     foreach (IPExtendedAddress address in ipAdapterInfo.ipAddressList.ToIPExtendedAddressArrayList())
     {
         this.unicastAddresses.InternalAdd(new SystemUnicastIPAddressInformation(ipAdapterInfo, address));
     }
     try
     {
         this.ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterInfo);
         if ((this.dnsAddresses == null) || (this.dnsAddresses.Count == 0))
         {
             this.dnsAddresses = this.ipv4Properties.DnsAddresses;
         }
     }
     catch (NetworkInformationException exception)
     {
         if (exception.ErrorCode != 0x57L)
         {
             throw;
         }
     }
 }
        private static SystemNetworkInterface[] PostWin2KGetNetworkInterfaces(AddressFamily family)
        {
            FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo();

            SystemNetworkInterface[] adaptersAddresses = null;
Label_0008:
            try
            {
                adaptersAddresses = GetAdaptersAddresses(family, fixedInfo);
            }
            catch (NetworkInformationException exception)
            {
                if (exception.ErrorCode != 1L)
                {
                    throw;
                }
                goto Label_0008;
            }
            if (Socket.OSSupportsIPv4)
            {
                uint          pOutBufLen   = 0;
                uint          adaptersInfo = 0;
                SafeLocalFree pAdapterInfo = null;
                if ((family == AddressFamily.Unspecified) || (family == AddressFamily.InterNetwork))
                {
                    adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(SafeLocalFree.Zero, ref pOutBufLen);
                    int num3 = 0;
                    while (adaptersInfo == 0x6f)
                    {
                        try
                        {
                            pAdapterInfo = SafeLocalFree.LocalAlloc((int)pOutBufLen);
                            adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(pAdapterInfo, ref pOutBufLen);
                            if (adaptersInfo == 0)
                            {
                                IntPtr handle = pAdapterInfo.DangerousGetHandle();
                                while (handle != IntPtr.Zero)
                                {
                                    IpAdapterInfo ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(handle, typeof(IpAdapterInfo));
                                    for (int i = 0; i < adaptersAddresses.Length; i++)
                                    {
                                        if ((adaptersAddresses[i] != null) && (ipAdapterInfo.index == adaptersAddresses[i].index))
                                        {
                                            if (!adaptersAddresses[i].interfaceProperties.Update(fixedInfo, ipAdapterInfo))
                                            {
                                                adaptersAddresses[i] = null;
                                                num3++;
                                            }
                                            break;
                                        }
                                    }
                                    handle = ipAdapterInfo.Next;
                                }
                            }
                            continue;
                        }
                        finally
                        {
                            if (pAdapterInfo != null)
                            {
                                pAdapterInfo.Close();
                            }
                        }
                    }
                    if (num3 != 0)
                    {
                        SystemNetworkInterface[] interfaceArray2 = new SystemNetworkInterface[adaptersAddresses.Length - num3];
                        int num5 = 0;
                        for (int j = 0; j < adaptersAddresses.Length; j++)
                        {
                            if (adaptersAddresses[j] != null)
                            {
                                interfaceArray2[num5++] = adaptersAddresses[j];
                            }
                        }
                        adaptersAddresses = interfaceArray2;
                    }
                }
                if ((adaptersInfo != 0) && (adaptersInfo != 0xe8))
                {
                    throw new NetworkInformationException((int)adaptersInfo);
                }
            }
            return(adaptersAddresses);
        }
 internal bool Update(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
 {
     try
     {
         foreach (IPExtendedAddress address in ipAdapterInfo.ipAddressList.ToIPExtendedAddressArrayList())
         {
             foreach (SystemUnicastIPAddressInformation information in this.unicastAddresses)
             {
                 if (address.address.Equals(information.Address))
                 {
                     information.ipv4Mask = address.mask;
                 }
             }
         }
         this.ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterInfo);
         if ((this.dnsAddresses == null) || (this.dnsAddresses.Count == 0))
         {
             this.dnsAddresses = this.ipv4Properties.DnsAddresses;
         }
     }
     catch (NetworkInformationException exception)
     {
         if (((exception.ErrorCode != 0x57L) && (exception.ErrorCode != 13L)) && (((exception.ErrorCode != 0xe8L) && (exception.ErrorCode != 1L)) && (exception.ErrorCode != 2L)))
         {
             throw;
         }
         return false;
     }
     return true;
 }