blob: f7b3e67006a1ac53443b3088872360195415d71a [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"
Darin Petkovaceede32011-07-18 15:32:38 -070014#include "shill/proxy_factory.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()
Darin Petkovab565bb2011-10-06 02:55:51 -070033 : proxy_factory_(ProxyFactory::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__;
Darin Petkovab565bb2011-10-06 02:55:51 -070055 listener_.reset(new DHCPCDListener(proxy_factory_->connection(), this));
Darin Petkovf7897bc2011-06-08 17:13:36 -070056 glib_ = glib;
Chris Masone19e30402011-07-19 15:48:47 -070057 control_interface_ = control_interface;
Darin Petkova7b89492011-07-27 12:48:17 -070058 dispatcher_ = dispatcher;
Paul Stewart3bdf1ab2014-07-17 19:22:26 -070059 metrics_ = metrics;
Darin Petkovd1b715b2011-06-02 21:21:22 -070060}
61
Paul Stewartd32f4842012-01-11 16:08:13 -080062DHCPConfigRefPtr DHCPProvider::CreateConfig(const string &device_name,
Paul Stewartd408fdf2012-05-07 17:15:57 -070063 const string &host_name,
64 const string &lease_file_suffix,
Paul Stewartb1083182014-06-25 03:04:53 -070065 bool arp_gateway) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070066 SLOG(this, 2) << __func__ << " device: " << device_name;
Paul Stewartd408fdf2012-05-07 17:15:57 -070067 return new DHCPConfig(control_interface_,
68 dispatcher_,
69 this,
70 device_name,
71 host_name,
72 lease_file_suffix,
73 arp_gateway,
Paul Stewart3bdf1ab2014-07-17 19:22:26 -070074 glib_,
75 metrics_);
Darin Petkovd1b715b2011-06-02 21:21:22 -070076}
77
Darin Petkov98dd6a02011-06-10 15:12:57 -070078DHCPConfigRefPtr DHCPProvider::GetConfig(int pid) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070079 SLOG(this, 2) << __func__ << " pid: " << pid;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070080 PIDConfigMap::const_iterator it = configs_.find(pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070081 if (it == configs_.end()) {
Ben Chancc225ef2014-09-30 13:26:51 -070082 return nullptr;
Darin Petkovd1b715b2011-06-02 21:21:22 -070083 }
84 return it->second;
85}
86
Chris Masone2b105542011-06-22 10:58:09 -070087void DHCPProvider::BindPID(int pid, const DHCPConfigRefPtr &config) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070088 SLOG(this, 2) << __func__ << " pid: " << pid;
Darin Petkovd1b715b2011-06-02 21:21:22 -070089 configs_[pid] = config;
90}
91
Darin Petkov92c43902011-06-09 20:46:06 -070092void DHCPProvider::UnbindPID(int pid) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070093 SLOG(this, 2) << __func__ << " pid: " << pid;
Darin Petkovd1b715b2011-06-02 21:21:22 -070094 configs_.erase(pid);
95}
96
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080097void DHCPProvider::DestroyLease(const string &name) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070098 SLOG(this, 2) << __func__ << " name: " << name;
Ben Chana0ddf462014-02-06 11:32:42 -080099 base::DeleteFile(root_.Append(
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800100 base::StringPrintf(kDHCPCDPathFormatLease,
101 name.c_str())), false);
102}
103
Darin Petkov50308cd2011-06-01 18:25:07 -0700104} // namespace shill