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 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 7 | #include <arpa/inet.h> |
| 8 | #include <linux/rtnetlink.h> |
| 9 | |
| 10 | #include "shill/device_info.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 11 | #include "shill/resolver.h" |
| 12 | #include "shill/routing_table.h" |
| 13 | #include "shill/rtnl_handler.h" |
| 14 | |
| 15 | using std::string; |
| 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | // static |
| 20 | const uint32 Connection::kDefaultMetric = 1; |
| 21 | // static |
| 22 | const uint32 Connection::kNonDefaultMetric = 10; |
| 23 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 24 | Connection::Connection(int interface_index, |
| 25 | const std::string& interface_name, |
| 26 | const DeviceInfo *device_info) |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 27 | : is_default_(false), |
| 28 | interface_index_(interface_index), |
| 29 | interface_name_(interface_name), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 30 | device_info_(device_info), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 31 | resolver_(Resolver::GetInstance()), |
| 32 | routing_table_(RoutingTable::GetInstance()), |
| 33 | rtnl_handler_(RTNLHandler::GetInstance()) { |
| 34 | VLOG(2) << __func__; |
| 35 | } |
| 36 | |
| 37 | Connection::~Connection() { |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 38 | VLOG(2) << __func__; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 39 | |
| 40 | routing_table_->FlushRoutes(interface_index_); |
| 41 | device_info_->FlushAddresses(interface_index_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) { |
| 45 | VLOG(2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 46 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 47 | const IPConfig::Properties &properties = config->properties(); |
| 48 | IPAddress local(properties.address_family); |
| 49 | if (!local.SetAddressFromString(properties.address)) { |
| 50 | LOG(ERROR) << "Local address " << properties.address << " is invalid"; |
| 51 | return; |
| 52 | } |
| 53 | local.set_prefix(properties.subnet_cidr); |
| 54 | |
| 55 | IPAddress broadcast(properties.address_family); |
| 56 | if (!broadcast.SetAddressFromString(properties.broadcast_address)) { |
| 57 | LOG(ERROR) << "Broadcast address " << properties.broadcast_address |
| 58 | << " is invalid"; |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 63 | |
| 64 | uint32 metric = is_default_ ? kDefaultMetric : kNonDefaultMetric; |
| 65 | routing_table_->SetDefaultRoute(interface_index_, config, metric); |
| 66 | |
| 67 | // Save a copy of the last non-null DNS config |
| 68 | if (!config->properties().dns_servers.empty()) { |
| 69 | dns_servers_ = config->properties().dns_servers; |
| 70 | dns_domain_search_ = config->properties().domain_search; |
| 71 | } |
| 72 | |
| 73 | if (is_default_) { |
| 74 | resolver_->SetDNSFromIPConfig(config); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void Connection::SetDefault(bool is_default) { |
| 79 | VLOG(2) << __func__; |
| 80 | if (is_default == is_default_) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | routing_table_->SetDefaultMetric(interface_index_, |
| 85 | is_default ? kDefaultMetric : kNonDefaultMetric); |
| 86 | |
| 87 | if (is_default) { |
| 88 | resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_); |
| 89 | } |
| 90 | |
| 91 | is_default_ = is_default; |
| 92 | } |
| 93 | |
| 94 | } // namespace shill |