forked from 0neblock/Arduino_SNMP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBER.h
More file actions
461 lines (424 loc) · 13.5 KB
/
BER.h
File metadata and controls
461 lines (424 loc) · 13.5 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#ifndef BER_h
#define BER_h
#include <Arduino.h>
typedef enum ASN_TYPE_WITH_VALUE {
// Primatives
INTEGER = 0x02,
STRING = 0x04,
NULLTYPE = 0x05,
OID = 0x06,
// derived
// Complex
STRUCTURE = 0x30,
NETWORK_ADDRESS = 0x40,
TIMESTAMP = 0x43,
GetRequestPDU = 0xA0,
GetNextRequestPDU = 0xA1,
GetResponsePDU = 0xA2,
SetRequestPDU = 0xA3,
TrapPDU = 0xA4,
Trapv2PDU = 0xA7
} ASN_TYPE;
// primitive types inherits straight off the container, complex come off complexType
// all primitives have to serialise 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.
// complex types have a linked list of BER_CONTAINERS to hold its' children.
class BER_CONTAINER {
public:
BER_CONTAINER(bool isPrimative, ASN_TYPE type): _isPrimative(isPrimative), _type(type){};
virtual ~BER_CONTAINER(){};
bool _isPrimative;
ASN_TYPE _type;
unsigned short _length;
virtual int serialise(unsigned char* buf) =0;
virtual bool fromBuffer(unsigned char* buf) = 0;
virtual int getLength() = 0;
};
class NetworkAddress: public BER_CONTAINER {
public:
NetworkAddress(): BER_CONTAINER(true, NETWORK_ADDRESS){};
NetworkAddress(IPAddress ip): _value(ip), BER_CONTAINER(true, NETWORK_ADDRESS){};
~NetworkAddress(){};
IPAddress _value;
int serialise(unsigned char* buf){
unsigned char* ptr = buf;
*ptr++ = _type;
_length = 4;
*ptr++ = _length;
*ptr++ = _value[0];
*ptr++ = _value[1];
*ptr++ = _value[2];
*ptr++ = _value[3];
return _length + 2;
}
bool fromBuffer(unsigned char* buf){
buf++;// skip Type
_length = *buf;
buf++;
byte tempAddress[4];
tempAddress[0] = *buf++;
tempAddress[1] = *buf++;
tempAddress[2] = *buf++;
tempAddress[3] = *buf++;
_value = IPAddress(tempAddress);
return true;
}
int getLength(){
return _length;
}
};
class IntegerType: public BER_CONTAINER {
public:
IntegerType(): BER_CONTAINER(true, INTEGER){};
IntegerType(unsigned long value): _value(value), BER_CONTAINER(true, INTEGER){};
~IntegerType(){};
unsigned long _value;
int serialise(unsigned char* buf){
// here we print out the BER encoded ASN.1 bytes, which includes type, length and value. we return the length of the entire block (TL&V) ni bytes;
unsigned char* ptr = buf;
*ptr = _type;
ptr++;
unsigned char* lengthPtr = ptr++;
if(_value != 0){
_length = 4; // FIXME: need to give this dynamic length
// while(_length > 1){
// if(_value >> 24 == 0){
// _length--;
// _value = _value << 8;
// } else {
// break;
// }
// }
*ptr++ = _value >> 24 & 0xFF;
*ptr++ = _value >> 16 & 0xFF;
*ptr++ = _value >> 8 & 0xFF;
*ptr++ = _value & 0xFF;
} else {
_length = 1;
*ptr = 0;
}
*lengthPtr = _length;
return _length + 2;
}
bool fromBuffer(unsigned char* buf){
buf++;// skip Type
_length = *buf;
buf++;
unsigned short tempLength = _length;
// _value = *buf; // TODO: make work for integers more than 255
_value = 0;
while(tempLength > 0){
_value = _value << 8;
_value = _value | *buf++;
tempLength--;
}
return true;
}
int getLength(){
return _length;
}
};
class TimestampType: public IntegerType {
public:
TimestampType(): IntegerType(){
_type = TIMESTAMP;
};
TimestampType(unsigned long value): IntegerType(value){
_type = TIMESTAMP;
};
~TimestampType(){};
};
class OctetType: public BER_CONTAINER {
public:
OctetType(): BER_CONTAINER(true, STRING){};
OctetType(char* value): BER_CONTAINER(true, STRING){
strncpy(_value, value, 32);
_value[31] = 0;
};
~OctetType(){};
char _value[32];
int serialise(unsigned char* buf){
// here we print out the BER encoded ASN.1 bytes, which includes type, length and value.
char* ptr = (char*)buf;
*ptr = _type;
ptr++;
_length = sprintf(ptr + 1, "%s", _value);
*ptr = _length;
return _length + 2;
}
bool fromBuffer(unsigned char* buf){
buf++;// skip Type
_length = *buf;
buf++;
memset(_value, 0, 32);
if(_length > 32){
strncpy(_value, (char*)buf, 31);
} else {
strncpy(_value, (char*)buf, _length);
}
return true;
}
int getLength(){
return _length;
}
};
class OIDType: public BER_CONTAINER {
public:
OIDType(): BER_CONTAINER(true, OID){};
OIDType(char* value): BER_CONTAINER(true, OID){
strncpy(_value, value, 50);
};
~OIDType(){};
char _value[50];
int serialise(unsigned char* buf){
// here we print out the BER encoded ASN.1 bytes, which includes type, length and value.
char* ptr = (char*)buf;
*ptr = _type;
ptr++;
char* lengthPtr = ptr;
ptr++;
*ptr = 0x2b;
char* internalPtr = ++ptr;
char* valuePtr = &_value[5];
_length = 3;
bool toBreak = false;
while(true){
char* start = valuePtr;
char* end = strchr(start, '.');
if(!end) {
end = strchr(start, 0);
toBreak = true;
}
char tempBuf[10];
memset(tempBuf, 0, 10);
// char* tempBuf = (char*) malloc(sizeof(char) * (end-start));
strncpy(tempBuf, start, end-start+1);
long tempVal;
tempVal = atoi(tempBuf);
if(tempVal > 127){
*ptr++ = ((tempVal/128) | 0x80) & 0xFF;
*ptr++ = tempVal%128 & 0xFF;
_length += 2;
} else {
_length += 1;
*ptr++ = (char)tempVal;
}
valuePtr = end+1;
// free(tempBuf);
if(toBreak) break;
delay(1);
}
*lengthPtr = _length - 2;
return _length;
}
bool fromBuffer(unsigned char* buf){
buf++;// skip Type
_length = *buf;
buf++;
buf++;
memset(_value, 0, 50);
_value[0] = '.';
_value[1] = '1';
_value[2] = '.';
_value[3] = '3'; // we fill in the first two bytes already
char* ptr = &_value[4];
char i = _length -1;
while(i > 0){
if(*buf < 128){ // we can keep raw
ptr += sprintf(ptr, ".%d", *buf);
i--;
buf++;
} else { // we have to do the special >128 thing
long value = 0; // keep track of the actual thing
char n = 0; // count how many large bits have been set
unsigned char tempBuf[4]; // nobigger than 4 bytes
while(*buf > 127){
i--;
*buf<<=1;
*buf>>=1;
tempBuf[n] = *buf;
n++;
buf++;
}
value = *buf;
buf++;
i--;
for(char k = 0; k < n; k++){
value += (128 * (n-k)) * tempBuf[k];
}
ptr += sprintf(ptr, ".%d", value);
}
delay(1);
}
Serial.print("OID: " );Serial.println(_value);
// memcpy(_value, buf, _length);
return true;
}
int getLength(){
return _length;
}
};
class NullType: public BER_CONTAINER {
public:
NullType(): BER_CONTAINER(true, NULLTYPE){};
~NullType(){
};
char _value = NULL;
int serialise(unsigned char* buf){
// here we print out the BER encoded ASN.1 bytes, which includes type, length and value.
char* ptr = (char*)buf;
*ptr = _type;
ptr++;
*ptr = 0;
return 2;
}
bool fromBuffer(unsigned char* buf){
_length = 0;
return true;
}
int getLength(){
return 0;
}
};
typedef struct BER_LINKED_LIST {
~BER_LINKED_LIST(){
delete next; next = 0;
delete value; value = 0;
}
BER_CONTAINER* value = 0;
struct BER_LINKED_LIST* next = 0;
} ValuesList;
class ComplexType: public BER_CONTAINER {
public:
ComplexType(ASN_TYPE type): BER_CONTAINER(false, type){};
~ComplexType(){
delete _values;
}
ValuesList* _values = 0;
bool fromBuffer(unsigned char* buf){
// the buffer we get passed in is the complete ASN Container, including the type header.
buf++; // Skip our own type
_length = *buf;
if(_length > 127){
// do this
_length -= 128;
buf++;
_length = (_length*128) + (*buf-128);
}
buf++;
// now we are at the front of a list of one or many other types, lets do our loop
unsigned char i = 0;
while(i < _length){
ASN_TYPE valueType = (ASN_TYPE)*buf;
buf++; i++;
unsigned short valueLength = *buf;
if(valueLength > 127){
// also do this.
valueLength -= 128;
buf++; i++;
valueLength = (valueLength*128) + (*buf-128);
Serial.println(F("DOUBLE LENGTH BYTES"));
}
buf++; i++;
// Serial.println("SUP");
// char* newValue = (char*)malloc(sizeof(char) * valueLength + 2);
// memset(newValue, 0, valueLength + 2);
// memcpy(newValue, buf - 2, valueLength + 2);
// buf += valueLength; i+= valueLength;
BER_CONTAINER* newObj;
switch(valueType){
case STRUCTURE:
case GetRequestPDU:
case GetNextRequestPDU:
case GetResponsePDU:
case SetRequestPDU:
case TrapPDU: // should never get trap, but put it in anyway
case Trapv2PDU:
newObj = new ComplexType(valueType);
break;
// primitive
case INTEGER:
newObj = new IntegerType();
break;
case STRING:
newObj = new OctetType();
break;
case OID:
newObj = new OIDType();
break;
case NULLTYPE:
newObj = new NullType();
break;
// devired
case NETWORK_ADDRESS:
newObj = new NetworkAddress();
break;
case TIMESTAMP:
newObj = new TimestampType();
break;
}
newObj->fromBuffer(buf - 2);
buf += valueLength; i+= valueLength;
//newObj->fromBuffer(newValue);
// free(newValue);
addValueToList(newObj);
}
return true;
}
int serialise(unsigned char* buf){
int actualLength = 0;
unsigned char* ptr = buf;
*ptr = _type;
ptr++;
unsigned char* lengthPtr = ptr++;
*lengthPtr = 0;
ValuesList* conductor = _values;
while(conductor){
// Serial.print("about to serialise something of type: ");Serial.println(conductor->value->_type, HEX);
delay(0);
int length = conductor->value->serialise(ptr);
ptr += length;
actualLength += length;
conductor = conductor->next;
}
if(actualLength > 127){
// Serial.println("TOO BIG");
// bad, we have to add another byte and shift everything afterwards by 1 >>
// first byte is 128 + (actualLength / 128)
// second is actualLength % 128;
*lengthPtr = 129;
// lets move everything right one byte, start from back..
unsigned char* endPtrPos = ptr + 1;
for(unsigned char* i = endPtrPos; i > buf +1; i--){
// i is the char we are moving INTO
*i = *(i - 1);
}
*(lengthPtr+1) = (actualLength % 128)|0x80;
actualLength += 1; // account for extra byte in Length param
} else {
*lengthPtr = actualLength;
}
return actualLength + 2;
}
int getLength(){
return _length;
}
bool addValueToList(BER_CONTAINER* newObj){
ValuesList* conductor = _values;
if(_values != 0){
while(conductor->next != 0){
conductor = conductor->next;
delay(0);
}
conductor->next = new ValuesList;
conductor = conductor->next;
conductor->value = newObj;
conductor->next = 0;
} else {
_values = new ValuesList;
_values->value = newObj;
_values->next = 0;
}
}
};
#endif