forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_util.cc
205 lines (174 loc) · 5.46 KB
/
dns_util.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/dns_util.h"
#include <errno.h>
#include <limits.h>
#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"
#include "net/dns/dns_protocol.h"
namespace {
// RFC 1035, section 2.3.4: labels 63 octets or less.
// Section 3.1: Each label is represented as a one octet length field followed
// by that number of octets.
const int kMaxLabelLength = 63;
} // namespace
#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();
size_t n = dotted.size();
char label[kMaxLabelLength];
size_t labellen = 0; /* <= sizeof label */
char name[dns_protocol::kMaxNameLength];
size_t namelen = 0; /* <= sizeof name */
char ch;
bool valid_name = true;
for (;;) {
if (!n)
break;
ch = *buf++;
--n;
if (ch == '.') {
// Don't allow empty labels per http://crbug.com/456391.
if (!labellen)
return false;
if (namelen + labellen + 1 > sizeof name)
return false;
name[namelen++] = static_cast<char>(labellen);
memcpy(name + namelen, label, labellen);
namelen += labellen;
labellen = 0;
continue;
}
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;
}
UMA_HISTOGRAM_BOOLEAN("Net.ValidDNSName", valid_name);
// Allow empty label at end of name to disable suffix search.
if (labellen) {
if (namelen + labellen + 1 > sizeof name)
return false;
name[namelen++] = static_cast<char>(labellen);
memcpy(name + namelen, label, labellen);
namelen += labellen;
labellen = 0;
}
if (namelen + 1 > sizeof name)
return false;
if (namelen == 0) // Empty names e.g. "", "." are not valid.
return false;
name[namelen++] = 0; // This is the root label (of length 0).
*out = std::string(name, namelen);
return true;
}
bool IsValidDNSDomain(const base::StringPiece& dotted) {
std::string dns_formatted;
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;
for (unsigned i = 0; i < domain.size() && domain[i]; i += domain[i] + 1) {
#if CHAR_MIN < 0
if (domain[i] < 0)
return std::string();
#endif
if (domain[i] > kMaxLabelLength)
return std::string();
if (i)
ret += ".";
if (static_cast<unsigned>(domain[i]) + i + 1 > domain.size())
return std::string();
domain.substr(i + 1, domain[i]).AppendToString(&ret);
}
return ret;
}
#if !defined(OS_NACL)
namespace {
bool GetTimeDeltaForConnectionTypeFromFieldTrial(
const char* field_trial,
NetworkChangeNotifier::ConnectionType type,
base::TimeDelta* out) {
std::string group = base::FieldTrialList::FindFullName(field_trial);
if (group.empty())
return false;
std::vector<base::StringPiece> group_parts = base::SplitStringPiece(
group, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (type < 0)
return false;
size_t type_size = static_cast<size_t>(type);
if (type_size >= group_parts.size())
return false;
int64_t ms;
if (!base::StringToInt64(group_parts[type_size], &ms))
return false;
*out = base::TimeDelta::FromMilliseconds(ms);
return true;
}
} // namespace
base::TimeDelta GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault(
const char* field_trial,
base::TimeDelta default_delta,
NetworkChangeNotifier::ConnectionType type) {
base::TimeDelta out;
if (!GetTimeDeltaForConnectionTypeFromFieldTrial(field_trial, type, &out))
out = default_delta;
return out;
}
#endif // !defined(OS_NACL)
AddressListDeltaType FindAddressListDeltaType(const AddressList& a,
const AddressList& b) {
bool pairwise_mismatch = false;
bool any_match = false;
bool any_missing = false;
bool same_size = a.size() == b.size();
for (size_t i = 0; i < a.size(); ++i) {
bool this_match = false;
for (size_t j = 0; j < b.size(); ++j) {
if (a[i] == b[j]) {
any_match = true;
this_match = true;
} else if (i == j) {
pairwise_mismatch = true;
}
}
if (!this_match)
any_missing = true;
}
if (same_size && !pairwise_mismatch)
return DELTA_IDENTICAL;
else if (same_size && !any_missing)
return DELTA_REORDERED;
else if (any_match)
return DELTA_OVERLAP;
else
return DELTA_DISJOINT;
}
} // namespace net