Index: net/base/ip_address.cc |
diff --git a/net/base/ip_address.cc b/net/base/ip_address.cc |
index 606483b0bf65f829fc67f31a8b46eb83aa6600a1..42071e010e358be1b30e46431574de9e5dd116f2 100644 |
--- a/net/base/ip_address.cc |
+++ b/net/base/ip_address.cc |
@@ -4,6 +4,9 @@ |
#include "net/base/ip_address.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_piece.h" |
+#include "base/strings/string_split.h" |
#include "net/base/ip_address_number.h" |
#include "url/gurl.h" |
#include "url/url_canon_ip.h" |
@@ -139,6 +142,36 @@ bool IPAddressMatchesPrefix(const IPAddress& ip_address, |
prefix_length_in_bits); |
} |
+bool ParseCIDRBlock(const std::string& cidr_literal, |
+ IPAddress* ip_address, |
+ size_t* prefix_length_in_bits) { |
+ // We expect CIDR notation to match one of these two templates: |
+ // <IPv4-literal> "/" <number of bits> |
+ // <IPv6-literal> "/" <number of bits> |
+ |
+ std::vector<base::StringPiece> parts = base::SplitStringPiece( |
+ cidr_literal, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
+ if (parts.size() != 2) |
+ return false; |
+ |
+ // Parse the IP address. |
+ if (!ip_address->AssignFromIPLiteral(parts[0])) |
+ return false; |
+ |
+ // Parse the prefix length. |
+ int number_of_bits = -1; |
+ if (!base::StringToInt(parts[1], &number_of_bits)) |
+ return false; |
+ |
+ // Make sure the prefix length is in a valid range. |
+ if (number_of_bits < 0 || |
+ number_of_bits > static_cast<int>(ip_address->size() * 8)) |
+ return false; |
+ |
+ *prefix_length_in_bits = static_cast<size_t>(number_of_bits); |
+ return true; |
+} |
+ |
unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) { |
return CommonPrefixLength(a1.bytes(), a2.bytes()); |
} |