blob: 13a8c5ce841983cd2d2e7d1081e060d9f320d0b2 [file] [log] [blame]
Ben Chanfad4a0b2012-04-18 15:49:59 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov50308cd2011-06-01 18:25:07 -07002// 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/dhcp_provider.h"
6
Ben Chan11c213f2014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Ben Chana0ddf462014-02-06 11:32:42 -08008#include <base/strings/stringprintf.h>
Albert Chaulk0e1cdea2013-02-27 15:32:55 -08009
Chris Masone19e30402011-07-19 15:48:47 -070010#include "shill/control_interface.h"
Chris Masone2b105542011-06-22 10:58:09 -070011#include "shill/dhcp_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070012#include "shill/dhcpcd_proxy.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070013#include "shill/logging.h"
Paul Stewart8dc5e7b2014-12-11 19:24:50 -080014#include "shill/shared_dbus_connection.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070015
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080016using base::FilePath;
Darin Petkovf65e9282011-06-21 14:29:56 -070017using std::string;
18
Darin Petkov50308cd2011-06-01 18:25:07 -070019namespace shill {
20
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070021namespace Logging {
22static auto kModuleLogScope = ScopeLogger::kDHCP;
23static string ObjectID(DHCPProvider *d) { return "(dhcp_provider)"; }
24}
25
Darin Petkove636c692012-05-31 10:22:17 +020026namespace {
27base::LazyInstance<DHCPProvider> g_dhcp_provider = LAZY_INSTANCE_INITIALIZER;
28} // namespace
Paul Stewart0d2ada32011-08-09 17:01:57 -070029
mukesh agrawal88fb16f2014-04-24 10:37:39 -070030constexpr char DHCPProvider::kDHCPCDPathFormatLease[];
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080031
Darin Petkova7b89492011-07-27 12:48:17 -070032DHCPProvider::DHCPProvider()
Paul Stewart8dc5e7b2014-12-11 19:24:50 -080033 : shared_dbus_connection_(SharedDBusConnection::GetInstance()),
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080034 root_("/"),
Ben Chancc225ef2014-09-30 13:26:51 -070035 control_interface_(nullptr),
36 dispatcher_(nullptr),
37 glib_(nullptr),
38 metrics_(nullptr) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070039 SLOG(this, 2) << __func__;
Darin Petkov50308cd2011-06-01 18:25:07 -070040}
41
42DHCPProvider::~DHCPProvider() {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070043 SLOG(this, 2) << __func__;
Darin Petkov50308cd2011-06-01 18:25:07 -070044}
45
46DHCPProvider* DHCPProvider::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070047 return g_dhcp_provider.Pointer();
Darin Petkov50308cd2011-06-01 18:25:07 -070048}
49
Darin Petkova7b89492011-07-27 12:48:17 -070050void DHCPProvider::Init(ControlInterface *control_interface,
51 EventDispatcher *dispatcher,
Paul Stewart3bdf1ab2014-07-17 19:22:26 -070052 GLib *glib,
53 Metrics *metrics) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070054 SLOG(this, 2) << __func__;
Paul Stewart04f00c62015-01-08 15:59:10 -080055 DBus::Connection *connection = shared_dbus_connection_->GetProxyConnection();
Paul Stewart8dc5e7b2014-12-11 19:24:50 -080056 listener_.reset(new DHCPCDListener(connection, this));
Darin Petkovf7897bc2011-06-08 17:13:36 -070057 glib_ = glib;
Chris Masone19e30402011-07-19 15:48:47 -070058 control_interface_ = control_interface;
Darin Petkova7b89492011-07-27 12:48:17 -070059 dispatcher_ = dispatcher;
Paul Stewart3bdf1ab2014-07-17 19:22:26 -070060 metrics_ = metrics;
Darin Petkovd1b715b2011-06-02 21:21:22 -070061}
62
Paul Stewart8dc5e7b2014-12-11 19:24:50 -080063void DHCPProvider::Stop() {
64 listener_.reset();
65}
66
Paul Stewartd32f4842012-01-11 16:08:13 -080067DHCPConfigRefPtr DHCPProvider::CreateConfig(const string &device_name,
Paul Stewartd408fdf2012-05-07 17:15:57 -070068 const string &host_name,
69 const string &lease_file_suffix,
Paul Stewartb1083182014-06-25 03:04:53 -070070 bool arp_gateway) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070071 SLOG(this, 2) << __func__ << " device: " << device_name;
Paul Stewartd408fdf2012-05-07 17:15:57 -070072 return new DHCPConfig(control_interface_,
73 dispatcher_,
74 this,
75 device_name,
76 host_name,
77 lease_file_suffix,
78 arp_gateway,
Paul Stewart3bdf1ab2014-07-17 19:22:26 -070079 glib_,
80 metrics_);
Darin Petkovd1b715b2011-06-02 21:21:22 -070081}
82
Darin Petkov98dd6a02011-06-10 15:12:57 -070083DHCPConfigRefPtr DHCPProvider::GetConfig(int pid) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070084 SLOG(this, 2) << __func__ << " pid: " << pid;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070085 PIDConfigMap::const_iterator it = configs_.find(pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070086 if (it == configs_.end()) {
Ben Chancc225ef2014-09-30 13:26:51 -070087 return nullptr;
Darin Petkovd1b715b2011-06-02 21:21:22 -070088 }
89 return it->second;
90}
91
Chris Masone2b105542011-06-22 10:58:09 -070092void DHCPProvider::BindPID(int pid, const DHCPConfigRefPtr &config) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070093 SLOG(this, 2) << __func__ << " pid: " << pid;
Darin Petkovd1b715b2011-06-02 21:21:22 -070094 configs_[pid] = config;
95}
96
Darin Petkov92c43902011-06-09 20:46:06 -070097void DHCPProvider::UnbindPID(int pid) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070098 SLOG(this, 2) << __func__ << " pid: " << pid;
Darin Petkovd1b715b2011-06-02 21:21:22 -070099 configs_.erase(pid);
100}
101
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800102void DHCPProvider::DestroyLease(const string &name) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700103 SLOG(this, 2) << __func__ << " name: " << name;
Ben Chana0ddf462014-02-06 11:32:42 -0800104 base::DeleteFile(root_.Append(
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800105 base::StringPrintf(kDHCPCDPathFormatLease,
106 name.c_str())), false);
107}
108
Darin Petkov50308cd2011-06-01 18:25:07 -0700109} // namespace shill