change log updated
[public/netxms.git] / include / nxsnmp.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2013 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
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
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 **
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: nxsnmp.h
20 **
21 **/
22
23 #ifndef _nxsnmp_h_
24 #define _nxsnmp_h_
25
26 #ifdef __cplusplus
27 #include <nms_common.h>
28 #include <nms_threads.h>
29 #endif
30
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
41 /** \ingroup SNMP
42 * @{
43 */
44
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
53 #define SNMP_DEFAULT_PORT 161
54
55 #else
56
57
58 //
59 // Various constants
60 //
61
62 #define MAX_OID_LEN ((size_t)128)
63 #define MAX_MIB_OBJECT_NAME ((size_t)64)
64 #define SNMP_DEFAULT_PORT 161
65 #define SNMP_MAX_CONTEXT_NAME ((size_t)256)
66 #define SNMP_MAX_ENGINEID_LEN ((size_t)256)
67 #define SNMP_DEFAULT_MSG_MAX_SIZE ((size_t)65536)
68
69
70 //
71 // OID comparision results
72 //
73
74 #define OID_ERROR -1
75 #define OID_EQUAL 0
76 #define OID_PRECEDING 1
77 #define OID_FOLLOWING 2
78 #define OID_SHORTER 3
79 #define OID_LONGER 4
80
81 /**
82 * libnxsnmp error codes
83 */
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 */
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 */
105
106
107 //
108 // MIB parser error codes
109 //
110
111 #define SNMP_MPE_SUCCESS 0
112 #define SNMP_MPE_PARSE_ERROR 1
113
114
115 //
116 // SNMP versions
117 //
118
119 #define SNMP_VERSION_1 0
120 #define SNMP_VERSION_2C 1
121 #define SNMP_VERSION_3 3
122
123
124 //
125 // PDU types
126 //
127
128 #define SNMP_INVALID_PDU 255
129 #define SNMP_GET_REQUEST 0
130 #define SNMP_GET_NEXT_REQUEST 1
131 #define SNMP_RESPONSE 2
132 #define SNMP_SET_REQUEST 3
133 #define SNMP_TRAP 4
134 #define SNMP_GET_BULK_REQUEST 5
135 #define SNMP_INFORM_REQUEST 6
136 #define SNMP_REPORT 8
137
138
139 //
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 //
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
169 #define ASN_NO_SUCH_OBJECT 0x80
170 #define ASN_NO_SUCH_INSTANCE 0x81
171 #define ASN_GET_REQUEST_PDU 0xA0
172 #define ASN_GET_NEXT_REQUEST_PDU 0xA1
173 #define ASN_RESPONSE_PDU 0xA2
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
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
189
190
191 //
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 //
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 //
219 // MIB object access types
220 //
221
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
228
229
230 //
231 // MIB object status codes
232 //
233
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
280
281 /**
282 * Flags for SNMPSaveMIBTree
283 */
284 #define SMT_COMPRESS_DATA 0x01
285 #define SMT_SKIP_DESCRIPTIONS 0x02
286
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
295
296 #endif /* NXSNMP_WITH_NET_SNMP */
297
298
299 #ifdef __cplusplus
300
301 //
302 // MIB tree node
303 //
304
305 class ZFile;
306
307 class LIBNXSNMP_EXPORTABLE SNMP_MIBObject
308 {
309 private:
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
316 UINT32 m_dwOID;
317 TCHAR *m_pszName;
318 TCHAR *m_pszDescription;
319 TCHAR *m_pszTextualConvention;
320 int m_iType;
321 int m_iStatus;
322 int m_iAccess;
323
324 void Initialize();
325
326 public:
327 SNMP_MIBObject();
328 SNMP_MIBObject(UINT32 dwOID, const TCHAR *pszName);
329 SNMP_MIBObject(UINT32 dwOID, const TCHAR *pszName, int iType,
330 int iStatus, int iAccess, const TCHAR *pszDescription,
331 const TCHAR *pszTextualConvention);
332 ~SNMP_MIBObject();
333
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); }
338
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; }
344
345 UINT32 getObjectId() { return m_dwOID; }
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; }
351
352 SNMP_MIBObject *findChildByID(UINT32 dwOID);
353
354 void print(int nIndent);
355
356 // File I/O, supposed to be callsed only from libnxsnmp functions
357 void writeToFile(ZFile *pFile, UINT32 dwFlags);
358 BOOL readFromFile(ZFile *pFile);
359 };
360
361 /**
362 * Object identifier (OID)
363 */
364 class LIBNXSNMP_EXPORTABLE SNMP_ObjectId
365 {
366 private:
367 size_t m_length;
368 UINT32 *m_value;
369
370 public:
371 SNMP_ObjectId();
372 SNMP_ObjectId(const SNMP_ObjectId &src);
373 SNMP_ObjectId(const UINT32 *value, size_t length);
374 ~SNMP_ObjectId();
375
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; }
385
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; }
395
396 static SNMP_ObjectId parse(const TCHAR *oid);
397 };
398
399 /**
400 * SNMP variable (varbind)
401 */
402 class LIBNXSNMP_EXPORTABLE SNMP_Variable
403 {
404 private:
405 SNMP_ObjectId m_name;
406 UINT32 m_type;
407 size_t m_valueLength;
408 BYTE *m_value;
409
410 public:
411 SNMP_Variable();
412 SNMP_Variable(const TCHAR *name);
413 SNMP_Variable(const UINT32 *name, size_t nameLen);
414 SNMP_Variable(const SNMP_ObjectId &name);
415 SNMP_Variable(const SNMP_Variable *src);
416 ~SNMP_Variable();
417
418 bool parse(const BYTE *data, size_t varLength);
419 size_t encode(BYTE *buffer, size_t bufferSize);
420
421 const SNMP_ObjectId& getName() const { return m_name; }
422 UINT32 getType() const { return m_type; }
423 size_t getValueLength() const { return m_valueLength; }
424 const BYTE *getValue() const { return m_value; }
425
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;
431 SNMP_ObjectId getValueAsObjectId() const;
432 TCHAR *getValueAsMACAddr(TCHAR *buffer) const;
433 TCHAR *getValueAsIPAddr(TCHAR *buffer) const;
434
435 void setValueFromString(UINT32 type, const TCHAR *value);
436 };
437
438 /**
439 * SNMP engine
440 */
441 class LIBNXSNMP_EXPORTABLE SNMP_Engine
442 {
443 private:
444 BYTE m_id[SNMP_MAX_ENGINEID_LEN];
445 size_t m_idLen;
446 int m_engineBoots;
447 int m_engineTime;
448
449 public:
450 SNMP_Engine();
451 SNMP_Engine(BYTE *id, size_t idLen, int engineBoots = 0, int engineTime = 0);
452 SNMP_Engine(const SNMP_Engine *src);
453 ~SNMP_Engine();
454
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; }
459
460 void setBoots(int boots) { m_engineBoots = boots; }
461 void setTime(int engineTime) { m_engineTime = engineTime; }
462 };
463
464 /**
465 * Security context
466 */
467 class LIBNXSNMP_EXPORTABLE SNMP_SecurityContext
468 {
469 private:
470 int m_securityModel;
471 char *m_authName; // community for V1/V2c, user for V3 USM
472 char *m_authPassword;
473 char *m_privPassword;
474 char *m_contextName;
475 BYTE m_authKeyMD5[16];
476 BYTE m_authKeySHA1[20];
477 BYTE m_privKey[20];
478 SNMP_Engine m_authoritativeEngine;
479 int m_authMethod;
480 int m_privMethod;
481
482 void recalculateKeys();
483
484 public:
485 SNMP_SecurityContext();
486 SNMP_SecurityContext(SNMP_SecurityContext *src);
487 SNMP_SecurityContext(const char *community);
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);
491 ~SNMP_SecurityContext();
492
493 int getSecurityModel() { return m_securityModel; }
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); }
498 const char *getContextName() { return m_contextName; }
499
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); }
502 int getAuthMethod() { return m_authMethod; }
503 int getPrivMethod() { return m_privMethod; }
504 BYTE *getAuthKeyMD5() { return m_authKeyMD5; }
505 BYTE *getAuthKeySHA1() { return m_authKeySHA1; }
506 BYTE *getPrivKey() { return m_privKey; }
507
508 json_t *toJson() const;
509
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; }
517 void setSecurityModel(int model);
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
524
525 void setAuthoritativeEngine(const SNMP_Engine &engine);
526 const SNMP_Engine& getAuthoritativeEngine() { return m_authoritativeEngine; }
527 };
528
529 /**
530 * SNMP PDU
531 */
532 class LIBNXSNMP_EXPORTABLE SNMP_PDU
533 {
534 private:
535 UINT32 m_version;
536 UINT32 m_command;
537 ObjectArray<SNMP_Variable> *m_variables;
538 SNMP_ObjectId *m_pEnterprise;
539 int m_trapType;
540 int m_specificTrap;
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;
548 BYTE m_contextEngineId[SNMP_MAX_ENGINEID_LEN];
549 size_t m_contextEngineIdLen;
550 char m_contextName[SNMP_MAX_CONTEXT_NAME];
551 BYTE m_salt[8];
552 bool m_reportable;
553
554 // The following attributes only used by parser and
555 // valid only for received PDUs
556 BYTE m_flags;
557 char *m_authObject;
558 SNMP_Engine m_authoritativeEngine;
559 int m_securityModel;
560 BYTE m_signature[12];
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);
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);
577 bool decryptData(const BYTE *data, size_t length, BYTE *decryptedData, SNMP_SecurityContext *securityContext);
578
579 public:
580 SNMP_PDU();
581 SNMP_PDU(UINT32 dwCommand, UINT32 dwRqId, UINT32 dwVersion = SNMP_VERSION_2C);
582 SNMP_PDU(SNMP_PDU *src);
583 ~SNMP_PDU();
584
585 bool parse(const BYTE *rawData, size_t rawLength, SNMP_SecurityContext *securityContext, bool engineIdAutoupdate);
586 size_t encode(BYTE **ppBuffer, SNMP_SecurityContext *securityContext);
587
588 UINT32 getCommand() { return m_command; }
589 SNMP_ObjectId *getTrapId() { return m_pEnterprise; }
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; }
595 UINT32 getErrorCode() { return m_dwErrorCode; }
596
597 void setMessageId(UINT32 msgId) { m_msgId = msgId; }
598 UINT32 getMessageId() { return m_msgId; }
599
600 bool isReportable() { return m_reportable; }
601 void setReportable(bool value) { m_reportable = value; }
602
603 const char *getCommunity() { return (m_authObject != NULL) ? m_authObject : ""; }
604 const char *getUser() { return (m_authObject != NULL) ? m_authObject : ""; }
605 SNMP_Engine& getAuthoritativeEngine() { return m_authoritativeEngine; }
606 int getSecurityModel() { return m_securityModel; }
607 int getFlags() { return (int)m_flags; }
608
609 UINT32 getRequestId() { return m_dwRqId; }
610 void setRequestId(UINT32 dwId) { m_dwRqId = dwId; }
611
612 void setContextEngineId(const BYTE *id, size_t len);
613 void setContextEngineId(const char *id);
614 void setContextName(const char *name);
615 const char *getContextName() { return m_contextName; }
616 size_t getContextEngineIdLength() { return m_contextEngineIdLen; }
617 BYTE *getContextEngineId() { return m_contextEngineId; }
618
619 void unlinkVariables() { m_variables->setOwner(false); m_variables->clear(); m_variables->setOwner(true); }
620 void bindVariable(SNMP_Variable *pVar);
621 };
622
623 /**
624 * Generic SNMP transport
625 */
626 class LIBNXSNMP_EXPORTABLE SNMP_Transport
627 {
628 protected:
629 SNMP_SecurityContext *m_securityContext;
630 SNMP_Engine *m_authoritativeEngine;
631 SNMP_Engine *m_contextEngine;
632 bool m_enableEngineIdAutoupdate;
633 bool m_updatePeerOnRecv;
634 bool m_reliable;
635 int m_snmpVersion;
636
637 public:
638 SNMP_Transport();
639 virtual ~SNMP_Transport();
640
641 virtual int readMessage(SNMP_PDU **data, UINT32 timeout = INFINITE,
642 struct sockaddr *sender = NULL, socklen_t *addrSize = NULL,
643 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL) = 0;
644 virtual int sendMessage(SNMP_PDU *pdu) = 0;
645 virtual InetAddress getPeerIpAddress() = 0;
646 virtual UINT16 getPort() = 0;
647 virtual bool isProxyTransport() = 0;
648
649 UINT32 doRequest(SNMP_PDU *request, SNMP_PDU **response, UINT32 timeout = INFINITE, int numRetries = 1);
650
651 void setSecurityContext(SNMP_SecurityContext *ctx);
652 SNMP_SecurityContext *getSecurityContext() { return m_securityContext; }
653 const char *getCommunityString() { return (m_securityContext != NULL) ? m_securityContext->getCommunity() : ""; }
654 const SNMP_Engine *getAuthoritativeEngine() { return m_authoritativeEngine; }
655
656 void enableEngineIdAutoupdate(bool enabled) { m_enableEngineIdAutoupdate = enabled; }
657 bool isEngineIdAutoupdateEnabled() { return m_enableEngineIdAutoupdate; }
658
659 void setPeerUpdatedOnRecv(bool enabled) { m_updatePeerOnRecv = enabled; }
660 bool isPeerUpdatedOnRecv() { return m_updatePeerOnRecv; }
661
662 void setSnmpVersion(int version) { m_snmpVersion = version; }
663 int getSnmpVersion() { return m_snmpVersion; }
664 };
665
666 /**
667 * UDP SNMP transport
668 */
669 class LIBNXSNMP_EXPORTABLE SNMP_UDPTransport : public SNMP_Transport
670 {
671 protected:
672 SOCKET m_hSocket;
673 SockAddrBuffer m_peerAddr;
674 bool m_connected;
675 size_t m_dwBufferSize;
676 size_t m_dwBytesInBuffer;
677 size_t m_dwBufferPos;
678 BYTE *m_pBuffer;
679 UINT16 m_port;
680
681 size_t preParsePDU();
682 int recvData(UINT32 dwTimeout, struct sockaddr *pSender, socklen_t *piAddrSize);
683 void clearBuffer();
684
685 public:
686 SNMP_UDPTransport();
687 SNMP_UDPTransport(SOCKET hSocket);
688 virtual ~SNMP_UDPTransport();
689
690 virtual int readMessage(SNMP_PDU **data, UINT32 timeout = INFINITE,
691 struct sockaddr *sender = NULL, socklen_t *addrSize = NULL,
692 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
693 virtual int sendMessage(SNMP_PDU *pPDU);
694 virtual InetAddress getPeerIpAddress();
695 virtual UINT16 getPort();
696 virtual bool isProxyTransport();
697
698 UINT32 createUDPTransport(const TCHAR *hostName, UINT16 port = SNMP_DEFAULT_PORT);
699 UINT32 createUDPTransport(const InetAddress& hostAddr, UINT16 port = SNMP_DEFAULT_PORT);
700 bool isConnected() { return m_connected; }
701 };
702
703 struct SNMP_SnapshotIndexEntry;
704
705 /**
706 * SNMP snapshot
707 */
708 class LIBNXSNMP_EXPORTABLE SNMP_Snapshot
709 {
710 private:
711 ObjectArray<SNMP_Variable> *m_values;
712 SNMP_SnapshotIndexEntry *m_index;
713
714 static UINT32 callback(SNMP_Variable *var, SNMP_Transport *transport, void *arg);
715
716 void buildIndex();
717 SNMP_SnapshotIndexEntry *find(const UINT32 *oid, size_t oidLen) const;
718 SNMP_SnapshotIndexEntry *find(const TCHAR *oid) const;
719 SNMP_SnapshotIndexEntry *find(const SNMP_ObjectId& oid) const;
720
721 public:
722 SNMP_Snapshot();
723 virtual ~SNMP_Snapshot();
724
725 static SNMP_Snapshot *create(SNMP_Transport *transport, const TCHAR *baseOid);
726 static SNMP_Snapshot *create(SNMP_Transport *transport, const UINT32 *baseOid, size_t oidLen);
727
728 Iterator<SNMP_Variable> *iterator() { return m_values->iterator(); }
729 EnumerationCallbackResult walk(const TCHAR *baseOid, EnumerationCallbackResult (*handler)(const SNMP_Variable *, const SNMP_Snapshot *, void *), void *userArg) const;
730 EnumerationCallbackResult walk(const UINT32 *baseOid, size_t baseOidLen, EnumerationCallbackResult (*handler)(const SNMP_Variable *, const SNMP_Snapshot *, void *), void *userArg) const;
731
732 const SNMP_Variable *get(const TCHAR *oid) const;
733 const SNMP_Variable *get(const SNMP_ObjectId& oid) const;
734 const SNMP_Variable *get(const UINT32 *oid, size_t oidLen) const;
735 const SNMP_Variable *getNext(const TCHAR *oid) const;
736 const SNMP_Variable *getNext(const SNMP_ObjectId& oid) const;
737 const SNMP_Variable *getNext(const UINT32 *oid, size_t oidLen) const;
738 const SNMP_Variable *first() const { return m_values->get(0); }
739 const SNMP_Variable *last() const { return m_values->get(m_values->size() - 1); }
740
741 UINT32 getAsInt32(const TCHAR *oid) const { const SNMP_Variable *v = get(oid); return (v != NULL) ? v->getValueAsInt() : 0; }
742 UINT32 getAsInt32(const SNMP_ObjectId& oid) const { const SNMP_Variable *v = get(oid); return (v != NULL) ? v->getValueAsInt() : 0; }
743 UINT32 getAsInt32(const UINT32 *oid, size_t oidLen) const { const SNMP_Variable *v = get(oid, oidLen); return (v != NULL) ? v->getValueAsInt() : 0; }
744 UINT32 getAsUInt32(const TCHAR *oid) const { const SNMP_Variable *v = get(oid); return (v != NULL) ? v->getValueAsUInt() : 0; }
745 UINT32 getAsUInt32(const SNMP_ObjectId& oid) const { const SNMP_Variable *v = get(oid); return (v != NULL) ? v->getValueAsUInt() : 0; }
746 UINT32 getAsUInt32(const UINT32 *oid, size_t oidLen) const { const SNMP_Variable *v = get(oid, oidLen); return (v != NULL) ? v->getValueAsUInt() : 0; }
747 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; }
748 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; }
749 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; }
750
751 int size() const { return m_values->size(); }
752 bool isEmpty() const { return m_values->size() == 0; }
753 };
754
755 /**
756 * Functions
757 */
758 TCHAR LIBNXSNMP_EXPORTABLE *SNMPConvertOIDToText(size_t length, const UINT32 *value, TCHAR *buffer, size_t bufferSize);
759 size_t LIBNXSNMP_EXPORTABLE SNMPParseOID(const TCHAR *text, UINT32 *buffer, size_t bufferSize);
760 bool LIBNXSNMP_EXPORTABLE SNMPIsCorrectOID(const TCHAR *oid);
761 size_t LIBNXSNMP_EXPORTABLE SNMPGetOIDLength(const TCHAR *oid);
762 const TCHAR LIBNXSNMP_EXPORTABLE *SNMPGetErrorText(UINT32 dwError);
763 UINT32 LIBNXSNMP_EXPORTABLE SNMPSaveMIBTree(const TCHAR *fileName, SNMP_MIBObject *pRoot, UINT32 dwFlags);
764 UINT32 LIBNXSNMP_EXPORTABLE SNMPLoadMIBTree(const TCHAR *fileName, SNMP_MIBObject **ppRoot);
765 UINT32 LIBNXSNMP_EXPORTABLE SNMPGetMIBTreeTimestamp(const TCHAR *fileName, UINT32 *pdwTimestamp);
766 UINT32 LIBNXSNMP_EXPORTABLE SNMPResolveDataType(const TCHAR *pszType);
767 TCHAR LIBNXSNMP_EXPORTABLE *SNMPDataTypeName(UINT32 type, TCHAR *buffer, size_t bufferSize);
768
769 UINT32 LIBNXSNMP_EXPORTABLE SnmpNewRequestId();
770 void LIBNXSNMP_EXPORTABLE SnmpSetMessageIds(DWORD msgParseError, DWORD msgTypeError, DWORD msgGetError);
771 void LIBNXSNMP_EXPORTABLE SnmpSetDefaultTimeout(UINT32 timeout);
772 UINT32 LIBNXSNMP_EXPORTABLE SnmpGetDefaultTimeout();
773 UINT32 LIBNXSNMP_EXPORTABLE SnmpGet(int version, SNMP_Transport *transport,
774 const TCHAR *szOidStr, const UINT32 *oidBinary, size_t oidLen, void *pValue,
775 size_t bufferSize, UINT32 dwFlags);
776 UINT32 LIBNXSNMP_EXPORTABLE SnmpGetEx(SNMP_Transport *pTransport,
777 const TCHAR *szOidStr, const UINT32 *oidBinary, size_t oidLen, void *pValue,
778 size_t bufferSize, UINT32 dwFlags, UINT32 *dataLen);
779 UINT32 LIBNXSNMP_EXPORTABLE SnmpWalk(SNMP_Transport *transport, const TCHAR *rootOid,
780 UINT32 (* handler)(SNMP_Variable *, SNMP_Transport *, void *),
781 void *userArg, bool logErrors = false);
782 UINT32 LIBNXSNMP_EXPORTABLE SnmpWalk(SNMP_Transport *transport, const UINT32 *rootOid, size_t rootOidLen,
783 UINT32 (* handler)(SNMP_Variable *, SNMP_Transport *, void *),
784 void *userArg, bool logErrors = false);
785
786 #endif /* __cplusplus */
787
788 /** @} */
789
790 #endif