Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #ifndef SHILL_USER_BOUND_NLMESSAGE_H_ |
| 6 | #define SHILL_USER_BOUND_NLMESSAGE_H_ |
| 7 | |
| 8 | #include <iomanip> |
| 9 | #include <map> |
| 10 | #include <vector> |
| 11 | #include <string> |
| 12 | |
| 13 | #include <base/basictypes.h> |
| 14 | #include <base/bind.h> |
| 15 | #include <base/lazy_instance.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | |
| 18 | #include <linux/nl80211.h> |
| 19 | |
| 20 | struct nlattr; |
| 21 | struct nlmsghdr; |
| 22 | |
| 23 | namespace shill { |
| 24 | |
| 25 | // Class for messages received from libnl. |
| 26 | class UserBoundNlMessage { |
| 27 | public: |
| 28 | enum Type { |
| 29 | kTypeUnspecified, |
| 30 | kTypeU8, |
| 31 | kTypeU16, |
| 32 | kTypeU32, |
| 33 | kTypeU64, |
| 34 | kTypeString, |
| 35 | kTypeFlag, |
| 36 | kTypeMsecs, |
| 37 | kTypeNested, |
| 38 | kTypeOther, // Specified in the message but not listed, here. |
| 39 | kTypeError |
| 40 | }; |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 41 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 42 | // TODO(wdg): break 'Attribute' into its own class to handle |
| 43 | // nested attributes better. |
| 44 | |
| 45 | // A const iterator to the attribute names in the attributes_ map of a |
| 46 | // UserBoundNlMessage. The purpose, here, is to hide the way that the |
| 47 | // attribute is stored. |
| 48 | class AttributeNameIterator { |
| 49 | public: |
| 50 | explicit AttributeNameIterator(const std::map<nl80211_attrs, |
| 51 | nlattr *> &map_param) |
| 52 | : map_(map_param) { |
| 53 | iter_ = map_.begin(); |
| 54 | } |
| 55 | |
| 56 | // Causes the iterator to point to the next attribute in the list. |
| 57 | void Advance() { ++iter_; } |
| 58 | |
| 59 | // Returns 'true' if the iterator points beyond the last attribute in the |
| 60 | // list; returns 'false' otherwise. |
| 61 | bool AtEnd() const { return iter_ == map_.end(); } |
| 62 | |
| 63 | // Returns the attribute name (which is actually an 'enum' value). |
| 64 | nl80211_attrs GetName() const { return iter_->first; } |
| 65 | |
| 66 | private: |
| 67 | const std::map<nl80211_attrs, nlattr *> &map_; |
| 68 | std::map<nl80211_attrs, nlattr *>::const_iterator iter_; |
| 69 | |
| 70 | DISALLOW_COPY_AND_ASSIGN(AttributeNameIterator); |
| 71 | }; |
| 72 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 73 | static const char kBogusMacAddress[]; |
| 74 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 75 | UserBoundNlMessage(uint8 message_type, const char *message_type_string) |
| 76 | : message_(NULL), |
| 77 | message_type_(message_type), |
| 78 | message_type_string_(message_type_string) { } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 79 | virtual ~UserBoundNlMessage(); |
| 80 | |
| 81 | // Non-trivial initialization. |
| 82 | virtual bool Init(nlattr *tb[NL80211_ATTR_MAX + 1], nlmsghdr *msg); |
| 83 | |
| 84 | // Provide a suite of methods to allow (const) iteration over the names of the |
| 85 | // attributes inside a message object. |
| 86 | |
| 87 | AttributeNameIterator *GetAttributeNameIterator() const; |
| 88 | bool HasAttributes() const { return !attributes_.empty(); } |
| 89 | uint32_t GetAttributeCount() const { return attributes_.size(); } |
| 90 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 91 | // Other ways to see the internals of the object. |
| 92 | |
| 93 | // Return true if the attribute is in our map, regardless of the value of |
| 94 | // the attribute, itself. |
| 95 | bool AttributeExists(nl80211_attrs name) const; |
| 96 | |
| 97 | // Message ID is equivalent to the message's sequence number. |
| 98 | uint32_t GetId() const; |
| 99 | |
| 100 | // Returns the data type of a given attribute. |
| 101 | Type GetAttributeType(nl80211_attrs name) const; |
| 102 | |
| 103 | // Returns a string describing the data type of a given attribute. |
| 104 | std::string GetAttributeTypeString(nl80211_attrs name) const; |
| 105 | |
| 106 | // If successful, returns 'true' and sets *|value| to the raw attribute data |
| 107 | // (after the header) for this attribute and, if |length| is not |
| 108 | // null, *|length| to the number of bytes of data available. If no |
| 109 | // attribute by this name exists in this message, sets *|value| to NULL and |
| 110 | // returns 'false'. If otherwise unsuccessful, returns 'false' and leaves |
| 111 | // |value| and |length| unchanged. |
| 112 | bool GetRawAttributeData(nl80211_attrs name, void **value, int *length) const; |
| 113 | |
| 114 | // Each of these methods set |value| with the value of the specified |
| 115 | // attribute (if the attribute is not found, |value| remains unchanged). |
| 116 | |
| 117 | bool GetStringAttribute(nl80211_attrs name, std::string *value) const; |
| 118 | bool GetU8Attribute(nl80211_attrs name, uint8_t *value) const; |
| 119 | bool GetU16Attribute(nl80211_attrs name, uint16_t *value) const; |
| 120 | bool GetU32Attribute(nl80211_attrs name, uint32_t *value) const; |
| 121 | bool GetU64Attribute(nl80211_attrs name, uint64_t *value) const; |
| 122 | |
| 123 | // Fill a string with characters that represents the value of the attribute. |
| 124 | // If no attribute is found or if the type isn't trivially stringizable, |
| 125 | // this method returns 'false' and |value| remains unchanged. |
| 126 | bool GetAttributeString(nl80211_attrs name, std::string *value) const; |
| 127 | |
| 128 | // Helper function to provide a string for a MAC address. If no attribute |
| 129 | // is found, this method returns 'false'. On any error with a non-NULL |
| 130 | // |value|, this method sets |value| to a bogus MAC address. |
| 131 | bool GetMacAttributeString(nl80211_attrs name, std::string *value) const; |
| 132 | |
| 133 | // Helper function to provide a vector of scan frequencies for attributes |
| 134 | // that contain them (such as NL80211_ATTR_SCAN_FREQUENCIES). |
| 135 | bool GetScanFrequenciesAttribute(enum nl80211_attrs name, |
| 136 | std::vector<uint32_t> *value) const; |
| 137 | |
| 138 | // Helper function to provide a vector of SSIDs for attributes that contain |
| 139 | // them (such as NL80211_ATTR_SCAN_SSIDS). |
| 140 | bool GetScanSsidsAttribute(enum nl80211_attrs name, |
| 141 | std::vector<std::string> *value) const; |
| 142 | |
| 143 | // Writes the raw attribute data to a string. For debug. |
| 144 | virtual std::string RawToString(nl80211_attrs name) const; |
| 145 | |
| 146 | // Returns a string describing a given attribute name. |
| 147 | static std::string StringFromAttributeName(nl80211_attrs name); |
| 148 | |
| 149 | // Stringizes the MAC address found in 'arg'. If there are problems (such |
| 150 | // as a NULL |arg|), |value| is set to a bogus MAC address. |
| 151 | static std::string StringFromMacAddress(const uint8_t *arg); |
| 152 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 153 | // Returns a string representing the passed-in |status| or |reason|, the |
| 154 | // value of which has been acquired from libnl (for example, from the |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 155 | // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute). |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 156 | static std::string StringFromReason(uint16_t reason); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 157 | static std::string StringFromStatus(uint16_t status); |
| 158 | |
| 159 | // Returns a string that describes this message. |
| 160 | virtual std::string ToString() const { return GetHeaderString(); } |
| 161 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 162 | uint8 message_type() const { return message_type_; } |
| 163 | const char *message_type_string() const { return message_type_string_; } |
| 164 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 165 | protected: |
| 166 | // Duplicate attribute data, store in map indexed on 'name'. |
| 167 | bool AddAttribute(nl80211_attrs name, nlattr *data); |
| 168 | |
| 169 | // Returns the raw nlattr for a given attribute (NULL if attribute doesn't |
| 170 | // exist). |
| 171 | const nlattr *GetAttribute(nl80211_attrs name) const; |
| 172 | |
| 173 | // Returns a string that should precede all user-bound message strings. |
| 174 | virtual std::string GetHeaderString() const; |
| 175 | |
| 176 | // Returns a string that describes the contents of the frame pointed to by |
| 177 | // 'attr'. |
| 178 | std::string StringFromFrame(nl80211_attrs attr_name) const; |
| 179 | |
| 180 | // Converts key_type to a string. |
| 181 | static std::string StringFromKeyType(nl80211_key_type key_type); |
| 182 | |
| 183 | // Returns a string representation of the REG initiator described by the |
| 184 | // method's parameter. |
| 185 | static std::string StringFromRegInitiator(__u8 initiator); |
| 186 | |
| 187 | // Returns a string based on the SSID found in 'data'. Non-printable |
| 188 | // characters are string-ized. |
| 189 | static std::string StringFromSsid(const uint8_t len, const uint8_t *data); |
| 190 | |
| 191 | private: |
| 192 | friend class AttributeNameIterator; |
| 193 | friend class Config80211Test; |
| 194 | FRIEND_TEST(Config80211Test, NL80211_CMD_NOTIFY_CQM); |
| 195 | |
| 196 | static const uint32_t kIllegalMessage; |
| 197 | static const int kEthernetAddressBytes; |
| 198 | |
| 199 | nlmsghdr *message_; |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 200 | const uint8 message_type_; |
| 201 | const char *message_type_string_; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 202 | static std::map<uint16_t, std::string> *reason_code_string_; |
| 203 | static std::map<uint16_t, std::string> *status_code_string_; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 204 | std::map<nl80211_attrs, nlattr *> attributes_; |
| 205 | |
| 206 | DISALLOW_COPY_AND_ASSIGN(UserBoundNlMessage); |
| 207 | }; |
| 208 | |
| 209 | class Nl80211Frame { |
| 210 | public: |
| 211 | enum Type { |
| 212 | kAssocResponseFrameType = 0x10, |
| 213 | kReassocResponseFrameType = 0x30, |
| 214 | kAssocRequestFrameType = 0x00, |
| 215 | kReassocRequestFrameType = 0x20, |
| 216 | kAuthFrameType = 0xb0, |
| 217 | kDisassocFrameType = 0xa0, |
| 218 | kDeauthFrameType = 0xc0, |
| 219 | kIllegalFrameType = 0xff |
| 220 | }; |
| 221 | |
| 222 | Nl80211Frame(const uint8_t *frame, int frame_byte_count); |
| 223 | ~Nl80211Frame(); |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 224 | bool ToString(std::string *output) const; |
| 225 | bool IsEqual(const Nl80211Frame &other) const; |
| 226 | uint16_t reason() const { return reason_; } |
| 227 | uint16_t status() const { return status_; } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 228 | |
| 229 | private: |
| 230 | static const uint8_t kMinimumFrameByteCount; |
| 231 | static const uint8_t kFrameTypeMask; |
| 232 | |
| 233 | std::string mac_from_; |
| 234 | std::string mac_to_; |
| 235 | uint8_t frame_type_; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 236 | uint16_t reason_; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 237 | uint16_t status_; |
| 238 | uint8_t *frame_; |
| 239 | int byte_count_; |
| 240 | |
| 241 | DISALLOW_COPY_AND_ASSIGN(Nl80211Frame); |
| 242 | }; |
| 243 | |
| 244 | // |
| 245 | // Specific UserBoundNlMessage types. |
| 246 | // |
| 247 | |
| 248 | class AssociateMessage : public UserBoundNlMessage { |
| 249 | public: |
| 250 | static const uint8_t kCommand; |
| 251 | static const char kCommandString[]; |
| 252 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 253 | AssociateMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 254 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 255 | virtual std::string ToString() const; |
| 256 | |
| 257 | private: |
| 258 | DISALLOW_COPY_AND_ASSIGN(AssociateMessage); |
| 259 | }; |
| 260 | |
| 261 | |
| 262 | class AuthenticateMessage : public UserBoundNlMessage { |
| 263 | public: |
| 264 | static const uint8_t kCommand; |
| 265 | static const char kCommandString[]; |
| 266 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 267 | AuthenticateMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 268 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 269 | virtual std::string ToString() const; |
| 270 | |
| 271 | private: |
| 272 | DISALLOW_COPY_AND_ASSIGN(AuthenticateMessage); |
| 273 | }; |
| 274 | |
| 275 | |
| 276 | class CancelRemainOnChannelMessage : public UserBoundNlMessage { |
| 277 | public: |
| 278 | static const uint8_t kCommand; |
| 279 | static const char kCommandString[]; |
| 280 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 281 | CancelRemainOnChannelMessage() |
| 282 | : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 283 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 284 | virtual std::string ToString() const; |
| 285 | |
| 286 | private: |
| 287 | DISALLOW_COPY_AND_ASSIGN(CancelRemainOnChannelMessage); |
| 288 | }; |
| 289 | |
| 290 | |
| 291 | class ConnectMessage : public UserBoundNlMessage { |
| 292 | public: |
| 293 | static const uint8_t kCommand; |
| 294 | static const char kCommandString[]; |
| 295 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 296 | ConnectMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 297 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 298 | virtual std::string ToString() const; |
| 299 | |
| 300 | private: |
| 301 | DISALLOW_COPY_AND_ASSIGN(ConnectMessage); |
| 302 | }; |
| 303 | |
| 304 | |
| 305 | class DeauthenticateMessage : public UserBoundNlMessage { |
| 306 | public: |
| 307 | static const uint8_t kCommand; |
| 308 | static const char kCommandString[]; |
| 309 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 310 | DeauthenticateMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 311 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 312 | virtual std::string ToString() const; |
| 313 | |
| 314 | private: |
| 315 | DISALLOW_COPY_AND_ASSIGN(DeauthenticateMessage); |
| 316 | }; |
| 317 | |
| 318 | |
| 319 | class DeleteStationMessage : public UserBoundNlMessage { |
| 320 | public: |
| 321 | static const uint8_t kCommand; |
| 322 | static const char kCommandString[]; |
| 323 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 324 | DeleteStationMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 325 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 326 | virtual std::string ToString() const; |
| 327 | |
| 328 | private: |
| 329 | DISALLOW_COPY_AND_ASSIGN(DeleteStationMessage); |
| 330 | }; |
| 331 | |
| 332 | |
| 333 | class DisassociateMessage : public UserBoundNlMessage { |
| 334 | public: |
| 335 | static const uint8_t kCommand; |
| 336 | static const char kCommandString[]; |
| 337 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 338 | DisassociateMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 339 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 340 | virtual std::string ToString() const; |
| 341 | |
| 342 | private: |
| 343 | DISALLOW_COPY_AND_ASSIGN(DisassociateMessage); |
| 344 | }; |
| 345 | |
| 346 | |
| 347 | class DisconnectMessage : public UserBoundNlMessage { |
| 348 | public: |
| 349 | static const uint8_t kCommand; |
| 350 | static const char kCommandString[]; |
| 351 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 352 | DisconnectMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 353 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 354 | virtual std::string ToString() const; |
| 355 | |
| 356 | private: |
| 357 | DISALLOW_COPY_AND_ASSIGN(DisconnectMessage); |
| 358 | }; |
| 359 | |
| 360 | |
| 361 | class FrameTxStatusMessage : public UserBoundNlMessage { |
| 362 | public: |
| 363 | static const uint8_t kCommand; |
| 364 | static const char kCommandString[]; |
| 365 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 366 | FrameTxStatusMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 367 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 368 | virtual std::string ToString() const; |
| 369 | |
| 370 | private: |
| 371 | DISALLOW_COPY_AND_ASSIGN(FrameTxStatusMessage); |
| 372 | }; |
| 373 | |
| 374 | |
| 375 | class JoinIbssMessage : public UserBoundNlMessage { |
| 376 | public: |
| 377 | static const uint8_t kCommand; |
| 378 | static const char kCommandString[]; |
| 379 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 380 | JoinIbssMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 381 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 382 | virtual std::string ToString() const; |
| 383 | |
| 384 | private: |
| 385 | DISALLOW_COPY_AND_ASSIGN(JoinIbssMessage); |
| 386 | }; |
| 387 | |
| 388 | |
| 389 | class MichaelMicFailureMessage : public UserBoundNlMessage { |
| 390 | public: |
| 391 | static const uint8_t kCommand; |
| 392 | static const char kCommandString[]; |
| 393 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 394 | MichaelMicFailureMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 395 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 396 | virtual std::string ToString() const; |
| 397 | |
| 398 | private: |
| 399 | DISALLOW_COPY_AND_ASSIGN(MichaelMicFailureMessage); |
| 400 | }; |
| 401 | |
| 402 | |
| 403 | class NewScanResultsMessage : public UserBoundNlMessage { |
| 404 | public: |
| 405 | static const uint8_t kCommand; |
| 406 | static const char kCommandString[]; |
| 407 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 408 | NewScanResultsMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 409 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 410 | virtual std::string ToString() const; |
| 411 | |
| 412 | private: |
| 413 | DISALLOW_COPY_AND_ASSIGN(NewScanResultsMessage); |
| 414 | }; |
| 415 | |
| 416 | |
| 417 | class NewStationMessage : public UserBoundNlMessage { |
| 418 | public: |
| 419 | static const uint8_t kCommand; |
| 420 | static const char kCommandString[]; |
| 421 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 422 | NewStationMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 423 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 424 | virtual std::string ToString() const; |
| 425 | |
| 426 | private: |
| 427 | DISALLOW_COPY_AND_ASSIGN(NewStationMessage); |
| 428 | }; |
| 429 | |
| 430 | |
| 431 | class NewWifiMessage : public UserBoundNlMessage { |
| 432 | public: |
| 433 | static const uint8_t kCommand; |
| 434 | static const char kCommandString[]; |
| 435 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 436 | NewWifiMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 437 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 438 | virtual std::string ToString() const; |
| 439 | |
| 440 | private: |
| 441 | DISALLOW_COPY_AND_ASSIGN(NewWifiMessage); |
| 442 | }; |
| 443 | |
| 444 | |
| 445 | class NotifyCqmMessage : public UserBoundNlMessage { |
| 446 | public: |
| 447 | static const uint8_t kCommand; |
| 448 | static const char kCommandString[]; |
| 449 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 450 | NotifyCqmMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 451 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 452 | virtual std::string ToString() const; |
| 453 | |
| 454 | private: |
| 455 | DISALLOW_COPY_AND_ASSIGN(NotifyCqmMessage); |
| 456 | }; |
| 457 | |
| 458 | |
| 459 | class PmksaCandidateMessage : public UserBoundNlMessage { |
| 460 | public: |
| 461 | static const uint8_t kCommand; |
| 462 | static const char kCommandString[]; |
| 463 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 464 | PmksaCandidateMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 465 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 466 | virtual std::string ToString() const; |
| 467 | |
| 468 | private: |
| 469 | DISALLOW_COPY_AND_ASSIGN(PmksaCandidateMessage); |
| 470 | }; |
| 471 | |
| 472 | |
| 473 | class RegBeaconHintMessage : public UserBoundNlMessage { |
| 474 | public: |
| 475 | static const uint8_t kCommand; |
| 476 | static const char kCommandString[]; |
| 477 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 478 | RegBeaconHintMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 479 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 480 | virtual std::string ToString() const; |
| 481 | |
| 482 | private: |
| 483 | struct ieee80211_beacon_channel { |
| 484 | __u16 center_freq; |
| 485 | bool passive_scan; |
| 486 | bool no_ibss; |
| 487 | }; |
| 488 | |
| 489 | // Returns the channel ID calculated from the 802.11 frequency. |
| 490 | static int ChannelFromIeee80211Frequency(int freq); |
| 491 | |
| 492 | // Sets values in |chan| based on attributes in |tb|, the array of pointers |
| 493 | // to netlink attributes, indexed by attribute type. |
| 494 | int ParseBeaconHintChan(const nlattr *tb, |
| 495 | ieee80211_beacon_channel *chan) const; |
| 496 | |
| 497 | DISALLOW_COPY_AND_ASSIGN(RegBeaconHintMessage); |
| 498 | }; |
| 499 | |
| 500 | |
| 501 | class RegChangeMessage : public UserBoundNlMessage { |
| 502 | public: |
| 503 | static const uint8_t kCommand; |
| 504 | static const char kCommandString[]; |
| 505 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 506 | RegChangeMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 507 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 508 | virtual std::string ToString() const; |
| 509 | |
| 510 | private: |
| 511 | DISALLOW_COPY_AND_ASSIGN(RegChangeMessage); |
| 512 | }; |
| 513 | |
| 514 | |
| 515 | class RemainOnChannelMessage : public UserBoundNlMessage { |
| 516 | public: |
| 517 | static const uint8_t kCommand; |
| 518 | static const char kCommandString[]; |
| 519 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 520 | RemainOnChannelMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 521 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 522 | virtual std::string ToString() const; |
| 523 | |
| 524 | private: |
| 525 | DISALLOW_COPY_AND_ASSIGN(RemainOnChannelMessage); |
| 526 | }; |
| 527 | |
| 528 | |
| 529 | class RoamMessage : public UserBoundNlMessage { |
| 530 | public: |
| 531 | static const uint8_t kCommand; |
| 532 | static const char kCommandString[]; |
| 533 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 534 | RoamMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 535 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 536 | virtual std::string ToString() const; |
| 537 | |
| 538 | private: |
| 539 | DISALLOW_COPY_AND_ASSIGN(RoamMessage); |
| 540 | }; |
| 541 | |
| 542 | |
| 543 | class ScanAbortedMessage : public UserBoundNlMessage { |
| 544 | public: |
| 545 | static const uint8_t kCommand; |
| 546 | static const char kCommandString[]; |
| 547 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 548 | ScanAbortedMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 549 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 550 | virtual std::string ToString() const; |
| 551 | |
| 552 | private: |
| 553 | DISALLOW_COPY_AND_ASSIGN(ScanAbortedMessage); |
| 554 | }; |
| 555 | |
| 556 | |
| 557 | class TriggerScanMessage : public UserBoundNlMessage { |
| 558 | public: |
| 559 | static const uint8_t kCommand; |
| 560 | static const char kCommandString[]; |
| 561 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 562 | TriggerScanMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 563 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 564 | virtual std::string ToString() const; |
| 565 | |
| 566 | private: |
| 567 | DISALLOW_COPY_AND_ASSIGN(TriggerScanMessage); |
| 568 | }; |
| 569 | |
| 570 | |
| 571 | class UnknownMessage : public UserBoundNlMessage { |
| 572 | public: |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 573 | explicit UnknownMessage(uint8_t command) |
| 574 | : UserBoundNlMessage(command, kCommandString), |
| 575 | command_(command) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 576 | |
| 577 | static const uint8_t kCommand; |
| 578 | static const char kCommandString[]; |
| 579 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 580 | virtual std::string ToString() const; |
| 581 | |
| 582 | private: |
| 583 | uint8_t command_; |
| 584 | |
| 585 | DISALLOW_COPY_AND_ASSIGN(UnknownMessage); |
| 586 | }; |
| 587 | |
| 588 | |
| 589 | class UnprotDeauthenticateMessage : public UserBoundNlMessage { |
| 590 | public: |
| 591 | static const uint8_t kCommand; |
| 592 | static const char kCommandString[]; |
| 593 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 594 | UnprotDeauthenticateMessage() |
| 595 | : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 596 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 597 | virtual std::string ToString() const; |
| 598 | |
| 599 | private: |
| 600 | DISALLOW_COPY_AND_ASSIGN(UnprotDeauthenticateMessage); |
| 601 | }; |
| 602 | |
| 603 | |
| 604 | class UnprotDisassociateMessage : public UserBoundNlMessage { |
| 605 | public: |
| 606 | static const uint8_t kCommand; |
| 607 | static const char kCommandString[]; |
| 608 | |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 609 | UnprotDisassociateMessage() : UserBoundNlMessage(kCommand, kCommandString) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 610 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 611 | virtual std::string ToString() const; |
| 612 | |
| 613 | private: |
| 614 | DISALLOW_COPY_AND_ASSIGN(UnprotDisassociateMessage); |
| 615 | }; |
| 616 | |
| 617 | |
| 618 | // |
| 619 | // Factory class. |
| 620 | // |
| 621 | |
| 622 | class UserBoundNlMessageFactory { |
| 623 | public: |
| 624 | // Ownership of the message is passed to the caller and, as such, he should |
| 625 | // delete it. |
| 626 | static UserBoundNlMessage *CreateMessage(nlmsghdr *msg); |
| 627 | |
| 628 | private: |
| 629 | DISALLOW_COPY_AND_ASSIGN(UserBoundNlMessageFactory); |
| 630 | }; |
| 631 | |
| 632 | |
| 633 | // UserBoundNlMessageDataCollector - this class is used to collect data to be |
| 634 | // used for unit tests. It is only invoked in this case. |
| 635 | |
| 636 | class UserBoundNlMessageDataCollector { |
| 637 | public: |
| 638 | // This is a singleton -- use Config80211::GetInstance()->Foo() |
| 639 | static UserBoundNlMessageDataCollector *GetInstance(); |
| 640 | |
| 641 | void CollectDebugData(const UserBoundNlMessage &message, nlmsghdr *msg); |
| 642 | |
| 643 | protected: |
| 644 | friend struct |
| 645 | base::DefaultLazyInstanceTraits<UserBoundNlMessageDataCollector>; |
| 646 | |
| 647 | explicit UserBoundNlMessageDataCollector(); |
| 648 | |
| 649 | private: |
| 650 | // In order to limit the output from this class, I keep track of types I |
| 651 | // haven't yet printed. |
| 652 | std::map<uint8_t, bool> need_to_print; |
| 653 | }; |
| 654 | |
| 655 | } // namespace shill |
| 656 | |
| 657 | #endif // SHILL_USER_BOUND_NLMESSAGE_H_ |