Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 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/dhcp_provider.h" |
| 6 | |
| 7 | #include <base/logging.h> |
| 8 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 9 | #include "shill/control_interface.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 10 | #include "shill/dhcp_config.h" |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 11 | #include "shill/dhcpcd_proxy.h" |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 12 | #include "shill/proxy_factory.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 13 | #include "shill/scope_logger.h" |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 14 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 15 | using std::string; |
| 16 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 17 | namespace shill { |
| 18 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 19 | // TODO(ers): not using LAZY_INSTANCE_INITIALIZER |
| 20 | // because of http://crbug.com/114828 |
| 21 | static base::LazyInstance<DHCPProvider> g_dhcp_provider = {0, {{0}} }; |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 22 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 23 | DHCPProvider::DHCPProvider() |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 24 | : proxy_factory_(ProxyFactory::GetInstance()), |
| 25 | control_interface_(NULL), |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 26 | dispatcher_(NULL), |
| 27 | glib_(NULL) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 28 | SLOG(DHCP, 2) << __func__; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | DHCPProvider::~DHCPProvider() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 32 | SLOG(DHCP, 2) << __func__; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | DHCPProvider* DHCPProvider::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 36 | return g_dhcp_provider.Pointer(); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 39 | void DHCPProvider::Init(ControlInterface *control_interface, |
| 40 | EventDispatcher *dispatcher, |
| 41 | GLib *glib) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 42 | SLOG(DHCP, 2) << __func__; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 43 | listener_.reset(new DHCPCDListener(proxy_factory_->connection(), this)); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 44 | glib_ = glib; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 45 | control_interface_ = control_interface; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 46 | dispatcher_ = dispatcher; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 49 | DHCPConfigRefPtr DHCPProvider::CreateConfig(const string &device_name, |
| 50 | const string &host_name) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 51 | SLOG(DHCP, 2) << __func__ << " device: " << device_name; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 52 | return new DHCPConfig( |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 53 | control_interface_, dispatcher_, this, device_name, host_name, glib_); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 56 | DHCPConfigRefPtr DHCPProvider::GetConfig(int pid) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 57 | SLOG(DHCP, 2) << __func__ << " pid: " << pid; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 58 | PIDConfigMap::const_iterator it = configs_.find(pid); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 59 | if (it == configs_.end()) { |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 60 | return NULL; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 61 | } |
| 62 | return it->second; |
| 63 | } |
| 64 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 65 | void DHCPProvider::BindPID(int pid, const DHCPConfigRefPtr &config) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 66 | SLOG(DHCP, 2) << __func__ << " pid: " << pid; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 67 | configs_[pid] = config; |
| 68 | } |
| 69 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 70 | void DHCPProvider::UnbindPID(int pid) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 71 | SLOG(DHCP, 2) << __func__ << " pid: " << pid; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 72 | configs_.erase(pid); |
| 73 | } |
| 74 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 75 | } // namespace shill |