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

Unified Diff: net/dns/dns_config_service_posix.cc

Issue 21368005: Detect domain-specific resolvers on OS X and disable DnsClient in such cases. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update net.gyp Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: net/dns/dns_config_service_posix.cc
diff --git a/net/dns/dns_config_service_posix.cc b/net/dns/dns_config_service_posix.cc
index ff2295e704a6aa3b1b6a80ac49d2d31185f9717f..fe08b54be5df54bac57e91a937e8dd0717bc2aa5 100644
--- a/net/dns/dns_config_service_posix.cc
+++ b/net/dns/dns_config_service_posix.cc
@@ -10,6 +10,7 @@
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/file_path_watcher.h"
+#include "base/lazy_instance.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
#include "base/time/time.h"
@@ -20,6 +21,61 @@
#include "net/dns/notify_watcher_mac.h"
#include "net/dns/serial_worker.h"
+#if defined(OS_MACOSX)
+#include <dlfcn.h>
+#include "third_party/apple_apsl/dnsinfo.h"
+namespace {
+
+// dnsinfo symbols are available via libSystem.dylib, but can also be present in
+// SystemConfiguration.framework. To avoid confusion, load them explicitly from
+// libSystem.dylib.
+class DnsInfoApi {
+ public:
+ typedef const char* (*dns_configuration_notify_key_t)();
+ typedef dns_config_t* (*dns_configuration_copy_t)();
+ typedef void (*dns_configuration_free_t)(dns_config_t*);
+
+ DnsInfoApi() {
+ handle_ = dlopen("/usr/lib/libSystem.dylib",
+ RTLD_LAZY | RTLD_NOLOAD);
+ if (!handle_) return;
+ dns_configuration_notify_key =
+ reinterpret_cast<dns_configuration_notify_key_t>(
+ dlsym(handle_, "dns_configuration_notify_key"));
+ dns_configuration_copy =
+ reinterpret_cast<dns_configuration_copy_t>(
+ dlsym(handle_, "dns_configuration_copy"));
+ dns_configuration_free =
+ reinterpret_cast<dns_configuration_free_t>(
+ dlsym(handle_, "dns_configuration_free"));
+ }
+ ~DnsInfoApi() {
+ if (handle_) dlclose(handle_);
+ }
+
+ dns_configuration_notify_key_t dns_configuration_notify_key;
+ dns_configuration_copy_t dns_configuration_copy;
+ dns_configuration_free_t dns_configuration_free;
+
+ private:
+ void* handle_;
+};
+
+const DnsInfoApi& dns_info_api() {
+ base::LazyInstance<DnsInfoApi>::Leaky api = LAZY_INSTANCE_INITIALIZER;
+ return api.Get();
+}
+
+struct DnsConfigTDeleter {
+ inline void operator()(dns_config_t* ptr) const {
+ DCHECK(dns_info_api().dns_configuration_free);
Mark Mentovai 2013/08/06 18:05:28 I think you’d want to be defensive and wrap the ne
szym 2013/08/06 22:14:19 Done.
+ dns_info_api().dns_configuration_free(ptr);
+ }
+};
+
+} // namespace
+#endif
+
namespace net {
#if !defined(OS_ANDROID)
@@ -31,14 +87,12 @@ const base::FilePath::CharType* kFilePathHosts =
FILE_PATH_LITERAL("/etc/hosts");
#if defined(OS_MACOSX)
-// From 10.7.3 configd-395.10/dnsinfo/dnsinfo.h
-static const char* kDnsNotifyKey =
- "com.apple.system.SystemConfiguration.dns_configuration";
-
class ConfigWatcher {
public:
bool Watch(const base::Callback<void(bool succeeded)>& callback) {
- return watcher_.Watch(kDnsNotifyKey, callback);
+ if (!dns_info_api().dns_configuration_notify_key) return false;
+ return watcher_.Watch(dns_info_api().dns_configuration_notify_key(),
+ callback);
}
private:
@@ -100,6 +154,31 @@ ConfigParsePosixResult ReadDnsConfig(DnsConfig* config) {
res_nclose(&res);
#endif
#endif
+
+#if defined(OS_MACOSX)
+ if (!dns_info_api().dns_configuration_copy)
+ return CONFIG_PARSE_POSIX_NO_DNSINFO;
+ scoped_ptr<dns_config_t, DnsConfigTDeleter> dns_config(
+ dns_info_api().dns_configuration_copy());
+ if (!dns_config)
+ return CONFIG_PARSE_POSIX_NO_DNSINFO;
+
+ // TODO(szym): Parse dns_config_t for resolvers rather than res_state.
+ // DnsClient can't handle domain-specific unscoped resolvers.
+ unsigned num_resolvers = 0;
+ for (int i = 0; i < dns_config->n_resolver; ++i) {
+ dns_resolver_t* resolver = dns_config->resolver[i];
+ if (!resolver->n_nameserver)
+ continue;
+ if (resolver->options && !strcmp(resolver->options, "mdns"))
Mark Mentovai 2013/08/06 18:05:28 Do we know anything about the “options” format? Is
szym 2013/08/06 22:14:19 http://opensource.apple.com/source/mDNSResponder/m
+ continue;
+ ++num_resolvers;
+ }
+ if (num_resolvers > 1) {
+ LOG(WARNING) << "dns_config has unhandled options!";
+ return CONFIG_PARSE_POSIX_UNHANDLED_OPTIONS;
+ }
+#endif
// Override timeout value to match default setting on Windows.
config->timeout = base::TimeDelta::FromSeconds(kDnsTimeoutSeconds);
return result;
@@ -171,8 +250,20 @@ class DnsConfigServicePosix::ConfigReader : public SerialWorker {
virtual void DoWork() OVERRIDE {
base::TimeTicks start_time = base::TimeTicks::Now();
+ dns_config_.unhandled_options = false;
ConfigParsePosixResult result = ReadDnsConfig(&dns_config_);
- success_ = (result == CONFIG_PARSE_POSIX_OK);
+ switch (result) {
+ case CONFIG_PARSE_POSIX_MISSING_OPTIONS:
+ case CONFIG_PARSE_POSIX_UNHANDLED_OPTIONS:
+ dns_config_.unhandled_options = true;
Mark Mentovai 2013/08/06 18:05:28 It seems like ReadDnsConfig should be responsible
szym 2013/08/06 22:14:19 Done, although I'm not sure it's easier to follow
+ // Fall through.
+ case CONFIG_PARSE_POSIX_OK:
+ success_ = true;
+ break;
+ default:
+ success_ = false;
+ break;
+ }
UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ConfigParsePosix",
result, CONFIG_PARSE_POSIX_MAX);
UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigParseResult", success_);

Powered by Google App Engine
This is Rietveld 408576698