latest changes backported from 1.0.x branch
[public/netxms.git] / src / server / include / nxsrvapi.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** Server Library
0702ed69 4** Copyright (C) 2003-2010 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
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"
5039dede
AK
62# define DDIR_PACKAGES "\\packages"
63# define DDIR_BACKGROUNDS "\\backgrounds"
64# define DDIR_SHARED_FILES "\\shared"
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("{search}")
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"
5039dede
AK
83# define DDIR_PACKAGES "/packages"
84# define DDIR_BACKGROUNDS "/backgrounds"
85# define DDIR_SHARED_FILES "/shared"
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_DAEMON 0x00000001
97#define AF_USE_SYSLOG 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_SYNC_NODE_NAMES_WITH_DNS 0x00000100
105#define AF_CHECK_TRUSTED_NODES 0x00000200
cbc777ee 106#define AF_WRITE_FULL_DUMP 0x00080000
5039dede
AK
107#define AF_RESOLVE_NODE_NAMES 0x00100000
108#define AF_CATCH_EXCEPTIONS 0x00200000
109#define AF_INTERNAL_CA 0x00400000
110#define AF_DB_LOCKED 0x01000000
111#define AF_ENABLE_MULTIPLE_DB_CONN 0x02000000
112#define AF_DB_CONNECTION_LOST 0x04000000
113#define AF_NO_NETWORK_CONNECTIVITY 0x08000000
114#define AF_EVENT_STORM_DETECTED 0x08000000
115#define AF_SERVER_INITIALIZED 0x40000000
116#define AF_SHUTDOWN 0x80000000
117
118#define IsStandalone() (!(g_dwFlags & AF_DAEMON))
119#define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
120
121
122//
123// Encryption usage policies
124//
125
126#define ENCRYPTION_DISABLED 0
127#define ENCRYPTION_ALLOWED 1
128#define ENCRYPTION_PREFERRED 2
129#define ENCRYPTION_REQUIRED 3
130
131
5039dede
AK
132//
133// Win32 service and syslog constants
134//
135
136#ifdef _WIN32
137
138#define CORE_SERVICE_NAME _T("NetXMSCore")
139#define CORE_EVENT_SOURCE _T("NetXMSCore")
140#define NETXMSD_SYSLOG_NAME CORE_EVENT_SOURCE
141
142#else
143
144#define NETXMSD_SYSLOG_NAME _T("netxmsd")
145
146#endif /* _WIN32 */
147
148
149//
150// Single ARP cache entry
151//
152
153typedef struct
154{
155 DWORD dwIndex; // Interface index
156 DWORD dwIpAddr;
157 BYTE bMacAddr[MAC_ADDR_LENGTH];
158} ARP_ENTRY;
159
160
161//
162// ARP cache structure used by discovery functions and AgentConnection class
163//
164
165typedef struct
166{
167 DWORD dwNumEntries;
168 ARP_ENTRY *pEntries;
169} ARP_CACHE;
170
171
172//
173// Interface information structure used by discovery functions and AgentConnection class
174//
175
176typedef struct
177{
178 TCHAR szName[MAX_DB_STRING];
179 DWORD dwIndex;
180 DWORD dwType;
181 DWORD dwIpAddr;
182 DWORD dwIpNetMask;
183 BYTE bMacAddr[MAC_ADDR_LENGTH];
184 int iNumSecondary; // Number of secondary IP's on this interface
185} INTERFACE_INFO;
186
187
188//
189// Interface list used by discovery functions and AgentConnection class
190//
191
192typedef struct
193{
194 int iNumEntries; // Number of entries in pInterfaces
195 int iEnumPos; // Used by index enumeration handler
196 void *pArg; // Can be used by custom enumeration handlers
197 INTERFACE_INFO *pInterfaces; // Interface entries
198} INTERFACE_LIST;
199
200
201//
202// Route information
203//
204
205typedef struct
206{
207 DWORD dwDestAddr;
208 DWORD dwDestMask;
209 DWORD dwNextHop;
210 DWORD dwIfIndex;
211 DWORD dwRouteType;
212} ROUTE;
213
214
215//
216// Routing table
217//
218
219typedef struct
220{
221 int iNumEntries; // Number of entries
222 ROUTE *pRoutes; // Route list
223} ROUTING_TABLE;
224
225
226//
227// Agent connection
228//
229
230class LIBNXSRV_EXPORTABLE AgentConnection
231{
232private:
233 DWORD m_dwAddr;
234 int m_nProtocolVersion;
235 int m_iAuthMethod;
236 TCHAR m_szSecret[MAX_SECRET_LENGTH];
237 time_t m_tLastCommandTime;
238 SOCKET m_hSocket;
239 DWORD m_dwNumDataLines;
240 DWORD m_dwRequestId;
241 DWORD m_dwCommandTimeout;
7c521895 242 DWORD m_connectionTimeout;
5039dede
AK
243 DWORD m_dwRecvTimeout;
244 TCHAR **m_ppDataLines;
245 MsgWaitQueue *m_pMsgWaitQueue;
246 BOOL m_bIsConnected;
247 MUTEX m_mutexDataLock;
248 THREAD m_hReceiverThread;
249 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
250 int m_iEncryptionPolicy;
251 BOOL m_bUseProxy;
252 DWORD m_dwProxyAddr;
253 WORD m_wPort;
254 WORD m_wProxyPort;
255 int m_iProxyAuth;
256 TCHAR m_szProxySecret[MAX_SECRET_LENGTH];
257 int m_hCurrFile;
9f6d453a 258 TCHAR m_currentFileName[MAX_PATH];
bb85e341
VK
259 DWORD m_dwDownloadRequestId;
260 CONDITION m_condFileDownload;
261 BOOL m_fileDownloadSucceeded;
262 void (*m_downloadProgressCallback)(size_t, void *);
263 void *m_downloadProgressCallbackArg;
901a5a9b 264 bool m_deleteFileOnDownloadFailure;
4685a2ad 265 bool m_fileUploadInProgress;
5039dede
AK
266
267 void ReceiverThread(void);
268 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
269
270protected:
7c521895
VK
271 void destroyResultData();
272 BOOL sendMessage(CSCPMessage *pMsg);
273 CSCPMessage *waitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut) { return m_pMsgWaitQueue->WaitForMessage(wCode, dwId, dwTimeOut); }
274 DWORD waitForRCC(DWORD dwRqId, DWORD dwTimeOut);
275 DWORD setupEncryption(RSA *pServerKey);
276 DWORD authenticate(BOOL bProxyData);
277 DWORD setupProxyConnection();
278 DWORD getIpAddr() { return ntohl(m_dwAddr); }
279 DWORD prepareFileDownload(const TCHAR *fileName, DWORD rqId, bool append, void (*downloadProgressCallback)(size_t, void *), void *cbArg);
5039dede
AK
280
281 virtual void PrintMsg(const TCHAR *pszFormat, ...);
f480bdd4
VK
282 virtual void onTrap(CSCPMessage *pMsg);
283 virtual void onDataPush(CSCPMessage *msg);
284 virtual void onFileDownload(BOOL success);
5039dede
AK
285
286 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
287 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
288
289public:
5039dede
AK
290 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
291 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
292 virtual ~AgentConnection();
293
c3acd0f6 294 BOOL connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, DWORD *pdwError = NULL, DWORD *pdwSocketError = NULL);
7c521895
VK
295 void disconnect();
296 BOOL isConnected() { return m_bIsConnected; }
297 int getProtocolVersion() { return m_nProtocolVersion; }
5039dede 298
45d84f8a 299 SOCKET getSocket() { return m_hSocket; }
5a9e58a3 300
5039dede 301 ARP_CACHE *GetArpCache(void);
7c521895
VK
302 INTERFACE_LIST *GetInterfaceList();
303 ROUTING_TABLE *GetRoutingTable();
5039dede
AK
304 DWORD GetParameter(const TCHAR *pszParam, DWORD dwBufSize, TCHAR *pszBuffer);
305 DWORD GetList(const TCHAR *pszParam);
7c521895 306 DWORD nop();
5039dede 307 DWORD ExecAction(const TCHAR *pszAction, int argc, TCHAR **argv);
6173bea8 308 DWORD UploadFile(const TCHAR *pszFile, void (* progressCallback)(INT64, void *) = NULL, void *cbArg = NULL);
5039dede
AK
309 DWORD StartUpgrade(const TCHAR *pszPkgName);
310 DWORD CheckNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType, WORD wPort = 0,
311 WORD wProto = 0, const TCHAR *pszRequest = NULL, const TCHAR *pszResponse = NULL);
312 DWORD GetSupportedParameters(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList);
313 DWORD GetConfigFile(TCHAR **ppszConfig, DWORD *pdwSize);
45d84f8a 314 DWORD updateConfigFile(const TCHAR *pszConfig);
7c521895 315 DWORD enableTraps();
e58b5e54
VK
316
317 DWORD generateRequestId() { return m_dwRequestId++; }
45d84f8a 318 CSCPMessage *customRequest(CSCPMessage *pRequest, const TCHAR *recvFile = NULL, bool appendFile = false,
f2665675 319 void (*downloadProgressCallback)(size_t, void *) = NULL, void *cbArg = NULL);
5039dede 320
7c521895
VK
321 DWORD getNumDataLines() { return m_dwNumDataLines; }
322 const TCHAR *getDataLine(DWORD dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
5039dede 323
7c521895
VK
324 void setConnectionTimeout(DWORD dwTimeout) { m_connectionTimeout = max(dwTimeout, 1000); }
325 DWORD getConnectionTimeout() { return m_connectionTimeout; }
45d84f8a
VK
326 void setCommandTimeout(DWORD dwTimeout) { m_dwCommandTimeout = max(dwTimeout, 500); }
327 DWORD getCommandTimeout() { return m_dwCommandTimeout; }
7c521895
VK
328 void setRecvTimeout(DWORD dwTimeout) { m_dwRecvTimeout = max(dwTimeout, 10000); }
329 void setEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
330 void setProxy(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
5039dede 331 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
7c521895
VK
332 void setPort(WORD wPort) { m_wPort = wPort; }
333 void setAuthData(int nMethod, const char *pszSecret) { m_iAuthMethod = nMethod; nx_strncpy(m_szSecret, pszSecret, MAX_SECRET_LENGTH); }
901a5a9b 334 void setDeleteFileOnDownloadFailure(bool flag) { m_deleteFileOnDownloadFailure = flag; }
5039dede
AK
335};
336
337
338//
339// Proxy SNMP transport
340//
341
342class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
343{
344protected:
345 AgentConnection *m_pAgentConnection;
346 CSCPMessage *m_pResponse;
347 DWORD m_dwIpAddr;
348 WORD m_wPort;
349
350public:
351 SNMP_ProxyTransport(AgentConnection *pConn, DWORD dwIpAddr, WORD wPort);
352 virtual ~SNMP_ProxyTransport();
353
c4366266 354 virtual int readMessage(SNMP_PDU **ppData, DWORD dwTimeout = INFINITE,
cd9f247e
VK
355 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL,
356 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
c4366266 357 virtual int sendMessage(SNMP_PDU *pdu);
5039dede
AK
358};
359
360
361//
362// ISC flags
363//
364
365#define ISCF_IS_CONNECTED ((DWORD)0x00000001)
366#define ISCF_REQUIRE_ENCRYPTION ((DWORD)0x00000002)
367
368
369//
370// Inter-server connection (ISC)
371//
372
373class LIBNXSRV_EXPORTABLE ISC
374{
375private:
376 DWORD m_flags;
377 DWORD m_addr;
378 WORD m_port;
379 SOCKET m_socket;
380 int m_protocolVersion;
381 DWORD m_requestId;
382 DWORD m_recvTimeout;
383 MsgWaitQueue *m_msgWaitQueue;
384 MUTEX m_mutexDataLock;
385 THREAD m_hReceiverThread;
386 CSCP_ENCRYPTION_CONTEXT *m_ctx;
387 DWORD m_commandTimeout;
388
389 void ReceiverThread(void);
390 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
391
392protected:
393 void DestroyResultData(void);
394 DWORD SetupEncryption(RSA *pServerKey);
395 DWORD ConnectToService(DWORD service);
396
397 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
398 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
399
400 virtual void PrintMsg(const TCHAR *format, ...);
401
402public:
403 ISC();
404 ISC(DWORD addr, WORD port = NETXMS_ISC_PORT);
405 virtual ~ISC();
406
407 DWORD Connect(DWORD service, RSA *serverKey = NULL, BOOL requireEncryption = FALSE);
408 void Disconnect();
409
410 BOOL SendMessage(CSCPMessage *msg);
411 CSCPMessage *WaitForMessage(WORD code, DWORD id, DWORD timeOut) { return m_msgWaitQueue->WaitForMessage(code, id, timeOut); }
412 DWORD WaitForRCC(DWORD rqId, DWORD timeOut);
413
414 DWORD Nop(void);
415};
416
417
418//
419// Functions
420//
421
422void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
423void LIBNXSRV_EXPORTABLE DestroyInterfaceList(INTERFACE_LIST *pIfList);
424void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
425void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
426const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError);
427
428void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...);
429void LIBNXSRV_EXPORTABLE DbgPrintf(int level, const TCHAR *format, ...);
9d88cdc9 430void LIBNXSRV_EXPORTABLE DbgPrintf2(int level, const TCHAR *format, va_list args);
c20b2798 431
5039dede
AK
432void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
433
434const TCHAR LIBNXSRV_EXPORTABLE *ISCErrorCodeToText(DWORD code);
435
436
437//
438// Variables
439//
440
441extern DWORD LIBNXSRV_EXPORTABLE g_dwFlags;
442extern int LIBNXSRV_EXPORTABLE g_nDebugLevel;
5039dede
AK
443
444#endif /* _nxsrvapi_h_ */