-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathValueCallbacks.cpp
More file actions
275 lines (208 loc) · 7.75 KB
/
ValueCallbacks.cpp
File metadata and controls
275 lines (208 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "include/ValueCallbacks.h"
#include "include/BER.h"
#include <algorithm>
#define ASSERT_VALID_VALUE(value) if(!value) return nullptr;
#define SETTING_NON_SETTABLE_ERROR READ_ONLY
// If the value to be set is invalid
#define ASSERT_VALID_SETTABLE_VALUE(value) if(!value) return GEN_ERR;
#define ASSERT_VALID_SETTING_VALUE(value) if(!value) return WRONG_VALUE;
// If we ever remove the setting pre-check, use this
// #define ASSERT_CALLBACK_SETTABLE if(!(static_cast<ValueCallback*>(this)->isSettable)) return SETTING_NON_SETTABLE_ERROR;
#define ASSERT_CALLBACK_SETTABLE()
ValueCallback* ValueCallback::findCallback(std::deque<ValueCallback*> &callbacks, const OIDType* const oid, bool walk, size_t startAt, size_t *foundAt){
bool useNext = false;
for(size_t i = startAt; i < callbacks.size(); i++){
auto callback = callbacks[i];
if(useNext){
if(foundAt){
*foundAt = i;
}
return callback;
}
if(oid->equals(callback->OID)){
if(walk){
useNext = true;
continue;
}
if(foundAt){
*foundAt = i;
}
return callback;
}
if(walk && callback->OID->isSubTreeOf(oid)){
// If the oid passed in is a substring of our current callback, and it begins at the start
if(foundAt){
*foundAt = i;
}
return callback;
}
}
return nullptr;
}
std::shared_ptr<BER_CONTAINER> ValueCallback::getValueForCallback(ValueCallback* callback){
SNMP_LOGD("Getting value for callback of OID: %s, type: %d\n", callback->OID->string().c_str(), callback->type);
auto value = callback->buildTypeWithValue();
return value;
}
SNMP_ERROR_STATUS ValueCallback::setValueForCallback(ValueCallback* callback, const std::shared_ptr<BER_CONTAINER> &value){
SNMP_LOGD("Setting value for callback of OID: %s\n", callback->OID->string().c_str());
if(!callback->isSettable){
return SETTING_NON_SETTABLE_ERROR;
}
SNMP_ERROR_STATUS valid = callback->setTypeWithValue(value.get());
if(valid == NO_ERROR){
callback->setOccurred = true;
}
return valid;
}
std::shared_ptr<BER_CONTAINER> IntegerCallback::buildTypeWithValue(){
ASSERT_VALID_VALUE(this->value);
auto val = std::make_shared<IntegerType>(*this->value);
if(this->modifier != 0){
// Apple local division if callback was asked to
val->_value /= this->modifier;
}
return val;
}
SNMP_ERROR_STATUS IntegerCallback::setTypeWithValue(BER_CONTAINER* rawValue){
ASSERT_CALLBACK_SETTABLE();
ASSERT_VALID_SETTABLE_VALUE(this->value);
IntegerType* val = static_cast<IntegerType*>(rawValue);
if(this->modifier){
// Apple local division if callback was asked to
// val->_value /= this->modifier;
}
*this->value = val->_value;
return NO_ERROR;
}
std::shared_ptr<BER_CONTAINER> TimestampCallback::buildTypeWithValue(){
ASSERT_VALID_VALUE(this->value);
return std::make_shared<TimestampType>(*this->value);
}
SNMP_ERROR_STATUS TimestampCallback::setTypeWithValue(BER_CONTAINER* rawValue){
ASSERT_CALLBACK_SETTABLE();
ASSERT_VALID_SETTABLE_VALUE(this->value);
TimestampType* val = static_cast<TimestampType*>(rawValue);
*this->value = val->_value;
return NO_ERROR;
}
std::shared_ptr<BER_CONTAINER> StringCallback::buildTypeWithValue(){
ASSERT_VALID_VALUE(this->value);
return std::make_shared<OctetType>(*this->value);
}
SNMP_ERROR_STATUS StringCallback::setTypeWithValue(BER_CONTAINER* rawValue){
ASSERT_CALLBACK_SETTABLE();
ASSERT_VALID_SETTABLE_VALUE(this->value);
OctetType* val = static_cast<OctetType*>(rawValue);
if(val->_value.length() >= this->max_len) return WRONG_LENGTH;
strncpy(*this->value, val->_value.data(), this->max_len);
return NO_ERROR;
}
std::shared_ptr<BER_CONTAINER> ReadOnlyStringCallback::buildTypeWithValue(){
return std::make_shared<OctetType>(this->value);
}
std::shared_ptr<BER_CONTAINER> OpaqueCallback::buildTypeWithValue(){
ASSERT_VALID_VALUE(this->value);
return std::make_shared<OpaqueType>(this->value, this->data_len);
}
SNMP_ERROR_STATUS OpaqueCallback::setTypeWithValue(BER_CONTAINER* rawValue){
ASSERT_CALLBACK_SETTABLE();
ASSERT_VALID_SETTABLE_VALUE(this->value);
OpaqueType* val = static_cast<OpaqueType*>(rawValue);
ASSERT_VALID_SETTING_VALUE(val->_value);
if(val->_dataLength > this->data_len) return WRONG_LENGTH;
memcpy(this->value, val->_value, this->data_len);
return NO_ERROR;
}
std::shared_ptr<BER_CONTAINER> OIDCallback::buildTypeWithValue(){
auto oid = std::make_shared<OIDType>(this->value);
if(!oid->valid) return nullptr;
return oid;
}
std::shared_ptr<BER_CONTAINER> Counter32Callback::buildTypeWithValue(){
ASSERT_VALID_VALUE(this->value);
return std::make_shared<Counter32>(*this->value);
}
SNMP_ERROR_STATUS Counter32Callback::setTypeWithValue(BER_CONTAINER* rawValue){
ASSERT_CALLBACK_SETTABLE();
ASSERT_VALID_SETTABLE_VALUE(this->value);
Counter32* val = static_cast<Counter32*>(rawValue);
*this->value = val->_value;
return NO_ERROR;
}
std::shared_ptr<BER_CONTAINER> Gauge32Callback::buildTypeWithValue(){
ASSERT_VALID_VALUE(this->value);
return std::make_shared<Gauge>(*this->value);
}
SNMP_ERROR_STATUS Gauge32Callback::setTypeWithValue(BER_CONTAINER* rawValue){
ASSERT_CALLBACK_SETTABLE();
ASSERT_VALID_SETTABLE_VALUE(this->value);
Gauge* val = static_cast<Gauge*>(rawValue);
*this->value = val->_value;
return NO_ERROR;
}
std::shared_ptr<BER_CONTAINER> Counter64Callback::buildTypeWithValue(){
ASSERT_VALID_VALUE(this->value);
return std::make_shared<Counter64>(*this->value);
}
SNMP_ERROR_STATUS Counter64Callback::setTypeWithValue(BER_CONTAINER* rawValue){
ASSERT_CALLBACK_SETTABLE();
ASSERT_VALID_SETTABLE_VALUE(this->value);
Counter64* val = static_cast<Counter64*>(rawValue);
*this->value = val->_value;
return NO_ERROR;
}
std::shared_ptr<BER_CONTAINER> NetworkAddressCallback::buildTypeWithValue(){
ASSERT_VALID_VALUE(this->value);
return std::make_shared<NetworkAddress>(*this->value);
}
SNMP_ERROR_STATUS NetworkAddressCallback::setTypeWithValue(BER_CONTAINER* rawValue){
ASSERT_CALLBACK_SETTABLE();
ASSERT_VALID_SETTABLE_VALUE(this->value);
NetworkAddress* val = static_cast<NetworkAddress*>(rawValue);
*this->value = val->_value;
return NO_ERROR;
}
bool SortableOIDType::sort_oids(SortableOIDType* oid1, SortableOIDType* oid2){ // returns true if oid1 EARLIER than oid2
const auto& map1 = oid1->sortingMap;
const auto& map2 = oid2->sortingMap;
if(map1.empty()) return false;
if(map2.empty()) return true;
int i;
if(map1.size() < map2.size()){
i = map1.size();
} else {
i = map2.size();
}
for(int j = 0; j < i; j++){
if(map1[j] != map2[j]){ // if they're the same then we're on same level
return map1[j] < map2[j];
}
}
return map1.size() < map2.size();
}
bool compare_callbacks (const ValueCallback* first, const ValueCallback* second){
return SortableOIDType::sort_oids(first->OID, second->OID);
}
void sort_handlers(std::deque<ValueCallback*>& callbacks){
std::sort(callbacks.begin(), callbacks.end(), compare_callbacks);
}
bool remove_handler(std::deque<ValueCallback*>& callbacks, ValueCallback* callback){
int i = 0;
int found = -1;
for(auto cb : callbacks){
if(cb == callback){
found = i;
break;
}
i++;
}
if(found > -1){
auto it = callbacks.begin();
std::advance(it, found);
callbacks.erase(it);
return true;
} else {
return false;
}
}