| | | 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.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.Net.Sockets; |
| | | 8 | | using System.Numerics; |
| | | 9 | | using System.Runtime.InteropServices; |
| | | 10 | | using System.Text.Unicode; |
| | | 11 | | |
| | | 12 | | namespace System.Net |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides an IP address. |
| | | 16 | | /// </summary> |
| | | 17 | | public class IPEndPoint : EndPoint, ISpanFormattable, ISpanParsable<IPEndPoint>, IUtf8SpanFormattable, IUtf8SpanPars |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Specifies the minimum acceptable value for the <see cref='System.Net.IPEndPoint.Port'/> property. |
| | | 21 | | /// </summary> |
| | | 22 | | public const int MinPort = 0x00000000; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Specifies the maximum acceptable value for the <see cref='System.Net.IPEndPoint.Port'/> property. |
| | | 26 | | /// </summary> |
| | | 27 | | public const int MaxPort = 0x0000FFFF; |
| | | 28 | | |
| | | 29 | | private IPAddress _address; |
| | | 30 | | private int _port; |
| | | 31 | | |
| | 0 | 32 | | public override AddressFamily AddressFamily => _address.AddressFamily; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Creates a new instance of the IPEndPoint class with the specified address and port. |
| | | 36 | | /// </summary> |
| | 0 | 37 | | public IPEndPoint(long address, int port) |
| | 0 | 38 | | { |
| | 0 | 39 | | if (!TcpValidationHelpers.ValidatePortNumber(port)) |
| | 0 | 40 | | { |
| | 0 | 41 | | throw new ArgumentOutOfRangeException(nameof(port)); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | _port = port; |
| | 0 | 45 | | _address = new IPAddress(address); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Creates a new instance of the IPEndPoint class with the specified address and port. |
| | | 50 | | /// </summary> |
| | 0 | 51 | | public IPEndPoint(IPAddress address, int port) |
| | 0 | 52 | | { |
| | 0 | 53 | | ArgumentNullException.ThrowIfNull(address); |
| | | 54 | | |
| | 0 | 55 | | if (!TcpValidationHelpers.ValidatePortNumber(port)) |
| | 0 | 56 | | { |
| | 0 | 57 | | throw new ArgumentOutOfRangeException(nameof(port)); |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | _port = port; |
| | 0 | 61 | | _address = address; |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets or sets the IP address. |
| | | 66 | | /// </summary> |
| | | 67 | | public IPAddress Address |
| | | 68 | | { |
| | 0 | 69 | | get => _address; |
| | | 70 | | set |
| | 0 | 71 | | { |
| | 0 | 72 | | ArgumentNullException.ThrowIfNull(value); |
| | 0 | 73 | | _address = value; |
| | 0 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets or sets the port. |
| | | 79 | | /// </summary> |
| | | 80 | | public int Port |
| | | 81 | | { |
| | 0 | 82 | | get => _port; |
| | | 83 | | set |
| | 0 | 84 | | { |
| | 0 | 85 | | if (!TcpValidationHelpers.ValidatePortNumber(value)) |
| | 0 | 86 | | { |
| | 0 | 87 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | _port = value; |
| | 0 | 91 | | } |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public static bool TryParse(string s, [NotNullWhen(true)] out IPEndPoint? result) |
| | 0 | 95 | | { |
| | 0 | 96 | | return TryParse(s.AsSpan(), out result); |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | internal static bool InternalTryParse<TChar>(ReadOnlySpan<TChar> s, [NotNullWhen(true)] out IPEndPoint? result) |
| | | 100 | | where TChar : unmanaged, IBinaryInteger<TChar> |
| | 0 | 101 | | { |
| | 0 | 102 | | Debug.Assert(typeof(TChar) == typeof(byte) || typeof(TChar) == typeof(char)); |
| | | 103 | | |
| | 0 | 104 | | int addressLength = s.Length; // If there's no port then send the entire string to the address parser |
| | 0 | 105 | | int lastColonPos = s.LastIndexOf(TChar.CreateTruncating(':')); |
| | | 106 | | |
| | | 107 | | // Look to see if this is an IPv6 address with a port. |
| | 0 | 108 | | if (lastColonPos > 0) |
| | 0 | 109 | | { |
| | 0 | 110 | | if (s[lastColonPos - 1] == TChar.CreateTruncating(']')) |
| | 0 | 111 | | { |
| | 0 | 112 | | addressLength = lastColonPos; |
| | 0 | 113 | | } |
| | | 114 | | // Look to see if this is IPv4 with a port (IPv6 will have another colon) |
| | 0 | 115 | | else if (s.Slice(0, lastColonPos).LastIndexOf(TChar.CreateTruncating(':')) == -1) |
| | 0 | 116 | | { |
| | 0 | 117 | | addressLength = lastColonPos; |
| | 0 | 118 | | } |
| | 0 | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | IPAddress? address = IPAddressParser.Parse(s.Slice(0, addressLength), true); |
| | 0 | 122 | | if (address is not null) |
| | 0 | 123 | | { |
| | 0 | 124 | | if (addressLength == s.Length) |
| | 0 | 125 | | { |
| | 0 | 126 | | result = new IPEndPoint(address, 0); |
| | 0 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | else |
| | 0 | 130 | | { |
| | | 131 | | uint port; |
| | 0 | 132 | | ReadOnlySpan<TChar> portSpan = s.Slice(addressLength + 1); |
| | | 133 | | bool isConvertedToInt; |
| | | 134 | | |
| | 0 | 135 | | if (typeof(TChar) == typeof(byte)) |
| | 0 | 136 | | { |
| | 0 | 137 | | isConvertedToInt = uint.TryParse(MemoryMarshal.Cast<TChar, byte>(portSpan), NumberStyles.None, C |
| | 0 | 138 | | } |
| | | 139 | | else |
| | 0 | 140 | | { |
| | 0 | 141 | | isConvertedToInt = uint.TryParse(MemoryMarshal.Cast<TChar, char>(portSpan), NumberStyles.None, C |
| | 0 | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | if (isConvertedToInt && port <= MaxPort) |
| | 0 | 145 | | { |
| | 0 | 146 | | result = new IPEndPoint(address, (int)port); |
| | 0 | 147 | | return true; |
| | | 148 | | } |
| | 0 | 149 | | } |
| | 0 | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | result = null; |
| | 0 | 153 | | return false; |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary>Tries to convert the character span to its <see cref="IPEndPoint"/> equivalent.</summary> |
| | | 157 | | /// <param name="s">A span container the characters representing the <see cref="IPEndPoint"/> to convert.</param |
| | | 158 | | /// <param name="result">When this method returns, contains the <see cref="IPEndPoint"/> value equivalent to wha |
| | | 159 | | /// <returns><c>true</c> if <paramref name="s" /> was converted successfully; otherwise, false.</returns> |
| | 0 | 160 | | public static bool TryParse(ReadOnlySpan<char> s, [NotNullWhen(true)] out IPEndPoint? result) => InternalTryPars |
| | | 161 | | |
| | | 162 | | public static IPEndPoint Parse(string s) |
| | 0 | 163 | | { |
| | 0 | 164 | | ArgumentNullException.ThrowIfNull(s); |
| | | 165 | | |
| | 0 | 166 | | return Parse(s.AsSpan()); |
| | 0 | 167 | | } |
| | | 168 | | |
| | | 169 | | public static IPEndPoint Parse(ReadOnlySpan<char> s) |
| | 0 | 170 | | { |
| | 0 | 171 | | if (TryParse(s, out IPEndPoint? result)) |
| | 0 | 172 | | { |
| | 0 | 173 | | return result; |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | throw new FormatException(SR.bad_endpoint_string); |
| | 0 | 177 | | } |
| | | 178 | | |
| | | 179 | | public override string ToString() => |
| | 0 | 180 | | _address.AddressFamily == AddressFamily.InterNetworkV6 ? |
| | 0 | 181 | | string.Create(NumberFormatInfo.InvariantInfo, $"[{_address}]:{_port}") : |
| | 0 | 182 | | string.Create(NumberFormatInfo.InvariantInfo, $"{_address}:{_port}"); |
| | | 183 | | |
| | 0 | 184 | | public override SocketAddress Serialize() => new SocketAddress(Address, Port); |
| | | 185 | | |
| | | 186 | | public override EndPoint Create(SocketAddress socketAddress) |
| | 0 | 187 | | { |
| | 0 | 188 | | ArgumentNullException.ThrowIfNull(socketAddress); |
| | | 189 | | |
| | 0 | 190 | | if (socketAddress.Family is not (AddressFamily.InterNetwork or AddressFamily.InterNetworkV6)) |
| | 0 | 191 | | { |
| | 0 | 192 | | throw new ArgumentException(SR.Format(SR.net_InvalidAddressFamily, socketAddress.Family.ToString(), GetT |
| | | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | int minSize = AddressFamily == AddressFamily.InterNetworkV6 ? SocketAddress.IPv6AddressSize : SocketAddress. |
| | 0 | 196 | | if (socketAddress.Size < minSize) |
| | 0 | 197 | | { |
| | 0 | 198 | | throw new ArgumentException(SR.Format(SR.net_InvalidSocketAddressSize, socketAddress.GetType().FullName, |
| | | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | return socketAddress.GetIPEndPoint(); |
| | 0 | 202 | | } |
| | | 203 | | |
| | | 204 | | public override bool Equals([NotNullWhen(true)] object? comparand) |
| | 0 | 205 | | { |
| | 0 | 206 | | return comparand is IPEndPoint other && other._address.Equals(_address) && other._port == _port; |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | public override int GetHashCode() |
| | 0 | 210 | | { |
| | 0 | 211 | | return _address.GetHashCode() ^ _port; |
| | 0 | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary>Converts the UTF-8 span to its <see cref="IPEndPoint"/> equivalent.</summary> |
| | | 215 | | /// <param name="utf8Text">A span containing the characters representing the <see cref="IPEndPoint"/> to convert |
| | | 216 | | /// <returns>contains the <see cref="IPEndPoint"/> value equivalent to what is contained in <paramref name="utf8 |
| | | 217 | | /// <exception cref="FormatException"><paramref name="utf8Text"/> is invalid</exception> |
| | | 218 | | public static IPEndPoint Parse(ReadOnlySpan<byte> utf8Text) |
| | 0 | 219 | | { |
| | 0 | 220 | | if (TryParse(utf8Text, out IPEndPoint? result)) |
| | 0 | 221 | | { |
| | 0 | 222 | | return result; |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | throw new FormatException(SR.bad_endpoint_string); |
| | 0 | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary>Converts the character span to its <see cref="IPEndPoint"/> equivalent.</summary> |
| | | 229 | | /// <param name="s">A span containing the characters representing the <see cref="IPEndPoint"/> to convert.</para |
| | | 230 | | /// <param name="provider">An object that provides culture-specific formatting information about <paramref name= |
| | | 231 | | /// <returns>contains the <see cref="IPEndPoint"/> value equivalent to what is contained in <paramref name="s" / |
| | | 232 | | /// <exception cref="FormatException"><paramref name="s"/> is invalid</exception> |
| | 0 | 233 | | static IPEndPoint ISpanParsable<IPEndPoint>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider) => Parse(s); |
| | | 234 | | |
| | | 235 | | /// <summary>Converts the string to its <see cref="IPEndPoint"/> equivalent.</summary> |
| | | 236 | | /// <param name="s">A string containing the characters representing the <see cref="IPEndPoint"/> to convert.</pa |
| | | 237 | | /// <param name="provider">An object that provides culture-specific formatting information about <paramref name= |
| | | 238 | | /// <returns>contains the <see cref="IPEndPoint"/> value equivalent to what is contained in <paramref name="s" / |
| | | 239 | | /// <exception cref="FormatException"><paramref name="s"/> is invalid</exception> |
| | 0 | 240 | | static IPEndPoint IParsable<IPEndPoint>.Parse(string s, IFormatProvider? provider) => Parse(s); |
| | | 241 | | |
| | | 242 | | /// <summary>Converts the UTF-8 span to its <see cref="IPEndPoint"/> equivalent.</summary> |
| | | 243 | | /// <param name="utf8Text">A Span containing the UTF-8 characters representing the <see cref="IPEndPoint"/> to c |
| | | 244 | | /// <param name="provider">An object that provides culture-specific formatting information about <paramref name= |
| | | 245 | | /// <returns>contains the <see cref="IPEndPoint"/> value equivalent to what is contained in <paramref name="utf8 |
| | | 246 | | /// <exception cref="FormatException"><paramref name="utf8Text"/> is invalid</exception> |
| | 0 | 247 | | static IPEndPoint IUtf8SpanParsable<IPEndPoint>.Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider) => |
| | | 248 | | |
| | | 249 | | /// <summary>Tries to convert the UTF-8 span to its <see cref="IPEndPoint"/> equivalent.</summary> |
| | | 250 | | /// <param name="utf8Text">A span containing the UTF-8 characters representing the <see cref="IPEndPoint"/> to c |
| | | 251 | | /// <param name="result">When this method returns, contains the <see cref="IPEndPoint"/> value equivalent to wha |
| | | 252 | | /// <returns><c>true</c> if <paramref name="utf8Text" /> was converted successfully; otherwise, false.</returns> |
| | 0 | 253 | | public static bool TryParse(ReadOnlySpan<byte> utf8Text, [NotNullWhen(true)] out IPEndPoint? result) => Internal |
| | | 254 | | |
| | | 255 | | /// <summary>Tries to convert the character span to its <see cref="IPEndPoint"/> equivalent.</summary> |
| | | 256 | | /// <param name="s">A span container the characters representing the <see cref="IPEndPoint"/> to convert.</param |
| | | 257 | | /// <param name="provider">An object that provides culture-specific formatting information about <paramref name= |
| | | 258 | | /// <param name="result">When this method returns, contains the <see cref="IPEndPoint"/> value equivalent to wha |
| | | 259 | | /// <returns><c>true</c> if <paramref name="s" /> was converted successfully; otherwise, false.</returns> |
| | 0 | 260 | | static bool ISpanParsable<IPEndPoint>.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, [NotNullWhen(tru |
| | | 261 | | |
| | | 262 | | /// <summary>Tries to convert the string to its <see cref="IPEndPoint"/> equivalent.</summary> |
| | | 263 | | /// <param name="s">A string representing the <see cref="IPEndPoint"/> to convert.</param> |
| | | 264 | | /// <param name="provider">An object that provides culture-specific formatting information about <paramref name= |
| | | 265 | | /// <param name="result">When this method returns, contains the <see cref="IPEndPoint"/> value equivalent to wha |
| | | 266 | | /// <returns><c>true</c> if <paramref name="s" /> was converted successfully; otherwise, false.</returns> |
| | | 267 | | static bool IParsable<IPEndPoint>.TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [NotNullWhe |
| | 0 | 268 | | { |
| | 0 | 269 | | if (s is null) |
| | 0 | 270 | | { |
| | 0 | 271 | | result = default; |
| | 0 | 272 | | return false; |
| | | 273 | | } |
| | | 274 | | |
| | 0 | 275 | | return TryParse(s, out result); |
| | 0 | 276 | | } |
| | | 277 | | |
| | | 278 | | /// <summary>Tries to convert the UTF-8 span to its <see cref="IPEndPoint"/> equivalent.</summary> |
| | | 279 | | /// <param name="utf8Text">A span container the characters representing the <see cref="IPEndPoint"/> to convert. |
| | | 280 | | /// <param name="provider">An object that provides culture-specific formatting information about <paramref name= |
| | | 281 | | /// <param name="result">When this method returns, contains the <see cref="IPEndPoint"/> value equivalent to wha |
| | | 282 | | /// <returns><c>true</c> if <paramref name="utf8Text" /> was converted successfully; otherwise, false.</returns> |
| | 0 | 283 | | static bool IUtf8SpanParsable<IPEndPoint>.TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, [NotN |
| | | 284 | | |
| | | 285 | | /// <summary>Returns the string representation of the current instance using the specified format string to defi |
| | | 286 | | /// <param name="format">A standard or custom numeric format string that defines the format of individual elemen |
| | | 287 | | /// <param name="formatProvider">A format provider that supplies culture-specific formatting information.</param |
| | | 288 | | /// <returns>The string representation of the current instance.</returns> |
| | 0 | 289 | | string IFormattable.ToString(string? format, IFormatProvider? formatProvider) => ToString(); |
| | | 290 | | |
| | | 291 | | /// <summary>Tries to format the value of the current instance as characters into the provided span of character |
| | | 292 | | /// <param name="destination">When this method returns, this parameter is filled with this instance formatted ch |
| | | 293 | | /// <param name="charsWritten">When this method returns, the number of bytes that were written in <paramref name |
| | | 294 | | /// <returns><see langword="true"/> if the formatting was successful; otherwise, <see langword="false"/>.</retur |
| | | 295 | | public bool TryFormat(Span<char> destination, out int charsWritten) => |
| | 0 | 296 | | _address.AddressFamily == AddressFamily.InterNetworkV6 ? |
| | 0 | 297 | | destination.TryWrite(CultureInfo.InvariantCulture, $"[{_address}]:{_port}", out charsWritten) : |
| | 0 | 298 | | destination.TryWrite(CultureInfo.InvariantCulture, $"{_address}:{_port}", out charsWritten); |
| | | 299 | | |
| | | 300 | | /// <summary>Tries to format the value of the current instance as UTF-8 bytes into the provided span.</summary> |
| | | 301 | | /// <param name="utf8Destination">When this method returns, this parameter is filled with this instance formatte |
| | | 302 | | /// <param name="bytesWritten">When this method returns, the number of bytes that were written in <paramref name |
| | | 303 | | /// <returns><see langword="true"/> if the formatting was successful; otherwise, <see langword="false"/>.</retur |
| | | 304 | | public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten) => |
| | 0 | 305 | | _address.AddressFamily == AddressFamily.InterNetworkV6 ? |
| | 0 | 306 | | Utf8.TryWrite(utf8Destination, CultureInfo.InvariantCulture, $"[{_address}]:{_port}", out bytesWritten) |
| | 0 | 307 | | Utf8.TryWrite(utf8Destination, CultureInfo.InvariantCulture, $"{_address}:{_port}", out bytesWritten); |
| | | 308 | | |
| | | 309 | | /// <summary>Tries to format the value of the current instance as characters into the provided span of character |
| | | 310 | | /// <param name="destination">When this method returns, this parameter is filled with this instance formatted ch |
| | | 311 | | /// <param name="charsWritten">When this method returns, the number of bytes that were written in <paramref name |
| | | 312 | | /// <param name="format">A span containing the characters that represent a standard or custom format string that |
| | | 313 | | /// <param name="provider">An optional object that supplies culture-specific formatting information for <paramre |
| | | 314 | | /// <returns><see langword="true"/> if the formatting was successful; otherwise, <see langword="false"/>.</retur |
| | | 315 | | bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormat |
| | 0 | 316 | | TryFormat(destination, out charsWritten); |
| | | 317 | | |
| | | 318 | | /// <summary>Tries to format the value of the current instance as UTF-8 bytes into the provided span.</summary> |
| | | 319 | | /// <param name="utf8Destination">When this method returns, this parameter is filled with this instance formatte |
| | | 320 | | /// <param name="bytesWritten">When this method returns, the number of bytes that were written in <paramref name |
| | | 321 | | /// <param name="format">A span containing the characters that represent a standard or custom format string that |
| | | 322 | | /// <param name="provider">An optional object that supplies culture-specific formatting information for <paramre |
| | | 323 | | /// <returns><see langword="true"/> if the formatting was successful; otherwise, <see langword="false"/>.</retur |
| | | 324 | | bool IUtf8SpanFormattable.TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, |
| | 0 | 325 | | TryFormat(utf8Destination, out bytesWritten); |
| | | 326 | | } |
| | | 327 | | } |
| | | 328 | | |