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/dhcpcd_proxy.h" |
| 6 | |
| 7 | #include <base/logging.h> |
| 8 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 9 | #include "shill/dhcp_provider.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 10 | #include "shill/scope_logger.h" |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 11 | |
| 12 | using std::string; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 13 | using std::vector; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 14 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 15 | namespace shill { |
| 16 | |
| 17 | const char DHCPCDProxy::kDBusInterfaceName[] = "org.chromium.dhcpcd"; |
| 18 | const char DHCPCDProxy::kDBusPath[] = "/org/chromium/dhcpcd"; |
| 19 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 20 | DHCPCDListener::DHCPCDListener(DBus::Connection *connection, |
| 21 | DHCPProvider *provider) |
| 22 | : proxy_(connection, provider) {} |
| 23 | |
| 24 | DHCPCDListener::Proxy::Proxy(DBus::Connection *connection, |
| 25 | DHCPProvider *provider) |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 26 | : DBus::InterfaceProxy(DHCPCDProxy::kDBusInterfaceName), |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 27 | DBus::ObjectProxy(*connection, DHCPCDProxy::kDBusPath), |
| 28 | provider_(provider) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 29 | SLOG(DHCP, 2) << __func__; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 30 | connect_signal(DHCPCDListener::Proxy, Event, EventSignal); |
| 31 | connect_signal(DHCPCDListener::Proxy, StatusChanged, StatusChangedSignal); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 34 | void DHCPCDListener::Proxy::EventSignal(const DBus::SignalMessage &signal) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 35 | SLOG(DBus, 2) << __func__; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 36 | DBus::MessageIter ri = signal.reader(); |
| 37 | unsigned int pid; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 38 | 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 Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 45 | SLOG(DHCP, 2) << "sender(" << signal.sender() << ") pid(" << pid << ")"; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 46 | |
| 47 | DHCPConfigRefPtr config = provider_->GetConfig(pid); |
| 48 | if (!config.get()) { |
| 49 | LOG(ERROR) << "Unknown DHCP client PID " << pid; |
| 50 | return; |
| 51 | } |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 52 | config->InitProxy(signal.sender()); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 53 | |
| 54 | string reason; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 55 | try { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 56 | ri >> reason; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 57 | } 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 Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 62 | DHCPConfig::Configuration configuration; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 63 | 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 Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 70 | config->ProcessEventSignal(reason, configuration); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 73 | void DHCPCDListener::Proxy::StatusChangedSignal( |
| 74 | const DBus::SignalMessage &signal) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 75 | SLOG(DBus, 2) << __func__; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 76 | DBus::MessageIter ri = signal.reader(); |
| 77 | unsigned int pid; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 78 | 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 Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 85 | SLOG(DHCP, 2) << "sender(" << signal.sender() << ") pid(" << pid << ")"; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 86 | |
| 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 Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 94 | config->InitProxy(signal.sender()); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 97 | DHCPCDProxy::DHCPCDProxy(DBus::Connection *connection, const string &service) |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 98 | : proxy_(connection, service) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 99 | SLOG(DHCP, 2) << "DHCPCDProxy(service=" << service << ")."; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 100 | } |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 101 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 102 | void DHCPCDProxy::Rebind(const string &interface) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 103 | SLOG(DBus, 2) << __func__; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 104 | 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 Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void DHCPCDProxy::Release(const string &interface) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 113 | SLOG(DBus, 2) << __func__; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 114 | 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 Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | DHCPCDProxy::Proxy::Proxy(DBus::Connection *connection, |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 123 | const string &service) |
| 124 | : DBus::ObjectProxy(*connection, kDBusPath, service.c_str()) { |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 125 | // Don't catch signals directly in this proxy because they will be dispatched |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 126 | // to the client by the DHCPCD listener. |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 127 | _signals.erase("Event"); |
| 128 | _signals.erase("StatusChanged"); |
| 129 | } |
| 130 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 131 | DHCPCDProxy::Proxy::~Proxy() {} |
| 132 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 133 | void DHCPCDProxy::Proxy::Event( |
| 134 | const uint32_t &/*pid*/, |
| 135 | const std::string &/*reason*/, |
| 136 | const DHCPConfig::Configuration &/*configuration*/) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 137 | SLOG(DBus, 2) << __func__; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 138 | NOTREACHED(); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 139 | } |
| 140 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 141 | void DHCPCDProxy::Proxy::StatusChanged(const uint32_t &/*pid*/, |
| 142 | const std::string &/*status*/) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 143 | SLOG(DBus, 2) << __func__; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 144 | NOTREACHED(); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace shill |