private unsafe static IPAddress InternalParse(string ipString, bool tryParse) { if (ipString == null) { if (tryParse) { return null; } throw new ArgumentNullException("ipString"); } else { if (ipString.IndexOf(':') != -1) { SocketException innerException; if (Socket.OSSupportsIPv6) { byte[] array = new byte[16]; SocketAddress socketAddress = new SocketAddress(AddressFamily.InterNetworkV6, 28); if (OSSOCK.WSAStringToAddress(ipString, AddressFamily.InterNetworkV6, IntPtr.Zero, socketAddress.m_Buffer, ref socketAddress.m_Size) == SocketError.Success) { for (int i = 0; i < 16; i++) { array[i] = socketAddress[i + 8]; } long scopeid = (long)(((int)socketAddress[27] << 24) + ((int)socketAddress[26] << 16) + ((int)socketAddress[25] << 8) + (int)socketAddress[24]); return new IPAddress(array, scopeid); } if (tryParse) { return null; } innerException = new SocketException(); } else { int start = 0; if (ipString[0] != '[') { ipString += ']'; } else { start = 1; } int length = ipString.Length; fixed (char* name = ipString) { if (IPv6AddressHelper.IsValidStrict(name, start, ref length) || length != ipString.Length) { ushort[] array2 = new ushort[8]; string text = null; fixed (ushort* ptr = array2) { IPv6AddressHelper.Parse(ipString, ptr, 0, ref text); } IPAddress result; if (text == null || text.Length == 0) { result = new IPAddress(array2, 0u); } else { text = text.Substring(1); uint scopeid2; if (!uint.TryParse(text, NumberStyles.None, null, out scopeid2)) { goto IL_193; } result = new IPAddress(array2, scopeid2); } return result; } IL_193: ; } if (tryParse) { return null; } innerException = new SocketException(SocketError.InvalidArgument); } throw new FormatException("dns_bad_ip_address" + innerException); } Socket.InitializeSockets(); int length2 = ipString.Length; long num; fixed (char* name2 = ipString) { num = IPv4AddressHelper.ParseNonCanonical(name2, 0, ref length2, true); } if (num != -1L && length2 == ipString.Length) { num = ((num & 255L) << 24 | ((num & 65280L) << 8 | ((num & 16711680L) >> 8 | (num & 1095216660480) >> 24))); return new IPAddress(num); } if (tryParse) { return null; } throw new FormatException("dns_bad_ip_address"); } }
/// <summary>Converts an Internet address to its standard notation.</summary> /// <returns>A string that contains the IP address in either IPv4 dotted-quad or in IPv6 colon-hexadecimal notation.</returns> /// <PermissionSet> /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> /// </PermissionSet> public unsafe override string ToString() { if (this.m_ToString == null) { if (this.m_Family == AddressFamily.InterNetworkV6) { int capacity = 256; StringBuilder stringBuilder = new StringBuilder(capacity); if (Socket.OSSupportsIPv6) { SocketAddress socketAddress = new SocketAddress(AddressFamily.InterNetworkV6, 28); int num = 8; for (int i = 0; i < 8; i++) { socketAddress[num++] = (byte)(this.m_Numbers[i] >> 8); socketAddress[num++] = (byte)this.m_Numbers[i]; } if (this.m_ScopeId > 0L) { socketAddress[24] = (byte)this.m_ScopeId; socketAddress[25] = (byte)(this.m_ScopeId >> 8); socketAddress[26] = (byte)(this.m_ScopeId >> 16); socketAddress[27] = (byte)(this.m_ScopeId >> 24); } SocketError socketError = OSSOCK.WSAAddressToString(socketAddress.m_Buffer, socketAddress.m_Size, IntPtr.Zero, stringBuilder, ref capacity); if (socketError != SocketError.Success) { throw new SocketException(); } } else { string value = string.Format(CultureInfo.InvariantCulture, "{0:x4}:{1:x4}:{2:x4}:{3:x4}:{4:x4}:{5:x4}:{6}.{7}.{8}.{9}", new object[] { this.m_Numbers[0], this.m_Numbers[1], this.m_Numbers[2], this.m_Numbers[3], this.m_Numbers[4], this.m_Numbers[5], this.m_Numbers[6] >> 8 & 255, (int)(this.m_Numbers[6] & 255), this.m_Numbers[7] >> 8 & 255, (int)(this.m_Numbers[7] & 255) }); stringBuilder.Append(value); if (this.m_ScopeId != 0L) { stringBuilder.Append('%').Append((uint)this.m_ScopeId); } } this.m_ToString = stringBuilder.ToString(); } else { int num2 = 15; char* ptr = stackalloc char[15]; int num3 = (int)(this.m_Address >> 24 & 255L); do { ptr[(--num2)] = (char)(48 + num3 % 10); num3 /= 10; } while (num3 > 0); ptr[(--num2)] = '.'; num3 = (int)(this.m_Address >> 16 & 255L); do { ptr[(--num2)] = (char)(48 + num3 % 10); num3 /= 10; } while (num3 > 0); ptr[(--num2)] = '.'; num3 = (int)(this.m_Address >> 8 & 255L); do { ptr[(--num2)] = (char)(48 + num3 % 10); num3 /= 10; } while (num3 > 0); ptr[(--num2)] = '.'; num3 = (int)(this.m_Address & 255L); do { ptr[(--num2)] = (char)(48 + num3 % 10); num3 /= 10; } while (num3 > 0); this.m_ToString = new string(ptr, num2, 15 - num2); } } return this.m_ToString; }
/// <summary>Creates an <see cref="T:System.Net.EndPoint" /> instance from a <see cref="T:System.Net.SocketAddress" /> instance.</summary> /// <returns>A new <see cref="T:System.Net.EndPoint" /> instance that is initialized from the specified <see cref="T:System.Net.SocketAddress" /> instance.</returns> /// <param name="socketAddress">The socket address that serves as the endpoint for a connection. </param> /// <exception cref="T:System.NotImplementedException">Any attempt is made to access the method when the method is not overridden in a descendant class. </exception> /// <PermissionSet> /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" /> /// </PermissionSet> public virtual EndPoint Create(SocketAddress socketAddress) { throw new NotImplementedException(); }