license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / libnxcl / snmp.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: snmp.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Do SNMP walk
29 //
30
31 DWORD LIBNXCL_EXPORTABLE NXCSnmpWalk(NXC_SESSION hSession, DWORD dwNode,
32 TCHAR *pszRootOID, void *pUserData,
33 void (* pfCallback)(TCHAR *, DWORD, TCHAR *, void *))
34 {
35 CSCPMessage msg, *pData;
36 DWORD i, dwNumVars, dwRetCode, dwRqId, dwId, dwType;
37 TCHAR szVarName[4096], szValue[4096];
38 BOOL bStop = FALSE;
39
40 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
41
42 msg.SetCode(CMD_START_SNMP_WALK);
43 msg.SetId(dwRqId);
44 msg.SetVariable(VID_SNMP_OID, pszRootOID);
45 msg.SetVariable(VID_OBJECT_ID, dwNode);
46 ((NXCL_Session *)hSession)->SendMsg(&msg);
47
48 dwRetCode = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
49 if (dwRetCode == RCC_SUCCESS)
50 {
51 do
52 {
53 pData = ((NXCL_Session *)hSession)->WaitForMessage(CMD_SNMP_WALK_DATA, dwRqId);
54 if (pData != NULL)
55 {
56 dwNumVars = pData->GetVariableLong(VID_NUM_VARIABLES);
57 for(i = 0, dwId = VID_SNMP_WALKER_DATA_BASE; i < dwNumVars; i++)
58 {
59 pData->GetVariableStr(dwId++, szVarName, 4096);
60 dwType = pData->GetVariableLong(dwId++);
61 pData->GetVariableStr(dwId++, szValue, 4096);
62 pfCallback(szVarName, dwType, szValue, pUserData);
63 }
64 bStop = pData->IsEndOfSequence();
65 delete pData;
66 }
67 else
68 {
69 dwRetCode = RCC_TIMEOUT;
70 }
71 } while((dwRetCode == RCC_SUCCESS) && (!bStop));
72 }
73
74 return dwRetCode;
75 }
76
77
78 //
79 // Set SNMP variable
80 //
81
82 DWORD LIBNXCL_EXPORTABLE NXCSnmpSet(NXC_SESSION hSession, DWORD dwNode,
83 TCHAR *pszVarName, DWORD dwType, void *pValue)
84 {
85 return 0;
86 }
87
88
89 //
90 // Get list of community strings
91 //
92
93 DWORD LIBNXCL_EXPORTABLE NXCGetSnmpCommunityList(NXC_SESSION hSession, DWORD *pdwNumStrings,
94 TCHAR ***pppszStringList)
95 {
96 CSCPMessage msg, *pResponse;
97 DWORD i, count, dwRetCode, dwRqId, id;
98
99 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
100
101 msg.SetCode(CMD_GET_COMMUNITY_LIST);
102 msg.SetId(dwRqId);
103 ((NXCL_Session *)hSession)->SendMsg(&msg);
104
105 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
106 if (pResponse != NULL)
107 {
108 dwRetCode = pResponse->GetVariableLong(VID_RCC);
109 if (dwRetCode == RCC_SUCCESS)
110 {
111 count = pResponse->GetVariableLong(VID_NUM_STRINGS);
112 *pdwNumStrings = count;
113 if (count > 0)
114 {
115 *pppszStringList = (TCHAR **)malloc(sizeof(TCHAR *) * count);
116 for(i = 0, id = VID_STRING_LIST_BASE; i < count; i++)
117 {
118 (*pppszStringList)[i] = pResponse->GetVariableStr(id++);
119 }
120 }
121 else
122 {
123 *pppszStringList = NULL;
124 }
125 }
126 delete pResponse;
127 }
128 else
129 {
130 dwRetCode = RCC_TIMEOUT;
131 }
132 return dwRetCode;
133 }
134
135
136 //
137 // Update list of community strings
138 //
139
140 DWORD LIBNXCL_EXPORTABLE NXCUpdateSnmpCommunityList(NXC_SESSION hSession, DWORD dwNumStrings,
141 TCHAR **ppszStringList)
142 {
143 CSCPMessage msg;
144 DWORD i, id, dwRqId;
145
146 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
147
148 msg.SetCode(CMD_UPDATE_COMMUNITY_LIST);
149 msg.SetId(dwRqId);
150
151 msg.SetVariable(VID_NUM_STRINGS, dwNumStrings);
152 for(i = 0, id = VID_STRING_LIST_BASE; i < dwNumStrings; i++)
153 {
154 msg.SetVariable(id++, ppszStringList[i]);
155 }
156
157 ((NXCL_Session *)hSession)->SendMsg(&msg);
158
159 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
160 }
161
162
163 //
164 // Get list of USM credentials
165 //
166
167 DWORD LIBNXCL_EXPORTABLE NXCGetSnmpUsmCredentials(NXC_SESSION hSession, DWORD *listSize, NXC_SNMP_USM_CRED **list)
168 {
169 CSCPMessage msg, *pResponse;
170 DWORD i, count, dwRetCode, dwRqId, id;
171
172 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
173
174 msg.SetCode(CMD_GET_USM_CREDENTIALS);
175 msg.SetId(dwRqId);
176 ((NXCL_Session *)hSession)->SendMsg(&msg);
177
178 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
179 if (pResponse != NULL)
180 {
181 dwRetCode = pResponse->GetVariableLong(VID_RCC);
182 if (dwRetCode == RCC_SUCCESS)
183 {
184 count = pResponse->GetVariableLong(VID_NUM_RECORDS);
185 *listSize = count;
186 if (count > 0)
187 {
188 *list = (NXC_SNMP_USM_CRED *)malloc(sizeof(NXC_SNMP_USM_CRED) * count);
189 NXC_SNMP_USM_CRED *curr = *list;
190 for(i = 0, id = VID_USM_CRED_LIST_BASE; i < count; i++, id += 5, curr++)
191 {
192 pResponse->GetVariableStr(id++, curr->name, MAX_DB_STRING);
193 curr->authMethod = (int)pResponse->GetVariableShort(id++);
194 curr->privMethod = (int)pResponse->GetVariableShort(id++);
195 pResponse->GetVariableStr(id++, curr->authPassword, MAX_DB_STRING);
196 pResponse->GetVariableStr(id++, curr->privPassword, MAX_DB_STRING);
197 }
198 }
199 else
200 {
201 *list = NULL;
202 }
203 }
204 delete pResponse;
205 }
206 else
207 {
208 dwRetCode = RCC_TIMEOUT;
209 }
210 return dwRetCode;
211 }
212
213
214 //
215 // Update list of USM credentials
216 //
217
218 DWORD LIBNXCL_EXPORTABLE NXCUpdateSnmpUsmCredentials(NXC_SESSION hSession, DWORD count, NXC_SNMP_USM_CRED *list)
219 {
220 CSCPMessage msg;
221 DWORD i, id, dwRqId;
222
223 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
224
225 msg.SetCode(CMD_UPDATE_USM_CREDENTIALS);
226 msg.SetId(dwRqId);
227
228 msg.SetVariable(VID_NUM_RECORDS, count);
229 for(i = 0, id = VID_USM_CRED_LIST_BASE; i < count; i++, id += 5)
230 {
231 msg.SetVariable(id++, list[i].name);
232 msg.SetVariable(id++, (WORD)list[i].authMethod);
233 msg.SetVariable(id++, (WORD)list[i].privMethod);
234 msg.SetVariable(id++, list[i].authPassword);
235 msg.SetVariable(id++, list[i].privPassword);
236 }
237
238 ((NXCL_Session *)hSession)->SendMsg(&msg);
239
240 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
241 }