Initial version of crash dump generator on Windows
[public/netxms.git] / src / server / include / nxsrvapi.h
CommitLineData
df33b6f7
VK
1/*
2** NetXMS - Network Management System
3** Server Library
df7156b3 4** Copyright (C) 2003, 2004, 2005, 2006 Victor Kirhenshtein
df33b6f7
VK
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**
df7156b3 20** File: nxsrvapi.h
df33b6f7
VK
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>
9f2ad16c 41#include <nxcpapi.h>
df33b6f7 42#include <nms_agent.h>
9cc9ea72 43#include <messages.h>
9bb31635 44#include <dbdrv.h>
439c93c0 45#include <nxsnmp.h>
9cc9ea72
VK
46
47
fc935a60
VK
48//
49// Default files
50//
51
52#ifdef _WIN32
e44ac467
VK
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"
ba2c1467 59# define DEFAULT_DUMP_DIR "C:\\"
e44ac467
VK
60
61# define DDIR_MIBS "\\mibs"
62# define DDIR_IMAGES "\\images"
63# define DDIR_PACKAGES "\\packages"
a2cdb3a4 64# define DDIR_BACKGROUNDS "\\backgrounds"
e44ac467 65# define DFILE_KEYS "\\server_key"
1dcd1bee 66# define DFILE_COMPILED_MIB "\\mibs\\netxms.mib"
e44ac467
VK
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
ba2c1467 80# define DEFAULT_DUMP_DIR "/"
e44ac467
VK
81
82# define DDIR_MIBS "/mibs"
83# define DDIR_IMAGES "/images"
84# define DDIR_PACKAGES "/packages"
a2cdb3a4 85# define DDIR_BACKGROUNDS "/backgrounds"
e44ac467 86# define DFILE_KEYS "/.server_key"
1dcd1bee 87# define DFILE_COMPILED_MIB "/mibs/netxms.mib"
e44ac467
VK
88
89#endif /* _WIN32 */
fc935a60
VK
90
91
dd5b3364
VK
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
cd00d133 116#define AF_RESOLVE_NODE_NAMES 0x00100000
ba2c1467 117#define AF_CATCH_EXCEPTIONS 0x00200000
dd5b3364
VK
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
f9a4ab9a
VK
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
9cc9ea72
VK
138//
139// DB-related constants
140//
141
142#define MAX_DB_LOGIN 64
143#define MAX_DB_PASSWORD 64
81727357 144#define MAX_DB_NAME 256
9cc9ea72
VK
145
146
27cc829a
VK
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
a3c76aba
VK
156//
157// Database connection structure
158//
159
160struct db_handle_t
161{
162 DB_CONNECTION hConn;
163 MUTEX mutexTransLock; // Transaction lock
164 int nTransactionLevel;
27cc829a
VK
165 TCHAR *pszServer;
166 TCHAR *pszLogin;
167 TCHAR *pszPassword;
168 TCHAR *pszDBName;
a3c76aba
VK
169};
170typedef db_handle_t * DB_HANDLE;
171
172
9cc9ea72
VK
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 */
df33b6f7
VK
183
184
185//
186// Single ARP cache entry
187//
188
189typedef struct
190{
191 DWORD dwIndex; // Interface index
192 DWORD dwIpAddr;
b50f1100 193 BYTE bMacAddr[MAC_ADDR_LENGTH];
df33b6f7
VK
194} ARP_ENTRY;
195
196
197//
198// ARP cache structure used by discovery functions and AgentConnection class
199//
200
201typedef 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
212typedef struct
213{
7e3ae259 214 TCHAR szName[MAX_DB_STRING];
df33b6f7
VK
215 DWORD dwIndex;
216 DWORD dwType;
217 DWORD dwIpAddr;
218 DWORD dwIpNetMask;
b50f1100 219 BYTE bMacAddr[MAC_ADDR_LENGTH];
df33b6f7
VK
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
228typedef struct
229{
230 int iNumEntries; // Number of entries in pInterfaces
231 int iEnumPos; // Used by index enumeration handler
18e26ff8 232 void *pArg; // Can be used by custom enumeration handlers
df33b6f7
VK
233 INTERFACE_INFO *pInterfaces; // Interface entries
234} INTERFACE_LIST;
235
236
6a4f307b
VK
237//
238// Route information
239//
240
241typedef 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
255typedef struct
256{
257 int iNumEntries; // Number of entries
258 ROUTE *pRoutes; // Route list
259} ROUTING_TABLE;
260
261
df33b6f7
VK
262//
263// Agent connection
264//
265
266class LIBNXSRV_EXPORTABLE AgentConnection
267{
df33b6f7
VK
268private:
269 DWORD m_dwAddr;
4bd3d6c5 270 int m_nProtocolVersion;
df33b6f7
VK
271 int m_iAuthMethod;
272 char m_szSecret[MAX_SECRET_LENGTH];
273 time_t m_tLastCommandTime;
274 SOCKET m_hSocket;
df33b6f7 275 DWORD m_dwNumDataLines;
6c426e3a
VK
276 DWORD m_dwRequestId;
277 DWORD m_dwCommandTimeout;
08043edf 278 DWORD m_dwRecvTimeout;
268774a0 279 TCHAR **m_ppDataLines;
6c426e3a 280 MsgWaitQueue *m_pMsgWaitQueue;
1c62be36 281 BOOL m_bIsConnected;
38e832d6 282 MUTEX m_mutexDataLock;
ccdbbb52 283 THREAD m_hReceiverThread;
e44ac467 284 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
f9a4ab9a 285 int m_iEncryptionPolicy;
f1448d17
VK
286 BOOL m_bUseProxy;
287 DWORD m_dwProxyAddr;
33f68bf6 288 WORD m_wPort;
f1448d17
VK
289 WORD m_wProxyPort;
290 int m_iProxyAuth;
291 char m_szProxySecret[MAX_SECRET_LENGTH];
df33b6f7
VK
292
293 void ReceiverThread(void);
ccdbbb52 294 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
df33b6f7
VK
295
296protected:
297 void DestroyResultData(void);
6c426e3a
VK
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);
e44ac467 301 DWORD SetupEncryption(RSA *pServerKey);
f1448d17
VK
302 DWORD Authenticate(BOOL bProxyData);
303 DWORD SetupProxyConnection(void);
15c8cf46 304 DWORD GetIpAddr(void) { return ntohl(m_dwAddr); }
df33b6f7 305
268774a0 306 virtual void PrintMsg(TCHAR *pszFormat, ...);
901c96c7 307 virtual void OnTrap(CSCPMessage *pMsg);
df33b6f7 308
38e832d6
VK
309 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
310 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
1c62be36 311
df33b6f7
VK
312public:
313 AgentConnection();
f1448d17
VK
314 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
315 int iAuthMethod = AUTH_NONE, TCHAR *pszSecret = NULL);
456d5d1c 316 virtual ~AgentConnection();
df33b6f7 317
1ba3c17a 318 BOOL Connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, DWORD *pdwError = NULL);
df33b6f7 319 void Disconnect(void);
1c62be36 320 BOOL IsConnected(void) { return m_bIsConnected; }
439c93c0 321 int GetProtocolVersion(void) { return m_nProtocolVersion; }
df33b6f7
VK
322
323 ARP_CACHE *GetArpCache(void);
324 INTERFACE_LIST *GetInterfaceList(void);
6a4f307b 325 ROUTING_TABLE *GetRoutingTable(void);
268774a0
VK
326 DWORD GetParameter(TCHAR *pszParam, DWORD dwBufSize, TCHAR *pszBuffer);
327 DWORD GetList(TCHAR *pszParam);
df33b6f7 328 DWORD Nop(void);
268774a0 329 DWORD ExecAction(TCHAR *pszAction, int argc, TCHAR **argv);
d096bcdd 330 DWORD UploadFile(TCHAR *pszFile);
e925a5fc 331 DWORD StartUpgrade(TCHAR *pszPkgName);
3f0ca036 332 DWORD CheckNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType, WORD wPort = 0,
1a17feb0 333 WORD wProto = 0, TCHAR *pszRequest = NULL, TCHAR *pszResponse = NULL);
125c0e81 334 DWORD GetSupportedParameters(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList);
cb27dff1 335 DWORD GetConfigFile(TCHAR **ppszConfig, DWORD *pdwSize);
3c2c7839 336 DWORD UpdateConfigFile(TCHAR *pszConfig);
15c8cf46 337 DWORD EnableTraps(void);
439c93c0 338 CSCPMessage *CustomRequest(CSCPMessage *pRequest);
901c96c7
VK
339
340 DWORD GetNumDataLines(void) { return m_dwNumDataLines; }
268774a0 341 const TCHAR *GetDataLine(DWORD dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
962b3915
VK
342
343 void SetCommandTimeout(DWORD dwTimeout) { if (dwTimeout > 500) m_dwCommandTimeout = dwTimeout; }
08043edf 344 void SetRecvTimeout(DWORD dwTimeout) { if (dwTimeout > 10000) m_dwRecvTimeout = dwTimeout; }
f9a4ab9a 345 void SetEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
f1448d17
VK
346 void SetProxy(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
347 int iAuthMethod = AUTH_NONE, TCHAR *pszSecret = NULL);
81727357
VK
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); }
df33b6f7
VK
350};
351
352
439c93c0
VK
353//
354// Proxy SNMP transport
355//
356
357class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
358{
359protected:
360 AgentConnection *m_pAgentConnection;
361 CSCPMessage *m_pResponse;
362 DWORD m_dwIpAddr;
363 WORD m_wPort;
364
365public:
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
df33b6f7
VK
375//
376// Functions
377//
378
379void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
380void LIBNXSRV_EXPORTABLE DestroyInterfaceList(INTERFACE_LIST *pIfList);
6a4f307b
VK
381void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
382void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
268774a0 383const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError);
79743401 384BYTE LIBNXSRV_EXPORTABLE *LoadFile(char *pszFileName, DWORD *pdwFileSize);
df33b6f7 385
9cc9ea72
VK
386void LIBNXSRV_EXPORTABLE InitLog(BOOL bUseSystemLog, char *pszLogFile, BOOL bPrintToScreen);
387void LIBNXSRV_EXPORTABLE CloseLog(void);
388void LIBNXSRV_EXPORTABLE WriteLog(DWORD msg, WORD wType, char *format, ...);
dd5b3364 389void LIBNXSRV_EXPORTABLE DbgPrintf(DWORD dwFlags, TCHAR *szFormat, ...);
9cc9ea72 390
27cc829a
VK
391BOOL LIBNXSRV_EXPORTABLE DBInit(BOOL bWriteLog, BOOL bLogErrors, BOOL bDumpSQL,
392 void (* fpEventHandler)(DWORD, TCHAR *));
9cc9ea72 393DB_HANDLE LIBNXSRV_EXPORTABLE DBConnect(void);
79743401
VK
394DB_HANDLE LIBNXSRV_EXPORTABLE DBConnectEx(TCHAR *pszServer, TCHAR *pszDBName,
395 TCHAR *pszLogin, TCHAR *pszPassword);
9cc9ea72 396void LIBNXSRV_EXPORTABLE DBDisconnect(DB_HANDLE hConn);
7d4b78b3
VK
397BOOL LIBNXSRV_EXPORTABLE DBQuery(DB_HANDLE hConn, TCHAR *szQuery);
398DB_RESULT LIBNXSRV_EXPORTABLE DBSelect(DB_HANDLE hConn, TCHAR *szQuery);
399DB_ASYNC_RESULT LIBNXSRV_EXPORTABLE DBAsyncSelect(DB_HANDLE hConn, TCHAR *szQuery);
9cc9ea72 400BOOL LIBNXSRV_EXPORTABLE DBFetch(DB_ASYNC_RESULT hResult);
7d4b78b3
VK
401TCHAR LIBNXSRV_EXPORTABLE *DBGetField(DB_RESULT hResult, int iRow, int iColumn,
402 TCHAR *pszBuffer, int nBufLen);
0212dc5a 403LONG LIBNXSRV_EXPORTABLE DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
9cc9ea72 404DWORD LIBNXSRV_EXPORTABLE DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
0c6014e4
VK
405INT64 LIBNXSRV_EXPORTABLE DBGetFieldInt64(DB_RESULT hResult, int iRow, int iColumn);
406QWORD LIBNXSRV_EXPORTABLE DBGetFieldUInt64(DB_RESULT hResult, int iRow, int iColumn);
9cc9ea72
VK
407double LIBNXSRV_EXPORTABLE DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn);
408DWORD LIBNXSRV_EXPORTABLE DBGetFieldIPAddr(DB_RESULT hResult, int iRow, int iColumn);
b6ce3970
VK
409BOOL LIBNXSRV_EXPORTABLE DBGetFieldByteArray(DB_RESULT hResult, int iRow, int iColumn,
410 int *pnArray, int nSize, int nDefault);
95b6a1b6
VK
411BOOL LIBNXSRV_EXPORTABLE DBGetFieldGUID(DB_RESULT hResult, int iRow,
412 int iColumn, uuid_t guid);
7d4b78b3 413TCHAR LIBNXSRV_EXPORTABLE *DBGetFieldAsync(DB_ASYNC_RESULT hResult, int iColumn, TCHAR *pBuffer, int iBufSize);
0212dc5a 414LONG LIBNXSRV_EXPORTABLE DBGetFieldAsyncLong(DB_RESULT hResult, int iColumn);
9cc9ea72 415DWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncULong(DB_ASYNC_RESULT hResult, int iColumn);
0c6014e4
VK
416INT64 LIBNXSRV_EXPORTABLE DBGetFieldAsyncInt64(DB_RESULT hResult, int iColumn);
417QWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncUInt64(DB_ASYNC_RESULT hResult, int iColumn);
9cc9ea72
VK
418double LIBNXSRV_EXPORTABLE DBGetFieldAsyncDouble(DB_RESULT hResult, int iColumn);
419DWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncIPAddr(DB_RESULT hResult, int iColumn);
420int LIBNXSRV_EXPORTABLE DBGetNumRows(DB_RESULT hResult);
421void LIBNXSRV_EXPORTABLE DBFreeResult(DB_RESULT hResult);
a3c76aba
VK
422void LIBNXSRV_EXPORTABLE DBFreeAsyncResult(DB_HANDLE hConn, DB_ASYNC_RESULT hResult);
423BOOL LIBNXSRV_EXPORTABLE DBBegin(DB_HANDLE hConn);
424BOOL LIBNXSRV_EXPORTABLE DBCommit(DB_HANDLE hConn);
425BOOL LIBNXSRV_EXPORTABLE DBRollback(DB_HANDLE hConn);
9cc9ea72
VK
426void LIBNXSRV_EXPORTABLE DBUnloadDriver(void);
427
56cd40b6
VK
428TCHAR LIBNXSRV_EXPORTABLE *EncodeSQLString(const TCHAR *pszIn);
429void LIBNXSRV_EXPORTABLE DecodeSQLString(TCHAR *pszStr);
430
f9a4ab9a
VK
431void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
432
9cc9ea72
VK
433
434//
435// Variables
436//
437
dd5b3364 438extern DWORD LIBNXSRV_EXPORTABLE g_dwFlags;
7d4b78b3
VK
439extern TCHAR LIBNXSRV_EXPORTABLE g_szDbDriver[];
440extern TCHAR LIBNXSRV_EXPORTABLE g_szDbDrvParams[];
441extern TCHAR LIBNXSRV_EXPORTABLE g_szDbServer[];
442extern TCHAR LIBNXSRV_EXPORTABLE g_szDbLogin[];
443extern TCHAR LIBNXSRV_EXPORTABLE g_szDbPassword[];
444extern TCHAR LIBNXSRV_EXPORTABLE g_szDbName[];
df33b6f7
VK
445
446#endif /* _nxsrvapi_h_ */