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