blob: ef53eb312f36fc064ec5f8ebd8faa96c63d3f815 [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) {
mukesh agrawal06175d72012-04-23 16:46:01 -070035 SLOG(DBus, 2) << __func__;
Darin Petkov50308cd2011-06-01 18:25:07 -070036 DBus::MessageIter ri = signal.reader();
37 unsigned int pid;
Gary Morain610977f2012-05-04 16:03:52 -070038 try {
39 ri >> pid;
40 } catch (const DBus::Error &e) {
41 LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what()
42 << " interface: " << signal.interface()
43 << " member: " << signal.member() << " path: " << signal.path();
44 }
Ben Chanfad4a0b2012-04-18 15:49:59 -070045 SLOG(DHCP, 2) << "sender(" << signal.sender() << ") pid(" << pid << ")";
Darin Petkovd1b715b2011-06-02 21:21:22 -070046
47 DHCPConfigRefPtr config = provider_->GetConfig(pid);
48 if (!config.get()) {
49 LOG(ERROR) << "Unknown DHCP client PID " << pid;
50 return;
51 }
Darin Petkovaceede32011-07-18 15:32:38 -070052 config->InitProxy(signal.sender());
Darin Petkove7cb7f82011-06-03 13:21:51 -070053
54 string reason;
Gary Morain610977f2012-05-04 16:03:52 -070055 try {
Darin Petkove7cb7f82011-06-03 13:21:51 -070056 ri >> reason;
Gary Morain610977f2012-05-04 16:03:52 -070057 } catch (const DBus::Error &e) {
58 LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what()
59 << " interface: " << signal.interface()
60 << " member: " << signal.member() << " path: " << signal.path();
61 }
Darin Petkove7cb7f82011-06-03 13:21:51 -070062 DHCPConfig::Configuration configuration;
Gary Morain610977f2012-05-04 16:03:52 -070063 try {
64 ri >> configuration;
65 } catch (const DBus::Error &e) {
66 LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what()
67 << " interface: " << signal.interface()
68 << " member: " << signal.member() << " path: " << signal.path();
69 }
Darin Petkove7cb7f82011-06-03 13:21:51 -070070 config->ProcessEventSignal(reason, configuration);
Darin Petkov50308cd2011-06-01 18:25:07 -070071}
72
Darin Petkovaceede32011-07-18 15:32:38 -070073void DHCPCDListener::Proxy::StatusChangedSignal(
74 const DBus::SignalMessage &signal) {
mukesh agrawal06175d72012-04-23 16:46:01 -070075 SLOG(DBus, 2) << __func__;
Darin Petkov50308cd2011-06-01 18:25:07 -070076 DBus::MessageIter ri = signal.reader();
77 unsigned int pid;
Gary Morain610977f2012-05-04 16:03:52 -070078 try {
79 ri >> pid;
80 } catch (const DBus::Error &e) {
81 LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what()
82 << " interface: " << signal.interface()
83 << " member: " << signal.member() << " path: " << signal.path();
84 }
Ben Chanfad4a0b2012-04-18 15:49:59 -070085 SLOG(DHCP, 2) << "sender(" << signal.sender() << ") pid(" << pid << ")";
Darin Petkovd1b715b2011-06-02 21:21:22 -070086
87 // Accept StatusChanged signals just to get the sender address and create an
88 // appropriate proxy for the PID/sender pair.
89 DHCPConfigRefPtr config = provider_->GetConfig(pid);
90 if (!config.get()) {
91 LOG(ERROR) << "Unknown DHCP client PID " << pid;
92 return;
93 }
Darin Petkovaceede32011-07-18 15:32:38 -070094 config->InitProxy(signal.sender());
Darin Petkov50308cd2011-06-01 18:25:07 -070095}
96
Darin Petkova7b89492011-07-27 12:48:17 -070097DHCPCDProxy::DHCPCDProxy(DBus::Connection *connection, const string &service)
Darin Petkovaceede32011-07-18 15:32:38 -070098 : proxy_(connection, service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070099 SLOG(DHCP, 2) << "DHCPCDProxy(service=" << service << ").";
Darin Petkovaceede32011-07-18 15:32:38 -0700100}
Darin Petkov50308cd2011-06-01 18:25:07 -0700101
Darin Petkovaceede32011-07-18 15:32:38 -0700102void DHCPCDProxy::Rebind(const string &interface) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700103 SLOG(DBus, 2) << __func__;
Gary Morain610977f2012-05-04 16:03:52 -0700104 try {
105 proxy_.Rebind(interface);
106 } catch (const DBus::Error &e) {
107 LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what()
108 << " interface: " << interface;
109 }
Darin Petkovaceede32011-07-18 15:32:38 -0700110}
111
112void DHCPCDProxy::Release(const string &interface) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700113 SLOG(DBus, 2) << __func__;
Gary Morain610977f2012-05-04 16:03:52 -0700114 try {
115 proxy_.Release(interface);
116 } catch (const DBus::Error &e) {
117 LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what()
118 << " interface: " << interface;
119 }
Darin Petkovaceede32011-07-18 15:32:38 -0700120}
121
122DHCPCDProxy::Proxy::Proxy(DBus::Connection *connection,
Darin Petkova7b89492011-07-27 12:48:17 -0700123 const string &service)
124 : DBus::ObjectProxy(*connection, kDBusPath, service.c_str()) {
Darin Petkov50308cd2011-06-01 18:25:07 -0700125 // Don't catch signals directly in this proxy because they will be dispatched
Darin Petkovaceede32011-07-18 15:32:38 -0700126 // to the client by the DHCPCD listener.
Darin Petkov50308cd2011-06-01 18:25:07 -0700127 _signals.erase("Event");
128 _signals.erase("StatusChanged");
129}
130
Darin Petkova7b89492011-07-27 12:48:17 -0700131DHCPCDProxy::Proxy::~Proxy() {}
132
mukesh agrawal1830fa12011-09-26 14:31:40 -0700133void DHCPCDProxy::Proxy::Event(
134 const uint32_t &/*pid*/,
135 const std::string &/*reason*/,
136 const DHCPConfig::Configuration &/*configuration*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700137 SLOG(DBus, 2) << __func__;
Darin Petkovd1b715b2011-06-02 21:21:22 -0700138 NOTREACHED();
Darin Petkov50308cd2011-06-01 18:25:07 -0700139}
140
mukesh agrawal1830fa12011-09-26 14:31:40 -0700141void DHCPCDProxy::Proxy::StatusChanged(const uint32_t &/*pid*/,
142 const std::string &/*status*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700143 SLOG(DBus, 2) << __func__;
Darin Petkovd1b715b2011-06-02 21:21:22 -0700144 NOTREACHED();
Darin Petkov50308cd2011-06-01 18:25:07 -0700145}
146
147} // namespace shill