summaryrefslogtreecommitdiffstats
path: root/net/tools/net_watcher
diff options
context:
space:
mode:
Diffstat (limited to 'net/tools/net_watcher')
-rw-r--r--net/tools/net_watcher/net_watcher.cc205
1 files changed, 205 insertions, 0 deletions
diff --git a/net/tools/net_watcher/net_watcher.cc b/net/tools/net_watcher/net_watcher.cc
new file mode 100644
index 0000000..e1e359d
--- /dev/null
+++ b/net/tools/net_watcher/net_watcher.cc
@@ -0,0 +1,205 @@
+// Copyright (c) 2012 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.
+
+// This is a small utility that watches for and logs network changes.
+
+#include <string>
+
+#include "base/at_exit.h"
+#include "base/basictypes.h"
+#include "base/command_line.h"
+#include "base/compiler_specific.h"
+#include "base/json/json_writer.h"
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop.h"
+#include "base/values.h"
+#include "build/build_config.h"
+#include "net/base/network_change_notifier.h"
+#include "net/proxy/proxy_config.h"
+#include "net/proxy/proxy_config_service.h"
+#include "net/proxy/proxy_service.h"
+
+#if defined(OS_MACOSX)
+#include "base/mac/scoped_nsautorelease_pool.h"
+#endif
+
+namespace {
+
+// Conversions from various network-related types to string.
+
+const char* ConnectionTypeToString(
+ net::NetworkChangeNotifier::ConnectionType type) {
+ switch (type) {
+ case net::NetworkChangeNotifier::CONNECTION_UNKNOWN:
+ return "CONNECTION_UNKNOWN";
+ case net::NetworkChangeNotifier::CONNECTION_ETHERNET:
+ return "CONNECTION_ETHERNET";
+ case net::NetworkChangeNotifier::CONNECTION_WIFI:
+ return "CONNECTION_WIFI";
+ case net::NetworkChangeNotifier::CONNECTION_2G:
+ return "CONNECTION_2G";
+ case net::NetworkChangeNotifier::CONNECTION_3G:
+ return "CONNECTION_3G";
+ case net::NetworkChangeNotifier::CONNECTION_4G:
+ return "CONNECTION_4G";
+ case net::NetworkChangeNotifier::CONNECTION_NONE:
+ return "CONNECTION_NONE";
+ default:
+ return "CONNECTION_UNEXPECTED";
+ }
+}
+
+std::string DNSDetailToString(unsigned detail) {
+ const char kSeparator[] = " | ";
+ std::string detail_str;
+ if (detail & net::NetworkChangeNotifier::CHANGE_DNS_SETTINGS)
+ detail_str += "CHANGE_DNS_SETTINGS";
+ if (detail & net::NetworkChangeNotifier::CHANGE_DNS_HOSTS) {
+ if (!detail_str.empty())
+ detail_str += kSeparator;
+ detail_str += "CHANGE_DNS_HOSTS";
+ }
+ if (detail & net::NetworkChangeNotifier::CHANGE_DNS_WATCH_STARTED) {
+ if (!detail_str.empty())
+ detail_str += kSeparator;
+ detail_str += "CHANGE_DNS_WATCH_STARTED";
+ }
+ if (detail & net::NetworkChangeNotifier::CHANGE_DNS_WATCH_FAILED) {
+ if (!detail_str.empty())
+ detail_str += kSeparator;
+ detail_str += "CHANGE_DNS_WATCH_FAILED";
+ }
+ return detail_str;
+}
+
+std::string ProxyConfigToString(const net::ProxyConfig& config) {
+ scoped_ptr<base::Value> config_value(config.ToValue());
+ std::string str;
+ base::JSONWriter::Write(config_value.get(), &str);
+ return str;
+}
+
+const char* ConfigAvailabilityToString(
+ net::ProxyConfigService::ConfigAvailability availability) {
+ switch (availability) {
+ case net::ProxyConfigService::CONFIG_PENDING:
+ return "CONFIG_PENDING";
+ case net::ProxyConfigService::CONFIG_VALID:
+ return "CONFIG_VALID";
+ case net::ProxyConfigService::CONFIG_UNSET:
+ return "CONFIG_UNSET";
+ default:
+ return "CONFIG_UNEXPECTED";
+ }
+}
+
+// The main observer class that logs network events.
+class NetWatcher :
+ public net::NetworkChangeNotifier::IPAddressObserver,
+ public net::NetworkChangeNotifier::ConnectionTypeObserver,
+ public net::NetworkChangeNotifier::DNSObserver,
+ public net::ProxyConfigService::Observer {
+ public:
+ NetWatcher() {}
+
+ virtual ~NetWatcher() {}
+
+ // net::NetworkChangeNotifier::IPAddressObserver implementation.
+ virtual void OnIPAddressChanged() OVERRIDE {
+ LOG(INFO) << "OnIPAddressChanged()";
+ }
+
+ // net::NetworkChangeNotifier::ConnectionTypeObserver implementation.
+ virtual void OnConnectionTypeChanged(
+ net::NetworkChangeNotifier::ConnectionType type) OVERRIDE {
+ LOG(INFO) << "OnConnectionTypeChanged("
+ << ConnectionTypeToString(type) << ")";
+ }
+
+ // net::NetworkChangeNotifier::DNSObserver implementation.
+ virtual void OnDNSChanged(unsigned detail) OVERRIDE {
+ LOG(INFO) << "OnDNSChanged(" << DNSDetailToString(detail) << ")";
+ }
+
+ // net::ProxyConfigService::Observer implementation.
+ virtual void OnProxyConfigChanged(
+ const net::ProxyConfig& config,
+ net::ProxyConfigService::ConfigAvailability availability) OVERRIDE {
+ LOG(INFO) << "OnProxyConfigChanged("
+ << ProxyConfigToString(config) << ", "
+ << ConfigAvailabilityToString(availability) << ")";
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(NetWatcher);
+};
+
+} // namespace
+
+int main(int argc, char* argv[]) {
+#if defined(OS_MACOSX)
+ base::mac::ScopedNSAutoreleasePool pool;
+#endif
+ base::AtExitManager exit_manager;
+ CommandLine::Init(argc, argv);
+ logging::InitLogging(
+ NULL,
+ logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
+ logging::LOCK_LOG_FILE,
+ logging::DELETE_OLD_LOG_FILE,
+ logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
+
+ // Just make the main message loop the network loop.
+ MessageLoopForIO network_loop;
+
+ NetWatcher net_watcher;
+
+ scoped_ptr<net::NetworkChangeNotifier> network_change_notifier(
+ net::NetworkChangeNotifier::Create());
+
+ // Use the network loop as the file loop also.
+ scoped_ptr<net::ProxyConfigService> proxy_config_service(
+ net::ProxyService::CreateSystemProxyConfigService(
+ network_loop.message_loop_proxy(),
+ &network_loop));
+
+ network_change_notifier->AddIPAddressObserver(&net_watcher);
+ network_change_notifier->AddConnectionTypeObserver(&net_watcher);
+ network_change_notifier->AddDNSObserver(&net_watcher);
+
+ proxy_config_service->AddObserver(&net_watcher);
+
+ LOG(INFO) << "Initial connection type: "
+ << ConnectionTypeToString(
+ network_change_notifier->GetCurrentConnectionType());
+
+ {
+ net::ProxyConfig config;
+ const net::ProxyConfigService::ConfigAvailability availability =
+ proxy_config_service->GetLatestProxyConfig(&config);
+ LOG(INFO) << "Initial proxy config: "
+ << ProxyConfigToString(config) << ", "
+ << ConfigAvailabilityToString(availability);
+ }
+
+ LOG(INFO) << "Watching for network events...";
+
+ if (net::NetworkChangeNotifier::IsWatchingDNS()) {
+ LOG(INFO) << "Watching for DNS changes...";
+ } else {
+ LOG(INFO) << "Not watching for DNS changes";
+ }
+
+ // Start watching for events.
+ network_loop.Run();
+
+ proxy_config_service->RemoveObserver(&net_watcher);
+
+ network_change_notifier->RemoveDNSObserver(&net_watcher);
+ network_change_notifier->RemoveConnectionTypeObserver(&net_watcher);
+ network_change_notifier->RemoveIPAddressObserver(&net_watcher);
+
+ return 0;
+}