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