d8a672bccc18d4a40b5ac21ff223eddad5eaa99d
[public/netxms.git] / src / libnxcl / main.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2003-2013 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: main.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26 /**
27 * Global variables
28 */
29 NXC_DEBUG_CALLBACK g_pDebugCallBack = NULL;
30
31 /**
32 * Print debug messages
33 */
34 void DebugPrintf(const TCHAR *format, ...)
35 {
36 if (g_pDebugCallBack == NULL)
37 return;
38
39 va_list args;
40 TCHAR buffer[4096];
41
42 va_start(args, format);
43 _vsntprintf(buffer, 4096, format, args);
44 va_end(args);
45 g_pDebugCallBack(buffer);
46 }
47
48 /**
49 * Debug callback for crypto lib
50 */
51 static void CryptoLibDebugCallback(int level, const TCHAR *format, va_list args)
52 {
53 if (g_pDebugCallBack == NULL)
54 return;
55
56 TCHAR buffer[4096];
57 _vsntprintf(buffer, 4096, format, args);
58 g_pDebugCallBack(buffer);
59 }
60
61 /**
62 * Initialization function
63 */
64 BOOL LIBNXCL_EXPORTABLE NXCInitialize()
65 {
66 return InitCryptoLib(0xFFFF, CryptoLibDebugCallback);
67 }
68
69 /**
70 * Shutdown function
71 */
72 void LIBNXCL_EXPORTABLE NXCShutdown()
73 {
74 }
75
76 /**
77 * Get library version
78 */
79 UINT32 LIBNXCL_EXPORTABLE NXCGetVersion()
80 {
81 return (NETXMS_VERSION_MAJOR << 24) | (NETXMS_VERSION_MINOR << 16) | NETXMS_VERSION_BUILD;
82 }
83
84 /**
85 * Set event handler
86 */
87 void LIBNXCL_EXPORTABLE NXCSetEventHandler(NXC_SESSION hSession, NXC_EVENT_HANDLER pHandler)
88 {
89 ((NXCL_Session *)hSession)->m_pEventHandler = pHandler;
90 }
91
92 /**
93 * Set callback for debug messages
94 */
95 void LIBNXCL_EXPORTABLE NXCSetDebugCallback(NXC_DEBUG_CALLBACK pFunc)
96 {
97 g_pDebugCallBack = pFunc;
98 }
99
100 /**
101 * Set command timeout
102 */
103 void LIBNXCL_EXPORTABLE NXCSetCommandTimeout(NXC_SESSION hSession, UINT32 dwTimeout)
104 {
105 if ((dwTimeout >= 1000) && (dwTimeout <= 60000))
106 ((NXCL_Session *)hSession)->m_dwCommandTimeout = dwTimeout;
107 }
108
109 /**
110 * Get server ID
111 */
112 void LIBNXCL_EXPORTABLE NXCGetServerID(NXC_SESSION hSession, BYTE *pbsId)
113 {
114 memcpy(pbsId, ((NXCL_Session *)hSession)->m_bsServerId, 8);
115 }
116
117 /**
118 * Subscribe to channel
119 */
120 UINT32 LIBNXCL_EXPORTABLE NXCSubscribe(NXC_SESSION hSession, UINT32 dwChannels)
121 {
122 return ((NXCL_Session *)hSession)->SetSubscriptionStatus(dwChannels, 1);
123 }
124
125 /**
126 * Unsubscribe from channel
127 */
128 UINT32 LIBNXCL_EXPORTABLE NXCUnsubscribe(NXC_SESSION hSession, UINT32 dwChannels)
129 {
130 return ((NXCL_Session *)hSession)->SetSubscriptionStatus(dwChannels, 0);
131 }
132
133 /**
134 * Set client data
135 */
136 void LIBNXCL_EXPORTABLE NXCSetClientData(NXC_SESSION hSession, void *pData)
137 {
138 if (hSession != NULL)
139 ((NXCL_Session *)hSession)->setClientData(pData);
140 }
141
142 /**
143 * Get client data
144 */
145 void LIBNXCL_EXPORTABLE *NXCGetClientData(NXC_SESSION hSession)
146 {
147 return (hSession != NULL) ? ((NXCL_Session *)hSession)->getClientData() : NULL;
148 }
149
150 /**
151 * Check if password needs to be changed
152 */
153 BOOL LIBNXCL_EXPORTABLE NXCNeedPasswordChange(NXC_SESSION hSession)
154 {
155 return ((NXCL_Session *)hSession)->needPasswordChange();
156 }
157
158 /**
159 * Check if server has problems with backend database connection
160 */
161 BOOL LIBNXCL_EXPORTABLE NXCIsDBConnLost(NXC_SESSION hSession)
162 {
163 return ((NXCL_Session *)hSession)->isDBConnLost();
164 }
165
166 /**
167 * Check if password needs to be changed
168 */
169 void LIBNXCL_EXPORTABLE NXCStartWatchdog(NXC_SESSION hSession)
170 {
171 ((NXCL_Session *)hSession)->StartWatchdogThread();
172 }
173
174 /**
175 * Get last lock information (owner of already locked component)
176 */
177 void LIBNXCL_EXPORTABLE NXCGetLastLockOwner(NXC_SESSION hSession, TCHAR *pszBuffer,
178 int nBufSize)
179 {
180 if (hSession != NULL)
181 nx_strncpy(pszBuffer, ((NXCL_Session *)hSession)->getLastLock(), nBufSize);
182 else
183 nx_strncpy(pszBuffer, _T("INVALID SESSION HANDLE"), nBufSize);
184 }
185
186 /**
187 * Send SMS via server
188 */
189 UINT32 LIBNXCL_EXPORTABLE NXCSendSMS(NXC_SESSION hSession, TCHAR *phone, TCHAR *message)
190 {
191 CSCPMessage msg;
192 UINT32 dwRqId;
193
194 CHECK_SESSION_HANDLE();
195
196 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
197
198 msg.SetCode(CMD_SEND_SMS);
199 msg.SetId(dwRqId);
200 msg.SetVariable(VID_RCPT_ADDR, phone);
201 msg.SetVariable(VID_MESSAGE, message);
202 ((NXCL_Session *)hSession)->SendMsg(&msg);
203
204 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
205 }
206
207 /**
208 * Check connection status by sending keepalive message
209 */
210 UINT32 LIBNXCL_EXPORTABLE NXCCheckConnection(NXC_SESSION hSession)
211 {
212 CSCPMessage msg;
213 UINT32 dwRqId;
214
215 CHECK_SESSION_HANDLE();
216
217 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
218
219 msg.SetCode(CMD_KEEPALIVE);
220 msg.SetId(dwRqId);
221 if (!((NXCL_Session *)hSession)->SendMsg(&msg))
222 return RCC_COMM_FAILURE;
223
224 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
225 }
226
227 /**
228 * Get server's time zone
229 */
230 const TCHAR LIBNXCL_EXPORTABLE *NXCGetServerTimeZone(NXC_SESSION hSession)
231 {
232 const TCHAR *ptr;
233
234 ptr = ((NXCL_Session *)hSession)->getServerTimeZone();
235 return (*ptr == 0) ? NULL : ptr;
236 }
237
238 /**
239 * Generate Message ID
240 */
241 UINT32 LIBNXCL_EXPORTABLE NXCGenerateMessageId(NXC_SESSION hSession)
242 {
243 return ((NXCL_Session *)hSession)->CreateRqId();
244 }
245
246 /**
247 * Send prepared NXCP message
248 */
249 BOOL LIBNXCL_EXPORTABLE NXCSendMessage(NXC_SESSION hSession, CSCPMessage *msg)
250 {
251 CHECK_SESSION_HANDLE();
252 return ((NXCL_Session *)hSession)->SendMsg(msg);
253 }
254
255 /**
256 * Wait for message
257 */
258 CSCPMessage LIBNXCL_EXPORTABLE *NXCWaitForMessage(NXC_SESSION hSession, WORD wCode, UINT32 dwRqId)
259 {
260 return (hSession != NULL) ? ((NXCL_Session *)hSession)->WaitForMessage(wCode, dwRqId) : NULL;
261 }
262
263 /**
264 * Wait for CMD_REQUEST_COMPLETED message and return value of VID_RCC
265 */
266 UINT32 LIBNXCL_EXPORTABLE NXCWaitForRCC(NXC_SESSION hSession, UINT32 dwRqId)
267 {
268 CHECK_SESSION_HANDLE();
269 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
270 }
271
272 /**
273 * Get text for error
274 */
275 const TCHAR LIBNXCL_EXPORTABLE *NXCGetErrorText(UINT32 dwError)
276 {
277 static const TCHAR *pszErrorText[] =
278 {
279 _T("Request completed successfully"),
280 _T("Component locked"),
281 _T("Access denied"),
282 _T("Invalid request"),
283 _T("Request timed out"),
284 _T("Request is out of state"),
285 _T("Database failure"),
286 _T("Invalid object ID"),
287 _T("Object already exist"),
288 _T("Communication failure"),
289 _T("System failure"),
290 _T("Invalid user ID"),
291 _T("Invalid argument"),
292 _T("Duplicate DCI"),
293 _T("Invalid DCI ID"),
294 _T("Out of memory"),
295 _T("Input/Output error"),
296 _T("Incompatible operation"),
297 _T("Object creation failed"),
298 _T("Loop in object relationship detected"),
299 _T("Invalid object name"),
300 _T("Invalid alarm ID"),
301 _T("Invalid action ID"),
302 _T("Operation in progress"),
303 _T("Copy operation failed for one or more DCI(s)"),
304 _T("Invalid or unknown event code"),
305 _T("No interfaces suitable for sending magic packet"),
306 _T("No MAC address on interface"),
307 _T("Command not implemented"),
308 _T("Invalid trap configuration record ID"),
309 _T("Requested data collection item is not supported by agent"),
310 _T("Client and server versions mismatch"),
311 _T("Error parsing package information file"),
312 _T("Package with specified properties already installed on server"),
313 _T("Package file already exist on server"),
314 _T("Server resource busy"),
315 _T("Invalid package ID"),
316 _T("Invalid IP address"),
317 _T("Action is used in event processing policy"),
318 _T("Variable not found"),
319 _T("Server uses incompatible version of communication protocol"),
320 _T("Address already in use"),
321 _T("Unable to select cipher"),
322 _T("Invalid public key"),
323 _T("Invalid session key"),
324 _T("Encryption is not supported by peer"),
325 _T("Server internal error"),
326 _T("Execution of external command failed"),
327 _T("Invalid object tool ID"),
328 _T("SNMP protocol error"),
329 _T("Incorrect regular expression"),
330 _T("Parameter is not supported by agent"),
331 _T("File I/O operation failed"),
332 _T("MIB file is corrupted"),
333 _T("File transfer operation already in progress"),
334 _T("Invalid job ID"),
335 _T("Invalid script ID"),
336 _T("Invalid script name"),
337 _T("Unknown map name"),
338 _T("Invalid map ID"),
339 _T("Account disabled"),
340 _T("No more grace logins"),
341 _T("Server connection broken"),
342 _T("Invalid agent configuration ID"),
343 _T("Server has lost connection with backend database"),
344 _T("Alarm is still open in helpdesk system"),
345 _T("Alarm is not in \"outstanding\" state"),
346 _T("DCI data source is not a push agent"),
347 _T("Error parsing configuration import file"),
348 _T("Configuration cannot be imported because of validation errors"),
349 _T("Invalid graph ID"),
350 _T("Local cryptographic provider failure"),
351 _T("Unsupported authentication type"),
352 _T("Bad certificate"),
353 _T("Invalid certificate ID"),
354 _T("SNMP failure"),
355 _T("Node has no support for layer 2 topology discovery"),
356 _T("Invalid situation ID"),
357 _T("Named instance not found"),
358 _T("Invalid event ID"),
359 _T("Operation cannot be completed due to agent error"),
360 _T("Unknown variable"),
361 _T("Requested resource not available"),
362 _T("Job cannot be cancelled"),
363 _T("Invalid policy ID"),
364 _T("Unknown log name"),
365 _T("Invalid log handle"),
366 _T("New password is too weak"),
367 _T("Password was used before"),
368 _T("Invalid session handle"),
369 _T("Node already is a member of a cluster"),
370 _T("Job cannot be hold"),
371 _T("Job cannot be unhold"),
372 _T("Zone ID is already in use"),
373 _T("Invalid zone ID"),
374 _T("Cannot delete non-empty zone object"),
375 _T("No physical component data"),
376 _T("Invalid alarm note ID"),
377 _T("Encryption error"),
378 _T("Invalid mapping table ID"),
379 _T("No software package data"),
380 _T("Invalid DCI summary table ID"),
381 _T("User is logged in"),
382 _T("Error parsing XML"),
383 _T("SQL query cost is too high"),
384 _T("License violation"),
385 _T("Number of client licenses exceeded"),
386 _T("Object already exist"),
387 _T("No helpdesk link"),
388 _T("Helpdesk link communication failure"),
389 _T("Helpdesk link access denied"),
390 _T("Helpdesk link internal error"),
391 _T("LDAP connection error"),
392 _T("Routing table unavailable")
393 };
394 return (dwError <= RCC_NO_ROUTING_TABLE) ? pszErrorText[dwError] : _T("No text message for this error");
395 }
396
397 #if defined(_WIN32) && !defined(UNDER_CE)
398
399 /**
400 * DLL entry point
401 */
402 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
403 {
404 if (dwReason == DLL_PROCESS_ATTACH)
405 DisableThreadLibraryCalls(hInstance);
406 return TRUE;
407 }
408
409 #endif /* _WIN32 */