added NXSL function AgentExecuteAction
[public/netxms.git] / src / server / include / nxsrvapi.h
1 /*
2 ** NetXMS - Network Management System
3 ** Server Library
4 ** Copyright (C) 2003-2015 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
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
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 **
16 ** You should have received a copy of the GNU Lesser General Public License
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 #include <nxcpapi.h>
38 #include <nms_util.h>
39 #include <nms_agent.h>
40 #include <nxsnmp.h>
41 #include <netxms_isc.h>
42 #include <nxcldefs.h>
43
44 #ifdef INCLUDE_LIBNXSRV_MESSAGES
45 #include "../libnxsrv/messages.h"
46 #endif
47
48 /**
49 * Default files
50 */
51 #ifdef _WIN32
52
53 #define DEFAULT_LOG_FILE _T("C:\\netxmsd.log")
54 #define DEFAULT_DUMP_DIR _T("C:\\")
55
56 #define LDIR_NDD _T("\\ndd")
57 #define LDIR_PDSDRV _T("\\pdsdrv")
58
59 #define DDIR_PACKAGES _T("\\packages")
60 #define DDIR_BACKGROUNDS _T("\\backgrounds")
61 #define DFILE_KEYS _T("\\server_key")
62 #define DFILE_COMPILED_MIB _T("\\netxms.mib")
63 #define DDIR_IMAGES _T("\\images")
64 #define DDIR_FILES _T("\\files")
65
66 #define DDIR_TEMPLATES _T("\\templates")
67
68 #else /* _WIN32 */
69
70 #ifndef DATADIR
71 #define DATADIR _T("/usr/share/netxms")
72 #endif
73
74 #ifndef STATEDIR
75 #define STATEDIR _T("/var/lib/netxms")
76 #endif
77
78 #ifndef LIBDIR
79 #define LIBDIR _T("/usr/lib")
80 #endif
81
82 #ifndef PKGLIBDIR
83 #define PKGLIBDIR _T("/usr/lib/netxms")
84 #endif
85
86 #define DEFAULT_LOG_FILE _T("/var/log/netxmsd.log")
87 #define DEFAULT_DUMP_DIR _T("/var/tmp")
88
89 #define LDIR_NDD _T("/ndd")
90 #define LDIR_PDSDRV _T("/pdsdrv")
91
92 #define DDIR_PACKAGES _T("/packages")
93 #define DDIR_BACKGROUNDS _T("/backgrounds")
94 #define DFILE_KEYS _T("/.server_key")
95 #define DFILE_COMPILED_MIB _T("/netxms.mib")
96 #define DDIR_IMAGES _T("/images")
97 #define DDIR_FILES _T("/files")
98
99 #define DDIR_TEMPLATES _T("/templates")
100
101 #endif /* _WIN32 */
102
103 /**
104 * Application flags
105 */
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)
125 #define AF_DB_LOCKED _ULL(0x0000000001000000)
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)
131 #define AF_RESOLVE_IP_FOR_EACH_STATUS_POLL _ULL(0x0000000080000000)
132 #define AF_PERFDATA_STORAGE_DRIVER_LOADED _ULL(0x0000000100000000)
133 #define AF_BACKGROUND_LOG_WRITER _ULL(0x0000000200000000)
134 #define AF_CASE_INSENSITIVE_LOGINS _ULL(0x0000000400000000)
135 #define AF_TRAP_SOURCES_IN_ALL_ZONES _ULL(0x0000000800000000)
136 #define AF_SERVER_INITIALIZED _ULL(0x4000000000000000)
137 #define AF_SHUTDOWN _ULL(0x8000000000000000)
138
139 /**
140 * Encryption usage policies
141 */
142 #define ENCRYPTION_DISABLED 0
143 #define ENCRYPTION_ALLOWED 1
144 #define ENCRYPTION_PREFERRED 2
145 #define ENCRYPTION_REQUIRED 3
146
147 /**
148 * Agent action output callback events
149 */
150 enum ActionCallbackEvent
151 {
152 ACE_CONNECTED = 0,
153 ACE_DATA = 1,
154 ACE_DISCONNECTED = 2
155 };
156
157 /**
158 * Win32 service and syslog constants
159 */
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
172 /**
173 * Single ARP cache entry
174 */
175 typedef struct
176 {
177 UINT32 dwIndex; // Interface index
178 InetAddress ipAddr;
179 BYTE bMacAddr[MAC_ADDR_LENGTH];
180 } ARP_ENTRY;
181
182 /**
183 * ARP cache structure used by discovery functions and AgentConnection class
184 */
185 typedef struct
186 {
187 UINT32 dwNumEntries;
188 ARP_ENTRY *pEntries;
189 } ARP_CACHE;
190
191 /**
192 * Interface information structure used by discovery functions and AgentConnection class
193 */
194 class InterfaceInfo
195 {
196 private:
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
213 public:
214 UINT32 index;
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
218 UINT32 type;
219 UINT32 mtu;
220 UINT64 speed; // interface speed in bits/sec
221 UINT32 bridgePort;
222 UINT32 slot;
223 UINT32 port;
224 InetAddressList ipAddrList;
225 BYTE macAddr[MAC_ADDR_LENGTH];
226 bool isPhysicalPort;
227 bool isSystem;
228 UINT32 ifTableSuffix[16]; // actual ifTable suffix
229 int ifTableSuffixLength;
230
231 InterfaceInfo(UINT32 ifIndex)
232 {
233 index = ifIndex;
234 ifTableSuffixLength = 0;
235 init();
236 }
237
238 InterfaceInfo(UINT32 ifIndex, int suffixLen, const UINT32 *suffix)
239 {
240 index = ifIndex;
241 ifTableSuffixLength = ((suffixLen >= 0) && (suffixLen < 16)) ? suffixLen : 0;
242 memcpy(ifTableSuffix, suffix, ifTableSuffixLength * sizeof(UINT32));
243 init();
244 }
245
246 bool hasAddress(const InetAddress& addr) { return ipAddrList.hasAddress(addr); }
247 };
248
249 /**
250 * Interface list used by discovery functions and AgentConnection class
251 */
252 class LIBNXSRV_EXPORTABLE InterfaceList
253 {
254 private:
255 ObjectArray<InterfaceInfo> *m_interfaces;
256 void *m_data; // Can be used by custom enumeration handlers
257 bool m_needPrefixWalk;
258
259 public:
260 InterfaceList(int initialAlloc = 8);
261 ~InterfaceList();
262
263 void add(InterfaceInfo *iface) { m_interfaces->add(iface); }
264 void remove(int index) { m_interfaces->remove(index); }
265
266 int size() { return m_interfaces->size(); }
267 InterfaceInfo *get(int index) { return m_interfaces->get(index); }
268 InterfaceInfo *findByIfIndex(UINT32 ifIndex);
269
270 void setData(void *data) { m_data = data; }
271 void *getData() { return m_data; }
272
273 bool isPrefixWalkNeeded() { return m_needPrefixWalk; }
274 void setPrefixWalkNeeded() { m_needPrefixWalk = true; }
275 };
276
277 /**
278 * Vlan information
279 */
280 #define VLAN_PRM_IFINDEX 0
281 #define VLAN_PRM_SLOTPORT 1
282 #define VLAN_PRM_BPORT 2
283
284 class LIBNXSRV_EXPORTABLE VlanInfo
285 {
286 private:
287 int m_vlanId;
288 TCHAR *m_name;
289 int m_portRefMode; // Port reference mode - by ifIndex or by slot/port
290 int m_allocated;
291 int m_numPorts; // Number of ports in VLAN
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
295
296 public:
297 VlanInfo(int vlanId, int prm);
298 ~VlanInfo();
299
300 int getVlanId() { return m_vlanId; }
301 int getPortReferenceMode() { return m_portRefMode; }
302 const TCHAR *getName() { return CHECK_NULL_EX(m_name); }
303 int getNumPorts() { return m_numPorts; }
304 UINT32 *getPorts() { return m_ports; }
305 UINT32 *getIfIndexes() { return m_indexes; }
306 UINT32 *getIfIds() { return m_ids; }
307
308 void add(UINT32 slot, UINT32 port);
309 void add(UINT32 ifIndex);
310 void setName(const TCHAR *name);
311
312 void prepareForResolve();
313 void resolvePort(int index, UINT32 sp, UINT32 ifIndex, UINT32 id);
314 };
315
316 /**
317 * Vlan list
318 */
319 class LIBNXSRV_EXPORTABLE VlanList : public RefCountObject
320 {
321 private:
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
327 public:
328 VlanList(int initialAlloc = 8);
329 virtual ~VlanList();
330
331 void add(VlanInfo *vlan);
332 void addMemberPort(int vlanId, UINT32 portId);
333
334 int size() { return m_size; }
335 VlanInfo *get(int index) { return ((index >= 0) && (index < m_size)) ? m_vlans[index] : NULL; }
336 VlanInfo *findById(int id);
337 VlanInfo *findByName(const TCHAR *name);
338
339 void setData(void *data) { m_data = data; }
340 void *getData() { return m_data; }
341
342 void fillMessage(NXCPMessage *msg);
343 };
344
345 /**
346 * Route information
347 */
348 typedef struct
349 {
350 UINT32 dwDestAddr;
351 UINT32 dwDestMask;
352 UINT32 dwNextHop;
353 UINT32 dwIfIndex;
354 UINT32 dwRouteType;
355 } ROUTE;
356
357 /**
358 * Routing table
359 */
360 typedef struct
361 {
362 int iNumEntries; // Number of entries
363 ROUTE *pRoutes; // Route list
364 } ROUTING_TABLE;
365
366 /**
367 * Information about policies installed on agent
368 */
369 class LIBNXSRV_EXPORTABLE AgentPolicyInfo
370 {
371 private:
372 int m_size;
373 BYTE *m_guidList;
374 int *m_typeList;
375 TCHAR **m_serverInfoList;
376 UINT64 *m_serverIdList;
377 int *m_version;
378
379 public:
380 AgentPolicyInfo(NXCPMessage *msg);
381 ~AgentPolicyInfo();
382
383 int size() { return m_size; }
384 uuid getGuid(int index);
385 int getType(int index) { return ((index >= 0) && (index < m_size)) ? m_typeList[index] : -1; }
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; }
389 };
390
391 /**
392 * Agent parameter definition
393 */
394 class LIBNXSRV_EXPORTABLE AgentParameterDefinition
395 {
396 private:
397 TCHAR *m_name;
398 TCHAR *m_description;
399 int m_dataType;
400
401 public:
402 AgentParameterDefinition(NXCPMessage *msg, UINT32 baseId);
403 AgentParameterDefinition(AgentParameterDefinition *src);
404 ~AgentParameterDefinition();
405
406 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
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 */
416 struct 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 */
431 class LIBNXSRV_EXPORTABLE AgentTableDefinition
432 {
433 private:
434 TCHAR *m_name;
435 TCHAR *m_description;
436 StringList *m_instanceColumns;
437 ObjectArray<AgentTableColumnDefinition> *m_columns;
438
439 public:
440 AgentTableDefinition(NXCPMessage *msg, UINT32 baseId);
441 AgentTableDefinition(AgentTableDefinition *src);
442 ~AgentTableDefinition();
443
444 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
445
446 const TCHAR *getName() { return m_name; }
447 const TCHAR *getDescription() { return m_description; }
448 };
449
450 /**
451 * Agent connection
452 */
453 class LIBNXSRV_EXPORTABLE AgentConnection
454 {
455 private:
456 VolatileCounter m_userRefCount;
457 VolatileCounter m_internalRefCount;
458 InetAddress m_addr;
459 int m_nProtocolVersion;
460 int m_iAuthMethod;
461 char m_szSecret[MAX_SECRET_LENGTH];
462 time_t m_tLastCommandTime;
463 SOCKET m_hSocket;
464 UINT32 m_dwNumDataLines;
465 VolatileCounter m_requestId;
466 UINT32 m_dwCommandTimeout;
467 UINT32 m_connectionTimeout;
468 UINT32 m_dwRecvTimeout;
469 TCHAR **m_ppDataLines;
470 MsgWaitQueue *m_pMsgWaitQueue;
471 bool m_isConnected;
472 MUTEX m_mutexDataLock;
473 MUTEX m_mutexSocketWrite;
474 THREAD m_hReceiverThread;
475 NXCPEncryptionContext *m_pCtx;
476 int m_iEncryptionPolicy;
477 BOOL m_bUseProxy;
478 InetAddress m_proxyAddr;
479 WORD m_wPort;
480 WORD m_wProxyPort;
481 int m_iProxyAuth;
482 char m_szProxySecret[MAX_SECRET_LENGTH];
483 int m_hCurrFile;
484 TCHAR m_currentFileName[MAX_PATH];
485 UINT32 m_dwDownloadRequestId;
486 CONDITION m_condFileDownload;
487 bool m_fileDownloadSucceeded;
488 void (*m_downloadProgressCallback)(size_t, void *);
489 void *m_downloadProgressCallbackArg;
490 bool m_deleteFileOnDownloadFailure;
491 void (*m_sendToClientMessageCallback)(NXCP_MESSAGE*, void *);
492 bool m_fileUploadInProgress;
493
494 void receiverThread();
495 static THREAD_RESULT THREAD_CALL receiverThreadStarter(void *);
496
497 void processCollectedDataCallback(NXCPMessage *msg);
498 void onDataPushCallback(NXCPMessage *msg);
499 void onSnmpTrapCallback(NXCPMessage *msg);
500 void onTrapCallback(NXCPMessage *msg);
501 void onSyslogMessageCallback(NXCPMessage *msg);
502
503 protected:
504 void destroyResultData();
505 UINT32 waitForRCC(UINT32 dwRqId, UINT32 dwTimeOut);
506 UINT32 setupEncryption(RSA *pServerKey);
507 UINT32 authenticate(BOOL bProxyData);
508 UINT32 setupProxyConnection();
509 const InetAddress& getIpAddr() { return m_addr; }
510 UINT32 prepareFileDownload(const TCHAR *fileName, UINT32 rqId, bool append, void (*downloadProgressCallback)(size_t, void *), void (*fileResendCallback)(NXCP_MESSAGE*, void *), void *cbArg);
511
512 virtual void printMsg(const TCHAR *format, ...);
513 virtual void onTrap(NXCPMessage *pMsg);
514 virtual void onSyslogMessage(NXCPMessage *pMsg);
515 virtual void onDataPush(NXCPMessage *msg);
516 virtual void onFileMonitoringData(NXCPMessage *msg);
517 virtual void onSnmpTrap(NXCPMessage *pMsg);
518 virtual void onFileDownload(bool success);
519 virtual UINT32 processCollectedData(NXCPMessage *msg);
520 virtual UINT32 processBulkCollectedData(NXCPMessage *request, NXCPMessage *response);
521 virtual bool processCustomMessage(NXCPMessage *pMsg);
522
523 void lock() { MutexLock(m_mutexDataLock); }
524 void unlock() { MutexUnlock(m_mutexDataLock); }
525 NXCPEncryptionContext *acquireEncryptionContext();
526
527 void incInternalRefCount() { InterlockedIncrement(&m_internalRefCount); }
528 void decInternalRefCount() { if (InterlockedDecrement(&m_internalRefCount) == 0) delete this; }
529
530 virtual ~AgentConnection();
531
532 public:
533 AgentConnection(InetAddress addr, WORD port = AGENT_LISTEN_PORT, int authMethod = AUTH_NONE, const TCHAR *secret = NULL);
534
535 void incRefCount() { InterlockedIncrement(&m_userRefCount); }
536 void decRefCount() { if (InterlockedDecrement(&m_userRefCount) == 0) { disconnect(); decInternalRefCount(); } }
537
538 bool connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, UINT32 *pdwError = NULL, UINT32 *pdwSocketError = NULL, UINT64 serverId = 0);
539 void disconnect();
540 bool isConnected() { return m_isConnected; }
541 int getProtocolVersion() { return m_nProtocolVersion; }
542 SOCKET getSocket() { return m_hSocket; }
543
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
548 ARP_CACHE *getArpCache();
549 InterfaceList *getInterfaceList();
550 ROUTING_TABLE *getRoutingTable();
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();
555 UINT32 setServerCapabilities();
556 UINT32 setServerId(UINT64 serverId);
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);
558 UINT32 uploadFile(const TCHAR *localFile, const TCHAR *destinationFile = NULL, void (* progressCallback)(INT64, void *) = NULL, void *cbArg = NULL, NXCPCompressionMethod compMethod = NXCP_COMPRESSION_NONE);
559 UINT32 startUpgrade(const TCHAR *pszPkgName);
560 UINT32 checkNetworkService(UINT32 *pdwStatus, const InetAddress& addr, int iServiceType, WORD wPort = 0,
561 WORD wProto = 0, const TCHAR *pszRequest = NULL, const TCHAR *pszResponse = NULL, UINT32 *responseTime = NULL);
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();
566 UINT32 enableFileUpdates();
567 UINT32 getPolicyInventory(AgentPolicyInfo **info);
568 UINT32 uninstallPolicy(const uuid& guid);
569 UINT32 takeScreenshot(const TCHAR *sessionName, BYTE **data, size_t *size);
570
571 UINT32 generateRequestId() { return (UINT32)InterlockedIncrement(&m_requestId); }
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);
574
575 UINT32 getNumDataLines() { return m_dwNumDataLines; }
576 const TCHAR *getDataLine(UINT32 dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
577
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); }
583 void setEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
584 void setProxy(InetAddress addr, WORD wPort = AGENT_LISTEN_PORT,
585 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
586 void setPort(WORD wPort) { m_wPort = wPort; }
587 void setAuthData(int method, const TCHAR *secret);
588 void setDeleteFileOnDownloadFailure(bool flag) { m_deleteFileOnDownloadFailure = flag; }
589 };
590
591 /**
592 * Proxy SNMP transport
593 */
594 class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
595 {
596 protected:
597 AgentConnection *m_agentConnection;
598 NXCPMessage *m_response;
599 InetAddress m_ipAddr;
600 WORD m_port;
601 bool m_waitForResponse;
602
603 public:
604 SNMP_ProxyTransport(AgentConnection *conn, const InetAddress& ipAddr, WORD port);
605 virtual ~SNMP_ProxyTransport();
606
607 virtual int readMessage(SNMP_PDU **ppData, UINT32 dwTimeout = INFINITE,
608 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL,
609 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
610 virtual int sendMessage(SNMP_PDU *pdu);
611 virtual InetAddress getPeerIpAddress();
612 WORD getPort() { return m_port; }
613
614 void setWaitForResponse(bool wait) { m_waitForResponse = wait; }
615 };
616
617 /**
618 * ISC flags
619 */
620 #define ISCF_IS_CONNECTED ((UINT32)0x00000001)
621 #define ISCF_REQUIRE_ENCRYPTION ((UINT32)0x00000002)
622
623 /**
624 * Inter-server connection (ISC)
625 */
626 class LIBNXSRV_EXPORTABLE ISC
627 {
628 private:
629 UINT32 m_flags;
630 InetAddress m_addr;
631 WORD m_port;
632 SOCKET m_socket;
633 int m_protocolVersion;
634 VolatileCounter m_requestId;
635 UINT32 m_recvTimeout;
636 MsgWaitQueue *m_msgWaitQueue;
637 MUTEX m_mutexDataLock;
638 MUTEX m_socketLock;
639 THREAD m_hReceiverThread;
640 NXCPEncryptionContext *m_ctx;
641 UINT32 m_commandTimeout;
642
643 void receiverThread();
644 static THREAD_RESULT THREAD_CALL receiverThreadStarter(void *);
645
646 protected:
647 UINT32 setupEncryption(RSA *pServerKey);
648 UINT32 connectToService(UINT32 service);
649
650 void lock() { MutexLock(m_mutexDataLock); }
651 void unlock() { MutexUnlock(m_mutexDataLock); }
652
653 virtual void printMessage(const TCHAR *format, ...);
654 virtual void onBinaryMessage(NXCP_MESSAGE *rawMsg);
655 virtual bool onMessage(NXCPMessage *msg);
656
657 public:
658 ISC();
659 ISC(const InetAddress& addr, WORD port = NETXMS_ISC_PORT);
660 virtual ~ISC();
661
662 UINT32 connect(UINT32 service, RSA *serverKey = NULL, BOOL requireEncryption = FALSE);
663 void disconnect();
664 bool connected() { return m_flags & ISCF_IS_CONNECTED; };
665
666 BOOL sendMessage(NXCPMessage *msg);
667 NXCPMessage *waitForMessage(WORD code, UINT32 id, UINT32 timeOut) { return m_msgWaitQueue->waitForMessage(code, id, timeOut); }
668 UINT32 waitForRCC(UINT32 rqId, UINT32 timeOut);
669 UINT32 generateMessageId() { return (UINT32)InterlockedIncrement(&m_requestId); }
670
671 UINT32 nop();
672 };
673
674
675 //
676 // Functions
677 //
678
679 void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
680 void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
681 void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
682 const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(UINT32 err);
683 UINT32 LIBNXSRV_EXPORTABLE AgentErrorToRCC(UINT32 err);
684
685 void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...)
686 #if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
687 __attribute__ ((format(printf, 2, 3)))
688 #endif
689 ;
690
691 // for compatibility - new code should use nxlog_debug
692 #define DbgPrintf nxlog_debug
693
694 void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
695
696 const TCHAR LIBNXSRV_EXPORTABLE *ISCErrorCodeToText(UINT32 code);
697
698 /**
699 * Variables
700 */
701 extern UINT64 LIBNXSRV_EXPORTABLE g_flags;
702 extern ThreadPool LIBNXSRV_EXPORTABLE *g_agentConnectionThreadPool;
703
704 /**
705 * Helper finctions for checking server flags
706 */
707 inline bool IsStandalone()
708 {
709 return !(g_flags & AF_DAEMON) ? true : false;
710 }
711
712 inline bool IsZoningEnabled()
713 {
714 return (g_flags & AF_ENABLE_ZONING) ? true : false;
715 }
716
717 inline bool IsShutdownInProgress()
718 {
719 return (g_flags & AF_SHUTDOWN) ? true : false;
720 }
721
722 #endif /* _nxsrvapi_h_ */