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.
5 #include "chromeos/network/network_util.h"
7 #include "base/strings/string_tokenizer.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "chromeos/network/network_state.h"
11 #include "chromeos/network/network_state_handler.h"
12 #include "chromeos/network/onc/onc_signature.h"
13 #include "chromeos/network/onc/onc_translation_tables.h"
14 #include "chromeos/network/onc/onc_translator.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
19 WifiAccessPoint::WifiAccessPoint()
25 WifiAccessPoint::~WifiAccessPoint() {
28 CellularScanResult::CellularScanResult() {
31 CellularScanResult::~CellularScanResult() {
34 namespace network_util
{
36 std::string
PrefixLengthToNetmask(int32 prefix_length
) {
38 // Return the empty string for invalid inputs.
39 if (prefix_length
< 0 || prefix_length
> 32)
41 for (int i
= 0; i
< 4; i
++) {
43 if (prefix_length
>= 8) {
46 remainder
= prefix_length
;
51 int value
= remainder
== 0 ? 0 :
52 ((2L << (remainder
- 1)) - 1) << (8 - remainder
);
53 netmask
+= base::StringPrintf("%d", value
);
58 int32
NetmaskToPrefixLength(const std::string
& netmask
) {
60 int prefix_length
= 0;
61 base::StringTokenizer
t(netmask
, ".");
63 // If there are more than 4 numbers, then it's invalid.
67 std::string token
= t
.token();
68 // If we already found the last mask and the current one is not
69 // "0" then the netmask is invalid. For example, 255.224.255.0
70 if (prefix_length
/ 8 != count
) {
73 } else if (token
== "255") {
75 } else if (token
== "254") {
77 } else if (token
== "252") {
79 } else if (token
== "248") {
81 } else if (token
== "240") {
83 } else if (token
== "224") {
85 } else if (token
== "192") {
87 } else if (token
== "128") {
89 } else if (token
== "0") {
92 // mask is not a valid number.
102 std::string
FormattedMacAddress(const std::string
& shill_mac_address
) {
103 if (shill_mac_address
.size() % 2 != 0)
104 return shill_mac_address
;
106 for (size_t i
= 0; i
< shill_mac_address
.size(); ++i
) {
107 if ((i
!= 0) && (i
% 2 == 0))
108 result
.push_back(':');
109 result
.push_back(base::ToUpperASCII(shill_mac_address
[i
]));
114 bool ParseCellularScanResults(const base::ListValue
& list
,
115 std::vector
<CellularScanResult
>* scan_results
) {
116 scan_results
->clear();
117 scan_results
->reserve(list
.GetSize());
118 for (base::ListValue::const_iterator it
= list
.begin();
119 it
!= list
.end(); ++it
) {
120 if (!(*it
)->IsType(base::Value::TYPE_DICTIONARY
))
122 CellularScanResult scan_result
;
123 const base::DictionaryValue
* dict
=
124 static_cast<const base::DictionaryValue
*>(*it
);
125 // If the network id property is not present then this network cannot be
126 // connected to so don't include it in the results.
127 if (!dict
->GetStringWithoutPathExpansion(shill::kNetworkIdProperty
,
128 &scan_result
.network_id
))
130 dict
->GetStringWithoutPathExpansion(shill::kStatusProperty
,
131 &scan_result
.status
);
132 dict
->GetStringWithoutPathExpansion(shill::kLongNameProperty
,
133 &scan_result
.long_name
);
134 dict
->GetStringWithoutPathExpansion(shill::kShortNameProperty
,
135 &scan_result
.short_name
);
136 dict
->GetStringWithoutPathExpansion(shill::kTechnologyProperty
,
137 &scan_result
.technology
);
138 scan_results
->push_back(scan_result
);
143 scoped_ptr
<base::DictionaryValue
> TranslateNetworkStateToONC(
144 const NetworkState
* network
) {
145 // Get the properties from the NetworkState.
146 base::DictionaryValue shill_dictionary
;
147 network
->GetStateProperties(&shill_dictionary
);
149 scoped_ptr
<base::DictionaryValue
> onc_dictionary
=
150 TranslateShillServiceToONCPart(
151 shill_dictionary
, &onc::kNetworkWithStateSignature
);
152 return onc_dictionary
.Pass();
155 scoped_ptr
<base::ListValue
> TranslateNetworkListToONC(
156 NetworkTypePattern pattern
,
157 bool configured_only
,
160 bool debugging_properties
) {
161 NetworkStateHandler::NetworkStateList network_states
;
162 NetworkHandler::Get()->network_state_handler()->GetNetworkListByType(
163 pattern
, configured_only
, visible_only
, limit
, &network_states
);
165 scoped_ptr
<base::ListValue
> network_properties_list(new base::ListValue
);
166 for (NetworkStateHandler::NetworkStateList::iterator it
=
167 network_states
.begin();
168 it
!= network_states
.end();
170 scoped_ptr
<base::DictionaryValue
> onc_dictionary
=
171 TranslateNetworkStateToONC(*it
);
173 if (debugging_properties
) {
174 onc_dictionary
->SetBoolean("connectable", (*it
)->connectable());
175 onc_dictionary
->SetBoolean("visible", (*it
)->visible());
176 onc_dictionary
->SetString("profile_path", (*it
)->profile_path());
177 onc_dictionary
->SetString("service_path", (*it
)->path());
180 network_properties_list
->Append(onc_dictionary
.release());
182 return network_properties_list
.Pass();
185 std::string
TranslateONCTypeToShill(const std::string
& onc_type
) {
186 if (onc_type
== ::onc::network_type::kEthernet
)
187 return shill::kTypeEthernet
;
188 std::string shill_type
;
189 onc::TranslateStringToShill(onc::kNetworkTypeTable
, onc_type
, &shill_type
);
193 } // namespace network_util
194 } // namespace chromeos