added missing file
[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
5ad2167d
VK
136//
137// Flags for SnmpGet
138//
139
140#define SG_VERBOSE 0x0001
141#define SG_STRING_RESULT 0x0002
142#define SG_RAW_RESULT 0x0004
143#define SG_HSTRING_RESULT 0x0008
144
145
5039dede
AK
146//
147// Win32 service and syslog constants
148//
149
150#ifdef _WIN32
151
152#define CORE_SERVICE_NAME _T("NetXMSCore")
153#define CORE_EVENT_SOURCE _T("NetXMSCore")
154#define NETXMSD_SYSLOG_NAME CORE_EVENT_SOURCE
155
156#else
157
158#define NETXMSD_SYSLOG_NAME _T("netxmsd")
159
160#endif /* _WIN32 */
161
162
163//
164// Single ARP cache entry
165//
166
167typedef struct
168{
169 DWORD dwIndex; // Interface index
170 DWORD dwIpAddr;
171 BYTE bMacAddr[MAC_ADDR_LENGTH];
172} ARP_ENTRY;
173
174
175//
176// ARP cache structure used by discovery functions and AgentConnection class
177//
178
179typedef struct
180{
181 DWORD dwNumEntries;
182 ARP_ENTRY *pEntries;
183} ARP_CACHE;
184
185
186//
187// Interface information structure used by discovery functions and AgentConnection class
188//
189
190typedef struct
191{
192 TCHAR szName[MAX_DB_STRING];
193 DWORD dwIndex;
194 DWORD dwType;
eec253a8
VK
195 DWORD dwBridgePortNumber;
196 DWORD dwSlotNumber;
197 DWORD dwPortNumber;
5039dede
AK
198 DWORD dwIpAddr;
199 DWORD dwIpNetMask;
200 BYTE bMacAddr[MAC_ADDR_LENGTH];
201 int iNumSecondary; // Number of secondary IP's on this interface
202} INTERFACE_INFO;
203
204
205//
206// Interface list used by discovery functions and AgentConnection class
207//
208
98762401 209class LIBNXSRV_EXPORTABLE InterfaceList
5039dede 210{
98762401
VK
211private:
212 int m_size; // Number of valid entries
213 int m_allocated; // Number of allocated entries
214 void *m_data; // Can be used by custom enumeration handlers
215 INTERFACE_INFO *m_interfaces; // Interface entries
216
217public:
218 InterfaceList(int initialAlloc = 8);
219 ~InterfaceList();
220
221 void add(INTERFACE_INFO *iface);
222 void remove(int index);
223 void removeLoopbacks();
224
225 int getSize() { return m_size; }
226 INTERFACE_INFO *get(int index) { return ((index >= 0) && (index < m_size)) ? &m_interfaces[index] : NULL; }
227
228 void setData(void *data) { m_data = data; }
229 void *getData() { return m_data; }
230};
5039dede
AK
231
232
233//
234// Route information
235//
236
237typedef struct
238{
239 DWORD dwDestAddr;
240 DWORD dwDestMask;
241 DWORD dwNextHop;
242 DWORD dwIfIndex;
243 DWORD dwRouteType;
244} ROUTE;
245
246
247//
248// Routing table
249//
250
251typedef struct
252{
253 int iNumEntries; // Number of entries
254 ROUTE *pRoutes; // Route list
255} ROUTING_TABLE;
256
257
1f385e47
VK
258//
259// Information about policies installed on agent
260//
261
262class LIBNXSRV_EXPORTABLE AgentPolicyInfo
263{
264private:
265 int m_size;
266 BYTE *m_guidList;
267 int *m_typeList;
268 TCHAR **m_serverList;
269
270public:
271 AgentPolicyInfo(CSCPMessage *msg);
272 ~AgentPolicyInfo();
273
274 int getSize() { return m_size; }
275 bool getGuid(int index, uuid_t guid);
276 int getType(int index) { return ((index >= 0) && (index < m_size)) ? m_typeList[index] : -1; }
277 const TCHAR *getServer(int index) { return ((index >= 0) && (index < m_size)) ? m_serverList[index] : NULL; }
278};
279
280
5039dede
AK
281//
282// Agent connection
283//
284
285class LIBNXSRV_EXPORTABLE AgentConnection
286{
287private:
288 DWORD m_dwAddr;
289 int m_nProtocolVersion;
290 int m_iAuthMethod;
08b214c6 291 char m_szSecret[MAX_SECRET_LENGTH];
5039dede
AK
292 time_t m_tLastCommandTime;
293 SOCKET m_hSocket;
294 DWORD m_dwNumDataLines;
295 DWORD m_dwRequestId;
296 DWORD m_dwCommandTimeout;
7c521895 297 DWORD m_connectionTimeout;
5039dede
AK
298 DWORD m_dwRecvTimeout;
299 TCHAR **m_ppDataLines;
300 MsgWaitQueue *m_pMsgWaitQueue;
301 BOOL m_bIsConnected;
302 MUTEX m_mutexDataLock;
303 THREAD m_hReceiverThread;
304 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
305 int m_iEncryptionPolicy;
306 BOOL m_bUseProxy;
307 DWORD m_dwProxyAddr;
308 WORD m_wPort;
309 WORD m_wProxyPort;
310 int m_iProxyAuth;
08b214c6 311 char m_szProxySecret[MAX_SECRET_LENGTH];
5039dede 312 int m_hCurrFile;
9f6d453a 313 TCHAR m_currentFileName[MAX_PATH];
bb85e341
VK
314 DWORD m_dwDownloadRequestId;
315 CONDITION m_condFileDownload;
316 BOOL m_fileDownloadSucceeded;
317 void (*m_downloadProgressCallback)(size_t, void *);
318 void *m_downloadProgressCallbackArg;
901a5a9b 319 bool m_deleteFileOnDownloadFailure;
4685a2ad 320 bool m_fileUploadInProgress;
5039dede
AK
321
322 void ReceiverThread(void);
323 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
324
325protected:
7c521895
VK
326 void destroyResultData();
327 BOOL sendMessage(CSCPMessage *pMsg);
328 CSCPMessage *waitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut) { return m_pMsgWaitQueue->WaitForMessage(wCode, dwId, dwTimeOut); }
329 DWORD waitForRCC(DWORD dwRqId, DWORD dwTimeOut);
330 DWORD setupEncryption(RSA *pServerKey);
331 DWORD authenticate(BOOL bProxyData);
332 DWORD setupProxyConnection();
333 DWORD getIpAddr() { return ntohl(m_dwAddr); }
334 DWORD prepareFileDownload(const TCHAR *fileName, DWORD rqId, bool append, void (*downloadProgressCallback)(size_t, void *), void *cbArg);
5039dede
AK
335
336 virtual void PrintMsg(const TCHAR *pszFormat, ...);
f480bdd4
VK
337 virtual void onTrap(CSCPMessage *pMsg);
338 virtual void onDataPush(CSCPMessage *msg);
90284364 339 virtual bool processCustomMessage(CSCPMessage *pMsg);
f480bdd4 340 virtual void onFileDownload(BOOL success);
5039dede
AK
341
342 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
343 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
344
345public:
5039dede
AK
346 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
347 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
348 virtual ~AgentConnection();
349
c3acd0f6 350 BOOL connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, DWORD *pdwError = NULL, DWORD *pdwSocketError = NULL);
7c521895
VK
351 void disconnect();
352 BOOL isConnected() { return m_bIsConnected; }
353 int getProtocolVersion() { return m_nProtocolVersion; }
5039dede 354
45d84f8a 355 SOCKET getSocket() { return m_hSocket; }
5a9e58a3 356
4687826e 357 ARP_CACHE *getArpCache();
98762401 358 InterfaceList *getInterfaceList();
4687826e
VK
359 ROUTING_TABLE *getRoutingTable();
360 DWORD getParameter(const TCHAR *pszParam, DWORD dwBufSize, TCHAR *pszBuffer);
361 DWORD getList(const TCHAR *pszParam);
362 DWORD getTable(const TCHAR *pszParam, Table **table);
7c521895 363 DWORD nop();
4687826e 364 DWORD execAction(const TCHAR *pszAction, int argc, TCHAR **argv);
619e5c9b 365 DWORD uploadFile(const TCHAR *localFile, const TCHAR *destinationFile = NULL, void (* progressCallback)(INT64, void *) = NULL, void *cbArg = NULL);
4687826e
VK
366 DWORD startUpgrade(const TCHAR *pszPkgName);
367 DWORD checkNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType, WORD wPort = 0,
5039dede 368 WORD wProto = 0, const TCHAR *pszRequest = NULL, const TCHAR *pszResponse = NULL);
4687826e
VK
369 DWORD getSupportedParameters(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList);
370 DWORD getConfigFile(TCHAR **ppszConfig, DWORD *pdwSize);
45d84f8a 371 DWORD updateConfigFile(const TCHAR *pszConfig);
7c521895 372 DWORD enableTraps();
1f385e47
VK
373 DWORD getPolicyInventory(AgentPolicyInfo **info);
374 DWORD uninstallPolicy(uuid_t guid);
e58b5e54
VK
375
376 DWORD generateRequestId() { return m_dwRequestId++; }
45d84f8a 377 CSCPMessage *customRequest(CSCPMessage *pRequest, const TCHAR *recvFile = NULL, bool appendFile = false,
f2665675 378 void (*downloadProgressCallback)(size_t, void *) = NULL, void *cbArg = NULL);
5039dede 379
7c521895
VK
380 DWORD getNumDataLines() { return m_dwNumDataLines; }
381 const TCHAR *getDataLine(DWORD dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
5039dede 382
7c521895
VK
383 void setConnectionTimeout(DWORD dwTimeout) { m_connectionTimeout = max(dwTimeout, 1000); }
384 DWORD getConnectionTimeout() { return m_connectionTimeout; }
45d84f8a
VK
385 void setCommandTimeout(DWORD dwTimeout) { m_dwCommandTimeout = max(dwTimeout, 500); }
386 DWORD getCommandTimeout() { return m_dwCommandTimeout; }
7c521895
VK
387 void setRecvTimeout(DWORD dwTimeout) { m_dwRecvTimeout = max(dwTimeout, 10000); }
388 void setEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
389 void setProxy(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
5039dede 390 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
7c521895 391 void setPort(WORD wPort) { m_wPort = wPort; }
08b214c6 392 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 393 void setDeleteFileOnDownloadFailure(bool flag) { m_deleteFileOnDownloadFailure = flag; }
5039dede
AK
394};
395
396
397//
398// Proxy SNMP transport
399//
400
401class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
402{
403protected:
404 AgentConnection *m_pAgentConnection;
405 CSCPMessage *m_pResponse;
406 DWORD m_dwIpAddr;
407 WORD m_wPort;
408
409public:
410 SNMP_ProxyTransport(AgentConnection *pConn, DWORD dwIpAddr, WORD wPort);
411 virtual ~SNMP_ProxyTransport();
412
c4366266 413 virtual int readMessage(SNMP_PDU **ppData, DWORD dwTimeout = INFINITE,
cd9f247e
VK
414 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL,
415 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
c4366266 416 virtual int sendMessage(SNMP_PDU *pdu);
5039dede
AK
417};
418
419
420//
421// ISC flags
422//
423
424#define ISCF_IS_CONNECTED ((DWORD)0x00000001)
425#define ISCF_REQUIRE_ENCRYPTION ((DWORD)0x00000002)
426
427
428//
429// Inter-server connection (ISC)
430//
431
432class LIBNXSRV_EXPORTABLE ISC
433{
434private:
435 DWORD m_flags;
436 DWORD m_addr;
437 WORD m_port;
438 SOCKET m_socket;
439 int m_protocolVersion;
440 DWORD m_requestId;
441 DWORD m_recvTimeout;
442 MsgWaitQueue *m_msgWaitQueue;
443 MUTEX m_mutexDataLock;
444 THREAD m_hReceiverThread;
445 CSCP_ENCRYPTION_CONTEXT *m_ctx;
446 DWORD m_commandTimeout;
447
448 void ReceiverThread(void);
449 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
450
451protected:
452 void DestroyResultData(void);
453 DWORD SetupEncryption(RSA *pServerKey);
454 DWORD ConnectToService(DWORD service);
455
456 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
457 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
458
459 virtual void PrintMsg(const TCHAR *format, ...);
460
461public:
462 ISC();
463 ISC(DWORD addr, WORD port = NETXMS_ISC_PORT);
464 virtual ~ISC();
465
466 DWORD Connect(DWORD service, RSA *serverKey = NULL, BOOL requireEncryption = FALSE);
467 void Disconnect();
468
469 BOOL SendMessage(CSCPMessage *msg);
470 CSCPMessage *WaitForMessage(WORD code, DWORD id, DWORD timeOut) { return m_msgWaitQueue->WaitForMessage(code, id, timeOut); }
471 DWORD WaitForRCC(DWORD rqId, DWORD timeOut);
472
473 DWORD Nop(void);
474};
475
476
477//
478// Functions
479//
480
481void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
5039dede
AK
482void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
483void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
484const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError);
485
486void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...);
487void LIBNXSRV_EXPORTABLE DbgPrintf(int level, const TCHAR *format, ...);
9d88cdc9 488void LIBNXSRV_EXPORTABLE DbgPrintf2(int level, const TCHAR *format, va_list args);
c20b2798 489
5039dede
AK
490void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
491
492const TCHAR LIBNXSRV_EXPORTABLE *ISCErrorCodeToText(DWORD code);
493
5ad2167d
VK
494DWORD LIBNXSRV_EXPORTABLE SnmpNewRequestId();
495DWORD LIBNXSRV_EXPORTABLE SnmpGet(DWORD dwVersion, SNMP_Transport *pTransport,
496 const TCHAR *szOidStr, const DWORD *oidBinary, DWORD dwOidLen, void *pValue,
497 DWORD dwBufferSize, DWORD dwFlags);
498DWORD LIBNXSRV_EXPORTABLE SnmpEnumerate(DWORD dwVersion, SNMP_Transport *pTransport, const TCHAR *szRootOid,
499 DWORD (* pHandler)(DWORD, SNMP_Variable *, SNMP_Transport *, void *),
500 void *pUserArg, BOOL bVerbose);
501
5039dede
AK
502
503//
504// Variables
505//
506
507extern DWORD LIBNXSRV_EXPORTABLE g_dwFlags;
5ad2167d 508extern DWORD LIBNXSRV_EXPORTABLE g_dwSNMPTimeout;
5039dede 509extern int LIBNXSRV_EXPORTABLE g_nDebugLevel;
5039dede
AK
510
511#endif /* _nxsrvapi_h_ */