public static unsafe IPHostEntry GetHostByAddr(IPAddress addr)
        {
            // TODO #2891: Optimize this (or decide if this legacy code can be removed):
            byte[] addressBytes = addr.GetAddressBytes();
            var address = new Interop.libc.in_addr { s_addr = unchecked((uint)BitConverter.ToInt32(addressBytes, 0)) };

            int bufferSize = 512;
            byte* stackBuffer = stackalloc byte[bufferSize];

            var hostent = default(Interop.libc.hostent);
            var result = (Interop.libc.hostent*)null;
            if (TryGetHostByAddr(address, stackBuffer, bufferSize, &hostent, &result))
            {
                return CreateHostEntry(result);
            }

            for (; ;)
            {
                bufferSize *= 2;
                fixed (byte* heapBuffer = new byte[bufferSize])
                {
                    if (TryGetHostByAddr(address, heapBuffer, bufferSize, &hostent, &result))
                    {
                        return CreateHostEntry(result);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static unsafe IPHostEntry GetHostByAddr(IPAddress addr)
        {
            // TODO #2891: Optimize this (or decide if this legacy code can be removed):
            byte[] addressBytes = addr.GetAddressBytes();
            var    address      = new Interop.libc.in_addr {
                s_addr = unchecked ((uint)BitConverter.ToInt32(addressBytes, 0))
            };

            int   bufferSize  = 512;
            byte *stackBuffer = stackalloc byte[bufferSize];

            var hostent = default(Interop.libc.hostent);
            var result  = (Interop.libc.hostent *)null;

            if (TryGetHostByAddr(address, stackBuffer, bufferSize, &hostent, &result))
            {
                return(CreateHostEntry(&hostent));
            }

            for (; ;)
            {
                bufferSize *= 2;
                fixed(byte *heapBuffer = new byte[bufferSize])
                {
                    if (TryGetHostByAddr(address, heapBuffer, bufferSize, &hostent, &result))
                    {
                        return(CreateHostEntry(&hostent));
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static unsafe IPHostEntry GetHostByAddr(IPAddress addr)
        {
            // TODO #2891: Optimize this (or decide if this legacy code can be removed):
            byte[] addressBytes = addr.GetAddressBytes();
            var address = new Interop.libc.in_addr { s_addr = unchecked((uint)BitConverter.ToInt32(addressBytes, 0)) };

            Interop.libc.hostent* hostent = Interop.libc.gethostbyaddr(&address, (uint)sizeof(Interop.libc.in_addr), Interop.libc.AF_INET);
            if (hostent == null)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new InternalSocketException(GetSocketErrorForErrno(errno), errno);
            }

            return CreateHostEntry(hostent);
        }
Exemplo n.º 4
0
        private static unsafe bool TryGetHostByAddr(Interop.libc.in_addr address, byte *buffer, int bufferSize, Interop.libc.hostent *hostent, Interop.libc.hostent **result)
        {
            int errno;
            int err = Interop.libc.gethostbyaddr_r(&address, (uint)sizeof(Interop.libc.in_addr), Interop.libc.AF_INET, hostent, buffer, (IntPtr)bufferSize, result, &errno);

            switch (Interop.Sys.ConvertErrorPlatformToPal(err))
            {
            case 0:
                return(true);

            case Interop.Error.ERANGE:
                return(false);

            default:
                throw new InternalSocketException(GetSocketErrorForErrno(errno), errno);
            }
        }
Exemplo n.º 5
0
        public static unsafe IPHostEntry GetHostByAddr(IPAddress addr)
        {
            // TODO #2891: Optimize this (or decide if this legacy code can be removed):
            byte[] addressBytes = addr.GetAddressBytes();
            var    address      = new Interop.libc.in_addr {
                s_addr = unchecked ((uint)BitConverter.ToInt32(addressBytes, 0))
            };

            Interop.libc.hostent *hostent = Interop.libc.gethostbyaddr(&address, (uint)sizeof(Interop.libc.in_addr), Interop.libc.AF_INET);
            if (hostent == null)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new InternalSocketException(GetSocketErrorForErrno(errno), errno);
            }

            return(CreateHostEntry(hostent));
        }