Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 5 | #include "shill/wifi_endpoint.h" |
| 6 | |
| 7 | #include <base/logging.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 8 | #include <base/stl_util-inl.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 9 | #include <base/stringprintf.h> |
| 10 | #include <base/string_number_conversions.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 11 | #include <base/string_util.h> |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 13 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 14 | #include "shill/wpa_supplicant.h" |
| 15 | |
| 16 | using std::map; |
| 17 | using std::set; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 18 | using std::string; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 19 | using std::vector; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 20 | |
| 21 | namespace shill { |
| 22 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 23 | WiFiEndpoint::WiFiEndpoint( |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame^] | 24 | const map<string, ::DBus::Variant> &properties) |
| 25 | : frequency_(0) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 26 | // XXX will segfault on missing properties |
| 27 | ssid_ = |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 28 | properties.find(wpa_supplicant::kBSSPropertySSID)->second. |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 29 | operator std::vector<uint8_t>(); |
| 30 | bssid_ = |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 31 | properties.find(wpa_supplicant::kBSSPropertyBSSID)->second. |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 32 | operator std::vector<uint8_t>(); |
| 33 | signal_strength_ = |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 34 | properties.find(wpa_supplicant::kBSSPropertySignal)->second. |
| 35 | reader().get_int16(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame^] | 36 | map<string, ::DBus::Variant>::const_iterator it = |
| 37 | properties.find(wpa_supplicant::kBSSPropertyFrequency); |
| 38 | if (it != properties.end()) |
| 39 | frequency_ = it->second.reader().get_uint16(); |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 40 | network_mode_ = ParseMode( |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 41 | properties.find(wpa_supplicant::kBSSPropertyMode)->second); |
| 42 | security_mode_ = ParseSecurity(properties); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 43 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 44 | if (network_mode_.empty()) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 45 | // XXX log error? |
| 46 | } |
| 47 | |
| 48 | ssid_string_ = string(ssid_.begin(), ssid_.end()); |
| 49 | ssid_hex_ = base::HexEncode(&(*ssid_.begin()), ssid_.size()); |
| 50 | bssid_string_ = StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 51 | bssid_[0], bssid_[1], bssid_[2], |
| 52 | bssid_[3], bssid_[4], bssid_[5]); |
| 53 | bssid_hex_ = base::HexEncode(&(*bssid_.begin()), bssid_.size()); |
| 54 | } |
| 55 | |
| 56 | WiFiEndpoint::~WiFiEndpoint() {} |
| 57 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 58 | // static |
| 59 | uint32_t WiFiEndpoint::ModeStringToUint(const std::string &mode_string) { |
| 60 | if (mode_string == flimflam::kModeManaged) |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 61 | return wpa_supplicant::kNetworkModeInfrastructureInt; |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 62 | else if (mode_string == flimflam::kModeAdhoc) |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 63 | return wpa_supplicant::kNetworkModeAdHocInt; |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 64 | else |
| 65 | NOTIMPLEMENTED() << "Shill dos not support " << mode_string |
| 66 | << " mode at this time."; |
| 67 | return 0; |
| 68 | } |
| 69 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 70 | const vector<uint8_t> &WiFiEndpoint::ssid() const { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 71 | return ssid_; |
| 72 | } |
| 73 | |
| 74 | const string &WiFiEndpoint::ssid_string() const { |
| 75 | return ssid_string_; |
| 76 | } |
| 77 | |
| 78 | const string &WiFiEndpoint::ssid_hex() const { |
| 79 | return ssid_hex_; |
| 80 | } |
| 81 | |
| 82 | const string &WiFiEndpoint::bssid_string() const { |
| 83 | return bssid_string_; |
| 84 | } |
| 85 | |
| 86 | const string &WiFiEndpoint::bssid_hex() const { |
| 87 | return bssid_hex_; |
| 88 | } |
| 89 | |
| 90 | int16_t WiFiEndpoint::signal_strength() const { |
| 91 | return signal_strength_; |
| 92 | } |
| 93 | |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame^] | 94 | uint16 WiFiEndpoint::frequency() const { |
| 95 | return frequency_; |
| 96 | } |
| 97 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 98 | const string &WiFiEndpoint::network_mode() const { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 99 | return network_mode_; |
| 100 | } |
| 101 | |
| 102 | const string &WiFiEndpoint::security_mode() const { |
| 103 | return security_mode_; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 106 | // static |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 107 | WiFiEndpoint *WiFiEndpoint::MakeOpenEndpoint( |
| 108 | const string &ssid, const string &bssid) { |
| 109 | map <string, ::DBus::Variant> args; |
| 110 | ::DBus::MessageIter writer; |
| 111 | |
| 112 | writer = args[wpa_supplicant::kBSSPropertySSID].writer(); |
| 113 | writer << vector<uint8_t>(ssid.begin(), ssid.end()); |
| 114 | |
| 115 | string bssid_nosep; |
| 116 | RemoveChars(bssid, ":", &bssid_nosep); |
| 117 | vector<uint8_t> bssid_bytes; |
| 118 | base::HexStringToBytes(bssid_nosep, &bssid_bytes); |
| 119 | writer = args[wpa_supplicant::kBSSPropertyBSSID].writer(); |
| 120 | writer << bssid_bytes; |
| 121 | |
| 122 | args[wpa_supplicant::kBSSPropertySignal].writer().append_int16(0); |
| 123 | args[wpa_supplicant::kBSSPropertyMode].writer().append_string( |
| 124 | wpa_supplicant::kNetworkModeInfrastructure); |
| 125 | // We indicate this is an open BSS by leaving out all security properties. |
| 126 | |
| 127 | return new WiFiEndpoint(args); |
| 128 | } |
| 129 | |
| 130 | // static |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 131 | const char *WiFiEndpoint::ParseMode(const string &mode_string) { |
| 132 | if (mode_string == wpa_supplicant::kNetworkModeInfrastructure) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 133 | return flimflam::kModeManaged; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 134 | } else if (mode_string == wpa_supplicant::kNetworkModeAdHoc) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 135 | return flimflam::kModeAdhoc; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 136 | } else if (mode_string == wpa_supplicant::kNetworkModeAccessPoint) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 137 | NOTREACHED() << "Shill does not support AP mode at this time."; |
| 138 | return NULL; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 139 | } else { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 140 | NOTREACHED() << "Unknown WiFi endpoint mode!"; |
| 141 | return NULL; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 145 | // static |
| 146 | const char *WiFiEndpoint::ParseSecurity( |
| 147 | const map<string, ::DBus::Variant> &properties) { |
| 148 | set<KeyManagement> rsn_key_management_methods; |
| 149 | if (ContainsKey(properties, wpa_supplicant::kPropertyRSN)) { |
| 150 | // TODO(quiche): check type before casting |
| 151 | const map<string, ::DBus::Variant> rsn_properties( |
| 152 | properties.find(wpa_supplicant::kPropertyRSN)->second. |
| 153 | operator map<string, ::DBus::Variant>()); |
| 154 | ParseKeyManagementMethods(rsn_properties, &rsn_key_management_methods); |
| 155 | } |
| 156 | |
| 157 | set<KeyManagement> wpa_key_management_methods; |
| 158 | if (ContainsKey(properties, wpa_supplicant::kPropertyWPA)) { |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 159 | // TODO(quiche): check type before casting |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 160 | const map<string, ::DBus::Variant> rsn_properties( |
| 161 | properties.find(wpa_supplicant::kPropertyWPA)->second. |
| 162 | operator map<string, ::DBus::Variant>()); |
| 163 | ParseKeyManagementMethods(rsn_properties, &wpa_key_management_methods); |
| 164 | } |
| 165 | |
| 166 | bool wep_privacy = false; |
| 167 | if (ContainsKey(properties, wpa_supplicant::kPropertyPrivacy)) { |
| 168 | wep_privacy = properties.find(wpa_supplicant::kPropertyPrivacy)->second. |
| 169 | reader().get_bool(); |
| 170 | } |
| 171 | |
| 172 | if (ContainsKey(rsn_key_management_methods, kKeyManagement802_1x) || |
| 173 | ContainsKey(wpa_key_management_methods, kKeyManagement802_1x)) { |
| 174 | return flimflam::kSecurity8021x; |
| 175 | } else if (ContainsKey(rsn_key_management_methods, kKeyManagementPSK)) { |
| 176 | return flimflam::kSecurityRsn; |
| 177 | } else if (ContainsKey(wpa_key_management_methods, kKeyManagementPSK)) { |
| 178 | return flimflam::kSecurityWpa; |
| 179 | } else if (wep_privacy) { |
| 180 | return flimflam::kSecurityWep; |
| 181 | } else { |
| 182 | return flimflam::kSecurityNone; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // static |
| 187 | void WiFiEndpoint::ParseKeyManagementMethods( |
| 188 | const map<string, ::DBus::Variant> &security_method_properties, |
| 189 | set<KeyManagement> *key_management_methods) { |
| 190 | if (!ContainsKey(security_method_properties, |
| 191 | wpa_supplicant::kSecurityMethodPropertyKeyManagement)) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // TODO(quiche): check type before cast |
| 196 | const vector<string> key_management_vec = |
| 197 | security_method_properties. |
| 198 | find(wpa_supplicant::kSecurityMethodPropertyKeyManagement)->second. |
| 199 | operator vector<string>(); |
| 200 | for (vector<string>::const_iterator it = key_management_vec.begin(); |
| 201 | it != key_management_vec.end(); |
| 202 | ++it) { |
| 203 | if (EndsWith(*it, wpa_supplicant::kKeyManagementMethodSuffixEAP, true)) { |
| 204 | key_management_methods->insert(kKeyManagement802_1x); |
| 205 | } else if ( |
| 206 | EndsWith(*it, wpa_supplicant::kKeyManagementMethodSuffixPSK, true)) { |
| 207 | key_management_methods->insert(kKeyManagementPSK); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 212 | } // namespace shill |