Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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> |
| 8 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 9 | #include <base/logging.h> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 10 | #include <glib.h> |
| 11 | |
| 12 | #include "shill/dhcpcd_proxy.h" |
| 13 | #include "shill/dhcp_provider.h" |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 14 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 15 | using std::string; |
| 16 | using std::vector; |
| 17 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 18 | namespace shill { |
| 19 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 20 | const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress"; |
| 21 | const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers"; |
| 22 | const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName"; |
| 23 | const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch"; |
| 24 | const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress"; |
| 25 | const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU"; |
| 26 | const char DHCPConfig::kConfigurationKeyRouters[] = "Routers"; |
| 27 | const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR"; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 28 | const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd"; |
| 29 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 30 | |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 31 | DHCPConfig::DHCPConfig(DHCPProvider *provider, DeviceConstRefPtr device) |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 32 | : IPConfig(device), |
| 33 | provider_(provider), |
| 34 | pid_(0) { |
| 35 | VLOG(2) << __func__ << ": " << GetDeviceName(); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | DHCPConfig::~DHCPConfig() { |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 39 | VLOG(2) << __func__ << ": " << GetDeviceName(); |
| 40 | } |
| 41 | |
| 42 | bool DHCPConfig::Request() { |
| 43 | VLOG(2) << __func__ << ": " << GetDeviceName(); |
| 44 | if (!pid_) { |
| 45 | return Start(); |
| 46 | } |
| 47 | if (!proxy_.get()) { |
| 48 | LOG(ERROR) |
| 49 | << "Unable to acquire destination address before receiving request."; |
| 50 | return false; |
| 51 | } |
| 52 | return Renew(); |
| 53 | } |
| 54 | |
| 55 | bool DHCPConfig::Renew() { |
| 56 | VLOG(2) << __func__ << ": " << GetDeviceName(); |
| 57 | if (!pid_ || !proxy_.get()) { |
| 58 | return false; |
| 59 | } |
| 60 | proxy_->DoRebind(GetDeviceName()); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | void DHCPConfig::InitProxy(DBus::Connection *connection, const char *service) { |
| 65 | if (!proxy_.get()) { |
| 66 | proxy_.reset(new DHCPCDProxy(connection, service)); |
| 67 | } |
| 68 | } |
| 69 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 70 | void DHCPConfig::ProcessEventSignal(const std::string &reason, |
| 71 | const Configuration &configuration) { |
| 72 | LOG(INFO) << "Event reason: " << reason; |
| 73 | |
| 74 | IPConfig::Properties properties; |
| 75 | if (!ParseConfiguration(configuration, &properties)) { |
| 76 | LOG(ERROR) << "Unable to parse the new DHCP configuration -- ignored."; |
| 77 | return; |
| 78 | } |
| 79 | UpdateProperties(properties); |
| 80 | } |
| 81 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 82 | bool DHCPConfig::Start() { |
| 83 | VLOG(2) << __func__ << ": " << GetDeviceName(); |
| 84 | |
| 85 | char *argv[4], *envp[1]; |
| 86 | argv[0] = const_cast<char *>(kDHCPCDPath); |
| 87 | argv[1] = const_cast<char *>("-B"); // foreground |
| 88 | argv[2] = const_cast<char *>(GetDeviceName().c_str()); |
| 89 | argv[3] = NULL; |
| 90 | |
| 91 | envp[0] = NULL; |
| 92 | |
| 93 | GPid pid = 0; |
| 94 | if (!g_spawn_async(NULL, |
| 95 | argv, |
| 96 | envp, |
| 97 | G_SPAWN_DO_NOT_REAP_CHILD, |
| 98 | NULL, |
| 99 | NULL, |
| 100 | &pid, |
| 101 | NULL)) { |
| 102 | LOG(ERROR) << "Unable to spawn " << kDHCPCDPath; |
| 103 | return false; |
| 104 | } |
| 105 | pid_ = pid; |
| 106 | LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_; |
| 107 | provider_->BindPID(pid_, DHCPConfigRefPtr(this)); |
| 108 | // TODO(petkov): Add an exit watch to cleanup w/ g_spawn_close_pid. |
| 109 | return true; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 112 | string DHCPConfig::GetIPv4AddressString(unsigned int address) { |
| 113 | char str[INET_ADDRSTRLEN]; |
| 114 | if (inet_ntop(AF_INET, &address, str, arraysize(str))) { |
| 115 | return str; |
| 116 | } |
| 117 | LOG(ERROR) << "Unable to convert IPv4 address to string: " << address; |
| 118 | return ""; |
| 119 | } |
| 120 | |
| 121 | bool DHCPConfig::ParseConfiguration(const Configuration& configuration, |
| 122 | IPConfig::Properties *properties) { |
| 123 | VLOG(2) << __func__; |
| 124 | for (Configuration::const_iterator it = configuration.begin(); |
| 125 | it != configuration.end(); ++it) { |
| 126 | const string &key = it->first; |
| 127 | const DBus::Variant &value = it->second; |
| 128 | VLOG(2) << "Processing key: " << key; |
| 129 | if (key == kConfigurationKeyIPAddress) { |
| 130 | properties->address = GetIPv4AddressString(value.reader().get_uint32()); |
| 131 | if (properties->address.empty()) { |
| 132 | return false; |
| 133 | } |
| 134 | } else if (key == kConfigurationKeySubnetCIDR) { |
| 135 | properties->subnet_cidr = value.reader().get_byte(); |
| 136 | } else if (key == kConfigurationKeyBroadcastAddress) { |
| 137 | properties->broadcast_address = |
| 138 | GetIPv4AddressString(value.reader().get_uint32()); |
| 139 | if (properties->broadcast_address.empty()) { |
| 140 | return false; |
| 141 | } |
| 142 | } else if (key == kConfigurationKeyRouters) { |
| 143 | vector<unsigned int> routers; |
| 144 | DBus::MessageIter reader = value.reader(); |
| 145 | reader >> routers; |
| 146 | if (routers.empty()) { |
| 147 | LOG(ERROR) << "No routers provided."; |
| 148 | return false; |
| 149 | } |
| 150 | properties->gateway = GetIPv4AddressString(routers[0]); |
| 151 | if (properties->gateway.empty()) { |
| 152 | return false; |
| 153 | } |
| 154 | } else if (key == kConfigurationKeyDNS) { |
| 155 | vector<unsigned int> servers; |
| 156 | DBus::MessageIter reader = value.reader(); |
| 157 | reader >> servers; |
| 158 | vector<string> dns_servers; |
| 159 | for (vector<unsigned int>::const_iterator it = servers.begin(); |
| 160 | it != servers.end(); ++it) { |
| 161 | string server = GetIPv4AddressString(*it); |
| 162 | if (server.empty()) { |
| 163 | return false; |
| 164 | } |
| 165 | properties->dns_servers.push_back(server); |
| 166 | } |
| 167 | } else if (key == kConfigurationKeyDomainName) { |
| 168 | properties->domain_name = value.reader().get_string(); |
| 169 | } else if (key == kConfigurationKeyDomainSearch) { |
| 170 | DBus::MessageIter reader = value.reader(); |
| 171 | reader >> properties->domain_search; |
| 172 | } else if (key == kConfigurationKeyMTU) { |
| 173 | int mtu = value.reader().get_uint16(); |
| 174 | if (mtu >= 576) { |
| 175 | properties->mtu = mtu; |
| 176 | } |
| 177 | } else { |
| 178 | VLOG(2) << "Key ignored."; |
| 179 | } |
| 180 | } |
| 181 | return true; |
| 182 | } |
| 183 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 184 | } // namespace shill |