- SNMP agent connectivity check changed: now agent considered as reacheable even...
[public/netxms.git] / include / nxsnmp.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003, 2004 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 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 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 ** $module: nxsnmp.h
20 **
21 **/
22
23 #ifndef _nxsnmp_h_
24 #define _nxsnmp_h_
25
26 #include <nms_common.h>
27 #include <nms_threads.h>
28
29 #ifdef _WIN32
30 #ifdef LIBNXSNMP_EXPORTS
31 #define LIBNXSNMP_EXPORTABLE __declspec(dllexport)
32 #else
33 #define LIBNXSNMP_EXPORTABLE __declspec(dllimport)
34 #endif
35 #else /* _WIN32 */
36 #define LIBNXSNMP_EXPORTABLE
37 #endif
38
39
40 /***************************************************************
41 Following part of the file may conflict with net-snmp includes,
42 so it can be excluded by defining NXSNMP_WITH_NET_SNMP
43 ****************************************************************/
44
45 #ifdef NXSNMP_WITH_NET_SNMP
46
47 #define SNMP_VERSION_2C 1
48
49 #else
50
51
52 //
53 // Various constants
54 //
55
56 #define MAX_OID_LEN 128
57
58
59 //
60 // OID comparision results
61 //
62
63 #define OID_ERROR -1
64 #define OID_EQUAL 0
65 #define OID_NOT_EQUAL 1
66 #define OID_SHORTER 2
67 #define OID_LONGER 3
68
69
70 //
71 // libnxsnmp error codes
72 //
73
74 #define SNMP_ERR_SUCCESS 0 /* success */
75 #define SNMP_ERR_TIMEOUT 1 /* request timeout */
76 #define SNMP_ERR_PARAM 2 /* invalid parameters passed to function */
77 #define SNMP_ERR_SOCKET 3 /* unable to create socket */
78 #define SNMP_ERR_COMM 4 /* send/receive error */
79 #define SNMP_ERR_PARSE 5 /* error parsing PDU */
80 #define SNMP_ERR_NO_OBJECT 6 /* given object doesn't exist on agent */
81 #define SNMP_ERR_HOSTNAME 7 /* invalid hostname or IP address */
82 #define SNMP_ERR_BAD_OID 8 /* object id is incorrect */
83 #define SNMP_ERR_AGENT 9 /* agent returns an error */
84 #define SNMP_ERR_BAD_TYPE 10 /* unknown variable data type */
85
86
87 //
88 // SNMP versions
89 //
90
91 #define SNMP_VERSION_1 0
92 #define SNMP_VERSION_2C 1
93
94
95 //
96 // PDU types
97 //
98
99 #define SNMP_INVALID_PDU 255
100 #define SNMP_GET_REQUEST 0
101 #define SNMP_GET_NEXT_REQUEST 1
102 #define SNMP_GET_RESPONCE 2
103 #define SNMP_SET_REQUEST 3
104 #define SNMP_TRAP 4
105 #define SNMP_GET_BULK_REQUEST 5
106 #define SNMP_INFORM_REQUEST 6
107
108
109 //
110 // PDU error codes
111 //
112
113 #define SNMP_PDU_ERR_SUCCESS 0
114 #define SNMP_PDU_ERR_TOO_BIG 1
115 #define SNMP_PDU_ERR_NO_SUCH_NAME 2
116 #define SNMP_PDU_ERR_BAD_VALUE 3
117 #define SNMP_PDU_ERR_READ_ONLY 4
118 #define SNMP_PDU_ERR_GENERIC 5
119
120
121 //
122 // ASN.1 identifier types
123 //
124
125 #define ASN_INTEGER 0x02
126 #define ASN_BIT_STRING 0x03
127 #define ASN_OCTET_STRING 0x04
128 #define ASN_NULL 0x05
129 #define ASN_OBJECT_ID 0x06
130 #define ASN_SEQUENCE 0x30
131 #define ASN_IP_ADDR 0x40
132 #define ASN_COUNTER32 0x41
133 #define ASN_GAUGE32 0x42
134 #define ASN_TIMETICKS 0x43
135 #define ASN_OPAQUE 0x44
136 #define ASN_NSAP_ADDR 0x45
137 #define ASN_COUNTER64 0x46
138 #define ASN_UINTEGER32 0x47
139 #define ASN_NO_SUCH_OBJECT 0x80
140 #define ASN_NO_SUCH_INSTANCE 0x81
141 #define ASN_GET_REQUEST_PDU 0xA0
142 #define ASN_GET_NEXT_REQUEST_PDU 0xA1
143 #define ASN_GET_RESPONCE_PDU 0xA2
144 #define ASN_SET_REQUEST_PDU 0xA3
145 #define ASN_TRAP_V1_PDU 0xA4
146 #define ASN_GET_BULK_REQUEST_PDU 0xA5
147 #define ASN_INFORM_REQUEST_PDU 0xA6
148 #define ASN_TRAP_V2_PDU 0xA7
149
150
151 #endif /* NXSNMP_WITH_NET_SNMP */
152
153
154 //
155 // Object identifier
156 //
157
158 class LIBNXSNMP_EXPORTABLE SNMP_ObjectId
159 {
160 private:
161 DWORD m_dwLength;
162 DWORD *m_pdwValue;
163 TCHAR *m_pszTextValue;
164
165 void ConvertToText(void);
166
167 public:
168 SNMP_ObjectId();
169 SNMP_ObjectId(DWORD dwLength, DWORD *pdwValue);
170 ~SNMP_ObjectId();
171
172 DWORD Length(void) { return m_dwLength; }
173 const DWORD *GetValue(void) { return m_pdwValue; }
174 const TCHAR *GetValueAsText(void) { return CHECK_NULL(m_pszTextValue); }
175 void SetValue(DWORD *pdwValue, DWORD dwLength);
176
177 int Compare(TCHAR *pszOid);
178 int Compare(DWORD *pdwOid, DWORD dwLen);
179 };
180
181
182 //
183 // SNMP variable
184 //
185
186 class LIBNXSNMP_EXPORTABLE SNMP_Variable
187 {
188 private:
189 SNMP_ObjectId *m_pName;
190 DWORD m_dwType;
191 DWORD m_dwValueLength;
192 BYTE *m_pValue;
193
194 public:
195 SNMP_Variable();
196 SNMP_Variable(TCHAR *pszName);
197 SNMP_Variable(DWORD *pdwName, DWORD dwNameLen);
198 ~SNMP_Variable();
199
200 BOOL Parse(BYTE *pData, DWORD dwVarLength);
201 DWORD Encode(BYTE *pBuffer, DWORD dwBufferSize);
202
203 SNMP_ObjectId *GetName(void) { return m_pName; }
204 DWORD GetType(void) { return m_dwType; }
205 DWORD GetValueLength(void) { return m_dwValueLength; }
206 const BYTE *GetValue(void) { return m_pValue; }
207
208 DWORD GetValueAsUInt(void);
209 long GetValueAsInt(void);
210 TCHAR *GetValueAsString(TCHAR *pszBuffer, DWORD dwBufferSize);
211 SNMP_ObjectId *GetValueAsObjectId(void);
212 };
213
214
215 //
216 // SNMP PDU
217 //
218
219 class LIBNXSNMP_EXPORTABLE SNMP_PDU
220 {
221 private:
222 DWORD m_dwVersion;
223 DWORD m_dwCommand;
224 char *m_pszCommunity;
225 DWORD m_dwNumVariables;
226 SNMP_Variable **m_ppVarList;
227 SNMP_ObjectId *m_pEnterprise;
228 int m_iTrapType;
229 int m_iSpecificTrap;
230 DWORD m_dwTimeStamp;
231 DWORD m_dwAgentAddr;
232 DWORD m_dwRqId;
233 DWORD m_dwErrorCode;
234 DWORD m_dwErrorIndex;
235
236 BOOL ParseVariable(BYTE *pData, DWORD dwVarLength);
237 BOOL ParseVarBinds(BYTE *pData, DWORD dwPDULength);
238 BOOL ParsePDU(BYTE *pData, DWORD dwPDULength);
239 BOOL ParseTrapPDU(BYTE *pData, DWORD dwPDULength);
240 BOOL ParseTrap2PDU(BYTE *pData, DWORD dwPDULength);
241
242 public:
243 SNMP_PDU();
244 SNMP_PDU(DWORD dwCommand, char *pszCommunity, DWORD dwRqId, DWORD dwVersion = SNMP_VERSION_2C);
245 ~SNMP_PDU();
246
247 BOOL Parse(BYTE *pRawData, DWORD dwRawLength);
248 DWORD Encode(BYTE **ppBuffer);
249
250 DWORD GetCommand(void) { return m_dwCommand; }
251 SNMP_ObjectId *GetTrapId(void) { return m_pEnterprise; }
252 int GetTrapType(void) { return m_iTrapType; }
253 int GetSpecificTrapType(void) { return m_iSpecificTrap; }
254 DWORD GetNumVariables(void) { return m_dwNumVariables; }
255 SNMP_Variable *GetVariable(DWORD dwIndex) { return (dwIndex < m_dwNumVariables) ? m_ppVarList[dwIndex] : NULL; }
256 const char *GetCommunity(void) { return m_pszCommunity; }
257 DWORD GetVersion(void) { return m_dwVersion; }
258 DWORD GetErrorCode(void) { return m_dwErrorCode; }
259
260 DWORD GetRequestId(void) { return m_dwRqId; }
261 void SetRequestId(DWORD dwId) { m_dwRqId = dwId; }
262
263 void BindVariable(SNMP_Variable *pVar);
264 };
265
266
267 //
268 // SNMP transport
269 //
270
271 class LIBNXSNMP_EXPORTABLE SNMP_Transport
272 {
273 private:
274 SOCKET m_hSocket;
275 DWORD m_dwBufferSize;
276 DWORD m_dwBytesInBuffer;
277 DWORD m_dwBufferPos;
278 BYTE *m_pBuffer;
279
280 DWORD PreParsePDU(void);
281 int RecvData(DWORD dwTimeout, struct sockaddr *pSender, socklen_t *piAddrSize);
282 void ClearBuffer(void);
283
284 public:
285 SNMP_Transport();
286 SNMP_Transport(SOCKET hSocket);
287 ~SNMP_Transport();
288
289 int Read(SNMP_PDU **ppData, DWORD dwTimeout = INFINITE,
290 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL);
291 int Send(SNMP_PDU *pPDU, struct sockaddr *pRcpt = NULL, socklen_t iAddrLen = 0);
292 DWORD DoRequest(SNMP_PDU *pRequest, SNMP_PDU **pResponce,
293 DWORD dwTimeout = INFINITE, DWORD dwNumRetries = 1);
294
295 DWORD CreateUDPTransport(TCHAR *pszHostName, DWORD dwHostAddr = 0, WORD wPort = 161);
296 };
297
298
299 //
300 // Functions
301 //
302
303 void LIBNXSNMP_EXPORTABLE SNMPConvertOIDToText(DWORD dwLength, DWORD *pdwValue, TCHAR *pszBuffer, DWORD dwBufferSize);
304 DWORD LIBNXSNMP_EXPORTABLE SNMPParseOID(const TCHAR *pszText, DWORD *pdwBuffer, DWORD dwBufferSize);
305 BOOL LIBNXSNMP_EXPORTABLE SNMPIsCorrectOID(const TCHAR *pszText);
306 const TCHAR LIBNXSNMP_EXPORTABLE *SNMPGetErrorText(DWORD dwError);
307
308
309 #endif