-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathBER.h
More file actions
330 lines (259 loc) · 9.47 KB
/
BER.h
File metadata and controls
330 lines (259 loc) · 9.47 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#ifndef BER_h
#define BER_h
#include <math.h>
#include <utility>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <string>
#ifdef COMPILING_TESTS
#include "tests/required/IPAddress.h"
#include "tests/required/UDP.h"
#else
#include <Arduino.h>
#include "IPAddress.h"
#endif
#include <memory>
#include "include/defs.h"
typedef enum ASN_TYPE_WITH_VALUE {
// Primatives
INTEGER = 0x02,
STRING = 0x04,
NULLTYPE = 0x05,
OID = 0x06,
// Complex
STRUCTURE = 0x30,
NETWORK_ADDRESS = 0x40,
COUNTER32 = 0x41,
GAUGE32 = 0x42,
USIGNED32 = 0x42, // Same as Gauge32
TIMESTAMP = 0x43,
OPAQUE = 0x44,
COUNTER64 = 0x46,
/*
FROM: RFC3416
*/
NOSUCHOBJECT = 0x80,
NOSUCHINSTANCE = 0x81,
ENDOFMIBVIEW = 0x82,
// Structure Types
GetRequestPDU = 0xA0,
GetNextRequestPDU = 0xA1,
GetResponsePDU = 0xA2,
SetRequestPDU = 0xA3,
TrapPDU = 0xA4,
GetBulkRequestPDU = 0xA5,
InformRequestPDU = 0xA6,
Trapv2PDU = 0xA7
} ASN_TYPE;
#define ASN_PDU_TYPE_MIN_VALUE GetRequestPDU
#define ASN_PDU_TYPE_MAX_VALUE Trapv2PDU
#define MAX_DYNAMIC_ASN_TYPE COUNTER64
typedef int SNMP_BUFFER_PARSE_ERROR;
typedef int SNMP_BUFFER_ENCODE_ERROR;
#define SNMP_BUFFER_ERROR_MAX_LEN_EXCEEDED (-1 + SNMP_BUFFER_PARSE_ERROR_OFFSET)
#define SNMP_BUFFER_ERROR_TLV_TOO_SMALL (-2 + SNMP_BUFFER_PARSE_ERROR_OFFSET)
#define SNMP_BUFFER_ERROR_PROBLEM_DESERIALISING (-3 + SNMP_BUFFER_PARSE_ERROR_OFFSET)
#define SNMP_BUFFER_ERROR_UNKNOWN_TYPE (-4 + SNMP_BUFFER_PARSE_ERROR_OFFSET)
#define SNMP_BUFFER_ERROR_TYPE_MISMATCH (-5 + SNMP_BUFFER_PARSE_ERROR_OFFSET)
#define SNMP_BUFFER_ERROR_OCTET_TOO_BIG (-6 + SNMP_BUFFER_PARSE_ERROR_OFFSET)
#define SNMP_BUFFER_ERROR_INVALID_OID (-7 + SNMP_BUFFER_PARSE_ERROR_OFFSET)
#define SNMP_BUFFER_ENCODE_ERR_LEN_EXCEEDED (-1 + SNMP_BUFFER_ENCODE_ERROR_OFFSET)
#define SNMP_BUFFER_ENCODE_ERROR_INVALID_ITEM (-2 + SNMP_BUFFER_ENCODE_ERROR_OFFSET)
#define SNMP_BUFFER_ENCODE_ERROR_INVALID_OID (-7 + SNMP_BUFFER_ENCODE_ERROR_OFFSET)
#define CHECK_DECODE_ERR(i) if((i) < 0) return i
#define CHECK_ENCODE_ERR(i) if((i) < 0) return i
// primitive types inherits straight off the container, complex come off complexType
// all primitives have to serialiseInto themselves (type, length, data), to be put straight into the packet.
// for deserialising, from the parent container we check the type, then create anobject of that type and calls deSerialise, passing in the data, which pulls it out and saves, and if complex, first split up it schildren into seperate BERs, then creates and passes them creates a child with it's data using the same process.
class BER_CONTAINER {
public:
BER_CONTAINER(ASN_TYPE type) : _type(type){};
virtual ~BER_CONTAINER()= default;
ASN_TYPE _type;
int _length = 0;
protected:
// Serialise object in BER notation into buf, with a maximum size of max_len; returns number of bytes used
virtual int serialise(uint8_t* buf, size_t max_len);
virtual int serialise(uint8_t* buf, size_t max_len, size_t known_length);
// returns number of bytes used from buf, limited by max_len, return -1 if failed to parse
virtual int fromBuffer(const uint8_t *buf, size_t max_len);
friend class ComplexType;
};
class NetworkAddress: public BER_CONTAINER {
public:
NetworkAddress(): BER_CONTAINER(NETWORK_ADDRESS) {};
explicit NetworkAddress(const IPAddress& ip): NetworkAddress(){
_value = ip;
};
IPAddress _value = INADDR_NONE;
protected:
int serialise(uint8_t* buf, size_t max_len) override;
int fromBuffer(const uint8_t *buf, size_t max_len) override;
};
class IntegerType: public BER_CONTAINER {
public:
IntegerType(): BER_CONTAINER(INTEGER) {};
explicit IntegerType(int value): IntegerType(){
_value = value;
};
int _value = 0;
int _knownLen = 4;
protected:
int serialise(uint8_t* buf, size_t max_len) override;
int fromBuffer(const uint8_t *buf, size_t max_len) override;
};
class ByteType: public IntegerType {
public:
ByteType(): IntegerType() {
_knownLen = 1;
};
explicit ByteType(uint8_t value): IntegerType(value) {
_knownLen = 1;
};
};
class TimestampType: public IntegerType {
public:
TimestampType(): IntegerType(){
_type = TIMESTAMP;
};
explicit TimestampType(unsigned long value): IntegerType(value){
_type = TIMESTAMP;
};
};
class OctetType: public BER_CONTAINER {
public:
explicit OctetType(const std::string& value): BER_CONTAINER(STRING), _value(value){};
std::string _value;
protected:
int serialise(uint8_t* buf, size_t max_len) override;
int fromBuffer(const uint8_t *buf, size_t max_len) override;
OctetType(): BER_CONTAINER(STRING) {};
friend class ComplexType; // So ComplexType can use the empty constructor
};
class OpaqueType: public BER_CONTAINER {
public:
OpaqueType(uint8_t* value, int length): OpaqueType(){
this->_value = (uint8_t*)calloc(length, sizeof(uint8_t));
memcpy(this->_value, value, length);
this->_dataLength = length;
}
~OpaqueType() override{
if(this->_value) free(this->_value);
}
uint8_t* _value = nullptr;
int _dataLength = 0;
protected:
int serialise(uint8_t* buf, size_t max_len) override;
int fromBuffer(const uint8_t *buf, size_t max_len) override;
OpaqueType(): BER_CONTAINER(OPAQUE) {};
friend class ComplexType; // So ComplexType can use the empty constructor
};
class OIDType: public BER_CONTAINER {
public:
explicit OIDType(const std::string& value): BER_CONTAINER(OID), _value(value) {
// When creating a user OID, we generate our data vector immediately
this->valid = this->generateInternalData();
};
std::shared_ptr<OIDType> cloneOID() const {
// Copy all available data points
return std::shared_ptr<OIDType>(new OIDType(this->_value, this->data, this->valid));
};
// This is for display and finding purposes, only builds the string from data on request
const std::string& string();
bool valid = false;
bool equals(const std::shared_ptr<OIDType> oid) const {
return this->data == oid->data;
}
bool equals(const OIDType* oid) const {
return this->data == oid->data;
}
bool isSubTreeOf(const OIDType* const oid){
// If the oid being searched for is smaller than us and is wholly contained in us, true
// compare from the back so it's quicker
return oid->data.size() < this->data.size() &&
std::equal(oid->data.rbegin(), oid->data.rend(), this->data.rbegin() + (this->data.size() - oid->data.size()));
}
protected:
int serialise(uint8_t* buf, size_t max_len) override;
int fromBuffer(const uint8_t *buf, size_t max_len) override;
friend class ComplexType; // So ComplexType gets the empty constructor
OIDType(): BER_CONTAINER(OID) {};
// Value is only filled if we make it ourselves or a decoded one gets string() called on it
std::string _value;
std::vector<uint8_t> data;
private:
explicit OIDType(const std::string& value, const std::vector<uint8_t>& data, bool valid): BER_CONTAINER(OID), valid(valid), _value(value), data(data) {};
bool generateInternalData();
};
class SortableOIDType: public OIDType {
public:
explicit SortableOIDType(const std::string& value): OIDType(value), sortingMap(generateSortingMap()){}
static bool sort_oids(SortableOIDType* oid1, SortableOIDType* oid2);
bool operator < (SortableOIDType& other){
return SortableOIDType::sort_oids(this, &other);
}
const std::vector<unsigned long> sortingMap;
private:
const std::vector<unsigned long> generateSortingMap() const;
};
class NullType: public BER_CONTAINER {
public:
NullType(): BER_CONTAINER(NULLTYPE) {};
protected:
int serialise(uint8_t* buf, size_t max_len) override;
int fromBuffer(const uint8_t *buf, size_t max_len) override;
};
class ImplicitNullType: public NullType {
public:
explicit ImplicitNullType(ASN_TYPE type): NullType(){
//TODO: check that we're one of the implicit null types
_type = type;
};
};
class Counter64: public BER_CONTAINER {
public:
Counter64(): BER_CONTAINER(COUNTER64) {};
explicit Counter64(uint64_t value): Counter64(){
_value = value;
};
uint64_t _value = 0;
protected:
int serialise(uint8_t* buf, size_t max_len) override;
int fromBuffer(const uint8_t *buf, size_t max_len) override;
};
class Counter32: public IntegerType {
public:
Counter32(): IntegerType(){
_type = COUNTER32;
};
explicit Counter32(unsigned int value): IntegerType(value){
_type = COUNTER32;
};
};
class Gauge: public IntegerType { // Unsigned int
public:
Gauge(): IntegerType(){
_type = GAUGE32;
};
explicit Gauge(unsigned int value): IntegerType(value){
_type = GAUGE32;
};
};
class ComplexType: public BER_CONTAINER {
public:
explicit ComplexType(ASN_TYPE type): BER_CONTAINER(type) {};
std::vector<std::shared_ptr<BER_CONTAINER>> values;
int fromBuffer(const uint8_t *buf, size_t max_len) override;
int serialise(uint8_t* buf, size_t max_len) override;
std::shared_ptr<BER_CONTAINER> addValueToList(const std::shared_ptr<BER_CONTAINER>& newObj){
this->values.push_back(newObj);
return newObj;
}
private:
static std::shared_ptr<BER_CONTAINER> createObjectForType(ASN_TYPE valueType);
};
#endif