forked from mberntsen/Agentuino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgentuino.cpp
377 lines (312 loc) · 9.8 KB
/
Agentuino.cpp
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
/*
Agentuino.cpp - An Arduino library for a lightweight SNMP Agent.
Copyright (C) 2010 Eric C. Gionet <[email protected]>
All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//
// sketch_aug23a
//
#include "Agentuino.h"
#include "EtherCard.h"
//EthernetUDP Udp;
SNMP_API_STAT_CODES AgentuinoClass::begin()
{
// set community names
_getCommName = "public";
_setCommName = "private";
// set community name set/get sizes
_setSize = strlen(_setCommName);
_getSize = strlen(_getCommName);
// init UDP socket
//Udp.begin(SNMP_DEFAULT_PORT);
//register udpSerialPrint() to port 1337
//ether.udpServerListenOnPort(&udpSerialPrint, SNMP_DEFAULT_PORT);
_srcPort = SNMP_DEFAULT_PORT;
return SNMP_API_STAT_SUCCESS;
}
SNMP_API_STAT_CODES AgentuinoClass::begin(char *getCommName, char *setCommName, uint16_t port)
{
// set community name set/get sizes
_setSize = strlen(setCommName);
_getSize = strlen(getCommName);
// validate get/set community name sizes
if ( _setSize > SNMP_MAX_NAME_LEN + 1 || _getSize > SNMP_MAX_NAME_LEN + 1 ) {
return SNMP_API_STAT_NAME_TOO_BIG;
}
// set community names
_getCommName = getCommName;
_setCommName = setCommName;
// validate session port number
if ( port == NULL || port == 0 ) port = SNMP_DEFAULT_PORT;
_srcPort = port;
// init UDP socket
//Udp.begin(port);
//register udpSerialPrint() to port 1337
//ether.udpServerListenOnPort(&udpSerialPrint, SNMP_DEFAULT_PORT);
return SNMP_API_STAT_SUCCESS;
}
void AgentuinoClass::listen(void)
{
// if bytes are available in receive buffer
// and pointer to a function (delegate function)
// isn't null, trigger the function
ether.packetLoop(ether.packetReceive());
//Udp.parsePacket();
if ( _packetAvailable && _callback != NULL ) (*_callback)();
}
void AgentuinoClass::parsePacket(uint16_t dest_port, uint8_t src_ip[IP_LEN], uint16_t src_port, const char *data, uint16_t len){
_packetAvailable = true;
memcpy(_dstIp, src_ip, 4);
_dstPort = src_port;
_srcPort = dest_port;
_packetSize = len;
// reset packet array
memset(_packet, 0, SNMP_MAX_PACKET_LEN);
// validate packet
if ( _packetSize != 0 && _packetSize > SNMP_MAX_PACKET_LEN ) {
_apiError = SNMP_API_STAT_PACKET_TOO_BIG;
return;
}
// get UDP packet
memcpy(_packet, data, _packetSize);
// packet check 1
if ( _packet[0] != 0x30 ) {
_apiError = SNMP_API_STAT_PACKET_INVALID;
return;
}
_apiError = SNMP_API_STAT_SUCCESS;
}
SNMP_API_STAT_CODES AgentuinoClass::requestPdu(SNMP_PDU *pdu)
{
char *community;
// sequence length
byte seqLen;
// version
byte verLen, verEnd;
// community string
byte comLen, comEnd;
// pdu
byte pduTyp, pduLen;
byte ridLen, ridEnd;
byte errLen, errEnd;
byte eriLen, eriEnd;
byte vblTyp, vblLen;
byte vbiTyp, vbiLen;
byte obiLen, obiEnd;
byte valTyp, valLen, valEnd;
byte i;
_packetAvailable = false;
if (_apiError != SNMP_API_STAT_SUCCESS) {
return _apiError;
}
// sequence length
seqLen = _packet[1];
// version
verLen = _packet[3];
verEnd = 3 + verLen;
// community string
comLen = _packet[verEnd + 2];
comEnd = verEnd + 2 + comLen;
// pdu
pduTyp = _packet[comEnd + 1];
pduLen = _packet[comEnd + 2];
ridLen = _packet[comEnd + 4];
ridEnd = comEnd + 4 + ridLen;
errLen = _packet[ridEnd + 2];
errEnd = ridEnd + 2 + errLen;
eriLen = _packet[errEnd + 2];
eriEnd = errEnd + 2 + eriLen;
vblTyp = _packet[eriEnd + 1];
vblLen = _packet[eriEnd + 2];
vbiTyp = _packet[eriEnd + 3];
vbiLen = _packet[eriEnd + 4];
obiLen = _packet[eriEnd + 6];
obiEnd = eriEnd + obiLen + 6;
valTyp = _packet[obiEnd + 1];
valLen = _packet[obiEnd + 2];
valEnd = obiEnd + 2 + valLen;
// extract version
pdu->version = 0;
for ( i = 0; i < verLen; i++ ) {
pdu->version = (pdu->version << 8) | _packet[5 + i];
}
// validate version
// pdu-type
pdu->type = (SNMP_PDU_TYPES)pduTyp;
_dstType = pdu->type;
// validate community size
if ( comLen > SNMP_MAX_NAME_LEN ) {
// set pdu error
pdu->error = SNMP_ERR_TOO_BIG;
return SNMP_API_STAT_NAME_TOO_BIG;
}
// validate community name
if ( pdu->type == SNMP_PDU_SET && comLen == _setSize ) {
for ( i = 0; i < _setSize; i++ ) {
if( _packet[verEnd + 3 + i] != (byte)_setCommName[i] ) {
// set pdu error
pdu->error = SNMP_ERR_NO_SUCH_NAME;
return SNMP_API_STAT_NO_SUCH_NAME;
}
}
} else if ( pdu->type == SNMP_PDU_GET && comLen == _getSize ) {
for ( i = 0; i < _getSize; i++ ) {
if( _packet[verEnd + 3 + i] != (byte)_getCommName[i] ) {
// set pdu error
pdu->error = SNMP_ERR_NO_SUCH_NAME;
return SNMP_API_STAT_NO_SUCH_NAME;
}
}
} else if ( pdu->type == SNMP_PDU_GET_NEXT && comLen == _getSize ) {
for ( i = 0; i < _getSize; i++ ) {
if( _packet[verEnd + 3 + i] != (byte)_getCommName[i] ) {
// set pdu error
pdu->error = SNMP_ERR_NO_SUCH_NAME;
return SNMP_API_STAT_NO_SUCH_NAME;
}
}
} else {
// set pdu error
pdu->error = SNMP_ERR_NO_SUCH_NAME;
return SNMP_API_STAT_NO_SUCH_NAME;
}
// extract reqiest-id 0x00 0x00 0x00 0x01 (4-byte int aka int32)
pdu->requestId = 0;
for ( i = 0; i < ridLen; i++ ) {
pdu->requestId = (pdu->requestId << 8) | _packet[comEnd + 5 + i];
}
// extract error
pdu->error = SNMP_ERR_NO_ERROR;
int32_t err = 0;
for ( i = 0; i < errLen; i++ ) {
err = (err << 8) | _packet[ridEnd + 3 + i];
}
pdu->error = (SNMP_ERR_CODES)err;
// extract error-index
pdu->errorIndex = 0;
for ( i = 0; i < eriLen; i++ ) {
pdu->errorIndex = (pdu->errorIndex << 8) | _packet[errEnd + 3 + i];
}
// validate object-identifier size
if ( obiLen > SNMP_MAX_OID_LEN ) {
// set pdu error
pdu->error = SNMP_ERR_TOO_BIG;
return SNMP_API_STAT_OID_TOO_BIG;
}
// extract and contruct object-identifier
memset(pdu->OID.data, 0, SNMP_MAX_OID_LEN);
pdu->OID.size = obiLen;
for ( i = 0; i < obiLen; i++ ) {
pdu->OID.data[i] = _packet[eriEnd + 7 + i];
}
// value-type
pdu->VALUE.syntax = (SNMP_SYNTAXES)valTyp;
// validate value size
if ( obiLen > SNMP_MAX_VALUE_LEN ) {
// set pdu error
pdu->error = SNMP_ERR_TOO_BIG;
return SNMP_API_STAT_VALUE_TOO_BIG;
}
// value-size
pdu->VALUE.size = valLen;
// extract value
memset(pdu->VALUE.data, 0, SNMP_MAX_VALUE_LEN);
for ( i = 0; i < valLen; i++ ) {
pdu->VALUE.data[i] = _packet[obiEnd + 3 + i];
}
return SNMP_API_STAT_SUCCESS;
}
SNMP_API_STAT_CODES AgentuinoClass::responsePdu(SNMP_PDU *pdu)
{
int32_u u;
ether.makeUdpReplyStart(_srcPort);
// Length of entire SNMP packet
_packetSize = 25 + sizeof(pdu->requestId) + sizeof(pdu->error) + sizeof(pdu->errorIndex) + pdu->OID.size + pdu->VALUE.size;
memset(_packet, 0, SNMP_MAX_PACKET_LEN);
if ( _dstType == SNMP_PDU_SET ) {
_packetSize += _setSize;
} else {
_packetSize += _getSize;
}
ether.makeUdpReplyData(SNMP_SYNTAX_SEQUENCE);
ether.makeUdpReplyData(_packetSize - 2);
// SNMP version
ether.makeUdpReplyData(SNMP_SYNTAX_INT);
ether.makeUdpReplyData(0x01);
ether.makeUdpReplyData(0x00);
// SNMP community string
ether.makeUdpReplyData(SNMP_SYNTAX_OCTETS);
if ( _dstType == SNMP_PDU_SET ) {
ether.makeUdpReplyData(_setSize);
ether.makeUdpReplyData(_setCommName, _setSize);
} else {
ether.makeUdpReplyData(_getSize);
ether.makeUdpReplyData(_getCommName, _getSize);
}
// SNMP PDU
ether.makeUdpReplyData(pdu->type);
ether.makeUdpReplyData(sizeof(pdu->requestId) + sizeof((int32_t)pdu->error) + sizeof(pdu->errorIndex) + pdu->OID.size + pdu->VALUE.size + 14);
// Request ID (size always 4 e.g. 4-byte int)
ether.makeUdpReplyData(SNMP_SYNTAX_INT);
ether.makeUdpReplyData(sizeof(pdu->requestId));
u.int32 = pdu->requestId;
ether.makeUdpReplyData(u.data[3]);
ether.makeUdpReplyData(u.data[2]);
ether.makeUdpReplyData(u.data[1]);
ether.makeUdpReplyData(u.data[0]);
// Error (size always 4 e.g. 4-byte int)
ether.makeUdpReplyData(SNMP_SYNTAX_INT);
ether.makeUdpReplyData(sizeof((int32_t)pdu->error));
u.int32 = pdu->error;
ether.makeUdpReplyData(u.data[3]);
ether.makeUdpReplyData(u.data[2]);
ether.makeUdpReplyData(u.data[1]);
ether.makeUdpReplyData(u.data[0]);
// Error Index (size always 4 e.g. 4-byte int)
ether.makeUdpReplyData(SNMP_SYNTAX_INT);
ether.makeUdpReplyData(sizeof(pdu->errorIndex));
u.int32 = pdu->errorIndex;
ether.makeUdpReplyData(u.data[3]);
ether.makeUdpReplyData(u.data[2]);
ether.makeUdpReplyData(u.data[1]);
ether.makeUdpReplyData(u.data[0]);
// Varbind List
ether.makeUdpReplyData(SNMP_SYNTAX_SEQUENCE);
ether.makeUdpReplyData(pdu->OID.size + pdu->VALUE.size + 6);
// Varbind
ether.makeUdpReplyData(SNMP_SYNTAX_SEQUENCE);
ether.makeUdpReplyData(pdu->OID.size + pdu->VALUE.size + 4);
// ObjectIdentifier
ether.makeUdpReplyData(SNMP_SYNTAX_OID);
ether.makeUdpReplyData(pdu->OID.size);
ether.makeUdpReplyData(pdu->OID.data, pdu->OID.size);
// Value
ether.makeUdpReplyData(pdu->VALUE.syntax);
ether.makeUdpReplyData(pdu->VALUE.size);
ether.makeUdpReplyData(pdu->VALUE.data, pdu->VALUE.size);
ether.makeUdpReplyFinish();
return SNMP_API_STAT_SUCCESS;
}
void AgentuinoClass::onPduReceive(onPduReceiveCallback pduReceived)
{
_callback = pduReceived;
}
void AgentuinoClass::freePdu(SNMP_PDU *pdu)
{
memset(pdu->OID.data, 0, SNMP_MAX_OID_LEN);
memset(pdu->VALUE.data, 0, SNMP_MAX_VALUE_LEN);
free((char *) pdu);
}
// Create one global object
AgentuinoClass Agentuino;