license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / include / nxsnmp.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2010 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
42 /***************************************************************
43 Following part of the file may conflict with net-snmp includes,
44 so it can be excluded by defining NXSNMP_WITH_NET_SNMP
45 ****************************************************************/
46
47 #ifdef NXSNMP_WITH_NET_SNMP
48
49 #define SNMP_VERSION_2C 1
50 #define SNMP_DEFAULT_PORT 161
51
52 #else
53
54
55 //
56 // Various constants
57 //
58
59 #define MAX_OID_LEN 128
60 #define MAX_MIB_OBJECT_NAME 64
61 #define SNMP_DEFAULT_PORT 161
62 #define SNMP_MAX_CONTEXT_NAME 256
63 #define SNMP_MAX_ENGINEID_LEN 256
64 #define SNMP_DEFAULT_MSG_MAX_SIZE 65536
65
66
67 //
68 // OID comparision results
69 //
70
71 #define OID_ERROR -1
72 #define OID_EQUAL 0
73 #define OID_NOT_EQUAL 1
74 #define OID_SHORTER 2
75 #define OID_LONGER 3
76
77
78 //
79 // libnxsnmp error codes
80 //
81
82 #define SNMP_ERR_SUCCESS 0 /* success */
83 #define SNMP_ERR_TIMEOUT 1 /* request timeout */
84 #define SNMP_ERR_PARAM 2 /* invalid parameters passed to function */
85 #define SNMP_ERR_SOCKET 3 /* unable to create socket */
86 #define SNMP_ERR_COMM 4 /* send/receive error */
87 #define SNMP_ERR_PARSE 5 /* error parsing PDU */
88 #define SNMP_ERR_NO_OBJECT 6 /* given object doesn't exist on agent */
89 #define SNMP_ERR_HOSTNAME 7 /* invalid hostname or IP address */
90 #define SNMP_ERR_BAD_OID 8 /* object id is incorrect */
91 #define SNMP_ERR_AGENT 9 /* agent returns an error */
92 #define SNMP_ERR_BAD_TYPE 10 /* unknown variable data type */
93 #define SNMP_ERR_FILE_IO 11 /* file I/O error */
94 #define SNMP_ERR_BAD_FILE_HEADER 12 /* file header is invalid */
95 #define SNMP_ERR_BAD_FILE_DATA 13 /* file data is invalid or corrupted */
96 #define SNMP_ERR_UNSUPP_SEC_LEVEL 14 /* unsupported security level */
97 #define SNMP_ERR_TIME_WINDOW 15 /* not in time window */
98 #define SNMP_ERR_SEC_NAME 16 /* unknown security name */
99 #define SNMP_ERR_ENGINE_ID 17 /* unknown engine ID */
100 #define SNMP_ERR_AUTH_FAILURE 18 /* authentication failure */
101 #define SNMP_ERR_DECRYPTION 19 /* decryption error */
102 #define SNMP_ERR_BAD_RESPONSE 20 /* malformed or unexpected response from agent */
103
104
105 //
106 // MIB parser error codes
107 //
108
109 #define SNMP_MPE_SUCCESS 0
110 #define SNMP_MPE_PARSE_ERROR 1
111
112
113 //
114 // SNMP versions
115 //
116
117 #define SNMP_VERSION_1 0
118 #define SNMP_VERSION_2C 1
119 #define SNMP_VERSION_3 3
120
121
122 //
123 // PDU types
124 //
125
126 #define SNMP_INVALID_PDU 255
127 #define SNMP_GET_REQUEST 0
128 #define SNMP_GET_NEXT_REQUEST 1
129 #define SNMP_RESPONSE 2
130 #define SNMP_SET_REQUEST 3
131 #define SNMP_TRAP 4
132 #define SNMP_GET_BULK_REQUEST 5
133 #define SNMP_INFORM_REQUEST 6
134 #define SNMP_REPORT 8
135
136
137 //
138 // PDU error codes
139 //
140
141 #define SNMP_PDU_ERR_SUCCESS 0
142 #define SNMP_PDU_ERR_TOO_BIG 1
143 #define SNMP_PDU_ERR_NO_SUCH_NAME 2
144 #define SNMP_PDU_ERR_BAD_VALUE 3
145 #define SNMP_PDU_ERR_READ_ONLY 4
146 #define SNMP_PDU_ERR_GENERIC 5
147
148
149 //
150 // ASN.1 identifier types
151 //
152
153 #define ASN_INTEGER 0x02
154 #define ASN_BIT_STRING 0x03
155 #define ASN_OCTET_STRING 0x04
156 #define ASN_NULL 0x05
157 #define ASN_OBJECT_ID 0x06
158 #define ASN_SEQUENCE 0x30
159 #define ASN_IP_ADDR 0x40
160 #define ASN_COUNTER32 0x41
161 #define ASN_GAUGE32 0x42
162 #define ASN_TIMETICKS 0x43
163 #define ASN_OPAQUE 0x44
164 #define ASN_NSAP_ADDR 0x45
165 #define ASN_COUNTER64 0x46
166 #define ASN_UINTEGER32 0x47
167 #define ASN_NO_SUCH_OBJECT 0x80
168 #define ASN_NO_SUCH_INSTANCE 0x81
169 #define ASN_GET_REQUEST_PDU 0xA0
170 #define ASN_GET_NEXT_REQUEST_PDU 0xA1
171 #define ASN_RESPONSE_PDU 0xA2
172 #define ASN_SET_REQUEST_PDU 0xA3
173 #define ASN_TRAP_V1_PDU 0xA4
174 #define ASN_GET_BULK_REQUEST_PDU 0xA5
175 #define ASN_INFORM_REQUEST_PDU 0xA6
176 #define ASN_TRAP_V2_PDU 0xA7
177 #define ASN_REPORT_PDU 0xA8
178
179
180 //
181 // Security models
182 //
183
184 #define SNMP_SECURITY_MODEL_V1 1
185 #define SNMP_SECURITY_MODEL_V2C 2
186 #define SNMP_SECURITY_MODEL_USM 3
187
188
189 //
190 // SNMP V3 header flags
191 //
192
193 #define SNMP_AUTH_FLAG 0x01
194 #define SNMP_PRIV_FLAG 0x02
195 #define SNMP_REPORTABLE_FLAG 0x04
196
197
198 //
199 // SNMP v3 authentication methods
200 //
201
202 #define SNMP_AUTH_NONE 0
203 #define SNMP_AUTH_MD5 1
204 #define SNMP_AUTH_SHA1 2
205
206
207 //
208 // SNMP v3 encryption methods
209 //
210
211 #define SNMP_ENCRYPT_NONE 0
212 #define SNMP_ENCRYPT_DES 1
213 #define SNMP_ENCRYPT_AES 2
214
215
216 //
217 // MIB object access types
218 //
219
220 #define MIB_ACCESS_READONLY 1
221 #define MIB_ACCESS_READWRITE 2
222 #define MIB_ACCESS_WRITEONLY 3
223 #define MIB_ACCESS_NOACCESS 4
224 #define MIB_ACCESS_NOTIFY 5
225 #define MIB_ACCESS_CREATE 6
226
227
228 //
229 // MIB object status codes
230 //
231
232 #define MIB_STATUS_MANDATORY 1
233 #define MIB_STATUS_OPTIONAL 2
234 #define MIB_STATUS_OBSOLETE 3
235 #define MIB_STATUS_DEPRECATED 4
236 #define MIB_STATUS_CURRENT 5
237
238
239 //
240 // MIB object data types
241 //
242
243 #define MIB_TYPE_OTHER 0
244 #define MIB_TYPE_IMPORT_ITEM 1
245 #define MIB_TYPE_OBJID 2
246 #define MIB_TYPE_BITSTRING 3
247 #define MIB_TYPE_INTEGER 4
248 #define MIB_TYPE_INTEGER32 5
249 #define MIB_TYPE_INTEGER64 6
250 #define MIB_TYPE_UNSIGNED32 7
251 #define MIB_TYPE_COUNTER 8
252 #define MIB_TYPE_COUNTER32 9
253 #define MIB_TYPE_COUNTER64 10
254 #define MIB_TYPE_GAUGE 11
255 #define MIB_TYPE_GAUGE32 12
256 #define MIB_TYPE_TIMETICKS 13
257 #define MIB_TYPE_OCTETSTR 14
258 #define MIB_TYPE_OPAQUE 15
259 #define MIB_TYPE_IPADDR 16
260 #define MIB_TYPE_PHYSADDR 17
261 #define MIB_TYPE_NETADDR 18
262 #define MIB_TYPE_NAMED_TYPE 19
263 #define MIB_TYPE_SEQID 20
264 #define MIB_TYPE_SEQUENCE 21
265 #define MIB_TYPE_CHOICE 22
266 #define MIB_TYPE_TEXTUAL_CONVENTION 23
267 #define MIB_TYPE_MACRO_DEFINITION 24
268 #define MIB_TYPE_MODCOMP 25
269 #define MIB_TYPE_TRAPTYPE 26
270 #define MIB_TYPE_NOTIFTYPE 27
271 #define MIB_TYPE_MODID 28
272 #define MIB_TYPE_NSAPADDRESS 29
273 #define MIB_TYPE_AGENTCAP 30
274 #define MIB_TYPE_UINTEGER 31
275 #define MIB_TYPE_NULL 32
276 #define MIB_TYPE_OBJGROUP 33
277 #define MIB_TYPE_NOTIFGROUP 34
278
279
280 //
281 // Flags for SNMPSaveMIBTree
282 //
283
284 #define SMT_COMPRESS_DATA 0x01
285 #define SMT_SKIP_DESCRIPTIONS 0x02
286
287
288 #endif /* NXSNMP_WITH_NET_SNMP */
289
290
291 #ifdef __cplusplus
292
293 //
294 // MIB tree node
295 //
296
297 class ZFile;
298
299 class LIBNXSNMP_EXPORTABLE SNMP_MIBObject
300 {
301 private:
302 SNMP_MIBObject *m_pParent;
303 SNMP_MIBObject *m_pNext;
304 SNMP_MIBObject *m_pPrev;
305 SNMP_MIBObject *m_pFirst; // First child
306 SNMP_MIBObject *m_pLast; // Last child
307
308 DWORD m_dwOID;
309 TCHAR *m_pszName;
310 TCHAR *m_pszDescription;
311 int m_iType;
312 int m_iStatus;
313 int m_iAccess;
314
315 void Initialize(void);
316
317 public:
318 SNMP_MIBObject(void);
319 SNMP_MIBObject(DWORD dwOID, const TCHAR *pszName);
320 SNMP_MIBObject(DWORD dwOID, const TCHAR *pszName, int iType,
321 int iStatus, int iAccess, const TCHAR *pszDescription);
322 ~SNMP_MIBObject();
323
324 void SetParent(SNMP_MIBObject *pObject) { m_pParent = pObject; }
325 void AddChild(SNMP_MIBObject *pObject);
326 void SetInfo(int iType, int iStatus, int iAccess, const TCHAR *pszDescription);
327 void SetName(const TCHAR *pszName) { safe_free(m_pszName); m_pszName = _tcsdup(pszName); }
328
329 SNMP_MIBObject *Parent(void) { return m_pParent; }
330 SNMP_MIBObject *Next(void) { return m_pNext; }
331 SNMP_MIBObject *Prev(void) { return m_pPrev; }
332 SNMP_MIBObject *FirstChild(void) { return m_pFirst; }
333 SNMP_MIBObject *LastChild(void) { return m_pLast; }
334
335 DWORD OID(void) { return m_dwOID; }
336 const TCHAR *Name(void) { return m_pszName; }
337 const TCHAR *Description(void) { return m_pszDescription; }
338 int Type(void) { return m_iType; }
339 int Status(void) { return m_iStatus; }
340 int Access(void) { return m_iAccess; }
341
342 SNMP_MIBObject *FindChildByID(DWORD dwOID);
343
344 void Print(int nIndent);
345
346 // File I/O, supposed to be callsed only from libnxsnmp functions
347 void WriteToFile(ZFile *pFile, DWORD dwFlags);
348 BOOL ReadFromFile(ZFile *pFile);
349 };
350
351
352 //
353 // Object identifier
354 //
355
356 class LIBNXSNMP_EXPORTABLE SNMP_ObjectId
357 {
358 private:
359 DWORD m_dwLength;
360 DWORD *m_pdwValue;
361 TCHAR *m_pszTextValue;
362
363 void ConvertToText(void);
364
365 public:
366 SNMP_ObjectId();
367 SNMP_ObjectId(DWORD dwLength, DWORD *pdwValue);
368 ~SNMP_ObjectId();
369
370 DWORD Length(void) { return m_dwLength; }
371 const DWORD *GetValue(void) { return m_pdwValue; }
372 const TCHAR *GetValueAsText(void) { return CHECK_NULL(m_pszTextValue); }
373 void SetValue(DWORD *pdwValue, DWORD dwLength);
374 void Extend(DWORD dwSubId);
375
376 int Compare(TCHAR *pszOid);
377 int Compare(DWORD *pdwOid, DWORD dwLen);
378 };
379
380
381 //
382 // SNMP variable
383 //
384
385 class LIBNXSNMP_EXPORTABLE SNMP_Variable
386 {
387 private:
388 SNMP_ObjectId *m_pName;
389 DWORD m_dwType;
390 DWORD m_dwValueLength;
391 BYTE *m_pValue;
392
393 public:
394 SNMP_Variable();
395 SNMP_Variable(TCHAR *pszName);
396 SNMP_Variable(DWORD *pdwName, DWORD dwNameLen);
397 ~SNMP_Variable();
398
399 BOOL Parse(BYTE *pData, DWORD dwVarLength);
400 DWORD Encode(BYTE *pBuffer, DWORD dwBufferSize);
401
402 SNMP_ObjectId *GetName(void) { return m_pName; }
403 DWORD GetType(void) { return m_dwType; }
404 DWORD GetValueLength(void) { return m_dwValueLength; }
405 const BYTE *GetValue(void) { return m_pValue; }
406
407 DWORD GetValueAsUInt(void);
408 LONG GetValueAsInt(void);
409 TCHAR *GetValueAsString(TCHAR *pszBuffer, DWORD dwBufferSize);
410 SNMP_ObjectId *GetValueAsObjectId(void);
411 TCHAR *GetValueAsMACAddr(TCHAR *pszBuffer);
412 TCHAR *GetValueAsIPAddr(TCHAR *pszBuffer);
413
414 void SetValueFromString(DWORD dwType, TCHAR *pszValue);
415 };
416
417
418 //
419 // SNMP engine
420 //
421
422 class LIBNXSNMP_EXPORTABLE SNMP_Engine
423 {
424 private:
425 BYTE m_id[SNMP_MAX_ENGINEID_LEN];
426 int m_idLen;
427 int m_engineBoots;
428 int m_engineTime;
429
430 public:
431 SNMP_Engine();
432 SNMP_Engine(BYTE *id, int idLen, int engineBoots = 0, int engineTime = 0);
433 SNMP_Engine(SNMP_Engine *src);
434 ~SNMP_Engine();
435
436 BYTE *getId() { return m_id; }
437 int getIdLen() { return m_idLen; }
438 int getBoots() { return m_engineBoots; }
439 int getTime() { return m_engineTime; }
440
441 void setBoots(int boots) { m_engineBoots = boots; }
442 void setTime(int engineTime) { m_engineTime = engineTime; }
443 };
444
445
446 //
447 // Security context
448 //
449
450 class LIBNXSNMP_EXPORTABLE SNMP_SecurityContext
451 {
452 private:
453 int m_securityModel;
454 char *m_authName; // community for V1/V2c, user for V3 USM
455 char *m_authPassword;
456 char *m_privPassword;
457 BYTE m_authKeyMD5[16];
458 BYTE m_authKeySHA1[20];
459 BYTE m_privKey[20];
460 SNMP_Engine m_authoritativeEngine;
461 int m_authMethod;
462 int m_privMethod;
463
464 void recalculateKeys();
465
466 public:
467 SNMP_SecurityContext();
468 SNMP_SecurityContext(SNMP_SecurityContext *src);
469 SNMP_SecurityContext(const char *user);
470 SNMP_SecurityContext(const char *user, const char *authPassword, int authMethod);
471 SNMP_SecurityContext(const char *user, const char *authPassword, const char *encryptionPassword,
472 int authMethod, int encryptionMethod);
473 ~SNMP_SecurityContext();
474
475 int getSecurityModel() { return m_securityModel; }
476 const char *getCommunity() { return CHECK_NULL_EX_A(m_authName); }
477 const char *getUser() { return CHECK_NULL_EX_A(m_authName); }
478 const char *getAuthPassword() { return CHECK_NULL_EX_A(m_authPassword); }
479 const char *getPrivPassword() { return CHECK_NULL_EX_A(m_privPassword); }
480
481 bool needAuthentication() { return (m_authMethod != SNMP_AUTH_NONE) && (m_authoritativeEngine.getIdLen() != 0); }
482 bool needEncryption() { return (m_privMethod != SNMP_ENCRYPT_NONE) && (m_authoritativeEngine.getIdLen() != 0); }
483 int getAuthMethod() { return m_authMethod; }
484 int getPrivMethod() { return m_privMethod; }
485 BYTE *getAuthKeyMD5() { return m_authKeyMD5; }
486 BYTE *getAuthKeySHA1() { return m_authKeySHA1; }
487 BYTE *getPrivKey() { return m_privKey; }
488
489 void setAuthName(const char *name);
490 void setCommunity(const char *community) { setAuthName(community); }
491 void setUser(const char *user) { setAuthName(user); }
492 void setAuthPassword(const char *password);
493 void setPrivPassword(const char *password);
494 void setAuthMethod(int method) { m_authMethod = method; }
495 void setPrivMethod(int method) { m_privMethod = method; }
496 void setSecurityModel(int model) { m_securityModel = model; }
497
498 void setAuthoritativeEngine(SNMP_Engine &engine);
499 SNMP_Engine& getAuthoritativeEngine() { return m_authoritativeEngine; }
500 };
501
502
503 //
504 // SNMP PDU
505 //
506
507 class LIBNXSNMP_EXPORTABLE SNMP_PDU
508 {
509 private:
510 DWORD m_dwVersion;
511 DWORD m_dwCommand;
512 DWORD m_dwNumVariables;
513 SNMP_Variable **m_ppVarList;
514 SNMP_ObjectId *m_pEnterprise;
515 int m_iTrapType;
516 int m_iSpecificTrap;
517 DWORD m_dwTimeStamp;
518 DWORD m_dwAgentAddr;
519 DWORD m_dwRqId;
520 DWORD m_dwErrorCode;
521 DWORD m_dwErrorIndex;
522 DWORD m_msgId;
523 DWORD m_msgMaxSize;
524 BYTE m_contextEngineId[SNMP_MAX_ENGINEID_LEN];
525 int m_contextEngineIdLen;
526 char m_contextName[SNMP_MAX_CONTEXT_NAME];
527 BYTE m_salt[8];
528
529 // The following attributes only used by parser and
530 // valid only for received PDUs
531 BYTE m_flags;
532 char *m_authObject;
533 SNMP_Engine m_authoritativeEngine;
534 int m_securityModel;
535 BYTE m_signature[12];
536
537 BOOL parseVariable(BYTE *pData, DWORD dwVarLength);
538 BOOL parseVarBinds(BYTE *pData, DWORD dwPDULength);
539 BOOL parsePdu(BYTE *pdu, DWORD pduLength);
540 BOOL parseTrapPDU(BYTE *pData, DWORD dwPDULength);
541 BOOL parseTrap2PDU(BYTE *pData, DWORD dwPDULength);
542 BOOL parsePduContent(BYTE *pData, DWORD dwPDULength);
543 BOOL parseV3Header(BYTE *pData, DWORD dwPDULength);
544 BOOL parseV3SecurityUsm(BYTE *pData, DWORD dwPDULength);
545 BOOL parseV3ScopedPdu(BYTE *pData, DWORD dwPDULength);
546 BOOL validateSignedMessage(BYTE *msg, DWORD msgLen, SNMP_SecurityContext *securityContext);
547 DWORD encodeV3Header(BYTE *buffer, DWORD bufferSize, SNMP_SecurityContext *securityContext);
548 DWORD encodeV3SecurityParameters(BYTE *buffer, DWORD bufferSize, SNMP_SecurityContext *securityContext);
549 DWORD encodeV3ScopedPDU(DWORD pduType, BYTE *pdu, DWORD pduSize, BYTE *buffer, DWORD bufferSize);
550 void signMessage(BYTE *msg, DWORD msgLen, SNMP_SecurityContext *securityContext);
551 BOOL decryptData(BYTE *data, DWORD length, BYTE *decryptedData, SNMP_SecurityContext *securityContext);
552
553 public:
554 SNMP_PDU();
555 SNMP_PDU(DWORD dwCommand, DWORD dwRqId, DWORD dwVersion = SNMP_VERSION_2C);
556 ~SNMP_PDU();
557
558 BOOL parse(BYTE *pRawData, DWORD dwRawLength, SNMP_SecurityContext *securityContext, bool engineIdAutoupdate);
559 DWORD encode(BYTE **ppBuffer, SNMP_SecurityContext *securityContext);
560
561 DWORD getCommand(void) { return m_dwCommand; }
562 SNMP_ObjectId *getTrapId(void) { return m_pEnterprise; }
563 int getTrapType(void) { return m_iTrapType; }
564 int getSpecificTrapType(void) { return m_iSpecificTrap; }
565 DWORD getNumVariables(void) { return m_dwNumVariables; }
566 SNMP_Variable *getVariable(DWORD dwIndex) { return (dwIndex < m_dwNumVariables) ? m_ppVarList[dwIndex] : NULL; }
567 DWORD getVersion(void) { return m_dwVersion; }
568 DWORD getErrorCode(void) { return m_dwErrorCode; }
569 DWORD getMessageId() { return m_msgId; }
570
571 const char *getCommunity() { return (m_authObject != NULL) ? m_authObject : ""; }
572 const char *getUser() { return (m_authObject != NULL) ? m_authObject : ""; }
573 SNMP_Engine& getAuthoritativeEngine() { return m_authoritativeEngine; }
574 int getSecurityModel() { return m_securityModel; }
575 int getFlags() { return (int)m_flags; }
576
577 DWORD getRequestId(void) { return m_dwRqId; }
578 void setRequestId(DWORD dwId) { m_dwRqId = dwId; }
579
580 void setContextEngineId(BYTE *id, int len);
581 void setContextEngineId(const char *id);
582 void setContextName(const char *name);
583 const char *getContextName() { return m_contextName; }
584 int getContextEngineIdLength() { return m_contextEngineIdLen; }
585 BYTE *getContextEngineId() { return m_contextEngineId; }
586
587 void bindVariable(SNMP_Variable *pVar);
588 };
589
590
591 //
592 // Generic SNMP transport
593 //
594
595 class LIBNXSNMP_EXPORTABLE SNMP_Transport
596 {
597 protected:
598 SNMP_SecurityContext *m_securityContext;
599 SNMP_Engine *m_authoritativeEngine;
600 SNMP_Engine *m_contextEngine;
601 bool m_enableEngineIdAutoupdate;
602
603 public:
604 SNMP_Transport();
605 virtual ~SNMP_Transport();
606
607 virtual int readMessage(SNMP_PDU **data, DWORD timeout = INFINITE,
608 struct sockaddr *sender = NULL, socklen_t *addrSize = NULL,
609 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL)
610 {
611 return -1;
612 }
613 virtual int sendMessage(SNMP_PDU *pdu)
614 {
615 return -1;
616 }
617
618 DWORD doRequest(SNMP_PDU *request, SNMP_PDU **response,
619 DWORD timeout = INFINITE, int numRetries = 1);
620
621 void setSecurityContext(SNMP_SecurityContext *ctx);
622 const char *getCommunityString() { return (m_securityContext != NULL) ? m_securityContext->getCommunity() : ""; }
623
624 void enableEngineIdAutoupdate(bool enabled) { m_enableEngineIdAutoupdate = enabled; }
625 bool isEngineIdAutoupdateEnabled() { return m_enableEngineIdAutoupdate; }
626 };
627
628
629 //
630 // UDP SNMP transport
631 //
632
633 class LIBNXSNMP_EXPORTABLE SNMP_UDPTransport : public SNMP_Transport
634 {
635 private:
636 SOCKET m_hSocket;
637 DWORD m_dwBufferSize;
638 DWORD m_dwBytesInBuffer;
639 DWORD m_dwBufferPos;
640 BYTE *m_pBuffer;
641
642 DWORD preParsePDU(void);
643 int recvData(DWORD dwTimeout, struct sockaddr *pSender, socklen_t *piAddrSize);
644 void clearBuffer(void);
645
646 public:
647 SNMP_UDPTransport();
648 SNMP_UDPTransport(SOCKET hSocket);
649 virtual ~SNMP_UDPTransport();
650
651 virtual int readMessage(SNMP_PDU **data, DWORD timeout = INFINITE,
652 struct sockaddr *sender = NULL, socklen_t *addrSize = NULL,
653 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
654 virtual int sendMessage(SNMP_PDU *pPDU);
655
656 DWORD createUDPTransport(TCHAR *pszHostName, DWORD dwHostAddr = 0, WORD wPort = SNMP_DEFAULT_PORT);
657 };
658
659
660 //
661 // Functions
662 //
663
664 TCHAR LIBNXSNMP_EXPORTABLE *SNMPConvertOIDToText(DWORD dwLength, DWORD *pdwValue, TCHAR *pszBuffer, DWORD dwBufferSize);
665 DWORD LIBNXSNMP_EXPORTABLE SNMPParseOID(const TCHAR *pszText, DWORD *pdwBuffer, DWORD dwBufferSize);
666 BOOL LIBNXSNMP_EXPORTABLE SNMPIsCorrectOID(const TCHAR *pszText);
667 const TCHAR LIBNXSNMP_EXPORTABLE *SNMPGetErrorText(DWORD dwError);
668 DWORD LIBNXSNMP_EXPORTABLE SNMPSaveMIBTree(const TCHAR *pszFile, SNMP_MIBObject *pRoot, DWORD dwFlags);
669 DWORD LIBNXSNMP_EXPORTABLE SNMPLoadMIBTree(const TCHAR *pszFile, SNMP_MIBObject **ppRoot);
670 DWORD LIBNXSNMP_EXPORTABLE SNMPGetMIBTreeTimestamp(const TCHAR *pszFile, DWORD *pdwTimestamp);
671 DWORD LIBNXSNMP_EXPORTABLE SNMPResolveDataType(const TCHAR *pszType);
672
673 #endif /* __cplusplus */
674
675 #endif