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

Unified Diff: net/base/network_change_notifier_linux.cc

Issue 10689015: [net] Adds AddressTrackerLinux which keeps track of interface addresses using rtnetlink. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added NET_EXPORT_PRIVATE for tests. Created 8 years, 5 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
« no previous file with comments | « net/base/network_change_notifier_linux.h ('k') | net/base/network_change_notifier_netlink_linux.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/network_change_notifier_linux.cc
diff --git a/net/base/network_change_notifier_linux.cc b/net/base/network_change_notifier_linux.cc
index b7acd468aedca8dd5757884e7a85ef336fc4078d..1638f974fc97255530aec93a9067c4844e6d69ad 100644
--- a/net/base/network_change_notifier_linux.cc
+++ b/net/base/network_change_notifier_linux.cc
@@ -9,15 +9,12 @@
#include "net/base/network_change_notifier_linux.h"
-#include <errno.h>
#include <resolv.h>
-#include <sys/socket.h>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
-#include "base/eintr_wrapper.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
@@ -27,16 +24,14 @@
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
+#include "net/base/address_tracker_linux.h"
#include "net/base/net_errors.h"
-#include "net/base/network_change_notifier_netlink_linux.h"
#include "net/dns/dns_config_watcher.h"
namespace net {
namespace {
-const int kInvalidSocket = -1;
-
const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager";
const char kNetworkManagerPath[] = "/org/freedesktop/NetworkManager";
const char kNetworkManagerInterface[] = "org.freedesktop.NetworkManager";
@@ -243,56 +238,45 @@ NetworkManagerApi::GetCurrentConnectionType() {
NetworkChangeNotifier::CONNECTION_UNKNOWN;
}
-class NetworkChangeNotifierLinux::Thread
- : public base::Thread, public MessageLoopForIO::Watcher {
+class NetworkChangeNotifierLinux::Thread : public base::Thread {
public:
explicit Thread(dbus::Bus* bus);
virtual ~Thread();
- // MessageLoopForIO::Watcher:
- virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
- virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE;
-
// Plumbing for NetworkChangeNotifier::GetCurrentConnectionType.
// Safe to call from any thread.
NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() {
return network_manager_api_.GetCurrentConnectionType();
}
+ const internal::AddressTrackerLinux* address_tracker() const {
+ return &address_tracker_;
+ }
+
protected:
// base::Thread
virtual void Init() OVERRIDE;
virtual void CleanUp() OVERRIDE;
private:
- // Starts listening for netlink messages. Also handles the messages if there
- // are any available on the netlink socket.
- void ListenForNotifications();
-
- // Attempts to read from the netlink socket into |buf| of length |len|.
- // Returns the bytes read on synchronous success and ERR_IO_PENDING if the
- // recv() would block. Otherwise, it returns a net error code.
- int ReadNotificationMessage(char* buf, size_t len);
-
- // The netlink socket descriptor.
- int netlink_fd_;
- MessageLoopForIO::FileDescriptorWatcher netlink_watcher_;
-
// Used to detect online/offline state changes.
NetworkManagerApi network_manager_api_;
internal::DnsConfigWatcher dns_watcher_;
+ internal::AddressTrackerLinux address_tracker_;
DISALLOW_COPY_AND_ASSIGN(Thread);
};
NetworkChangeNotifierLinux::Thread::Thread(dbus::Bus* bus)
: base::Thread("NetworkChangeNotifier"),
- netlink_fd_(kInvalidSocket),
network_manager_api_(
base::Bind(&NetworkChangeNotifier::
NotifyObserversOfConnectionTypeChange),
- bus) {
+ bus),
+ address_tracker_(
+ base::Bind(&NetworkChangeNotifier::
+ NotifyObserversOfIPAddressChange)) {
}
NetworkChangeNotifierLinux::Thread::~Thread() {
@@ -300,77 +284,16 @@ NetworkChangeNotifierLinux::Thread::~Thread() {
}
void NetworkChangeNotifierLinux::Thread::Init() {
- netlink_fd_ = InitializeNetlinkSocket();
- if (netlink_fd_ < 0) {
- netlink_fd_ = kInvalidSocket;
- return;
- }
- ListenForNotifications();
-
network_manager_api_.Init();
-
dns_watcher_.Init();
+ address_tracker_.Init();
}
void NetworkChangeNotifierLinux::Thread::CleanUp() {
- if (netlink_fd_ != kInvalidSocket) {
- if (HANDLE_EINTR(close(netlink_fd_)) != 0)
- PLOG(ERROR) << "Failed to close socket";
- netlink_fd_ = kInvalidSocket;
- netlink_watcher_.StopWatchingFileDescriptor();
- }
network_manager_api_.CleanUp();
-
dns_watcher_.CleanUp();
}
-void NetworkChangeNotifierLinux::Thread::OnFileCanReadWithoutBlocking(int fd) {
- DCHECK_EQ(fd, netlink_fd_);
- ListenForNotifications();
-}
-
-void NetworkChangeNotifierLinux::Thread::OnFileCanWriteWithoutBlocking(
- int /* fd */) {
- NOTREACHED();
-}
-
-void NetworkChangeNotifierLinux::Thread::ListenForNotifications() {
- char buf[4096];
- int rv = ReadNotificationMessage(buf, arraysize(buf));
- while (rv > 0) {
- if (HandleNetlinkMessage(buf, rv)) {
- VLOG(1) << "Detected IP address changes.";
- NotifyObserversOfIPAddressChange();
- }
- rv = ReadNotificationMessage(buf, arraysize(buf));
- }
-
- if (rv == ERR_IO_PENDING) {
- rv = MessageLoopForIO::current()->WatchFileDescriptor(netlink_fd_, false,
- MessageLoopForIO::WATCH_READ, &netlink_watcher_, this);
- LOG_IF(ERROR, !rv) << "Failed to watch netlink socket: " << netlink_fd_;
- }
-}
-
-int NetworkChangeNotifierLinux::Thread::ReadNotificationMessage(
- char* buf,
- size_t len) {
- DCHECK_NE(len, 0u);
- DCHECK(buf);
- memset(buf, 0, len);
- int rv = recv(netlink_fd_, buf, len, 0);
- if (rv > 0)
- return rv;
-
- DCHECK_NE(rv, 0);
- if (errno != EAGAIN && errno != EWOULDBLOCK) {
- PLOG(DFATAL) << "recv";
- return ERR_FAILED;
- }
-
- return ERR_IO_PENDING;
-}
-
NetworkChangeNotifierLinux* NetworkChangeNotifierLinux::Create() {
return new NetworkChangeNotifierLinux(NULL);
}
@@ -400,4 +323,9 @@ NetworkChangeNotifierLinux::GetCurrentConnectionType() const {
return notifier_thread_->GetCurrentConnectionType();
}
+const internal::AddressTrackerLinux*
+NetworkChangeNotifierLinux::GetAddressTrackerInternal() const {
+ return notifier_thread_->address_tracker();
+}
+
} // namespace net
« no previous file with comments | « net/base/network_change_notifier_linux.h ('k') | net/base/network_change_notifier_netlink_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698