 Chromium Code Reviews
 Chromium Code Reviews Issue 2739203003:
  Measure how often DNS hostnames aren't in preferred name form.  (Closed)
    
  
    Issue 2739203003:
  Measure how often DNS hostnames aren't in preferred name form.  (Closed) 
  | Index: net/dns/dns_util.cc | 
| diff --git a/net/dns/dns_util.cc b/net/dns/dns_util.cc | 
| index f80eaee9c54a80fbd6a96ea13dc4d78167c629ad..90a31427b6835281367a00a549d86505f158ce15 100644 | 
| --- a/net/dns/dns_util.cc | 
| +++ b/net/dns/dns_util.cc | 
| @@ -10,22 +10,38 @@ | 
| #include <cstring> | 
| #include "base/metrics/field_trial.h" | 
| +#include "base/metrics/histogram_macros.h" | 
| #include "base/strings/string_number_conversions.h" | 
| #include "base/strings/string_split.h" | 
| #include "build/build_config.h" | 
| #include "net/base/address_list.h" | 
| +#if defined(OS_POSIX) | 
| +#include <netinet/in.h> | 
| +#if !defined(OS_NACL) | 
| +#include <net/if.h> | 
| +#if !defined(OS_ANDROID) | 
| +#include <ifaddrs.h> | 
| +#endif // !defined(OS_ANDROID) | 
| +#endif // !defined(OS_NACL) | 
| +#endif // defined(OS_POSIX) | 
| + | 
| +#if defined(OS_ANDROID) | 
| +#include "net/android/network_library.h" | 
| +#endif | 
| + | 
| namespace net { | 
| // Based on DJB's public domain code. | 
| bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { | 
| const char* buf = dotted.data(); | 
| - unsigned n = dotted.size(); | 
| + size_t n = dotted.size(); | 
| char label[63]; | 
| size_t labellen = 0; /* <= sizeof label */ | 
| char name[255]; | 
| size_t namelen = 0; /* <= sizeof name */ | 
| char ch; | 
| + bool valid_name = true; | 
| for (;;) { | 
| if (!n) | 
| @@ -46,6 +62,11 @@ bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { | 
| } | 
| if (labellen >= sizeof label) | 
| return false; | 
| + if (!IsValidHostLabelCharacter(ch, labellen == 0)) { | 
| + // TODO(palmer): In the future, when we can remove support for invalid | 
| + // names, return false here instead (and remove the UMA counter). | 
| + valid_name = false; | 
| + } | 
| label[labellen++] = ch; | 
| } | 
| @@ -59,6 +80,8 @@ bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { | 
| labellen = 0; | 
| } | 
| + UMA_HISTOGRAM_BOOLEAN("Net.ValidDNSName", valid_name); | 
| 
Ryan Sleevi
2017/03/22 01:10:46
Did you mean to move this down further? That is, t
 | 
| + | 
| if (namelen + 1 > sizeof name) | 
| return false; | 
| if (namelen == 0) // Empty names e.g. "", "." are not valid. | 
| @@ -74,6 +97,11 @@ bool IsValidDNSDomain(const base::StringPiece& dotted) { | 
| return DNSDomainFromDot(dotted, &dns_formatted); | 
| } | 
| +bool IsValidHostLabelCharacter(char c, bool is_first_char) { | 
| + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | 
| + (c >= '0' && c <= '9') || (!is_first_char && c == '-') || c == '_'; | 
| +} | 
| + | 
| std::string DNSDomainToString(const base::StringPiece& domain) { | 
| std::string ret; |