OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/cros/cros_network_functions.h" | 5 #include "chrome/browser/chromeos/cros/cros_network_functions.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/string_tokenizer.h" |
9 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
10 #include "base/values.h" | 11 #include "base/values.h" |
11 #include "chrome/browser/chromeos/cros/sms_watcher.h" | 12 #include "chrome/browser/chromeos/cros/sms_watcher.h" |
12 #include "chromeos/dbus/cashew_client.h" | 13 #include "chromeos/dbus/cashew_client.h" |
13 #include "chromeos/dbus/dbus_thread_manager.h" | 14 #include "chromeos/dbus/dbus_thread_manager.h" |
14 #include "chromeos/dbus/flimflam_device_client.h" | 15 #include "chromeos/dbus/flimflam_device_client.h" |
15 #include "chromeos/dbus/flimflam_ipconfig_client.h" | 16 #include "chromeos/dbus/flimflam_ipconfig_client.h" |
16 #include "chromeos/dbus/flimflam_manager_client.h" | 17 #include "chromeos/dbus/flimflam_manager_client.h" |
17 #include "chromeos/dbus/flimflam_network_client.h" | 18 #include "chromeos/dbus/flimflam_network_client.h" |
18 #include "chromeos/dbus/flimflam_profile_client.h" | 19 #include "chromeos/dbus/flimflam_profile_client.h" |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 return IPCONFIG_TYPE_BOOTP; | 225 return IPCONFIG_TYPE_BOOTP; |
225 if (type == flimflam::kTypeZeroConf) | 226 if (type == flimflam::kTypeZeroConf) |
226 return IPCONFIG_TYPE_ZEROCONF; | 227 return IPCONFIG_TYPE_ZEROCONF; |
227 if (type == flimflam::kTypeDHCP6) | 228 if (type == flimflam::kTypeDHCP6) |
228 return IPCONFIG_TYPE_DHCP6; | 229 return IPCONFIG_TYPE_DHCP6; |
229 if (type == flimflam::kTypePPP) | 230 if (type == flimflam::kTypePPP) |
230 return IPCONFIG_TYPE_PPP; | 231 return IPCONFIG_TYPE_PPP; |
231 return IPCONFIG_TYPE_UNKNOWN; | 232 return IPCONFIG_TYPE_UNKNOWN; |
232 } | 233 } |
233 | 234 |
234 // Converts a prefix length to a netmask. (for ipv4) | |
235 // e.g. a netmask of 255.255.255.0 has a prefixlen of 24 | |
236 std::string PrefixlenToNetmask(int32 prefixlen) { | |
237 std::string netmask; | |
238 for (int i = 0; i < 4; i++) { | |
239 int len = 8; | |
240 if (prefixlen >= 8) { | |
241 prefixlen -= 8; | |
242 } else { | |
243 len = prefixlen; | |
244 prefixlen = 0; | |
245 } | |
246 if (i > 0) | |
247 netmask += "."; | |
248 int num = len == 0 ? 0 : ((2L << (len - 1)) - 1) << (8 - len); | |
249 netmask += StringPrintf("%d", num); | |
250 } | |
251 return netmask; | |
252 } | |
253 | |
254 // Converts a list of name servers to a string. | 235 // Converts a list of name servers to a string. |
255 std::string ConvertNameSerersListToString(const base::ListValue& name_servers) { | 236 std::string ConvertNameSerersListToString(const base::ListValue& name_servers) { |
256 std::string result; | 237 std::string result; |
257 for (size_t i = 0; i != name_servers.GetSize(); ++i) { | 238 for (size_t i = 0; i != name_servers.GetSize(); ++i) { |
258 std::string name_server; | 239 std::string name_server; |
259 if (!name_servers.GetString(i, &name_server)) { | 240 if (!name_servers.GetString(i, &name_server)) { |
260 LOG(ERROR) << "name_servers[" << i << "] is not a string."; | 241 LOG(ERROR) << "name_servers[" << i << "] is not a string."; |
261 continue; | 242 continue; |
262 } | 243 } |
263 if (!result.empty()) | 244 if (!result.empty()) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 base::ListValue* name_servers = NULL; | 279 base::ListValue* name_servers = NULL; |
299 std::string name_servers_string; | 280 std::string name_servers_string; |
300 // store nameservers as a comma delimited list | 281 // store nameservers as a comma delimited list |
301 if (properties->GetListWithoutPathExpansion(flimflam::kNameServersProperty, | 282 if (properties->GetListWithoutPathExpansion(flimflam::kNameServersProperty, |
302 &name_servers)) { | 283 &name_servers)) { |
303 name_servers_string = ConvertNameSerersListToString(*name_servers); | 284 name_servers_string = ConvertNameSerersListToString(*name_servers); |
304 } else { | 285 } else { |
305 LOG(ERROR) << "Cannot get name servers."; | 286 LOG(ERROR) << "Cannot get name servers."; |
306 } | 287 } |
307 ipconfig_vector->push_back( | 288 ipconfig_vector->push_back( |
308 NetworkIPConfig(device_path, ParseIPConfigType(type_string), address, | 289 NetworkIPConfig(device_path, |
309 PrefixlenToNetmask(prefix_len), gateway, | 290 ParseIPConfigType(type_string), |
| 291 address, |
| 292 CrosPrefixLengthToNetmask(prefix_len), |
| 293 gateway, |
310 name_servers_string)); | 294 name_servers_string)); |
311 return true; | 295 return true; |
312 } | 296 } |
313 | 297 |
314 } // namespace | 298 } // namespace |
315 | 299 |
316 SMS::SMS() | 300 SMS::SMS() |
317 : validity(0), | 301 : validity(0), |
318 msgclass(0) { | 302 msgclass(0) { |
319 } | 303 } |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 if (!found_at_least_one_device) | 754 if (!found_at_least_one_device) |
771 return false; // No powered device found that has a 'Networks' array. | 755 return false; // No powered device found that has a 'Networks' array. |
772 return true; | 756 return true; |
773 } | 757 } |
774 | 758 |
775 void CrosConfigureService(const base::DictionaryValue& properties) { | 759 void CrosConfigureService(const base::DictionaryValue& properties) { |
776 DBusThreadManager::Get()->GetFlimflamManagerClient()->ConfigureService( | 760 DBusThreadManager::Get()->GetFlimflamManagerClient()->ConfigureService( |
777 properties, base::Bind(&DoNothing)); | 761 properties, base::Bind(&DoNothing)); |
778 } | 762 } |
779 | 763 |
| 764 std::string CrosPrefixLengthToNetmask(int32 prefix_length) { |
| 765 std::string netmask; |
| 766 // Return the empty string for invalid inputs. |
| 767 if (prefix_length < 0 || prefix_length > 32) |
| 768 return netmask; |
| 769 for (int i = 0; i < 4; i++) { |
| 770 int remainder = 8; |
| 771 if (prefix_length >= 8) { |
| 772 prefix_length -= 8; |
| 773 } else { |
| 774 remainder = prefix_length; |
| 775 prefix_length = 0; |
| 776 } |
| 777 if (i > 0) |
| 778 netmask += "."; |
| 779 int value = remainder == 0 ? 0 : |
| 780 ((2L << (remainder - 1)) - 1) << (8 - remainder); |
| 781 netmask += StringPrintf("%d", value); |
| 782 } |
| 783 return netmask; |
| 784 } |
| 785 |
| 786 int32 CrosNetmaskToPrefixLength(const std::string& netmask) { |
| 787 int count = 0; |
| 788 int prefix_length = 0; |
| 789 StringTokenizer t(netmask, "."); |
| 790 while (t.GetNext()) { |
| 791 // If there are more than 4 numbers, then it's invalid. |
| 792 if (count == 4) |
| 793 return -1; |
| 794 |
| 795 std::string token = t.token(); |
| 796 // If we already found the last mask and the current one is not |
| 797 // "0" then the netmask is invalid. For example, 255.224.255.0 |
| 798 if (prefix_length / 8 != count) { |
| 799 if (token != "0") |
| 800 return -1; |
| 801 } else if (token == "255") { |
| 802 prefix_length += 8; |
| 803 } else if (token == "254") { |
| 804 prefix_length += 7; |
| 805 } else if (token == "252") { |
| 806 prefix_length += 6; |
| 807 } else if (token == "248") { |
| 808 prefix_length += 5; |
| 809 } else if (token == "240") { |
| 810 prefix_length += 4; |
| 811 } else if (token == "224") { |
| 812 prefix_length += 3; |
| 813 } else if (token == "192") { |
| 814 prefix_length += 2; |
| 815 } else if (token == "128") { |
| 816 prefix_length += 1; |
| 817 } else if (token == "0") { |
| 818 prefix_length += 0; |
| 819 } else { |
| 820 // mask is not a valid number. |
| 821 return -1; |
| 822 } |
| 823 count++; |
| 824 } |
| 825 if (count < 4) |
| 826 return -1; |
| 827 return prefix_length; |
| 828 } |
| 829 |
780 } // namespace chromeos | 830 } // namespace chromeos |
OLD | NEW |