forked from 0neblock/Arduino_SNMP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSNMPResponse.h
128 lines (108 loc) · 3.32 KB
/
SNMPResponse.h
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
#ifndef SNMPResponse_h
#define SNMPResponse_h
typedef enum ERROR_STATUS_WITH_VALUE {
NO_ERROR = 0,
TOO_BIG = 1,
NO_SUCH_NAME = 2,
BAD_VALUE = 3,
READ_ONLY = 4,
GEN_ERR = 5,
// v2c options are below
NO_ACCESS = 6,
WRONG_TYPE = 7,
WRONG_LENGTH = 8,
WRONG_ENCODING = 9,
WRONG_VALUE = 10,
NO_CREATION = 11,
INCONSISTENT_VALUE = 12,
RESOURCE_UNAVAILABLE = 13,
COMMIT_FAILED = 14,
UNDO_FAILED = 15,
AUTHORIZATION_ERROR = 16,
NOT_WRITABLE = 17,
INCONSISTENT_NAME = 18
} ERROR_STATUS;
struct SNMPOIDResponse {
~SNMPOIDResponse(){
}
ERROR_STATUS errorStatus;
ASN_TYPE type;
OIDType* oid = 0;
BER_CONTAINER* value = 0;
};
typedef struct OIDResponseList {
~OIDResponseList(){
delete next; next = 0;
delete value; value = 0;
}
SNMPOIDResponse* value = 0;
struct OIDResponseList* next = 0;
} ResponseList;
class SNMPResponse {
public:
~SNMPResponse(){
delete responseList; responseList = 0;
delete response; response = 0;
}
int version = 0;
char communityString[15];
unsigned long requestID = 0;
ERROR_STATUS errorStatus = (ERROR_STATUS)0;
int errorIndex = 0;
ASN_TYPE responseType = GetResponsePDU;
ResponseList* responseList = new ResponseList();
ResponseList* responseConductor = responseList;
bool addResponse(SNMPOIDResponse* response);
bool addErrorResponse(SNMPOIDResponse* response, int index);
int serialise(unsigned char* buf);
private:
ComplexType* response = 0;
bool build();
};
bool SNMPResponse::addResponse(SNMPOIDResponse* response){
responseConductor->value = response;
responseConductor->next = new ResponseList();
responseConductor = responseConductor->next;
}
bool SNMPResponse::addErrorResponse(SNMPOIDResponse* response, int index){
responseConductor->value = response;
// check for error passed in
if(response->errorStatus != NO_ERROR){
errorStatus = response->errorStatus;
errorIndex = index;
}
responseConductor->next = new ResponseList();
responseConductor = responseConductor->next;
}
int SNMPResponse::serialise(unsigned char* buf){
if(build()){
return response->serialise(buf);
}
return 0;
}
bool SNMPResponse::build(){
response = new ComplexType(STRUCTURE);
response->addValueToList(new IntegerType(version));
response->addValueToList(new OctetType(communityString));
ComplexType* PDUObj = new ComplexType(responseType);
PDUObj->addValueToList(new IntegerType(requestID));
PDUObj->addValueToList(new IntegerType(errorStatus));
PDUObj->addValueToList(new IntegerType(errorIndex));
ComplexType* varBindList = new ComplexType(STRUCTURE);
responseConductor = responseList;
// chuck all varBinds in
while(true){
ComplexType* varBind = new ComplexType(STRUCTURE);
varBind->addValueToList(responseConductor->value->oid);
varBind->addValueToList(responseConductor->value->value);
varBindList->addValueToList(varBind);
if(!responseConductor->next->value){
break;
}
responseConductor = responseConductor->next;
delay(0);
}
PDUObj->addValueToList(varBindList);
response->addValueToList(PDUObj);
}
#endif