Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 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_CONNECTION_ |
| 6 | #define SHILL_CONNECTION_ |
| 7 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 8 | #include <deque> |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/memory/ref_counted.h> |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 13 | #include <base/memory/weak_ptr.h> |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 15 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 16 | #include "shill/ipconfig.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 17 | #include "shill/refptr_types.h" |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 18 | #include "shill/technology.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 19 | |
| 20 | namespace shill { |
| 21 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 22 | class DeviceInfo; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 23 | class IPAddress; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 24 | class RTNLHandler; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 25 | class Resolver; |
| 26 | class RoutingTable; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 27 | struct RoutingTableEntry; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 28 | |
| 29 | // The Conneciton maintains the implemented state of an IPConfig, e.g, |
| 30 | // the IP address, routing table and DNS table entries. |
| 31 | class Connection : public base::RefCounted<Connection> { |
| 32 | public: |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 33 | // Clients can instantiate and use Binder to bind to a Connection and get |
| 34 | // notified when the bound Connection disconnects. Note that the client's |
| 35 | // disconnect callback will be executed at most once, and only if the bound |
| 36 | // Connection is destroyed or signals disconnect. The Binder unbinds itself |
| 37 | // from the underlying Connection when the Binder instance is destructed. |
| 38 | class Binder { |
| 39 | public: |
| 40 | Binder(const std::string &name, const base::Closure &disconnect_callback); |
| 41 | ~Binder(); |
| 42 | |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 43 | // Binds to |to_connection|. Unbinds the previous bound connection, if |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 44 | // any. Pass NULL to just unbind this Binder. |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 45 | void Attach(const ConnectionRefPtr &to_connection); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 46 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 47 | const std::string &name() const { return name_; } |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 48 | bool IsBound() const { return connection_ != NULL; } |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 49 | ConnectionRefPtr connection() const { return connection_.get(); } |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | friend class Connection; |
| 53 | FRIEND_TEST(ConnectionTest, Binder); |
| 54 | |
| 55 | // Invoked by |connection_|. |
| 56 | void OnDisconnect(); |
| 57 | |
| 58 | const std::string name_; |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 59 | base::WeakPtr<Connection> connection_; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 60 | const base::Closure client_disconnect_callback_; |
| 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(Binder); |
| 63 | }; |
| 64 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 65 | Connection(int interface_index, |
| 66 | const std::string &interface_name, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 67 | Technology::Identifier technology_, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 68 | const DeviceInfo *device_info); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 69 | |
| 70 | // Add the contents of an IPConfig reference to the list of managed state. |
| 71 | // This will replace all previous state for this address family. |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 72 | virtual void UpdateFromIPConfig(const IPConfigRefPtr &config); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 73 | |
| 74 | // Sets the current connection as "default", i.e., routes and DNS entries |
| 75 | // should be used by all system components that don't select explicitly. |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 76 | virtual bool is_default() const { return is_default_; } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 77 | virtual void SetIsDefault(bool is_default); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 78 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 79 | virtual const std::string &interface_name() const { return interface_name_; } |
| 80 | virtual const std::vector<std::string> &dns_servers() const { |
| 81 | return dns_servers_; |
| 82 | } |
| 83 | |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 84 | virtual const std::string &ipconfig_rpc_identifier() const { |
| 85 | return ipconfig_rpc_identifier_; |
| 86 | } |
| 87 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 88 | // Request to accept traffic routed to this connection even if it is not |
| 89 | // the default. This request is ref-counted so the caller must call |
| 90 | // ReleaseRouting() when they no longer need this facility. |
| 91 | virtual void RequestRouting(); |
| 92 | virtual void ReleaseRouting(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 93 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 94 | // Request a host route through this connection. |
| 95 | virtual bool RequestHostRoute(const IPAddress &destination); |
| 96 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 97 | protected: |
| 98 | friend class base::RefCounted<Connection>; |
| 99 | |
| 100 | virtual ~Connection(); |
| 101 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 102 | private: |
| 103 | friend class ConnectionTest; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 104 | FRIEND_TEST(ConnectionTest, AddConfig); |
| 105 | FRIEND_TEST(ConnectionTest, AddConfigReverse); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 106 | FRIEND_TEST(ConnectionTest, AddConfigWithBrokenNetmask); |
| 107 | FRIEND_TEST(ConnectionTest, AddConfigWithPeer); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 108 | FRIEND_TEST(ConnectionTest, Binder); |
| 109 | FRIEND_TEST(ConnectionTest, Binders); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 110 | FRIEND_TEST(ConnectionTest, Destructor); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 111 | FRIEND_TEST(ConnectionTest, FixGatewayReachability); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 112 | FRIEND_TEST(ConnectionTest, InitState); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 113 | FRIEND_TEST(ConnectionTest, OnRouteQueryResponse); |
| 114 | FRIEND_TEST(ConnectionTest, RequestHostRoute); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 115 | FRIEND_TEST(VPNServiceTest, OnConnectionDisconnected); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 116 | |
| 117 | static const uint32 kDefaultMetric; |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 118 | static const uint32 kNonDefaultMetricBase; |
| 119 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 120 | // Work around misconfigured servers which provide a gateway address that |
| 121 | // is unreachable with the provided netmask. |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 122 | static bool FixGatewayReachability(IPAddress *local, |
| 123 | const IPAddress &gateway, |
| 124 | const IPAddress &peer); |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 125 | uint32 GetMetric(bool is_default); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 126 | bool PinHostRoute(const IPConfig::Properties &config); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 127 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 128 | void OnRouteQueryResponse(int interface_index, |
| 129 | const RoutingTableEntry &entry); |
| 130 | |
| 131 | void AttachBinder(Binder *binder); |
| 132 | void DetachBinder(Binder *binder); |
| 133 | void NotifyBindersOnDisconnect(); |
| 134 | |
| 135 | void OnLowerDisconnect(); |
| 136 | |
| 137 | base::WeakPtrFactory<Connection> weak_ptr_factory_; |
| 138 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 139 | bool is_default_; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 140 | int routing_request_count_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 141 | int interface_index_; |
| 142 | const std::string interface_name_; |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 143 | Technology::Identifier technology_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 144 | std::vector<std::string> dns_servers_; |
| 145 | std::vector<std::string> dns_domain_search_; |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 146 | std::string ipconfig_rpc_identifier_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 147 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 148 | // A binder to a lower Connection that this Connection depends on, if any. |
| 149 | Binder lower_binder_; |
| 150 | |
| 151 | // Binders to clients -- usually to upper connections or related services and |
| 152 | // devices. |
| 153 | std::deque<Binder *> binders_; |
| 154 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 155 | // Store cached copies of singletons for speed/ease of testing |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 156 | const DeviceInfo *device_info_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 157 | Resolver *resolver_; |
| 158 | RoutingTable *routing_table_; |
| 159 | RTNLHandler *rtnl_handler_; |
| 160 | |
| 161 | DISALLOW_COPY_AND_ASSIGN(Connection); |
| 162 | }; |
| 163 | |
| 164 | } // namespace shill |
| 165 | |
| 166 | #endif // SHILL_CONNECTION_ |