diff options
author | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-16 22:27:24 +0000 |
---|---|---|
committer | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-16 22:27:24 +0000 |
commit | 671728091acbcbfa1ec4e2cffdaff112e43949a0 (patch) | |
tree | 44803eacadbf212e5def8ff4aaee23b747d0157e /net/base/address_tracker_linux.h | |
parent | 053566af2ee54e9b5cb0ef5d2255b62a3ae300de (diff) | |
download | chromium_src-671728091acbcbfa1ec4e2cffdaff112e43949a0.zip chromium_src-671728091acbcbfa1ec4e2cffdaff112e43949a0.tar.gz chromium_src-671728091acbcbfa1ec4e2cffdaff112e43949a0.tar.bz2 |
[net] Adds AddressTrackerLinux which keeps track of interface addresses using rtnetlink.
BUG=100690,113993
TEST=./net_unittests --gtest_filter=AddressTrackerLinuxTest.*
Review URL: https://chromiumcodereview.appspot.com/10689015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@146907 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/address_tracker_linux.h')
-rw-r--r-- | net/base/address_tracker_linux.h | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/net/base/address_tracker_linux.h b/net/base/address_tracker_linux.h new file mode 100644 index 0000000..a70a14c --- /dev/null +++ b/net/base/address_tracker_linux.h @@ -0,0 +1,68 @@ +// 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. + +#ifndef NET_BASE_ADDRESS_TRACKER_LINUX_H_ +#define NET_BASE_ADDRESS_TRACKER_LINUX_H_ + +#include <sys/socket.h> // Needed to include netlink. +// Mask superfluous definition of |struct net|. This is fixed in Linux 2.6.38. +#define net net_kernel +#include <linux/rtnetlink.h> +#undef net + +#include <map> + +#include "base/basictypes.h" +#include "base/callback.h" +#include "base/compiler_specific.h" +#include "base/message_loop.h" +#include "base/synchronization/lock.h" +#include "net/base/net_util.h" + +namespace net { +namespace internal { + +// Keeps track of network interface addresses using rtnetlink. Used by +// NetworkChangeNotifier to provide signals to registered IPAddressObservers. +class NET_EXPORT_PRIVATE AddressTrackerLinux + : public MessageLoopForIO::Watcher { + public: + typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap; + + // Will run |callback| when the AddressMap changes. + explicit AddressTrackerLinux(const base::Closure& callback); + virtual ~AddressTrackerLinux(); + + // Starts watching system configuration for changes. The current thread must + // have a MessageLoopForIO. + void Init(); + + AddressMap GetAddressMap() const; + + private: + friend class AddressTrackerLinuxTest; + + // Returns true if |map_| changed while reading messages from |netlink_fd_|. + bool ReadMessages(); + + // Returns true if |map_| changed while reading the message from |buffer|. + bool HandleMessage(const char* buffer, size_t length); + + // MessageLoopForIO::Watcher: + virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; + virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE; + + base::Closure callback_; + + int netlink_fd_; + MessageLoopForIO::FileDescriptorWatcher watcher_; + + mutable base::Lock lock_; + AddressMap map_; +}; + +} // namespace internal +} // namespace net + +#endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ |