blob: e89256916052098a9114001c0472162b6e4a82c3 [file] [log] [blame]
Paul Stewartc681fa02012-03-02 19:40:04 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartdd60e452011-08-08 11:38:36 -07002// 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
Paul Stewarte93b0382012-04-24 13:11:28 -070014#include "shill/ipconfig.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070015#include "shill/refptr_types.h"
Paul Stewarte00600e2012-03-16 07:08:00 -070016#include "shill/technology.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070017
18namespace shill {
19
Paul Stewart9a908082011-08-31 12:18:48 -070020class DeviceInfo;
Paul Stewartf748a362012-03-07 12:01:20 -080021class IPAddress;
Paul Stewartdd60e452011-08-08 11:38:36 -070022class Resolver;
23class RoutingTable;
24class RTNLHandler;
25
26// The Conneciton maintains the implemented state of an IPConfig, e.g,
27// the IP address, routing table and DNS table entries.
28class Connection : public base::RefCounted<Connection> {
29 public:
Paul Stewart9a908082011-08-31 12:18:48 -070030 Connection(int interface_index,
31 const std::string &interface_name,
Paul Stewarte00600e2012-03-16 07:08:00 -070032 Technology::Identifier technology_,
Paul Stewart9a908082011-08-31 12:18:48 -070033 const DeviceInfo *device_info);
Paul Stewartdd60e452011-08-08 11:38:36 -070034 virtual ~Connection();
35
36 // Add the contents of an IPConfig reference to the list of managed state.
37 // This will replace all previous state for this address family.
Paul Stewartc1dec4d2011-12-08 15:25:28 -080038 virtual void UpdateFromIPConfig(const IPConfigRefPtr &config);
Paul Stewartdd60e452011-08-08 11:38:36 -070039
40 // Sets the current connection as "default", i.e., routes and DNS entries
41 // should be used by all system components that don't select explicitly.
Paul Stewartc681fa02012-03-02 19:40:04 -080042 virtual bool is_default() const { return is_default_; }
Paul Stewartc1dec4d2011-12-08 15:25:28 -080043 virtual void SetIsDefault(bool is_default);
Paul Stewartdd60e452011-08-08 11:38:36 -070044
Paul Stewartc8f4bef2011-12-13 09:45:51 -080045 virtual const std::string &interface_name() const { return interface_name_; }
46 virtual const std::vector<std::string> &dns_servers() const {
47 return dns_servers_;
48 }
49
Paul Stewart10241e32012-04-23 18:15:06 -070050 virtual const std::string &ipconfig_rpc_identifier() const {
51 return ipconfig_rpc_identifier_;
52 }
53
Paul Stewartc8f4bef2011-12-13 09:45:51 -080054 // Request to accept traffic routed to this connection even if it is not
55 // the default. This request is ref-counted so the caller must call
56 // ReleaseRouting() when they no longer need this facility.
57 virtual void RequestRouting();
58 virtual void ReleaseRouting();
Paul Stewartbe5f5b32011-12-07 17:11:11 -080059
Paul Stewartf748a362012-03-07 12:01:20 -080060 // Request a host route through this connection.
61 virtual bool RequestHostRoute(const IPAddress &destination);
62
Paul Stewartdd60e452011-08-08 11:38:36 -070063 private:
64 friend class ConnectionTest;
Paul Stewartdd60e452011-08-08 11:38:36 -070065 FRIEND_TEST(ConnectionTest, AddConfig);
66 FRIEND_TEST(ConnectionTest, AddConfigReverse);
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070067 FRIEND_TEST(ConnectionTest, AddConfigWithBrokenNetmask);
68 FRIEND_TEST(ConnectionTest, AddConfigWithPeer);
Paul Stewartdd60e452011-08-08 11:38:36 -070069 FRIEND_TEST(ConnectionTest, Destructor);
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070070 FRIEND_TEST(ConnectionTest, InitState);
Paul Stewartdd60e452011-08-08 11:38:36 -070071
72 static const uint32 kDefaultMetric;
Paul Stewart7cfca042011-12-08 14:18:17 -080073 static const uint32 kNonDefaultMetricBase;
74
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070075 // Work around misconfigured servers which provide a gateway address that
76 // is unreachable with the provided netmask.
77 static void FixGatewayReachability(IPAddress *local,
78 const IPAddress &gateway);
Paul Stewart7cfca042011-12-08 14:18:17 -080079 uint32 GetMetric(bool is_default);
Paul Stewarte93b0382012-04-24 13:11:28 -070080 bool PinHostRoute(const IPConfig::Properties &config);
Paul Stewartdd60e452011-08-08 11:38:36 -070081
82 bool is_default_;
Paul Stewartc8f4bef2011-12-13 09:45:51 -080083 int routing_request_count_;
Paul Stewartdd60e452011-08-08 11:38:36 -070084 int interface_index_;
85 const std::string interface_name_;
Paul Stewarte00600e2012-03-16 07:08:00 -070086 Technology::Identifier technology_;
Paul Stewartdd60e452011-08-08 11:38:36 -070087 std::vector<std::string> dns_servers_;
88 std::vector<std::string> dns_domain_search_;
Paul Stewart10241e32012-04-23 18:15:06 -070089 std::string ipconfig_rpc_identifier_;
Paul Stewartdd60e452011-08-08 11:38:36 -070090
91 // Store cached copies of singletons for speed/ease of testing
Paul Stewart9a908082011-08-31 12:18:48 -070092 const DeviceInfo *device_info_;
Paul Stewartdd60e452011-08-08 11:38:36 -070093 Resolver *resolver_;
94 RoutingTable *routing_table_;
95 RTNLHandler *rtnl_handler_;
96
97 DISALLOW_COPY_AND_ASSIGN(Connection);
98};
99
100} // namespace shill
101
102#endif // SHILL_CONNECTION_