Initial version of crash dump generator on Windows
[public/netxms.git] / src / server / include / nxsrvapi.h
1 /*
2 ** NetXMS - Network Management System
3 ** Server Library
4 ** Copyright (C) 2003, 2004, 2005, 2006 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 <messages.h>
44 #include <dbdrv.h>
45 #include <nxsnmp.h>
46
47
48 //
49 // Default files
50 //
51
52 #ifdef _WIN32
53
54 # define DEFAULT_CONFIG_FILE _T("C:\\netxmsd.conf")
55
56 # define DEFAULT_SHELL "cmd.exe"
57 # define DEFAULT_LOG_FILE "C:\\NetXMS.log"
58 # define DEFAULT_DATA_DIR "C:\\NetXMS\\var"
59 # define DEFAULT_DUMP_DIR "C:\\"
60
61 # define DDIR_MIBS "\\mibs"
62 # define DDIR_IMAGES "\\images"
63 # define DDIR_PACKAGES "\\packages"
64 # define DDIR_BACKGROUNDS "\\backgrounds"
65 # define DFILE_KEYS "\\server_key"
66 # define DFILE_COMPILED_MIB "\\mibs\\netxms.mib"
67
68 #else /* _WIN32 */
69
70 # define DEFAULT_CONFIG_FILE _T("/etc/netxmsd.conf")
71
72 # define DEFAULT_SHELL "/bin/sh"
73
74 # ifndef DATADIR
75 # define DATADIR "/var/netxms"
76 # endif
77
78 # define DEFAULT_LOG_FILE DATADIR"/log/netxmsd.log"
79 # define DEFAULT_DATA_DIR DATADIR
80 # define DEFAULT_DUMP_DIR "/"
81
82 # define DDIR_MIBS "/mibs"
83 # define DDIR_IMAGES "/images"
84 # define DDIR_PACKAGES "/packages"
85 # define DDIR_BACKGROUNDS "/backgrounds"
86 # define DFILE_KEYS "/.server_key"
87 # define DFILE_COMPILED_MIB "/mibs/netxms.mib"
88
89 #endif /* _WIN32 */
90
91
92 //
93 // Application flags
94 //
95
96 #define AF_STANDALONE 0x00000001
97 #define AF_USE_EVENT_LOG 0x00000002
98 #define AF_ENABLE_NETWORK_DISCOVERY 0x00000004
99 #define AF_ACTIVE_NETWORK_DISCOVERY 0x00000008
100 #define AF_LOG_SQL_ERRORS 0x00000010
101 #define AF_DELETE_EMPTY_SUBNETS 0x00000020
102 #define AF_ENABLE_SNMP_TRAPD 0x00000040
103 #define AF_ENABLE_ZONING 0x00000080
104 #define AF_DEBUG_EVENTS 0x00000100
105 #define AF_DEBUG_CSCP 0x00000200
106 #define AF_DEBUG_DISCOVERY 0x00000400
107 #define AF_DEBUG_DC 0x00000800
108 #define AF_DEBUG_HOUSEKEEPER 0x00001000
109 #define AF_DEBUG_LOCKS 0x00002000
110 #define AF_DEBUG_ACTIONS 0x00004000
111 #define AF_DEBUG_MISC 0x00008000
112 #define AF_DEBUG_SQL 0x00010000
113 #define AF_DEBUG_SNMP 0x00020000
114 #define AF_DEBUG_OBJECTS 0x00040000
115 #define AF_DEBUG_ALL 0x0006FF00
116 #define AF_RESOLVE_NODE_NAMES 0x00100000
117 #define AF_CATCH_EXCEPTIONS 0x00200000
118 #define AF_DB_LOCKED 0x01000000
119 #define AF_ENABLE_MULTIPLE_DB_CONN 0x02000000
120 #define AF_DB_CONNECTION_LOST 0x04000000
121 #define AF_SERVER_INITIALIZED 0x40000000
122 #define AF_SHUTDOWN 0x80000000
123
124 #define IsStandalone() (g_dwFlags & AF_STANDALONE)
125 #define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
126
127
128 //
129 // Encryption usage policies
130 //
131
132 #define ENCRYPTION_DISABLED 0
133 #define ENCRYPTION_ALLOWED 1
134 #define ENCRYPTION_PREFERRED 2
135 #define ENCRYPTION_REQUIRED 3
136
137
138 //
139 // DB-related constants
140 //
141
142 #define MAX_DB_LOGIN 64
143 #define MAX_DB_PASSWORD 64
144 #define MAX_DB_NAME 256
145
146
147 //
148 // DB events
149 //
150
151 #define DBEVENT_CONNECTION_LOST 0
152 #define DBEVENT_CONNECTION_RESTORED 1
153 #define DBEVENT_QUERY_FAILED 2
154
155
156 //
157 // Database connection structure
158 //
159
160 struct db_handle_t
161 {
162 DB_CONNECTION hConn;
163 MUTEX mutexTransLock; // Transaction lock
164 int nTransactionLevel;
165 TCHAR *pszServer;
166 TCHAR *pszLogin;
167 TCHAR *pszPassword;
168 TCHAR *pszDBName;
169 };
170 typedef db_handle_t * DB_HANDLE;
171
172
173 //
174 // Win32 service constants
175 //
176
177 #ifdef _WIN32
178
179 #define CORE_SERVICE_NAME _T("NetXMSCore")
180 #define CORE_EVENT_SOURCE _T("NetXMSCore")
181
182 #endif /* _WIN32 */
183
184
185 //
186 // Single ARP cache entry
187 //
188
189 typedef struct
190 {
191 DWORD dwIndex; // Interface index
192 DWORD dwIpAddr;
193 BYTE bMacAddr[MAC_ADDR_LENGTH];
194 } ARP_ENTRY;
195
196
197 //
198 // ARP cache structure used by discovery functions and AgentConnection class
199 //
200
201 typedef struct
202 {
203 DWORD dwNumEntries;
204 ARP_ENTRY *pEntries;
205 } ARP_CACHE;
206
207
208 //
209 // Interface information structure used by discovery functions and AgentConnection class
210 //
211
212 typedef struct
213 {
214 TCHAR szName[MAX_DB_STRING];
215 DWORD dwIndex;
216 DWORD dwType;
217 DWORD dwIpAddr;
218 DWORD dwIpNetMask;
219 BYTE bMacAddr[MAC_ADDR_LENGTH];
220 int iNumSecondary; // Number of secondary IP's on this interface
221 } INTERFACE_INFO;
222
223
224 //
225 // Interface list used by discovery functions and AgentConnection class
226 //
227
228 typedef struct
229 {
230 int iNumEntries; // Number of entries in pInterfaces
231 int iEnumPos; // Used by index enumeration handler
232 void *pArg; // Can be used by custom enumeration handlers
233 INTERFACE_INFO *pInterfaces; // Interface entries
234 } INTERFACE_LIST;
235
236
237 //
238 // Route information
239 //
240
241 typedef struct
242 {
243 DWORD dwDestAddr;
244 DWORD dwDestMask;
245 DWORD dwNextHop;
246 DWORD dwIfIndex;
247 DWORD dwRouteType;
248 } ROUTE;
249
250
251 //
252 // Routing table
253 //
254
255 typedef struct
256 {
257 int iNumEntries; // Number of entries
258 ROUTE *pRoutes; // Route list
259 } ROUTING_TABLE;
260
261
262 //
263 // Agent connection
264 //
265
266 class LIBNXSRV_EXPORTABLE AgentConnection
267 {
268 private:
269 DWORD m_dwAddr;
270 int m_nProtocolVersion;
271 int m_iAuthMethod;
272 char m_szSecret[MAX_SECRET_LENGTH];
273 time_t m_tLastCommandTime;
274 SOCKET m_hSocket;
275 DWORD m_dwNumDataLines;
276 DWORD m_dwRequestId;
277 DWORD m_dwCommandTimeout;
278 DWORD m_dwRecvTimeout;
279 TCHAR **m_ppDataLines;
280 MsgWaitQueue *m_pMsgWaitQueue;
281 BOOL m_bIsConnected;
282 MUTEX m_mutexDataLock;
283 THREAD m_hReceiverThread;
284 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
285 int m_iEncryptionPolicy;
286 BOOL m_bUseProxy;
287 DWORD m_dwProxyAddr;
288 WORD m_wPort;
289 WORD m_wProxyPort;
290 int m_iProxyAuth;
291 char m_szProxySecret[MAX_SECRET_LENGTH];
292
293 void ReceiverThread(void);
294 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
295
296 protected:
297 void DestroyResultData(void);
298 BOOL SendMessage(CSCPMessage *pMsg);
299 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut) { return m_pMsgWaitQueue->WaitForMessage(wCode, dwId, dwTimeOut); }
300 DWORD WaitForRCC(DWORD dwRqId, DWORD dwTimeOut);
301 DWORD SetupEncryption(RSA *pServerKey);
302 DWORD Authenticate(BOOL bProxyData);
303 DWORD SetupProxyConnection(void);
304 DWORD GetIpAddr(void) { return ntohl(m_dwAddr); }
305
306 virtual void PrintMsg(TCHAR *pszFormat, ...);
307 virtual void OnTrap(CSCPMessage *pMsg);
308
309 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
310 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
311
312 public:
313 AgentConnection();
314 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
315 int iAuthMethod = AUTH_NONE, TCHAR *pszSecret = NULL);
316 virtual ~AgentConnection();
317
318 BOOL Connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, DWORD *pdwError = NULL);
319 void Disconnect(void);
320 BOOL IsConnected(void) { return m_bIsConnected; }
321 int GetProtocolVersion(void) { return m_nProtocolVersion; }
322
323 ARP_CACHE *GetArpCache(void);
324 INTERFACE_LIST *GetInterfaceList(void);
325 ROUTING_TABLE *GetRoutingTable(void);
326 DWORD GetParameter(TCHAR *pszParam, DWORD dwBufSize, TCHAR *pszBuffer);
327 DWORD GetList(TCHAR *pszParam);
328 DWORD Nop(void);
329 DWORD ExecAction(TCHAR *pszAction, int argc, TCHAR **argv);
330 DWORD UploadFile(TCHAR *pszFile);
331 DWORD StartUpgrade(TCHAR *pszPkgName);
332 DWORD CheckNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType, WORD wPort = 0,
333 WORD wProto = 0, TCHAR *pszRequest = NULL, TCHAR *pszResponse = NULL);
334 DWORD GetSupportedParameters(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList);
335 DWORD GetConfigFile(TCHAR **ppszConfig, DWORD *pdwSize);
336 DWORD UpdateConfigFile(TCHAR *pszConfig);
337 DWORD EnableTraps(void);
338 CSCPMessage *CustomRequest(CSCPMessage *pRequest);
339
340 DWORD GetNumDataLines(void) { return m_dwNumDataLines; }
341 const TCHAR *GetDataLine(DWORD dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
342
343 void SetCommandTimeout(DWORD dwTimeout) { if (dwTimeout > 500) m_dwCommandTimeout = dwTimeout; }
344 void SetRecvTimeout(DWORD dwTimeout) { if (dwTimeout > 10000) m_dwRecvTimeout = dwTimeout; }
345 void SetEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
346 void SetProxy(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
347 int iAuthMethod = AUTH_NONE, TCHAR *pszSecret = NULL);
348 void SetPort(WORD wPort) { m_wPort = wPort; }
349 void SetAuthData(int nMethod, char *pszSecret) { m_iAuthMethod = nMethod; strncpy(m_szSecret, pszSecret, MAX_SECRET_LENGTH); }
350 };
351
352
353 //
354 // Proxy SNMP transport
355 //
356
357 class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
358 {
359 protected:
360 AgentConnection *m_pAgentConnection;
361 CSCPMessage *m_pResponse;
362 DWORD m_dwIpAddr;
363 WORD m_wPort;
364
365 public:
366 SNMP_ProxyTransport(AgentConnection *pConn, DWORD dwIpAddr, WORD wPort);
367 virtual ~SNMP_ProxyTransport();
368
369 virtual int Read(SNMP_PDU **ppData, DWORD dwTimeout = INFINITE,
370 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL);
371 virtual int Send(SNMP_PDU *pPDU);
372 };
373
374
375 //
376 // Functions
377 //
378
379 void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
380 void LIBNXSRV_EXPORTABLE DestroyInterfaceList(INTERFACE_LIST *pIfList);
381 void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
382 void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
383 const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError);
384 BYTE LIBNXSRV_EXPORTABLE *LoadFile(char *pszFileName, DWORD *pdwFileSize);
385
386 void LIBNXSRV_EXPORTABLE InitLog(BOOL bUseSystemLog, char *pszLogFile, BOOL bPrintToScreen);
387 void LIBNXSRV_EXPORTABLE CloseLog(void);
388 void LIBNXSRV_EXPORTABLE WriteLog(DWORD msg, WORD wType, char *format, ...);
389 void LIBNXSRV_EXPORTABLE DbgPrintf(DWORD dwFlags, TCHAR *szFormat, ...);
390
391 BOOL LIBNXSRV_EXPORTABLE DBInit(BOOL bWriteLog, BOOL bLogErrors, BOOL bDumpSQL,
392 void (* fpEventHandler)(DWORD, TCHAR *));
393 DB_HANDLE LIBNXSRV_EXPORTABLE DBConnect(void);
394 DB_HANDLE LIBNXSRV_EXPORTABLE DBConnectEx(TCHAR *pszServer, TCHAR *pszDBName,
395 TCHAR *pszLogin, TCHAR *pszPassword);
396 void LIBNXSRV_EXPORTABLE DBDisconnect(DB_HANDLE hConn);
397 BOOL LIBNXSRV_EXPORTABLE DBQuery(DB_HANDLE hConn, TCHAR *szQuery);
398 DB_RESULT LIBNXSRV_EXPORTABLE DBSelect(DB_HANDLE hConn, TCHAR *szQuery);
399 DB_ASYNC_RESULT LIBNXSRV_EXPORTABLE DBAsyncSelect(DB_HANDLE hConn, TCHAR *szQuery);
400 BOOL LIBNXSRV_EXPORTABLE DBFetch(DB_ASYNC_RESULT hResult);
401 TCHAR LIBNXSRV_EXPORTABLE *DBGetField(DB_RESULT hResult, int iRow, int iColumn,
402 TCHAR *pszBuffer, int nBufLen);
403 LONG LIBNXSRV_EXPORTABLE DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
404 DWORD LIBNXSRV_EXPORTABLE DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
405 INT64 LIBNXSRV_EXPORTABLE DBGetFieldInt64(DB_RESULT hResult, int iRow, int iColumn);
406 QWORD LIBNXSRV_EXPORTABLE DBGetFieldUInt64(DB_RESULT hResult, int iRow, int iColumn);
407 double LIBNXSRV_EXPORTABLE DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn);
408 DWORD LIBNXSRV_EXPORTABLE DBGetFieldIPAddr(DB_RESULT hResult, int iRow, int iColumn);
409 BOOL LIBNXSRV_EXPORTABLE DBGetFieldByteArray(DB_RESULT hResult, int iRow, int iColumn,
410 int *pnArray, int nSize, int nDefault);
411 BOOL LIBNXSRV_EXPORTABLE DBGetFieldGUID(DB_RESULT hResult, int iRow,
412 int iColumn, uuid_t guid);
413 TCHAR LIBNXSRV_EXPORTABLE *DBGetFieldAsync(DB_ASYNC_RESULT hResult, int iColumn, TCHAR *pBuffer, int iBufSize);
414 LONG LIBNXSRV_EXPORTABLE DBGetFieldAsyncLong(DB_RESULT hResult, int iColumn);
415 DWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncULong(DB_ASYNC_RESULT hResult, int iColumn);
416 INT64 LIBNXSRV_EXPORTABLE DBGetFieldAsyncInt64(DB_RESULT hResult, int iColumn);
417 QWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncUInt64(DB_ASYNC_RESULT hResult, int iColumn);
418 double LIBNXSRV_EXPORTABLE DBGetFieldAsyncDouble(DB_RESULT hResult, int iColumn);
419 DWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncIPAddr(DB_RESULT hResult, int iColumn);
420 int LIBNXSRV_EXPORTABLE DBGetNumRows(DB_RESULT hResult);
421 void LIBNXSRV_EXPORTABLE DBFreeResult(DB_RESULT hResult);
422 void LIBNXSRV_EXPORTABLE DBFreeAsyncResult(DB_HANDLE hConn, DB_ASYNC_RESULT hResult);
423 BOOL LIBNXSRV_EXPORTABLE DBBegin(DB_HANDLE hConn);
424 BOOL LIBNXSRV_EXPORTABLE DBCommit(DB_HANDLE hConn);
425 BOOL LIBNXSRV_EXPORTABLE DBRollback(DB_HANDLE hConn);
426 void LIBNXSRV_EXPORTABLE DBUnloadDriver(void);
427
428 TCHAR LIBNXSRV_EXPORTABLE *EncodeSQLString(const TCHAR *pszIn);
429 void LIBNXSRV_EXPORTABLE DecodeSQLString(TCHAR *pszStr);
430
431 void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
432
433
434 //
435 // Variables
436 //
437
438 extern DWORD LIBNXSRV_EXPORTABLE g_dwFlags;
439 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbDriver[];
440 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbDrvParams[];
441 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbServer[];
442 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbLogin[];
443 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbPassword[];
444 extern TCHAR LIBNXSRV_EXPORTABLE g_szDbName[];
445
446 #endif /* _nxsrvapi_h_ */