Index: src/url_canon_ip.cc |
=================================================================== |
--- src/url_canon_ip.cc (revision 103) |
+++ src/url_canon_ip.cc (working copy) |
@@ -31,6 +31,7 @@ |
#include <stdlib.h> |
+#include "base/logging.h" |
#include "googleurl/src/url_canon_internal.h" |
namespace url_canon { |
@@ -54,8 +55,8 @@ |
template<typename CHAR, typename UCHAR> |
bool DoFindIPv4Components(const CHAR* spec, |
- const url_parse::Component& host, |
- url_parse::Component components[4]) { |
+ const url_parse::Component& host, |
+ url_parse::Component components[4]) { |
int cur_component = 0; // Index of the component we're working on. |
int cur_component_begin = host.begin; // Start of the current component. |
int end = host.end(); |
@@ -179,11 +180,12 @@ |
out_host->len = output->length() - out_host->begin; |
} |
-template<typename CHAR, typename UCHAR> |
-bool DoCanonicalizeIPv4Address(const CHAR* spec, |
- const url_parse::Component& host, |
- CanonOutput* output, |
- url_parse::Component* out_host) { |
+// Converts an IPv4 address to a 32-bit number (network byte order), returning |
+// true on success. False means that the input is not a valid IPv4 address. |
+template<typename CHAR> |
+bool IPv4AddressToNumber(const CHAR* spec, |
+ const url_parse::Component& host, |
+ unsigned char address[4]) { |
// The identified components. Not all may exist. |
url_parse::Component components[4]; |
if (!FindIPv4Components(spec, host, components)) |
@@ -203,7 +205,6 @@ |
} |
// Use that sequence of numbers to fill out the 4-component IP address. |
- unsigned char address[4]; |
// ...first fill all but the last component by truncating to one byte. |
for (int i = 0; i < existing_components - 1; i++) |
@@ -220,68 +221,382 @@ |
address[2] = (last_value & 0x0000FF00) >> 8; |
address[3] = last_value & 0xFF; |
- AppendIPv4Address(address, output, out_host); |
return true; |
} |
-// This function does NO canonicalization. It does _some_ validation |
-// and then copies the component as is to the output. |
-// TODO: Actual canonicalization! |
template<typename CHAR, typename UCHAR> |
-bool DoCanonicalizeIPv6Address(const CHAR* spec, |
+bool DoCanonicalizeIPv4Address(const CHAR* spec, |
const url_parse::Component& host, |
CanonOutput* output, |
url_parse::Component* out_host) { |
+ unsigned char address[4]; |
+ if (!IPv4AddressToNumber<CHAR>(spec, host, address)) |
+ return false; |
+ |
+ AppendIPv4Address(address, output, out_host); |
+ return true; |
+} |
+ |
+// Helper class that describes the main components of an IPv6 input string. |
+// See the following examples to understand how it breaks up an input string: |
+// |
+// [Example 1]: input = "[::aa:bb]" |
+// ==> num_hex_components = 2 |
+// ==> hex_components[0] = Component(3,2) "aa" |
+// ==> hex_components[1] = Component(6,2) "bb" |
+// ==> index_of_contraction = 0 |
+// ==> ipv4_component = Component(0, -1) |
+// |
+// [Example 2]: input = "[1:2::3:4:5]" |
+// ==> num_hex_components = 5 |
+// ==> hex_components[0] = Component(1,1) "1" |
+// ==> hex_components[1] = Component(3,1) "2" |
+// ==> hex_components[2] = Component(6,1) "3" |
+// ==> hex_components[3] = Component(8,1) "4" |
+// ==> hex_components[4] = Component(10,1) "5" |
+// ==> index_of_contraction = 2 |
+// ==> ipv4_component = Component(0, -1) |
+// |
+// [Example 3]: input = "[::ffff:192.168.0.1]" |
+// ==> num_hex_components = 1 |
+// ==> hex_components[0] = Component(3,4) "ffff" |
+// ==> index_of_contraction = 0 |
+// ==> ipv4_component = Component(8, 11) "192.168.0.1" |
+// |
+// [Example 4]: input = "[1::]" |
+// ==> num_hex_components = 1 |
+// ==> hex_components[0] = Component(1,1) "1" |
+// ==> index_of_contraction = 1 |
+// ==> ipv4_component = Component(0, -1) |
+// |
+// [Example 5]: input = "[::192.168.0.1]" |
+// ==> num_hex_components = 0 |
+// ==> index_of_contraction = 0 |
+// ==> ipv4_component = Component(8, 11) "192.168.0.1" |
+// |
+struct IPv6Parsed { |
+ // Zero-out the parse information. |
+ void reset() { |
+ num_hex_components = 0; |
+ index_of_contraction = -1; |
+ ipv4_component.reset(); |
+ } |
+ |
+ // There can be up to 8 hex components (colon separated) in the literal. |
+ url_parse::Component hex_components[8]; |
+ |
+ // The count of hex components present. Ranges from [0,8]. |
+ int num_hex_components; |
+ |
+ // The index of the hex component that the "::" contraction precedes, or |
+ // -1 if there is no contraction. |
+ int index_of_contraction; |
+ |
+ // The range of characters which are an IPv4 literal. |
+ url_parse::Component ipv4_component; |
+}; |
+ |
+// Parse the IPv6 input string. If parsing succeeded returns true and fills |
+// |parsed| with the information. If parsing failed (because the input is |
+// invalid) returns false. |
+template<typename CHAR, typename UCHAR> |
+bool DoParseIPv6(const CHAR* spec, |
+ const url_parse::Component& host, |
+ IPv6Parsed* parsed) { |
+ // Zero-out the info. |
+ parsed->reset(); |
+ |
+ if (!host.is_nonempty()) |
+ return false; |
+ |
+ // The index for start and end of address range (no brackets). |
+ int begin = host.begin; |
+ int end = host.end(); |
+ |
+ int cur_component_begin = begin; // Start of the current component. |
+ |
+ // Scan through the input, searching for hex components, "::" contractions, |
+ // and IPv4 components. |
+ for (int i = begin; /* i <= end */; i++) { |
+ bool is_colon = spec[i] == ':'; |
+ bool is_contraction = is_colon && i < end - 1 && spec[i + 1] == ':'; |
+ |
+ // We reached the end of the current component if we encounter a colon |
+ // (separator between hex components, or start of a contraction), or end of |
+ // input. |
+ if (is_colon || i == end) { |
+ int component_len = i - cur_component_begin; |
+ |
+ // A component should not have more than 4 hex digits. |
+ if (component_len > 4) |
+ return false; |
+ |
+ // Don't allow empty components. |
+ if (component_len == 0) { |
+ // The exception is when contractions appear at beginning of the |
+ // input or at the end of the input. |
+ if (!((is_contraction && i == begin) || (i == end && |
+ parsed->index_of_contraction == parsed->num_hex_components))) |
+ return false; |
+ } |
+ |
+ // Add the hex component we just found to running list. |
+ if (component_len > 0) { |
+ // Can't have more than 8 components! |
+ if (parsed->num_hex_components >= 8) |
+ return false; |
+ |
+ parsed->hex_components[parsed->num_hex_components++] = |
+ url_parse::Component(cur_component_begin, component_len); |
+ } |
+ } |
+ |
+ if (i == end) |
+ break; // Reached the end of the input, DONE. |
+ |
+ // We found a "::" contraction. |
+ if (is_contraction) { |
+ // There can be at most one contraction in the literal. |
+ if (parsed->index_of_contraction != -1) |
+ return false; |
+ parsed->index_of_contraction = parsed->num_hex_components; |
+ ++i; // Consume the colon we peeked. |
+ } |
+ |
+ if (is_colon) { |
+ // Colons are separators between components, keep track of where the |
+ // current component started (after this colon). |
+ cur_component_begin = i + 1; |
+ } else { |
+ if (static_cast<UCHAR>(spec[i]) >= 0x80) |
+ return false; // Not ASCII. |
+ |
+ if (!IsHexChar(static_cast<unsigned char>(spec[i]))) { |
+ // Regular components are hex numbers. It is also possible for |
+ // a component to be an IPv4 address in dotted form. |
+ if (IsIPv4Char(static_cast<unsigned char>(spec[i]))) { |
+ // Since IPv4 address can only appear at the end, assume the rest |
+ // of the string is an IPv4 address. (We will parse this separately |
+ // later). |
+ parsed->ipv4_component = url_parse::Component( |
+ cur_component_begin, end - cur_component_begin); |
+ break; |
+ } else { |
+ // The character was neither a hex digit, nor an IPv4 character. |
+ return false; |
+ } |
+ } |
+ } |
+ } |
+ |
+ return true; |
+} |
+ |
+// Verifies the parsed IPv6 information, checking that the various components |
+// add up to the right number of bits (hex components are 16 bits, while |
+// embedded IPv4 formats are 32 bits, and contractions are placeholdes for |
+// 16 or more bits). Returns true if sizes match up, false otherwise. On |
+// success writes the length of the contraction (if any) to |
+// |out_num_bytes_of_contraction|. |
+bool CheckIPv6ComponentsSize(const IPv6Parsed& parsed, |
+ int* out_num_bytes_of_contraction) { |
+ // Each group of four hex digits contributes 16 bits. |
+ int num_bytes_without_contraction = parsed.num_hex_components * 2; |
+ |
+ // If an IPv4 address was embedded at the end, it contributes 32 bits. |
+ if (parsed.ipv4_component.is_valid()) |
+ num_bytes_without_contraction += 4; |
+ |
+ // If there was a "::" contraction, its size is going to be: |
+ // MAX([16bits], [128bits] - num_bytes_without_contraction). |
+ int num_bytes_of_contraction = 0; |
+ if (parsed.index_of_contraction != -1) { |
+ num_bytes_of_contraction = 16 - num_bytes_without_contraction; |
+ if (num_bytes_of_contraction < 2) |
+ num_bytes_of_contraction = 2; |
+ } |
+ |
+ // Check that the numbers add up. |
+ if (num_bytes_without_contraction + num_bytes_of_contraction != 16) |
+ return false; |
+ |
+ *out_num_bytes_of_contraction = num_bytes_of_contraction; |
+ return true; |
+} |
+ |
+// Converts a hex comonent into a number. This cannot fail since the caller has |
+// already verified that each character in the string was a hex digit, and |
+// that there were no more than 4 characters. |
+template<typename CHAR> |
+uint16_t IPv6HexComponentToNumber(const CHAR* spec, |
+ const url_parse::Component& component) { |
+ DCHECK(component.len <= 4); |
+ |
+ // Copy the hex string into a C-string. |
+ char buf[5]; |
+ for (int i = 0; i < component.len; ++i) |
+ buf[i] = static_cast<char>(spec[component.begin + i]); |
+ buf[component.len] = '\0'; |
+ |
+ // Convert it to a number (overflow is not possible, since with 4 hex |
+ // characters we can at most have a 16 bit number). |
+ return static_cast<uint16_t>(_strtoui64(buf, NULL, 16)); |
+} |
+ |
+// Converts an IPv6 address to a 128-bit number (network byte order), returning |
+// true on success. False means that the input was not a valid IPv6 address. |
+template<typename CHAR, typename UCHAR> |
+bool IPv6AddressToNumber(const CHAR* spec, |
+ const url_parse::Component& host, |
+ unsigned char address[16]) { |
// Make sure the component is bounded by '[' and ']'. |
int end = host.end(); |
if (!host.is_nonempty() || spec[host.begin] != '[' || spec[end - 1] != ']') |
return false; |
- int num_colons = 0; |
- int num_dots = 0; |
- int num_hex = 0; |
- for (int i = host.begin + 1; i < end - 1; i++) { |
- if (static_cast<UCHAR>(spec[i]) >= 0x80) |
- return false; |
+ // Exclude the square brackets. |
+ url_parse::Component ipv6_comp(host.begin + 1, host.len - 2); |
- unsigned char u = static_cast<unsigned char>(spec[i]); |
- if (IsHexChar(u)) { |
- // No block between ':'s can be more than 4 hex characters. |
- if (num_hex > 3) |
+ // Parse the IPv6 address -- identify where all the colon separated hex |
+ // components are, the "::" contraction, and the embedded IPv4 address. |
+ IPv6Parsed ipv6_parsed; |
+ if (!DoParseIPv6<CHAR, UCHAR>(spec, ipv6_comp, &ipv6_parsed)) |
+ return false; |
+ |
+ // Do some basic size checks to make sure that the address doesn't |
+ // specify more than 128 bits or fewer than 128 bits. This also resolves |
+ // how may zero bytes the "::" contraction represents. |
+ int num_bytes_of_contraction; |
+ if (!CheckIPv6ComponentsSize(ipv6_parsed, &num_bytes_of_contraction)) |
+ return false; |
+ |
+ int cur_index_in_address = 0; |
+ |
+ // Loop through each hex components, and contraction in order. |
+ for (int i = 0; i <= ipv6_parsed.num_hex_components; ++i) { |
+ // Append the contraction if it appears before this component. |
+ if (i == ipv6_parsed.index_of_contraction) { |
+ for (int j = 0; j < num_bytes_of_contraction; ++j) |
+ address[cur_index_in_address++] = 0; |
+ } |
+ // Append the hex component's value. |
+ if (i != ipv6_parsed.num_hex_components) { |
+ // Get the 16-bit value for this hex component. |
+ uint16_t number = IPv6HexComponentToNumber<CHAR>( |
+ spec, ipv6_parsed.hex_components[i]); |
+ // Append to |address|, in network byte order. |
+ address[cur_index_in_address++] = (number & 0xFF00) >> 8; |
+ address[cur_index_in_address++] = (number & 0x00FF); |
+ } |
+ } |
+ |
+ // If there was an IPv4 section, convert it into a 32-bit number and append |
+ // it to |address|. |
+ if (ipv6_parsed.ipv4_component.is_valid()) { |
+ // We only allow the embedded IPv4 syntax to be used for "compat" and |
+ // "mapped" formats: |
+ // "compat" ==> 0:0:0:0:0:ffff:<IPv4-literal> |
+ // "mapped" ==> 0:0:0:0:0:0000:<IPv4-literal> |
+ for (int j = 0; j < 10; ++j) { |
+ if (address[j] != 0) |
return false; |
- num_hex++; |
- } else if (u == ':') { |
- // No ':'s can appear after '.'s have appeared and there can be no |
- // more than 7 ':'s separating the 8 hex shorts. |
- if (num_dots > 0 || num_colons > 6) |
- return false; |
- num_colons++; |
- num_hex = 0; |
- } else if (u == '.') { |
- // No hex chars between ':'s is fine (signifies successive |
- // zeroed shorts concatentated, but can only be used once). Not |
- // valid for embedded IPv4 addresses, however. |
- if (num_hex < 1) |
- return false; |
- num_dots++; |
- num_hex = 0; |
- } else { |
- // Invalid characters for an IPv6 address. |
+ } |
+ if (!((address[10] == 0 && address[11] == 0) || |
+ (address[10] == 0xFF && address[11] == 0xFF))) |
return false; |
+ |
+ // Append the 32-bit number to |address|. |
+ if (!IPv4AddressToNumber(spec, |
+ ipv6_parsed.ipv4_component, |
+ &address[cur_index_in_address])) |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+// Searches for the longest sequence of zeros in |address|, and writes the |
+// range into |contraction_range|. The run of zeros must be at least 16 bits, |
+// and if there is a tie the first is chosen. |
+void ChooseIPv6ContractionRange(const unsigned char address[16], |
+ url_parse::Component* contraction_range) { |
+ // The longest run of zeros in |address| seen so far. |
+ url_parse::Component max_range; |
+ |
+ // The current run of zeros in |address| being iterated over. |
+ url_parse::Component cur_range; |
+ |
+ for (int i = 0; i < 16; i += 2) { |
+ // Test for 16 bits worth of zero. |
+ bool is_zero = (address[i] == 0 && address[i + 1] == 0); |
+ |
+ if (is_zero) { |
+ // Add the zero to the current range (or start a new one). |
+ if (!cur_range.is_valid()) |
+ cur_range = url_parse::Component(i, 0); |
+ cur_range.len += 2; |
} |
+ |
+ if (!is_zero || i == 14) { |
+ // Just completed a run of zeros. If the run is greater than 16 bits, |
+ // it is a candidate for the contraction. |
+ if (cur_range.len > 2 && cur_range.len > max_range.len) { |
+ max_range = cur_range; |
+ } |
+ cur_range.reset(); |
+ } |
} |
- if (num_colons < 2) |
+ *contraction_range = max_range; |
+} |
+ |
+template<typename CHAR, typename UCHAR> |
+bool DoCanonicalizeIPv6Address(const CHAR* spec, |
+ const url_parse::Component& host, |
+ CanonOutput* output, |
+ url_parse::Component* out_host) { |
+ // Turn the IP address into a 128 bit number. |
+ unsigned char address[16]; |
+ if (!IPv6AddressToNumber<CHAR, UCHAR>(spec, host, address)) |
return false; |
- if (num_dots != 0 && num_dots != 3) |
- return false; |
- // This passed all the checks thus far, so just copy input to output. |
- // NOTE: It may still be invalid, and it's definitely not canonicalized. |
- // TODO: Actually canonicalize. |
out_host->begin = output->length(); |
- for (int i = host.begin; i < end; i++) |
- output->push_back(static_cast<char>(spec[i])); |
+ output->push_back('['); |
+ |
+ // We will now output the address according to the rules in: |
+ // http://tools.ietf.org/html/draft-kawamura-ipv6-text-representation-01#section-4 |
+ |
+ // Start by finding where to place the "::" contraction (if any). |
+ url_parse::Component contraction_range; |
+ ChooseIPv6ContractionRange(address, &contraction_range); |
+ |
+ for (int i = 0; i < 16;) { |
+ if (i == contraction_range.begin && contraction_range.len > 0) { |
+ // Jump over the contraction. |
+ if (i == 0) |
+ output->push_back(':'); |
+ output->push_back(':'); |
+ i = contraction_range.end(); |
+ } else { |
+ // Consume the next 16 bits from |address|. |
+ int x = address[i] << 8 | address[i + 1]; |
+ |
+ i += 2; |
+ |
+ // Stringify the 16 bit number (at most requires 4 hex digits). |
+ char str[5]; |
+ _itoa_s(x, str, 16); |
+ for (int ch = 0; str[ch] != 0; ++ch) |
+ output->push_back(str[ch]); |
+ |
+ // Put a colon after each number, except the last. |
+ if (i < 16) |
+ output->push_back(':'); |
+ } |
+ } |
+ |
+ output->push_back(']'); |
out_host->len = output->length() - out_host->begin; |
+ |
return true; |
} |