示例#1
0
        private static IPAddress[] GetLocalAddresses()
        {
            IPAddress[] local;

            ArrayList collections = new ArrayList(16);
            int       total       = 0;

            SafeLocalFree             buffer   = null;
            GetAdaptersAddressesFlags gaaFlags = GetAdaptersAddressesFlags.SkipAnycast | GetAdaptersAddressesFlags.SkipMulticast |
                                                 GetAdaptersAddressesFlags.SkipFriendlyName | GetAdaptersAddressesFlags.SkipDnsServer;
            uint size   = 0;
            uint result = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(AddressFamily.Unspecified, (uint)gaaFlags, IntPtr.Zero, SafeLocalFree.Zero, ref size);

            while (result == IpHelperErrors.ErrorBufferOverflow)
            {
                try
                {
                    buffer = SafeLocalFree.LocalAlloc((int)size);
                    result = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(AddressFamily.Unspecified, (uint)gaaFlags, IntPtr.Zero, buffer, ref size);

                    if (result == IpHelperErrors.Success)
                    {
                        IntPtr nextAdapter = buffer.DangerousGetHandle();

                        while (nextAdapter != IntPtr.Zero)
                        {
                            IpAdapterAddresses adapterAddresses = (IpAdapterAddresses)Marshal.PtrToStructure(
                                nextAdapter, typeof(IpAdapterAddresses));

                            if (adapterAddresses.firstUnicastAddress != IntPtr.Zero)
                            {
                                UnicastIPAddressInformationCollection coll =
                                    SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection(
                                        adapterAddresses.firstUnicastAddress);
                                total += coll.Count;
                                collections.Add(coll);
                            }

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

            if (result != IpHelperErrors.Success && result != IpHelperErrors.ErrorNoData)
            {
                throw new NetworkInformationException((int)result);
            }

            local = new IPAddress[total];
            uint i = 0;

            foreach (UnicastIPAddressInformationCollection coll in collections)
            {
                foreach (IPAddressInformation info in coll)
                {
                    local[i++] = info.Address;
                }
            }

            return(local);
        }
示例#2
0
 internal static extern unsafe int GetAdaptersAddresses(
     [In] AddressFamily family,
     [In] GetAdaptersAddressesFlags flags,
     [In] IntPtr pReserved,
     [In, Out] byte *adapterAddresses,
     [In, Out] ref int outBufLen);
        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());
        }
示例#4
0
        private static IPAddress[] GetLocalAddresses()
        {
            IPAddress[] addressArray;
            if (!ComNetOS.IsPostWin2K)
            {
                ArrayList     list2        = new ArrayList(0x10);
                int           num5         = 0;
                SafeLocalFree pAdapterInfo = null;
                uint          pOutBufLen   = 0;
                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 info = (IpAdapterInfo)Marshal.PtrToStructure(pAdapterInfo.DangerousGetHandle(), typeof(IpAdapterInfo));
                            while (true)
                            {
                                IPAddressCollection addresss = info.ipAddressList.ToIPAddressCollection();
                                num5 += addresss.Count;
                                list2.Add(addresss);
                                if (info.Next == IntPtr.Zero)
                                {
                                    break;
                                }
                                info = (IpAdapterInfo)Marshal.PtrToStructure(info.Next, typeof(IpAdapterInfo));
                            }
                        }
                        continue;
                    }
                    finally
                    {
                        if (pAdapterInfo != null)
                        {
                            pAdapterInfo.Close();
                        }
                    }
                }
                if ((adaptersInfo != 0) && (adaptersInfo != 0xe8))
                {
                    throw new NetworkInformationException((int)adaptersInfo);
                }
                addressArray = new IPAddress[num5];
                uint num8 = 0;
                foreach (IPAddressCollection addresss2 in list2)
                {
                    foreach (IPAddress address in addresss2)
                    {
                        addressArray[num8++] = address;
                    }
                }
                return(addressArray);
            }
            ArrayList                 list             = new ArrayList(0x10);
            int                       num              = 0;
            SafeLocalFree             adapterAddresses = null;
            GetAdaptersAddressesFlags flags            = GetAdaptersAddressesFlags.SkipFriendlyName | GetAdaptersAddressesFlags.SkipDnsServer | GetAdaptersAddressesFlags.SkipMulticast | GetAdaptersAddressesFlags.SkipAnycast;
            uint                      outBufLen        = 0;
            uint                      num3             = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(AddressFamily.Unspecified, (uint)flags, IntPtr.Zero, SafeLocalFree.Zero, ref outBufLen);

            while (num3 == 0x6f)
            {
                try
                {
                    adapterAddresses = SafeLocalFree.LocalAlloc((int)outBufLen);
                    num3             = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(AddressFamily.Unspecified, (uint)flags, IntPtr.Zero, adapterAddresses, ref outBufLen);
                    if (num3 != 0)
                    {
                        continue;
                    }
                    IpAdapterAddresses addresses = (IpAdapterAddresses)Marshal.PtrToStructure(adapterAddresses.DangerousGetHandle(), typeof(IpAdapterAddresses));
Label_0075:
                    if (addresses.FirstUnicastAddress != IntPtr.Zero)
                    {
                        UnicastIPAddressInformationCollection informations = SystemUnicastIPAddressInformation.ToAddressInformationCollection(addresses.FirstUnicastAddress);
                        num += informations.Count;
                        list.Add(informations);
                    }
                    if (!(addresses.next == IntPtr.Zero))
                    {
                        addresses = (IpAdapterAddresses)Marshal.PtrToStructure(addresses.next, typeof(IpAdapterAddresses));
                        goto Label_0075;
                    }
                    continue;
                }
                finally
                {
                    if (adapterAddresses != null)
                    {
                        adapterAddresses.Close();
                    }
                    adapterAddresses = null;
                }
            }
            if ((num3 != 0) && (num3 != 0xe8))
            {
                throw new NetworkInformationException((int)num3);
            }
            addressArray = new IPAddress[num];
            uint num4 = 0;

            foreach (UnicastIPAddressInformationCollection informations2 in list)
            {
                foreach (IPAddressInformation information in informations2)
                {
                    addressArray[num4++] = information.Address;
                }
            }
            return(addressArray);
        }