Implemented Sensor class; Added LoRaWAN subagent
[public/netxms.git] / include / nxsnmp.h
CommitLineData
489b117b 1/*
927fb8a6 2** NetXMS - Network Management System
7aad6641 3** Copyright (C) 2003-2013 Victor Kirhenshtein
927fb8a6
VK
4**
5** This program is free software; you can redistribute it and/or modify
65d2c384
VK
6** it under the terms of the GNU Lesser General Public License as published by
7** the Free Software Foundation; either version 3 of the License, or
927fb8a6
VK
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
65d2c384 15** You should have received a copy of the GNU Lesser General Public License
927fb8a6
VK
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
9eea20df 19** File: nxsnmp.h
927fb8a6
VK
20**
21**/
22
23#ifndef _nxsnmp_h_
24#define _nxsnmp_h_
25
34138f2d 26#ifdef __cplusplus
d853cd74
VK
27#include <nms_common.h>
28#include <nms_threads.h>
34138f2d 29#endif
d853cd74 30
927fb8a6
VK
31#ifdef _WIN32
32#ifdef LIBNXSNMP_EXPORTS
33#define LIBNXSNMP_EXPORTABLE __declspec(dllexport)
34#else
35#define LIBNXSNMP_EXPORTABLE __declspec(dllimport)
36#endif
37#else /* _WIN32 */
38#define LIBNXSNMP_EXPORTABLE
39#endif
40
1a4de45b
AK
41/** \ingroup SNMP
42 * @{
43 */
927fb8a6 44
21ea6058
VK
45/***************************************************************
46 Following part of the file may conflict with net-snmp includes,
47 so it can be excluded by defining NXSNMP_WITH_NET_SNMP
48****************************************************************/
49
50#ifdef NXSNMP_WITH_NET_SNMP
51
52#define SNMP_VERSION_2C 1
6bdca013 53#define SNMP_DEFAULT_PORT 161
21ea6058
VK
54
55#else
56
57
927fb8a6 58//
3d1cb7d6
VK
59// Various constants
60//
61
c4366266
VK
62#define MAX_OID_LEN 128
63#define MAX_MIB_OBJECT_NAME 64
64#define SNMP_DEFAULT_PORT 161
65#define SNMP_MAX_CONTEXT_NAME 256
66#define SNMP_MAX_ENGINEID_LEN 256
67#define SNMP_DEFAULT_MSG_MAX_SIZE 65536
3d1cb7d6
VK
68
69
70//
6ed0d23d
VK
71// OID comparision results
72//
73
74#define OID_ERROR -1
75#define OID_EQUAL 0
9c5ebc32
VK
76#define OID_PRECEDING 1
77#define OID_FOLLOWING 2
78#define OID_SHORTER 3
79#define OID_LONGER 4
6ed0d23d 80
1f4c37ee
VK
81/**
82 * libnxsnmp error codes
83 */
d454fb4c
VK
84#define SNMP_ERR_SUCCESS 0 /* success */
85#define SNMP_ERR_TIMEOUT 1 /* request timeout */
86#define SNMP_ERR_PARAM 2 /* invalid parameters passed to function */
87#define SNMP_ERR_SOCKET 3 /* unable to create socket */
88#define SNMP_ERR_COMM 4 /* send/receive error */
89#define SNMP_ERR_PARSE 5 /* error parsing PDU */
90#define SNMP_ERR_NO_OBJECT 6 /* given object doesn't exist on agent */
91#define SNMP_ERR_HOSTNAME 7 /* invalid hostname or IP address */
92#define SNMP_ERR_BAD_OID 8 /* object id is incorrect */
93#define SNMP_ERR_AGENT 9 /* agent returns an error */
94#define SNMP_ERR_BAD_TYPE 10 /* unknown variable data type */
95#define SNMP_ERR_FILE_IO 11 /* file I/O error */
96#define SNMP_ERR_BAD_FILE_HEADER 12 /* file header is invalid */
97#define SNMP_ERR_BAD_FILE_DATA 13 /* file data is invalid or corrupted */
c4366266
VK
98#define SNMP_ERR_UNSUPP_SEC_LEVEL 14 /* unsupported security level */
99#define SNMP_ERR_TIME_WINDOW 15 /* not in time window */
100#define SNMP_ERR_SEC_NAME 16 /* unknown security name */
101#define SNMP_ERR_ENGINE_ID 17 /* unknown engine ID */
102#define SNMP_ERR_AUTH_FAILURE 18 /* authentication failure */
103#define SNMP_ERR_DECRYPTION 19 /* decryption error */
104#define SNMP_ERR_BAD_RESPONSE 20 /* malformed or unexpected response from agent */
d454fb4c
VK
105
106
107//
108// MIB parser error codes
109//
110
111#define SNMP_MPE_SUCCESS 0
112#define SNMP_MPE_PARSE_ERROR 1
ddbdfb71
VK
113
114
115//
927fb8a6
VK
116// SNMP versions
117//
118
119#define SNMP_VERSION_1 0
120#define SNMP_VERSION_2C 1
07f6978d 121#define SNMP_VERSION_3 3
927fb8a6
VK
122
123
124//
125// PDU types
126//
127
d853cd74 128#define SNMP_INVALID_PDU 255
927fb8a6
VK
129#define SNMP_GET_REQUEST 0
130#define SNMP_GET_NEXT_REQUEST 1
c4366266 131#define SNMP_RESPONSE 2
927fb8a6
VK
132#define SNMP_SET_REQUEST 3
133#define SNMP_TRAP 4
134#define SNMP_GET_BULK_REQUEST 5
135#define SNMP_INFORM_REQUEST 6
c4366266 136#define SNMP_REPORT 8
927fb8a6
VK
137
138
139//
585ca569
VK
140// PDU error codes
141//
142
143#define SNMP_PDU_ERR_SUCCESS 0
144#define SNMP_PDU_ERR_TOO_BIG 1
145#define SNMP_PDU_ERR_NO_SUCH_NAME 2
146#define SNMP_PDU_ERR_BAD_VALUE 3
147#define SNMP_PDU_ERR_READ_ONLY 4
148#define SNMP_PDU_ERR_GENERIC 5
149
150
151//
927fb8a6
VK
152// ASN.1 identifier types
153//
154
155#define ASN_INTEGER 0x02
156#define ASN_BIT_STRING 0x03
157#define ASN_OCTET_STRING 0x04
158#define ASN_NULL 0x05
159#define ASN_OBJECT_ID 0x06
160#define ASN_SEQUENCE 0x30
161#define ASN_IP_ADDR 0x40
162#define ASN_COUNTER32 0x41
163#define ASN_GAUGE32 0x42
164#define ASN_TIMETICKS 0x43
165#define ASN_OPAQUE 0x44
166#define ASN_NSAP_ADDR 0x45
167#define ASN_COUNTER64 0x46
168#define ASN_UINTEGER32 0x47
2e0596dd
VK
169#define ASN_NO_SUCH_OBJECT 0x80
170#define ASN_NO_SUCH_INSTANCE 0x81
927fb8a6
VK
171#define ASN_GET_REQUEST_PDU 0xA0
172#define ASN_GET_NEXT_REQUEST_PDU 0xA1
c4366266 173#define ASN_RESPONSE_PDU 0xA2
927fb8a6
VK
174#define ASN_SET_REQUEST_PDU 0xA3
175#define ASN_TRAP_V1_PDU 0xA4
176#define ASN_GET_BULK_REQUEST_PDU 0xA5
177#define ASN_INFORM_REQUEST_PDU 0xA6
178#define ASN_TRAP_V2_PDU 0xA7
c4366266
VK
179#define ASN_REPORT_PDU 0xA8
180
181
182//
183// Security models
184//
185
186#define SNMP_SECURITY_MODEL_V1 1
187#define SNMP_SECURITY_MODEL_V2C 2
188#define SNMP_SECURITY_MODEL_USM 3
927fb8a6
VK
189
190
d454fb4c 191//
07f6978d
VK
192// SNMP V3 header flags
193//
194
195#define SNMP_AUTH_FLAG 0x01
196#define SNMP_PRIV_FLAG 0x02
197#define SNMP_REPORTABLE_FLAG 0x04
198
199
200//
e0d4b618
VK
201// SNMP v3 authentication methods
202//
203
204#define SNMP_AUTH_NONE 0
205#define SNMP_AUTH_MD5 1
206#define SNMP_AUTH_SHA1 2
207
208
209//
210// SNMP v3 encryption methods
211//
212
213#define SNMP_ENCRYPT_NONE 0
214#define SNMP_ENCRYPT_DES 1
215#define SNMP_ENCRYPT_AES 2
216
217
218//
d454fb4c
VK
219// MIB object access types
220//
221
34138f2d
VK
222#define MIB_ACCESS_READONLY 1
223#define MIB_ACCESS_READWRITE 2
224#define MIB_ACCESS_WRITEONLY 3
225#define MIB_ACCESS_NOACCESS 4
226#define MIB_ACCESS_NOTIFY 5
227#define MIB_ACCESS_CREATE 6
d454fb4c
VK
228
229
230//
231// MIB object status codes
232//
233
34138f2d
VK
234#define MIB_STATUS_MANDATORY 1
235#define MIB_STATUS_OPTIONAL 2
236#define MIB_STATUS_OBSOLETE 3
237#define MIB_STATUS_DEPRECATED 4
238#define MIB_STATUS_CURRENT 5
239
240
241//
242// MIB object data types
243//
244
245#define MIB_TYPE_OTHER 0
246#define MIB_TYPE_IMPORT_ITEM 1
247#define MIB_TYPE_OBJID 2
248#define MIB_TYPE_BITSTRING 3
249#define MIB_TYPE_INTEGER 4
250#define MIB_TYPE_INTEGER32 5
251#define MIB_TYPE_INTEGER64 6
252#define MIB_TYPE_UNSIGNED32 7
253#define MIB_TYPE_COUNTER 8
254#define MIB_TYPE_COUNTER32 9
255#define MIB_TYPE_COUNTER64 10
256#define MIB_TYPE_GAUGE 11
257#define MIB_TYPE_GAUGE32 12
258#define MIB_TYPE_TIMETICKS 13
259#define MIB_TYPE_OCTETSTR 14
260#define MIB_TYPE_OPAQUE 15
261#define MIB_TYPE_IPADDR 16
262#define MIB_TYPE_PHYSADDR 17
263#define MIB_TYPE_NETADDR 18
264#define MIB_TYPE_NAMED_TYPE 19
265#define MIB_TYPE_SEQID 20
266#define MIB_TYPE_SEQUENCE 21
267#define MIB_TYPE_CHOICE 22
268#define MIB_TYPE_TEXTUAL_CONVENTION 23
269#define MIB_TYPE_MACRO_DEFINITION 24
270#define MIB_TYPE_MODCOMP 25
271#define MIB_TYPE_TRAPTYPE 26
272#define MIB_TYPE_NOTIFTYPE 27
273#define MIB_TYPE_MODID 28
274#define MIB_TYPE_NSAPADDRESS 29
275#define MIB_TYPE_AGENTCAP 30
276#define MIB_TYPE_UINTEGER 31
277#define MIB_TYPE_NULL 32
278#define MIB_TYPE_OBJGROUP 33
279#define MIB_TYPE_NOTIFGROUP 34
d454fb4c 280
296ae03d
VK
281/**
282 * Flags for SNMPSaveMIBTree
283 */
d454fb4c
VK
284#define SMT_COMPRESS_DATA 0x01
285#define SMT_SKIP_DESCRIPTIONS 0x02
286
296ae03d
VK
287/**
288 * Flags for SnmpGet
289 */
290#define SG_VERBOSE 0x0001
291#define SG_STRING_RESULT 0x0002
292#define SG_RAW_RESULT 0x0004
293#define SG_HSTRING_RESULT 0x0008
294#define SG_PSTRING_RESULT 0x0010
d454fb4c 295
21ea6058
VK
296#endif /* NXSNMP_WITH_NET_SNMP */
297
298
34138f2d
VK
299#ifdef __cplusplus
300
927fb8a6 301//
d454fb4c
VK
302// MIB tree node
303//
304
8e8df806
VK
305class ZFile;
306
d454fb4c
VK
307class LIBNXSNMP_EXPORTABLE SNMP_MIBObject
308{
309private:
310 SNMP_MIBObject *m_pParent;
311 SNMP_MIBObject *m_pNext;
312 SNMP_MIBObject *m_pPrev;
313 SNMP_MIBObject *m_pFirst; // First child
314 SNMP_MIBObject *m_pLast; // Last child
315
967893bb 316 UINT32 m_dwOID;
d454fb4c
VK
317 TCHAR *m_pszName;
318 TCHAR *m_pszDescription;
6308c9be 319 TCHAR *m_pszTextualConvention;
d454fb4c
VK
320 int m_iType;
321 int m_iStatus;
322 int m_iAccess;
323
6308c9be 324 void Initialize();
d454fb4c
VK
325
326public:
6308c9be 327 SNMP_MIBObject();
967893bb 328 SNMP_MIBObject(UINT32 dwOID, const TCHAR *pszName);
489b117b 329 SNMP_MIBObject(UINT32 dwOID, const TCHAR *pszName, int iType,
6308c9be
VK
330 int iStatus, int iAccess, const TCHAR *pszDescription,
331 const TCHAR *pszTextualConvention);
d454fb4c
VK
332 ~SNMP_MIBObject();
333
6308c9be
VK
334 void setParent(SNMP_MIBObject *pObject) { m_pParent = pObject; }
335 void addChild(SNMP_MIBObject *pObject);
336 void setInfo(int iType, int iStatus, int iAccess, const TCHAR *pszDescription, const TCHAR *pszTextualConvention);
337 void setName(const TCHAR *pszName) { safe_free(m_pszName); m_pszName = _tcsdup(pszName); }
d454fb4c 338
6308c9be
VK
339 SNMP_MIBObject *getParent() { return m_pParent; }
340 SNMP_MIBObject *getNext() { return m_pNext; }
341 SNMP_MIBObject *getPrev() { return m_pPrev; }
342 SNMP_MIBObject *getFirstChild() { return m_pFirst; }
343 SNMP_MIBObject *getLastChild() { return m_pLast; }
d454fb4c 344
967893bb 345 UINT32 getObjectId() { return m_dwOID; }
6308c9be
VK
346 const TCHAR *getName() { return m_pszName; }
347 const TCHAR *getDescription() { return m_pszDescription; }
348 int getType() { return m_iType; }
349 int getStatus() { return m_iStatus; }
350 int getAccess() { return m_iAccess; }
34138f2d 351
967893bb 352 SNMP_MIBObject *findChildByID(UINT32 dwOID);
d454fb4c 353
6308c9be 354 void print(int nIndent);
d454fb4c 355
8e8df806 356 // File I/O, supposed to be callsed only from libnxsnmp functions
967893bb 357 void writeToFile(ZFile *pFile, UINT32 dwFlags);
6308c9be 358 BOOL readFromFile(ZFile *pFile);
d454fb4c
VK
359};
360
496390c2
VK
361/**
362 * Object identifier (OID)
363 */
927fb8a6
VK
364class LIBNXSNMP_EXPORTABLE SNMP_ObjectId
365{
366private:
9ceab287 367 size_t m_length;
e0471fad 368 UINT32 *m_value;
927fb8a6
VK
369
370public:
371 SNMP_ObjectId();
9ceab287
VK
372 SNMP_ObjectId(const SNMP_ObjectId &src);
373 SNMP_ObjectId(const UINT32 *value, size_t length);
927fb8a6
VK
374 ~SNMP_ObjectId();
375
9ceab287
VK
376 SNMP_ObjectId& operator =(const SNMP_ObjectId &src);
377
378 size_t length() const { return m_length; }
379 const UINT32 *value() const { return m_value; }
380 String toString() const;
381 TCHAR *toString(TCHAR *buffer, size_t bufferSize) const;
382 bool isValid() const { return (m_length > 0) && (m_value != NULL); }
383 bool isZeroDotZero() const { return (m_length == 2) && (m_value[0] == 0) && (m_value[1] == 0); }
384 UINT32 getElement(size_t index) const { return (index < m_length) ? m_value[index] : 0; }
6ed0d23d 385
9ceab287
VK
386 int compare(const TCHAR *oid) const;
387 int compare(const UINT32 *oid, size_t length) const;
388 int compare(const SNMP_ObjectId& oid) const;
389
390 void setValue(const UINT32 *value, size_t length);
391 void extend(UINT32 subId);
392 void extend(const UINT32 *subId, size_t length);
393 void truncate(size_t count);
394 void changeElement(size_t pos, UINT32 value) { if (pos < m_length) m_value[pos] = value; }
f34e7a6e 395
9ceab287 396 static SNMP_ObjectId parse(const TCHAR *oid);
927fb8a6
VK
397};
398
496390c2
VK
399/**
400 * SNMP variable (varbind)
401 */
927fb8a6
VK
402class LIBNXSNMP_EXPORTABLE SNMP_Variable
403{
404private:
9ceab287 405 SNMP_ObjectId m_name;
e0471fad
VK
406 UINT32 m_type;
407 size_t m_valueLength;
408 BYTE *m_value;
927fb8a6
VK
409
410public:
411 SNMP_Variable();
e0471fad 412 SNMP_Variable(const TCHAR *name);
9ceab287
VK
413 SNMP_Variable(const UINT32 *name, size_t nameLen);
414 SNMP_Variable(const SNMP_ObjectId &name);
9c5ebc32 415 SNMP_Variable(const SNMP_Variable *src);
927fb8a6 416 ~SNMP_Variable();
c5dffba0 417
1e019efa 418 bool parse(const BYTE *data, size_t varLength);
e0471fad 419 size_t encode(BYTE *buffer, size_t bufferSize);
c5dffba0 420
9ceab287 421 const SNMP_ObjectId& getName() const { return m_name; }
9c5ebc32
VK
422 UINT32 getType() const { return m_type; }
423 size_t getValueLength() const { return m_valueLength; }
424 const BYTE *getValue() const { return m_value; }
c5dffba0 425
9c5ebc32
VK
426 size_t getRawValue(BYTE *buffer, size_t bufSize) const;
427 UINT32 getValueAsUInt() const;
428 LONG getValueAsInt() const;
429 TCHAR *getValueAsString(TCHAR *buffer, size_t bufferSize) const;
430 TCHAR *getValueAsPrintableString(TCHAR *buffer, size_t bufferSize, bool *convertToHex) const;
9ceab287 431 SNMP_ObjectId getValueAsObjectId() const;
9c5ebc32
VK
432 TCHAR *getValueAsMACAddr(TCHAR *buffer) const;
433 TCHAR *getValueAsIPAddr(TCHAR *buffer) const;
e0471fad
VK
434
435 void setValueFromString(UINT32 type, const TCHAR *value);
927fb8a6
VK
436};
437
496390c2
VK
438/**
439 * SNMP engine
440 */
e0d4b618
VK
441class LIBNXSNMP_EXPORTABLE SNMP_Engine
442{
443private:
444 BYTE m_id[SNMP_MAX_ENGINEID_LEN];
e0471fad 445 size_t m_idLen;
e0d4b618
VK
446 int m_engineBoots;
447 int m_engineTime;
448
449public:
450 SNMP_Engine();
e0471fad 451 SNMP_Engine(BYTE *id, size_t idLen, int engineBoots = 0, int engineTime = 0);
aa379fa0 452 SNMP_Engine(const SNMP_Engine *src);
e0d4b618
VK
453 ~SNMP_Engine();
454
aa379fa0
VK
455 const BYTE *getId() const { return m_id; }
456 size_t getIdLen() const { return m_idLen; }
457 int getBoots() const { return m_engineBoots; }
458 int getTime() const { return m_engineTime; }
45e6c713
VK
459
460 void setBoots(int boots) { m_engineBoots = boots; }
461 void setTime(int engineTime) { m_engineTime = engineTime; }
e0d4b618
VK
462};
463
072e4643
VK
464/**
465 * Security context
466 */
c4366266
VK
467class LIBNXSNMP_EXPORTABLE SNMP_SecurityContext
468{
469private:
470 int m_securityModel;
e0d4b618 471 char *m_authName; // community for V1/V2c, user for V3 USM
c4366266 472 char *m_authPassword;
4b7d8903 473 char *m_privPassword;
072e4643 474 char *m_contextName;
e0d4b618
VK
475 BYTE m_authKeyMD5[16];
476 BYTE m_authKeySHA1[20];
6845efbd 477 BYTE m_privKey[20];
e0d4b618
VK
478 SNMP_Engine m_authoritativeEngine;
479 int m_authMethod;
4b7d8903 480 int m_privMethod;
e0d4b618
VK
481
482 void recalculateKeys();
c4366266
VK
483
484public:
485 SNMP_SecurityContext();
5d2c5741 486 SNMP_SecurityContext(SNMP_SecurityContext *src);
072e4643 487 SNMP_SecurityContext(const char *community);
e0d4b618
VK
488 SNMP_SecurityContext(const char *user, const char *authPassword, int authMethod);
489 SNMP_SecurityContext(const char *user, const char *authPassword, const char *encryptionPassword,
490 int authMethod, int encryptionMethod);
c4366266
VK
491 ~SNMP_SecurityContext();
492
493 int getSecurityModel() { return m_securityModel; }
3b5e095e
VK
494 const char *getCommunity() { return CHECK_NULL_EX_A(m_authName); }
495 const char *getUser() { return CHECK_NULL_EX_A(m_authName); }
496 const char *getAuthPassword() { return CHECK_NULL_EX_A(m_authPassword); }
497 const char *getPrivPassword() { return CHECK_NULL_EX_A(m_privPassword); }
072e4643 498 const char *getContextName() { return m_contextName; }
c4366266 499
5d47b852
VK
500 bool needAuthentication() { return (m_authMethod != SNMP_AUTH_NONE) && (m_authoritativeEngine.getIdLen() != 0); }
501 bool needEncryption() { return (m_privMethod != SNMP_ENCRYPT_NONE) && (m_authoritativeEngine.getIdLen() != 0); }
4b7d8903
VK
502 int getAuthMethod() { return m_authMethod; }
503 int getPrivMethod() { return m_privMethod; }
e0d4b618
VK
504 BYTE *getAuthKeyMD5() { return m_authKeyMD5; }
505 BYTE *getAuthKeySHA1() { return m_authKeySHA1; }
6845efbd 506 BYTE *getPrivKey() { return m_privKey; }
e0d4b618 507
950b229b
VK
508 json_t *toJson() const;
509
4b7d8903
VK
510 void setAuthName(const char *name);
511 void setCommunity(const char *community) { setAuthName(community); }
512 void setUser(const char *user) { setAuthName(user); }
513 void setAuthPassword(const char *password);
514 void setPrivPassword(const char *password);
515 void setAuthMethod(int method) { m_authMethod = method; }
516 void setPrivMethod(int method) { m_privMethod = method; }
09468947 517 void setSecurityModel(int model);
072e4643
VK
518 void setContextName(const TCHAR *name);
519#ifdef UNICODE
520 void setContextNameA(const char *name);
521#else
522 void setContextNameA(const char *name) { setContextName(name); }
523#endif
c4366266 524
aa379fa0
VK
525 void setAuthoritativeEngine(const SNMP_Engine &engine);
526 const SNMP_Engine& getAuthoritativeEngine() { return m_authoritativeEngine; }
c4366266
VK
527};
528
072e4643
VK
529/**
530 * SNMP PDU
531 */
927fb8a6
VK
532class LIBNXSNMP_EXPORTABLE SNMP_PDU
533{
534private:
e0471fad
VK
535 UINT32 m_version;
536 UINT32 m_command;
537 ObjectArray<SNMP_Variable> *m_variables;
927fb8a6 538 SNMP_ObjectId *m_pEnterprise;
e0471fad
VK
539 int m_trapType;
540 int m_specificTrap;
967893bb
VK
541 UINT32 m_dwTimeStamp;
542 UINT32 m_dwAgentAddr;
543 UINT32 m_dwRqId;
544 UINT32 m_dwErrorCode;
545 UINT32 m_dwErrorIndex;
546 UINT32 m_msgId;
547 UINT32 m_msgMaxSize;
c4366266 548 BYTE m_contextEngineId[SNMP_MAX_ENGINEID_LEN];
e0471fad 549 size_t m_contextEngineIdLen;
07f6978d 550 char m_contextName[SNMP_MAX_CONTEXT_NAME];
6845efbd 551 BYTE m_salt[8];
417ab143 552 bool m_reportable;
489b117b 553
e0d4b618
VK
554 // The following attributes only used by parser and
555 // valid only for received PDUs
556 BYTE m_flags;
e38391ba 557 char *m_authObject;
e0d4b618
VK
558 SNMP_Engine m_authoritativeEngine;
559 int m_securityModel;
e38391ba 560 BYTE m_signature[12];
1e019efa
VK
561 size_t m_signatureOffset;
562
563 bool parseVariable(const BYTE *pData, size_t varLength);
564 bool parseVarBinds(const BYTE *pData, size_t pduLength);
565 bool parsePdu(const BYTE *pdu, size_t pduLength);
566 bool parseTrapPDU(const BYTE *pData, size_t pduLength);
567 bool parseTrap2PDU(const BYTE *pData, size_t pduLength);
568 bool parsePduContent(const BYTE *pData, size_t pduLength);
569 bool parseV3Header(const BYTE *pData, size_t pduLength);
570 bool parseV3SecurityUsm(const BYTE *pData, size_t pduLength, const BYTE *rawMsg);
571 bool parseV3ScopedPdu(const BYTE *pData, size_t pduLength);
572 bool validateSignedMessage(const BYTE *msg, size_t msgLen, SNMP_SecurityContext *securityContext);
e0471fad
VK
573 size_t encodeV3Header(BYTE *buffer, size_t bufferSize, SNMP_SecurityContext *securityContext);
574 size_t encodeV3SecurityParameters(BYTE *buffer, size_t bufferSize, SNMP_SecurityContext *securityContext);
575 size_t encodeV3ScopedPDU(UINT32 pduType, BYTE *pdu, size_t pduSize, BYTE *buffer, size_t bufferSize);
576 void signMessage(BYTE *msg, size_t msgLen, SNMP_SecurityContext *securityContext);
1e019efa 577 bool decryptData(const BYTE *data, size_t length, BYTE *decryptedData, SNMP_SecurityContext *securityContext);
927fb8a6
VK
578
579public:
580 SNMP_PDU();
967893bb 581 SNMP_PDU(UINT32 dwCommand, UINT32 dwRqId, UINT32 dwVersion = SNMP_VERSION_2C);
e0471fad 582 SNMP_PDU(SNMP_PDU *src);
927fb8a6
VK
583 ~SNMP_PDU();
584
1e019efa 585 bool parse(const BYTE *rawData, size_t rawLength, SNMP_SecurityContext *securityContext, bool engineIdAutoupdate);
e0471fad 586 size_t encode(BYTE **ppBuffer, SNMP_SecurityContext *securityContext);
c4366266 587
e0471fad 588 UINT32 getCommand() { return m_command; }
417ab143 589 SNMP_ObjectId *getTrapId() { return m_pEnterprise; }
e0471fad
VK
590 int getTrapType() { return m_trapType; }
591 int getSpecificTrapType() { return m_specificTrap; }
592 int getNumVariables() { return m_variables->size(); }
593 SNMP_Variable *getVariable(int index) { return m_variables->get(index); }
594 UINT32 getVersion() { return m_version; }
967893bb 595 UINT32 getErrorCode() { return m_dwErrorCode; }
417ab143 596
967893bb
VK
597 void setMessageId(UINT32 msgId) { m_msgId = msgId; }
598 UINT32 getMessageId() { return m_msgId; }
e0d4b618 599
417ab143
VK
600 bool isReportable() { return m_reportable; }
601 void setReportable(bool value) { m_reportable = value; }
602
e38391ba
VK
603 const char *getCommunity() { return (m_authObject != NULL) ? m_authObject : ""; }
604 const char *getUser() { return (m_authObject != NULL) ? m_authObject : ""; }
e0d4b618
VK
605 SNMP_Engine& getAuthoritativeEngine() { return m_authoritativeEngine; }
606 int getSecurityModel() { return m_securityModel; }
607 int getFlags() { return (int)m_flags; }
c4366266 608
967893bb
VK
609 UINT32 getRequestId() { return m_dwRqId; }
610 void setRequestId(UINT32 dwId) { m_dwRqId = dwId; }
c4366266 611
aa379fa0 612 void setContextEngineId(const BYTE *id, size_t len);
c4366266
VK
613 void setContextEngineId(const char *id);
614 void setContextName(const char *name);
615 const char *getContextName() { return m_contextName; }
e0471fad 616 size_t getContextEngineIdLength() { return m_contextEngineIdLen; }
c4366266
VK
617 BYTE *getContextEngineId() { return m_contextEngineId; }
618
6fbbf94d 619 void unlinkVariables() { m_variables->setOwner(false); m_variables->clear(); m_variables->setOwner(true); }
c4366266 620 void bindVariable(SNMP_Variable *pVar);
927fb8a6
VK
621};
622
7aad6641
VK
623/**
624 * Generic SNMP transport
625 */
6bb44a2b
VK
626class LIBNXSNMP_EXPORTABLE SNMP_Transport
627{
c4366266 628protected:
e0d4b618 629 SNMP_SecurityContext *m_securityContext;
c4366266 630 SNMP_Engine *m_authoritativeEngine;
8c7a1c75 631 SNMP_Engine *m_contextEngine;
cd9f247e 632 bool m_enableEngineIdAutoupdate;
417ab143 633 bool m_updatePeerOnRecv;
014fbefc 634 bool m_reliable;
5ad2167d 635 int m_snmpVersion;
c4366266 636
48d28327 637public:
c4366266
VK
638 SNMP_Transport();
639 virtual ~SNMP_Transport();
48d28327 640
967893bb 641 virtual int readMessage(SNMP_PDU **data, UINT32 timeout = INFINITE,
cd9f247e
VK
642 struct sockaddr *sender = NULL, socklen_t *addrSize = NULL,
643 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL)
48d28327
VK
644 {
645 return -1;
646 }
c4366266 647 virtual int sendMessage(SNMP_PDU *pdu)
48d28327
VK
648 {
649 return -1;
650 }
c75e9ee4 651 virtual InetAddress getPeerIpAddress()
596dc3aa 652 {
c75e9ee4 653 return InetAddress();
596dc3aa 654 }
48d28327 655
d0a2ada6 656 virtual WORD getPort()=0;
657
c75e9ee4 658 UINT32 doRequest(SNMP_PDU *request, SNMP_PDU **response, UINT32 timeout = INFINITE, int numRetries = 1);
e0d4b618
VK
659
660 void setSecurityContext(SNMP_SecurityContext *ctx);
417ab143 661 SNMP_SecurityContext *getSecurityContext() { return m_securityContext; }
5d2c5741 662 const char *getCommunityString() { return (m_securityContext != NULL) ? m_securityContext->getCommunity() : ""; }
aa379fa0 663 const SNMP_Engine *getAuthoritativeEngine() { return m_authoritativeEngine; }
cd9f247e
VK
664
665 void enableEngineIdAutoupdate(bool enabled) { m_enableEngineIdAutoupdate = enabled; }
666 bool isEngineIdAutoupdateEnabled() { return m_enableEngineIdAutoupdate; }
5ad2167d 667
417ab143
VK
668 void setPeerUpdatedOnRecv(bool enabled) { m_updatePeerOnRecv = enabled; }
669 bool isPeerUpdatedOnRecv() { return m_updatePeerOnRecv; }
670
5ad2167d
VK
671 void setSnmpVersion(int version) { m_snmpVersion = version; }
672 int getSnmpVersion() { return m_snmpVersion; }
48d28327
VK
673};
674
7aad6641
VK
675/**
676 * UDP SNMP transport
677 */
48d28327
VK
678class LIBNXSNMP_EXPORTABLE SNMP_UDPTransport : public SNMP_Transport
679{
489b117b 680protected:
6bb44a2b 681 SOCKET m_hSocket;
c75e9ee4 682 SockAddrBuffer m_peerAddr;
452ea3e4 683 bool m_connected;
e0471fad
VK
684 size_t m_dwBufferSize;
685 size_t m_dwBytesInBuffer;
686 size_t m_dwBufferPos;
6bb44a2b 687 BYTE *m_pBuffer;
06c6f3ff 688 UINT16 m_port;
6bb44a2b 689
e0471fad 690 size_t preParsePDU();
967893bb 691 int recvData(UINT32 dwTimeout, struct sockaddr *pSender, socklen_t *piAddrSize);
5a7d6a10 692 void clearBuffer();
6bb44a2b
VK
693
694public:
48d28327
VK
695 SNMP_UDPTransport();
696 SNMP_UDPTransport(SOCKET hSocket);
697 virtual ~SNMP_UDPTransport();
6bb44a2b 698
967893bb 699 virtual int readMessage(SNMP_PDU **data, UINT32 timeout = INFINITE,
cd9f247e
VK
700 struct sockaddr *sender = NULL, socklen_t *addrSize = NULL,
701 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
c4366266 702 virtual int sendMessage(SNMP_PDU *pPDU);
c75e9ee4 703 virtual InetAddress getPeerIpAddress();
ddbdfb71 704
06c6f3ff
VK
705 UINT32 createUDPTransport(const TCHAR *hostName, UINT16 port = SNMP_DEFAULT_PORT);
706 UINT32 createUDPTransport(const InetAddress& hostAddr, UINT16 port = SNMP_DEFAULT_PORT);
452ea3e4 707 bool isConnected() { return m_connected; }
06c6f3ff 708 UINT16 getPort() { return m_port; }
6bb44a2b
VK
709};
710
9c5ebc32
VK
711struct SNMP_SnapshotIndexEntry;
712
713/**
714 * SNMP snapshot
715 */
716class LIBNXSNMP_EXPORTABLE SNMP_Snapshot
717{
718private:
719 ObjectArray<SNMP_Variable> *m_values;
720 SNMP_SnapshotIndexEntry *m_index;
721
722 static UINT32 callback(SNMP_Variable *var, SNMP_Transport *transport, void *arg);
723
724 void buildIndex();
725 SNMP_SnapshotIndexEntry *find(const UINT32 *oid, size_t oidLen) const;
726 SNMP_SnapshotIndexEntry *find(const TCHAR *oid) const;
d5566541 727 SNMP_SnapshotIndexEntry *find(const SNMP_ObjectId& oid) const;
9c5ebc32
VK
728
729public:
730 SNMP_Snapshot();
731 virtual ~SNMP_Snapshot();
732
733 static SNMP_Snapshot *create(SNMP_Transport *transport, const TCHAR *baseOid);
734 static SNMP_Snapshot *create(SNMP_Transport *transport, const UINT32 *baseOid, size_t oidLen);
735
736 Iterator<SNMP_Variable> *iterator() { return m_values->iterator(); }
737 EnumerationCallbackResult walk(const TCHAR *baseOid, EnumerationCallbackResult (*handler)(const SNMP_Variable *, const SNMP_Snapshot *, void *), void *userArg) const;
738 EnumerationCallbackResult walk(const UINT32 *baseOid, size_t baseOidLen, EnumerationCallbackResult (*handler)(const SNMP_Variable *, const SNMP_Snapshot *, void *), void *userArg) const;
739
d596c152
VK
740 const SNMP_Variable *get(const TCHAR *oid) const;
741 const SNMP_Variable *get(const SNMP_ObjectId& oid) const;
742 const SNMP_Variable *get(const UINT32 *oid, size_t oidLen) const;
743 const SNMP_Variable *getNext(const TCHAR *oid) const;
744 const SNMP_Variable *getNext(const SNMP_ObjectId& oid) const;
745 const SNMP_Variable *getNext(const UINT32 *oid, size_t oidLen) const;
746 const SNMP_Variable *first() const { return m_values->get(0); }
747 const SNMP_Variable *last() const { return m_values->get(m_values->size() - 1); }
748
749 UINT32 getAsInt32(const TCHAR *oid) const { const SNMP_Variable *v = get(oid); return (v != NULL) ? v->getValueAsInt() : 0; }
750 UINT32 getAsInt32(const SNMP_ObjectId& oid) const { const SNMP_Variable *v = get(oid); return (v != NULL) ? v->getValueAsInt() : 0; }
751 UINT32 getAsInt32(const UINT32 *oid, size_t oidLen) const { const SNMP_Variable *v = get(oid, oidLen); return (v != NULL) ? v->getValueAsInt() : 0; }
752 UINT32 getAsUInt32(const TCHAR *oid) const { const SNMP_Variable *v = get(oid); return (v != NULL) ? v->getValueAsUInt() : 0; }
753 UINT32 getAsUInt32(const SNMP_ObjectId& oid) const { const SNMP_Variable *v = get(oid); return (v != NULL) ? v->getValueAsUInt() : 0; }
754 UINT32 getAsUInt32(const UINT32 *oid, size_t oidLen) const { const SNMP_Variable *v = get(oid, oidLen); return (v != NULL) ? v->getValueAsUInt() : 0; }
755 TCHAR *getAsString(const TCHAR *oid, TCHAR *buffer, size_t bufferSize) const { const SNMP_Variable *v = get(oid); if (v != NULL) { v->getValueAsString(buffer, bufferSize); return buffer; } return NULL; }
756 TCHAR *getAsString(const SNMP_ObjectId& oid, TCHAR *buffer, size_t bufferSize) const { const SNMP_Variable *v = get(oid); if (v != NULL) { v->getValueAsString(buffer, bufferSize); return buffer; } return NULL; }
757 TCHAR *getAsString(const UINT32 *oid, size_t oidLen, TCHAR *buffer, size_t bufferSize) const { const SNMP_Variable *v = get(oid, oidLen); if (v != NULL) { v->getValueAsString(buffer, bufferSize); return buffer; } return NULL; }
d5566541 758
9c5ebc32
VK
759 int size() const { return m_values->size(); }
760 bool isEmpty() const { return m_values->size() == 0; }
761};
762
496390c2
VK
763/**
764 * Functions
765 */
e0471fad
VK
766TCHAR LIBNXSNMP_EXPORTABLE *SNMPConvertOIDToText(size_t length, const UINT32 *value, TCHAR *buffer, size_t bufferSize);
767size_t LIBNXSNMP_EXPORTABLE SNMPParseOID(const TCHAR *text, UINT32 *buffer, size_t bufferSize);
db117859 768bool LIBNXSNMP_EXPORTABLE SNMPIsCorrectOID(const TCHAR *oid);
e0471fad 769size_t LIBNXSNMP_EXPORTABLE SNMPGetOIDLength(const TCHAR *oid);
967893bb 770const TCHAR LIBNXSNMP_EXPORTABLE *SNMPGetErrorText(UINT32 dwError);
e0471fad
VK
771UINT32 LIBNXSNMP_EXPORTABLE SNMPSaveMIBTree(const TCHAR *fileName, SNMP_MIBObject *pRoot, UINT32 dwFlags);
772UINT32 LIBNXSNMP_EXPORTABLE SNMPLoadMIBTree(const TCHAR *fileName, SNMP_MIBObject **ppRoot);
773UINT32 LIBNXSNMP_EXPORTABLE SNMPGetMIBTreeTimestamp(const TCHAR *fileName, UINT32 *pdwTimestamp);
967893bb
VK
774UINT32 LIBNXSNMP_EXPORTABLE SNMPResolveDataType(const TCHAR *pszType);
775TCHAR LIBNXSNMP_EXPORTABLE *SNMPDataTypeName(UINT32 type, TCHAR *buffer, size_t bufferSize);
b02fb9bd 776
296ae03d
VK
777UINT32 LIBNXSNMP_EXPORTABLE SnmpNewRequestId();
778void LIBNXSNMP_EXPORTABLE SnmpSetMessageIds(DWORD msgParseError, DWORD msgTypeError, DWORD msgGetError);
779void LIBNXSNMP_EXPORTABLE SnmpSetDefaultTimeout(UINT32 timeout);
780UINT32 LIBNXSNMP_EXPORTABLE SnmpGetDefaultTimeout();
781UINT32 LIBNXSNMP_EXPORTABLE SnmpGet(int version, SNMP_Transport *transport,
782 const TCHAR *szOidStr, const UINT32 *oidBinary, size_t oidLen, void *pValue,
783 size_t bufferSize, UINT32 dwFlags);
784UINT32 LIBNXSNMP_EXPORTABLE SnmpGetEx(SNMP_Transport *pTransport,
785 const TCHAR *szOidStr, const UINT32 *oidBinary, size_t oidLen, void *pValue,
786 size_t bufferSize, UINT32 dwFlags, UINT32 *dataLen);
9c5ebc32
VK
787UINT32 LIBNXSNMP_EXPORTABLE SnmpWalk(SNMP_Transport *transport, const TCHAR *rootOid,
788 UINT32 (* handler)(SNMP_Variable *, SNMP_Transport *, void *),
789 void *userArg, bool logErrors = false);
790UINT32 LIBNXSNMP_EXPORTABLE SnmpWalk(SNMP_Transport *transport, const UINT32 *rootOid, size_t rootOidLen,
791 UINT32 (* handler)(SNMP_Variable *, SNMP_Transport *, void *),
792 void *userArg, bool logErrors = false);
296ae03d 793
34138f2d 794#endif /* __cplusplus */
b02fb9bd 795
1a4de45b
AK
796/** @} */
797
28c6afbc 798#endif