| | | 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.Numerics; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | |
| | | 9 | | namespace System.Net |
| | | 10 | | { |
| | | 11 | | internal static partial class IPv4AddressHelper |
| | | 12 | | { |
| | | 13 | | internal const long Invalid = -1; |
| | | 14 | | private const long MaxIPv4Value = uint.MaxValue; // the native parser cannot handle MaxIPv4Value, only MaxIPv4Va |
| | | 15 | | |
| | | 16 | | private const int Octal = 8; |
| | | 17 | | private const int Decimal = 10; |
| | | 18 | | private const int Hex = 16; |
| | | 19 | | |
| | | 20 | | private const int NumberOfLabels = 4; |
| | | 21 | | |
| | | 22 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 23 | | internal static ushort ToUShort<TChar>(TChar value) |
| | | 24 | | where TChar : unmanaged, IBinaryInteger<TChar> |
| | 9642 | 25 | | { |
| | 9642 | 26 | | Debug.Assert(typeof(TChar) == typeof(char) || typeof(TChar) == typeof(byte)); |
| | | 27 | | |
| | 9642 | 28 | | return typeof(TChar) == typeof(char) |
| | 9642 | 29 | | ? (char)(object)value |
| | 9642 | 30 | | : (byte)(object)value; |
| | 9642 | 31 | | } |
| | | 32 | | |
| | | 33 | | // Only called from the IPv6Helper, only parse the canonical format |
| | | 34 | | internal static unsafe int ParseHostNumber<TChar>(ReadOnlySpan<TChar> str, int start, int end) |
| | | 35 | | where TChar : unmanaged, IBinaryInteger<TChar> |
| | 0 | 36 | | { |
| | 0 | 37 | | Debug.Assert(typeof(TChar) == typeof(char) || typeof(TChar) == typeof(byte)); |
| | | 38 | | |
| | 0 | 39 | | Span<byte> numbers = stackalloc byte[NumberOfLabels]; |
| | | 40 | | |
| | 0 | 41 | | for (int i = 0; i < numbers.Length; ++i) |
| | 0 | 42 | | { |
| | 0 | 43 | | int b = 0; |
| | | 44 | | int ch; |
| | | 45 | | |
| | 0 | 46 | | for (; (start < end) && (ch = ToUShort(str[start])) != '.' && ch != ':'; ++start) |
| | 0 | 47 | | { |
| | 0 | 48 | | b = (b * 10) + ch - '0'; |
| | 0 | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | numbers[i] = (byte)b; |
| | 0 | 52 | | ++start; |
| | 0 | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | return BinaryPrimitives.ReadInt32BigEndian(numbers); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | // |
| | | 59 | | // IsValid |
| | | 60 | | // |
| | | 61 | | // Performs IsValid on a substring. Updates the index to where we |
| | | 62 | | // believe the IPv4 address ends |
| | | 63 | | // |
| | | 64 | | // Inputs: |
| | | 65 | | // <argument> name |
| | | 66 | | // string containing possible IPv4 address |
| | | 67 | | // |
| | | 68 | | // <argument> start |
| | | 69 | | // offset in <name> to start checking for IPv4 address |
| | | 70 | | // |
| | | 71 | | // <argument> end |
| | | 72 | | // offset in <name> of the last character we can touch in the check |
| | | 73 | | // |
| | | 74 | | // Outputs: |
| | | 75 | | // <argument> end |
| | | 76 | | // index of last character in <name> we checked |
| | | 77 | | // |
| | | 78 | | // <argument> allowIPv6 |
| | | 79 | | // enables parsing IPv4 addresses embedded in IPv6 address literals |
| | | 80 | | // |
| | | 81 | | // <argument> notImplicitFile |
| | | 82 | | // do not consider this URI holding an implicit filename |
| | | 83 | | // |
| | | 84 | | // <argument> unknownScheme |
| | | 85 | | // the check is made on an unknown scheme (suppress IPv4 canonicalization) |
| | | 86 | | // |
| | | 87 | | // Assumes: |
| | | 88 | | // The address string is terminated by either |
| | | 89 | | // end of the string, characters ':' '/' '\' '?' |
| | | 90 | | // |
| | | 91 | | // |
| | | 92 | | // Returns: |
| | | 93 | | // bool |
| | | 94 | | // |
| | | 95 | | // Throws: |
| | | 96 | | // Nothing |
| | | 97 | | // |
| | | 98 | | |
| | | 99 | | //Remark: MUST NOT be used unless all input indexes are verified and trusted. |
| | | 100 | | internal static bool IsValid<TChar>(ReadOnlySpan<TChar> name, out int end, bool allowIPv6, bool notImplicitFile, |
| | | 101 | | where TChar : unmanaged, IBinaryInteger<TChar> |
| | 448 | 102 | | { |
| | | 103 | | // IPv6 can only have canonical IPv4 embedded. Unknown schemes will not attempt parsing of non-canonical IPv |
| | 448 | 104 | | if (allowIPv6 || unknownScheme) |
| | 202 | 105 | | { |
| | 202 | 106 | | return IsValidCanonical(name, out end, allowIPv6, notImplicitFile); |
| | | 107 | | } |
| | | 108 | | else |
| | 246 | 109 | | { |
| | 246 | 110 | | return ParseNonCanonical(name, out end, notImplicitFile) != Invalid; |
| | | 111 | | } |
| | 448 | 112 | | } |
| | | 113 | | |
| | | 114 | | // |
| | | 115 | | // IsValidCanonical |
| | | 116 | | // |
| | | 117 | | // Checks if the substring is a valid canonical IPv4 address or an IPv4 address embedded in an IPv6 literal |
| | | 118 | | // This is an attempt to parse ABNF productions from RFC3986, Section 3.2.2: |
| | | 119 | | // IP-literal = "[" ( IPv6address / IPvFuture ) "]" |
| | | 120 | | // IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet |
| | | 121 | | // dec-octet = DIGIT ; 0-9 |
| | | 122 | | // / %x31-39 DIGIT ; 10-99 |
| | | 123 | | // / "1" 2DIGIT ; 100-199 |
| | | 124 | | // / "2" %x30-34 DIGIT ; 200-249 |
| | | 125 | | // / "25" %x30-35 ; 250-255 |
| | | 126 | | // |
| | | 127 | | internal static bool IsValidCanonical<TChar>(ReadOnlySpan<TChar> name, out int end, bool allowIPv6, bool notImpl |
| | | 128 | | where TChar : unmanaged, IBinaryInteger<TChar> |
| | 202 | 129 | | { |
| | 202 | 130 | | Debug.Assert(typeof(TChar) == typeof(char) || typeof(TChar) == typeof(byte)); |
| | | 131 | | |
| | 202 | 132 | | end = 0; // Default value in case of failure |
| | 202 | 133 | | int dots = 0; |
| | 202 | 134 | | long number = 0; |
| | 202 | 135 | | bool haveNumber = false; |
| | 202 | 136 | | int start = 0; |
| | | 137 | | |
| | 1824 | 138 | | while (start < name.Length) |
| | 1824 | 139 | | { |
| | 1824 | 140 | | int ch = ToUShort(name[start]); |
| | | 141 | | |
| | 1824 | 142 | | if (allowIPv6) |
| | 1824 | 143 | | { |
| | | 144 | | // For an IPv4 address nested inside an IPv6 address, the terminator is either the IPv6 address term |
| | 1824 | 145 | | if (ch == ']' || ch == '/' || ch == '%') |
| | 202 | 146 | | { |
| | 202 | 147 | | break; |
| | | 148 | | } |
| | 1622 | 149 | | } |
| | 0 | 150 | | else if (ch == '/' || ch == '\\' || (notImplicitFile && (ch == ':' || ch == '?' || ch == '#'))) |
| | 0 | 151 | | { |
| | | 152 | | // For a normal IPv4 address, the terminator is the prefix ('/' or its counterpart, '\'). If notImpl |
| | | 153 | | // is one of the characters which signify the start of the rest of the URI - the port number (':'), |
| | | 154 | | |
| | 0 | 155 | | break; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | // An explicit cast to an unsigned integer forces character values preceding '0' to underflow, eliminati |
| | 1622 | 159 | | uint parsedCharacter = (uint)(ch - '0'); |
| | | 160 | | |
| | 1622 | 161 | | if (parsedCharacter < IPv4AddressHelper.Decimal) |
| | 1016 | 162 | | { |
| | 1016 | 163 | | if (!haveNumber && parsedCharacter == 0 && |
| | 1016 | 164 | | (uint)(start + 1) < (uint)name.Length && char.IsAsciiDigit((char)ToUShort(name[start + 1]))) |
| | 0 | 165 | | { |
| | | 166 | | // Octal is not allowed in canonical format. |
| | 0 | 167 | | return false; |
| | | 168 | | } |
| | | 169 | | |
| | 1016 | 170 | | haveNumber = true; |
| | 1016 | 171 | | number = number * IPv4AddressHelper.Decimal + parsedCharacter; |
| | 1016 | 172 | | if (number > byte.MaxValue) |
| | 0 | 173 | | { |
| | 0 | 174 | | return false; |
| | | 175 | | } |
| | 1016 | 176 | | } |
| | 606 | 177 | | else if (ch == '.') |
| | 606 | 178 | | { |
| | | 179 | | // If the current character is not an integer, it may be the IPv4 component separator ('.') |
| | | 180 | | |
| | 606 | 181 | | if (!haveNumber) |
| | 0 | 182 | | { |
| | 0 | 183 | | return false; |
| | | 184 | | } |
| | | 185 | | |
| | 606 | 186 | | ++dots; |
| | 606 | 187 | | haveNumber = false; |
| | 606 | 188 | | number = 0; |
| | 606 | 189 | | } |
| | | 190 | | else |
| | 0 | 191 | | { |
| | 0 | 192 | | return false; |
| | | 193 | | } |
| | 1622 | 194 | | ++start; |
| | 1622 | 195 | | } |
| | 202 | 196 | | bool res = (dots == 3) && haveNumber; |
| | 202 | 197 | | end = res ? start : 0; |
| | 202 | 198 | | return res; |
| | 202 | 199 | | } |
| | | 200 | | |
| | | 201 | | // Parse any canonical or noncanonical IPv4 formats and return a long between 0 and MaxIPv4Value. |
| | | 202 | | // Return Invalid (-1) for failures. |
| | | 203 | | // If the address has less than three dots, only the rightmost section is assumed to contain the combined value |
| | | 204 | | // the missing sections: 0xFF00FFFF == 0xFF.0x00.0xFF.0xFF == 0xFF.0xFFFF |
| | | 205 | | internal static long ParseNonCanonical<TChar>(ReadOnlySpan<TChar> name, out int end, bool notImplicitFile) |
| | | 206 | | where TChar : unmanaged, IBinaryInteger<TChar> |
| | 246 | 207 | | { |
| | 246 | 208 | | Debug.Assert(typeof(TChar) == typeof(char) || typeof(TChar) == typeof(byte)); |
| | | 209 | | |
| | 246 | 210 | | end = 0; // Default value in case of failure |
| | | 211 | | int numberBase; |
| | 246 | 212 | | int ch = 0; |
| | 246 | 213 | | Span<long> parts = [0, 0, 0]; // One part per octet. Final octet doesn't have a terminator, so is stored in |
| | 246 | 214 | | long currentValue = 0; |
| | 246 | 215 | | bool atLeastOneChar = false; |
| | | 216 | | |
| | | 217 | | // Parse one dotted section at a time |
| | 246 | 218 | | int dotCount = 0; // Limit 3 |
| | 246 | 219 | | int current = 0; |
| | | 220 | | |
| | 590 | 221 | | for (; current < name.Length; current++) |
| | 418 | 222 | | { |
| | 418 | 223 | | ch = ToUShort(name[current]); |
| | 418 | 224 | | currentValue = 0; |
| | | 225 | | |
| | | 226 | | // Figure out what base this section is in, default to base 10. |
| | | 227 | | // A number starting with zero should be interpreted in base 8 / octal |
| | | 228 | | // If the number starts with 0x, it should be interpreted in base 16 / hex |
| | 418 | 229 | | numberBase = IPv4AddressHelper.Decimal; |
| | | 230 | | |
| | 418 | 231 | | if (ch == '0') |
| | 290 | 232 | | { |
| | 290 | 233 | | current++; |
| | 290 | 234 | | atLeastOneChar = true; |
| | 290 | 235 | | if (current < name.Length) |
| | 275 | 236 | | { |
| | 275 | 237 | | ch = ToUShort(name[current]); |
| | | 238 | | |
| | 275 | 239 | | if (ch == 'x' || ch == 'X') |
| | 22 | 240 | | { |
| | 22 | 241 | | numberBase = IPv4AddressHelper.Hex; |
| | | 242 | | |
| | 22 | 243 | | current++; |
| | 22 | 244 | | atLeastOneChar = false; |
| | 22 | 245 | | } |
| | | 246 | | else |
| | 253 | 247 | | { |
| | 253 | 248 | | numberBase = IPv4AddressHelper.Octal; |
| | 253 | 249 | | } |
| | 275 | 250 | | } |
| | 290 | 251 | | } |
| | | 252 | | |
| | | 253 | | // Parse this section |
| | 13766 | 254 | | for (; current < name.Length; current++) |
| | 6969 | 255 | | { |
| | 6969 | 256 | | ch = ToUShort(name[current]); |
| | 6969 | 257 | | int digitValue = HexConverter.FromChar(ch); |
| | | 258 | | |
| | 6969 | 259 | | if (digitValue >= numberBase) |
| | 295 | 260 | | { |
| | 295 | 261 | | break; // Invalid/terminator |
| | | 262 | | } |
| | 6674 | 263 | | currentValue = (currentValue * numberBase) + digitValue; |
| | | 264 | | |
| | 6674 | 265 | | if (currentValue > MaxIPv4Value) // Overflow |
| | 0 | 266 | | { |
| | 0 | 267 | | return Invalid; |
| | | 268 | | } |
| | | 269 | | |
| | 6674 | 270 | | atLeastOneChar = true; |
| | 6674 | 271 | | } |
| | | 272 | | |
| | 418 | 273 | | if (current < name.Length && ch == '.') |
| | 172 | 274 | | { |
| | 172 | 275 | | if (dotCount >= 3 // Max of 3 dots and 4 segments |
| | 172 | 276 | | || !atLeastOneChar // No empty segments: 1...1 |
| | 172 | 277 | | // Only the last segment can be more than 255 (if there are less than 3 dots) |
| | 172 | 278 | | || currentValue > 0xFF) |
| | 0 | 279 | | { |
| | 0 | 280 | | return Invalid; |
| | | 281 | | } |
| | 172 | 282 | | parts[dotCount] = currentValue; |
| | 172 | 283 | | dotCount++; |
| | 172 | 284 | | atLeastOneChar = false; |
| | 172 | 285 | | continue; |
| | | 286 | | } |
| | | 287 | | // We don't get here unless we find an invalid character or a terminator |
| | 0 | 288 | | break; |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | // Terminators |
| | 246 | 292 | | if (!atLeastOneChar) |
| | 0 | 293 | | { |
| | 0 | 294 | | return Invalid; // Empty trailing segment: 1.1.1. |
| | | 295 | | } |
| | 246 | 296 | | else if (current >= name.Length) |
| | 123 | 297 | | { |
| | | 298 | | // end of string, allowed |
| | 123 | 299 | | end = name.Length; |
| | 123 | 300 | | } |
| | 123 | 301 | | else if (ch == '/' || ch == '\\' || (notImplicitFile && (ch == ':' || ch == '?' || ch == '#'))) |
| | 123 | 302 | | { |
| | | 303 | | // For a normal IPv4 address, the terminator is the prefix ('/' or its counterpart, '\'). If notImplicit |
| | | 304 | | // is one of the characters which signify the start of the rest of the URI - the port number (':'), quer |
| | 123 | 305 | | end = current; |
| | 123 | 306 | | } |
| | | 307 | | else |
| | 0 | 308 | | { |
| | | 309 | | // not a valid terminating character |
| | 0 | 310 | | return Invalid; |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | // Parsed, reassemble and check for overflows in the last part. Previous parts have already been checked in |
| | 246 | 314 | | switch (dotCount) |
| | | 315 | | { |
| | | 316 | | case 0: // 0xFFFFFFFF |
| | 170 | 317 | | return currentValue; |
| | | 318 | | case 1: // 0xFF.0xFFFFFF |
| | 20 | 319 | | Debug.Assert(parts[0] <= 0xFF); |
| | 20 | 320 | | if (currentValue > 0xffffff) |
| | 0 | 321 | | { |
| | 0 | 322 | | return Invalid; |
| | | 323 | | } |
| | 20 | 324 | | return (parts[0] << 24) | currentValue; |
| | | 325 | | case 2: // 0xFF.0xFF.0xFFFF |
| | 16 | 326 | | Debug.Assert(parts[0] <= 0xFF); |
| | 16 | 327 | | Debug.Assert(parts[1] <= 0xFF); |
| | 16 | 328 | | if (currentValue > 0xffff) |
| | 0 | 329 | | { |
| | 0 | 330 | | return Invalid; |
| | | 331 | | } |
| | 16 | 332 | | return (parts[0] << 24) | (parts[1] << 16) | currentValue; |
| | | 333 | | case 3: // 0xFF.0xFF.0xFF.0xFF |
| | 40 | 334 | | Debug.Assert(parts[0] <= 0xFF); |
| | 40 | 335 | | Debug.Assert(parts[1] <= 0xFF); |
| | 40 | 336 | | Debug.Assert(parts[2] <= 0xFF); |
| | 40 | 337 | | if (currentValue > 0xff) |
| | 0 | 338 | | { |
| | 0 | 339 | | return Invalid; |
| | | 340 | | } |
| | 40 | 341 | | return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | currentValue; |
| | | 342 | | default: |
| | 0 | 343 | | return Invalid; |
| | | 344 | | } |
| | 246 | 345 | | } |
| | | 346 | | } |
| | | 347 | | } |
| | | 348 | | |