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 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/memory/ref_counted.h> |
| 12 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 13 | |
| 14 | #include "shill/refptr_types.h" |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 15 | #include "shill/technology.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 16 | |
| 17 | namespace shill { |
| 18 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 19 | class DeviceInfo; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 20 | class IPAddress; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 21 | class Resolver; |
| 22 | class RoutingTable; |
| 23 | class RTNLHandler; |
| 24 | |
| 25 | // The Conneciton maintains the implemented state of an IPConfig, e.g, |
| 26 | // the IP address, routing table and DNS table entries. |
| 27 | class Connection : public base::RefCounted<Connection> { |
| 28 | public: |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 29 | Connection(int interface_index, |
| 30 | const std::string &interface_name, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 31 | Technology::Identifier technology_, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 32 | const DeviceInfo *device_info); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 33 | virtual ~Connection(); |
| 34 | |
| 35 | // Add the contents of an IPConfig reference to the list of managed state. |
| 36 | // This will replace all previous state for this address family. |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 37 | virtual void UpdateFromIPConfig(const IPConfigRefPtr &config); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 38 | |
| 39 | // Sets the current connection as "default", i.e., routes and DNS entries |
| 40 | // should be used by all system components that don't select explicitly. |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 41 | virtual bool is_default() const { return is_default_; } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 42 | virtual void SetIsDefault(bool is_default); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 43 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 44 | virtual const std::string &interface_name() const { return interface_name_; } |
| 45 | virtual const std::vector<std::string> &dns_servers() const { |
| 46 | return dns_servers_; |
| 47 | } |
| 48 | |
| 49 | // Request to accept traffic routed to this connection even if it is not |
| 50 | // the default. This request is ref-counted so the caller must call |
| 51 | // ReleaseRouting() when they no longer need this facility. |
| 52 | virtual void RequestRouting(); |
| 53 | virtual void ReleaseRouting(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 54 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 55 | // Request a host route through this connection. |
| 56 | virtual bool RequestHostRoute(const IPAddress &destination); |
| 57 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 58 | private: |
| 59 | friend class ConnectionTest; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 60 | FRIEND_TEST(ConnectionTest, AddConfig); |
| 61 | FRIEND_TEST(ConnectionTest, AddConfigReverse); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 62 | FRIEND_TEST(ConnectionTest, AddConfigWithBrokenNetmask); |
| 63 | FRIEND_TEST(ConnectionTest, AddConfigWithPeer); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 64 | FRIEND_TEST(ConnectionTest, Destructor); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 65 | FRIEND_TEST(ConnectionTest, InitState); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 66 | |
| 67 | static const uint32 kDefaultMetric; |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 68 | static const uint32 kNonDefaultMetricBase; |
| 69 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 70 | // Work around misconfigured servers which provide a gateway address that |
| 71 | // is unreachable with the provided netmask. |
| 72 | static void FixGatewayReachability(IPAddress *local, |
| 73 | const IPAddress &gateway); |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 74 | uint32 GetMetric(bool is_default); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 75 | |
| 76 | bool is_default_; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 77 | int routing_request_count_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 78 | int interface_index_; |
| 79 | const std::string interface_name_; |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 80 | Technology::Identifier technology_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 81 | std::vector<std::string> dns_servers_; |
| 82 | std::vector<std::string> dns_domain_search_; |
| 83 | |
| 84 | // Store cached copies of singletons for speed/ease of testing |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 85 | const DeviceInfo *device_info_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 86 | Resolver *resolver_; |
| 87 | RoutingTable *routing_table_; |
| 88 | RTNLHandler *rtnl_handler_; |
| 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(Connection); |
| 91 | }; |
| 92 | |
| 93 | } // namespace shill |
| 94 | |
| 95 | #endif // SHILL_CONNECTION_ |