connection.cc revision e613202d36e3bfb06a40eea1888694413210ef7e
1// 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
39  // TODO(pstew): Create a centralized resource (perhaps in DeviceInfo?) to
40  // keep apply and keep track of addresses associated with an interface.
41  // Use this instead of directly setting the address over RTNL.
42  rtnl_handler_->AddInterfaceAddress(interface_index_, *config);
43
44  uint32 metric = is_default_ ? kDefaultMetric : kNonDefaultMetric;
45  routing_table_->SetDefaultRoute(interface_index_, config, metric);
46
47  // Save a copy of the last non-null DNS config
48  if (!config->properties().dns_servers.empty()) {
49    dns_servers_ = config->properties().dns_servers;
50    dns_domain_search_ = config->properties().domain_search;
51  }
52
53  if (is_default_) {
54    resolver_->SetDNSFromIPConfig(config);
55  }
56}
57
58void Connection::SetDefault(bool is_default) {
59  VLOG(2) << __func__;
60  if (is_default == is_default_) {
61    return;
62  }
63
64  routing_table_->SetDefaultMetric(interface_index_,
65      is_default ? kDefaultMetric : kNonDefaultMetric);
66
67  if (is_default) {
68    resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_);
69  }
70
71  is_default_ = is_default;
72}
73
74}  // namespace shill
75