Exemplo n.º 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);
        }
Exemplo n.º 2
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);
        }