forked from 0neblock/Arduino_SNMP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSNMPTrap.h
163 lines (139 loc) · 4.9 KB
/
SNMPTrap.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
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
// we're going to have a trap object, which is created in setup. it includes the main trapOID, other trap-ish options, and an attached list of the OIDCallback objects (the ones you get back from addIntegerHandler etc) to contain the values that need to be ent with this Trap.
// it ca then be called from code, trap->send of fuckin whatever. trap receivers should also be attached to a list that can be changed
#ifndef SNMPTrap_h
#define SNMPTrap_h
class SNMPTrap {
public:
SNMPTrap(const char* community, short version): _community(community), _version(version){
if(version == 0){
version1 = true;
}
if(version == 1){
version2 = true;
}
};
short _version;
const char* _community;
IPAddress agentIP;
OIDType* trapOID;
TimestampCallback* uptimeCallback;
short genericTrap = 6;
short specificTrap;
// the setters that need to be configured for each trap.
void setTrapOID(OIDType* oid){
trapOID = oid;
}
void setSpecificTrap(short num){
specificTrap = num;
}
void setIP(IPAddress ip){
agentIP = ip;
}
void setUDP(UDP* udp){
_udp = udp;
}
void setUptimeCallback(TimestampCallback* uptime){
uptimeCallback = uptime;
}
void addOIDPointer(ValueCallback* callback);
ValueCallbacks* callbacks = new ValueCallbacks();
ValueCallbacks* callbacksCursor = callbacks;
UDP* _udp = 0;
bool sendTo(IPAddress ip){
//Serial.println("Trying to Send Packet");
if(!_udp){
return false;
}
if(!build()){
Serial.println("Failed Building packet..");
delete packet;
return false;
}
unsigned char _packetBuffer[SNMP_PACKET_LENGTH];
memset(_packetBuffer, 0, SNMP_PACKET_LENGTH);
int length = packet->serialise(_packetBuffer);
delete packet;
_udp->beginPacket(ip, 162);
_udp->write(_packetBuffer, length);
return _udp->endPacket();
}
ComplexType* packet = 0;
bool build();
bool version1 = false;
bool version2 = false;
void clearOIDList(){ // this just removes the list, does not kill the values in the list
callbacksCursor = callbacks;
delete callbacksCursor;
callbacks = new ValueCallbacks();
callbacksCursor = callbacks;
}
};
bool SNMPTrap::build(){
if(packet) delete packet;
packet = new ComplexType(STRUCTURE);
packet->addValueToList(new IntegerType((int)_version));
packet->addValueToList(new OctetType((char*)_community));
ComplexType* trapPDU;
if(version1){
trapPDU = new ComplexType(TrapPDU);
} else if(version2){
trapPDU = new ComplexType(Trapv2PDU);
} else {
return false;
}
trapPDU->addValueToList(new OIDType(trapOID->_value));
trapPDU->addValueToList(new NetworkAddress(agentIP));
trapPDU->addValueToList(new IntegerType(genericTrap));
trapPDU->addValueToList(new IntegerType(specificTrap));
trapPDU->addValueToList(new TimestampType(*(uptimeCallback->value)));
ComplexType* varBindList = new ComplexType(STRUCTURE);
callbacksCursor = callbacks;
if(callbacksCursor->value){
while(true){
ComplexType* varBind = new ComplexType(STRUCTURE);
varBind->addValueToList(new OIDType(callbacksCursor->value->OID));
BER_CONTAINER* value;
switch(callbacksCursor->value->type){
case INTEGER:
{
value = new IntegerType(*((IntegerCallback*)callbacksCursor->value)->value);
}
break;
case TIMESTAMP:
{
value = new TimestampType(*((TimestampCallback*)callbacksCursor->value)->value);
}
break;
case STRING:
{
value = new OctetType(*((StringCallback*)callbacksCursor->value)->value);
}
break;
}
varBind->addValueToList(value);
varBindList->addValueToList(varBind);
if(callbacksCursor->next){
callbacksCursor = callbacksCursor->next;
} else {
break;
}
}
}
trapPDU->addValueToList(varBindList);
packet->addValueToList(trapPDU);
return true;
}
void SNMPTrap::addOIDPointer(ValueCallback* callback){
callbacksCursor = callbacks;
if(callbacksCursor->value){
while(callbacksCursor->next != 0){
callbacksCursor = callbacksCursor->next;
}
callbacksCursor->next = new ValueCallbacks();
callbacksCursor = callbacksCursor->next;
callbacksCursor->value = callback;
callbacksCursor->next = 0;
} else
callbacks->value = callback;
}
#endif