| 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> |
| 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 #include "net/dns/dns_protocol.h" | 18 #include "net/dns/dns_protocol.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // RFC 1035, section 2.3.4: labels 63 octets or less. | 22 // RFC 1035, section 2.3.4: labels 63 octets or less. |
| 22 // Section 3.1: Each label is represented as a one octet length field followed | 23 // Section 3.1: Each label is represented as a one octet length field followed |
| 23 // by that number of octets. | 24 // by that number of octets. |
| 24 const int kMaxLabelLength = 63; | 25 const int kMaxLabelLength = 63; |
| 25 | 26 |
| 26 } // namespace | 27 } // namespace |
| 27 | 28 |
| 29 #if defined(OS_POSIX) |
| 30 #include <netinet/in.h> |
| 31 #if !defined(OS_NACL) |
| 32 #include <net/if.h> |
| 33 #if !defined(OS_ANDROID) |
| 34 #include <ifaddrs.h> |
| 35 #endif // !defined(OS_ANDROID) |
| 36 #endif // !defined(OS_NACL) |
| 37 #endif // defined(OS_POSIX) |
| 38 |
| 39 #if defined(OS_ANDROID) |
| 40 #include "net/android/network_library.h" |
| 41 #endif |
| 42 |
| 28 namespace net { | 43 namespace net { |
| 29 | 44 |
| 30 // Based on DJB's public domain code. | 45 // Based on DJB's public domain code. |
| 31 bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { | 46 bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { |
| 32 const char* buf = dotted.data(); | 47 const char* buf = dotted.data(); |
| 33 unsigned n = dotted.size(); | 48 size_t n = dotted.size(); |
| 34 char label[kMaxLabelLength]; | 49 char label[kMaxLabelLength]; |
| 35 size_t labellen = 0; /* <= sizeof label */ | 50 size_t labellen = 0; /* <= sizeof label */ |
| 36 char name[dns_protocol::kMaxNameLength]; | 51 char name[dns_protocol::kMaxNameLength]; |
| 37 size_t namelen = 0; /* <= sizeof name */ | 52 size_t namelen = 0; /* <= sizeof name */ |
| 38 char ch; | 53 char ch; |
| 54 bool valid_name = true; |
| 39 | 55 |
| 40 for (;;) { | 56 for (;;) { |
| 41 if (!n) | 57 if (!n) |
| 42 break; | 58 break; |
| 43 ch = *buf++; | 59 ch = *buf++; |
| 44 --n; | 60 --n; |
| 45 if (ch == '.') { | 61 if (ch == '.') { |
| 46 // Don't allow empty labels per http://crbug.com/456391. | 62 // Don't allow empty labels per http://crbug.com/456391. |
| 47 if (!labellen) | 63 if (!labellen) |
| 48 return false; | 64 return false; |
| 49 if (namelen + labellen + 1 > sizeof name) | 65 if (namelen + labellen + 1 > sizeof name) |
| 50 return false; | 66 return false; |
| 51 name[namelen++] = static_cast<char>(labellen); | 67 name[namelen++] = static_cast<char>(labellen); |
| 52 memcpy(name + namelen, label, labellen); | 68 memcpy(name + namelen, label, labellen); |
| 53 namelen += labellen; | 69 namelen += labellen; |
| 54 labellen = 0; | 70 labellen = 0; |
| 55 continue; | 71 continue; |
| 56 } | 72 } |
| 57 if (labellen >= sizeof label) | 73 if (labellen >= sizeof label) |
| 58 return false; | 74 return false; |
| 75 if (!IsValidHostLabelCharacter(ch, labellen == 0)) { |
| 76 // TODO(palmer): In the future, when we can remove support for invalid |
| 77 // names, return false here instead (and remove the UMA counter). |
| 78 valid_name = false; |
| 79 } |
| 59 label[labellen++] = ch; | 80 label[labellen++] = ch; |
| 60 } | 81 } |
| 61 | 82 |
| 83 UMA_HISTOGRAM_BOOLEAN("Net.ValidDNSName", valid_name); |
| 84 |
| 62 // Allow empty label at end of name to disable suffix search. | 85 // Allow empty label at end of name to disable suffix search. |
| 63 if (labellen) { | 86 if (labellen) { |
| 64 if (namelen + labellen + 1 > sizeof name) | 87 if (namelen + labellen + 1 > sizeof name) |
| 65 return false; | 88 return false; |
| 66 name[namelen++] = static_cast<char>(labellen); | 89 name[namelen++] = static_cast<char>(labellen); |
| 67 memcpy(name + namelen, label, labellen); | 90 memcpy(name + namelen, label, labellen); |
| 68 namelen += labellen; | 91 namelen += labellen; |
| 69 labellen = 0; | 92 labellen = 0; |
| 70 } | 93 } |
| 71 | 94 |
| 72 if (namelen + 1 > sizeof name) | 95 if (namelen + 1 > sizeof name) |
| 73 return false; | 96 return false; |
| 74 if (namelen == 0) // Empty names e.g. "", "." are not valid. | 97 if (namelen == 0) // Empty names e.g. "", "." are not valid. |
| 75 return false; | 98 return false; |
| 76 name[namelen++] = 0; // This is the root label (of length 0). | 99 name[namelen++] = 0; // This is the root label (of length 0). |
| 77 | 100 |
| 78 *out = std::string(name, namelen); | 101 *out = std::string(name, namelen); |
| 79 return true; | 102 return true; |
| 80 } | 103 } |
| 81 | 104 |
| 82 bool IsValidDNSDomain(const base::StringPiece& dotted) { | 105 bool IsValidDNSDomain(const base::StringPiece& dotted) { |
| 83 std::string dns_formatted; | 106 std::string dns_formatted; |
| 84 return DNSDomainFromDot(dotted, &dns_formatted); | 107 return DNSDomainFromDot(dotted, &dns_formatted); |
| 85 } | 108 } |
| 86 | 109 |
| 110 bool IsValidHostLabelCharacter(char c, bool is_first_char) { |
| 111 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || |
| 112 (c >= '0' && c <= '9') || (!is_first_char && c == '-') || c == '_'; |
| 113 } |
| 114 |
| 87 std::string DNSDomainToString(const base::StringPiece& domain) { | 115 std::string DNSDomainToString(const base::StringPiece& domain) { |
| 88 std::string ret; | 116 std::string ret; |
| 89 | 117 |
| 90 for (unsigned i = 0; i < domain.size() && domain[i]; i += domain[i] + 1) { | 118 for (unsigned i = 0; i < domain.size() && domain[i]; i += domain[i] + 1) { |
| 91 #if CHAR_MIN < 0 | 119 #if CHAR_MIN < 0 |
| 92 if (domain[i] < 0) | 120 if (domain[i] < 0) |
| 93 return std::string(); | 121 return std::string(); |
| 94 #endif | 122 #endif |
| 95 if (domain[i] > kMaxLabelLength) | 123 if (domain[i] > kMaxLabelLength) |
| 96 return std::string(); | 124 return std::string(); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 return DELTA_IDENTICAL; | 196 return DELTA_IDENTICAL; |
| 169 else if (same_size && !any_missing) | 197 else if (same_size && !any_missing) |
| 170 return DELTA_REORDERED; | 198 return DELTA_REORDERED; |
| 171 else if (any_match) | 199 else if (any_match) |
| 172 return DELTA_OVERLAP; | 200 return DELTA_OVERLAP; |
| 173 else | 201 else |
| 174 return DELTA_DISJOINT; | 202 return DELTA_DISJOINT; |
| 175 } | 203 } |
| 176 | 204 |
| 177 } // namespace net | 205 } // namespace net |
| OLD | NEW |