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/stringprintf.h"
9 #include "third_party/cros_system_api/dbus/service_constants.h"
13 SMS::SMS() : validity(0), msgclass(0) {
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 bool ParseCellularScanResults(
103 const ListValue
& list
, std::vector
<CellularScanResult
>* scan_results
) {
104 scan_results
->clear();
105 scan_results
->reserve(list
.GetSize());
106 for (ListValue::const_iterator it
= list
.begin(); it
!= list
.end(); ++it
) {
107 if (!(*it
)->IsType(base::Value::TYPE_DICTIONARY
))
109 CellularScanResult scan_result
;
110 const DictionaryValue
* dict
= static_cast<const DictionaryValue
*>(*it
);
111 // If the network id property is not present then this network cannot be
112 // connected to so don't include it in the results.
113 if (!dict
->GetStringWithoutPathExpansion(flimflam::kNetworkIdProperty
,
114 &scan_result
.network_id
))
116 dict
->GetStringWithoutPathExpansion(flimflam::kStatusProperty
,
117 &scan_result
.status
);
118 dict
->GetStringWithoutPathExpansion(flimflam::kLongNameProperty
,
119 &scan_result
.long_name
);
120 dict
->GetStringWithoutPathExpansion(flimflam::kShortNameProperty
,
121 &scan_result
.short_name
);
122 dict
->GetStringWithoutPathExpansion(flimflam::kTechnologyProperty
,
123 &scan_result
.technology
);
124 scan_results
->push_back(scan_result
);
129 } // namespace network_util
130 } // namespace chromeos