Exemplo n.º 1
0
        private static unsafe IPHostEntry CreateHostEntry(Interop.libc.hostent *hostent)
        {
            string hostName = null;

            if (hostent->h_name != null)
            {
                hostName = Marshal.PtrToStringAnsi((IntPtr)hostent->h_name);
            }

            int numAddresses;

            for (numAddresses = 0; hostent->h_addr_list[numAddresses] != null; numAddresses++)
            {
            }

            IPAddress[] ipAddresses;
            if (numAddresses == 0)
            {
                ipAddresses = Array.Empty <IPAddress>();
            }
            else
            {
                ipAddresses = new IPAddress[numAddresses];
                for (int i = 0; i < numAddresses; i++)
                {
                    Debug.Assert(hostent->h_addr_list[i] != null);
                    ipAddresses[i] = new IPAddress(*(int *)hostent->h_addr_list[i]);
                }
            }

            int numAliases;

            for (numAliases = 0; hostent->h_aliases[numAliases] != null; numAliases++)
            {
            }

            string[] aliases;
            if (numAliases == 0)
            {
                aliases = Array.Empty <string>();
            }
            else
            {
                aliases = new string[numAliases];
                for (int i = 0; i < numAliases; i++)
                {
                    Debug.Assert(hostent->h_aliases[i] != null);
                    aliases[i] = Marshal.PtrToStringAnsi((IntPtr)hostent->h_aliases[i]);
                }
            }

            return(new IPHostEntry
            {
                HostName = hostName,
                AddressList = ipAddresses,
                Aliases = aliases
            });
        }
Exemplo n.º 2
0
        public static unsafe IPHostEntry GetHostByName(string hostName)
        {
            Interop.libc.hostent *hostent = Interop.libc.gethostbyname(hostName);
            if (hostent == null)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new InternalSocketException(GetSocketErrorForErrno(errno), errno);
            }

            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
        private static unsafe bool TryGetHostByName(string hostName, byte *buffer, int bufferSize, Interop.libc.hostent *hostent, Interop.libc.hostent **result)
        {
            int errno;
            int err = Interop.libc.gethostbyname_r(hostName, 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);
            }
        }