blob: 8feee05770009ba0c0a94ecb74106af81f18f6e4 [file] [log] [blame]
Darin Petkove02b3ca2011-05-31 16:00:44 -07001// 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 Petkove7cb7f82011-06-03 13:21:51 -07009#include <vector>
Darin Petkove02b3ca2011-05-31 16:00:44 -070010
Darin Petkovefb09c32011-06-07 20:24:17 -070011#include <base/callback_old.h>
Chris Masone0756f232011-07-21 17:24:00 -070012#include <base/logging.h>
Darin Petkove02b3ca2011-05-31 16:00:44 -070013#include <base/memory/ref_counted.h>
Darin Petkovefb09c32011-06-07 20:24:17 -070014#include <base/memory/scoped_ptr.h>
15#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkove02b3ca2011-05-31 16:00:44 -070016
Paul Stewart1d18e8c2011-07-15 11:00:31 -070017#include "shill/ip_address.h"
Chris Masonec6c6c132011-06-30 11:29:52 -070018#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070019#include "shill/refptr_types.h"
20
Chris Masonec1e50412011-06-07 13:04:53 -070021namespace shill {
Chris Masonec6c6c132011-06-30 11:29:52 -070022class ControlInterface;
23class Error;
24class IPConfigAdaptorInterface;
Chris Masone8a7b8be2011-07-22 12:43:37 -070025class StoreInterface;
Darin Petkove02b3ca2011-05-31 16:00:44 -070026
27// IPConfig superclass. Individual IP configuration types will inherit from this
28// class.
Chris Masone27c4aa52011-07-02 13:10:14 -070029class IPConfig : public base::RefCounted<IPConfig> {
Darin Petkove02b3ca2011-05-31 16:00:44 -070030 public:
Darin Petkove7cb7f82011-06-03 13:21:51 -070031
Paul Stewartc39f1132011-06-22 12:02:28 -070032 struct Properties {
Paul Stewart1d18e8c2011-07-15 11:00:31 -070033 Properties() : address_family(IPAddress::kAddressFamilyUnknown),
Paul Stewartc39f1132011-06-22 12:02:28 -070034 subnet_cidr(0),
35 mtu(0) {}
36
Paul Stewart1d18e8c2011-07-15 11:00:31 -070037 IPAddress::Family address_family;
Darin Petkove7cb7f82011-06-03 13:21:51 -070038 std::string address;
Chris Masone43b48a12011-07-01 13:37:07 -070039 int32 subnet_cidr;
Darin Petkove7cb7f82011-06-03 13:21:51 -070040 std::string broadcast_address;
Darin Petkove7cb7f82011-06-03 13:21:51 -070041 std::vector<std::string> dns_servers;
42 std::string domain_name;
43 std::vector<std::string> domain_search;
Chris Masone43b48a12011-07-01 13:37:07 -070044 std::string gateway;
45 std::string method;
46 std::string peer_address;
47 int32 mtu;
Darin Petkove7cb7f82011-06-03 13:21:51 -070048 };
49
Chris Masone19e30402011-07-19 15:48:47 -070050 IPConfig(ControlInterface *control_interface, const std::string &device_name);
Chris Masone0756f232011-07-21 17:24:00 -070051 IPConfig(ControlInterface *control_interface,
52 const std::string &device_name,
53 const std::string &type);
Darin Petkove02b3ca2011-05-31 16:00:44 -070054 virtual ~IPConfig();
55
Darin Petkovf65e9282011-06-21 14:29:56 -070056 const std::string &device_name() const { return device_name_; }
Chris Masone0756f232011-07-21 17:24:00 -070057 const std::string &type() const { return type_; }
58 uint serial() const { return serial_; }
Darin Petkove02b3ca2011-05-31 16:00:44 -070059
Chris Masone4e851612011-07-01 10:46:53 -070060 std::string GetRpcIdentifier();
61
Darin Petkovefb09c32011-06-07 20:24:17 -070062 // Registers a callback that's executed every time the configuration
63 // properties change. Takes ownership of |callback|. Pass NULL to remove a
Darin Petkovf9b0ca82011-06-20 12:10:23 -070064 // callback. The callback's first argument is a pointer to this IP
65 // configuration instance allowing clients to more easily manage multiple IP
66 // configurations. The callback's second argument is set to false if IP
67 // configuration failed.
Chris Masone2b105542011-06-22 10:58:09 -070068 void RegisterUpdateCallback(
69 Callback2<const IPConfigRefPtr&, bool>::Type *callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -070070
71 const Properties &properties() const { return properties_; }
72
Darin Petkov92c43902011-06-09 20:46:06 -070073 // Request, renew and release IP configuration. Return true on success, false
Darin Petkove7cb7f82011-06-03 13:21:51 -070074 // otherwise. The default implementation always returns false indicating a
75 // failure.
Darin Petkov92c43902011-06-09 20:46:06 -070076 virtual bool RequestIP();
77 virtual bool RenewIP();
78 virtual bool ReleaseIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070079
Chris Masone27c4aa52011-07-02 13:10:14 -070080 PropertyStore *store() { return &store_; }
Chris Masone43b48a12011-07-01 13:37:07 -070081
Chris Masone8a7b8be2011-07-22 12:43:37 -070082 bool Load(StoreInterface *storage);
83 bool Save(StoreInterface *storage);
84
Darin Petkovefb09c32011-06-07 20:24:17 -070085 protected:
86 // Updates the IP configuration properties and notifies registered listeners
Darin Petkovf9b0ca82011-06-20 12:10:23 -070087 // about the event. |success| is set to false if the IP configuration failed.
88 void UpdateProperties(const Properties &properties, bool success);
Darin Petkovefb09c32011-06-07 20:24:17 -070089
Chris Masone27c4aa52011-07-02 13:10:14 -070090 PropertyStore store_;
91
Darin Petkove02b3ca2011-05-31 16:00:44 -070092 private:
Chris Masonec6c6c132011-06-30 11:29:52 -070093 friend class IPConfigAdaptorInterface;
94
Darin Petkovafa6fc42011-06-21 16:21:08 -070095 FRIEND_TEST(DeviceTest, AcquireDHCPConfig);
96 FRIEND_TEST(DeviceTest, DestroyIPConfig);
Darin Petkovefb09c32011-06-07 20:24:17 -070097 FRIEND_TEST(IPConfigTest, UpdateCallback);
98 FRIEND_TEST(IPConfigTest, UpdateProperties);
99
Chris Masone8a7b8be2011-07-22 12:43:37 -0700100 static const char kStorageType[];
Chris Masone0756f232011-07-21 17:24:00 -0700101 static const char kType[];
102 static uint global_serial_;
103
Darin Petkovf65e9282011-06-21 14:29:56 -0700104 const std::string device_name_;
Chris Masone0756f232011-07-21 17:24:00 -0700105 const std::string type_;
106 const uint serial_;
Chris Masone19e30402011-07-19 15:48:47 -0700107 scoped_ptr<IPConfigAdaptorInterface> adaptor_;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700108 Properties properties_;
Chris Masone2b105542011-06-22 10:58:09 -0700109 scoped_ptr<Callback2<const IPConfigRefPtr&, bool>::Type> update_callback_;
Darin Petkove02b3ca2011-05-31 16:00:44 -0700110
Chris Masone0756f232011-07-21 17:24:00 -0700111 void Init();
Chris Masone8a7b8be2011-07-22 12:43:37 -0700112 std::string GetStorageIdentifier();
Chris Masone0756f232011-07-21 17:24:00 -0700113
Darin Petkove02b3ca2011-05-31 16:00:44 -0700114 DISALLOW_COPY_AND_ASSIGN(IPConfig);
115};
116
117} // namespace shill
118
119#endif // SHILL_IPCONFIG_