blob: a5a7c1faf9b47d059b89efacc16e4dca1e0902cf [file] [log] [blame]
Darin Petkov50308cd2011-06-01 18:25:07 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
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/dhcpcd_proxy.h"
6
7#include <base/logging.h>
8
Darin Petkovd1b715b2011-06-02 21:21:22 -07009#include "shill/dhcp_provider.h"
10
11using std::string;
Darin Petkove7cb7f82011-06-03 13:21:51 -070012using std::vector;
Darin Petkovd1b715b2011-06-02 21:21:22 -070013
Darin Petkov50308cd2011-06-01 18:25:07 -070014namespace shill {
15
16const char DHCPCDProxy::kDBusInterfaceName[] = "org.chromium.dhcpcd";
17const char DHCPCDProxy::kDBusPath[] = "/org/chromium/dhcpcd";
18
Darin Petkovaceede32011-07-18 15:32:38 -070019DHCPCDListener::DHCPCDListener(DBus::Connection *connection,
20 DHCPProvider *provider)
21 : proxy_(connection, provider) {}
22
23DHCPCDListener::Proxy::Proxy(DBus::Connection *connection,
24 DHCPProvider *provider)
Darin Petkov50308cd2011-06-01 18:25:07 -070025 : DBus::InterfaceProxy(DHCPCDProxy::kDBusInterfaceName),
Darin Petkovd1b715b2011-06-02 21:21:22 -070026 DBus::ObjectProxy(*connection, DHCPCDProxy::kDBusPath),
27 provider_(provider) {
Darin Petkov50308cd2011-06-01 18:25:07 -070028 VLOG(2) << __func__;
Darin Petkovaceede32011-07-18 15:32:38 -070029 connect_signal(DHCPCDListener::Proxy, Event, EventSignal);
30 connect_signal(DHCPCDListener::Proxy, StatusChanged, StatusChangedSignal);
Darin Petkov50308cd2011-06-01 18:25:07 -070031}
32
Darin Petkovaceede32011-07-18 15:32:38 -070033void DHCPCDListener::Proxy::EventSignal(const DBus::SignalMessage &signal) {
Darin Petkov50308cd2011-06-01 18:25:07 -070034 VLOG(2) << __func__;
35 DBus::MessageIter ri = signal.reader();
36 unsigned int pid;
37 ri >> pid;
38 VLOG(2) << "sender(" << signal.sender() << ") pid(" << pid << ")";
Darin Petkovd1b715b2011-06-02 21:21:22 -070039
40 DHCPConfigRefPtr config = provider_->GetConfig(pid);
41 if (!config.get()) {
42 LOG(ERROR) << "Unknown DHCP client PID " << pid;
43 return;
44 }
Darin Petkovaceede32011-07-18 15:32:38 -070045 config->InitProxy(signal.sender());
Darin Petkove7cb7f82011-06-03 13:21:51 -070046
47 string reason;
48 ri >> reason;
49 DHCPConfig::Configuration configuration;
50 ri >> configuration;
51 config->ProcessEventSignal(reason, configuration);
Darin Petkov50308cd2011-06-01 18:25:07 -070052}
53
Darin Petkovaceede32011-07-18 15:32:38 -070054void DHCPCDListener::Proxy::StatusChangedSignal(
55 const DBus::SignalMessage &signal) {
Darin Petkov50308cd2011-06-01 18:25:07 -070056 VLOG(2) << __func__;
57 DBus::MessageIter ri = signal.reader();
58 unsigned int pid;
59 ri >> pid;
60 VLOG(2) << "sender(" << signal.sender() << ") pid(" << pid << ")";
Darin Petkovd1b715b2011-06-02 21:21:22 -070061
62 // Accept StatusChanged signals just to get the sender address and create an
63 // appropriate proxy for the PID/sender pair.
64 DHCPConfigRefPtr config = provider_->GetConfig(pid);
65 if (!config.get()) {
66 LOG(ERROR) << "Unknown DHCP client PID " << pid;
67 return;
68 }
Darin Petkovaceede32011-07-18 15:32:38 -070069 config->InitProxy(signal.sender());
Darin Petkov50308cd2011-06-01 18:25:07 -070070}
71
Darin Petkova7b89492011-07-27 12:48:17 -070072DHCPCDProxy::DHCPCDProxy(DBus::Connection *connection, const string &service)
Darin Petkovaceede32011-07-18 15:32:38 -070073 : proxy_(connection, service) {
Darin Petkovd1b715b2011-06-02 21:21:22 -070074 VLOG(2) << "DHCPCDProxy(service=" << service << ").";
Darin Petkovaceede32011-07-18 15:32:38 -070075}
Darin Petkov50308cd2011-06-01 18:25:07 -070076
Darin Petkovaceede32011-07-18 15:32:38 -070077void DHCPCDProxy::Rebind(const string &interface) {
78 proxy_.Rebind(interface);
79}
80
81void DHCPCDProxy::Release(const string &interface) {
82 proxy_.Release(interface);
83}
84
85DHCPCDProxy::Proxy::Proxy(DBus::Connection *connection,
Darin Petkova7b89492011-07-27 12:48:17 -070086 const string &service)
87 : DBus::ObjectProxy(*connection, kDBusPath, service.c_str()) {
Darin Petkov50308cd2011-06-01 18:25:07 -070088 // Don't catch signals directly in this proxy because they will be dispatched
Darin Petkovaceede32011-07-18 15:32:38 -070089 // to the client by the DHCPCD listener.
Darin Petkov50308cd2011-06-01 18:25:07 -070090 _signals.erase("Event");
91 _signals.erase("StatusChanged");
92}
93
Darin Petkova7b89492011-07-27 12:48:17 -070094DHCPCDProxy::Proxy::~Proxy() {}
95
mukesh agrawal1830fa12011-09-26 14:31:40 -070096void DHCPCDProxy::Proxy::Event(
97 const uint32_t &/*pid*/,
98 const std::string &/*reason*/,
99 const DHCPConfig::Configuration &/*configuration*/) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700100 NOTREACHED();
Darin Petkov50308cd2011-06-01 18:25:07 -0700101}
102
mukesh agrawal1830fa12011-09-26 14:31:40 -0700103void DHCPCDProxy::Proxy::StatusChanged(const uint32_t &/*pid*/,
104 const std::string &/*status*/) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700105 NOTREACHED();
Darin Petkov50308cd2011-06-01 18:25:07 -0700106}
107
108} // namespace shill