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