Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS 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 SHILL_IPCONFIG_ |
| 6 | #define SHILL_IPCONFIG_ |
| 7 | |
| 8 | #include <string> |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 9 | #include <vector> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 10 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame^] | 11 | #include <base/callback_old.h> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 12 | #include <base/memory/ref_counted.h> |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame^] | 13 | #include <base/memory/scoped_ptr.h> |
| 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 15 | |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 16 | #include "shill/device.h" |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 17 | |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 18 | namespace shill { |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 19 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame^] | 20 | class IPConfig; |
| 21 | |
| 22 | typedef scoped_refptr<IPConfig> IPConfigRefPtr; |
| 23 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 24 | // IPConfig superclass. Individual IP configuration types will inherit from this |
| 25 | // class. |
| 26 | class IPConfig : public base::RefCounted<IPConfig> { |
| 27 | public: |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 28 | struct Properties { |
| 29 | Properties() : subnet_cidr(0), mtu(0) {} |
| 30 | |
| 31 | std::string address; |
| 32 | int subnet_cidr; |
| 33 | std::string broadcast_address; |
| 34 | std::string gateway; |
| 35 | std::vector<std::string> dns_servers; |
| 36 | std::string domain_name; |
| 37 | std::vector<std::string> domain_search; |
| 38 | int mtu; |
| 39 | }; |
| 40 | |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 41 | explicit IPConfig(DeviceConstRefPtr device); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 42 | virtual ~IPConfig(); |
| 43 | |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 44 | DeviceConstRefPtr device() const { return device_; } |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 45 | const std::string &GetDeviceName() const; |
| 46 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame^] | 47 | // Registers a callback that's executed every time the configuration |
| 48 | // properties change. Takes ownership of |callback|. Pass NULL to remove a |
| 49 | // callback. The callback's argument is a pointer to this IP configuration |
| 50 | // instance allowing clients to more easily manage multiple IP configurations. |
| 51 | void RegisterUpdateCallback(Callback1<IPConfigRefPtr>::Type *callback); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 52 | |
| 53 | const Properties &properties() const { return properties_; } |
| 54 | |
| 55 | // Request or renew IP configuration. Return true on success, false |
| 56 | // otherwise. The default implementation always returns false indicating a |
| 57 | // failure. |
| 58 | virtual bool Request(); |
| 59 | virtual bool Renew(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 60 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame^] | 61 | protected: |
| 62 | // Updates the IP configuration properties and notifies registered listeners |
| 63 | // about the event. |
| 64 | void UpdateProperties(const Properties &properties); |
| 65 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 66 | private: |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame^] | 67 | FRIEND_TEST(IPConfigTest, UpdateCallback); |
| 68 | FRIEND_TEST(IPConfigTest, UpdateProperties); |
| 69 | |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 70 | DeviceConstRefPtr device_; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 71 | Properties properties_; |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame^] | 72 | scoped_ptr<Callback1<IPConfigRefPtr>::Type> update_callback_; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 73 | |
| 74 | DISALLOW_COPY_AND_ASSIGN(IPConfig); |
| 75 | }; |
| 76 | |
| 77 | } // namespace shill |
| 78 | |
| 79 | #endif // SHILL_IPCONFIG_ |