blob: cd9e8fd7dab5da68e083ba9277eb9a224483ab68 [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;
41 int subnet_cidr;
42 std::string broadcast_address;
43 std::string gateway;
44 std::vector<std::string> dns_servers;
45 std::string domain_name;
46 std::vector<std::string> domain_search;
47 int mtu;
48 };
49
Darin Petkovf65e9282011-06-21 14:29:56 -070050 explicit IPConfig(const std::string &device_name);
Darin Petkove02b3ca2011-05-31 16:00:44 -070051 virtual ~IPConfig();
52
Darin Petkovf65e9282011-06-21 14:29:56 -070053 const std::string &device_name() const { return device_name_; }
Darin Petkove02b3ca2011-05-31 16:00:44 -070054
Darin Petkovefb09c32011-06-07 20:24:17 -070055 // Registers a callback that's executed every time the configuration
56 // properties change. Takes ownership of |callback|. Pass NULL to remove a
Darin Petkovf9b0ca82011-06-20 12:10:23 -070057 // callback. The callback's first argument is a pointer to this IP
58 // configuration instance allowing clients to more easily manage multiple IP
59 // configurations. The callback's second argument is set to false if IP
60 // configuration failed.
Chris Masone2b105542011-06-22 10:58:09 -070061 void RegisterUpdateCallback(
62 Callback2<const IPConfigRefPtr&, bool>::Type *callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -070063
64 const Properties &properties() const { return properties_; }
65
Darin Petkov92c43902011-06-09 20:46:06 -070066 // Request, renew and release IP configuration. Return true on success, false
Darin Petkove7cb7f82011-06-03 13:21:51 -070067 // otherwise. The default implementation always returns false indicating a
68 // failure.
Darin Petkov92c43902011-06-09 20:46:06 -070069 virtual bool RequestIP();
70 virtual bool RenewIP();
71 virtual bool ReleaseIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070072
Darin Petkovefb09c32011-06-07 20:24:17 -070073 protected:
74 // Updates the IP configuration properties and notifies registered listeners
Darin Petkovf9b0ca82011-06-20 12:10:23 -070075 // about the event. |success| is set to false if the IP configuration failed.
76 void UpdateProperties(const Properties &properties, bool success);
Darin Petkovefb09c32011-06-07 20:24:17 -070077
Darin Petkove02b3ca2011-05-31 16:00:44 -070078 private:
Chris Masonec6c6c132011-06-30 11:29:52 -070079 friend class IPConfigAdaptorInterface;
80
Darin Petkovafa6fc42011-06-21 16:21:08 -070081 FRIEND_TEST(DeviceTest, AcquireDHCPConfig);
82 FRIEND_TEST(DeviceTest, DestroyIPConfig);
Darin Petkovefb09c32011-06-07 20:24:17 -070083 FRIEND_TEST(IPConfigTest, UpdateCallback);
84 FRIEND_TEST(IPConfigTest, UpdateProperties);
85
Chris Masonec6c6c132011-06-30 11:29:52 -070086 scoped_ptr<IPConfigAdaptorInterface> adaptor_;
Darin Petkovf65e9282011-06-21 14:29:56 -070087 const std::string device_name_;
Darin Petkove7cb7f82011-06-03 13:21:51 -070088 Properties properties_;
Chris Masone2b105542011-06-22 10:58:09 -070089 scoped_ptr<Callback2<const IPConfigRefPtr&, bool>::Type> update_callback_;
Darin Petkove02b3ca2011-05-31 16:00:44 -070090
91 DISALLOW_COPY_AND_ASSIGN(IPConfig);
92};
93
94} // namespace shill
95
96#endif // SHILL_IPCONFIG_