repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [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 | #include "shill/attribute_list.h" |
| 6 | |
| 7 | #include <ctype.h> |
| 8 | #include <linux/nl80211.h> |
| 9 | #include <netlink/attr.h> |
| 10 | #include <netlink/netlink.h> |
| 11 | |
| 12 | #include <iomanip> |
| 13 | #include <map> |
| 14 | #include <string> |
| 15 | |
| 16 | #include <base/stl_util.h> |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 17 | #include <base/stringprintf.h> |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 18 | |
| 19 | #include "shill/logging.h" |
| 20 | #include "shill/nl80211_attribute.h" |
| 21 | #include "shill/scope_logger.h" |
| 22 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 23 | using base::StringAppendF; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 24 | using base::WeakPtr; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 25 | using std::map; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 26 | using std::string; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 27 | |
| 28 | namespace shill { |
| 29 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 30 | bool AttributeList::CreateAttribute( |
| 31 | int id, AttributeList::NewFromIdMethod factory) { |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 32 | if (ContainsKey(attributes_, id)) { |
| 33 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 34 | return false; |
| 35 | } |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 36 | attributes_[id] = AttributePointer(factory.Run(id)); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 37 | return true; |
| 38 | } |
| 39 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 40 | bool AttributeList::CreateAndInitAttribute( |
| 41 | int id, const nlattr *data, AttributeList::NewFromIdMethod factory) { |
| 42 | if (!CreateAttribute(id, factory)) { |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 43 | return false; |
| 44 | } |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 45 | return attributes_[id]->InitFromNlAttr(data); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 48 | void AttributeList::Print(int log_level, int indent) const { |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 49 | map<int, AttributePointer>::const_iterator i; |
| 50 | |
| 51 | for (i = attributes_.begin(); i != attributes_.end(); ++i) { |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 52 | i->second->Print(log_level, indent); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 53 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 54 | } |
| 55 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 56 | ByteString AttributeList::Encode() const { |
| 57 | ByteString result; |
| 58 | map<int, AttributePointer>::const_iterator i; |
| 59 | |
| 60 | for (i = attributes_.begin(); i != attributes_.end(); ++i) { |
| 61 | result.Append(i->second->Encode()); |
| 62 | } |
| 63 | return result; |
| 64 | } |
| 65 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 66 | // U8 Attribute. |
| 67 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 68 | bool AttributeList::GetU8AttributeValue(int id, uint8_t *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 69 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 70 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 71 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 72 | return attribute->GetU8Value(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 73 | } |
| 74 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 75 | bool AttributeList::CreateU8Attribute(int id, const char *id_string) { |
| 76 | if (ContainsKey(attributes_, id)) { |
| 77 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 78 | return false; |
| 79 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 80 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 81 | new NetlinkU8Attribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 85 | bool AttributeList::SetU8AttributeValue(int id, uint8_t value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 86 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 87 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 88 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 89 | return attribute->SetU8Value(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 93 | // U16 Attribute. |
| 94 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 95 | bool AttributeList::GetU16AttributeValue(int id, uint16_t *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 96 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 97 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 98 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 99 | return attribute->GetU16Value(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 100 | } |
| 101 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 102 | bool AttributeList::CreateU16Attribute(int id, const char *id_string) { |
| 103 | if (ContainsKey(attributes_, id)) { |
| 104 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 105 | return false; |
| 106 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 107 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 108 | new NetlinkU16Attribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 112 | bool AttributeList::SetU16AttributeValue(int id, uint16_t value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 113 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 114 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 115 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 116 | return attribute->SetU16Value(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 117 | } |
| 118 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 119 | // U32 Attribute. |
| 120 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 121 | bool AttributeList::GetU32AttributeValue(int id, uint32_t *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 122 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 123 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 124 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 125 | return attribute->GetU32Value(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 126 | } |
| 127 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 128 | bool AttributeList::CreateU32Attribute(int id, const char *id_string) { |
| 129 | if (ContainsKey(attributes_, id)) { |
| 130 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 131 | return false; |
| 132 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 133 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 134 | new NetlinkU32Attribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 138 | bool AttributeList::SetU32AttributeValue(int id, uint32_t value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 139 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 140 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 141 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 142 | return attribute->SetU32Value(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 143 | } |
| 144 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 145 | // U64 Attribute. |
| 146 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 147 | bool AttributeList::GetU64AttributeValue(int id, uint64_t *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 148 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 149 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 150 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 151 | return attribute->GetU64Value(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 152 | } |
| 153 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 154 | bool AttributeList::CreateU64Attribute(int id, const char *id_string) { |
| 155 | if (ContainsKey(attributes_, id)) { |
| 156 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 157 | return false; |
| 158 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 159 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 160 | new NetlinkU64Attribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 164 | bool AttributeList::SetU64AttributeValue(int id, uint64_t value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 165 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 166 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 167 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 168 | return attribute->SetU64Value(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 169 | } |
| 170 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 171 | // Flag Attribute. |
| 172 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 173 | bool AttributeList::GetFlagAttributeValue(int id, bool *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 174 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 175 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 176 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 177 | return attribute->GetFlagValue(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 178 | } |
| 179 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 180 | bool AttributeList::CreateFlagAttribute(int id, const char *id_string) { |
| 181 | if (ContainsKey(attributes_, id)) { |
| 182 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 183 | return false; |
| 184 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 185 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 186 | new NetlinkFlagAttribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 187 | return true; |
| 188 | } |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 189 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 190 | bool AttributeList::SetFlagAttributeValue(int id, bool value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 191 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 192 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 193 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 194 | return attribute->SetFlagValue(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | bool AttributeList::IsFlagAttributeTrue(int id) const { |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 198 | bool flag; |
| 199 | if (!GetFlagAttributeValue(id, &flag)) { |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | return flag; |
| 203 | } |
| 204 | |
| 205 | // String Attribute. |
| 206 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 207 | bool AttributeList::GetStringAttributeValue(int id, string *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 208 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 209 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 210 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 211 | return attribute->GetStringValue(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 212 | } |
| 213 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 214 | bool AttributeList::CreateStringAttribute(int id, const char *id_string) { |
| 215 | if (ContainsKey(attributes_, id)) { |
| 216 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 217 | return false; |
| 218 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 219 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 220 | new NetlinkStringAttribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 224 | bool AttributeList::SetStringAttributeValue(int id, string value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 225 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 226 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 227 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 228 | return attribute->SetStringValue(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Nested Attribute. |
| 232 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 233 | bool AttributeList::GetNestedAttributeList(int id, |
| 234 | AttributeListRefPtr *value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 235 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 236 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 237 | return false; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 238 | return attribute->GetNestedAttributeList(value); |
| 239 | } |
| 240 | |
| 241 | bool AttributeList::ConstGetNestedAttributeList( |
| 242 | int id, AttributeListConstRefPtr *value) const { |
| 243 | NetlinkAttribute *attribute = GetAttribute(id); |
| 244 | if (!attribute) |
| 245 | return false; |
| 246 | return attribute->ConstGetNestedAttributeList(value); |
| 247 | } |
| 248 | |
| 249 | bool AttributeList::SetNestedAttributeHasAValue(int id) { |
| 250 | NetlinkAttribute *attribute = GetAttribute(id); |
| 251 | if (!attribute) |
| 252 | return false; |
| 253 | return attribute->SetNestedHasAValue(); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | bool AttributeList::CreateNestedAttribute(int id, const char *id_string) { |
| 257 | if (ContainsKey(attributes_, id)) { |
| 258 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 259 | return false; |
| 260 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 261 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 262 | new NetlinkNestedAttribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 266 | // Raw Attribute. |
| 267 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 268 | bool AttributeList::GetRawAttributeValue(int id, |
| 269 | ByteString *output) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 270 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 271 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 272 | return false; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 273 | |
| 274 | ByteString raw_value; |
| 275 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 276 | if (!attribute->GetRawValue(&raw_value)) |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 277 | return false; |
| 278 | |
| 279 | if (output) { |
| 280 | const nlattr *const_data = |
| 281 | reinterpret_cast<const nlattr *>(raw_value.GetConstData()); |
| 282 | // nla_data and nla_len don't change their parameters but don't declare |
| 283 | // them to be const. Hence the cast. |
| 284 | nlattr *data_nlattr = const_cast<nlattr *>(const_data); |
| 285 | *output = ByteString( |
| 286 | reinterpret_cast<unsigned char *>(nla_data(data_nlattr)), |
| 287 | nla_len(data_nlattr)); |
| 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 292 | const NetlinkRawAttribute *AttributeList::GetRawAttribute( |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 293 | int id) const { |
| 294 | if (!HasRawAttribute(id)) { |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 295 | LOG(ERROR) << "No attribute " << id << " of type kTypeRaw exists."; |
| 296 | return NULL; |
| 297 | } |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 298 | const NetlinkRawAttribute *attr = |
| 299 | reinterpret_cast<const NetlinkRawAttribute *>(GetAttribute(id)); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 300 | return attr; |
| 301 | } |
| 302 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 303 | NetlinkAttribute *AttributeList::GetAttribute(int id) const { |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 304 | map<int, AttributePointer>::const_iterator i; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 305 | i = attributes_.find(id); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 306 | if (i == attributes_.end()) { |
| 307 | return NULL; |
| 308 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 309 | return i->second.get(); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 310 | } |
| 311 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 312 | bool AttributeList::HasRawAttribute(int id) const { |
| 313 | map<int, AttributePointer>::const_iterator i; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 314 | i = attributes_.find(id); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 315 | if (i == attributes_.end()) { |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 316 | LOG(ERROR) << "FALSE - Didn't find id " << id; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 317 | return false; |
| 318 | } |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 319 | return (i->second->datatype() == NetlinkAttribute::kTypeRaw) ? true : false; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
| 323 | } // namespace shill |