예제 #1
0
        public static unsafe Interop.Sys.IPAddress GetNativeIPAddress(this IPAddress ipAddress)
        {
            var nativeIPAddress = default(Interop.Sys.IPAddress);

            ipAddress.TryWriteBytes(new Span <byte>(nativeIPAddress.Address, Interop.Sys.IPv6AddressBytes), out int bytesWritten);
            Debug.Assert(bytesWritten == sizeof(uint) || bytesWritten == Interop.Sys.IPv6AddressBytes, $"Unexpected length: {bytesWritten}");

            if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                nativeIPAddress.IsIPv6  = true;
                nativeIPAddress.ScopeId = (uint)ipAddress.ScopeId;
            }

            return(nativeIPAddress);
        }
예제 #2
0
        internal SocketAddress(IPAddress ipAddress)
            : this(ipAddress.AddressFamily,
                   ((ipAddress.AddressFamily == AddressFamily.InterNetwork) ? IPv4AddressSize : IPv6AddressSize))
        {
            // No Port
            m_Buffer[2] = (byte)0;
            m_Buffer[3] = (byte)0;

            if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                // No handling for Flow Information
                m_Buffer[4] = (byte)0;
                m_Buffer[5] = (byte)0;
                m_Buffer[6] = (byte)0;
                m_Buffer[7] = (byte)0;

                // Scope serialization
                long scope = ipAddress.ScopeId;
                m_Buffer[24] = (byte)scope;
                m_Buffer[25] = (byte)(scope >> 8);
                m_Buffer[26] = (byte)(scope >> 16);
                m_Buffer[27] = (byte)(scope >> 24);

                // Address serialization
                byte[] addressBytes = ipAddress.GetAddressBytes();
                for (int i = 0; i < addressBytes.Length; i++)
                {
                    m_Buffer[8 + i] = addressBytes[i];
                }
            }
            else
            {
                // IPv4 Address serialization
#if MONO
                ipAddress.TryWriteBytes(m_Buffer.AsSpan(4), out int bytesWritten);
#else
                m_Buffer[4] = unchecked ((byte)(ipAddress.m_Address));
                m_Buffer[5] = unchecked ((byte)(ipAddress.m_Address >> 8));
                m_Buffer[6] = unchecked ((byte)(ipAddress.m_Address >> 16));
                m_Buffer[7] = unchecked ((byte)(ipAddress.m_Address >> 24));
#endif
            }
        }
        public static unsafe string?TryGetNameInfo(IPAddress addr, out SocketError socketError, out int nativeErrorCode)
        {
            byte *buffer = stackalloc byte[Interop.Sys.NI_MAXHOST + 1 /*for null*/];

            byte isIPv6;
            int  rawAddressLength;

            if (addr.AddressFamily == AddressFamily.InterNetwork)
            {
                isIPv6           = 0;
                rawAddressLength = IPAddressParserStatics.IPv4AddressBytes;
            }
            else
            {
                isIPv6           = 1;
                rawAddressLength = IPAddressParserStatics.IPv6AddressBytes;
            }

            byte *rawAddress = stackalloc byte[rawAddressLength];

            addr.TryWriteBytes(new Span <byte>(rawAddress, rawAddressLength), out int bytesWritten);
            Debug.Assert(bytesWritten == rawAddressLength);

            int error = Interop.Sys.GetNameInfo(
                rawAddress,
                (uint)rawAddressLength,
                isIPv6,
                buffer,
                Interop.Sys.NI_MAXHOST,
                null,
                0,
                Interop.Sys.GetNameInfoFlags.NI_NAMEREQD);

            socketError     = GetSocketErrorForNativeError(error);
            nativeErrorCode = error;
            return(socketError == SocketError.Success ? Marshal.PtrToStringAnsi((IntPtr)buffer) : null);
        }