blob: fc81171c7427de8a401ed30ef0f051979ebfb538 [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
14#include "shill/refptr_types.h"
Paul Stewarte00600e2012-03-16 07:08:00 -070015#include "shill/technology.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070016
17namespace shill {
18
Paul Stewart9a908082011-08-31 12:18:48 -070019class DeviceInfo;
Paul Stewartf748a362012-03-07 12:01:20 -080020class IPAddress;
Paul Stewartdd60e452011-08-08 11:38:36 -070021class Resolver;
22class RoutingTable;
23class RTNLHandler;
24
25// The Conneciton maintains the implemented state of an IPConfig, e.g,
26// the IP address, routing table and DNS table entries.
27class Connection : public base::RefCounted<Connection> {
28 public:
Paul Stewart9a908082011-08-31 12:18:48 -070029 Connection(int interface_index,
30 const std::string &interface_name,
Paul Stewarte00600e2012-03-16 07:08:00 -070031 Technology::Identifier technology_,
Paul Stewart9a908082011-08-31 12:18:48 -070032 const DeviceInfo *device_info);
Paul Stewartdd60e452011-08-08 11:38:36 -070033 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 Stewartc1dec4d2011-12-08 15:25:28 -080037 virtual void UpdateFromIPConfig(const IPConfigRefPtr &config);
Paul Stewartdd60e452011-08-08 11:38:36 -070038
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 Stewartc681fa02012-03-02 19:40:04 -080041 virtual bool is_default() const { return is_default_; }
Paul Stewartc1dec4d2011-12-08 15:25:28 -080042 virtual void SetIsDefault(bool is_default);
Paul Stewartdd60e452011-08-08 11:38:36 -070043
Paul Stewartc8f4bef2011-12-13 09:45:51 -080044 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 Stewartbe5f5b32011-12-07 17:11:11 -080054
Paul Stewartf748a362012-03-07 12:01:20 -080055 // Request a host route through this connection.
56 virtual bool RequestHostRoute(const IPAddress &destination);
57
Paul Stewartdd60e452011-08-08 11:38:36 -070058 private:
59 friend class ConnectionTest;
60 FRIEND_TEST(ConnectionTest, InitState);
61 FRIEND_TEST(ConnectionTest, AddConfig);
62 FRIEND_TEST(ConnectionTest, AddConfigReverse);
63 FRIEND_TEST(ConnectionTest, Destructor);
64
65 static const uint32 kDefaultMetric;
Paul Stewart7cfca042011-12-08 14:18:17 -080066 static const uint32 kNonDefaultMetricBase;
67
68 uint32 GetMetric(bool is_default);
Paul Stewartdd60e452011-08-08 11:38:36 -070069
70 bool is_default_;
Paul Stewartc8f4bef2011-12-13 09:45:51 -080071 int routing_request_count_;
Paul Stewartdd60e452011-08-08 11:38:36 -070072 int interface_index_;
73 const std::string interface_name_;
Paul Stewarte00600e2012-03-16 07:08:00 -070074 Technology::Identifier technology_;
Paul Stewartdd60e452011-08-08 11:38:36 -070075 std::vector<std::string> dns_servers_;
76 std::vector<std::string> dns_domain_search_;
77
78 // Store cached copies of singletons for speed/ease of testing
Paul Stewart9a908082011-08-31 12:18:48 -070079 const DeviceInfo *device_info_;
Paul Stewartdd60e452011-08-08 11:38:36 -070080 Resolver *resolver_;
81 RoutingTable *routing_table_;
82 RTNLHandler *rtnl_handler_;
83
84 DISALLOW_COPY_AND_ASSIGN(Connection);
85};
86
87} // namespace shill
88
89#endif // SHILL_CONNECTION_