Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 5 #ifndef NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| 6 #define NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 6 #define NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| 7 | 7 |
| 8 #include <sys/socket.h> // Needed to include netlink. | 8 #include <sys/socket.h> // Needed to include netlink. |
| 9 // Mask superfluous definition of |struct net|. This is fixed in Linux 2.6.38. | 9 // Mask superfluous definition of |struct net|. This is fixed in Linux 2.6.38. |
| 10 #define net net_kernel | 10 #define net net_kernel |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 | 58 |
| 59 AddressMap GetAddressMap() const; | 59 AddressMap GetAddressMap() const; |
| 60 | 60 |
| 61 // Returns set of interface indicies for online interfaces. | 61 // Returns set of interface indicies for online interfaces. |
| 62 base::hash_set<int> GetOnlineLinks() const; | 62 base::hash_set<int> GetOnlineLinks() const; |
| 63 | 63 |
| 64 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). | 64 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). |
| 65 // Safe to call from any thread, but will block until Init() has completed. | 65 // Safe to call from any thread, but will block until Init() has completed. |
| 66 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); | 66 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); |
| 67 | 67 |
| 68 // Returns the name for the interface with interface index |interface_index|. | |
| 69 // |buf| should be a pointer to an array of size IFNAMSIZ. The returned | |
| 70 // pointer | |
|
pauljensen
2015/01/26 18:09:35
why such a short line?
derekjchow1
2015/01/27 01:18:04
Done.
| |
| 71 // will point to |buf|. This function acts like if_indextoname which cannot be | |
| 72 // used as net/if.h cannot be mixed with linux/if.h. We'll stick with | |
| 73 // exclusively talking to the kernel and not the C library. | |
| 74 static char* GetInterfaceName(int interface_index, char* buf); | |
| 75 | |
| 68 private: | 76 private: |
| 69 friend class AddressTrackerLinuxTest; | 77 friend class AddressTrackerLinuxTest; |
| 70 | 78 |
| 71 // In tracking mode, holds |lock| while alive. In non-tracking mode, | 79 // In tracking mode, holds |lock| while alive. In non-tracking mode, |
| 72 // enforces single-threaded access. | 80 // enforces single-threaded access. |
| 73 class AddressTrackerAutoLock { | 81 class AddressTrackerAutoLock { |
| 74 public: | 82 public: |
| 75 AddressTrackerAutoLock(const AddressTrackerLinux& tracker, | 83 AddressTrackerAutoLock(const AddressTrackerLinux& tracker, |
| 76 base::Lock& lock); | 84 base::Lock& lock); |
| 77 ~AddressTrackerAutoLock(); | 85 ~AddressTrackerAutoLock(); |
| 78 | 86 |
| 79 private: | 87 private: |
| 80 const AddressTrackerLinux& tracker_; | 88 const AddressTrackerLinux& tracker_; |
| 81 base::Lock& lock_; | 89 base::Lock& lock_; |
| 82 DISALLOW_COPY_AND_ASSIGN(AddressTrackerAutoLock); | 90 DISALLOW_COPY_AND_ASSIGN(AddressTrackerAutoLock); |
| 83 }; | 91 }; |
| 84 | 92 |
| 85 // A function that returns the name of an interface given the interface index | 93 // A function that returns the name of an interface given the interface index |
| 86 // in |interface_index|. | 94 // in |interface_index|. |ifname| should be a buffer of size IFNAMSIZ. The |
| 87 typedef const char* (*GetInterfaceNameFunction)(int interface_index); | 95 // function should return a pointer to |ifname|. |
| 96 typedef char* (*GetInterfaceNameFunction)(int interface_index, char* ifname); | |
| 88 | 97 |
| 89 // Sets |*address_changed| to indicate whether |address_map_| changed and | 98 // Sets |*address_changed| to indicate whether |address_map_| changed and |
| 90 // sets |*link_changed| to indicate if |online_links_| changed and sets | 99 // sets |*link_changed| to indicate if |online_links_| changed and sets |
| 91 // |*tunnel_changed| to indicate if |online_links_| changed with regards to a | 100 // |*tunnel_changed| to indicate if |online_links_| changed with regards to a |
| 92 // tunnel interface while reading messages from |netlink_fd_|. | 101 // tunnel interface while reading messages from |netlink_fd_|. |
| 93 void ReadMessages(bool* address_changed, | 102 void ReadMessages(bool* address_changed, |
| 94 bool* link_changed, | 103 bool* link_changed, |
| 95 bool* tunnel_changed); | 104 bool* tunnel_changed); |
| 96 | 105 |
| 97 // Sets |*address_changed| to true if |address_map_| changed, sets | 106 // Sets |*address_changed| to true if |address_map_| changed, sets |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 110 // MessageLoopForIO::Watcher: | 119 // MessageLoopForIO::Watcher: |
| 111 void OnFileCanReadWithoutBlocking(int fd) override; | 120 void OnFileCanReadWithoutBlocking(int fd) override; |
| 112 void OnFileCanWriteWithoutBlocking(int /* fd */) override; | 121 void OnFileCanWriteWithoutBlocking(int /* fd */) override; |
| 113 | 122 |
| 114 // Close |netlink_fd_| | 123 // Close |netlink_fd_| |
| 115 void CloseSocket(); | 124 void CloseSocket(); |
| 116 | 125 |
| 117 // Does |msg| refer to a tunnel interface? | 126 // Does |msg| refer to a tunnel interface? |
| 118 bool IsTunnelInterface(const struct ifinfomsg* msg) const; | 127 bool IsTunnelInterface(const struct ifinfomsg* msg) const; |
| 119 | 128 |
| 129 // Updates current_connection_type_ based on the network list. | |
| 130 void UpdateCurrentConnectionType(); | |
| 131 | |
| 120 // Gets the name of an interface given the interface index |interface_index|. | 132 // Gets the name of an interface given the interface index |interface_index|. |
| 121 // May return empty string if it fails but should not return NULL. This is | 133 // May return empty string if it fails but should not return NULL. This is |
| 122 // overridden by tests. | 134 // overridden by tests. |
| 123 GetInterfaceNameFunction get_interface_name_; | 135 GetInterfaceNameFunction get_interface_name_; |
| 124 | 136 |
| 125 base::Closure address_callback_; | 137 base::Closure address_callback_; |
| 126 base::Closure link_callback_; | 138 base::Closure link_callback_; |
| 127 base::Closure tunnel_callback_; | 139 base::Closure tunnel_callback_; |
| 128 | 140 |
| 129 int netlink_fd_; | 141 int netlink_fd_; |
| 130 base::MessageLoopForIO::FileDescriptorWatcher watcher_; | 142 base::MessageLoopForIO::FileDescriptorWatcher watcher_; |
| 131 | 143 |
| 132 mutable base::Lock address_map_lock_; | 144 mutable base::Lock address_map_lock_; |
| 133 AddressMap address_map_; | 145 AddressMap address_map_; |
| 134 | 146 |
| 135 // Set of interface indices for links that are currently online. | 147 // Set of interface indices for links that are currently online. |
| 136 mutable base::Lock online_links_lock_; | 148 mutable base::Lock online_links_lock_; |
| 137 base::hash_set<int> online_links_; | 149 base::hash_set<int> online_links_; |
| 138 | 150 |
| 139 base::Lock is_offline_lock_; | 151 base::Lock connection_type_lock_; |
| 140 bool is_offline_; | 152 bool connection_type_initialized_; |
| 141 bool is_offline_initialized_; | 153 base::ConditionVariable connection_type_initialized_cv_; |
| 142 base::ConditionVariable is_offline_initialized_cv_; | 154 NetworkChangeNotifier::ConnectionType current_connection_type_; |
| 143 bool tracking_; | 155 bool tracking_; |
| 144 | 156 |
| 145 // Used to verify single-threaded access in non-tracking mode. | 157 // Used to verify single-threaded access in non-tracking mode. |
| 146 base::ThreadChecker thread_checker_; | 158 base::ThreadChecker thread_checker_; |
| 147 }; | 159 }; |
| 148 | 160 |
| 149 } // namespace internal | 161 } // namespace internal |
| 150 } // namespace net | 162 } // namespace net |
| 151 | 163 |
| 152 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 164 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| OLD | NEW |