Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 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 | |
| 11 | using std::string; |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | // static |
| 16 | const uint32 Connection::kDefaultMetric = 1; |
| 17 | // static |
| 18 | const uint32 Connection::kNonDefaultMetric = 10; |
| 19 | |
| 20 | Connection::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 | |
| 30 | Connection::~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 | |
| 36 | void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) { |
| 37 | VLOG(2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 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. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 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 | |
| 58 | void 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 |