internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses)
 {
     this.dnsEnabled = fixedInfo.EnableDns;
     this.index      = ipAdapterAddresses.index;
     this.name       = ipAdapterAddresses.AdapterName;
     this.ipv6Index  = ipAdapterAddresses.ipv6Index;
     if (this.index > 0)
     {
         this.versionSupported |= IPVersion.IPv4;
     }
     if (this.ipv6Index > 0)
     {
         this.versionSupported |= IPVersion.IPv6;
     }
     this.mtu                = ipAdapterAddresses.mtu;
     this.adapterFlags       = ipAdapterAddresses.flags;
     this.dnsSuffix          = ipAdapterAddresses.dnsSuffix;
     this.dynamicDnsEnabled  = (ipAdapterAddresses.flags & AdapterFlags.DnsEnabled) > 0;
     this.multicastAddresses = SystemMulticastIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstMulticastAddress);
     this.dnsAddresses       = SystemIPAddressInformation.ToAddressCollection(ipAdapterAddresses.FirstDnsServerAddress, this.versionSupported);
     this.anycastAddresses   = SystemIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstAnycastAddress, this.versionSupported);
     this.unicastAddresses   = SystemUnicastIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstUnicastAddress);
     if (this.ipv6Index > 0)
     {
         this.ipv6Properties = new SystemIPv6InterfaceProperties(this.ipv6Index, this.mtu);
     }
 }
        // This constructor is for Vista and newer
        internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) {
            adapterFlags = ipAdapterAddresses.flags;
            dnsSuffix = ipAdapterAddresses.dnsSuffix;
            dnsEnabled = fixedInfo.EnableDns;
            dynamicDnsEnabled = ((ipAdapterAddresses.flags & AdapterFlags.DnsEnabled) > 0);

            multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection(
                IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress));
            dnsAddresses = IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress);
            anycastAddresses = IpAdapterAddress.MarshalIpAddressInformationCollection(
                ipAdapterAddresses.firstAnycastAddress);
            unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection(
                ipAdapterAddresses.firstUnicastAddress);
            winsServersAddresses = IpAdapterAddress.MarshalIpAddressCollection(
                ipAdapterAddresses.firstWinsServerAddress);
            gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection(
                IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress));

            dhcpServers = new IPAddressCollection();
            if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero)
                dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress());
            if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero)
                dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress());

            if ((adapterFlags & AdapterFlags.IPv4Enabled) != 0) {
                ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses);
            }

            if ((adapterFlags & AdapterFlags.IPv6Enabled) != 0) {
                ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index, 
                    ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices);
            }
        }
 internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses)
 {
     this.dnsEnabled = fixedInfo.EnableDns;
     this.index = ipAdapterAddresses.index;
     this.name = ipAdapterAddresses.AdapterName;
     this.ipv6Index = ipAdapterAddresses.ipv6Index;
     if (this.index > 0)
     {
         this.versionSupported |= IPVersion.IPv4;
     }
     if (this.ipv6Index > 0)
     {
         this.versionSupported |= IPVersion.IPv6;
     }
     this.mtu = ipAdapterAddresses.mtu;
     this.adapterFlags = ipAdapterAddresses.flags;
     this.dnsSuffix = ipAdapterAddresses.dnsSuffix;
     this.dynamicDnsEnabled = (ipAdapterAddresses.flags & AdapterFlags.DnsEnabled) > 0;
     this.multicastAddresses = SystemMulticastIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstMulticastAddress);
     this.dnsAddresses = SystemIPAddressInformation.ToAddressCollection(ipAdapterAddresses.FirstDnsServerAddress, this.versionSupported);
     this.anycastAddresses = SystemIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstAnycastAddress, this.versionSupported);
     this.unicastAddresses = SystemUnicastIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstUnicastAddress);
     if (this.ipv6Index > 0)
     {
         this.ipv6Properties = new SystemIPv6InterfaceProperties(this.ipv6Index, this.mtu);
     }
 }
        // Vista+
        internal SystemIPv4InterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) {
            index = ipAdapterAddresses.index;
            routingEnabled = fixedInfo.EnableRouting;
            dhcpEnabled = ((ipAdapterAddresses.flags & AdapterFlags.DhcpEnabled) != 0);
            haveWins = (ipAdapterAddresses.firstWinsServerAddress != IntPtr.Zero);

            mtu = ipAdapterAddresses.mtu;

            GetPerAdapterInfo(ipAdapterAddresses.index);
        }
        // Vista+
        internal SystemIPv4InterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses)
        {
            index          = ipAdapterAddresses.index;
            routingEnabled = fixedInfo.EnableRouting;
            dhcpEnabled    = ((ipAdapterAddresses.flags & AdapterFlags.DhcpEnabled) != 0);
            haveWins       = (ipAdapterAddresses.firstWinsServerAddress != IntPtr.Zero);

            mtu = ipAdapterAddresses.mtu;

            GetPerAdapterInfo(ipAdapterAddresses.index);
        }
        private static SystemNetworkInterface[] GetAdaptersAddresses(AddressFamily family, FixedInfo fixedInfo)
        {
            uint          outBufLen        = 0;
            SafeLocalFree adapterAddresses = null;
            ArrayList     list             = new ArrayList();

            SystemNetworkInterface[] interfaceArray = null;
            uint num2 = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(family, 0, IntPtr.Zero, SafeLocalFree.Zero, ref outBufLen);

            while (num2 == 0x6f)
            {
                try
                {
                    adapterAddresses = SafeLocalFree.LocalAlloc((int)outBufLen);
                    num2             = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(family, 0, IntPtr.Zero, adapterAddresses, ref outBufLen);
                    if (num2 == 0)
                    {
                        IpAdapterAddresses ipAdapterAddresses = (IpAdapterAddresses)Marshal.PtrToStructure(adapterAddresses.DangerousGetHandle(), typeof(IpAdapterAddresses));
                        list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterAddresses));
                        while (ipAdapterAddresses.next != IntPtr.Zero)
                        {
                            ipAdapterAddresses = (IpAdapterAddresses)Marshal.PtrToStructure(ipAdapterAddresses.next, typeof(IpAdapterAddresses));
                            list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterAddresses));
                        }
                    }
                    continue;
                }
                finally
                {
                    if (adapterAddresses != null)
                    {
                        adapterAddresses.Close();
                    }
                    adapterAddresses = null;
                }
            }
            switch (num2)
            {
            case 0xe8:
            case 0x57:
                return(new SystemNetworkInterface[0]);
            }
            if (num2 != 0)
            {
                throw new NetworkInformationException((int)num2);
            }
            interfaceArray = new SystemNetworkInterface[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                interfaceArray[i] = (SystemNetworkInterface)list[i];
            }
            return(interfaceArray);
        }
 internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses)
 {
     this.id                  = ipAdapterAddresses.AdapterName;
     this.name                = ipAdapterAddresses.friendlyName;
     this.description         = ipAdapterAddresses.description;
     this.index               = ipAdapterAddresses.index;
     this.physicalAddress     = ipAdapterAddresses.address;
     this.addressLength       = ipAdapterAddresses.addressLength;
     this.type                = ipAdapterAddresses.type;
     this.operStatus          = ipAdapterAddresses.operStatus;
     this.ipv6Index           = ipAdapterAddresses.ipv6Index;
     this.adapterFlags        = ipAdapterAddresses.flags;
     this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses);
 }
 internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses)
 {
     this.id = ipAdapterAddresses.AdapterName;
     this.name = ipAdapterAddresses.friendlyName;
     this.description = ipAdapterAddresses.description;
     this.index = ipAdapterAddresses.index;
     this.physicalAddress = ipAdapterAddresses.address;
     this.addressLength = ipAdapterAddresses.addressLength;
     this.type = ipAdapterAddresses.type;
     this.operStatus = ipAdapterAddresses.operStatus;
     this.ipv6Index = ipAdapterAddresses.ipv6Index;
     this.adapterFlags = ipAdapterAddresses.flags;
     this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses);
 }
        // Vista+
        internal SystemNetworkInterface(FixedInfo fixedInfo, 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);
        }
Ejemplo n.º 10
0
        // This constructor is for Vista and newer
        internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses)
        {
            adapterFlags      = ipAdapterAddresses.flags;
            dnsSuffix         = ipAdapterAddresses.dnsSuffix;
            dnsEnabled        = fixedInfo.EnableDns;
            dynamicDnsEnabled = ((ipAdapterAddresses.flags & AdapterFlags.DnsEnabled) > 0);

            multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection(
                IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress));
            dnsAddresses     = IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress);
            anycastAddresses = IpAdapterAddress.MarshalIpAddressInformationCollection(
                ipAdapterAddresses.firstAnycastAddress);
            unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection(
                ipAdapterAddresses.firstUnicastAddress);
            winsServersAddresses = IpAdapterAddress.MarshalIpAddressCollection(
                ipAdapterAddresses.firstWinsServerAddress);
            gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection(
                IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress));

            dhcpServers = new IPAddressCollection();
            if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero)
            {
                dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress());
            }
            if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero)
            {
                dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress());
            }

            if ((adapterFlags & AdapterFlags.IPv4Enabled) != 0)
            {
                ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses);
            }

            if ((adapterFlags & AdapterFlags.IPv6Enabled) != 0)
            {
                ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index,
                                                                   ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices);
            }
        }
        internal static NetworkInterface[] GetNetworkInterfaces()
        {
            Contract.Ensures(Contract.Result <NetworkInterface[]>() != null);
            AddressFamily family     = AddressFamily.Unspecified;
            uint          bufferSize = 0;
            SafeLocalFree buffer     = null;
            FixedInfo     fixedInfo  = SystemIPGlobalProperties.GetFixedInfo();
            List <SystemNetworkInterface> interfaceList = new List <SystemNetworkInterface>();

            GetAdaptersAddressesFlags flags =
                GetAdaptersAddressesFlags.IncludeGateways
                | GetAdaptersAddressesFlags.IncludeWins;

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

            while (result == IpHelperErrors.ErrorBufferOverflow)
            {
                try {
                    // Allocate the buffer and get the adapter info
                    buffer = SafeLocalFree.LocalAlloc((int)bufferSize);
                    result = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(
                        family, (uint)flags, IntPtr.Zero, buffer, ref bufferSize);

                    // If succeeded, we're going to add each new interface
                    if (result == IpHelperErrors.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
                            IpAdapterAddresses adapterAddresses =
                                (IpAdapterAddresses)Marshal.PtrToStructure(ptr, typeof(IpAdapterAddresses));
                            interfaceList.Add(new SystemNetworkInterface(fixedInfo, adapterAddresses));

                            ptr = adapterAddresses.next;
                        }
                    }
                }
                finally {
                    if (buffer != null)
                    {
                        buffer.Close();
                    }
                    buffer = null;
                }
            }

            // if we don't have any interfaces detected, return empty.
            if (result == IpHelperErrors.ErrorNoData || result == IpHelperErrors.ErrorInvalidParameter)
            {
                return(new SystemNetworkInterface[0]);
            }

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

            return(interfaceList.ToArray());
        }
        // Vista+
        internal SystemNetworkInterface(FixedInfo fixedInfo, 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);
        }