Ejemplo n.º 1
0
        public static unsafe SocketError TryGetAddrInfo(string name, out IPHostEntry hostinfo, out int nativeErrorCode)
        {
            Interop.Sys.HostEntry *entry = null;
            int result = Interop.Sys.GetHostEntriesForName(name, &entry);

            if (result != 0)
            {
                hostinfo        = NameResolutionUtilities.GetUnresolvedAnswer(name);
                nativeErrorCode = result;
                return(GetSocketErrorForNativeError(result));
            }

            try
            {
                string canonicalName = Marshal.PtrToStringAnsi((IntPtr)entry->CanonicalName);

                hostinfo = new IPHostEntry
                {
                    HostName    = string.IsNullOrEmpty(canonicalName) ? name : canonicalName,
                    Aliases     = Array.Empty <string>(),
                    AddressList = new IPAddress[entry->Count]
                };

                // Clean this up when fixing #3570
                var buffer = new byte[SocketAddressPal.IPv6AddressSize];
                for (int i = 0; i < entry->Count; i++)
                {
                    SocketAddress sockaddr;
                    IPEndPoint    factory;
                    int           bufferLength;
                    if (entry->Addresses[i].IsIpv6)
                    {
                        sockaddr     = new SocketAddress(AddressFamily.InterNetworkV6);
                        factory      = IPEndPointStatics.IPv6Any;
                        bufferLength = SocketAddressPal.IPv6AddressSize;

                        SocketAddressPal.SetAddressFamily(buffer, AddressFamily.InterNetworkV6);
                        SocketAddressPal.SetIPv6Address(buffer, entry->Addresses[i].Address, entry->Addresses[i].Count, 0);
                        SocketAddressPal.SetPort(buffer, 0);
                    }
                    else
                    {
                        sockaddr     = new SocketAddress(AddressFamily.InterNetwork);
                        factory      = IPEndPointStatics.Any;
                        bufferLength = SocketAddressPal.IPv4AddressSize;

                        SocketAddressPal.SetAddressFamily(buffer, AddressFamily.InterNetwork);
                        SocketAddressPal.SetIPv4Address(buffer, entry->Addresses[i].Address);
                        SocketAddressPal.SetPort(buffer, 0);
                    }

                    for (int d = 0; d < bufferLength; d++)
                    {
                        sockaddr[d] = buffer[d];
                    }

                    hostinfo.AddressList[i] = ((IPEndPoint)factory.Create(sockaddr)).Address;
                }
            }
            finally
            {
                Interop.Sys.FreeHostEntriesForName(entry);
            }

            nativeErrorCode = 0;
            return(SocketError.Success);
        }
        private static unsafe IPAddress CreateIPv4Address(ReadOnlySpan <byte> socketAddress)
        {
            long address = (long)SocketAddressPal.GetIPv4Address(socketAddress) & 0x0FFFFFFFF;

            return(new IPAddress(address));
        }
Ejemplo n.º 3
0
 internal SocketAddress(IPAddress ipaddress, int port)
     : this(ipaddress)
 {
     SocketAddressPal.SetPort(Buffer, unchecked ((ushort)port));
 }