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

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: Wrapped HANDLE_EINTR(close) into CloseSocket. Created 8 years, 6 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/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..416efccf186118a8412e7dd17e51cc712a20adac 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,16 +238,11 @@ 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() {
@@ -265,30 +255,17 @@ class NetworkChangeNotifierLinux::Thread
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),
@@ -300,77 +277,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);
}

Powered by Google App Engine
This is Rietveld 408576698