Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 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 SHILL_DEVICE_INFO_ |
| 6 | #define SHILL_DEVICE_INFO_ |
| 7 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 8 | #include <map> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 11 | #include <vector> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 12 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 13 | #include <base/callback.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 14 | #include <base/memory/ref_counted.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 15 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 16 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 17 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 18 | #include "shill/byte_string.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 19 | #include "shill/device.h" |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 20 | #include "shill/ip_address.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 21 | #include "shill/rtnl_listener.h" |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 22 | #include "shill/technology.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 23 | |
| 24 | namespace shill { |
| 25 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 26 | class Manager; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 27 | class Metrics; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 28 | class RTNLHandler; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 29 | class RTNLMessage; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 30 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 31 | class DeviceInfo { |
| 32 | public: |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 33 | struct AddressData { |
| 34 | AddressData() |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 35 | : address(IPAddress::kFamilyUnknown), flags(0), scope(0) {} |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 36 | AddressData(const IPAddress &address_in, |
| 37 | unsigned char flags_in, |
| 38 | unsigned char scope_in) |
| 39 | : address(address_in), flags(flags_in), scope(scope_in) {} |
| 40 | IPAddress address; |
| 41 | unsigned char flags; |
| 42 | unsigned char scope; |
| 43 | }; |
| 44 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 45 | DeviceInfo(ControlInterface *control_interface, |
| 46 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 47 | Metrics *metrics, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 48 | Manager *manager); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 49 | ~DeviceInfo(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 50 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 51 | void AddDeviceToBlackList(const std::string &device_name); |
Eric Shienbrood | 5e628a5 | 2012-03-21 16:56:59 -0400 | [diff] [blame] | 52 | bool IsDeviceBlackListed(const std::string &device_name); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 53 | void Start(); |
| 54 | void Stop(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 55 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 56 | // Adds |device| to this DeviceInfo instance so that we can handle its link |
| 57 | // messages, and registers it with the manager. |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 58 | virtual void RegisterDevice(const DeviceRefPtr &device); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 59 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 60 | // Remove |device| from this DeviceInfo. This function should only |
| 61 | // be called for cellular devices because the lifetime of the |
| 62 | // cellular devices is controlled by the Modem object and its |
| 63 | // communication to modem manager, rather than by RTNL messages. |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 64 | virtual void DeregisterDevice(const DeviceRefPtr &device); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 65 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 66 | virtual DeviceRefPtr GetDevice(int interface_index) const; |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 67 | virtual bool GetMACAddress(int interface_index, ByteString *address) const; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 68 | virtual bool GetFlags(int interface_index, unsigned int *flags) const; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 69 | virtual bool GetAddresses(int interface_index, |
| 70 | std::vector<AddressData> *addresses) const; |
| 71 | virtual void FlushAddresses(int interface_index) const; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 72 | virtual bool CreateTunnelInterface(std::string *interface_name) const; |
| 73 | virtual bool DeleteInterface(int interface_index) const; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 74 | |
| 75 | private: |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 76 | friend class DeviceInfoTest; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 77 | FRIEND_TEST(CellularTest, StartLinked); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 78 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 79 | struct Info { |
| 80 | Info() : flags(0) {} |
| 81 | |
| 82 | DeviceRefPtr device; |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 83 | ByteString mac_address; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 84 | std::vector<AddressData> ip_addresses; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 85 | unsigned int flags; |
| 86 | }; |
| 87 | |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 88 | static const char kInterfaceUevent[]; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 89 | static const char kInterfaceUeventWifiSignature[]; |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 90 | static const char kInterfaceDriver[]; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 91 | static const char kInterfaceTunFlags[]; |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 92 | static const char kInterfaceType[]; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 93 | static const char *kModemDrivers[]; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 94 | static const char kTunDeviceName[]; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 95 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 96 | static Technology::Identifier GetDeviceTechnology( |
| 97 | const std::string &face_name); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 98 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 99 | void AddLinkMsgHandler(const RTNLMessage &msg); |
| 100 | void DelLinkMsgHandler(const RTNLMessage &msg); |
| 101 | void LinkMsgHandler(const RTNLMessage &msg); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 102 | void AddressMsgHandler(const RTNLMessage &msg); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 103 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 104 | const Info *GetInfo(int interface_index) const; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 105 | void RemoveInfo(int interface_index); |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 106 | void EnableDeviceIPv6Privacy(const std::string &link_name); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 107 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 108 | ControlInterface *control_interface_; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 109 | EventDispatcher *dispatcher_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 110 | Metrics *metrics_; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 111 | Manager *manager_; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 112 | std::map<int, Info> infos_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 113 | base::Callback<void(const RTNLMessage &)> link_callback_; |
| 114 | base::Callback<void(const RTNLMessage &)> address_callback_; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 115 | scoped_ptr<RTNLListener> link_listener_; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 116 | scoped_ptr<RTNLListener> address_listener_; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 117 | std::set<std::string> black_list_; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 118 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 119 | // Cache copy of singleton |
| 120 | RTNLHandler *rtnl_handler_; |
| 121 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 122 | DISALLOW_COPY_AND_ASSIGN(DeviceInfo); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 125 | } // namespace shill |
| 126 | |
| 127 | #endif // SHILL_DEVICE_INFO_ |