blob: 5ca7d86d5477b5ad601da72a6582807b95c37b5d [file] [log] [blame]
Paul Stewartdd60e452011-08-08 11:38:36 -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#include "shill/connection.h"
6
7#include "shill/resolver.h"
8#include "shill/routing_table.h"
9#include "shill/rtnl_handler.h"
10
11using std::string;
12
13namespace shill {
14
15// static
16const uint32 Connection::kDefaultMetric = 1;
17// static
18const uint32 Connection::kNonDefaultMetric = 10;
19
20Connection::Connection(int interface_index, const std::string& interface_name)
21 : is_default_(false),
22 interface_index_(interface_index),
23 interface_name_(interface_name),
24 resolver_(Resolver::GetInstance()),
25 routing_table_(RoutingTable::GetInstance()),
26 rtnl_handler_(RTNLHandler::GetInstance()) {
27 VLOG(2) << __func__;
28}
29
30Connection::~Connection() {
31 routing_table_->FlushRoutes(interface_index_);
32 // TODO(pstew): Also flush all addresses when DeviceInfo starts tracking them
33 VLOG(2) << __func__;
34}
35
36void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) {
37 VLOG(2) << __func__;
38 rtnl_handler_->AddInterfaceAddress(interface_index_, *config);
39
40 uint32 metric = is_default_ ? kDefaultMetric : kNonDefaultMetric;
41 routing_table_->SetDefaultRoute(interface_index_, config, metric);
42
43 // Save a copy of the last non-null DNS config
44 if (!config->properties().dns_servers.empty()) {
45 dns_servers_ = config->properties().dns_servers;
46 dns_domain_search_ = config->properties().domain_search;
47 }
48
49 if (is_default_) {
50 resolver_->SetDNSFromIPConfig(config);
51 }
52}
53
54void Connection::SetDefault(bool is_default) {
55 VLOG(2) << __func__;
56 if (is_default == is_default_) {
57 return;
58 }
59
60 routing_table_->SetDefaultMetric(interface_index_,
61 is_default ? kDefaultMetric : kNonDefaultMetric);
62
63 if (is_default) {
64 resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_);
65 }
66
67 is_default_ = is_default;
68}
69
70} // namespace shill