Exemple #1
0
        /// <devdoc>
        ///    <para>
        ///       Converts the Internet address to either standard dotted quad format
        ///       or standard IPv6 representation.
        ///    </para>
        /// </devdoc>
        public override string ToString()
        {
            if (m_ToString == null)
            {
                //
                // IPv6 Changes: generate the IPV6 representation
                //
                if (m_Family == AddressFamily.InterNetworkV6)
                {
#if !FEATURE_PAL
                    int           addressStringLength = 256;
                    StringBuilder addressString       = new StringBuilder(addressStringLength);

                    if (Socket.OSSupportsIPv6)
                    {
                        SocketAddress saddr = new SocketAddress(AddressFamily.InterNetworkV6, SocketAddress.IPv6AddressSize);
                        int           j     = 8;
                        for (int i = 0; i < NumberOfLabels; i++)
                        {
                            saddr[j++] = (byte)(this.m_Numbers[i] >> 8);
                            saddr[j++] = (byte)(this.m_Numbers[i]);
                        }
                        if (m_ScopeId > 0)
                        {
                            saddr[24] = (byte)m_ScopeId;
                            saddr[25] = (byte)(m_ScopeId >> 8);
                            saddr[26] = (byte)(m_ScopeId >> 16);
                            saddr[27] = (byte)(m_ScopeId >> 24);
                        }
                        SocketError errorCode =
                            UnsafeNclNativeMethods.OSSOCK.WSAAddressToString(
                                saddr.m_Buffer,
                                saddr.m_Size,
                                IntPtr.Zero,
                                addressString,
                                ref addressStringLength);
                        if (errorCode != SocketError.Success)
                        {
                            throw new SocketException();
                        }
                    }
                    else
                    {
                        const string numberFormat = "{0:x4}:{1:x4}:{2:x4}:{3:x4}:{4:x4}:{5:x4}:{6}.{7}.{8}.{9}";
                        string       address      = String.Format(CultureInfo.InvariantCulture, numberFormat,
                                                                  m_Numbers[0], m_Numbers[1], m_Numbers[2], m_Numbers[3], m_Numbers[4], m_Numbers[5],
                                                                  ((m_Numbers[6] >> 8) & 0xFF), (m_Numbers[6] & 0xFF),
                                                                  ((m_Numbers[7] >> 8) & 0xFF), (m_Numbers[7] & 0xFF));
                        addressString.Append(address);

                        if (m_ScopeId != 0)
                        {
                            addressString.Append('%').Append((uint)m_ScopeId);
                        }
                    }

                    m_ToString = addressString.ToString();
#else // !FEATURE_PAL
#if MONO
                    var v6 = new IPv6AddressFormatter(m_Numbers, ScopeId);
                    m_ToString = v6.ToString();
#else
                    // IPv6 addresses not supported for FEATURE_PAL
                    throw new SocketException(SocketError.OperationNotSupported);
#endif
#endif // !FEATURE_PAL
                }
                else
                {
                    unsafe {
                        const int MaxSize       = 15;
                        int       offset        = MaxSize;
                        char *    addressString = stackalloc char[MaxSize];
                        int       number        = (int)((m_Address >> 24) & 0xFF);
                        do
                        {
                            addressString[--offset] = (char)('0' + number % 10);
                            number = number / 10;
                        } while (number > 0);
                        addressString[--offset] = '.';
                        number = (int)((m_Address >> 16) & 0xFF);
                        do
                        {
                            addressString[--offset] = (char)('0' + number % 10);
                            number = number / 10;
                        } while (number > 0);
                        addressString[--offset] = '.';
                        number = (int)((m_Address >> 8) & 0xFF);
                        do
                        {
                            addressString[--offset] = (char)('0' + number % 10);
                            number = number / 10;
                        } while (number > 0);
                        addressString[--offset] = '.';
                        number = (int)(m_Address & 0xFF);
                        do
                        {
                            addressString[--offset] = (char)('0' + number % 10);
                            number = number / 10;
                        } while (number > 0);
                        m_ToString = new string(addressString, offset, MaxSize - offset);
                    }
                }
            }
            return(m_ToString);
        }
Exemple #2
0
        /// <devdoc>
        ///    <para>
        ///       Converts the Internet address to either standard dotted quad format
        ///       or standard IPv6 representation.
        ///    </para>
        /// </devdoc>
        public override string ToString() {
            if (m_ToString==null) {
                //
                // IPv6 Changes: generate the IPV6 representation
                //
                if ( m_Family == AddressFamily.InterNetworkV6 ) {

#if !FEATURE_PAL
                    int addressStringLength = 256;
                    StringBuilder addressString = new StringBuilder(addressStringLength);

                    if(Socket.OSSupportsIPv6)
                    {
                        SocketAddress saddr = new SocketAddress(AddressFamily.InterNetworkV6, SocketAddress.IPv6AddressSize);
                        int j = 8;
                        for ( int i = 0; i < NumberOfLabels; i++) {
                            saddr[j++] = (byte)(this.m_Numbers[i] >> 8);
                            saddr[j++] = (byte)(this.m_Numbers[i]);
                        }
                        if (m_ScopeId >0) {
                            saddr[24] = (byte)m_ScopeId;
                            saddr[25] = (byte)(m_ScopeId >> 8);
                            saddr[26] = (byte)(m_ScopeId >> 16);
                            saddr[27] = (byte)(m_ScopeId >> 24);
                        }
                        SocketError errorCode =
                            UnsafeNclNativeMethods.OSSOCK.WSAAddressToString(
                                saddr.m_Buffer,
                                saddr.m_Size,
                                IntPtr.Zero,
                                addressString,
                                ref addressStringLength);
                        if (errorCode!=SocketError.Success) {
                            throw new SocketException();
                        }
                    }
                    else
                    {
                        const string numberFormat = "{0:x4}:{1:x4}:{2:x4}:{3:x4}:{4:x4}:{5:x4}:{6}.{7}.{8}.{9}";
                        string address = String.Format(CultureInfo.InvariantCulture, numberFormat, 
                            m_Numbers[0], m_Numbers[1], m_Numbers[2], m_Numbers[3], m_Numbers[4], m_Numbers[5], 
                            ((m_Numbers[6] >> 8) & 0xFF), (m_Numbers[6] & 0xFF), 
                            ((m_Numbers[7] >> 8) & 0xFF), (m_Numbers[7] & 0xFF));
                        addressString.Append(address);

                        if (m_ScopeId != 0)
                        {
                            addressString.Append('%').Append((uint)m_ScopeId);
                        }
                    }

                    m_ToString = addressString.ToString();
#else // !FEATURE_PAL

#if MONO
                    var v6 = new IPv6AddressFormatter (m_Numbers, ScopeId);
                    m_ToString = v6.ToString ();
#else
                    // IPv6 addresses not supported for FEATURE_PAL
                    throw new SocketException(SocketError.OperationNotSupported);
#endif
#endif // !FEATURE_PAL
                }
                else {
                    unsafe {
                        const int MaxSize = 15;
                        int offset = MaxSize;
                        char* addressString = stackalloc char[MaxSize];
                        int number = (int)((m_Address>>24)&0xFF);
                        do {
                            addressString[--offset] = (char)('0' + number%10);
                            number = number/10;
                        } while (number>0);
                        addressString[--offset] = '.';
                        number = (int)((m_Address>>16)&0xFF);
                        do {
                            addressString[--offset] = (char)('0' + number%10);
                            number = number/10;
                        } while (number>0);
                        addressString[--offset] = '.';
                        number = (int)((m_Address>>8)&0xFF);
                        do {
                            addressString[--offset] = (char)('0' + number%10);
                            number = number/10;
                        } while (number>0);
                        addressString[--offset] = '.';
                        number = (int)(m_Address&0xFF);
                        do {
                            addressString[--offset] = (char)('0' + number%10);
                            number = number/10;
                        } while (number>0);
                        m_ToString = new string(addressString, offset, MaxSize-offset);
                    }
                }
            }
            return m_ToString;
        }