mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS 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 | |
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( |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 24 | const map<string, ::DBus::Variant> &properties) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 25 | // XXX will segfault on missing properties |
| 26 | ssid_ = |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 27 | properties.find(wpa_supplicant::kBSSPropertySSID)->second. |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 28 | operator std::vector<uint8_t>(); |
| 29 | bssid_ = |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 30 | properties.find(wpa_supplicant::kBSSPropertyBSSID)->second. |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 31 | operator std::vector<uint8_t>(); |
| 32 | signal_strength_ = |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame^] | 33 | properties.find(wpa_supplicant::kBSSPropertySignal)->second. |
| 34 | reader().get_int16(); |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 35 | network_mode_ = ParseMode( |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 36 | properties.find(wpa_supplicant::kBSSPropertyMode)->second); |
| 37 | security_mode_ = ParseSecurity(properties); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 38 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 39 | if (network_mode_.empty()) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 40 | // XXX log error? |
| 41 | } |
| 42 | |
| 43 | ssid_string_ = string(ssid_.begin(), ssid_.end()); |
| 44 | ssid_hex_ = base::HexEncode(&(*ssid_.begin()), ssid_.size()); |
| 45 | bssid_string_ = StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 46 | bssid_[0], bssid_[1], bssid_[2], |
| 47 | bssid_[3], bssid_[4], bssid_[5]); |
| 48 | bssid_hex_ = base::HexEncode(&(*bssid_.begin()), bssid_.size()); |
| 49 | } |
| 50 | |
| 51 | WiFiEndpoint::~WiFiEndpoint() {} |
| 52 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 53 | // static |
| 54 | uint32_t WiFiEndpoint::ModeStringToUint(const std::string &mode_string) { |
| 55 | if (mode_string == flimflam::kModeManaged) |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 56 | return wpa_supplicant::kNetworkModeInfrastructureInt; |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 57 | else if (mode_string == flimflam::kModeAdhoc) |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 58 | return wpa_supplicant::kNetworkModeAdHocInt; |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 59 | else |
| 60 | NOTIMPLEMENTED() << "Shill dos not support " << mode_string |
| 61 | << " mode at this time."; |
| 62 | return 0; |
| 63 | } |
| 64 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 65 | const vector<uint8_t> &WiFiEndpoint::ssid() const { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 66 | return ssid_; |
| 67 | } |
| 68 | |
| 69 | const string &WiFiEndpoint::ssid_string() const { |
| 70 | return ssid_string_; |
| 71 | } |
| 72 | |
| 73 | const string &WiFiEndpoint::ssid_hex() const { |
| 74 | return ssid_hex_; |
| 75 | } |
| 76 | |
| 77 | const string &WiFiEndpoint::bssid_string() const { |
| 78 | return bssid_string_; |
| 79 | } |
| 80 | |
| 81 | const string &WiFiEndpoint::bssid_hex() const { |
| 82 | return bssid_hex_; |
| 83 | } |
| 84 | |
| 85 | int16_t WiFiEndpoint::signal_strength() const { |
| 86 | return signal_strength_; |
| 87 | } |
| 88 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 89 | const string &WiFiEndpoint::network_mode() const { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 90 | return network_mode_; |
| 91 | } |
| 92 | |
| 93 | const string &WiFiEndpoint::security_mode() const { |
| 94 | return security_mode_; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 97 | // static |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 98 | const char *WiFiEndpoint::ParseMode(const string &mode_string) { |
| 99 | if (mode_string == wpa_supplicant::kNetworkModeInfrastructure) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 100 | return flimflam::kModeManaged; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 101 | } else if (mode_string == wpa_supplicant::kNetworkModeAdHoc) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 102 | return flimflam::kModeAdhoc; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 103 | } else if (mode_string == wpa_supplicant::kNetworkModeAccessPoint) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 104 | NOTREACHED() << "Shill does not support AP mode at this time."; |
| 105 | return NULL; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 106 | } else { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 107 | NOTREACHED() << "Unknown WiFi endpoint mode!"; |
| 108 | return NULL; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 112 | // static |
| 113 | const char *WiFiEndpoint::ParseSecurity( |
| 114 | const map<string, ::DBus::Variant> &properties) { |
| 115 | set<KeyManagement> rsn_key_management_methods; |
| 116 | if (ContainsKey(properties, wpa_supplicant::kPropertyRSN)) { |
| 117 | // TODO(quiche): check type before casting |
| 118 | const map<string, ::DBus::Variant> rsn_properties( |
| 119 | properties.find(wpa_supplicant::kPropertyRSN)->second. |
| 120 | operator map<string, ::DBus::Variant>()); |
| 121 | ParseKeyManagementMethods(rsn_properties, &rsn_key_management_methods); |
| 122 | } |
| 123 | |
| 124 | set<KeyManagement> wpa_key_management_methods; |
| 125 | if (ContainsKey(properties, wpa_supplicant::kPropertyWPA)) { |
| 126 | // TODO(qucihe): check type before casting |
| 127 | const map<string, ::DBus::Variant> rsn_properties( |
| 128 | properties.find(wpa_supplicant::kPropertyWPA)->second. |
| 129 | operator map<string, ::DBus::Variant>()); |
| 130 | ParseKeyManagementMethods(rsn_properties, &wpa_key_management_methods); |
| 131 | } |
| 132 | |
| 133 | bool wep_privacy = false; |
| 134 | if (ContainsKey(properties, wpa_supplicant::kPropertyPrivacy)) { |
| 135 | wep_privacy = properties.find(wpa_supplicant::kPropertyPrivacy)->second. |
| 136 | reader().get_bool(); |
| 137 | } |
| 138 | |
| 139 | if (ContainsKey(rsn_key_management_methods, kKeyManagement802_1x) || |
| 140 | ContainsKey(wpa_key_management_methods, kKeyManagement802_1x)) { |
| 141 | return flimflam::kSecurity8021x; |
| 142 | } else if (ContainsKey(rsn_key_management_methods, kKeyManagementPSK)) { |
| 143 | return flimflam::kSecurityRsn; |
| 144 | } else if (ContainsKey(wpa_key_management_methods, kKeyManagementPSK)) { |
| 145 | return flimflam::kSecurityWpa; |
| 146 | } else if (wep_privacy) { |
| 147 | return flimflam::kSecurityWep; |
| 148 | } else { |
| 149 | return flimflam::kSecurityNone; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // static |
| 154 | void WiFiEndpoint::ParseKeyManagementMethods( |
| 155 | const map<string, ::DBus::Variant> &security_method_properties, |
| 156 | set<KeyManagement> *key_management_methods) { |
| 157 | if (!ContainsKey(security_method_properties, |
| 158 | wpa_supplicant::kSecurityMethodPropertyKeyManagement)) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | // TODO(quiche): check type before cast |
| 163 | const vector<string> key_management_vec = |
| 164 | security_method_properties. |
| 165 | find(wpa_supplicant::kSecurityMethodPropertyKeyManagement)->second. |
| 166 | operator vector<string>(); |
| 167 | for (vector<string>::const_iterator it = key_management_vec.begin(); |
| 168 | it != key_management_vec.end(); |
| 169 | ++it) { |
| 170 | if (EndsWith(*it, wpa_supplicant::kKeyManagementMethodSuffixEAP, true)) { |
| 171 | key_management_methods->insert(kKeyManagement802_1x); |
| 172 | } else if ( |
| 173 | EndsWith(*it, wpa_supplicant::kKeyManagementMethodSuffixPSK, true)) { |
| 174 | key_management_methods->insert(kKeyManagementPSK); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 179 | } // namespace shill |