implemented SNMP target sync with agent for cached DCIs; GUID functions refactoring...
[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 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
68
69
70 //
71 // OID comparision results
72 //
73
74 #define OID_ERROR -1
75 #define OID_EQUAL 0
76 #define OID_NOT_EQUAL 1
77 #define OID_SHORTER 2
78 #define OID_LONGER 3
79
80 /**
81 * libnxsnmp error codes
82 */
83 #define SNMP_ERR_SUCCESS 0 /* success */
84 #define SNMP_ERR_TIMEOUT 1 /* request timeout */
85 #define SNMP_ERR_PARAM 2 /* invalid parameters passed to function */
86 #define SNMP_ERR_SOCKET 3 /* unable to create socket */
87 #define SNMP_ERR_COMM 4 /* send/receive error */
88 #define SNMP_ERR_PARSE 5 /* error parsing PDU */
89 #define SNMP_ERR_NO_OBJECT 6 /* given object doesn't exist on agent */
90 #define SNMP_ERR_HOSTNAME 7 /* invalid hostname or IP address */
91 #define SNMP_ERR_BAD_OID 8 /* object id is incorrect */
92 #define SNMP_ERR_AGENT 9 /* agent returns an error */
93 #define SNMP_ERR_BAD_TYPE 10 /* unknown variable data type */
94 #define SNMP_ERR_FILE_IO 11 /* file I/O error */
95 #define SNMP_ERR_BAD_FILE_HEADER 12 /* file header is invalid */
96 #define SNMP_ERR_BAD_FILE_DATA 13 /* file data is invalid or corrupted */
97 #define SNMP_ERR_UNSUPP_SEC_LEVEL 14 /* unsupported security level */
98 #define SNMP_ERR_TIME_WINDOW 15 /* not in time window */
99 #define SNMP_ERR_SEC_NAME 16 /* unknown security name */
100 #define SNMP_ERR_ENGINE_ID 17 /* unknown engine ID */
101 #define SNMP_ERR_AUTH_FAILURE 18 /* authentication failure */
102 #define SNMP_ERR_DECRYPTION 19 /* decryption error */
103 #define SNMP_ERR_BAD_RESPONSE 20 /* malformed or unexpected response from agent */
104
105
106 //
107 // MIB parser error codes
108 //
109
110 #define SNMP_MPE_SUCCESS 0
111 #define SNMP_MPE_PARSE_ERROR 1
112
113
114 //
115 // SNMP versions
116 //
117
118 #define SNMP_VERSION_1 0
119 #define SNMP_VERSION_2C 1
120 #define SNMP_VERSION_3 3
121
122
123 //
124 // PDU types
125 //
126
127 #define SNMP_INVALID_PDU 255
128 #define SNMP_GET_REQUEST 0
129 #define SNMP_GET_NEXT_REQUEST 1
130 #define SNMP_RESPONSE 2
131 #define SNMP_SET_REQUEST 3
132 #define SNMP_TRAP 4
133 #define SNMP_GET_BULK_REQUEST 5
134 #define SNMP_INFORM_REQUEST 6
135 #define SNMP_REPORT 8
136
137
138 //
139 // PDU error codes
140 //
141
142 #define SNMP_PDU_ERR_SUCCESS 0
143 #define SNMP_PDU_ERR_TOO_BIG 1
144 #define SNMP_PDU_ERR_NO_SUCH_NAME 2
145 #define SNMP_PDU_ERR_BAD_VALUE 3
146 #define SNMP_PDU_ERR_READ_ONLY 4
147 #define SNMP_PDU_ERR_GENERIC 5
148
149
150 //
151 // ASN.1 identifier types
152 //
153
154 #define ASN_INTEGER 0x02
155 #define ASN_BIT_STRING 0x03
156 #define ASN_OCTET_STRING 0x04
157 #define ASN_NULL 0x05
158 #define ASN_OBJECT_ID 0x06
159 #define ASN_SEQUENCE 0x30
160 #define ASN_IP_ADDR 0x40
161 #define ASN_COUNTER32 0x41
162 #define ASN_GAUGE32 0x42
163 #define ASN_TIMETICKS 0x43
164 #define ASN_OPAQUE 0x44
165 #define ASN_NSAP_ADDR 0x45
166 #define ASN_COUNTER64 0x46
167 #define ASN_UINTEGER32 0x47
168 #define ASN_NO_SUCH_OBJECT 0x80
169 #define ASN_NO_SUCH_INSTANCE 0x81
170 #define ASN_GET_REQUEST_PDU 0xA0
171 #define ASN_GET_NEXT_REQUEST_PDU 0xA1
172 #define ASN_RESPONSE_PDU 0xA2
173 #define ASN_SET_REQUEST_PDU 0xA3
174 #define ASN_TRAP_V1_PDU 0xA4
175 #define ASN_GET_BULK_REQUEST_PDU 0xA5
176 #define ASN_INFORM_REQUEST_PDU 0xA6
177 #define ASN_TRAP_V2_PDU 0xA7
178 #define ASN_REPORT_PDU 0xA8
179
180
181 //
182 // Security models
183 //
184
185 #define SNMP_SECURITY_MODEL_V1 1
186 #define SNMP_SECURITY_MODEL_V2C 2
187 #define SNMP_SECURITY_MODEL_USM 3
188
189
190 //
191 // SNMP V3 header flags
192 //
193
194 #define SNMP_AUTH_FLAG 0x01
195 #define SNMP_PRIV_FLAG 0x02
196 #define SNMP_REPORTABLE_FLAG 0x04
197
198
199 //
200 // SNMP v3 authentication methods
201 //
202
203 #define SNMP_AUTH_NONE 0
204 #define SNMP_AUTH_MD5 1
205 #define SNMP_AUTH_SHA1 2
206
207
208 //
209 // SNMP v3 encryption methods
210 //
211
212 #define SNMP_ENCRYPT_NONE 0
213 #define SNMP_ENCRYPT_DES 1
214 #define SNMP_ENCRYPT_AES 2
215
216
217 //
218 // MIB object access types
219 //
220
221 #define MIB_ACCESS_READONLY 1
222 #define MIB_ACCESS_READWRITE 2
223 #define MIB_ACCESS_WRITEONLY 3
224 #define MIB_ACCESS_NOACCESS 4
225 #define MIB_ACCESS_NOTIFY 5
226 #define MIB_ACCESS_CREATE 6
227
228
229 //
230 // MIB object status codes
231 //
232
233 #define MIB_STATUS_MANDATORY 1
234 #define MIB_STATUS_OPTIONAL 2
235 #define MIB_STATUS_OBSOLETE 3
236 #define MIB_STATUS_DEPRECATED 4
237 #define MIB_STATUS_CURRENT 5
238
239
240 //
241 // MIB object data types
242 //
243
244 #define MIB_TYPE_OTHER 0
245 #define MIB_TYPE_IMPORT_ITEM 1
246 #define MIB_TYPE_OBJID 2
247 #define MIB_TYPE_BITSTRING 3
248 #define MIB_TYPE_INTEGER 4
249 #define MIB_TYPE_INTEGER32 5
250 #define MIB_TYPE_INTEGER64 6
251 #define MIB_TYPE_UNSIGNED32 7
252 #define MIB_TYPE_COUNTER 8
253 #define MIB_TYPE_COUNTER32 9
254 #define MIB_TYPE_COUNTER64 10
255 #define MIB_TYPE_GAUGE 11
256 #define MIB_TYPE_GAUGE32 12
257 #define MIB_TYPE_TIMETICKS 13
258 #define MIB_TYPE_OCTETSTR 14
259 #define MIB_TYPE_OPAQUE 15
260 #define MIB_TYPE_IPADDR 16
261 #define MIB_TYPE_PHYSADDR 17
262 #define MIB_TYPE_NETADDR 18
263 #define MIB_TYPE_NAMED_TYPE 19
264 #define MIB_TYPE_SEQID 20
265 #define MIB_TYPE_SEQUENCE 21
266 #define MIB_TYPE_CHOICE 22
267 #define MIB_TYPE_TEXTUAL_CONVENTION 23
268 #define MIB_TYPE_MACRO_DEFINITION 24
269 #define MIB_TYPE_MODCOMP 25
270 #define MIB_TYPE_TRAPTYPE 26
271 #define MIB_TYPE_NOTIFTYPE 27
272 #define MIB_TYPE_MODID 28
273 #define MIB_TYPE_NSAPADDRESS 29
274 #define MIB_TYPE_AGENTCAP 30
275 #define MIB_TYPE_UINTEGER 31
276 #define MIB_TYPE_NULL 32
277 #define MIB_TYPE_OBJGROUP 33
278 #define MIB_TYPE_NOTIFGROUP 34
279
280 /**
281 * Flags for SNMPSaveMIBTree
282 */
283 #define SMT_COMPRESS_DATA 0x01
284 #define SMT_SKIP_DESCRIPTIONS 0x02
285
286 /**
287 * Flags for SnmpGet
288 */
289 #define SG_VERBOSE 0x0001
290 #define SG_STRING_RESULT 0x0002
291 #define SG_RAW_RESULT 0x0004
292 #define SG_HSTRING_RESULT 0x0008
293 #define SG_PSTRING_RESULT 0x0010
294
295 #endif /* NXSNMP_WITH_NET_SNMP */
296
297
298 #ifdef __cplusplus
299
300 //
301 // MIB tree node
302 //
303
304 class ZFile;
305
306 class LIBNXSNMP_EXPORTABLE SNMP_MIBObject
307 {
308 private:
309 SNMP_MIBObject *m_pParent;
310 SNMP_MIBObject *m_pNext;
311 SNMP_MIBObject *m_pPrev;
312 SNMP_MIBObject *m_pFirst; // First child
313 SNMP_MIBObject *m_pLast; // Last child
314
315 UINT32 m_dwOID;
316 TCHAR *m_pszName;
317 TCHAR *m_pszDescription;
318 TCHAR *m_pszTextualConvention;
319 int m_iType;
320 int m_iStatus;
321 int m_iAccess;
322
323 void Initialize();
324
325 public:
326 SNMP_MIBObject();
327 SNMP_MIBObject(UINT32 dwOID, const TCHAR *pszName);
328 SNMP_MIBObject(UINT32 dwOID, const TCHAR *pszName, int iType,
329 int iStatus, int iAccess, const TCHAR *pszDescription,
330 const TCHAR *pszTextualConvention);
331 ~SNMP_MIBObject();
332
333 void setParent(SNMP_MIBObject *pObject) { m_pParent = pObject; }
334 void addChild(SNMP_MIBObject *pObject);
335 void setInfo(int iType, int iStatus, int iAccess, const TCHAR *pszDescription, const TCHAR *pszTextualConvention);
336 void setName(const TCHAR *pszName) { safe_free(m_pszName); m_pszName = _tcsdup(pszName); }
337
338 SNMP_MIBObject *getParent() { return m_pParent; }
339 SNMP_MIBObject *getNext() { return m_pNext; }
340 SNMP_MIBObject *getPrev() { return m_pPrev; }
341 SNMP_MIBObject *getFirstChild() { return m_pFirst; }
342 SNMP_MIBObject *getLastChild() { return m_pLast; }
343
344 UINT32 getObjectId() { return m_dwOID; }
345 const TCHAR *getName() { return m_pszName; }
346 const TCHAR *getDescription() { return m_pszDescription; }
347 int getType() { return m_iType; }
348 int getStatus() { return m_iStatus; }
349 int getAccess() { return m_iAccess; }
350
351 SNMP_MIBObject *findChildByID(UINT32 dwOID);
352
353 void print(int nIndent);
354
355 // File I/O, supposed to be callsed only from libnxsnmp functions
356 void writeToFile(ZFile *pFile, UINT32 dwFlags);
357 BOOL readFromFile(ZFile *pFile);
358 };
359
360 /**
361 * Object identifier (OID)
362 */
363 class LIBNXSNMP_EXPORTABLE SNMP_ObjectId
364 {
365 private:
366 UINT32 m_length;
367 UINT32 *m_value;
368 TCHAR *m_textValue;
369
370 void convertToText();
371
372 public:
373 SNMP_ObjectId();
374 SNMP_ObjectId(SNMP_ObjectId *src);
375 SNMP_ObjectId(size_t length, const UINT32 *value);
376 ~SNMP_ObjectId();
377
378 size_t getLength() { return (size_t)m_length; }
379 const UINT32 *getValue() { return m_value; }
380 const TCHAR *getValueAsText() { return CHECK_NULL(m_textValue); }
381 void setValue(UINT32 *value, size_t length);
382 void extend(UINT32 subId);
383
384 int compare(const TCHAR *oid);
385 int compare(const UINT32 *oid, size_t length);
386 int compare(SNMP_ObjectId *oid);
387
388 bool isZeroDotZero() { return (m_length == 2) && (m_value[0] == 0) && (m_value[1] == 0); }
389 };
390
391 /**
392 * SNMP variable (varbind)
393 */
394 class LIBNXSNMP_EXPORTABLE SNMP_Variable
395 {
396 private:
397 SNMP_ObjectId *m_name;
398 UINT32 m_type;
399 size_t m_valueLength;
400 BYTE *m_value;
401
402 public:
403 SNMP_Variable();
404 SNMP_Variable(const TCHAR *name);
405 SNMP_Variable(UINT32 *name, size_t nameLen);
406 SNMP_Variable(SNMP_Variable *src);
407 ~SNMP_Variable();
408
409 bool parse(BYTE *data, size_t varLength);
410 size_t encode(BYTE *buffer, size_t bufferSize);
411
412 SNMP_ObjectId *getName() { return m_name; }
413 UINT32 getType() { return m_type; }
414 size_t getValueLength() { return m_valueLength; }
415 const BYTE *getValue() { return m_value; }
416
417 size_t getRawValue(BYTE *buffer, size_t bufSize);
418 UINT32 getValueAsUInt();
419 LONG getValueAsInt();
420 TCHAR *getValueAsString(TCHAR *buffer, size_t bufferSize);
421 TCHAR *getValueAsPrintableString(TCHAR *buffer, size_t bufferSize, bool *convertToHex);
422 SNMP_ObjectId *getValueAsObjectId();
423 TCHAR *getValueAsMACAddr(TCHAR *buffer);
424 TCHAR *getValueAsIPAddr(TCHAR *buffer);
425
426 void setValueFromString(UINT32 type, const TCHAR *value);
427 };
428
429 /**
430 * SNMP engine
431 */
432 class LIBNXSNMP_EXPORTABLE SNMP_Engine
433 {
434 private:
435 BYTE m_id[SNMP_MAX_ENGINEID_LEN];
436 size_t m_idLen;
437 int m_engineBoots;
438 int m_engineTime;
439
440 public:
441 SNMP_Engine();
442 SNMP_Engine(BYTE *id, size_t idLen, int engineBoots = 0, int engineTime = 0);
443 SNMP_Engine(const SNMP_Engine *src);
444 ~SNMP_Engine();
445
446 const BYTE *getId() const { return m_id; }
447 size_t getIdLen() const { return m_idLen; }
448 int getBoots() const { return m_engineBoots; }
449 int getTime() const { return m_engineTime; }
450
451 void setBoots(int boots) { m_engineBoots = boots; }
452 void setTime(int engineTime) { m_engineTime = engineTime; }
453 };
454
455 /**
456 * Security context
457 */
458 class LIBNXSNMP_EXPORTABLE SNMP_SecurityContext
459 {
460 private:
461 int m_securityModel;
462 char *m_authName; // community for V1/V2c, user for V3 USM
463 char *m_authPassword;
464 char *m_privPassword;
465 char *m_contextName;
466 BYTE m_authKeyMD5[16];
467 BYTE m_authKeySHA1[20];
468 BYTE m_privKey[20];
469 SNMP_Engine m_authoritativeEngine;
470 int m_authMethod;
471 int m_privMethod;
472
473 void recalculateKeys();
474
475 public:
476 SNMP_SecurityContext();
477 SNMP_SecurityContext(SNMP_SecurityContext *src);
478 SNMP_SecurityContext(const char *community);
479 SNMP_SecurityContext(const char *user, const char *authPassword, int authMethod);
480 SNMP_SecurityContext(const char *user, const char *authPassword, const char *encryptionPassword,
481 int authMethod, int encryptionMethod);
482 ~SNMP_SecurityContext();
483
484 int getSecurityModel() { return m_securityModel; }
485 const char *getCommunity() { return CHECK_NULL_EX_A(m_authName); }
486 const char *getUser() { return CHECK_NULL_EX_A(m_authName); }
487 const char *getAuthPassword() { return CHECK_NULL_EX_A(m_authPassword); }
488 const char *getPrivPassword() { return CHECK_NULL_EX_A(m_privPassword); }
489 const char *getContextName() { return m_contextName; }
490
491 bool needAuthentication() { return (m_authMethod != SNMP_AUTH_NONE) && (m_authoritativeEngine.getIdLen() != 0); }
492 bool needEncryption() { return (m_privMethod != SNMP_ENCRYPT_NONE) && (m_authoritativeEngine.getIdLen() != 0); }
493 int getAuthMethod() { return m_authMethod; }
494 int getPrivMethod() { return m_privMethod; }
495 BYTE *getAuthKeyMD5() { return m_authKeyMD5; }
496 BYTE *getAuthKeySHA1() { return m_authKeySHA1; }
497 BYTE *getPrivKey() { return m_privKey; }
498
499 void setAuthName(const char *name);
500 void setCommunity(const char *community) { setAuthName(community); }
501 void setUser(const char *user) { setAuthName(user); }
502 void setAuthPassword(const char *password);
503 void setPrivPassword(const char *password);
504 void setAuthMethod(int method) { m_authMethod = method; }
505 void setPrivMethod(int method) { m_privMethod = method; }
506 void setSecurityModel(int model) { m_securityModel = model; }
507 void setContextName(const TCHAR *name);
508 #ifdef UNICODE
509 void setContextNameA(const char *name);
510 #else
511 void setContextNameA(const char *name) { setContextName(name); }
512 #endif
513
514 void setAuthoritativeEngine(const SNMP_Engine &engine);
515 const SNMP_Engine& getAuthoritativeEngine() { return m_authoritativeEngine; }
516 };
517
518 /**
519 * SNMP PDU
520 */
521 class LIBNXSNMP_EXPORTABLE SNMP_PDU
522 {
523 private:
524 UINT32 m_version;
525 UINT32 m_command;
526 ObjectArray<SNMP_Variable> *m_variables;
527 SNMP_ObjectId *m_pEnterprise;
528 int m_trapType;
529 int m_specificTrap;
530 UINT32 m_dwTimeStamp;
531 UINT32 m_dwAgentAddr;
532 UINT32 m_dwRqId;
533 UINT32 m_dwErrorCode;
534 UINT32 m_dwErrorIndex;
535 UINT32 m_msgId;
536 UINT32 m_msgMaxSize;
537 BYTE m_contextEngineId[SNMP_MAX_ENGINEID_LEN];
538 size_t m_contextEngineIdLen;
539 char m_contextName[SNMP_MAX_CONTEXT_NAME];
540 BYTE m_salt[8];
541 bool m_reportable;
542
543 // The following attributes only used by parser and
544 // valid only for received PDUs
545 BYTE m_flags;
546 char *m_authObject;
547 SNMP_Engine m_authoritativeEngine;
548 int m_securityModel;
549 BYTE m_signature[12];
550
551 bool parseVariable(BYTE *pData, size_t varLength);
552 bool parseVarBinds(BYTE *pData, size_t pduLength);
553 bool parsePdu(BYTE *pdu, size_t pduLength);
554 bool parseTrapPDU(BYTE *pData, size_t pduLength);
555 bool parseTrap2PDU(BYTE *pData, size_t pduLength);
556 bool parsePduContent(BYTE *pData, size_t pduLength);
557 bool parseV3Header(BYTE *pData, size_t pduLength);
558 bool parseV3SecurityUsm(BYTE *pData, size_t pduLength);
559 bool parseV3ScopedPdu(BYTE *pData, size_t pduLength);
560 bool validateSignedMessage(BYTE *msg, size_t msgLen, SNMP_SecurityContext *securityContext);
561 size_t encodeV3Header(BYTE *buffer, size_t bufferSize, SNMP_SecurityContext *securityContext);
562 size_t encodeV3SecurityParameters(BYTE *buffer, size_t bufferSize, SNMP_SecurityContext *securityContext);
563 size_t encodeV3ScopedPDU(UINT32 pduType, BYTE *pdu, size_t pduSize, BYTE *buffer, size_t bufferSize);
564 void signMessage(BYTE *msg, size_t msgLen, SNMP_SecurityContext *securityContext);
565 bool decryptData(BYTE *data, size_t length, BYTE *decryptedData, SNMP_SecurityContext *securityContext);
566
567 public:
568 SNMP_PDU();
569 SNMP_PDU(UINT32 dwCommand, UINT32 dwRqId, UINT32 dwVersion = SNMP_VERSION_2C);
570 SNMP_PDU(SNMP_PDU *src);
571 ~SNMP_PDU();
572
573 bool parse(BYTE *rawData, size_t rawLength, SNMP_SecurityContext *securityContext, bool engineIdAutoupdate);
574 size_t encode(BYTE **ppBuffer, SNMP_SecurityContext *securityContext);
575
576 UINT32 getCommand() { return m_command; }
577 SNMP_ObjectId *getTrapId() { return m_pEnterprise; }
578 int getTrapType() { return m_trapType; }
579 int getSpecificTrapType() { return m_specificTrap; }
580 int getNumVariables() { return m_variables->size(); }
581 SNMP_Variable *getVariable(int index) { return m_variables->get(index); }
582 UINT32 getVersion() { return m_version; }
583 UINT32 getErrorCode() { return m_dwErrorCode; }
584
585 void setMessageId(UINT32 msgId) { m_msgId = msgId; }
586 UINT32 getMessageId() { return m_msgId; }
587
588 bool isReportable() { return m_reportable; }
589 void setReportable(bool value) { m_reportable = value; }
590
591 const char *getCommunity() { return (m_authObject != NULL) ? m_authObject : ""; }
592 const char *getUser() { return (m_authObject != NULL) ? m_authObject : ""; }
593 SNMP_Engine& getAuthoritativeEngine() { return m_authoritativeEngine; }
594 int getSecurityModel() { return m_securityModel; }
595 int getFlags() { return (int)m_flags; }
596
597 UINT32 getRequestId() { return m_dwRqId; }
598 void setRequestId(UINT32 dwId) { m_dwRqId = dwId; }
599
600 void setContextEngineId(const BYTE *id, size_t len);
601 void setContextEngineId(const char *id);
602 void setContextName(const char *name);
603 const char *getContextName() { return m_contextName; }
604 size_t getContextEngineIdLength() { return m_contextEngineIdLen; }
605 BYTE *getContextEngineId() { return m_contextEngineId; }
606
607 void unlinkVariables() { m_variables->setOwner(false); m_variables->clear(); m_variables->setOwner(true); }
608 void bindVariable(SNMP_Variable *pVar);
609 };
610
611 /**
612 * Generic SNMP transport
613 */
614 class LIBNXSNMP_EXPORTABLE SNMP_Transport
615 {
616 protected:
617 SNMP_SecurityContext *m_securityContext;
618 SNMP_Engine *m_authoritativeEngine;
619 SNMP_Engine *m_contextEngine;
620 bool m_enableEngineIdAutoupdate;
621 bool m_updatePeerOnRecv;
622 int m_snmpVersion;
623
624 public:
625 SNMP_Transport();
626 virtual ~SNMP_Transport();
627
628 virtual int readMessage(SNMP_PDU **data, UINT32 timeout = INFINITE,
629 struct sockaddr *sender = NULL, socklen_t *addrSize = NULL,
630 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL)
631 {
632 return -1;
633 }
634 virtual int sendMessage(SNMP_PDU *pdu)
635 {
636 return -1;
637 }
638 virtual InetAddress getPeerIpAddress()
639 {
640 return InetAddress();
641 }
642
643 UINT32 doRequest(SNMP_PDU *request, SNMP_PDU **response, UINT32 timeout = INFINITE, int numRetries = 1);
644
645 void setSecurityContext(SNMP_SecurityContext *ctx);
646 SNMP_SecurityContext *getSecurityContext() { return m_securityContext; }
647 const char *getCommunityString() { return (m_securityContext != NULL) ? m_securityContext->getCommunity() : ""; }
648 const SNMP_Engine *getAuthoritativeEngine() { return m_authoritativeEngine; }
649
650 void enableEngineIdAutoupdate(bool enabled) { m_enableEngineIdAutoupdate = enabled; }
651 bool isEngineIdAutoupdateEnabled() { return m_enableEngineIdAutoupdate; }
652
653 void setPeerUpdatedOnRecv(bool enabled) { m_updatePeerOnRecv = enabled; }
654 bool isPeerUpdatedOnRecv() { return m_updatePeerOnRecv; }
655
656 void setSnmpVersion(int version) { m_snmpVersion = version; }
657 int getSnmpVersion() { return m_snmpVersion; }
658 };
659
660 /**
661 * UDP SNMP transport
662 */
663 class LIBNXSNMP_EXPORTABLE SNMP_UDPTransport : public SNMP_Transport
664 {
665 protected:
666 SOCKET m_hSocket;
667 SockAddrBuffer m_peerAddr;
668 bool m_connected;
669 size_t m_dwBufferSize;
670 size_t m_dwBytesInBuffer;
671 size_t m_dwBufferPos;
672 BYTE *m_pBuffer;
673
674 size_t preParsePDU();
675 int recvData(UINT32 dwTimeout, struct sockaddr *pSender, socklen_t *piAddrSize);
676 void clearBuffer();
677
678 public:
679 SNMP_UDPTransport();
680 SNMP_UDPTransport(SOCKET hSocket);
681 virtual ~SNMP_UDPTransport();
682
683 virtual int readMessage(SNMP_PDU **data, UINT32 timeout = INFINITE,
684 struct sockaddr *sender = NULL, socklen_t *addrSize = NULL,
685 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
686 virtual int sendMessage(SNMP_PDU *pPDU);
687 virtual InetAddress getPeerIpAddress();
688
689 UINT32 createUDPTransport(const TCHAR *hostName, WORD port = SNMP_DEFAULT_PORT);
690 UINT32 createUDPTransport(const InetAddress& hostAddr, WORD port = SNMP_DEFAULT_PORT);
691 bool isConnected() { return m_connected; }
692 };
693
694 /**
695 * Functions
696 */
697 TCHAR LIBNXSNMP_EXPORTABLE *SNMPConvertOIDToText(size_t length, const UINT32 *value, TCHAR *buffer, size_t bufferSize);
698 size_t LIBNXSNMP_EXPORTABLE SNMPParseOID(const TCHAR *text, UINT32 *buffer, size_t bufferSize);
699 bool LIBNXSNMP_EXPORTABLE SNMPIsCorrectOID(const TCHAR *oid);
700 size_t LIBNXSNMP_EXPORTABLE SNMPGetOIDLength(const TCHAR *oid);
701 const TCHAR LIBNXSNMP_EXPORTABLE *SNMPGetErrorText(UINT32 dwError);
702 UINT32 LIBNXSNMP_EXPORTABLE SNMPSaveMIBTree(const TCHAR *fileName, SNMP_MIBObject *pRoot, UINT32 dwFlags);
703 UINT32 LIBNXSNMP_EXPORTABLE SNMPLoadMIBTree(const TCHAR *fileName, SNMP_MIBObject **ppRoot);
704 UINT32 LIBNXSNMP_EXPORTABLE SNMPGetMIBTreeTimestamp(const TCHAR *fileName, UINT32 *pdwTimestamp);
705 UINT32 LIBNXSNMP_EXPORTABLE SNMPResolveDataType(const TCHAR *pszType);
706 TCHAR LIBNXSNMP_EXPORTABLE *SNMPDataTypeName(UINT32 type, TCHAR *buffer, size_t bufferSize);
707
708 UINT32 LIBNXSNMP_EXPORTABLE SnmpNewRequestId();
709 void LIBNXSNMP_EXPORTABLE SnmpSetMessageIds(DWORD msgParseError, DWORD msgTypeError, DWORD msgGetError);
710 void LIBNXSNMP_EXPORTABLE SnmpSetDefaultTimeout(UINT32 timeout);
711 UINT32 LIBNXSNMP_EXPORTABLE SnmpGetDefaultTimeout();
712 UINT32 LIBNXSNMP_EXPORTABLE SnmpGet(int version, SNMP_Transport *transport,
713 const TCHAR *szOidStr, const UINT32 *oidBinary, size_t oidLen, void *pValue,
714 size_t bufferSize, UINT32 dwFlags);
715 UINT32 LIBNXSNMP_EXPORTABLE SnmpGetEx(SNMP_Transport *pTransport,
716 const TCHAR *szOidStr, const UINT32 *oidBinary, size_t oidLen, void *pValue,
717 size_t bufferSize, UINT32 dwFlags, UINT32 *dataLen);
718 UINT32 LIBNXSNMP_EXPORTABLE SnmpWalk(UINT32 dwVersion, SNMP_Transport *pTransport, const TCHAR *szRootOid,
719 UINT32 (* pHandler)(UINT32, SNMP_Variable *, SNMP_Transport *, void *),
720 void *pUserArg, BOOL bVerbose);
721
722 #endif /* __cplusplus */
723
724 /** @} */
725
726 #endif