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 | |
Paul Stewart | 3095c07 | 2012-12-14 08:05:07 -0800 | [diff] [blame] | 7 | #include <dbus/dbus.h> |
| 8 | #include <string.h> |
| 9 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 10 | #include <limits> |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 11 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 12 | #include "shill/dhcp_provider.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 13 | #include "shill/logging.h" |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 14 | |
| 15 | using std::string; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 16 | using std::vector; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 17 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 18 | namespace shill { |
| 19 | |
| 20 | const char DHCPCDProxy::kDBusInterfaceName[] = "org.chromium.dhcpcd"; |
| 21 | const char DHCPCDProxy::kDBusPath[] = "/org/chromium/dhcpcd"; |
| 22 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 23 | DHCPCDListener::DHCPCDListener(DBus::Connection *connection, |
| 24 | DHCPProvider *provider) |
| 25 | : proxy_(connection, provider) {} |
| 26 | |
| 27 | DHCPCDListener::Proxy::Proxy(DBus::Connection *connection, |
| 28 | DHCPProvider *provider) |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 29 | : DBus::InterfaceProxy(DHCPCDProxy::kDBusInterfaceName), |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 30 | DBus::ObjectProxy(*connection, DHCPCDProxy::kDBusPath), |
| 31 | provider_(provider) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 32 | SLOG(DHCP, 2) << __func__; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 33 | connect_signal(DHCPCDListener::Proxy, Event, EventSignal); |
| 34 | connect_signal(DHCPCDListener::Proxy, StatusChanged, StatusChangedSignal); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 37 | void DHCPCDListener::Proxy::EventSignal(const DBus::SignalMessage &signal) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 38 | SLOG(DBus, 2) << __func__; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 39 | DBus::MessageIter ri = signal.reader(); |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 40 | unsigned int pid = std::numeric_limits<unsigned int>::max(); |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 41 | try { |
| 42 | ri >> pid; |
| 43 | } catch (const DBus::Error &e) { |
| 44 | LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what() |
| 45 | << " interface: " << signal.interface() |
| 46 | << " member: " << signal.member() << " path: " << signal.path(); |
| 47 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 48 | SLOG(DHCP, 2) << "sender(" << signal.sender() << ") pid(" << pid << ")"; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 49 | |
| 50 | DHCPConfigRefPtr config = provider_->GetConfig(pid); |
| 51 | if (!config.get()) { |
| 52 | LOG(ERROR) << "Unknown DHCP client PID " << pid; |
| 53 | return; |
| 54 | } |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 55 | config->InitProxy(signal.sender()); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 56 | |
| 57 | string reason; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 58 | try { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 59 | ri >> reason; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 60 | } catch (const DBus::Error &e) { |
| 61 | LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what() |
| 62 | << " interface: " << signal.interface() |
| 63 | << " member: " << signal.member() << " path: " << signal.path(); |
| 64 | } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 65 | DHCPConfig::Configuration configuration; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 66 | try { |
| 67 | ri >> configuration; |
| 68 | } catch (const DBus::Error &e) { |
| 69 | LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what() |
| 70 | << " interface: " << signal.interface() |
| 71 | << " member: " << signal.member() << " path: " << signal.path(); |
| 72 | } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 73 | config->ProcessEventSignal(reason, configuration); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 76 | void DHCPCDListener::Proxy::StatusChangedSignal( |
| 77 | const DBus::SignalMessage &signal) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 78 | SLOG(DBus, 2) << __func__; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 79 | DBus::MessageIter ri = signal.reader(); |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 80 | unsigned int pid = std::numeric_limits<unsigned int>::max(); |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 81 | try { |
| 82 | ri >> pid; |
| 83 | } catch (const DBus::Error &e) { |
| 84 | LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what() |
| 85 | << " interface: " << signal.interface() |
| 86 | << " member: " << signal.member() << " path: " << signal.path(); |
| 87 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 88 | SLOG(DHCP, 2) << "sender(" << signal.sender() << ") pid(" << pid << ")"; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 89 | |
| 90 | // Accept StatusChanged signals just to get the sender address and create an |
| 91 | // appropriate proxy for the PID/sender pair. |
| 92 | DHCPConfigRefPtr config = provider_->GetConfig(pid); |
| 93 | if (!config.get()) { |
| 94 | LOG(ERROR) << "Unknown DHCP client PID " << pid; |
| 95 | return; |
| 96 | } |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 97 | config->InitProxy(signal.sender()); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 100 | DHCPCDProxy::DHCPCDProxy(DBus::Connection *connection, const string &service) |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 101 | : proxy_(connection, service) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 102 | SLOG(DHCP, 2) << "DHCPCDProxy(service=" << service << ")."; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 103 | } |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 104 | |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 105 | void DHCPCDProxy::Rebind(const string &interface) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 106 | SLOG(DBus, 2) << __func__; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 107 | try { |
| 108 | proxy_.Rebind(interface); |
| 109 | } catch (const DBus::Error &e) { |
| 110 | LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what() |
| 111 | << " interface: " << interface; |
| 112 | } |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void DHCPCDProxy::Release(const string &interface) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 116 | SLOG(DBus, 2) << __func__; |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 117 | try { |
| 118 | proxy_.Release(interface); |
| 119 | } catch (const DBus::Error &e) { |
Paul Stewart | f7f7ce7 | 2012-12-14 08:05:07 -0800 | [diff] [blame] | 120 | if (!strcmp(e.name(), DBUS_ERROR_SERVICE_UNKNOWN) || |
| 121 | !strcmp(e.name(), DBUS_ERROR_NO_REPLY)) { |
Paul Stewart | 3095c07 | 2012-12-14 08:05:07 -0800 | [diff] [blame] | 122 | LOG(ERROR) << "dhcpcd daemon appears to have already exited."; |
| 123 | } else { |
| 124 | LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what() |
| 125 | << " interface: " << interface; |
| 126 | } |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 127 | } |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | DHCPCDProxy::Proxy::Proxy(DBus::Connection *connection, |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 131 | const string &service) |
| 132 | : DBus::ObjectProxy(*connection, kDBusPath, service.c_str()) { |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 133 | // 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] | 134 | // to the client by the DHCPCD listener. |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 135 | _signals.erase("Event"); |
| 136 | _signals.erase("StatusChanged"); |
| 137 | } |
| 138 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 139 | DHCPCDProxy::Proxy::~Proxy() {} |
| 140 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 141 | void DHCPCDProxy::Proxy::Event( |
| 142 | const uint32_t &/*pid*/, |
| 143 | const std::string &/*reason*/, |
| 144 | const DHCPConfig::Configuration &/*configuration*/) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 145 | SLOG(DBus, 2) << __func__; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 146 | NOTREACHED(); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 147 | } |
| 148 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 149 | void DHCPCDProxy::Proxy::StatusChanged(const uint32_t &/*pid*/, |
| 150 | const std::string &/*status*/) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 151 | SLOG(DBus, 2) << __func__; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 152 | NOTREACHED(); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace shill |