Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [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/dhcp_config.h" |
| 6 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 8 | #include <sys/wait.h> |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 9 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 10 | #include <base/file_util.h> |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 12 | #include <base/stringprintf.h> |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 14 | |
| 15 | #include "shill/dhcpcd_proxy.h" |
| 16 | #include "shill/dhcp_provider.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 17 | #include "shill/event_dispatcher.h" |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 18 | #include "shill/glib.h" |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 19 | #include "shill/ip_address.h" |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 20 | #include "shill/proxy_factory.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 21 | #include "shill/scope_logger.h" |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 22 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 23 | using std::string; |
| 24 | using std::vector; |
| 25 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 26 | namespace shill { |
| 27 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 28 | // static |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 29 | const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress"; |
| 30 | const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers"; |
| 31 | const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName"; |
| 32 | const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch"; |
| 33 | const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress"; |
| 34 | const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU"; |
| 35 | const char DHCPConfig::kConfigurationKeyRouters[] = "Routers"; |
| 36 | const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR"; |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 37 | const int DHCPConfig::kDHCPCDExitPollMilliseconds = 50; |
| 38 | const int DHCPConfig::kDHCPCDExitWaitMilliseconds = 3000; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 39 | const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd"; |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 40 | const char DHCPConfig::kDHCPCDPathFormatLease[] = |
| 41 | "var/lib/dhcpcd/dhcpcd-%s.lease"; |
Jorge Lucangeli Obes | 2f3169d | 2012-04-25 11:38:25 -0700 | [diff] [blame] | 42 | const char DHCPConfig::kDHCPCDPathFormatPID[] = |
| 43 | "var/run/dhcpcd/dhcpcd-%s.pid"; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 44 | const int DHCPConfig::kDHCPTimeoutSeconds = 30; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 45 | const int DHCPConfig::kMinMTU = 576; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 46 | const char DHCPConfig::kReasonBound[] = "BOUND"; |
| 47 | const char DHCPConfig::kReasonFail[] = "FAIL"; |
| 48 | const char DHCPConfig::kReasonRebind[] = "REBIND"; |
| 49 | const char DHCPConfig::kReasonReboot[] = "REBOOT"; |
| 50 | const char DHCPConfig::kReasonRenew[] = "RENEW"; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 51 | // static |
| 52 | const char DHCPConfig::kType[] = "dhcp"; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 53 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 54 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 55 | DHCPConfig::DHCPConfig(ControlInterface *control_interface, |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 56 | EventDispatcher *dispatcher, |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 57 | DHCPProvider *provider, |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 58 | const string &device_name, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 59 | const string &request_hostname, |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 60 | const string &lease_file_suffix, |
| 61 | bool arp_gateway, |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 62 | GLib *glib) |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 63 | : IPConfig(control_interface, device_name, kType), |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 64 | proxy_factory_(ProxyFactory::GetInstance()), |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 65 | provider_(provider), |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 66 | request_hostname_(request_hostname), |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 67 | lease_file_suffix_(lease_file_suffix), |
| 68 | arp_gateway_(arp_gateway), |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 69 | pid_(0), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 70 | child_watch_tag_(0), |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 71 | lease_acquisition_timeout_seconds_(kDHCPTimeoutSeconds), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 72 | root_("/"), |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 73 | weak_ptr_factory_(this), |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 74 | dispatcher_(dispatcher), |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 75 | glib_(glib) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 76 | SLOG(DHCP, 2) << __func__ << ": " << device_name; |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 77 | if (lease_file_suffix_.empty()) { |
| 78 | lease_file_suffix_ = device_name; |
| 79 | } |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | DHCPConfig::~DHCPConfig() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 83 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 84 | |
| 85 | // Don't leave behind dhcpcd running. |
| 86 | Stop(); |
| 87 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 88 | // Make sure we don't get any callbacks to the destroyed instance. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 89 | CleanupClientState(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 92 | bool DHCPConfig::RequestIP() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 93 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 94 | if (!pid_) { |
| 95 | return Start(); |
| 96 | } |
| 97 | if (!proxy_.get()) { |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 98 | LOG(ERROR) << "Unable to request IP before acquiring destination."; |
| 99 | return Restart(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 100 | } |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 101 | return RenewIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 104 | bool DHCPConfig::RenewIP() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 105 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 106 | if (!pid_) { |
| 107 | return false; |
| 108 | } |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 109 | proxy_->Rebind(device_name()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 110 | StartDHCPTimeout(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 114 | bool DHCPConfig::ReleaseIP() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 115 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 116 | if (!pid_) { |
| 117 | return true; |
| 118 | } |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 119 | if (proxy_.get()) { |
| 120 | proxy_->Release(device_name()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 121 | } |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 122 | Stop(); |
| 123 | return true; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 126 | void DHCPConfig::InitProxy(const string &service) { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 127 | if (!proxy_.get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 128 | SLOG(DHCP, 2) << "Init DHCP Proxy: " << device_name() << " at " << service; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 129 | proxy_.reset(proxy_factory_->CreateDHCPProxy(service)); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 133 | void DHCPConfig::ProcessEventSignal(const string &reason, |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 134 | const Configuration &configuration) { |
| 135 | LOG(INFO) << "Event reason: " << reason; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 136 | if (reason == kReasonFail) { |
| 137 | LOG(ERROR) << "Received failure event from DHCP client."; |
| 138 | UpdateProperties(IPConfig::Properties(), false); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 139 | return; |
| 140 | } |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 141 | if (reason != kReasonBound && |
| 142 | reason != kReasonRebind && |
| 143 | reason != kReasonReboot && |
| 144 | reason != kReasonRenew) { |
| 145 | LOG(WARNING) << "Event ignored."; |
| 146 | return; |
| 147 | } |
| 148 | IPConfig::Properties properties; |
| 149 | CHECK(ParseConfiguration(configuration, &properties)); |
| 150 | UpdateProperties(properties, true); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 151 | } |
| 152 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 153 | void DHCPConfig::UpdateProperties(const Properties &properties, bool success) { |
| 154 | StopDHCPTimeout(); |
| 155 | IPConfig::UpdateProperties(properties, success); |
| 156 | } |
| 157 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 158 | bool DHCPConfig::Start() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 159 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 160 | |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 161 | vector<char *> args; |
| 162 | args.push_back(const_cast<char *>(kDHCPCDPath)); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 163 | args.push_back(const_cast<char *>("-B")); // Run in foreground. |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 164 | if (!request_hostname_.empty()) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 165 | args.push_back(const_cast<char *>("-h")); // Request hostname from server. |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 166 | args.push_back(const_cast<char *>(request_hostname_.c_str())); |
| 167 | } |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 168 | if (arp_gateway_) { |
| 169 | args.push_back(const_cast<char *>("-R")); // ARP for default gateway. |
| 170 | } |
| 171 | string interface_arg(device_name()); |
| 172 | if (lease_file_suffix_ != device_name()) { |
| 173 | interface_arg = base::StringPrintf("%s=%s", device_name().c_str(), |
| 174 | lease_file_suffix_.c_str()); |
| 175 | } |
| 176 | args.push_back(const_cast<char *>(interface_arg.c_str())); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 177 | args.push_back(NULL); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 178 | char *envp[1] = { NULL }; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 179 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 180 | CHECK(!pid_); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 181 | if (!glib_->SpawnAsync(NULL, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 182 | args.data(), |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 183 | envp, |
| 184 | G_SPAWN_DO_NOT_REAP_CHILD, |
| 185 | NULL, |
| 186 | NULL, |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 187 | &pid_, |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 188 | NULL)) { |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 189 | LOG(ERROR) << "Unable to spawn " << kDHCPCDPath; |
| 190 | return false; |
| 191 | } |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 192 | LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 193 | provider_->BindPID(pid_, this); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 194 | CHECK(!child_watch_tag_); |
| 195 | child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 196 | StartDHCPTimeout(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 197 | return true; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 200 | void DHCPConfig::Stop() { |
| 201 | if (pid_) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 202 | SLOG(DHCP, 2) << "Terminating " << pid_; |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 203 | if (kill(pid_, SIGTERM) < 0) { |
| 204 | PLOG(ERROR); |
| 205 | return; |
| 206 | } |
| 207 | pid_t ret; |
| 208 | int num_iterations = |
| 209 | kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds; |
| 210 | for (int count = 0; count < num_iterations; ++count) { |
| 211 | ret = waitpid(pid_, NULL, WNOHANG); |
| 212 | if (ret == pid_ || ret == -1) |
| 213 | break; |
| 214 | usleep(kDHCPCDExitPollMilliseconds * 1000); |
| 215 | if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd. |
| 216 | kill(pid_, SIGKILL); |
| 217 | } |
| 218 | if (ret != pid_) |
| 219 | PLOG(ERROR); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 220 | } |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 221 | StopDHCPTimeout(); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 224 | bool DHCPConfig::Restart() { |
| 225 | // Check to ensure that this instance doesn't get destroyed in the middle of |
| 226 | // this call. If stopping a running client while there's only one reference to |
| 227 | // this instance, we will end up destroying it when the PID is unbound from |
| 228 | // the Provider. Since the Provider doesn't invoke Restart, this would mean |
| 229 | // that Restart was erroneously executed through a bare reference. |
| 230 | CHECK(!pid_ || !HasOneRef()); |
| 231 | Stop(); |
| 232 | if (pid_) { |
| 233 | provider_->UnbindPID(pid_); |
| 234 | } |
| 235 | CleanupClientState(); |
| 236 | return Start(); |
| 237 | } |
| 238 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 239 | string DHCPConfig::GetIPv4AddressString(unsigned int address) { |
| 240 | char str[INET_ADDRSTRLEN]; |
| 241 | if (inet_ntop(AF_INET, &address, str, arraysize(str))) { |
| 242 | return str; |
| 243 | } |
| 244 | LOG(ERROR) << "Unable to convert IPv4 address to string: " << address; |
| 245 | return ""; |
| 246 | } |
| 247 | |
| 248 | bool DHCPConfig::ParseConfiguration(const Configuration& configuration, |
| 249 | IPConfig::Properties *properties) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 250 | SLOG(DHCP, 2) << __func__; |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 251 | properties->method = flimflam::kTypeDHCP; |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 252 | properties->address_family = IPAddress::kFamilyIPv4; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 253 | for (Configuration::const_iterator it = configuration.begin(); |
| 254 | it != configuration.end(); ++it) { |
| 255 | const string &key = it->first; |
| 256 | const DBus::Variant &value = it->second; |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 257 | SLOG(DHCP, 2) << "Processing key: " << key; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 258 | if (key == kConfigurationKeyIPAddress) { |
| 259 | properties->address = GetIPv4AddressString(value.reader().get_uint32()); |
| 260 | if (properties->address.empty()) { |
| 261 | return false; |
| 262 | } |
| 263 | } else if (key == kConfigurationKeySubnetCIDR) { |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 264 | properties->subnet_prefix = value.reader().get_byte(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 265 | } else if (key == kConfigurationKeyBroadcastAddress) { |
| 266 | properties->broadcast_address = |
| 267 | GetIPv4AddressString(value.reader().get_uint32()); |
| 268 | if (properties->broadcast_address.empty()) { |
| 269 | return false; |
| 270 | } |
| 271 | } else if (key == kConfigurationKeyRouters) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 272 | vector<unsigned int> routers = value.operator vector<unsigned int>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 273 | if (routers.empty()) { |
| 274 | LOG(ERROR) << "No routers provided."; |
| 275 | return false; |
| 276 | } |
| 277 | properties->gateway = GetIPv4AddressString(routers[0]); |
| 278 | if (properties->gateway.empty()) { |
| 279 | return false; |
| 280 | } |
| 281 | } else if (key == kConfigurationKeyDNS) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 282 | vector<unsigned int> servers = value.operator vector<unsigned int>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 283 | for (vector<unsigned int>::const_iterator it = servers.begin(); |
| 284 | it != servers.end(); ++it) { |
| 285 | string server = GetIPv4AddressString(*it); |
| 286 | if (server.empty()) { |
| 287 | return false; |
| 288 | } |
| 289 | properties->dns_servers.push_back(server); |
| 290 | } |
| 291 | } else if (key == kConfigurationKeyDomainName) { |
| 292 | properties->domain_name = value.reader().get_string(); |
| 293 | } else if (key == kConfigurationKeyDomainSearch) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 294 | properties->domain_search = value.operator vector<string>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 295 | } else if (key == kConfigurationKeyMTU) { |
| 296 | int mtu = value.reader().get_uint16(); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 297 | if (mtu >= kMinMTU) { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 298 | properties->mtu = mtu; |
| 299 | } |
| 300 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 301 | SLOG(DHCP, 2) << "Key ignored."; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | return true; |
| 305 | } |
| 306 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 307 | void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 308 | SLOG(DHCP, 2) << "pid " << pid << " exit status " << status; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 309 | DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data); |
| 310 | config->child_watch_tag_ = 0; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 311 | CHECK_EQ(pid, config->pid_); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 312 | config->CleanupClientState(); |
| 313 | |
| 314 | // |config| instance may be destroyed after this call. |
| 315 | config->provider_->UnbindPID(pid); |
| 316 | } |
| 317 | |
| 318 | void DHCPConfig::CleanupClientState() { |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 319 | if (child_watch_tag_) { |
| 320 | glib_->SourceRemove(child_watch_tag_); |
| 321 | child_watch_tag_ = 0; |
| 322 | } |
| 323 | if (pid_) { |
| 324 | glib_->SpawnClosePID(pid_); |
| 325 | pid_ = 0; |
| 326 | } |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 327 | proxy_.reset(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 328 | if (lease_file_suffix_ == device_name()) { |
| 329 | // If the lease file suffix was left as default, clean it up at exit. |
| 330 | file_util::Delete(root_.Append( |
| 331 | base::StringPrintf(kDHCPCDPathFormatLease, |
| 332 | device_name().c_str())), false); |
| 333 | } |
| 334 | file_util::Delete(root_.Append( |
| 335 | base::StringPrintf(kDHCPCDPathFormatPID, device_name().c_str())), false); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 336 | } |
| 337 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame^] | 338 | void DHCPConfig::StartDHCPTimeout() { |
| 339 | lease_acquisition_timeout_callback_.Reset( |
| 340 | Bind(&DHCPConfig::ProcessDHCPTimeout, weak_ptr_factory_.GetWeakPtr())); |
| 341 | dispatcher_->PostDelayedTask( |
| 342 | lease_acquisition_timeout_callback_.callback(), |
| 343 | lease_acquisition_timeout_seconds_ * 1000); |
| 344 | } |
| 345 | |
| 346 | void DHCPConfig::StopDHCPTimeout() { |
| 347 | lease_acquisition_timeout_callback_.Cancel(); |
| 348 | } |
| 349 | |
| 350 | void DHCPConfig::ProcessDHCPTimeout() { |
| 351 | LOG(ERROR) << "Timed out waiting for DHCP lease on " << device_name() << " " |
| 352 | << "(after " << lease_acquisition_timeout_seconds_ << " seconds)."; |
| 353 | UpdateProperties(IPConfig::Properties(), false); |
| 354 | } |
| 355 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 356 | } // namespace shill |