blob: f92d585113b7912aefe4c7290da229dbccdbad30 [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
Albert Chaulk0e1cdea2013-02-27 15:32:55 -08007#include <base/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
Darin Petkove636c692012-05-31 10:22:17 +020021namespace {
22base::LazyInstance<DHCPProvider> g_dhcp_provider = LAZY_INSTANCE_INITIALIZER;
23} // namespace
Paul Stewart0d2ada32011-08-09 17:01:57 -070024
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080025const char DHCPProvider::kDHCPCDPathFormatLease[] =
26 "var/lib/dhcpcd/dhcpcd-%s.lease";
27
Darin Petkova7b89492011-07-27 12:48:17 -070028DHCPProvider::DHCPProvider()
Darin Petkovab565bb2011-10-06 02:55:51 -070029 : proxy_factory_(ProxyFactory::GetInstance()),
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080030 root_("/"),
Darin Petkovab565bb2011-10-06 02:55:51 -070031 control_interface_(NULL),
Darin Petkova7b89492011-07-27 12:48:17 -070032 dispatcher_(NULL),
33 glib_(NULL) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070034 SLOG(DHCP, 2) << __func__;
Darin Petkov50308cd2011-06-01 18:25:07 -070035}
36
37DHCPProvider::~DHCPProvider() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070038 SLOG(DHCP, 2) << __func__;
Darin Petkov50308cd2011-06-01 18:25:07 -070039}
40
41DHCPProvider* DHCPProvider::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070042 return g_dhcp_provider.Pointer();
Darin Petkov50308cd2011-06-01 18:25:07 -070043}
44
Darin Petkova7b89492011-07-27 12:48:17 -070045void DHCPProvider::Init(ControlInterface *control_interface,
46 EventDispatcher *dispatcher,
47 GLib *glib) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070048 SLOG(DHCP, 2) << __func__;
Darin Petkovab565bb2011-10-06 02:55:51 -070049 listener_.reset(new DHCPCDListener(proxy_factory_->connection(), this));
Darin Petkovf7897bc2011-06-08 17:13:36 -070050 glib_ = glib;
Chris Masone19e30402011-07-19 15:48:47 -070051 control_interface_ = control_interface;
Darin Petkova7b89492011-07-27 12:48:17 -070052 dispatcher_ = dispatcher;
Darin Petkovd1b715b2011-06-02 21:21:22 -070053}
54
Paul Stewartd32f4842012-01-11 16:08:13 -080055DHCPConfigRefPtr DHCPProvider::CreateConfig(const string &device_name,
Paul Stewartd408fdf2012-05-07 17:15:57 -070056 const string &host_name,
57 const string &lease_file_suffix,
Paul Stewart75a68b92013-10-24 10:50:27 -070058 bool arp_gateway,
59 bool minimal_config) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070060 SLOG(DHCP, 2) << __func__ << " device: " << device_name;
Paul Stewartd408fdf2012-05-07 17:15:57 -070061 return new DHCPConfig(control_interface_,
62 dispatcher_,
63 this,
64 device_name,
65 host_name,
66 lease_file_suffix,
67 arp_gateway,
Paul Stewart75a68b92013-10-24 10:50:27 -070068 minimal_config,
Paul Stewartd408fdf2012-05-07 17:15:57 -070069 glib_);
Darin Petkovd1b715b2011-06-02 21:21:22 -070070}
71
Darin Petkov98dd6a02011-06-10 15:12:57 -070072DHCPConfigRefPtr DHCPProvider::GetConfig(int pid) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070073 SLOG(DHCP, 2) << __func__ << " pid: " << pid;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070074 PIDConfigMap::const_iterator it = configs_.find(pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070075 if (it == configs_.end()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -070076 return NULL;
Darin Petkovd1b715b2011-06-02 21:21:22 -070077 }
78 return it->second;
79}
80
Chris Masone2b105542011-06-22 10:58:09 -070081void DHCPProvider::BindPID(int pid, const DHCPConfigRefPtr &config) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070082 SLOG(DHCP, 2) << __func__ << " pid: " << pid;
Darin Petkovd1b715b2011-06-02 21:21:22 -070083 configs_[pid] = config;
84}
85
Darin Petkov92c43902011-06-09 20:46:06 -070086void DHCPProvider::UnbindPID(int pid) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070087 SLOG(DHCP, 2) << __func__ << " pid: " << pid;
Darin Petkovd1b715b2011-06-02 21:21:22 -070088 configs_.erase(pid);
89}
90
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080091void DHCPProvider::DestroyLease(const string &name) {
92 SLOG(DHCP, 2) << __func__ << " name: " << name;
Ben Chana0ddf462014-02-06 11:32:42 -080093 base::DeleteFile(root_.Append(
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080094 base::StringPrintf(kDHCPCDPathFormatLease,
95 name.c_str())), false);
96}
97
Darin Petkov50308cd2011-06-01 18:25:07 -070098} // namespace shill