static IPAddress() { // Note: this type is marked as 'beforefieldinit'. byte[] address = new byte[16]; IPAddress.IPv6Any = new IPAddress(address, 0L); IPAddress.IPv6Loopback = new IPAddress(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 0L); byte[] address2 = new byte[16]; IPAddress.IPv6None = new IPAddress(address2, 0L); }
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>Indicates whether the specified IP address is the loopback address.</summary> /// <returns>true if <paramref name="address" /> is the loopback address; otherwise, false.</returns> /// <param name="address">An IP address. </param> public static bool IsLoopback(IPAddress address) { if (address == null) { throw new ArgumentNullException("address"); } if (address.m_Family == AddressFamily.InterNetworkV6) { return address.Equals(IPAddress.IPv6Loopback); } return (address.m_Address & 255L) == (IPAddress.Loopback.m_Address & 255L); }
/// <summary>Determines whether a string is a valid IP address.</summary> /// <returns>true if <paramref name="ipString" /> is a valid IP address; otherwise, false.</returns> /// <param name="ipString">The string to validate.</param> /// <param name="address">The <see cref="T:System.Net.IPAddress" /> version of the string.</param> public static bool TryParse(string ipString, out IPAddress address) { address = IPAddress.InternalParse(ipString, true); return address != null; }