Exemplo n.º 1
0
        internal SocketAddress(IPAddress ipAddress)
            : this(ipAddress.AddressFamily,
                   ((ipAddress.AddressFamily == AddressFamily.InterNetwork) ? IPv4AddressSize : IPv6AddressSize))
        {
            // No Port
            SocketAddressPal.SetPort(Buffer, 0);

            if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                SocketAddressPal.SetIPv6Address(Buffer, ipAddress.GetAddressBytes(), (uint)ipAddress.ScopeId);
            }
            else
            {
                Debug.Assert(ipAddress.AddressFamily == AddressFamily.InterNetwork);
                SocketAddressPal.SetIPv4Address(Buffer, (uint)ipAddress.Address);
            }
        }
Exemplo n.º 2
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);
        }