OLD | NEW |
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> |
(...skipping 27 matching lines...) Expand all Loading... |
38 #endif // !defined(OS_NACL) | 38 #endif // !defined(OS_NACL) |
39 #endif // defined(OS_POSIX) | 39 #endif // defined(OS_POSIX) |
40 | 40 |
41 #if defined(OS_ANDROID) | 41 #if defined(OS_ANDROID) |
42 #include "net/android/network_library.h" | 42 #include "net/android/network_library.h" |
43 #endif | 43 #endif |
44 | 44 |
45 namespace net { | 45 namespace net { |
46 | 46 |
47 // Based on DJB's public domain code. | 47 // Based on DJB's public domain code. |
48 bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { | 48 bool DNSDomainFromDotWithValidityCheck(const base::StringPiece& dotted, |
| 49 std::string* out, |
| 50 bool* valid_name) { |
49 const char* buf = dotted.data(); | 51 const char* buf = dotted.data(); |
50 size_t n = dotted.size(); | 52 size_t n = dotted.size(); |
51 char label[kMaxLabelLength]; | 53 char label[kMaxLabelLength]; |
52 size_t labellen = 0; /* <= sizeof label */ | 54 size_t labellen = 0; /* <= sizeof label */ |
53 char name[dns_protocol::kMaxNameLength]; | 55 char name[dns_protocol::kMaxNameLength]; |
54 size_t namelen = 0; /* <= sizeof name */ | 56 size_t namelen = 0; /* <= sizeof name */ |
55 char ch; | 57 char ch; |
56 bool valid_name = true; | 58 *valid_name = true; |
57 | 59 |
58 for (;;) { | 60 for (;;) { |
59 if (!n) | 61 if (!n) |
60 break; | 62 break; |
61 ch = *buf++; | 63 ch = *buf++; |
62 --n; | 64 --n; |
63 if (ch == '.') { | 65 if (ch == '.') { |
64 // Don't allow empty labels per http://crbug.com/456391. | 66 // Don't allow empty labels per http://crbug.com/456391. |
65 if (!labellen) | 67 if (!labellen) |
66 return false; | 68 return false; |
67 if (namelen + labellen + 1 > sizeof name) | 69 if (namelen + labellen + 1 > sizeof name) |
68 return false; | 70 return false; |
69 name[namelen++] = static_cast<char>(labellen); | 71 name[namelen++] = static_cast<char>(labellen); |
70 memcpy(name + namelen, label, labellen); | 72 memcpy(name + namelen, label, labellen); |
71 namelen += labellen; | 73 namelen += labellen; |
72 labellen = 0; | 74 labellen = 0; |
73 continue; | 75 continue; |
74 } | 76 } |
75 if (labellen >= sizeof label) | 77 if (labellen >= sizeof label) |
76 return false; | 78 return false; |
77 if (!IsValidHostLabelCharacter(ch, labellen == 0)) { | 79 if (!IsValidHostLabelCharacter(ch, labellen == 0)) { |
78 // TODO(palmer): In the future, when we can remove support for invalid | 80 // TODO(crbug.com/695474): In the future, when we can remove support for |
79 // names, return false here instead (and remove the Net.Valid*DNSName UMA | 81 // invalid names, return false here instead (and remove the UMA counter). |
80 // counters). | 82 // And remove the |valid_name| parameter, rename this function back to |
81 valid_name = false; | 83 // |DNSDomainFromDot|, and remove the helper function by that name |
| 84 // (below). |
| 85 *valid_name = false; |
82 } | 86 } |
83 label[labellen++] = ch; | 87 label[labellen++] = ch; |
84 } | 88 } |
85 | 89 |
86 UMA_HISTOGRAM_BOOLEAN("Net.ValidDNSName", valid_name); | 90 UMA_HISTOGRAM_BOOLEAN("Net.ValidDNSName", *valid_name); |
87 if (valid_name) { | 91 if (*valid_name) { |
88 url::CanonHostInfo info; | 92 url::CanonHostInfo info; |
89 UMA_HISTOGRAM_BOOLEAN("Net.DNSNameCompliantIfValid", | 93 UMA_HISTOGRAM_BOOLEAN("Net.DNSNameCompliantIfValid", |
90 net::IsCanonicalizedHostCompliant( | 94 net::IsCanonicalizedHostCompliant( |
91 net::CanonicalizeHost(dotted, &info))); | 95 net::CanonicalizeHost(dotted, &info))); |
92 } | 96 } |
93 | 97 |
94 // Allow empty label at end of name to disable suffix search. | 98 // Allow empty label at end of name to disable suffix search. |
95 if (labellen) { | 99 if (labellen) { |
96 if (namelen + labellen + 1 > sizeof name) | 100 if (namelen + labellen + 1 > sizeof name) |
97 return false; | 101 return false; |
98 name[namelen++] = static_cast<char>(labellen); | 102 name[namelen++] = static_cast<char>(labellen); |
99 memcpy(name + namelen, label, labellen); | 103 memcpy(name + namelen, label, labellen); |
100 namelen += labellen; | 104 namelen += labellen; |
101 labellen = 0; | 105 labellen = 0; |
102 } | 106 } |
103 | 107 |
104 if (namelen + 1 > sizeof name) | 108 if (namelen + 1 > sizeof name) |
105 return false; | 109 return false; |
106 if (namelen == 0) // Empty names e.g. "", "." are not valid. | 110 if (namelen == 0) // Empty names e.g. "", "." are not valid. |
107 return false; | 111 return false; |
108 name[namelen++] = 0; // This is the root label (of length 0). | 112 name[namelen++] = 0; // This is the root label (of length 0). |
109 | 113 |
110 *out = std::string(name, namelen); | 114 *out = std::string(name, namelen); |
111 return true; | 115 return true; |
112 } | 116 } |
113 | 117 |
| 118 bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { |
| 119 bool ignored; |
| 120 return DNSDomainFromDotWithValidityCheck(dotted, out, &ignored); |
| 121 } |
| 122 |
114 bool IsValidDNSDomain(const base::StringPiece& dotted) { | 123 bool IsValidDNSDomain(const base::StringPiece& dotted) { |
115 std::string dns_formatted; | 124 std::string dns_formatted; |
116 return DNSDomainFromDot(dotted, &dns_formatted); | 125 return DNSDomainFromDot(dotted, &dns_formatted); |
117 } | 126 } |
118 | 127 |
119 bool IsValidHostLabelCharacter(char c, bool is_first_char) { | 128 bool IsValidHostLabelCharacter(char c, bool is_first_char) { |
120 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | 129 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || |
121 (c >= '0' && c <= '9') || (!is_first_char && c == '-') || c == '_'; | 130 (c >= '0' && c <= '9') || (!is_first_char && c == '-') || c == '_'; |
122 } | 131 } |
123 | 132 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 return DELTA_IDENTICAL; | 214 return DELTA_IDENTICAL; |
206 else if (same_size && !any_missing) | 215 else if (same_size && !any_missing) |
207 return DELTA_REORDERED; | 216 return DELTA_REORDERED; |
208 else if (any_match) | 217 else if (any_match) |
209 return DELTA_OVERLAP; | 218 return DELTA_OVERLAP; |
210 else | 219 else |
211 return DELTA_DISJOINT; | 220 return DELTA_DISJOINT; |
212 } | 221 } |
213 | 222 |
214 } // namespace net | 223 } // namespace net |
OLD | NEW |