blob: 3e291d8a884e133ea6f8242fd4b2114019068b9f [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/dhcpcd_proxy.h"
6
7#include <base/logging.h>
8
Darin Petkovd1b715b2011-06-02 21:21:22 -07009#include "shill/dhcp_provider.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070010#include "shill/scope_logger.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070011
12using std::string;
Darin Petkove7cb7f82011-06-03 13:21:51 -070013using std::vector;
Darin Petkovd1b715b2011-06-02 21:21:22 -070014
Darin Petkov50308cd2011-06-01 18:25:07 -070015namespace shill {
16
17const char DHCPCDProxy::kDBusInterfaceName[] = "org.chromium.dhcpcd";
18const char DHCPCDProxy::kDBusPath[] = "/org/chromium/dhcpcd";
19
Darin Petkovaceede32011-07-18 15:32:38 -070020DHCPCDListener::DHCPCDListener(DBus::Connection *connection,
21 DHCPProvider *provider)
22 : proxy_(connection, provider) {}
23
24DHCPCDListener::Proxy::Proxy(DBus::Connection *connection,
25 DHCPProvider *provider)
Darin Petkov50308cd2011-06-01 18:25:07 -070026 : DBus::InterfaceProxy(DHCPCDProxy::kDBusInterfaceName),
Darin Petkovd1b715b2011-06-02 21:21:22 -070027 DBus::ObjectProxy(*connection, DHCPCDProxy::kDBusPath),
28 provider_(provider) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070029 SLOG(DHCP, 2) << __func__;
Darin Petkovaceede32011-07-18 15:32:38 -070030 connect_signal(DHCPCDListener::Proxy, Event, EventSignal);
31 connect_signal(DHCPCDListener::Proxy, StatusChanged, StatusChangedSignal);
Darin Petkov50308cd2011-06-01 18:25:07 -070032}
33
Darin Petkovaceede32011-07-18 15:32:38 -070034void DHCPCDListener::Proxy::EventSignal(const DBus::SignalMessage &signal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070035 SLOG(DHCP, 2) << __func__;
Darin Petkov50308cd2011-06-01 18:25:07 -070036 DBus::MessageIter ri = signal.reader();
37 unsigned int pid;
38 ri >> pid;
Ben Chanfad4a0b2012-04-18 15:49:59 -070039 SLOG(DHCP, 2) << "sender(" << signal.sender() << ") pid(" << pid << ")";
Darin Petkovd1b715b2011-06-02 21:21:22 -070040
41 DHCPConfigRefPtr config = provider_->GetConfig(pid);
42 if (!config.get()) {
43 LOG(ERROR) << "Unknown DHCP client PID " << pid;
44 return;
45 }
Darin Petkovaceede32011-07-18 15:32:38 -070046 config->InitProxy(signal.sender());
Darin Petkove7cb7f82011-06-03 13:21:51 -070047
48 string reason;
49 ri >> reason;
50 DHCPConfig::Configuration configuration;
51 ri >> configuration;
52 config->ProcessEventSignal(reason, configuration);
Darin Petkov50308cd2011-06-01 18:25:07 -070053}
54
Darin Petkovaceede32011-07-18 15:32:38 -070055void DHCPCDListener::Proxy::StatusChangedSignal(
56 const DBus::SignalMessage &signal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070057 SLOG(DHCP, 2) << __func__;
Darin Petkov50308cd2011-06-01 18:25:07 -070058 DBus::MessageIter ri = signal.reader();
59 unsigned int pid;
60 ri >> pid;
Ben Chanfad4a0b2012-04-18 15:49:59 -070061 SLOG(DHCP, 2) << "sender(" << signal.sender() << ") pid(" << pid << ")";
Darin Petkovd1b715b2011-06-02 21:21:22 -070062
63 // Accept StatusChanged signals just to get the sender address and create an
64 // appropriate proxy for the PID/sender pair.
65 DHCPConfigRefPtr config = provider_->GetConfig(pid);
66 if (!config.get()) {
67 LOG(ERROR) << "Unknown DHCP client PID " << pid;
68 return;
69 }
Darin Petkovaceede32011-07-18 15:32:38 -070070 config->InitProxy(signal.sender());
Darin Petkov50308cd2011-06-01 18:25:07 -070071}
72
Darin Petkova7b89492011-07-27 12:48:17 -070073DHCPCDProxy::DHCPCDProxy(DBus::Connection *connection, const string &service)
Darin Petkovaceede32011-07-18 15:32:38 -070074 : proxy_(connection, service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070075 SLOG(DHCP, 2) << "DHCPCDProxy(service=" << service << ").";
Darin Petkovaceede32011-07-18 15:32:38 -070076}
Darin Petkov50308cd2011-06-01 18:25:07 -070077
Darin Petkovaceede32011-07-18 15:32:38 -070078void DHCPCDProxy::Rebind(const string &interface) {
79 proxy_.Rebind(interface);
80}
81
82void DHCPCDProxy::Release(const string &interface) {
83 proxy_.Release(interface);
84}
85
86DHCPCDProxy::Proxy::Proxy(DBus::Connection *connection,
Darin Petkova7b89492011-07-27 12:48:17 -070087 const string &service)
88 : DBus::ObjectProxy(*connection, kDBusPath, service.c_str()) {
Darin Petkov50308cd2011-06-01 18:25:07 -070089 // Don't catch signals directly in this proxy because they will be dispatched
Darin Petkovaceede32011-07-18 15:32:38 -070090 // to the client by the DHCPCD listener.
Darin Petkov50308cd2011-06-01 18:25:07 -070091 _signals.erase("Event");
92 _signals.erase("StatusChanged");
93}
94
Darin Petkova7b89492011-07-27 12:48:17 -070095DHCPCDProxy::Proxy::~Proxy() {}
96
mukesh agrawal1830fa12011-09-26 14:31:40 -070097void DHCPCDProxy::Proxy::Event(
98 const uint32_t &/*pid*/,
99 const std::string &/*reason*/,
100 const DHCPConfig::Configuration &/*configuration*/) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700101 NOTREACHED();
Darin Petkov50308cd2011-06-01 18:25:07 -0700102}
103
mukesh agrawal1830fa12011-09-26 14:31:40 -0700104void DHCPCDProxy::Proxy::StatusChanged(const uint32_t &/*pid*/,
105 const std::string &/*status*/) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700106 NOTREACHED();
Darin Petkov50308cd2011-06-01 18:25:07 -0700107}
108
109} // namespace shill