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 | namespace shill { |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 17 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 18 | class IPConfig; |
| 19 | |
| 20 | typedef scoped_refptr<IPConfig> IPConfigRefPtr; |
| 21 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 22 | // IPConfig superclass. Individual IP configuration types will inherit from this |
| 23 | // class. |
| 24 | class IPConfig : public base::RefCounted<IPConfig> { |
| 25 | public: |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 26 | struct Properties { |
| 27 | Properties() : subnet_cidr(0), mtu(0) {} |
| 28 | |
| 29 | std::string address; |
| 30 | int subnet_cidr; |
| 31 | std::string broadcast_address; |
| 32 | std::string gateway; |
| 33 | std::vector<std::string> dns_servers; |
| 34 | std::string domain_name; |
| 35 | std::vector<std::string> domain_search; |
| 36 | int mtu; |
| 37 | }; |
| 38 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame^] | 39 | explicit IPConfig(const std::string &device_name); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 40 | virtual ~IPConfig(); |
| 41 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame^] | 42 | const std::string &device_name() const { return device_name_; } |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 43 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 44 | // Registers a callback that's executed every time the configuration |
| 45 | // properties change. Takes ownership of |callback|. Pass NULL to remove a |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 46 | // callback. The callback's first argument is a pointer to this IP |
| 47 | // configuration instance allowing clients to more easily manage multiple IP |
| 48 | // configurations. The callback's second argument is set to false if IP |
| 49 | // configuration failed. |
| 50 | void RegisterUpdateCallback(Callback2<IPConfigRefPtr, bool>::Type *callback); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 51 | |
| 52 | const Properties &properties() const { return properties_; } |
| 53 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 54 | // Request, renew and release IP configuration. Return true on success, false |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 55 | // otherwise. The default implementation always returns false indicating a |
| 56 | // failure. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 57 | virtual bool RequestIP(); |
| 58 | virtual bool RenewIP(); |
| 59 | virtual bool ReleaseIP(); |
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 |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 63 | // about the event. |success| is set to false if the IP configuration failed. |
| 64 | void UpdateProperties(const Properties &properties, bool success); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 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 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame^] | 70 | const std::string device_name_; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 71 | Properties properties_; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 72 | scoped_ptr<Callback2<IPConfigRefPtr, bool>::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_ |