OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| 6 #define NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| 7 |
| 8 #include <sys/socket.h> // Needed to include netlink. |
| 9 // Mask superfluous definition of |struct net|. This is fixed in Linux 2.6.38. |
| 10 #define net net_kernel |
| 11 #include <linux/rtnetlink.h> |
| 12 #undef net |
| 13 |
| 14 #include <map> |
| 15 |
| 16 #include "base/basictypes.h" |
| 17 #include "base/callback.h" |
| 18 #include "base/compiler_specific.h" |
| 19 #include "base/message_loop.h" |
| 20 #include "base/synchronization/lock.h" |
| 21 #include "net/base/net_util.h" |
| 22 |
| 23 namespace net { |
| 24 namespace internal { |
| 25 |
| 26 // Keeps track of network interface addresses using rtnetlink. Used by |
| 27 // NetworkChangeNotifier to provide signals to registered IPAddressObservers. |
| 28 class NET_EXPORT_PRIVATE AddressTrackerLinux |
| 29 : public MessageLoopForIO::Watcher { |
| 30 public: |
| 31 typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap; |
| 32 |
| 33 // Will run |callback| when the AddressMap changes. |
| 34 explicit AddressTrackerLinux(const base::Closure& callback); |
| 35 virtual ~AddressTrackerLinux(); |
| 36 |
| 37 // Starts watching system configuration for changes. The current thread must |
| 38 // have a MessageLoopForIO. |
| 39 void Init(); |
| 40 |
| 41 AddressMap GetAddressMap() const; |
| 42 |
| 43 private: |
| 44 friend class AddressTrackerLinuxTest; |
| 45 |
| 46 // Returns true if |map_| changed while reading messages from |netlink_fd_|. |
| 47 bool ReadMessages(); |
| 48 |
| 49 // Returns true if |map_| changed while reading the message from |buffer|. |
| 50 bool HandleMessage(const char* buffer, size_t length); |
| 51 |
| 52 // MessageLoopForIO::Watcher: |
| 53 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 54 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE; |
| 55 |
| 56 base::Closure callback_; |
| 57 |
| 58 int netlink_fd_; |
| 59 MessageLoopForIO::FileDescriptorWatcher watcher_; |
| 60 |
| 61 mutable base::Lock lock_; |
| 62 AddressMap map_; |
| 63 }; |
| 64 |
| 65 } // namespace internal |
| 66 } // namespace net |
| 67 |
| 68 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
OLD | NEW |