blob: f4f634d1bc7200cac75d4d20f924df2d033c0dae [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>
Darin Petkove02b3ca2011-05-31 16:00:44 -070012#include <base/memory/ref_counted.h>
Darin Petkovefb09c32011-06-07 20:24:17 -070013#include <base/memory/scoped_ptr.h>
14#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkove02b3ca2011-05-31 16:00:44 -070015
Chris Masonec6c6c132011-06-30 11:29:52 -070016#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070017#include "shill/refptr_types.h"
18
Chris Masonec1e50412011-06-07 13:04:53 -070019namespace shill {
Chris Masonec6c6c132011-06-30 11:29:52 -070020class ControlInterface;
21class Error;
22class IPConfigAdaptorInterface;
Darin Petkove02b3ca2011-05-31 16:00:44 -070023
24// IPConfig superclass. Individual IP configuration types will inherit from this
25// class.
Chris Masonec6c6c132011-06-30 11:29:52 -070026class IPConfig : public PropertyStore, public base::RefCounted<IPConfig> {
Darin Petkove02b3ca2011-05-31 16:00:44 -070027 public:
Paul Stewartc39f1132011-06-22 12:02:28 -070028 enum AddressFamily {
29 kAddressFamilyUnknown,
30 kAddressFamilyIPv4,
31 kAddressFamilyIPv6
32 };
Darin Petkove7cb7f82011-06-03 13:21:51 -070033
Paul Stewartc39f1132011-06-22 12:02:28 -070034 struct Properties {
35 Properties() : address_family(kAddressFamilyUnknown),
36 subnet_cidr(0),
37 mtu(0) {}
38
39 AddressFamily address_family;
Darin Petkove7cb7f82011-06-03 13:21:51 -070040 std::string address;
Chris Masone43b48a12011-07-01 13:37:07 -070041 int32 subnet_cidr;
Darin Petkove7cb7f82011-06-03 13:21:51 -070042 std::string broadcast_address;
Darin Petkove7cb7f82011-06-03 13:21:51 -070043 std::vector<std::string> dns_servers;
44 std::string domain_name;
45 std::vector<std::string> domain_search;
Chris Masone43b48a12011-07-01 13:37:07 -070046 std::string gateway;
47 std::string method;
48 std::string peer_address;
49 int32 mtu;
Darin Petkove7cb7f82011-06-03 13:21:51 -070050 };
51
Darin Petkovf65e9282011-06-21 14:29:56 -070052 explicit IPConfig(const std::string &device_name);
Darin Petkove02b3ca2011-05-31 16:00:44 -070053 virtual ~IPConfig();
54
Darin Petkovf65e9282011-06-21 14:29:56 -070055 const std::string &device_name() const { return device_name_; }
Darin Petkove02b3ca2011-05-31 16:00:44 -070056
Chris Masone4e851612011-07-01 10:46:53 -070057 std::string GetRpcIdentifier();
58
Darin Petkovefb09c32011-06-07 20:24:17 -070059 // Registers a callback that's executed every time the configuration
60 // properties change. Takes ownership of |callback|. Pass NULL to remove a
Darin Petkovf9b0ca82011-06-20 12:10:23 -070061 // callback. The callback's first argument is a pointer to this IP
62 // configuration instance allowing clients to more easily manage multiple IP
63 // configurations. The callback's second argument is set to false if IP
64 // configuration failed.
Chris Masone2b105542011-06-22 10:58:09 -070065 void RegisterUpdateCallback(
66 Callback2<const IPConfigRefPtr&, bool>::Type *callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -070067
68 const Properties &properties() const { return properties_; }
69
Darin Petkov92c43902011-06-09 20:46:06 -070070 // Request, renew and release IP configuration. Return true on success, false
Darin Petkove7cb7f82011-06-03 13:21:51 -070071 // otherwise. The default implementation always returns false indicating a
72 // failure.
Darin Petkov92c43902011-06-09 20:46:06 -070073 virtual bool RequestIP();
74 virtual bool RenewIP();
75 virtual bool ReleaseIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070076
Chris Masone43b48a12011-07-01 13:37:07 -070077 // Implementation of PropertyStore
78 virtual bool SetInt32Property(const std::string& name,
79 int32 value,
80 Error *error);
81 virtual bool SetStringProperty(const std::string &name,
82 const std::string &value,
83 Error *error);
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
Darin Petkove02b3ca2011-05-31 16:00:44 -070090 private:
Chris Masonec6c6c132011-06-30 11:29:52 -070091 friend class IPConfigAdaptorInterface;
92
Darin Petkovafa6fc42011-06-21 16:21:08 -070093 FRIEND_TEST(DeviceTest, AcquireDHCPConfig);
94 FRIEND_TEST(DeviceTest, DestroyIPConfig);
Darin Petkovefb09c32011-06-07 20:24:17 -070095 FRIEND_TEST(IPConfigTest, UpdateCallback);
96 FRIEND_TEST(IPConfigTest, UpdateProperties);
97
Chris Masonec6c6c132011-06-30 11:29:52 -070098 scoped_ptr<IPConfigAdaptorInterface> adaptor_;
Darin Petkovf65e9282011-06-21 14:29:56 -070099 const std::string device_name_;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700100 Properties properties_;
Chris Masone2b105542011-06-22 10:58:09 -0700101 scoped_ptr<Callback2<const IPConfigRefPtr&, bool>::Type> update_callback_;
Darin Petkove02b3ca2011-05-31 16:00:44 -0700102
103 DISALLOW_COPY_AND_ASSIGN(IPConfig);
104};
105
106} // namespace shill
107
108#endif // SHILL_IPCONFIG_