blob: d5916abf7fd239e5cfa21f86bad72b9972b92f30 [file] [log] [blame]
Wade Guthrie16196242012-11-20 15:53:52 -08001// 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_NLATTRIBUTE_H_
6#define SHILL_NLATTRIBUTE_H_
7
8#include <linux/nl80211.h>
Wade Guthrie8343f7f2012-12-04 13:52:32 -08009#include <netlink/attr.h>
Wade Guthrie16196242012-11-20 15:53:52 -080010#include <netlink/netlink.h>
11
12#include <map>
13#include <string>
14
15#include <base/memory/scoped_ptr.h>
16
17#include "shill/byte_string.h"
18
19struct nlattr;
20
21namespace shill {
22
23// Nl80211Attribute is an abstract base class that describes an attribute in a
24// netlink-80211 message. Child classes are type-specific and will define
25// Get*Value and Set*Value methods (where * is the type). A second-level of
26// child classes exist for each individual attribute type.
27//
28// An attribute has a name (which is really an enumerated value), a data type,
29// and a value. In an nlattr (the underlying format for an attribute in a
30// message), the data is stored as a blob without type information; the writer
31// and reader of the attribute must agree on the data type.
32class Nl80211Attribute {
33 public:
34 enum Type {
35 kTypeU8,
36 kTypeU16,
37 kTypeU32,
38 kTypeU64,
39 kTypeString,
40 kTypeFlag,
41 kTypeMsecs,
42 kTypeNested,
43 kTypeRaw,
44 kTypeError
45 };
46
47 Nl80211Attribute(nl80211_attrs name, const char *name_string,
48 Type type, const char *type_string);
49 virtual ~Nl80211Attribute() {}
50
51 virtual bool InitFromNlAttr(const nlattr *data);
52
53 // Static factory generates the appropriate Nl80211Attribute object from the
54 // raw nlattr data.
55 static Nl80211Attribute *NewFromNlAttr(nl80211_attrs name,
56 const nlattr *data);
57
58 // Accessors for the attribute's name and type information.
59 nl80211_attrs name() const { return name_; }
60 virtual const char *name_string() const { return name_string_; }
61 Type type() const { return type_; }
62 std::string type_string() const { return type_string_; }
63
64 // TODO(wdg): Since |data| is used, externally, to support |nla_parse_nested|,
65 // make it protected once all functionality has been brought inside the
66 // Nl80211Attribute classes.
67 //
68 // |data_| contains an 'nlattr *' but it's been stored as a ByteString.
69 // This returns a pointer to the data in the form that is intended.
70 const nlattr *data() const {
71 return reinterpret_cast<const nlattr *>(data_.GetConstData());
72 }
73
74 // TODO(wdg): GetRawData is used to support
75 // UserBoundNlMessage::GetRawAttributeData which, in turn, is used to support
76 // NL80211_ATTR_FRAME and NL80211_ATTR_KEY_SEQ. Remove this method (and that
77 // one) once those classes are fleshed-out.
78 //
79 // If successful, returns 'true' and sets *|value| to the raw attribute data
80 // (after the header) for this attribute. Otherwise, returns 'false' and
81 // leaves |value| unchanged.
Wade Guthrie8343f7f2012-12-04 13:52:32 -080082 bool GetRawData(ByteString *output) const;
Wade Guthrie16196242012-11-20 15:53:52 -080083
84 // Fill a string with characters that represents the value of the attribute.
85 // If no attribute is found or if the type isn't trivially stringizable,
86 // this method returns 'false' and |value| remains unchanged.
87 virtual bool AsString(std::string *value) const = 0;
88
89 // Writes the raw attribute data to a string. For debug.
90 std::string RawToString() const;
91
Wade Guthrie8343f7f2012-12-04 13:52:32 -080092 // Note that |nla_get_*| don't change their arguments but don't declare
93 // themselves as 'const', either. These methods wrap the const castness.
94 static char *NlaGetString(const nlattr *input) {
95 return nla_get_string(const_cast<nlattr *>(input));
96 }
97 static uint8_t NlaGetU8(const nlattr *input) {
98 return nla_get_u8(const_cast<nlattr *>(input));
99 }
100 static uint16_t NlaGetU16(const nlattr *input) {
101 return nla_get_u16(const_cast<nlattr *>(input));
102 }
103 static uint32_t NlaGetU32(const nlattr *input) {
104 return nla_get_u32(const_cast<nlattr *>(input));
105 }
106 static uint64_t NlaGetU64(const nlattr *input) {
107 return nla_get_u64(const_cast<nlattr *>(input));
108 }
109
Wade Guthrie16196242012-11-20 15:53:52 -0800110 protected:
111 // Raw data corresponding to the value in any of the child classes.
112 // TODO(wdg): When 'data()' is removed, move this to the Nl80211RawAttribute
113 // class.
114 ByteString data_;
115
116 private:
117 nl80211_attrs name_;
118 const char *name_string_;
119 Type type_;
120 const char *type_string_;
121};
122
123// Type-specific sub-classes. These provide their own type-specific data get
124// and set functions.
125
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800126class Nl80211U8Attribute : public Nl80211Attribute {
127 public:
128 static const char kMyTypeString[];
129 static const Type kType;
130 Nl80211U8Attribute(nl80211_attrs name, const char *name_string)
131 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
132 bool InitFromNlAttr(const nlattr *data);
133 bool GetU8Value(uint8_t *value) const;
134 bool SetU8Value(uint8_t new_value);
135 bool AsString(std::string *value) const;
136
137 private:
138 uint8_t value_;
139};
140
141class Nl80211U16Attribute : public Nl80211Attribute {
142 public:
143 static const char kMyTypeString[];
144 static const Type kType;
145 Nl80211U16Attribute(nl80211_attrs name, const char *name_string)
146 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
147 bool InitFromNlAttr(const nlattr *data);
148 bool GetU16Value(uint16_t *value) const;
149 bool SetU16Value(uint16_t new_value);
150 bool AsString(std::string *value) const;
151
152 private:
153 uint16_t value_;
154};
155
Wade Guthrie16196242012-11-20 15:53:52 -0800156class Nl80211U32Attribute : public Nl80211Attribute {
157 public:
158 static const char kMyTypeString[];
159 static const Type kType;
160 Nl80211U32Attribute(nl80211_attrs name, const char *name_string)
161 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
162 bool InitFromNlAttr(const nlattr *data);
163 bool GetU32Value(uint32_t *value) const;
164 bool SetU32Value(uint32_t new_value);
165 bool AsString(std::string *value) const;
166
167 private:
168 uint32_t value_;
169};
170
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800171class Nl80211U64Attribute : public Nl80211Attribute {
172 public:
173 static const char kMyTypeString[];
174 static const Type kType;
175 Nl80211U64Attribute(nl80211_attrs name, const char *name_string)
176 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
177 bool InitFromNlAttr(const nlattr *data);
178 bool GetU64Value(uint64_t *value) const;
179 bool SetU64Value(uint64_t new_value);
180 bool AsString(std::string *value) const;
181
182 private:
183 uint64_t value_;
184};
185
186class Nl80211FlagAttribute : public Nl80211Attribute {
187 public:
188 static const char kMyTypeString[];
189 static const Type kType;
190 Nl80211FlagAttribute(nl80211_attrs name, const char *name_string)
191 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
192 bool InitFromNlAttr(const nlattr *data);
193 bool GetFlagValue(bool *value) const;
194 bool SetFlagValue(bool new_value);
195 bool AsString(std::string *value) const;
196
197 private:
198 bool value_;
199};
200
201class Nl80211StringAttribute : public Nl80211Attribute {
202 public:
203 static const char kMyTypeString[];
204 static const Type kType;
205 Nl80211StringAttribute(nl80211_attrs name, const char *name_string)
206 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
207 bool InitFromNlAttr(const nlattr *data);
208 bool GetStringValue(std::string *value) const;
209 bool SetStringValue(const std::string new_value);
210 bool AsString(std::string *value) const;
211
212 private:
213 std::string value_;
214};
Wade Guthrie16196242012-11-20 15:53:52 -0800215
216class Nl80211RawAttribute : public Nl80211Attribute {
217 public:
218 static const char kMyTypeString[];
219 static const Type kType;
220 Nl80211RawAttribute(nl80211_attrs name, const char *name_string)
221 : Nl80211Attribute(name, name_string, kType, kMyTypeString) {}
222 bool InitFromNlAttr(const nlattr *data);
223 bool GetRawValue(const ByteString **value) const;
224 // Not supporting 'set' for raw data. This type is a "don't know" type to
225 // be used for user-bound massages (via InitFromNlAttr). The 'set' method
226 // is intended for building kernel-bound messages and shouldn't be used with
227 // raw data.
228 bool AsString(std::string *value) const;
229};
230
231// Attribute-specific sub-classes.
232
233class Nl80211AttributeDuration : public Nl80211U32Attribute {
234 public:
235 static const nl80211_attrs kName;
236 static const char kNameString[];
237 explicit Nl80211AttributeDuration()
238 : Nl80211U32Attribute(kName, kNameString) {}
239};
240
241class Nl80211AttributeGeneric : public Nl80211RawAttribute {
242 public:
243 explicit Nl80211AttributeGeneric(nl80211_attrs name);
244 const char *name_string() const;
245
246 private:
247 std::string name_string_;
248};
249
250} // namespace shill
251
252#endif // SHILL_NLATTRIBUTE_H_