Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: net/dns/dns_util.cc

Issue 2739203003: Measure how often DNS hostnames aren't in preferred name form. (Closed)
Patch Set: Change the behavior to reporting via UMA; add documentation. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/dns/dns_util.h" 5 #include "net/dns/dns_util.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <limits.h> 8 #include <limits.h>
9 9
10 #include <cstring> 10 #include <cstring>
11 11
12 #include "base/metrics/field_trial.h" 12 #include "base/metrics/field_trial.h"
13 #include "base/metrics/histogram_macros.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
15 #include "build/build_config.h" 16 #include "build/build_config.h"
16 #include "net/base/address_list.h" 17 #include "net/base/address_list.h"
17 18
19 #if defined(OS_POSIX)
20 #include <netinet/in.h>
21 #if !defined(OS_NACL)
22 #include <net/if.h>
23 #if !defined(OS_ANDROID)
24 #include <ifaddrs.h>
25 #endif // !defined(OS_ANDROID)
26 #endif // !defined(OS_NACL)
27 #endif // defined(OS_POSIX)
28
29 #if defined(OS_ANDROID)
30 #include "net/android/network_library.h"
31 #endif
32
18 namespace net { 33 namespace net {
19 34
20 // Based on DJB's public domain code. 35 // Based on DJB's public domain code.
21 bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { 36 bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) {
22 const char* buf = dotted.data(); 37 const char* buf = dotted.data();
23 unsigned n = dotted.size(); 38 size_t n = dotted.size();
24 char label[63]; 39 char label[63];
25 size_t labellen = 0; /* <= sizeof label */ 40 size_t labellen = 0; /* <= sizeof label */
26 char name[255]; 41 char name[255];
27 size_t namelen = 0; /* <= sizeof name */ 42 size_t namelen = 0; /* <= sizeof name */
28 char ch; 43 char ch;
44 bool valid_name = true;
29 45
30 for (;;) { 46 for (;;) {
31 if (!n) 47 if (!n)
32 break; 48 break;
33 ch = *buf++; 49 ch = *buf++;
34 --n; 50 --n;
35 if (ch == '.') { 51 if (ch == '.') {
36 // Don't allow empty labels per http://crbug.com/456391. 52 // Don't allow empty labels per http://crbug.com/456391.
37 if (!labellen) 53 if (!labellen)
38 return false; 54 return false;
39 if (namelen + labellen + 1 > sizeof name) 55 if (namelen + labellen + 1 > sizeof name)
40 return false; 56 return false;
41 name[namelen++] = static_cast<char>(labellen); 57 name[namelen++] = static_cast<char>(labellen);
42 memcpy(name + namelen, label, labellen); 58 memcpy(name + namelen, label, labellen);
43 namelen += labellen; 59 namelen += labellen;
44 labellen = 0; 60 labellen = 0;
45 continue; 61 continue;
46 } 62 }
47 if (labellen >= sizeof label) 63 if (labellen >= sizeof label)
48 return false; 64 return false;
65 if (!IsValidLabelCharacter(ch, labellen == 0)) {
66 // TODO(palmer): In the future, when we can remove support for invalid
67 // names, return false here instead (and remove the UMA counter).
68 valid_name = false;
69 }
49 label[labellen++] = ch; 70 label[labellen++] = ch;
50 } 71 }
51 72
73 UMA_HISTOGRAM_BOOLEAN("Net.ValidDNSName", valid_name);
Ryan Sleevi 2017/03/22 00:16:07 Do you want to record this after line 85/87?
palmer 2017/03/22 01:03:33 Oops, yes. Done.
74
52 // Allow empty label at end of name to disable suffix search. 75 // Allow empty label at end of name to disable suffix search.
53 if (labellen) { 76 if (labellen) {
54 if (namelen + labellen + 1 > sizeof name) 77 if (namelen + labellen + 1 > sizeof name)
55 return false; 78 return false;
56 name[namelen++] = static_cast<char>(labellen); 79 name[namelen++] = static_cast<char>(labellen);
57 memcpy(name + namelen, label, labellen); 80 memcpy(name + namelen, label, labellen);
58 namelen += labellen; 81 namelen += labellen;
59 labellen = 0; 82 labellen = 0;
60 } 83 }
61 84
62 if (namelen + 1 > sizeof name) 85 if (namelen + 1 > sizeof name)
63 return false; 86 return false;
64 if (namelen == 0) // Empty names e.g. "", "." are not valid. 87 if (namelen == 0) // Empty names e.g. "", "." are not valid.
65 return false; 88 return false;
66 name[namelen++] = 0; // This is the root label (of length 0). 89 name[namelen++] = 0; // This is the root label (of length 0).
67 90
68 *out = std::string(name, namelen); 91 *out = std::string(name, namelen);
69 return true; 92 return true;
70 } 93 }
71 94
72 bool IsValidDNSDomain(const base::StringPiece& dotted) { 95 bool IsValidDNSDomain(const base::StringPiece& dotted) {
73 std::string dns_formatted; 96 std::string dns_formatted;
74 return DNSDomainFromDot(dotted, &dns_formatted); 97 return DNSDomainFromDot(dotted, &dns_formatted);
75 } 98 }
76 99
100 bool IsValidLabelCharacter(char c, bool is_first_char) {
101 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
102 (c >= '0' && c <= '9') || (!is_first_char && c == '-') || c == '_';
103 }
104
77 std::string DNSDomainToString(const base::StringPiece& domain) { 105 std::string DNSDomainToString(const base::StringPiece& domain) {
78 std::string ret; 106 std::string ret;
79 107
80 for (unsigned i = 0; i < domain.size() && domain[i]; i += domain[i] + 1) { 108 for (unsigned i = 0; i < domain.size() && domain[i]; i += domain[i] + 1) {
81 #if CHAR_MIN < 0 109 #if CHAR_MIN < 0
82 if (domain[i] < 0) 110 if (domain[i] < 0)
83 return std::string(); 111 return std::string();
84 #endif 112 #endif
85 if (domain[i] > 63) 113 if (domain[i] > 63)
86 return std::string(); 114 return std::string();
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 return DELTA_IDENTICAL; 186 return DELTA_IDENTICAL;
159 else if (same_size && !any_missing) 187 else if (same_size && !any_missing)
160 return DELTA_REORDERED; 188 return DELTA_REORDERED;
161 else if (any_match) 189 else if (any_match)
162 return DELTA_OVERLAP; 190 return DELTA_OVERLAP;
163 else 191 else
164 return DELTA_DISJOINT; 192 return DELTA_DISJOINT;
165 } 193 }
166 194
167 } // namespace net 195 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698