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 3 // found in the LICENSE file. 4 5 #include "chromeos/network/network_util.h" 6 7 #include "base/strings/string_tokenizer.h" 8 #include "base/strings/stringprintf.h" 9 #include "third_party/cros_system_api/dbus/service_constants.h" 10 11 namespace chromeos { 12 13 WifiAccessPoint::WifiAccessPoint() 14 : signal_strength(0), 15 signal_to_noise(0), 16 channel(0) { 17 } 18 19 WifiAccessPoint::~WifiAccessPoint() { 20 } 21 22 CellularScanResult::CellularScanResult() { 23 } 24 25 CellularScanResult::~CellularScanResult() { 26 } 27 28 namespace network_util { 29 30 std::string PrefixLengthToNetmask(int32 prefix_length) { 31 std::string netmask; 32 // Return the empty string for invalid inputs. 33 if (prefix_length < 0 || prefix_length > 32) 34 return netmask; 35 for (int i = 0; i < 4; i++) { 36 int remainder = 8; 37 if (prefix_length >= 8) { 38 prefix_length -= 8; 39 } else { 40 remainder = prefix_length; 41 prefix_length = 0; 42 } 43 if (i > 0) 44 netmask += "."; 45 int value = remainder == 0 ? 0 : 46 ((2L << (remainder - 1)) - 1) << (8 - remainder); 47 netmask += base::StringPrintf("%d", value); 48 } 49 return netmask; 50 } 51 52 int32 NetmaskToPrefixLength(const std::string& netmask) { 53 int count = 0; 54 int prefix_length = 0; 55 base::StringTokenizer t(netmask, "."); 56 while (t.GetNext()) { 57 // If there are more than 4 numbers, then it's invalid. 58 if (count == 4) 59 return -1; 60 61 std::string token = t.token(); 62 // If we already found the last mask and the current one is not 63 // "0" then the netmask is invalid. For example, 255.224.255.0 64 if (prefix_length / 8 != count) { 65 if (token != "0") 66 return -1; 67 } else if (token == "255") { 68 prefix_length += 8; 69 } else if (token == "254") { 70 prefix_length += 7; 71 } else if (token == "252") { 72 prefix_length += 6; 73 } else if (token == "248") { 74 prefix_length += 5; 75 } else if (token == "240") { 76 prefix_length += 4; 77 } else if (token == "224") { 78 prefix_length += 3; 79 } else if (token == "192") { 80 prefix_length += 2; 81 } else if (token == "128") { 82 prefix_length += 1; 83 } else if (token == "0") { 84 prefix_length += 0; 85 } else { 86 // mask is not a valid number. 87 return -1; 88 } 89 count++; 90 } 91 if (count < 4) 92 return -1; 93 return prefix_length; 94 } 95 96 bool ParseCellularScanResults( 97 const ListValue& list, std::vector<CellularScanResult>* scan_results) { 98 scan_results->clear(); 99 scan_results->reserve(list.GetSize()); 100 for (ListValue::const_iterator it = list.begin(); it != list.end(); ++it) { 101 if (!(*it)->IsType(base::Value::TYPE_DICTIONARY)) 102 return false; 103 CellularScanResult scan_result; 104 const DictionaryValue* dict = static_cast<const DictionaryValue*>(*it); 105 // If the network id property is not present then this network cannot be 106 // connected to so don't include it in the results. 107 if (!dict->GetStringWithoutPathExpansion(shill::kNetworkIdProperty, 108 &scan_result.network_id)) 109 continue; 110 dict->GetStringWithoutPathExpansion(shill::kStatusProperty, 111 &scan_result.status); 112 dict->GetStringWithoutPathExpansion(shill::kLongNameProperty, 113 &scan_result.long_name); 114 dict->GetStringWithoutPathExpansion(shill::kShortNameProperty, 115 &scan_result.short_name); 116 dict->GetStringWithoutPathExpansion(shill::kTechnologyProperty, 117 &scan_result.technology); 118 scan_results->push_back(scan_result); 119 } 120 return true; 121 } 122 123 } // namespace network_util 124 } // namespace chromeos 125