| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers.Binary; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Net.Sockets; |
| | | 8 | | using System.Numerics; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using System.Runtime.InteropServices; |
| | | 11 | | using System.Runtime.Intrinsics; |
| | | 12 | | |
| | | 13 | | namespace System.Net |
| | | 14 | | { |
| | | 15 | | /// <devdoc> |
| | | 16 | | /// <para> |
| | | 17 | | /// Provides an Internet Protocol (IP) address. |
| | | 18 | | /// </para> |
| | | 19 | | /// </devdoc> |
| | | 20 | | public class IPAddress : ISpanFormattable, ISpanParsable<IPAddress>, IUtf8SpanFormattable, IUtf8SpanParsable<IPAddre |
| | | 21 | | { |
| | 0 | 22 | | public static readonly IPAddress Any = new ReadOnlyIPAddress([0, 0, 0, 0]); |
| | 0 | 23 | | public static readonly IPAddress Loopback = new ReadOnlyIPAddress([127, 0, 0, 1]); |
| | 0 | 24 | | public static readonly IPAddress Broadcast = new ReadOnlyIPAddress([255, 255, 255, 255]); |
| | 0 | 25 | | public static readonly IPAddress None = Broadcast; |
| | | 26 | | |
| | | 27 | | internal const uint LoopbackMaskHostOrder = 0xFF000000; |
| | | 28 | | |
| | 0 | 29 | | public static readonly IPAddress IPv6Any = new ReadOnlyIPAddress([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| | 0 | 30 | | public static readonly IPAddress IPv6Loopback = new ReadOnlyIPAddress([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| | 0 | 31 | | public static readonly IPAddress IPv6None = IPv6Any; |
| | | 32 | | |
| | 0 | 33 | | private static readonly IPAddress s_loopbackMappedToIPv6 = new ReadOnlyIPAddress([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// For IPv4 addresses, this field stores the Address. |
| | | 37 | | /// For IPv6 addresses, this field stores the ScopeId. |
| | | 38 | | /// Instead of accessing this field directly, use the <see cref="PrivateAddress"/> or <see cref="PrivateScopeId" |
| | | 39 | | /// </summary> |
| | | 40 | | private uint _addressOrScopeId; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// This field is only used for IPv6 addresses. A null value indicates that this instance is an IPv4 address. |
| | | 44 | | /// </summary> |
| | | 45 | | private readonly ushort[]? _numbers; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// A lazily initialized cache of the result of calling <see cref="ToString"/>. |
| | | 49 | | /// </summary> |
| | | 50 | | private string? _toString; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// A lazily initialized cache of the <see cref="GetHashCode"/> value. |
| | | 54 | | /// </summary> |
| | | 55 | | private int _hashCode; |
| | | 56 | | |
| | | 57 | | internal const int NumberOfLabels = IPAddressParserStatics.IPv6AddressBytes / 2; |
| | | 58 | | |
| | | 59 | | [MemberNotNullWhen(false, nameof(_numbers))] |
| | | 60 | | private bool IsIPv4 |
| | | 61 | | { |
| | 12129 | 62 | | get { return _numbers == null; } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | [MemberNotNullWhen(true, nameof(_numbers))] |
| | | 66 | | private bool IsIPv6 |
| | | 67 | | { |
| | 13650 | 68 | | get { return _numbers != null; } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | internal uint PrivateAddress |
| | | 72 | | { |
| | | 73 | | get |
| | 492 | 74 | | { |
| | 492 | 75 | | Debug.Assert(IsIPv4); |
| | 492 | 76 | | return _addressOrScopeId; |
| | 492 | 77 | | } |
| | | 78 | | private set |
| | 246 | 79 | | { |
| | 246 | 80 | | Debug.Assert(IsIPv4); |
| | 246 | 81 | | _toString = null; |
| | 246 | 82 | | _hashCode = 0; |
| | 246 | 83 | | _addressOrScopeId = value; |
| | 246 | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | internal uint PrivateIPv4Address |
| | | 88 | | { |
| | | 89 | | get |
| | 0 | 90 | | { |
| | 0 | 91 | | Debug.Assert(IsIPv4 || IsIPv4MappedToIPv6); |
| | 0 | 92 | | if (IsIPv4) |
| | 0 | 93 | | { |
| | 0 | 94 | | return _addressOrScopeId; |
| | | 95 | | } |
| | 0 | 96 | | uint address = (uint)_numbers[6] << 16 | (uint)_numbers[7]; |
| | 0 | 97 | | return (uint)HostToNetworkOrder(unchecked((int)address)); |
| | 0 | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | private uint PrivateScopeId |
| | | 102 | | { |
| | | 103 | | get |
| | 2152 | 104 | | { |
| | 2152 | 105 | | Debug.Assert(IsIPv6); |
| | 2152 | 106 | | return _addressOrScopeId; |
| | 2152 | 107 | | } |
| | | 108 | | set |
| | 1076 | 109 | | { |
| | 1076 | 110 | | Debug.Assert(IsIPv6); |
| | 1076 | 111 | | _toString = null; |
| | 1076 | 112 | | _hashCode = 0; |
| | 1076 | 113 | | _addressOrScopeId = value; |
| | 1076 | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <devdoc> |
| | | 118 | | /// <para> |
| | | 119 | | /// Initializes a new instance of the <see cref='System.Net.IPAddress'/> |
| | | 120 | | /// class with the specified address. |
| | | 121 | | /// </para> |
| | | 122 | | /// </devdoc> |
| | 246 | 123 | | public IPAddress(long newAddress) |
| | 246 | 124 | | { |
| | 246 | 125 | | ArgumentOutOfRangeException.ThrowIfGreaterThan((ulong)newAddress, 0x00000000FFFFFFFF, nameof(newAddress)); |
| | | 126 | | |
| | 246 | 127 | | PrivateAddress = (uint)newAddress; |
| | 246 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <devdoc> |
| | | 131 | | /// <para> |
| | | 132 | | /// Constructor for an IPv6 Address with a specified Scope. |
| | | 133 | | /// </para> |
| | | 134 | | /// </devdoc> |
| | | 135 | | public IPAddress(byte[] address, long scopeid) : |
| | 0 | 136 | | this(new ReadOnlySpan<byte>(address ?? ThrowAddressNullException()), scopeid) |
| | 0 | 137 | | { |
| | 0 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | public IPAddress(ReadOnlySpan<byte> address, long scopeid) |
| | 0 | 141 | | { |
| | 0 | 142 | | if (address.Length != IPAddressParserStatics.IPv6AddressBytes) |
| | 0 | 143 | | { |
| | 0 | 144 | | throw new ArgumentException(SR.dns_bad_ip_address, nameof(address)); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | // Consider: Since scope is only valid for link-local and site-local |
| | | 148 | | // addresses we could implement some more robust checking here |
| | 0 | 149 | | ArgumentOutOfRangeException.ThrowIfGreaterThan((ulong)scopeid, 0x00000000FFFFFFFF, nameof(scopeid)); |
| | | 150 | | |
| | 0 | 151 | | _numbers = ReadUInt16NumbersFromBytes(address); |
| | 0 | 152 | | PrivateScopeId = (uint)scopeid; |
| | 0 | 153 | | } |
| | | 154 | | |
| | 1076 | 155 | | internal IPAddress(ReadOnlySpan<ushort> numbers, uint scopeid) |
| | 1076 | 156 | | { |
| | 1076 | 157 | | Debug.Assert(numbers.Length == NumberOfLabels); |
| | | 158 | | |
| | 1076 | 159 | | _numbers = numbers.ToArray(); |
| | 1076 | 160 | | PrivateScopeId = scopeid; |
| | 1076 | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | private IPAddress(ushort[] numbers, uint scopeid) |
| | 0 | 164 | | { |
| | 0 | 165 | | Debug.Assert(numbers != null); |
| | 0 | 166 | | Debug.Assert(numbers.Length == NumberOfLabels); |
| | | 167 | | |
| | 0 | 168 | | _numbers = numbers; |
| | 0 | 169 | | PrivateScopeId = scopeid; |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <devdoc> |
| | | 173 | | /// <para> |
| | | 174 | | /// Constructor for IPv4 and IPv6 Address. |
| | | 175 | | /// </para> |
| | | 176 | | /// </devdoc> |
| | | 177 | | public IPAddress(byte[] address) : |
| | 0 | 178 | | this(new ReadOnlySpan<byte>(address ?? ThrowAddressNullException())) |
| | 0 | 179 | | { |
| | 0 | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | public IPAddress(ReadOnlySpan<byte> address) |
| | 0 | 183 | | { |
| | 0 | 184 | | if (address.Length == IPAddressParserStatics.IPv4AddressBytes) |
| | 0 | 185 | | { |
| | 0 | 186 | | PrivateAddress = BitConverter.ToUInt32(address); |
| | 0 | 187 | | } |
| | 0 | 188 | | else if (address.Length == IPAddressParserStatics.IPv6AddressBytes) |
| | 0 | 189 | | { |
| | 0 | 190 | | _numbers = ReadUInt16NumbersFromBytes(address); |
| | 0 | 191 | | } |
| | | 192 | | else |
| | 0 | 193 | | { |
| | 0 | 194 | | throw new ArgumentException(SR.dns_bad_ip_address, nameof(address)); |
| | | 195 | | } |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 199 | | private static ushort[] ReadUInt16NumbersFromBytes(ReadOnlySpan<byte> address) |
| | 0 | 200 | | { |
| | 0 | 201 | | ushort[] numbers = new ushort[NumberOfLabels]; |
| | 0 | 202 | | if (Vector128.IsHardwareAccelerated && BitConverter.IsLittleEndian) |
| | 0 | 203 | | { |
| | 0 | 204 | | Vector128<ushort> ushorts = Vector128.Create(address).AsUInt16(); |
| | | 205 | | // Reverse endianness of each ushort |
| | 0 | 206 | | ushorts = (ushorts << 8) | (ushorts >> 8); |
| | 0 | 207 | | ushorts.CopyTo(numbers); |
| | 0 | 208 | | } |
| | | 209 | | else |
| | 0 | 210 | | { |
| | 0 | 211 | | for (int i = 0; i < numbers.Length; i++) |
| | 0 | 212 | | { |
| | 0 | 213 | | numbers[i] = BinaryPrimitives.ReadUInt16BigEndian(address.Slice(i * 2)); |
| | 0 | 214 | | } |
| | 0 | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | return numbers; |
| | 0 | 218 | | } |
| | | 219 | | |
| | | 220 | | // We need this internally since we need to interface with winsock, |
| | | 221 | | // and winsock only understands Int32. |
| | | 222 | | internal IPAddress(int newAddress) |
| | | 223 | | { |
| | | 224 | | PrivateAddress = (uint)newAddress; |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary>Determines whether the provided span contains a valid <see cref="IPAddress"/>.</summary> |
| | | 228 | | /// <param name="ipSpan">The text to parse.</param> |
| | 2757 | 229 | | public static bool IsValid(ReadOnlySpan<char> ipSpan) => IPAddressParser.IsValid(ipSpan); |
| | | 230 | | |
| | | 231 | | /// <summary>Determines whether the provided span contains a valid <see cref="IPAddress"/>.</summary> |
| | | 232 | | /// <param name="utf8Text">The text to parse.</param> |
| | | 233 | | /// <returns><see langword="true"/> if <paramref name="utf8Text"/> contains a valid IP address; otherwise, <see |
| | 1435 | 234 | | public static bool IsValidUtf8(ReadOnlySpan<byte> utf8Text) => IPAddressParser.IsValid(utf8Text); |
| | | 235 | | |
| | | 236 | | /// <devdoc> |
| | | 237 | | /// <para> |
| | | 238 | | /// Converts an IP address string to an <see cref='System.Net.IPAddress'/> instance. |
| | | 239 | | /// </para> |
| | | 240 | | /// </devdoc> |
| | | 241 | | public static bool TryParse([NotNullWhen(true)] string? ipString, [NotNullWhen(true)] out IPAddress? address) |
| | 661 | 242 | | { |
| | 661 | 243 | | if (ipString == null) |
| | 0 | 244 | | { |
| | 0 | 245 | | address = null; |
| | 0 | 246 | | return false; |
| | | 247 | | } |
| | | 248 | | |
| | 661 | 249 | | address = IPAddressParser.Parse(ipString.AsSpan(), tryParse: true); |
| | 661 | 250 | | return (address != null); |
| | 661 | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Tries to parse a span of UTF-8 characters into a value. |
| | | 255 | | /// </summary> |
| | | 256 | | /// <param name="utf8Text">The span of UTF-8 characters to parse.</param> |
| | | 257 | | /// <param name="result">On return, contains the result of successfully parsing <paramref name="utf8Text"/> or a |
| | | 258 | | /// <returns><c>true</c> if <paramref name="utf8Text"/> was successfully parsed; otherwise, <c>false</c>.</retur |
| | | 259 | | public static bool TryParse(ReadOnlySpan<byte> utf8Text, [NotNullWhen(true)] out IPAddress? result) |
| | 1445 | 260 | | { |
| | 1445 | 261 | | result = IPAddressParser.Parse(utf8Text, tryParse: true); |
| | 1445 | 262 | | return (result != null); |
| | 1445 | 263 | | } |
| | | 264 | | |
| | | 265 | | public static bool TryParse(ReadOnlySpan<char> ipSpan, [NotNullWhen(true)] out IPAddress? address) |
| | 1425 | 266 | | { |
| | 1425 | 267 | | address = IPAddressParser.Parse(ipSpan, tryParse: true); |
| | 1425 | 268 | | return (address != null); |
| | 1425 | 269 | | } |
| | | 270 | | |
| | | 271 | | /// <inheritdoc/> |
| | | 272 | | static bool IUtf8SpanParsable<IPAddress>.TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, [NotNu |
| | 0 | 273 | | TryParse(utf8Text, out result); |
| | | 274 | | |
| | | 275 | | /// <inheritdoc/> |
| | | 276 | | static bool IParsable<IPAddress>.TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [NotNullWhen |
| | | 277 | | // provider is explicitly ignored |
| | 0 | 278 | | TryParse(s, out result); |
| | | 279 | | |
| | | 280 | | /// <inheritdoc/> |
| | | 281 | | static bool ISpanParsable<IPAddress>.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, [NotNullWhen(true |
| | | 282 | | // provider is explicitly ignored |
| | 0 | 283 | | TryParse(s, out result); |
| | | 284 | | |
| | | 285 | | public static IPAddress Parse(string ipString) |
| | 0 | 286 | | { |
| | 0 | 287 | | ArgumentNullException.ThrowIfNull(ipString); |
| | | 288 | | |
| | 0 | 289 | | return IPAddressParser.Parse(ipString.AsSpan(), tryParse: false)!; |
| | 0 | 290 | | } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Parses a span of UTF-8 characters into a value. |
| | | 294 | | /// </summary> |
| | | 295 | | /// <param name="utf8Text">The span of UTF-8 characters to parse.</param> |
| | | 296 | | /// <returns>The result of parsing <paramref name="utf8Text"/>.</returns> |
| | | 297 | | public static IPAddress Parse(ReadOnlySpan<byte> utf8Text) |
| | 0 | 298 | | { |
| | 0 | 299 | | return IPAddressParser.Parse(utf8Text, tryParse: false)!; |
| | 0 | 300 | | } |
| | | 301 | | |
| | | 302 | | public static IPAddress Parse(ReadOnlySpan<char> ipSpan) |
| | 0 | 303 | | { |
| | 0 | 304 | | return IPAddressParser.Parse(ipSpan, tryParse: false)!; |
| | 0 | 305 | | } |
| | | 306 | | |
| | | 307 | | /// <inheritdoc/> |
| | | 308 | | static IPAddress IUtf8SpanParsable<IPAddress>.Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider) => |
| | | 309 | | // provider is explicitly ignored |
| | 0 | 310 | | Parse(utf8Text); |
| | | 311 | | |
| | | 312 | | /// <inheritdoc/> |
| | | 313 | | static IPAddress ISpanParsable<IPAddress>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider) => |
| | | 314 | | // provider is explicitly ignored |
| | 0 | 315 | | Parse(s); |
| | | 316 | | |
| | | 317 | | /// <inheritdoc/> |
| | | 318 | | static IPAddress IParsable<IPAddress>.Parse(string s, IFormatProvider? provider) => |
| | | 319 | | // provider is explicitly ignored |
| | 0 | 320 | | Parse(s); |
| | | 321 | | |
| | | 322 | | public bool TryWriteBytes(Span<byte> destination, out int bytesWritten) |
| | 0 | 323 | | { |
| | 0 | 324 | | if (IsIPv6) |
| | 0 | 325 | | { |
| | 0 | 326 | | if (destination.Length < IPAddressParserStatics.IPv6AddressBytes) |
| | 0 | 327 | | { |
| | 0 | 328 | | bytesWritten = 0; |
| | 0 | 329 | | return false; |
| | | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | WriteIPv6Bytes(destination); |
| | 0 | 333 | | bytesWritten = IPAddressParserStatics.IPv6AddressBytes; |
| | 0 | 334 | | } |
| | | 335 | | else |
| | 0 | 336 | | { |
| | 0 | 337 | | if (destination.Length < IPAddressParserStatics.IPv4AddressBytes) |
| | 0 | 338 | | { |
| | 0 | 339 | | bytesWritten = 0; |
| | 0 | 340 | | return false; |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | WriteIPv4Bytes(destination); |
| | 0 | 344 | | bytesWritten = IPAddressParserStatics.IPv4AddressBytes; |
| | 0 | 345 | | } |
| | | 346 | | |
| | 0 | 347 | | return true; |
| | 0 | 348 | | } |
| | | 349 | | |
| | | 350 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 351 | | private void WriteIPv6Bytes(Span<byte> destination) |
| | 0 | 352 | | { |
| | 0 | 353 | | ushort[]? numbers = _numbers; |
| | 0 | 354 | | Debug.Assert(numbers is { Length: NumberOfLabels }); |
| | | 355 | | |
| | 0 | 356 | | if (BitConverter.IsLittleEndian) |
| | 0 | 357 | | { |
| | 0 | 358 | | if (Vector128.IsHardwareAccelerated) |
| | 0 | 359 | | { |
| | 0 | 360 | | Vector128<ushort> ushorts = Vector128.Create(numbers).AsUInt16(); |
| | 0 | 361 | | ushorts = (ushorts << 8) | (ushorts >> 8); |
| | 0 | 362 | | ushorts.AsByte().CopyTo(destination); |
| | 0 | 363 | | } |
| | | 364 | | else |
| | 0 | 365 | | { |
| | 0 | 366 | | for (int i = 0; i < numbers.Length; i++) |
| | 0 | 367 | | { |
| | 0 | 368 | | BinaryPrimitives.WriteUInt16BigEndian(destination.Slice(i * 2), numbers[i]); |
| | 0 | 369 | | } |
| | 0 | 370 | | } |
| | 0 | 371 | | } |
| | | 372 | | else |
| | 0 | 373 | | { |
| | 0 | 374 | | MemoryMarshal.AsBytes<ushort>(numbers).CopyTo(destination); |
| | 0 | 375 | | } |
| | 0 | 376 | | } |
| | | 377 | | |
| | | 378 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 379 | | private void WriteIPv4Bytes(Span<byte> destination) |
| | 0 | 380 | | { |
| | 0 | 381 | | uint address = PrivateAddress; |
| | 0 | 382 | | MemoryMarshal.Write(destination, in address); |
| | 0 | 383 | | } |
| | | 384 | | |
| | | 385 | | /// <devdoc> |
| | | 386 | | /// <para> |
| | | 387 | | /// Provides a copy of the IPAddress internals as an array of bytes. |
| | | 388 | | /// </para> |
| | | 389 | | /// </devdoc> |
| | | 390 | | public byte[] GetAddressBytes() |
| | 0 | 391 | | { |
| | 0 | 392 | | if (IsIPv6) |
| | 0 | 393 | | { |
| | 0 | 394 | | Debug.Assert(_numbers is { Length: NumberOfLabels }); |
| | 0 | 395 | | byte[] bytes = new byte[IPAddressParserStatics.IPv6AddressBytes]; |
| | 0 | 396 | | WriteIPv6Bytes(bytes); |
| | 0 | 397 | | return bytes; |
| | | 398 | | } |
| | | 399 | | else |
| | 0 | 400 | | { |
| | 0 | 401 | | byte[] bytes = new byte[IPAddressParserStatics.IPv4AddressBytes]; |
| | 0 | 402 | | WriteIPv4Bytes(bytes); |
| | 0 | 403 | | return bytes; |
| | | 404 | | } |
| | 0 | 405 | | } |
| | | 406 | | |
| | | 407 | | public AddressFamily AddressFamily |
| | | 408 | | { |
| | | 409 | | get |
| | 2644 | 410 | | { |
| | 2644 | 411 | | return IsIPv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6; |
| | 2644 | 412 | | } |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <devdoc> |
| | | 416 | | /// <para> |
| | | 417 | | /// IPv6 Scope identifier. This is really a uint32, but that isn't CLS compliant |
| | | 418 | | /// </para> |
| | | 419 | | /// </devdoc> |
| | | 420 | | public long ScopeId |
| | | 421 | | { |
| | | 422 | | get |
| | 0 | 423 | | { |
| | | 424 | | // Not valid for IPv4 addresses |
| | 0 | 425 | | if (IsIPv4) |
| | 0 | 426 | | { |
| | 0 | 427 | | ThrowSocketOperationNotSupported(); |
| | | 428 | | } |
| | | 429 | | |
| | 0 | 430 | | return PrivateScopeId; |
| | 0 | 431 | | } |
| | | 432 | | set |
| | 0 | 433 | | { |
| | | 434 | | // Not valid for IPv4 addresses |
| | 0 | 435 | | if (IsIPv4) |
| | 0 | 436 | | { |
| | 0 | 437 | | ThrowSocketOperationNotSupported(); |
| | | 438 | | } |
| | | 439 | | |
| | 0 | 440 | | if (this is ReadOnlyIPAddress) |
| | 0 | 441 | | { |
| | 0 | 442 | | ThrowSocketOperationNotSupportedReadOnly(); |
| | | 443 | | } |
| | | 444 | | |
| | | 445 | | // Consider: Since scope is only valid for link-local and site-local |
| | | 446 | | // addresses we could implement some more robust checking here |
| | 0 | 447 | | ArgumentOutOfRangeException.ThrowIfNegative(value); |
| | 0 | 448 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(value, 0x00000000FFFFFFFF); |
| | | 449 | | |
| | 0 | 450 | | PrivateScopeId = (uint)value; |
| | 0 | 451 | | } |
| | | 452 | | } |
| | | 453 | | |
| | | 454 | | /// <devdoc> |
| | | 455 | | /// <para> |
| | | 456 | | /// Converts the Internet address to either standard dotted quad format |
| | | 457 | | /// or standard IPv6 representation. |
| | | 458 | | /// </para> |
| | | 459 | | /// </devdoc> |
| | | 460 | | public override unsafe string ToString() |
| | 661 | 461 | | { |
| | 661 | 462 | | string? toString = _toString; |
| | 661 | 463 | | if (toString is null) |
| | 661 | 464 | | { |
| | 661 | 465 | | Span<char> span = stackalloc char[IPAddressParser.MaxIPv6StringLength]; |
| | 661 | 466 | | int length = IsIPv4 ? |
| | 661 | 467 | | IPAddressParser.FormatIPv4Address(_addressOrScopeId, span) : |
| | 661 | 468 | | IPAddressParser.FormatIPv6Address(_numbers, _addressOrScopeId, span); |
| | 661 | 469 | | _toString = toString = new string(span.Slice(0, length)); |
| | 661 | 470 | | } |
| | | 471 | | |
| | 661 | 472 | | return toString; |
| | 661 | 473 | | } |
| | | 474 | | |
| | | 475 | | /// <inheritdoc/> |
| | | 476 | | string IFormattable.ToString(string? format, IFormatProvider? formatProvider) => |
| | | 477 | | // format and provider are explicitly ignored |
| | 0 | 478 | | ToString(); |
| | | 479 | | |
| | | 480 | | public bool TryFormat(Span<char> destination, out int charsWritten) => |
| | 0 | 481 | | TryFormatCore(destination, out charsWritten); |
| | | 482 | | |
| | | 483 | | /// <summary>Tries to format the current IP address into the provided span.</summary> |
| | | 484 | | /// <param name="utf8Destination">When this method returns, the IP address as a span of UTF-8 bytes.</param> |
| | | 485 | | /// <param name="bytesWritten">When this method returns, the number of bytes written into the <paramref name="ut |
| | | 486 | | /// <returns><see langword="true" /> if the formatting was successful; otherwise, <see langword="false" />.</ret |
| | | 487 | | public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten) => |
| | 0 | 488 | | TryFormatCore(utf8Destination, out bytesWritten); |
| | | 489 | | |
| | | 490 | | /// <inheritdoc/> |
| | | 491 | | bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormat |
| | | 492 | | // format and provider are explicitly ignored |
| | 0 | 493 | | TryFormatCore(destination, out charsWritten); |
| | | 494 | | |
| | | 495 | | /// <inheritdoc/> |
| | | 496 | | bool IUtf8SpanFormattable.TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, |
| | | 497 | | // format and provider are explicitly ignored |
| | 0 | 498 | | TryFormatCore(utf8Destination, out bytesWritten); |
| | | 499 | | |
| | | 500 | | private unsafe bool TryFormatCore<TChar>(Span<TChar> destination, out int charsWritten) where TChar : unmanaged, |
| | 0 | 501 | | { |
| | 0 | 502 | | if (IsIPv4) |
| | 0 | 503 | | { |
| | 0 | 504 | | if (destination.Length >= IPAddressParser.MaxIPv4StringLength) |
| | 0 | 505 | | { |
| | 0 | 506 | | charsWritten = IPAddressParser.FormatIPv4Address(_addressOrScopeId, destination); |
| | 0 | 507 | | return true; |
| | | 508 | | } |
| | 0 | 509 | | } |
| | | 510 | | else |
| | 0 | 511 | | { |
| | 0 | 512 | | if (destination.Length >= IPAddressParser.MaxIPv6StringLength) |
| | 0 | 513 | | { |
| | 0 | 514 | | charsWritten = IPAddressParser.FormatIPv6Address(_numbers, _addressOrScopeId, destination); |
| | 0 | 515 | | return true; |
| | | 516 | | } |
| | 0 | 517 | | } |
| | | 518 | | |
| | 0 | 519 | | Span<TChar> tmpDestination = stackalloc TChar[IPAddressParser.MaxIPv6StringLength]; |
| | 0 | 520 | | Debug.Assert(tmpDestination.Length >= IPAddressParser.MaxIPv4StringLength); |
| | | 521 | | |
| | 0 | 522 | | int written = IsIPv4 ? |
| | 0 | 523 | | IPAddressParser.FormatIPv4Address(PrivateAddress, tmpDestination) : |
| | 0 | 524 | | IPAddressParser.FormatIPv6Address(_numbers, PrivateScopeId, tmpDestination); |
| | | 525 | | |
| | 0 | 526 | | if (tmpDestination.Slice(0, written).TryCopyTo(destination)) |
| | 0 | 527 | | { |
| | 0 | 528 | | charsWritten = written; |
| | 0 | 529 | | return true; |
| | | 530 | | } |
| | | 531 | | |
| | 0 | 532 | | charsWritten = 0; |
| | 0 | 533 | | return false; |
| | 0 | 534 | | } |
| | | 535 | | |
| | | 536 | | public static long HostToNetworkOrder(long host) |
| | 0 | 537 | | { |
| | 0 | 538 | | return BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(host) : host; |
| | 0 | 539 | | } |
| | | 540 | | |
| | | 541 | | public static int HostToNetworkOrder(int host) |
| | 539 | 542 | | { |
| | 539 | 543 | | return BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(host) : host; |
| | 539 | 544 | | } |
| | | 545 | | |
| | | 546 | | public static short HostToNetworkOrder(short host) |
| | 0 | 547 | | { |
| | 0 | 548 | | return BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(host) : host; |
| | 0 | 549 | | } |
| | | 550 | | |
| | | 551 | | public static long NetworkToHostOrder(long network) |
| | 0 | 552 | | { |
| | 0 | 553 | | return HostToNetworkOrder(network); |
| | 0 | 554 | | } |
| | | 555 | | |
| | | 556 | | public static int NetworkToHostOrder(int network) |
| | 208 | 557 | | { |
| | 208 | 558 | | return HostToNetworkOrder(network); |
| | 208 | 559 | | } |
| | | 560 | | |
| | | 561 | | public static short NetworkToHostOrder(short network) |
| | 0 | 562 | | { |
| | 0 | 563 | | return HostToNetworkOrder(network); |
| | 0 | 564 | | } |
| | | 565 | | |
| | | 566 | | public static bool IsLoopback(IPAddress address) |
| | 0 | 567 | | { |
| | 0 | 568 | | ArgumentNullException.ThrowIfNull(address); |
| | | 569 | | |
| | 0 | 570 | | if (address.IsIPv6) |
| | 0 | 571 | | { |
| | | 572 | | // Do Equals test for IPv6 addresses |
| | 0 | 573 | | return address.Equals(IPv6Loopback) || address.Equals(s_loopbackMappedToIPv6); |
| | | 574 | | } |
| | | 575 | | else |
| | 0 | 576 | | { |
| | 0 | 577 | | long LoopbackMask = (uint)HostToNetworkOrder(unchecked((int)LoopbackMaskHostOrder)); |
| | 0 | 578 | | return ((address.PrivateAddress & LoopbackMask) == (Loopback.PrivateAddress & LoopbackMask)); |
| | | 579 | | } |
| | 0 | 580 | | } |
| | | 581 | | |
| | | 582 | | /// <devdoc> |
| | | 583 | | /// <para> |
| | | 584 | | /// Determines if an address is an IPv6 Multicast address |
| | | 585 | | /// </para> |
| | | 586 | | /// </devdoc> |
| | | 587 | | public bool IsIPv6Multicast |
| | | 588 | | { |
| | | 589 | | get |
| | 0 | 590 | | { |
| | 0 | 591 | | return IsIPv6 && ((_numbers[0] & 0xFF00) == 0xFF00); |
| | 0 | 592 | | } |
| | | 593 | | } |
| | | 594 | | |
| | | 595 | | /// <devdoc> |
| | | 596 | | /// <para> |
| | | 597 | | /// Determines if an address is an IPv6 Link Local address |
| | | 598 | | /// </para> |
| | | 599 | | /// </devdoc> |
| | | 600 | | public bool IsIPv6LinkLocal |
| | | 601 | | { |
| | | 602 | | get |
| | 0 | 603 | | { |
| | 0 | 604 | | return IsIPv6 && ((_numbers[0] & 0xFFC0) == 0xFE80); |
| | 0 | 605 | | } |
| | | 606 | | } |
| | | 607 | | |
| | | 608 | | /// <devdoc> |
| | | 609 | | /// <para> |
| | | 610 | | /// Determines if an address is an IPv6 Site Local address |
| | | 611 | | /// </para> |
| | | 612 | | /// </devdoc> |
| | | 613 | | public bool IsIPv6SiteLocal |
| | | 614 | | { |
| | | 615 | | get |
| | 0 | 616 | | { |
| | 0 | 617 | | return IsIPv6 && ((_numbers[0] & 0xFFC0) == 0xFEC0); |
| | 0 | 618 | | } |
| | | 619 | | } |
| | | 620 | | |
| | | 621 | | public bool IsIPv6Teredo |
| | | 622 | | { |
| | | 623 | | get |
| | 0 | 624 | | { |
| | 0 | 625 | | return IsIPv6 && |
| | 0 | 626 | | (_numbers[0] == 0x2001) && |
| | 0 | 627 | | (_numbers[1] == 0); |
| | 0 | 628 | | } |
| | | 629 | | } |
| | | 630 | | |
| | | 631 | | /// <summary>Gets whether the address is an IPv6 Unique Local address.</summary> |
| | | 632 | | public bool IsIPv6UniqueLocal |
| | | 633 | | { |
| | | 634 | | get |
| | 0 | 635 | | { |
| | 0 | 636 | | return IsIPv6 && ((_numbers[0] & 0xFE00) == 0xFC00); |
| | 0 | 637 | | } |
| | | 638 | | } |
| | | 639 | | |
| | | 640 | | // 0:0:0:0:0:FFFF:x.x.x.x |
| | | 641 | | public bool IsIPv4MappedToIPv6 |
| | | 642 | | { |
| | | 643 | | get |
| | 0 | 644 | | { |
| | 0 | 645 | | return !IsIPv4 && _numbers.AsSpan(0, 6).SequenceEqual((ReadOnlySpan<ushort>)[0, 0, 0, 0, 0, 0xFFFF]); |
| | 0 | 646 | | } |
| | | 647 | | } |
| | | 648 | | |
| | | 649 | | [Obsolete("IPAddress.Address is address family dependent and has been deprecated. Use IPAddress.Equals to perfor |
| | | 650 | | public long Address |
| | | 651 | | { |
| | | 652 | | get |
| | 0 | 653 | | { |
| | 0 | 654 | | if (AddressFamily == AddressFamily.InterNetworkV6) |
| | 0 | 655 | | { |
| | 0 | 656 | | ThrowSocketOperationNotSupported(); |
| | | 657 | | } |
| | | 658 | | |
| | 0 | 659 | | return PrivateAddress; |
| | 0 | 660 | | } |
| | | 661 | | set |
| | 0 | 662 | | { |
| | 0 | 663 | | if (AddressFamily == AddressFamily.InterNetworkV6) |
| | 0 | 664 | | { |
| | 0 | 665 | | ThrowSocketOperationNotSupported(); |
| | | 666 | | } |
| | | 667 | | |
| | 0 | 668 | | if (PrivateAddress != value) |
| | 0 | 669 | | { |
| | 0 | 670 | | if (this is ReadOnlyIPAddress) |
| | 0 | 671 | | { |
| | 0 | 672 | | ThrowSocketOperationNotSupportedReadOnly(); |
| | | 673 | | } |
| | | 674 | | |
| | 0 | 675 | | PrivateAddress = unchecked((uint)value); |
| | 0 | 676 | | } |
| | 0 | 677 | | } |
| | | 678 | | } |
| | | 679 | | |
| | | 680 | | /// <summary>Compares two IP addresses.</summary> |
| | | 681 | | public override bool Equals([NotNullWhen(true)] object? comparand) |
| | 1322 | 682 | | { |
| | 1322 | 683 | | return comparand is IPAddress address && Equals(address); |
| | 1322 | 684 | | } |
| | | 685 | | |
| | | 686 | | internal bool Equals(IPAddress comparand) |
| | 1322 | 687 | | { |
| | 1322 | 688 | | Debug.Assert(comparand != null); |
| | | 689 | | |
| | | 690 | | // Compare families before address representations |
| | 1322 | 691 | | if (AddressFamily != comparand.AddressFamily) |
| | 0 | 692 | | { |
| | 0 | 693 | | return false; |
| | | 694 | | } |
| | | 695 | | |
| | 1322 | 696 | | if (IsIPv6) |
| | 1076 | 697 | | { |
| | | 698 | | // For IPv6 addresses, we must compare the full 128-bit representation and the scope IDs. |
| | | 699 | | // We give JIT a hint that the arrays are always of length IPv6AddressShorts, so it |
| | | 700 | | // can unroll the comparison. JIT probably could do it on its own, but that requires |
| | | 701 | | // complex inter-procedural optimizations. |
| | 1076 | 702 | | Debug.Assert(_numbers.Length == IPAddressParserStatics.IPv6AddressShorts); |
| | 1076 | 703 | | Debug.Assert(comparand._numbers!.Length == IPAddressParserStatics.IPv6AddressShorts); |
| | | 704 | | |
| | 1076 | 705 | | ReadOnlySpan<ushort> left = _numbers.AsSpan(0, IPAddressParserStatics.IPv6AddressShorts); |
| | 1076 | 706 | | ReadOnlySpan<ushort> right = comparand._numbers.AsSpan(0, IPAddressParserStatics.IPv6AddressShorts); |
| | 1076 | 707 | | return left.SequenceEqual(right) && PrivateScopeId == comparand.PrivateScopeId; |
| | | 708 | | } |
| | | 709 | | |
| | | 710 | | // For IPv4 addresses, compare the integer representation. |
| | 246 | 711 | | return comparand.PrivateAddress == PrivateAddress; |
| | 1322 | 712 | | } |
| | | 713 | | |
| | | 714 | | public override int GetHashCode() |
| | 0 | 715 | | { |
| | 0 | 716 | | if (_hashCode == 0) |
| | 0 | 717 | | { |
| | | 718 | | // For IPv4 addresses, we calculate the hashcode based on address bytes. |
| | | 719 | | // For IPv6 addresses, we also factor in scope ID. |
| | 0 | 720 | | if (IsIPv6) |
| | 0 | 721 | | { |
| | 0 | 722 | | ReadOnlySpan<byte> numbers = MemoryMarshal.AsBytes<ushort>(_numbers).Slice(0, 16); |
| | 0 | 723 | | _hashCode = HashCode.Combine( |
| | 0 | 724 | | BitConverter.ToUInt32(numbers), |
| | 0 | 725 | | BitConverter.ToUInt32(numbers.Slice(4)), |
| | 0 | 726 | | BitConverter.ToUInt32(numbers.Slice(8)), |
| | 0 | 727 | | BitConverter.ToUInt32(numbers.Slice(12)), |
| | 0 | 728 | | _addressOrScopeId); |
| | 0 | 729 | | } |
| | | 730 | | else |
| | 0 | 731 | | { |
| | 0 | 732 | | _hashCode = HashCode.Combine(_addressOrScopeId); |
| | 0 | 733 | | } |
| | 0 | 734 | | } |
| | | 735 | | |
| | 0 | 736 | | return _hashCode; |
| | 0 | 737 | | } |
| | | 738 | | |
| | | 739 | | // IPv4 192.168.1.1 maps as ::FFFF:192.168.1.1 |
| | | 740 | | public IPAddress MapToIPv6() |
| | 0 | 741 | | { |
| | 0 | 742 | | if (IsIPv6) |
| | 0 | 743 | | { |
| | 0 | 744 | | return this; |
| | | 745 | | } |
| | | 746 | | |
| | 0 | 747 | | uint address = (uint)NetworkToHostOrder(unchecked((int)PrivateAddress)); |
| | 0 | 748 | | ushort[] labels = new ushort[NumberOfLabels]; |
| | 0 | 749 | | labels[5] = 0xFFFF; |
| | 0 | 750 | | labels[6] = (ushort)(address >> 16); |
| | 0 | 751 | | labels[7] = (ushort)address; |
| | 0 | 752 | | return new IPAddress(labels, 0); |
| | 0 | 753 | | } |
| | | 754 | | |
| | | 755 | | // Takes the last 4 bytes of an IPv6 address and converts it to an IPv4 address. |
| | | 756 | | // This does not restrict to address with the ::FFFF: prefix because other types of |
| | | 757 | | // addresses display the tail segments as IPv4 like Terado. |
| | | 758 | | public IPAddress MapToIPv4() |
| | 0 | 759 | | { |
| | 0 | 760 | | if (IsIPv4) |
| | 0 | 761 | | { |
| | 0 | 762 | | return this; |
| | | 763 | | } |
| | | 764 | | |
| | 0 | 765 | | uint address = (uint)_numbers[6] << 16 | (uint)_numbers[7]; |
| | 0 | 766 | | return new IPAddress((uint)HostToNetworkOrder(unchecked((int)address))); |
| | 0 | 767 | | } |
| | | 768 | | |
| | | 769 | | [DoesNotReturn] |
| | 0 | 770 | | private static byte[] ThrowAddressNullException() => throw new ArgumentNullException("address"); |
| | | 771 | | |
| | | 772 | | [DoesNotReturn] |
| | 0 | 773 | | private void ThrowSocketOperationNotSupported() => throw new SocketException(SocketError.OperationNotSupported, |
| | | 774 | | |
| | | 775 | | [DoesNotReturn] |
| | 0 | 776 | | private static void ThrowSocketOperationNotSupportedReadOnly() => throw new SocketException(SocketError.Operatio |
| | | 777 | | |
| | | 778 | | private sealed class ReadOnlyIPAddress : IPAddress |
| | | 779 | | { |
| | 0 | 780 | | public ReadOnlyIPAddress(ReadOnlySpan<byte> newAddress) : base(newAddress) |
| | 0 | 781 | | { } |
| | | 782 | | |
| | 0 | 783 | | public ReadOnlyIPAddress(ReadOnlySpan<byte> address, long scopeid) : base(address, scopeid) |
| | 0 | 784 | | { } |
| | | 785 | | } |
| | | 786 | | } |
| | | 787 | | } |
| | | 788 | | |