libnxsrv and nxcore moved to VS2005 (nxcore cannot be linked because libnxsl not...
[public/netxms.git] / src / server / include / nxsrvapi.h
1 /*
2 ** NetXMS - Network Management System
3 ** Server Library
4 ** Copyright (C) 2003, 2004, 2005, 2006, 2007 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 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 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: nxsrvapi.h
21 **
22 **/
23
24 #ifndef _nxsrvapi_h_
25 #define _nxsrvapi_h_
26
27 #ifdef _WIN32
28 #ifdef LIBNXSRV_EXPORTS
29 #define LIBNXSRV_EXPORTABLE __declspec(dllexport)
30 #else
31 #define LIBNXSRV_EXPORTABLE __declspec(dllimport)
32 #endif
33 #else /* _WIN32 */
34 #define LIBNXSRV_EXPORTABLE
35 #endif
36
37 #ifndef LIBNXCL_NO_DECLARATIONS
38 #define LIBNXCL_NO_DECLARATIONS 1
39 #endif
40 #include <nxclapi.h>
41 #include <nxcpapi.h>
42 #include <nms_agent.h>
43 #include "../libnxsrv/messages.h"
44 #include <dbdrv.h>
45 #include <nxsnmp.h>
46 #include <netxms_isc.h>
47
48
49 //
50 // Default files
51 //
52
53 #ifdef _WIN32
54
55 # define DEFAULT_CONFIG_FILE _T("C:\\netxmsd.conf")
56
57 # define DEFAULT_SHELL "cmd.exe"
58 # define DEFAULT_LOG_FILE "C:\\NetXMS.log"
59 # define DEFAULT_DATA_DIR "C:\\NetXMS\\var"
60 # define DEFAULT_DUMP_DIR "C:\\"
61
62 # define DDIR_MIBS "\\mibs"
63 # define DDIR_IMAGES "\\images"
64 # define DDIR_PACKAGES "\\packages"
65 # define DDIR_BACKGROUNDS "\\backgrounds"
66 # define DDIR_SHARED_FILES "\\shared"
67 # define DFILE_KEYS "\\server_key"
68 # define DFILE_COMPILED_MIB "\\mibs\\netxms.mib"
69
70 #else /* _WIN32 */
71
72 # define DEFAULT_CONFIG_FILE _T("{search}")
73
74 # define DEFAULT_SHELL "/bin/sh"
75
76 # ifndef DATADIR
77 # define DATADIR "/var/netxms"
78 # endif
79
80 # define DEFAULT_LOG_FILE DATADIR"/log/netxmsd.log"
81 # define DEFAULT_DATA_DIR DATADIR
82 # define DEFAULT_DUMP_DIR "/"
83
84 # define DDIR_MIBS "/mibs"
85 # define DDIR_IMAGES "/images"
86 # define DDIR_PACKAGES "/packages"
87 # define DDIR_BACKGROUNDS "/backgrounds"
88 # define DDIR_SHARED_FILES "/shared"
89 # define DFILE_KEYS "/.server_key"
90 # define DFILE_COMPILED_MIB "/mibs/netxms.mib"
91
92 #endif /* _WIN32 */
93
94
95 //
96 // Application flags
97 //
98
99 #define AF_DAEMON 0x00000001
100 #define AF_USE_SYSLOG 0x00000002
101 #define AF_ENABLE_NETWORK_DISCOVERY 0x00000004
102 #define AF_ACTIVE_NETWORK_DISCOVERY 0x00000008
103 #define AF_LOG_SQL_ERRORS 0x00000010
104 #define AF_DELETE_EMPTY_SUBNETS 0x00000020
105 #define AF_ENABLE_SNMP_TRAPD 0x00000040
106 #define AF_ENABLE_ZONING 0x00000080
107 #define AF_SYNC_NODE_NAMES_WITH_DNS 0x00000100
108 #define AF_CHECK_TRUSTED_NODES 0x00000200
109 #define AF_RESOLVE_NODE_NAMES 0x00100000
110 #define AF_CATCH_EXCEPTIONS 0x00200000
111 #define AF_INTERNAL_CA 0x00400000
112 #define AF_DB_LOCKED 0x01000000
113 #define AF_ENABLE_MULTIPLE_DB_CONN 0x02000000
114 #define AF_DB_CONNECTION_LOST 0x04000000
115 #define AF_NO_NETWORK_CONNECTIVITY 0x08000000
116 #define AF_EVENT_STORM_DETECTED 0x08000000
117 #define AF_SERVER_INITIALIZED 0x40000000
118 #define AF_SHUTDOWN 0x80000000
119
120 #define IsStandalone() (!(g_dwFlags & AF_DAEMON))
121 #define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
122
123
124 //
125 // Encryption usage policies
126 //
127
128 #define ENCRYPTION_DISABLED 0
129 #define ENCRYPTION_ALLOWED 1
130 #define ENCRYPTION_PREFERRED 2
131 #define ENCRYPTION_REQUIRED 3
132
133
134 //
135 // DB-related constants
136 //
137
138 #define MAX_DB_LOGIN 64
139 #define MAX_DB_PASSWORD 64
140 #define MAX_DB_NAME 256
141
142
143 //
144 // DB events
145 //
146
147 #define DBEVENT_CONNECTION_LOST 0
148 #define DBEVENT_CONNECTION_RESTORED 1
149 #define DBEVENT_QUERY_FAILED 2
150
151
152 //
153 // Database connection structure
154 //
155
156 struct db_handle_t
157 {
158 DB_CONNECTION hConn;
159 MUTEX mutexTransLock; // Transaction lock
160 int nTransactionLevel;
161 TCHAR *pszServer;
162 TCHAR *pszLogin;
163 TCHAR *pszPassword;
164 TCHAR *pszDBName;
165 };
166 typedef db_handle_t * DB_HANDLE;
167
168
169 //
170 // Win32 service and syslog constants
171 //
172
173 #ifdef _WIN32
174
175 #define CORE_SERVICE_NAME _T("NetXMSCore")
176 #define CORE_EVENT_SOURCE _T("NetXMSCore")
177 #define NETXMSD_SYSLOG_NAME CORE_EVENT_SOURCE
178
179 #else
180
181 #define NETXMSD_SYSLOG_NAME _T("netxmsd")
182
183 #endif /* _WIN32 */
184
185
186 //
187 // Single ARP cache entry
188 //
189
190 typedef struct
191 {
192 DWORD dwIndex; // Interface index
193 DWORD dwIpAddr;
194 BYTE bMacAddr[MAC_ADDR_LENGTH];
195 } ARP_ENTRY;
196
197
198 //
199 // ARP cache structure used by discovery functions and AgentConnection class
200 //
201
202 typedef struct
203 {
204 DWORD dwNumEntries;
205 ARP_ENTRY *pEntries;
206 } ARP_CACHE;
207
208
209 //
210 // Interface information structure used by discovery functions and AgentConnection class
211 //
212
213 typedef struct
214 {
215 TCHAR szName[MAX_DB_STRING];
216 DWORD dwIndex;
217 DWORD dwType;
218 DWORD dwIpAddr;
219 DWORD dwIpNetMask;
220 BYTE bMacAddr[MAC_ADDR_LENGTH];
221 int iNumSecondary; // Number of secondary IP's on this interface
222 } INTERFACE_INFO;
223
224
225 //
226 // Interface list used by discovery functions and AgentConnection class
227 //
228
229 typedef struct
230 {
231 int iNumEntries; // Number of entries in pInterfaces
232 int iEnumPos; // Used by index enumeration handler
233 void *pArg; // Can be used by custom enumeration handlers
234 INTERFACE_INFO *pInterfaces; // Interface entries
235 } INTERFACE_LIST;
236
237
238 //
239 // Route information
240 //
241
242 typedef struct
243 {
244 DWORD dwDestAddr;
245 DWORD dwDestMask;
246 DWORD dwNextHop;
247 DWORD dwIfIndex;
248 DWORD dwRouteType;
249 } ROUTE;
250
251
252 //
253 // Routing table
254 //
255
256 typedef struct
257 {
258 int iNumEntries; // Number of entries
259 ROUTE *pRoutes; // Route list
260 } ROUTING_TABLE;
261
262
263 //
264 // Agent connection
265 //
266
267 class LIBNXSRV_EXPORTABLE AgentConnection
268 {
269 private:
270 DWORD m_dwAddr;
271 int m_nProtocolVersion;
272 int m_iAuthMethod;
273 TCHAR m_szSecret[MAX_SECRET_LENGTH];
274 time_t m_tLastCommandTime;
275 SOCKET m_hSocket;
276 DWORD m_dwNumDataLines;
277 DWORD m_dwRequestId;
278 DWORD m_dwCommandTimeout;
279 DWORD m_dwRecvTimeout;
280 TCHAR **m_ppDataLines;
281 MsgWaitQueue *m_pMsgWaitQueue;
282 BOOL m_bIsConnected;
283 MUTEX m_mutexDataLock;
284 THREAD m_hReceiverThread;
285 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
286 int m_iEncryptionPolicy;
287 BOOL m_bUseProxy;
288 DWORD m_dwProxyAddr;
289 WORD m_wPort;
290 WORD m_wProxyPort;
291 int m_iProxyAuth;
292 TCHAR m_szProxySecret[MAX_SECRET_LENGTH];
293 int m_hCurrFile;
294 DWORD m_dwUploadRequestId;
295 CONDITION m_condFileUpload;
296 BOOL m_fileUploadSucceeded;
297
298 void ReceiverThread(void);
299 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
300
301 protected:
302 void DestroyResultData(void);
303 BOOL SendMessage(CSCPMessage *pMsg);
304 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut) { return m_pMsgWaitQueue->WaitForMessage(wCode, dwId, dwTimeOut); }
305 DWORD WaitForRCC(DWORD dwRqId, DWORD dwTimeOut);
306 DWORD SetupEncryption(RSA *pServerKey);
307 DWORD Authenticate(BOOL bProxyData);
308 DWORD SetupProxyConnection(void);
309 DWORD GetIpAddr(void) { return ntohl(m_dwAddr); }
310 DWORD PrepareFileUpload(const TCHAR *fileName, DWORD rqId);
311
312 virtual void PrintMsg(const TCHAR *pszFormat, ...);
313 virtual void OnTrap(CSCPMessage *pMsg);
314 virtual void OnFileUpload(BOOL success);
315
316 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
317 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
318
319 public:
320 AgentConnection();
321 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
322 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
323 virtual ~AgentConnection();
324
325 BOOL Connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, DWORD *pdwError = NULL);
326 void Disconnect(void);
327 BOOL IsConnected(void) { return m_bIsConnected; }
328 int GetProtocolVersion(void) { return m_nProtocolVersion; }
329
330 ARP_CACHE *GetArpCache(void);
331 INTERFACE_LIST *GetInterfaceList(void);
332 ROUTING_TABLE *GetRoutingTable(void);
333 DWORD GetParameter(const TCHAR *pszParam, DWORD dwBufSize, TCHAR *pszBuffer);
334 DWORD GetList(const TCHAR *pszParam);
335 DWORD Nop(void);
336 DWORD ExecAction(const TCHAR *pszAction, int argc, TCHAR **argv);
337 DWORD UploadFile(const TCHAR *pszFile);
338 DWORD StartUpgrade(const TCHAR *pszPkgName);
339 DWORD CheckNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType, WORD wPort = 0,
340 WORD wProto = 0, const TCHAR *pszRequest = NULL, const TCHAR *pszResponse = NULL);
341 DWORD GetSupportedParameters(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList);
342 DWORD GetConfigFile(TCHAR **ppszConfig, DWORD *pdwSize);
343 DWORD UpdateConfigFile(const TCHAR *pszConfig);
344 DWORD EnableTraps(void);
345 CSCPMessage *CustomRequest(CSCPMessage *pRequest, const TCHAR *recvFile = NULL);
346
347 DWORD GetNumDataLines(void) { return m_dwNumDataLines; }
348 const TCHAR *GetDataLine(DWORD dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
349
350 void SetCommandTimeout(DWORD dwTimeout) { m_dwCommandTimeout = max(dwTimeout, 500); }
351 void SetRecvTimeout(DWORD dwTimeout) { m_dwRecvTimeout = max(dwTimeout, 10000); }
352 void SetEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
353 void SetProxy(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
354 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
355 void SetPort(WORD wPort) { m_wPort = wPort; }
356 void SetAuthData(int nMethod, const char *pszSecret) { m_iAuthMethod = nMethod; strncpy(m_szSecret, pszSecret, MAX_SECRET_LENGTH); }
357 };
358
359
360 //
361 // Proxy SNMP transport
362 //
363
364 class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
365 {
366 protected:
367 AgentConnection *m_pAgentConnection;
368 CSCPMessage *m_pResponse;
369 DWORD m_dwIpAddr;
370 WORD m_wPort;
371
372 public:
373 SNMP_ProxyTransport(AgentConnection *pConn, DWORD dwIpAddr, WORD wPort);
374 virtual ~SNMP_ProxyTransport();
375
376 virtual int Read(SNMP_PDU **ppData, DWORD dwTimeout = INFINITE,
377 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL);
378 virtual int Send(SNMP_PDU *pPDU);
379 };
380
381
382 //
383 // ISC flags
384 //
385
386 #define ISCF_IS_CONNECTED ((DWORD)0x00000001)
387 #define ISCF_REQUIRE_ENCRYPTION ((DWORD)0x00000002)
388
389
390 //
391 // Inter-server connection (ISC)
392 //
393
394 class LIBNXSRV_EXPORTABLE ISC
395 {
396 private:
397 DWORD m_flags;
398 DWORD m_addr;
399 WORD m_port;
400 SOCKET m_socket;
401 int m_protocolVersion;
402 DWORD m_requestId;
403 DWORD m_recvTimeout;
404 MsgWaitQueue *m_msgWaitQueue;
405 MUTEX m_mutexDataLock;
406 THREAD m_hReceiverThread;
407 CSCP_ENCRYPTION_CONTEXT *m_ctx;
408 DWORD m_commandTimeout;
409
410 void ReceiverThread(void);
411 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
412
413 protected:
414 void DestroyResultData(void);
415 DWORD SetupEncryption(RSA *pServerKey);
416 DWORD ConnectToService(DWORD service);
417
418 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
419 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
420
421 virtual void PrintMsg(const TCHAR *format, ...);
422
423 public:
424 ISC();
425 ISC(DWORD addr, WORD port = NETXMS_ISC_PORT);
426 virtual ~ISC();
427
428 DWORD Connect(DWORD service, RSA *serverKey = NULL, BOOL requireEncryption = FALSE);
429 void Disconnect();
430
431 BOOL SendMessage(CSCPMessage *msg);
432 CSCPMessage *WaitForMessage(WORD code, DWORD id, DWORD timeOut) { return m_msgWaitQueue->WaitForMessage(code, id, timeOut); }
433 DWORD WaitForRCC(DWORD rqId, DWORD timeOut);
434
435 DWORD Nop(void);
436 };
437
438
439 //
440 // Functions
441 //
442
443 void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
444 void LIBNXSRV_EXPORTABLE DestroyInterfaceList(INTERFACE_LIST *pIfList);
445 void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
446 void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
447 const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError);
448
449 void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...);
450 void LIBNXSRV_EXPORTABLE DbgPrintf(int level, const TCHAR *format, ...);
451
452 BOOL LIBNXSRV_EXPORTABLE DBInit(BOOL bWriteLog, BOOL bLogErrors, BOOL bDumpSQL,
453 void (* fpEventHandler)(DWORD, TCHAR *));
454 DB_HANDLE LIBNXSRV_EXPORTABLE DBConnect(void);
455 DB_HANDLE LIBNXSRV_EXPORTABLE DBConnectEx(const TCHAR *pszServer, const TCHAR *pszDBName,
456 const TCHAR *pszLogin, const TCHAR *pszPassword);
457 void LIBNXSRV_EXPORTABLE DBDisconnect(DB_HANDLE hConn);
458 BOOL LIBNXSRV_EXPORTABLE DBQuery(DB_HANDLE hConn, const TCHAR *szQuery);
459 BOOL LIBNXSRV_EXPORTABLE DBQueryEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
460 DB_RESULT LIBNXSRV_EXPORTABLE DBSelect(DB_HANDLE hConn, const TCHAR *szQuery);
461 DB_RESULT LIBNXSRV_EXPORTABLE DBSelectEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
462 DB_ASYNC_RESULT LIBNXSRV_EXPORTABLE DBAsyncSelect(DB_HANDLE hConn, const TCHAR *szQuery);
463 BOOL LIBNXSRV_EXPORTABLE DBFetch(DB_ASYNC_RESULT hResult);
464 TCHAR LIBNXSRV_EXPORTABLE *DBGetField(DB_RESULT hResult, int iRow, int iColumn,
465 TCHAR *pszBuffer, int nBufLen);
466 LONG LIBNXSRV_EXPORTABLE DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
467 DWORD LIBNXSRV_EXPORTABLE DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
468 INT64 LIBNXSRV_EXPORTABLE DBGetFieldInt64(DB_RESULT hResult, int iRow, int iColumn);
469 QWORD LIBNXSRV_EXPORTABLE DBGetFieldUInt64(DB_RESULT hResult, int iRow, int iColumn);
470 double LIBNXSRV_EXPORTABLE DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn);
471 DWORD LIBNXSRV_EXPORTABLE DBGetFieldIPAddr(DB_RESULT hResult, int iRow, int iColumn);
472 BOOL LIBNXSRV_EXPORTABLE DBGetFieldByteArray(DB_RESULT hResult, int iRow, int iColumn,
473 int *pnArray, int nSize, int nDefault);
474 BOOL LIBNXSRV_EXPORTABLE DBGetFieldGUID(DB_RESULT hResult, int iRow,
475 int iColumn, uuid_t guid);
476 TCHAR LIBNXSRV_EXPORTABLE *DBGetFieldAsync(DB_ASYNC_RESULT hResult, int iColumn, TCHAR *pBuffer, int iBufSize);
477 LONG LIBNXSRV_EXPORTABLE DBGetFieldAsyncLong(DB_RESULT hResult, int iColumn);
478 DWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncULong(DB_ASYNC_RESULT hResult, int iColumn);
479 INT64 LIBNXSRV_EXPORTABLE DBGetFieldAsyncInt64(DB_RESULT hResult, int iColumn);
480 QWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncUInt64(DB_ASYNC_RESULT hResult, int iColumn);
481 double LIBNXSRV_EXPORTABLE DBGetFieldAsyncDouble(DB_RESULT hResult, int iColumn);
482 DWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncIPAddr(DB_RESULT hResult, int iColumn);
483 int LIBNXSRV_EXPORTABLE DBGetNumRows(DB_RESULT hResult);
484 void LIBNXSRV_EXPORTABLE DBFreeResult(DB_RESULT hResult);
485 void LIBNXSRV_EXPORTABLE DBFreeAsyncResult(DB_HANDLE hConn, DB_ASYNC_RESULT hResult);
486 BOOL LIBNXSRV_EXPORTABLE DBBegin(DB_HANDLE hConn);
487 BOOL LIBNXSRV_EXPORTABLE DBCommit(DB_HANDLE hConn);
488 BOOL LIBNXSRV_EXPORTABLE DBRollback(DB_HANDLE hConn);
489 void LIBNXSRV_EXPORTABLE DBUnloadDriver(void);
490
491 TCHAR LIBNXSRV_EXPORTABLE *EncodeSQLString(const TCHAR *pszIn);
492 void LIBNXSRV_EXPORTABLE DecodeSQLString(TCHAR *pszStr);
493
494 void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
495
496 const TCHAR LIBNXSRV_EXPORTABLE *ISCErrorCodeToText(DWORD code);
497
498
499 //
500 // Variables
501 //
502
503 extern DWORD LIBNXSRV_EXPORTABLE g_dwFlags;
504 extern int LIBNXSRV_EXPORTABLE g_nDebugLevel;
505 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbDriver[];
506 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbDrvParams[];
507 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbServer[];
508 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbLogin[];
509 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbPassword[];
510 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbName[];
511
512 #endif /* _nxsrvapi_h_ */