blob: 611fe513cb5c4374c14fa5880b5d96a4f753da7f [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
7#include <base/logging.h>
8
Chris Masone19e30402011-07-19 15:48:47 -07009#include "shill/control_interface.h"
Chris Masone2b105542011-06-22 10:58:09 -070010#include "shill/dhcp_config.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070011#include "shill/dhcpcd_proxy.h"
Darin Petkovaceede32011-07-18 15:32:38 -070012#include "shill/proxy_factory.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070013#include "shill/scope_logger.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070014
Darin Petkovf65e9282011-06-21 14:29:56 -070015using std::string;
16
Darin Petkov50308cd2011-06-01 18:25:07 -070017namespace shill {
18
Eric Shienbrood3e20a232012-02-16 11:35:56 -050019// TODO(ers): not using LAZY_INSTANCE_INITIALIZER
20// because of http://crbug.com/114828
21static base::LazyInstance<DHCPProvider> g_dhcp_provider = {0, {{0}} };
Paul Stewart0d2ada32011-08-09 17:01:57 -070022
Darin Petkova7b89492011-07-27 12:48:17 -070023DHCPProvider::DHCPProvider()
Darin Petkovab565bb2011-10-06 02:55:51 -070024 : proxy_factory_(ProxyFactory::GetInstance()),
25 control_interface_(NULL),
Darin Petkova7b89492011-07-27 12:48:17 -070026 dispatcher_(NULL),
27 glib_(NULL) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070028 SLOG(DHCP, 2) << __func__;
Darin Petkov50308cd2011-06-01 18:25:07 -070029}
30
31DHCPProvider::~DHCPProvider() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070032 SLOG(DHCP, 2) << __func__;
Darin Petkov50308cd2011-06-01 18:25:07 -070033}
34
35DHCPProvider* DHCPProvider::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070036 return g_dhcp_provider.Pointer();
Darin Petkov50308cd2011-06-01 18:25:07 -070037}
38
Darin Petkova7b89492011-07-27 12:48:17 -070039void DHCPProvider::Init(ControlInterface *control_interface,
40 EventDispatcher *dispatcher,
41 GLib *glib) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070042 SLOG(DHCP, 2) << __func__;
Darin Petkovab565bb2011-10-06 02:55:51 -070043 listener_.reset(new DHCPCDListener(proxy_factory_->connection(), this));
Darin Petkovf7897bc2011-06-08 17:13:36 -070044 glib_ = glib;
Chris Masone19e30402011-07-19 15:48:47 -070045 control_interface_ = control_interface;
Darin Petkova7b89492011-07-27 12:48:17 -070046 dispatcher_ = dispatcher;
Darin Petkovd1b715b2011-06-02 21:21:22 -070047}
48
Paul Stewartd32f4842012-01-11 16:08:13 -080049DHCPConfigRefPtr DHCPProvider::CreateConfig(const string &device_name,
50 const string &host_name) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070051 SLOG(DHCP, 2) << __func__ << " device: " << device_name;
Darin Petkova7b89492011-07-27 12:48:17 -070052 return new DHCPConfig(
Paul Stewartd32f4842012-01-11 16:08:13 -080053 control_interface_, dispatcher_, this, device_name, host_name, glib_);
Darin Petkovd1b715b2011-06-02 21:21:22 -070054}
55
Darin Petkov98dd6a02011-06-10 15:12:57 -070056DHCPConfigRefPtr DHCPProvider::GetConfig(int pid) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070057 SLOG(DHCP, 2) << __func__ << " pid: " << pid;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070058 PIDConfigMap::const_iterator it = configs_.find(pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070059 if (it == configs_.end()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -070060 return NULL;
Darin Petkovd1b715b2011-06-02 21:21:22 -070061 }
62 return it->second;
63}
64
Chris Masone2b105542011-06-22 10:58:09 -070065void DHCPProvider::BindPID(int pid, const DHCPConfigRefPtr &config) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070066 SLOG(DHCP, 2) << __func__ << " pid: " << pid;
Darin Petkovd1b715b2011-06-02 21:21:22 -070067 configs_[pid] = config;
68}
69
Darin Petkov92c43902011-06-09 20:46:06 -070070void DHCPProvider::UnbindPID(int pid) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070071 SLOG(DHCP, 2) << __func__ << " pid: " << pid;
Darin Petkovd1b715b2011-06-02 21:21:22 -070072 configs_.erase(pid);
73}
74
Darin Petkov50308cd2011-06-01 18:25:07 -070075} // namespace shill