multiple bug fixes; implemented partial object sync (required for mobile client)
[public/netxms.git] / src / server / include / nxsrvapi.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** Server Library
619e5c9b 4** Copyright (C) 2003-2011 Victor Kirhenshtein
5039dede
AK
5**
6** This program is free software; you can redistribute it and/or modify
0702ed69
VK
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
5039dede
AK
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**
0702ed69 16** You should have received a copy of the GNU Lesser General Public License
5039dede
AK
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>
944016d6 43#include "../libnxsrv/messages.h"
5039dede
AK
44#include <nxsnmp.h>
45#include <netxms_isc.h>
46
47
48//
49// Default files
50//
51
52#ifdef _WIN32
53
54# define DEFAULT_CONFIG_FILE _T("C:\\netxmsd.conf")
55
08b214c6
VK
56# define DEFAULT_SHELL _T("cmd.exe")
57# define DEFAULT_LOG_FILE _T("C:\\NetXMS.log")
58# define DEFAULT_DATA_DIR _T("C:\\NetXMS\\var")
59# define DEFAULT_DUMP_DIR _T("C:\\")
60
61# define DDIR_MIBS _T("\\mibs")
62# define DDIR_PACKAGES _T("\\packages")
63# define DDIR_BACKGROUNDS _T("\\backgrounds")
64# define DDIR_SHARED_FILES _T("\\shared")
65# define DFILE_KEYS _T("\\server_key")
66# define DFILE_COMPILED_MIB _T("\\mibs\\netxms.mib")
e6b9439a 67# define DDIR_IMAGES _T("\\images")
619e5c9b 68# define DDIR_FILES _T("\\files")
5039dede
AK
69
70#else /* _WIN32 */
71
72# define DEFAULT_CONFIG_FILE _T("{search}")
73
08b214c6 74# define DEFAULT_SHELL _T("/bin/sh")
5039dede
AK
75
76# ifndef DATADIR
08b214c6 77# define DATADIR _T("/var/netxms")
5039dede
AK
78# endif
79
08b214c6 80# define DEFAULT_LOG_FILE DATADIR _T("/log/netxmsd.log")
5039dede 81# define DEFAULT_DATA_DIR DATADIR
08b214c6 82# define DEFAULT_DUMP_DIR _T("/")
5039dede 83
08b214c6
VK
84# define DDIR_MIBS _T("/mibs")
85# define DDIR_PACKAGES _T("/packages")
86# define DDIR_BACKGROUNDS _T("/backgrounds")
87# define DDIR_SHARED_FILES _T("/shared")
88# define DFILE_KEYS _T("/.server_key")
89# define DFILE_COMPILED_MIB _T("/mibs/netxms.mib")
e6b9439a 90# define DDIR_IMAGES _T("/images")
619e5c9b 91# define DDIR_FILES _T("/files")
5039dede
AK
92
93#endif /* _WIN32 */
94
95
96//
97// Application flags
98//
99
100#define AF_DAEMON 0x00000001
101#define AF_USE_SYSLOG 0x00000002
102#define AF_ENABLE_NETWORK_DISCOVERY 0x00000004
103#define AF_ACTIVE_NETWORK_DISCOVERY 0x00000008
104#define AF_LOG_SQL_ERRORS 0x00000010
105#define AF_DELETE_EMPTY_SUBNETS 0x00000020
106#define AF_ENABLE_SNMP_TRAPD 0x00000040
107#define AF_ENABLE_ZONING 0x00000080
108#define AF_SYNC_NODE_NAMES_WITH_DNS 0x00000100
109#define AF_CHECK_TRUSTED_NODES 0x00000200
cbc777ee 110#define AF_WRITE_FULL_DUMP 0x00080000
5039dede
AK
111#define AF_RESOLVE_NODE_NAMES 0x00100000
112#define AF_CATCH_EXCEPTIONS 0x00200000
113#define AF_INTERNAL_CA 0x00400000
114#define AF_DB_LOCKED 0x01000000
115#define AF_ENABLE_MULTIPLE_DB_CONN 0x02000000
116#define AF_DB_CONNECTION_LOST 0x04000000
117#define AF_NO_NETWORK_CONNECTIVITY 0x08000000
118#define AF_EVENT_STORM_DETECTED 0x08000000
119#define AF_SERVER_INITIALIZED 0x40000000
120#define AF_SHUTDOWN 0x80000000
121
122#define IsStandalone() (!(g_dwFlags & AF_DAEMON))
123#define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
124
125
126//
127// Encryption usage policies
128//
129
130#define ENCRYPTION_DISABLED 0
131#define ENCRYPTION_ALLOWED 1
132#define ENCRYPTION_PREFERRED 2
133#define ENCRYPTION_REQUIRED 3
134
135
5039dede
AK
136//
137// Win32 service and syslog constants
138//
139
140#ifdef _WIN32
141
142#define CORE_SERVICE_NAME _T("NetXMSCore")
143#define CORE_EVENT_SOURCE _T("NetXMSCore")
144#define NETXMSD_SYSLOG_NAME CORE_EVENT_SOURCE
145
146#else
147
148#define NETXMSD_SYSLOG_NAME _T("netxmsd")
149
150#endif /* _WIN32 */
151
152
153//
154// Single ARP cache entry
155//
156
157typedef struct
158{
159 DWORD dwIndex; // Interface index
160 DWORD dwIpAddr;
161 BYTE bMacAddr[MAC_ADDR_LENGTH];
162} ARP_ENTRY;
163
164
165//
166// ARP cache structure used by discovery functions and AgentConnection class
167//
168
169typedef struct
170{
171 DWORD dwNumEntries;
172 ARP_ENTRY *pEntries;
173} ARP_CACHE;
174
175
176//
177// Interface information structure used by discovery functions and AgentConnection class
178//
179
180typedef struct
181{
182 TCHAR szName[MAX_DB_STRING];
183 DWORD dwIndex;
184 DWORD dwType;
eec253a8
VK
185 DWORD dwBridgePortNumber;
186 DWORD dwSlotNumber;
187 DWORD dwPortNumber;
5039dede
AK
188 DWORD dwIpAddr;
189 DWORD dwIpNetMask;
190 BYTE bMacAddr[MAC_ADDR_LENGTH];
191 int iNumSecondary; // Number of secondary IP's on this interface
192} INTERFACE_INFO;
193
194
195//
196// Interface list used by discovery functions and AgentConnection class
197//
198
199typedef struct
200{
201 int iNumEntries; // Number of entries in pInterfaces
202 int iEnumPos; // Used by index enumeration handler
203 void *pArg; // Can be used by custom enumeration handlers
204 INTERFACE_INFO *pInterfaces; // Interface entries
205} INTERFACE_LIST;
206
207
208//
209// Route information
210//
211
212typedef struct
213{
214 DWORD dwDestAddr;
215 DWORD dwDestMask;
216 DWORD dwNextHop;
217 DWORD dwIfIndex;
218 DWORD dwRouteType;
219} ROUTE;
220
221
222//
223// Routing table
224//
225
226typedef struct
227{
228 int iNumEntries; // Number of entries
229 ROUTE *pRoutes; // Route list
230} ROUTING_TABLE;
231
232
1f385e47
VK
233//
234// Information about policies installed on agent
235//
236
237class LIBNXSRV_EXPORTABLE AgentPolicyInfo
238{
239private:
240 int m_size;
241 BYTE *m_guidList;
242 int *m_typeList;
243 TCHAR **m_serverList;
244
245public:
246 AgentPolicyInfo(CSCPMessage *msg);
247 ~AgentPolicyInfo();
248
249 int getSize() { return m_size; }
250 bool getGuid(int index, uuid_t guid);
251 int getType(int index) { return ((index >= 0) && (index < m_size)) ? m_typeList[index] : -1; }
252 const TCHAR *getServer(int index) { return ((index >= 0) && (index < m_size)) ? m_serverList[index] : NULL; }
253};
254
255
5039dede
AK
256//
257// Agent connection
258//
259
260class LIBNXSRV_EXPORTABLE AgentConnection
261{
262private:
263 DWORD m_dwAddr;
264 int m_nProtocolVersion;
265 int m_iAuthMethod;
08b214c6 266 char m_szSecret[MAX_SECRET_LENGTH];
5039dede
AK
267 time_t m_tLastCommandTime;
268 SOCKET m_hSocket;
269 DWORD m_dwNumDataLines;
270 DWORD m_dwRequestId;
271 DWORD m_dwCommandTimeout;
7c521895 272 DWORD m_connectionTimeout;
5039dede
AK
273 DWORD m_dwRecvTimeout;
274 TCHAR **m_ppDataLines;
275 MsgWaitQueue *m_pMsgWaitQueue;
276 BOOL m_bIsConnected;
277 MUTEX m_mutexDataLock;
278 THREAD m_hReceiverThread;
279 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
280 int m_iEncryptionPolicy;
281 BOOL m_bUseProxy;
282 DWORD m_dwProxyAddr;
283 WORD m_wPort;
284 WORD m_wProxyPort;
285 int m_iProxyAuth;
08b214c6 286 char m_szProxySecret[MAX_SECRET_LENGTH];
5039dede 287 int m_hCurrFile;
9f6d453a 288 TCHAR m_currentFileName[MAX_PATH];
bb85e341
VK
289 DWORD m_dwDownloadRequestId;
290 CONDITION m_condFileDownload;
291 BOOL m_fileDownloadSucceeded;
292 void (*m_downloadProgressCallback)(size_t, void *);
293 void *m_downloadProgressCallbackArg;
901a5a9b 294 bool m_deleteFileOnDownloadFailure;
4685a2ad 295 bool m_fileUploadInProgress;
5039dede
AK
296
297 void ReceiverThread(void);
298 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
299
300protected:
7c521895
VK
301 void destroyResultData();
302 BOOL sendMessage(CSCPMessage *pMsg);
303 CSCPMessage *waitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut) { return m_pMsgWaitQueue->WaitForMessage(wCode, dwId, dwTimeOut); }
304 DWORD waitForRCC(DWORD dwRqId, DWORD dwTimeOut);
305 DWORD setupEncryption(RSA *pServerKey);
306 DWORD authenticate(BOOL bProxyData);
307 DWORD setupProxyConnection();
308 DWORD getIpAddr() { return ntohl(m_dwAddr); }
309 DWORD prepareFileDownload(const TCHAR *fileName, DWORD rqId, bool append, void (*downloadProgressCallback)(size_t, void *), void *cbArg);
5039dede
AK
310
311 virtual void PrintMsg(const TCHAR *pszFormat, ...);
f480bdd4
VK
312 virtual void onTrap(CSCPMessage *pMsg);
313 virtual void onDataPush(CSCPMessage *msg);
90284364 314 virtual bool processCustomMessage(CSCPMessage *pMsg);
f480bdd4 315 virtual void onFileDownload(BOOL success);
5039dede
AK
316
317 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
318 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
319
320public:
5039dede
AK
321 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
322 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
323 virtual ~AgentConnection();
324
c3acd0f6 325 BOOL connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, DWORD *pdwError = NULL, DWORD *pdwSocketError = NULL);
7c521895
VK
326 void disconnect();
327 BOOL isConnected() { return m_bIsConnected; }
328 int getProtocolVersion() { return m_nProtocolVersion; }
5039dede 329
45d84f8a 330 SOCKET getSocket() { return m_hSocket; }
5a9e58a3 331
4687826e
VK
332 ARP_CACHE *getArpCache();
333 INTERFACE_LIST *getInterfaceList();
334 ROUTING_TABLE *getRoutingTable();
335 DWORD getParameter(const TCHAR *pszParam, DWORD dwBufSize, TCHAR *pszBuffer);
336 DWORD getList(const TCHAR *pszParam);
337 DWORD getTable(const TCHAR *pszParam, Table **table);
7c521895 338 DWORD nop();
4687826e 339 DWORD execAction(const TCHAR *pszAction, int argc, TCHAR **argv);
619e5c9b 340 DWORD uploadFile(const TCHAR *localFile, const TCHAR *destinationFile = NULL, void (* progressCallback)(INT64, void *) = NULL, void *cbArg = NULL);
4687826e
VK
341 DWORD startUpgrade(const TCHAR *pszPkgName);
342 DWORD checkNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType, WORD wPort = 0,
5039dede 343 WORD wProto = 0, const TCHAR *pszRequest = NULL, const TCHAR *pszResponse = NULL);
4687826e
VK
344 DWORD getSupportedParameters(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList);
345 DWORD getConfigFile(TCHAR **ppszConfig, DWORD *pdwSize);
45d84f8a 346 DWORD updateConfigFile(const TCHAR *pszConfig);
7c521895 347 DWORD enableTraps();
1f385e47
VK
348 DWORD getPolicyInventory(AgentPolicyInfo **info);
349 DWORD uninstallPolicy(uuid_t guid);
e58b5e54
VK
350
351 DWORD generateRequestId() { return m_dwRequestId++; }
45d84f8a 352 CSCPMessage *customRequest(CSCPMessage *pRequest, const TCHAR *recvFile = NULL, bool appendFile = false,
f2665675 353 void (*downloadProgressCallback)(size_t, void *) = NULL, void *cbArg = NULL);
5039dede 354
7c521895
VK
355 DWORD getNumDataLines() { return m_dwNumDataLines; }
356 const TCHAR *getDataLine(DWORD dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
5039dede 357
7c521895
VK
358 void setConnectionTimeout(DWORD dwTimeout) { m_connectionTimeout = max(dwTimeout, 1000); }
359 DWORD getConnectionTimeout() { return m_connectionTimeout; }
45d84f8a
VK
360 void setCommandTimeout(DWORD dwTimeout) { m_dwCommandTimeout = max(dwTimeout, 500); }
361 DWORD getCommandTimeout() { return m_dwCommandTimeout; }
7c521895
VK
362 void setRecvTimeout(DWORD dwTimeout) { m_dwRecvTimeout = max(dwTimeout, 10000); }
363 void setEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
364 void setProxy(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
5039dede 365 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
7c521895 366 void setPort(WORD wPort) { m_wPort = wPort; }
08b214c6 367 void setAuthData(int nMethod, const char *pszSecret) { m_iAuthMethod = nMethod; strncpy(m_szSecret, pszSecret, MAX_SECRET_LENGTH); m_szSecret[MAX_SECRET_LENGTH - 1] = 0; }
901a5a9b 368 void setDeleteFileOnDownloadFailure(bool flag) { m_deleteFileOnDownloadFailure = flag; }
5039dede
AK
369};
370
371
372//
373// Proxy SNMP transport
374//
375
376class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
377{
378protected:
379 AgentConnection *m_pAgentConnection;
380 CSCPMessage *m_pResponse;
381 DWORD m_dwIpAddr;
382 WORD m_wPort;
383
384public:
385 SNMP_ProxyTransport(AgentConnection *pConn, DWORD dwIpAddr, WORD wPort);
386 virtual ~SNMP_ProxyTransport();
387
c4366266 388 virtual int readMessage(SNMP_PDU **ppData, DWORD dwTimeout = INFINITE,
cd9f247e
VK
389 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL,
390 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
c4366266 391 virtual int sendMessage(SNMP_PDU *pdu);
5039dede
AK
392};
393
394
395//
396// ISC flags
397//
398
399#define ISCF_IS_CONNECTED ((DWORD)0x00000001)
400#define ISCF_REQUIRE_ENCRYPTION ((DWORD)0x00000002)
401
402
403//
404// Inter-server connection (ISC)
405//
406
407class LIBNXSRV_EXPORTABLE ISC
408{
409private:
410 DWORD m_flags;
411 DWORD m_addr;
412 WORD m_port;
413 SOCKET m_socket;
414 int m_protocolVersion;
415 DWORD m_requestId;
416 DWORD m_recvTimeout;
417 MsgWaitQueue *m_msgWaitQueue;
418 MUTEX m_mutexDataLock;
419 THREAD m_hReceiverThread;
420 CSCP_ENCRYPTION_CONTEXT *m_ctx;
421 DWORD m_commandTimeout;
422
423 void ReceiverThread(void);
424 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
425
426protected:
427 void DestroyResultData(void);
428 DWORD SetupEncryption(RSA *pServerKey);
429 DWORD ConnectToService(DWORD service);
430
431 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
432 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
433
434 virtual void PrintMsg(const TCHAR *format, ...);
435
436public:
437 ISC();
438 ISC(DWORD addr, WORD port = NETXMS_ISC_PORT);
439 virtual ~ISC();
440
441 DWORD Connect(DWORD service, RSA *serverKey = NULL, BOOL requireEncryption = FALSE);
442 void Disconnect();
443
444 BOOL SendMessage(CSCPMessage *msg);
445 CSCPMessage *WaitForMessage(WORD code, DWORD id, DWORD timeOut) { return m_msgWaitQueue->WaitForMessage(code, id, timeOut); }
446 DWORD WaitForRCC(DWORD rqId, DWORD timeOut);
447
448 DWORD Nop(void);
449};
450
451
452//
453// Functions
454//
455
456void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
457void LIBNXSRV_EXPORTABLE DestroyInterfaceList(INTERFACE_LIST *pIfList);
458void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
459void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
460const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError);
461
462void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...);
463void LIBNXSRV_EXPORTABLE DbgPrintf(int level, const TCHAR *format, ...);
9d88cdc9 464void LIBNXSRV_EXPORTABLE DbgPrintf2(int level, const TCHAR *format, va_list args);
c20b2798 465
5039dede
AK
466void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
467
468const TCHAR LIBNXSRV_EXPORTABLE *ISCErrorCodeToText(DWORD code);
469
470
471//
472// Variables
473//
474
475extern DWORD LIBNXSRV_EXPORTABLE g_dwFlags;
476extern int LIBNXSRV_EXPORTABLE g_nDebugLevel;
5039dede
AK
477
478#endif /* _nxsrvapi_h_ */