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