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