added NXSL function AgentExecuteAction
[public/netxms.git] / src / server / include / nxsrvapi.h
CommitLineData
76b4edb5 1/*
5039dede
AK
2** NetXMS - Network Management System
3** Server Library
33560996 4** Copyright (C) 2003-2015 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
5039dede 37#include <nxcpapi.h>
bf6fb6c3 38#include <nms_util.h>
5039dede 39#include <nms_agent.h>
5039dede
AK
40#include <nxsnmp.h>
41#include <netxms_isc.h>
bf6fb6c3 42#include <nxcldefs.h>
5039dede 43
f1a3b4fe 44#ifdef INCLUDE_LIBNXSRV_MESSAGES
45#include "../libnxsrv/messages.h"
46#endif
47
f375019e
VK
48/**
49 * Default files
50 */
5039dede
AK
51#ifdef _WIN32
52
1288683d 53#define DEFAULT_LOG_FILE _T("C:\\netxmsd.log")
1039d7ee 54#define DEFAULT_DUMP_DIR _T("C:\\")
9796ce45 55
03b96461
VK
56#define LDIR_NDD _T("\\ndd")
57#define LDIR_PDSDRV _T("\\pdsdrv")
58
9796ce45
VK
59#define DDIR_PACKAGES _T("\\packages")
60#define DDIR_BACKGROUNDS _T("\\backgrounds")
9796ce45 61#define DFILE_KEYS _T("\\server_key")
1039d7ee 62#define DFILE_COMPILED_MIB _T("\\netxms.mib")
9796ce45
VK
63#define DDIR_IMAGES _T("\\images")
64#define DDIR_FILES _T("\\files")
5039dede 65
03b96461 66#define DDIR_TEMPLATES _T("\\templates")
5039dede 67
1039d7ee 68#else /* _WIN32 */
5039dede 69
9796ce45 70#ifndef DATADIR
1039d7ee
VK
71#define DATADIR _T("/usr/share/netxms")
72#endif
73
74#ifndef STATEDIR
75#define STATEDIR _T("/var/lib/netxms")
9796ce45 76#endif
5039dede 77
9796ce45
VK
78#ifndef LIBDIR
79#define LIBDIR _T("/usr/lib")
80#endif
5039dede 81
644f97f5
VK
82#ifndef PKGLIBDIR
83#define PKGLIBDIR _T("/usr/lib/netxms")
84#endif
85
1288683d 86#define DEFAULT_LOG_FILE _T("/var/log/netxmsd.log")
1288683d 87#define DEFAULT_DUMP_DIR _T("/var/tmp")
9796ce45
VK
88
89#define LDIR_NDD _T("/ndd")
8fd95c92 90#define LDIR_PDSDRV _T("/pdsdrv")
9796ce45 91
9796ce45
VK
92#define DDIR_PACKAGES _T("/packages")
93#define DDIR_BACKGROUNDS _T("/backgrounds")
9796ce45 94#define DFILE_KEYS _T("/.server_key")
1039d7ee 95#define DFILE_COMPILED_MIB _T("/netxms.mib")
9796ce45
VK
96#define DDIR_IMAGES _T("/images")
97#define DDIR_FILES _T("/files")
5039dede 98
03b96461
VK
99#define DDIR_TEMPLATES _T("/templates")
100
5039dede
AK
101#endif /* _WIN32 */
102
f375019e
VK
103/**
104 * Application flags
105 */
c8076b19
VK
106#define AF_DAEMON _ULL(0x0000000000000001)
107#define AF_USE_SYSLOG _ULL(0x0000000000000002)
108#define AF_ENABLE_NETWORK_DISCOVERY _ULL(0x0000000000000004)
109#define AF_ACTIVE_NETWORK_DISCOVERY _ULL(0x0000000000000008)
110#define AF_LOG_SQL_ERRORS _ULL(0x0000000000000010)
111#define AF_DELETE_EMPTY_SUBNETS _ULL(0x0000000000000020)
112#define AF_ENABLE_SNMP_TRAPD _ULL(0x0000000000000040)
113#define AF_ENABLE_ZONING _ULL(0x0000000000000080)
114#define AF_SYNC_NODE_NAMES_WITH_DNS _ULL(0x0000000000000100)
115#define AF_CHECK_TRUSTED_NODES _ULL(0x0000000000000200)
116#define AF_ENABLE_NXSL_CONTAINER_FUNCS _ULL(0x0000000000000400)
117#define AF_USE_FQDN_FOR_NODE_NAMES _ULL(0x0000000000000800)
118#define AF_APPLY_TO_DISABLED_DCI_FROM_TEMPLATE _ULL(0x0000000000001000)
119#define AF_DEBUG_CONSOLE_DISABLED _ULL(0x0000000000002000)
120#define AF_ENABLE_OBJECT_TRANSACTIONS _ULL(0x0000000000004000)
121#define AF_WRITE_FULL_DUMP _ULL(0x0000000000080000)
122#define AF_RESOLVE_NODE_NAMES _ULL(0x0000000000100000)
123#define AF_CATCH_EXCEPTIONS _ULL(0x0000000000200000)
124#define AF_HELPDESK_LINK_ACTIVE _ULL(0x0000000000400000)
c8076b19 125#define AF_DB_LOCKED _ULL(0x0000000001000000)
c8076b19
VK
126#define AF_DB_CONNECTION_LOST _ULL(0x0000000004000000)
127#define AF_NO_NETWORK_CONNECTIVITY _ULL(0x0000000008000000)
128#define AF_EVENT_STORM_DETECTED _ULL(0x0000000010000000)
129#define AF_SNMP_TRAP_DISCOVERY _ULL(0x0000000020000000)
130#define AF_TRAPS_FROM_UNMANAGED_NODES _ULL(0x0000000040000000)
385b1f20 131#define AF_RESOLVE_IP_FOR_EACH_STATUS_POLL _ULL(0x0000000080000000)
4e0e77e6
VK
132#define AF_PERFDATA_STORAGE_DRIVER_LOADED _ULL(0x0000000100000000)
133#define AF_BACKGROUND_LOG_WRITER _ULL(0x0000000200000000)
8a1519ce 134#define AF_CASE_INSENSITIVE_LOGINS _ULL(0x0000000400000000)
0eff2ce4 135#define AF_TRAP_SOURCES_IN_ALL_ZONES _ULL(0x0000000800000000)
c8076b19
VK
136#define AF_SERVER_INITIALIZED _ULL(0x4000000000000000)
137#define AF_SHUTDOWN _ULL(0x8000000000000000)
5039dede 138
f375019e
VK
139/**
140 * Encryption usage policies
141 */
5039dede
AK
142#define ENCRYPTION_DISABLED 0
143#define ENCRYPTION_ALLOWED 1
144#define ENCRYPTION_PREFERRED 2
145#define ENCRYPTION_REQUIRED 3
146
908d71bd
VK
147/**
148 * Agent action output callback events
149 */
150enum ActionCallbackEvent
151{
152 ACE_CONNECTED = 0,
153 ACE_DATA = 1,
154 ACE_DISCONNECTED = 2
155};
156
f375019e
VK
157/**
158 * Win32 service and syslog constants
159 */
5039dede
AK
160#ifdef _WIN32
161
162#define CORE_SERVICE_NAME _T("NetXMSCore")
163#define CORE_EVENT_SOURCE _T("NetXMSCore")
164#define NETXMSD_SYSLOG_NAME CORE_EVENT_SOURCE
165
166#else
167
168#define NETXMSD_SYSLOG_NAME _T("netxmsd")
169
170#endif /* _WIN32 */
171
f375019e
VK
172/**
173 * Single ARP cache entry
174 */
5039dede
AK
175typedef struct
176{
967893bb 177 UINT32 dwIndex; // Interface index
c75e9ee4 178 InetAddress ipAddr;
5039dede
AK
179 BYTE bMacAddr[MAC_ADDR_LENGTH];
180} ARP_ENTRY;
181
f375019e
VK
182/**
183 * ARP cache structure used by discovery functions and AgentConnection class
184 */
5039dede
AK
185typedef struct
186{
967893bb 187 UINT32 dwNumEntries;
5039dede
AK
188 ARP_ENTRY *pEntries;
189} ARP_CACHE;
190
f375019e
VK
191/**
192 * Interface information structure used by discovery functions and AgentConnection class
193 */
c30c0c0f 194class InterfaceInfo
5039dede 195{
3d37f7bf
VK
196private:
197 void init()
198 {
199 name[0] = 0;
200 description[0] = 0;
201 alias[0] = 0;
202 type = IFTYPE_OTHER;
203 mtu = 0;
204 speed = 0;
205 bridgePort = 0;
206 slot = 0;
207 port = 0;
208 memset(macAddr, 0, sizeof(macAddr));
209 isPhysicalPort = false;
210 isSystem = false;
211 }
212
c30c0c0f
VK
213public:
214 UINT32 index;
e95680e5
VK
215 TCHAR name[MAX_DB_STRING]; // Interface display name
216 TCHAR description[MAX_DB_STRING]; // Value of ifDescr MIB variable for SNMP agents
217 TCHAR alias[MAX_DB_STRING]; // Value of ifDescr MIB variable for SNMP agents
e95680e5
VK
218 UINT32 type;
219 UINT32 mtu;
3d37f7bf 220 UINT64 speed; // interface speed in bits/sec
e95680e5
VK
221 UINT32 bridgePort;
222 UINT32 slot;
223 UINT32 port;
c30c0c0f 224 InetAddressList ipAddrList;
e95680e5 225 BYTE macAddr[MAC_ADDR_LENGTH];
4c16cdc7 226 bool isPhysicalPort;
01152a54 227 bool isSystem;
3d37f7bf
VK
228 UINT32 ifTableSuffix[16]; // actual ifTable suffix
229 int ifTableSuffixLength;
c30c0c0f
VK
230
231 InterfaceInfo(UINT32 ifIndex)
e13420c1 232 {
c30c0c0f 233 index = ifIndex;
3d37f7bf
VK
234 ifTableSuffixLength = 0;
235 init();
236 }
237
e5f4e3ac 238 InterfaceInfo(UINT32 ifIndex, int suffixLen, const UINT32 *suffix)
3d37f7bf
VK
239 {
240 index = ifIndex;
241 ifTableSuffixLength = ((suffixLen >= 0) && (suffixLen < 16)) ? suffixLen : 0;
242 memcpy(ifTableSuffix, suffix, ifTableSuffixLength * sizeof(UINT32));
243 init();
c30c0c0f
VK
244 }
245
246 bool hasAddress(const InetAddress& addr) { return ipAddrList.hasAddress(addr); }
247};
5039dede 248
f375019e
VK
249/**
250 * Interface list used by discovery functions and AgentConnection class
251 */
98762401 252class LIBNXSRV_EXPORTABLE InterfaceList
5039dede 253{
98762401 254private:
c30c0c0f 255 ObjectArray<InterfaceInfo> *m_interfaces;
98762401 256 void *m_data; // Can be used by custom enumeration handlers
f34e7a6e 257 bool m_needPrefixWalk;
98762401
VK
258
259public:
260 InterfaceList(int initialAlloc = 8);
261 ~InterfaceList();
262
c30c0c0f
VK
263 void add(InterfaceInfo *iface) { m_interfaces->add(iface); }
264 void remove(int index) { m_interfaces->remove(index); }
98762401 265
c30c0c0f
VK
266 int size() { return m_interfaces->size(); }
267 InterfaceInfo *get(int index) { return m_interfaces->get(index); }
268 InterfaceInfo *findByIfIndex(UINT32 ifIndex);
98762401
VK
269
270 void setData(void *data) { m_data = data; }
271 void *getData() { return m_data; }
f34e7a6e
VK
272
273 bool isPrefixWalkNeeded() { return m_needPrefixWalk; }
274 void setPrefixWalkNeeded() { m_needPrefixWalk = true; }
98762401 275};
5039dede 276
f375019e
VK
277/**
278 * Vlan information
279 */
0208aa8c
VK
280#define VLAN_PRM_IFINDEX 0
281#define VLAN_PRM_SLOTPORT 1
5eb3a790 282#define VLAN_PRM_BPORT 2
0208aa8c 283
77c7801c
VK
284class LIBNXSRV_EXPORTABLE VlanInfo
285{
286private:
287 int m_vlanId;
288 TCHAR *m_name;
0208aa8c 289 int m_portRefMode; // Port reference mode - by ifIndex or by slot/port
77c7801c
VK
290 int m_allocated;
291 int m_numPorts; // Number of ports in VLAN
967893bb
VK
292 UINT32 *m_ports; // member ports (slot/port pairs or ifIndex)
293 UINT32 *m_indexes; // ifIndexes for ports
294 UINT32 *m_ids; // Interface object IDs for ports
77c7801c
VK
295
296public:
0208aa8c 297 VlanInfo(int vlanId, int prm);
77c7801c
VK
298 ~VlanInfo();
299
300 int getVlanId() { return m_vlanId; }
0208aa8c 301 int getPortReferenceMode() { return m_portRefMode; }
77c7801c
VK
302 const TCHAR *getName() { return CHECK_NULL_EX(m_name); }
303 int getNumPorts() { return m_numPorts; }
967893bb
VK
304 UINT32 *getPorts() { return m_ports; }
305 UINT32 *getIfIndexes() { return m_indexes; }
306 UINT32 *getIfIds() { return m_ids; }
77c7801c 307
967893bb
VK
308 void add(UINT32 slot, UINT32 port);
309 void add(UINT32 ifIndex);
77c7801c 310 void setName(const TCHAR *name);
b1bd1fb2
VK
311
312 void prepareForResolve();
967893bb 313 void resolvePort(int index, UINT32 sp, UINT32 ifIndex, UINT32 id);
77c7801c
VK
314};
315
a3050773
VK
316/**
317 * Vlan list
318 */
7f632dfe 319class LIBNXSRV_EXPORTABLE VlanList : public RefCountObject
77c7801c
VK
320{
321private:
322 int m_size; // Number of valid entries
323 int m_allocated; // Number of allocated entries
324 void *m_data; // Can be used by custom enumeration handlers
325 VlanInfo **m_vlans; // VLAN entries
326
327public:
328 VlanList(int initialAlloc = 8);
7f632dfe 329 virtual ~VlanList();
77c7801c
VK
330
331 void add(VlanInfo *vlan);
967893bb 332 void addMemberPort(int vlanId, UINT32 portId);
77c7801c 333
a6312bd6 334 int size() { return m_size; }
77c7801c 335 VlanInfo *get(int index) { return ((index >= 0) && (index < m_size)) ? m_vlans[index] : NULL; }
0208aa8c 336 VlanInfo *findById(int id);
0239cfea 337 VlanInfo *findByName(const TCHAR *name);
77c7801c
VK
338
339 void setData(void *data) { m_data = data; }
340 void *getData() { return m_data; }
7f632dfe 341
b368969c 342 void fillMessage(NXCPMessage *msg);
77c7801c
VK
343};
344
a3050773
VK
345/**
346 * Route information
347 */
5039dede
AK
348typedef struct
349{
967893bb
VK
350 UINT32 dwDestAddr;
351 UINT32 dwDestMask;
352 UINT32 dwNextHop;
353 UINT32 dwIfIndex;
354 UINT32 dwRouteType;
5039dede
AK
355} ROUTE;
356
a3050773
VK
357/**
358 * Routing table
359 */
5039dede
AK
360typedef struct
361{
362 int iNumEntries; // Number of entries
363 ROUTE *pRoutes; // Route list
364} ROUTING_TABLE;
365
a3050773
VK
366/**
367 * Information about policies installed on agent
368 */
1f385e47
VK
369class LIBNXSRV_EXPORTABLE AgentPolicyInfo
370{
371private:
372 int m_size;
373 BYTE *m_guidList;
374 int *m_typeList;
7f6ecb6d
TD
375 TCHAR **m_serverInfoList;
376 UINT64 *m_serverIdList;
377 int *m_version;
1f385e47
VK
378
379public:
b368969c 380 AgentPolicyInfo(NXCPMessage *msg);
1f385e47
VK
381 ~AgentPolicyInfo();
382
a6312bd6 383 int size() { return m_size; }
de4af576 384 uuid getGuid(int index);
1f385e47 385 int getType(int index) { return ((index >= 0) && (index < m_size)) ? m_typeList[index] : -1; }
7f6ecb6d
TD
386 const TCHAR *getServerInfo(int index) { return ((index >= 0) && (index < m_size)) ? m_serverInfoList[index] : NULL; }
387 UINT64 getServerId(int index) { return ((index >= 0) && (index < m_size)) ? m_serverIdList[index] : -1; }
388 int getVersion(int index) { return ((index >= 0) && (index < m_size)) ? m_version[index] : -1; }
1f385e47
VK
389};
390
86c126f5
VK
391/**
392 * Agent parameter definition
393 */
394class LIBNXSRV_EXPORTABLE AgentParameterDefinition
395{
396private:
397 TCHAR *m_name;
398 TCHAR *m_description;
399 int m_dataType;
400
401public:
b368969c 402 AgentParameterDefinition(NXCPMessage *msg, UINT32 baseId);
86c126f5
VK
403 AgentParameterDefinition(AgentParameterDefinition *src);
404 ~AgentParameterDefinition();
405
b368969c 406 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
86c126f5
VK
407
408 const TCHAR *getName() { return m_name; }
409 const TCHAR *getDescription() { return m_description; }
410 int getDataType() { return m_dataType; }
411};
412
413/**
414 * Agent table column definition
415 */
416struct AgentTableColumnDefinition
417{
418 TCHAR m_name[MAX_COLUMN_NAME];
419 int m_dataType;
420
421 AgentTableColumnDefinition(AgentTableColumnDefinition *src)
422 {
423 nx_strncpy(m_name, src->m_name, MAX_COLUMN_NAME);
424 m_dataType = src->m_dataType;
425 }
426};
427
428/**
429 * Agent table definition
430 */
431class LIBNXSRV_EXPORTABLE AgentTableDefinition
432{
433private:
434 TCHAR *m_name;
435 TCHAR *m_description;
436 StringList *m_instanceColumns;
437 ObjectArray<AgentTableColumnDefinition> *m_columns;
438
439public:
b368969c 440 AgentTableDefinition(NXCPMessage *msg, UINT32 baseId);
86c126f5
VK
441 AgentTableDefinition(AgentTableDefinition *src);
442 ~AgentTableDefinition();
443
b368969c 444 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
86c126f5
VK
445
446 const TCHAR *getName() { return m_name; }
447 const TCHAR *getDescription() { return m_description; }
448};
449
a3050773
VK
450/**
451 * Agent connection
452 */
a87ef571 453class LIBNXSRV_EXPORTABLE AgentConnection
5039dede
AK
454{
455private:
a87ef571
VK
456 VolatileCounter m_userRefCount;
457 VolatileCounter m_internalRefCount;
c11eee9b 458 InetAddress m_addr;
5039dede
AK
459 int m_nProtocolVersion;
460 int m_iAuthMethod;
08b214c6 461 char m_szSecret[MAX_SECRET_LENGTH];
5039dede
AK
462 time_t m_tLastCommandTime;
463 SOCKET m_hSocket;
967893bb 464 UINT32 m_dwNumDataLines;
e9902466 465 VolatileCounter m_requestId;
967893bb
VK
466 UINT32 m_dwCommandTimeout;
467 UINT32 m_connectionTimeout;
468 UINT32 m_dwRecvTimeout;
5039dede
AK
469 TCHAR **m_ppDataLines;
470 MsgWaitQueue *m_pMsgWaitQueue;
9fd816cc 471 bool m_isConnected;
5039dede 472 MUTEX m_mutexDataLock;
d3a7cf4c 473 MUTEX m_mutexSocketWrite;
5039dede 474 THREAD m_hReceiverThread;
98abc9f1 475 NXCPEncryptionContext *m_pCtx;
5039dede
AK
476 int m_iEncryptionPolicy;
477 BOOL m_bUseProxy;
c11eee9b 478 InetAddress m_proxyAddr;
5039dede
AK
479 WORD m_wPort;
480 WORD m_wProxyPort;
481 int m_iProxyAuth;
08b214c6 482 char m_szProxySecret[MAX_SECRET_LENGTH];
5039dede 483 int m_hCurrFile;
9f6d453a 484 TCHAR m_currentFileName[MAX_PATH];
967893bb 485 UINT32 m_dwDownloadRequestId;
bb85e341 486 CONDITION m_condFileDownload;
1693f955 487 bool m_fileDownloadSucceeded;
bb85e341
VK
488 void (*m_downloadProgressCallback)(size_t, void *);
489 void *m_downloadProgressCallbackArg;
901a5a9b 490 bool m_deleteFileOnDownloadFailure;
b368969c 491 void (*m_sendToClientMessageCallback)(NXCP_MESSAGE*, void *);
4685a2ad 492 bool m_fileUploadInProgress;
5039dede 493
a3050773
VK
494 void receiverThread();
495 static THREAD_RESULT THREAD_CALL receiverThreadStarter(void *);
5039dede 496
1693f955
VK
497 void processCollectedDataCallback(NXCPMessage *msg);
498 void onDataPushCallback(NXCPMessage *msg);
cce2f2ef
VK
499 void onSnmpTrapCallback(NXCPMessage *msg);
500 void onTrapCallback(NXCPMessage *msg);
685508a7 501 void onSyslogMessageCallback(NXCPMessage *msg);
1693f955 502
5039dede 503protected:
7c521895 504 void destroyResultData();
967893bb
VK
505 UINT32 waitForRCC(UINT32 dwRqId, UINT32 dwTimeOut);
506 UINT32 setupEncryption(RSA *pServerKey);
507 UINT32 authenticate(BOOL bProxyData);
508 UINT32 setupProxyConnection();
c11eee9b 509 const InetAddress& getIpAddr() { return m_addr; }
b368969c 510 UINT32 prepareFileDownload(const TCHAR *fileName, UINT32 rqId, bool append, void (*downloadProgressCallback)(size_t, void *), void (*fileResendCallback)(NXCP_MESSAGE*, void *), void *cbArg);
5039dede 511
af21affe 512 virtual void printMsg(const TCHAR *format, ...);
b368969c 513 virtual void onTrap(NXCPMessage *pMsg);
685508a7 514 virtual void onSyslogMessage(NXCPMessage *pMsg);
b368969c
VK
515 virtual void onDataPush(NXCPMessage *msg);
516 virtual void onFileMonitoringData(NXCPMessage *msg);
517 virtual void onSnmpTrap(NXCPMessage *pMsg);
1693f955 518 virtual void onFileDownload(bool success);
02d936bd 519 virtual UINT32 processCollectedData(NXCPMessage *msg);
a1273b42 520 virtual UINT32 processBulkCollectedData(NXCPMessage *request, NXCPMessage *response);
6fbaa926 521 virtual bool processCustomMessage(NXCPMessage *pMsg);
5039dede 522
a3050773
VK
523 void lock() { MutexLock(m_mutexDataLock); }
524 void unlock() { MutexUnlock(m_mutexDataLock); }
98abc9f1 525 NXCPEncryptionContext *acquireEncryptionContext();
5039dede 526
a87ef571
VK
527 void incInternalRefCount() { InterlockedIncrement(&m_internalRefCount); }
528 void decInternalRefCount() { if (InterlockedDecrement(&m_internalRefCount) == 0) delete this; }
529
7dd6369a
VK
530 virtual ~AgentConnection();
531
5039dede 532public:
c11eee9b 533 AgentConnection(InetAddress addr, WORD port = AGENT_LISTEN_PORT, int authMethod = AUTH_NONE, const TCHAR *secret = NULL);
5039dede 534
a87ef571
VK
535 void incRefCount() { InterlockedIncrement(&m_userRefCount); }
536 void decRefCount() { if (InterlockedDecrement(&m_userRefCount) == 0) { disconnect(); decInternalRefCount(); } }
537
7f6ecb6d 538 bool connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, UINT32 *pdwError = NULL, UINT32 *pdwSocketError = NULL, UINT64 serverId = 0);
7c521895 539 void disconnect();
9fd816cc 540 bool isConnected() { return m_isConnected; }
7c521895 541 int getProtocolVersion() { return m_nProtocolVersion; }
45d84f8a 542 SOCKET getSocket() { return m_hSocket; }
5a9e58a3 543
1693f955
VK
544 bool sendMessage(NXCPMessage *pMsg);
545 bool sendRawMessage(NXCP_MESSAGE *pMsg);
546 NXCPMessage *waitForMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut) { return m_pMsgWaitQueue->waitForMessage(wCode, dwId, dwTimeOut); }
547
4687826e 548 ARP_CACHE *getArpCache();
98762401 549 InterfaceList *getInterfaceList();
4687826e 550 ROUTING_TABLE *getRoutingTable();
967893bb
VK
551 UINT32 getParameter(const TCHAR *pszParam, UINT32 dwBufSize, TCHAR *pszBuffer);
552 UINT32 getList(const TCHAR *pszParam);
553 UINT32 getTable(const TCHAR *pszParam, Table **table);
554 UINT32 nop();
1a5ddd2a 555 UINT32 setServerCapabilities();
e9902466 556 UINT32 setServerId(UINT64 serverId);
1e84fc1d 557 UINT32 execAction(const TCHAR *action, int argc, const TCHAR * const *argv, bool withOutput = false, void (* outputCallback)(ActionCallbackEvent, const TCHAR *, void *) = NULL, void *cbData = NULL);
503da871 558 UINT32 uploadFile(const TCHAR *localFile, const TCHAR *destinationFile = NULL, void (* progressCallback)(INT64, void *) = NULL, void *cbArg = NULL, NXCPCompressionMethod compMethod = NXCP_COMPRESSION_NONE);
967893bb 559 UINT32 startUpgrade(const TCHAR *pszPkgName);
c75e9ee4 560 UINT32 checkNetworkService(UINT32 *pdwStatus, const InetAddress& addr, int iServiceType, WORD wPort = 0,
b8014eee 561 WORD wProto = 0, const TCHAR *pszRequest = NULL, const TCHAR *pszResponse = NULL, UINT32 *responseTime = NULL);
967893bb
VK
562 UINT32 getSupportedParameters(ObjectArray<AgentParameterDefinition> **paramList, ObjectArray<AgentTableDefinition> **tableList);
563 UINT32 getConfigFile(TCHAR **ppszConfig, UINT32 *pdwSize);
564 UINT32 updateConfigFile(const TCHAR *pszConfig);
565 UINT32 enableTraps();
e13420c1 566 UINT32 enableFileUpdates();
967893bb 567 UINT32 getPolicyInventory(AgentPolicyInfo **info);
7dd6369a 568 UINT32 uninstallPolicy(const uuid& guid);
9c786c0f 569 UINT32 takeScreenshot(const TCHAR *sessionName, BYTE **data, size_t *size);
967893bb 570
e9902466 571 UINT32 generateRequestId() { return (UINT32)InterlockedIncrement(&m_requestId); }
b368969c
VK
572 NXCPMessage *customRequest(NXCPMessage *pRequest, const TCHAR *recvFile = NULL, bool append = false, void (*downloadProgressCallback)(size_t, void *) = NULL,
573 void (*fileResendCallback)(NXCP_MESSAGE*, void *) = NULL, void *cbArg = NULL);
5039dede 574
967893bb
VK
575 UINT32 getNumDataLines() { return m_dwNumDataLines; }
576 const TCHAR *getDataLine(UINT32 dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
5039dede 577
967893bb
VK
578 void setConnectionTimeout(UINT32 dwTimeout) { m_connectionTimeout = max(dwTimeout, 1000); }
579 UINT32 getConnectionTimeout() { return m_connectionTimeout; }
580 void setCommandTimeout(UINT32 dwTimeout) { m_dwCommandTimeout = max(dwTimeout, 500); }
581 UINT32 getCommandTimeout() { return m_dwCommandTimeout; }
582 void setRecvTimeout(UINT32 dwTimeout) { m_dwRecvTimeout = max(dwTimeout, 10000); }
7c521895 583 void setEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
c11eee9b 584 void setProxy(InetAddress addr, WORD wPort = AGENT_LISTEN_PORT,
5039dede 585 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
7c521895 586 void setPort(WORD wPort) { m_wPort = wPort; }
af21affe 587 void setAuthData(int method, const TCHAR *secret);
76b4edb5 588 void setDeleteFileOnDownloadFailure(bool flag) { m_deleteFileOnDownloadFailure = flag; }
5039dede
AK
589};
590
a3050773
VK
591/**
592 * Proxy SNMP transport
593 */
5039dede
AK
594class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
595{
596protected:
c75e9ee4
VK
597 AgentConnection *m_agentConnection;
598 NXCPMessage *m_response;
599 InetAddress m_ipAddr;
600 WORD m_port;
489b117b 601 bool m_waitForResponse;
5039dede
AK
602
603public:
c75e9ee4 604 SNMP_ProxyTransport(AgentConnection *conn, const InetAddress& ipAddr, WORD port);
5039dede
AK
605 virtual ~SNMP_ProxyTransport();
606
967893bb 607 virtual int readMessage(SNMP_PDU **ppData, UINT32 dwTimeout = INFINITE,
cd9f247e
VK
608 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL,
609 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
c4366266 610 virtual int sendMessage(SNMP_PDU *pdu);
c75e9ee4 611 virtual InetAddress getPeerIpAddress();
d0a2ada6 612 WORD getPort() { return m_port; }
b368969c
VK
613
614 void setWaitForResponse(bool wait) { m_waitForResponse = wait; }
5039dede
AK
615};
616
596dc3aa
VK
617/**
618 * ISC flags
619 */
967893bb
VK
620#define ISCF_IS_CONNECTED ((UINT32)0x00000001)
621#define ISCF_REQUIRE_ENCRYPTION ((UINT32)0x00000002)
5039dede 622
ed950274
VK
623/**
624 * Inter-server connection (ISC)
625 */
5039dede
AK
626class LIBNXSRV_EXPORTABLE ISC
627{
628private:
967893bb 629 UINT32 m_flags;
9d061c7b 630 InetAddress m_addr;
5039dede
AK
631 WORD m_port;
632 SOCKET m_socket;
633 int m_protocolVersion;
bc7767c4 634 VolatileCounter m_requestId;
967893bb 635 UINT32 m_recvTimeout;
5039dede
AK
636 MsgWaitQueue *m_msgWaitQueue;
637 MUTEX m_mutexDataLock;
7b8b337e 638 MUTEX m_socketLock;
5039dede 639 THREAD m_hReceiverThread;
98abc9f1 640 NXCPEncryptionContext *m_ctx;
967893bb 641 UINT32 m_commandTimeout;
5039dede 642
bc7767c4
VK
643 void receiverThread();
644 static THREAD_RESULT THREAD_CALL receiverThreadStarter(void *);
5039dede
AK
645
646protected:
bc7767c4
VK
647 UINT32 setupEncryption(RSA *pServerKey);
648 UINT32 connectToService(UINT32 service);
5039dede 649
796baedf
VK
650 void lock() { MutexLock(m_mutexDataLock); }
651 void unlock() { MutexUnlock(m_mutexDataLock); }
5039dede 652
796baedf 653 virtual void printMessage(const TCHAR *format, ...);
b368969c
VK
654 virtual void onBinaryMessage(NXCP_MESSAGE *rawMsg);
655 virtual bool onMessage(NXCPMessage *msg);
5039dede
AK
656
657public:
658 ISC();
9d061c7b 659 ISC(const InetAddress& addr, WORD port = NETXMS_ISC_PORT);
5039dede
AK
660 virtual ~ISC();
661
bc7767c4
VK
662 UINT32 connect(UINT32 service, RSA *serverKey = NULL, BOOL requireEncryption = FALSE);
663 void disconnect();
e7aff566 664 bool connected() { return m_flags & ISCF_IS_CONNECTED; };
5039dede 665
b368969c
VK
666 BOOL sendMessage(NXCPMessage *msg);
667 NXCPMessage *waitForMessage(WORD code, UINT32 id, UINT32 timeOut) { return m_msgWaitQueue->waitForMessage(code, id, timeOut); }
bc7767c4
VK
668 UINT32 waitForRCC(UINT32 rqId, UINT32 timeOut);
669 UINT32 generateMessageId() { return (UINT32)InterlockedIncrement(&m_requestId); }
5039dede 670
bc7767c4 671 UINT32 nop();
5039dede
AK
672};
673
674
675//
676// Functions
677//
678
679void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
5039dede
AK
680void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
681void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
bf6fb6c3
VK
682const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(UINT32 err);
683UINT32 LIBNXSRV_EXPORTABLE AgentErrorToRCC(UINT32 err);
5039dede 684
640b6719 685void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...)
6e383343 686#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
640b6719
VK
687 __attribute__ ((format(printf, 2, 3)))
688#endif
689;
690
2df047f4
VK
691// for compatibility - new code should use nxlog_debug
692#define DbgPrintf nxlog_debug
c20b2798 693
5039dede
AK
694void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
695
967893bb 696const TCHAR LIBNXSRV_EXPORTABLE *ISCErrorCodeToText(UINT32 code);
5039dede 697
640b6719
VK
698/**
699 * Variables
700 */
c8076b19 701extern UINT64 LIBNXSRV_EXPORTABLE g_flags;
1693f955 702extern ThreadPool LIBNXSRV_EXPORTABLE *g_agentConnectionThreadPool;
5039dede 703
640b6719
VK
704/**
705 * Helper finctions for checking server flags
706 */
89135050
VK
707inline bool IsStandalone()
708{
c8076b19 709 return !(g_flags & AF_DAEMON) ? true : false;
89135050
VK
710}
711
712inline bool IsZoningEnabled()
713{
c8076b19 714 return (g_flags & AF_ENABLE_ZONING) ? true : false;
89135050
VK
715}
716
717inline bool IsShutdownInProgress()
718{
c8076b19 719 return (g_flags & AF_SHUTDOWN) ? true : false;
89135050
VK
720}
721
5039dede 722#endif /* _nxsrvapi_h_ */