2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2013 Victor Kirhenshtein
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.
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.
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.
27 #include <nms_common.h>
28 #include <nms_threads.h>
32 #ifdef LIBNXSNMP_EXPORTS
33 #define LIBNXSNMP_EXPORTABLE __declspec(dllexport)
35 #define LIBNXSNMP_EXPORTABLE __declspec(dllimport)
38 #define LIBNXSNMP_EXPORTABLE
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 ****************************************************************/
50 #ifdef NXSNMP_WITH_NET_SNMP
52 #define SNMP_VERSION_2C 1
53 #define SNMP_DEFAULT_PORT 161
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
71 // OID comparision results
76 #define OID_PRECEDING 1
77 #define OID_FOLLOWING 2
82 * libnxsnmp error codes
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 */
108 // MIB parser error codes
111 #define SNMP_MPE_SUCCESS 0
112 #define SNMP_MPE_PARSE_ERROR 1
119 #define SNMP_VERSION_1 0
120 #define SNMP_VERSION_2C 1
121 #define SNMP_VERSION_3 3
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
134 #define SNMP_GET_BULK_REQUEST 5
135 #define SNMP_INFORM_REQUEST 6
136 #define SNMP_REPORT 8
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
152 // ASN.1 identifier types
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
186 #define SNMP_SECURITY_MODEL_V1 1
187 #define SNMP_SECURITY_MODEL_V2C 2
188 #define SNMP_SECURITY_MODEL_USM 3
192 // SNMP V3 header flags
195 #define SNMP_AUTH_FLAG 0x01
196 #define SNMP_PRIV_FLAG 0x02
197 #define SNMP_REPORTABLE_FLAG 0x04
201 // SNMP v3 authentication methods
204 #define SNMP_AUTH_NONE 0
205 #define SNMP_AUTH_MD5 1
206 #define SNMP_AUTH_SHA1 2
210 // SNMP v3 encryption methods
213 #define SNMP_ENCRYPT_NONE 0
214 #define SNMP_ENCRYPT_DES 1
215 #define SNMP_ENCRYPT_AES 2
219 // MIB object access types
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
231 // MIB object status codes
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
242 // MIB object data types
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
282 * Flags for SNMPSaveMIBTree
284 #define SMT_COMPRESS_DATA 0x01
285 #define SMT_SKIP_DESCRIPTIONS 0x02
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
296 #endif /* NXSNMP_WITH_NET_SNMP */
307 class LIBNXSNMP_EXPORTABLE SNMP_MIBObject
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
318 TCHAR
*m_pszDescription
;
319 TCHAR
*m_pszTextualConvention
;
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
);
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
); }
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
; }
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
; }
352 SNMP_MIBObject
*findChildByID(UINT32 dwOID
);
354 void print(int nIndent
);
356 // File I/O, supposed to be callsed only from libnxsnmp functions
357 void writeToFile(ZFile
*pFile
, UINT32 dwFlags
);
358 BOOL
readFromFile(ZFile
*pFile
);
362 * Object identifier (OID)
364 class LIBNXSNMP_EXPORTABLE SNMP_ObjectId
372 SNMP_ObjectId(const SNMP_ObjectId
&src
);
373 SNMP_ObjectId(const UINT32
*value
, size_t length
);
376 SNMP_ObjectId
& operator =(const SNMP_ObjectId
&src
);
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; }
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;
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
; }
396 static SNMP_ObjectId
parse(const TCHAR
*oid
);
400 * SNMP variable (varbind)
402 class LIBNXSNMP_EXPORTABLE SNMP_Variable
405 SNMP_ObjectId m_name
;
407 size_t m_valueLength
;
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
);
418 bool parse(const BYTE
*data
, size_t varLength
);
419 size_t encode(BYTE
*buffer
, size_t bufferSize
);
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
; }
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;
435 void setValueFromString(UINT32 type
, const TCHAR
*value
);
441 class LIBNXSNMP_EXPORTABLE SNMP_Engine
444 BYTE m_id
[SNMP_MAX_ENGINEID_LEN
];
451 SNMP_Engine(BYTE
*id
, size_t idLen
, int engineBoots
= 0, int engineTime
= 0);
452 SNMP_Engine(const SNMP_Engine
*src
);
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
; }
460 void setBoots(int boots
) { m_engineBoots
= boots
; }
461 void setTime(int engineTime
) { m_engineTime
= engineTime
; }
467 class LIBNXSNMP_EXPORTABLE SNMP_SecurityContext
471 char *m_authName
; // community for V1/V2c, user for V3 USM
472 char *m_authPassword
;
473 char *m_privPassword
;
475 BYTE m_authKeyMD5
[16];
476 BYTE m_authKeySHA1
[20];
478 SNMP_Engine m_authoritativeEngine
;
482 void recalculateKeys();
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();
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
; }
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
; }
508 json_t
*toJson() const;
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
);
520 void setContextNameA(const char *name
);
522 void setContextNameA(const char *name
) { setContextName(name
); }
525 void setAuthoritativeEngine(const SNMP_Engine
&engine
);
526 const SNMP_Engine
& getAuthoritativeEngine() { return m_authoritativeEngine
; }
532 class LIBNXSNMP_EXPORTABLE SNMP_PDU
537 ObjectArray
<SNMP_Variable
> *m_variables
;
538 SNMP_ObjectId
*m_pEnterprise
;
541 UINT32 m_dwTimeStamp
;
542 UINT32 m_dwAgentAddr
;
544 UINT32 m_dwErrorCode
;
545 UINT32 m_dwErrorIndex
;
548 BYTE m_contextEngineId
[SNMP_MAX_ENGINEID_LEN
];
549 size_t m_contextEngineIdLen
;
550 char m_contextName
[SNMP_MAX_CONTEXT_NAME
];
554 // The following attributes only used by parser and
555 // valid only for received PDUs
558 SNMP_Engine m_authoritativeEngine
;
560 BYTE m_signature
[12];
561 size_t m_signatureOffset
;
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
);
581 SNMP_PDU(UINT32 dwCommand
, UINT32 dwRqId
, UINT32 dwVersion
= SNMP_VERSION_2C
);
582 SNMP_PDU(SNMP_PDU
*src
);
585 bool parse(const BYTE
*rawData
, size_t rawLength
, SNMP_SecurityContext
*securityContext
, bool engineIdAutoupdate
);
586 size_t encode(BYTE
**ppBuffer
, SNMP_SecurityContext
*securityContext
);
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
; }
597 void setMessageId(UINT32 msgId
) { m_msgId
= msgId
; }
598 UINT32
getMessageId() { return m_msgId
; }
600 bool isReportable() { return m_reportable
; }
601 void setReportable(bool value
) { m_reportable
= value
; }
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
; }
609 UINT32
getRequestId() { return m_dwRqId
; }
610 void setRequestId(UINT32 dwId
) { m_dwRqId
= dwId
; }
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
; }
619 void unlinkVariables() { m_variables
->setOwner(false); m_variables
->clear(); m_variables
->setOwner(true); }
620 void bindVariable(SNMP_Variable
*pVar
);
624 * Generic SNMP transport
626 class LIBNXSNMP_EXPORTABLE SNMP_Transport
629 SNMP_SecurityContext
*m_securityContext
;
630 SNMP_Engine
*m_authoritativeEngine
;
631 SNMP_Engine
*m_contextEngine
;
632 bool m_enableEngineIdAutoupdate
;
633 bool m_updatePeerOnRecv
;
639 virtual ~SNMP_Transport();
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
)
647 virtual int sendMessage(SNMP_PDU
*pdu
)
651 virtual InetAddress
getPeerIpAddress()
653 return InetAddress();
656 virtual WORD
getPort()=0;
658 UINT32
doRequest(SNMP_PDU
*request
, SNMP_PDU
**response
, UINT32 timeout
= INFINITE
, int numRetries
= 1);
660 void setSecurityContext(SNMP_SecurityContext
*ctx
);
661 SNMP_SecurityContext
*getSecurityContext() { return m_securityContext
; }
662 const char *getCommunityString() { return (m_securityContext
!= NULL
) ? m_securityContext
->getCommunity() : ""; }
663 const SNMP_Engine
*getAuthoritativeEngine() { return m_authoritativeEngine
; }
665 void enableEngineIdAutoupdate(bool enabled
) { m_enableEngineIdAutoupdate
= enabled
; }
666 bool isEngineIdAutoupdateEnabled() { return m_enableEngineIdAutoupdate
; }
668 void setPeerUpdatedOnRecv(bool enabled
) { m_updatePeerOnRecv
= enabled
; }
669 bool isPeerUpdatedOnRecv() { return m_updatePeerOnRecv
; }
671 void setSnmpVersion(int version
) { m_snmpVersion
= version
; }
672 int getSnmpVersion() { return m_snmpVersion
; }
678 class LIBNXSNMP_EXPORTABLE SNMP_UDPTransport
: public SNMP_Transport
682 SockAddrBuffer m_peerAddr
;
684 size_t m_dwBufferSize
;
685 size_t m_dwBytesInBuffer
;
686 size_t m_dwBufferPos
;
690 size_t preParsePDU();
691 int recvData(UINT32 dwTimeout
, struct sockaddr
*pSender
, socklen_t
*piAddrSize
);
696 SNMP_UDPTransport(SOCKET hSocket
);
697 virtual ~SNMP_UDPTransport();
699 virtual int readMessage(SNMP_PDU
**data
, UINT32 timeout
= INFINITE
,
700 struct sockaddr
*sender
= NULL
, socklen_t
*addrSize
= NULL
,
701 SNMP_SecurityContext
* (*contextFinder
)(struct sockaddr
*, socklen_t
) = NULL
);
702 virtual int sendMessage(SNMP_PDU
*pPDU
);
703 virtual InetAddress
getPeerIpAddress();
705 UINT32
createUDPTransport(const TCHAR
*hostName
, UINT16 port
= SNMP_DEFAULT_PORT
);
706 UINT32
createUDPTransport(const InetAddress
& hostAddr
, UINT16 port
= SNMP_DEFAULT_PORT
);
707 bool isConnected() { return m_connected
; }
708 UINT16
getPort() { return m_port
; }
711 struct SNMP_SnapshotIndexEntry
;
716 class LIBNXSNMP_EXPORTABLE SNMP_Snapshot
719 ObjectArray
<SNMP_Variable
> *m_values
;
720 SNMP_SnapshotIndexEntry
*m_index
;
722 static UINT32
callback(SNMP_Variable
*var
, SNMP_Transport
*transport
, void *arg
);
725 SNMP_SnapshotIndexEntry
*find(const UINT32
*oid
, size_t oidLen
) const;
726 SNMP_SnapshotIndexEntry
*find(const TCHAR
*oid
) const;
727 SNMP_SnapshotIndexEntry
*find(const SNMP_ObjectId
& oid
) const;
731 virtual ~SNMP_Snapshot();
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
);
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;
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); }
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
; }
759 int size() const { return m_values
->size(); }
760 bool isEmpty() const { return m_values
->size() == 0; }
766 TCHAR LIBNXSNMP_EXPORTABLE
*SNMPConvertOIDToText(size_t length
, const UINT32
*value
, TCHAR
*buffer
, size_t bufferSize
);
767 size_t LIBNXSNMP_EXPORTABLE
SNMPParseOID(const TCHAR
*text
, UINT32
*buffer
, size_t bufferSize
);
768 bool LIBNXSNMP_EXPORTABLE
SNMPIsCorrectOID(const TCHAR
*oid
);
769 size_t LIBNXSNMP_EXPORTABLE
SNMPGetOIDLength(const TCHAR
*oid
);
770 const TCHAR LIBNXSNMP_EXPORTABLE
*SNMPGetErrorText(UINT32 dwError
);
771 UINT32 LIBNXSNMP_EXPORTABLE
SNMPSaveMIBTree(const TCHAR
*fileName
, SNMP_MIBObject
*pRoot
, UINT32 dwFlags
);
772 UINT32 LIBNXSNMP_EXPORTABLE
SNMPLoadMIBTree(const TCHAR
*fileName
, SNMP_MIBObject
**ppRoot
);
773 UINT32 LIBNXSNMP_EXPORTABLE
SNMPGetMIBTreeTimestamp(const TCHAR
*fileName
, UINT32
*pdwTimestamp
);
774 UINT32 LIBNXSNMP_EXPORTABLE
SNMPResolveDataType(const TCHAR
*pszType
);
775 TCHAR LIBNXSNMP_EXPORTABLE
*SNMPDataTypeName(UINT32 type
, TCHAR
*buffer
, size_t bufferSize
);
777 UINT32 LIBNXSNMP_EXPORTABLE
SnmpNewRequestId();
778 void LIBNXSNMP_EXPORTABLE
SnmpSetMessageIds(DWORD msgParseError
, DWORD msgTypeError
, DWORD msgGetError
);
779 void LIBNXSNMP_EXPORTABLE
SnmpSetDefaultTimeout(UINT32 timeout
);
780 UINT32 LIBNXSNMP_EXPORTABLE
SnmpGetDefaultTimeout();
781 UINT32 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
);
784 UINT32 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
);
787 UINT32 LIBNXSNMP_EXPORTABLE
SnmpWalk(SNMP_Transport
*transport
, const TCHAR
*rootOid
,
788 UINT32 (* handler
)(SNMP_Variable
*, SNMP_Transport
*, void *),
789 void *userArg
, bool logErrors
= false);
790 UINT32 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);
794 #endif /* __cplusplus */