1f71f58d170223f9ca6d3179543b408b645130f1
[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 SDIR_SCRIPTS _T("\\scripts")
67 #define SDIR_TEMPLATES _T("\\templates")
68 #define SFILE_RADDICT _T("\\radius.dict")
69
70 #else /* _WIN32 */
71
72 #ifndef DATADIR
73 #define DATADIR _T("/usr/share/netxms")
74 #endif
75
76 #ifndef STATEDIR
77 #define STATEDIR _T("/var/lib/netxms")
78 #endif
79
80 #ifndef LIBDIR
81 #define LIBDIR _T("/usr/lib")
82 #endif
83
84 #ifndef PKGLIBDIR
85 #define PKGLIBDIR _T("/usr/lib/netxms")
86 #endif
87
88 #define DEFAULT_LOG_FILE _T("/var/log/netxmsd.log")
89 #define DEFAULT_DUMP_DIR _T("/var/tmp")
90
91 #define LDIR_NDD _T("/ndd")
92 #define LDIR_PDSDRV _T("/pdsdrv")
93
94 #define DDIR_PACKAGES _T("/packages")
95 #define DDIR_BACKGROUNDS _T("/backgrounds")
96 #define DFILE_KEYS _T("/.server_key")
97 #define DFILE_COMPILED_MIB _T("/netxms.mib")
98 #define DDIR_IMAGES _T("/images")
99 #define DDIR_FILES _T("/files")
100
101 #define SDIR_SCRIPTS _T("/scripts")
102 #define SDIR_TEMPLATES _T("/templates")
103 #define SFILE_RADDICT _T("/radius.dict")
104
105 #endif /* _WIN32 */
106
107 /**
108 * Application flags
109 */
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)
129 #define AF_DB_LOCKED _ULL(0x0000000001000000)
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)
135 #define AF_RESOLVE_IP_FOR_EACH_STATUS_POLL _ULL(0x0000000080000000)
136 #define AF_PERFDATA_STORAGE_DRIVER_LOADED _ULL(0x0000000100000000)
137 #define AF_BACKGROUND_LOG_WRITER _ULL(0x0000000200000000)
138 #define AF_CASE_INSENSITIVE_LOGINS _ULL(0x0000000400000000)
139 #define AF_TRAP_SOURCES_IN_ALL_ZONES _ULL(0x0000000800000000)
140 #define AF_SYSLOG_DISCOVERY _ULL(0x0000001000000000)
141 #define AF_ENABLE_LOCAL_CONSOLE _ULL(0x0000002000000000)
142 #define AF_SERVER_INITIALIZED _ULL(0x4000000000000000)
143 #define AF_SHUTDOWN _ULL(0x8000000000000000)
144
145 /**
146 * Encryption usage policies
147 */
148 #define ENCRYPTION_DISABLED 0
149 #define ENCRYPTION_ALLOWED 1
150 #define ENCRYPTION_PREFERRED 2
151 #define ENCRYPTION_REQUIRED 3
152
153 /**
154 * Agent action output callback events
155 */
156 enum ActionCallbackEvent
157 {
158 ACE_CONNECTED = 0,
159 ACE_DATA = 1,
160 ACE_DISCONNECTED = 2
161 };
162
163 /**
164 * Win32 service and syslog constants
165 */
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
178 /**
179 * Single ARP cache entry
180 */
181 typedef struct
182 {
183 UINT32 dwIndex; // Interface index
184 InetAddress ipAddr;
185 BYTE bMacAddr[MAC_ADDR_LENGTH];
186 } ARP_ENTRY;
187
188 /**
189 * ARP cache structure used by discovery functions and AgentConnection class
190 */
191 typedef struct
192 {
193 UINT32 dwNumEntries;
194 ARP_ENTRY *pEntries;
195 } ARP_CACHE;
196
197 /**
198 * Interface information structure used by discovery functions and AgentConnection class
199 */
200 class InterfaceInfo
201 {
202 private:
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;
217 parentIndex = 0;
218 }
219
220 public:
221 UINT32 index;
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
225 UINT32 type;
226 UINT32 mtu;
227 UINT64 speed; // interface speed in bits/sec
228 UINT32 bridgePort;
229 UINT32 slot;
230 UINT32 port;
231 InetAddressList ipAddrList;
232 BYTE macAddr[MAC_ADDR_LENGTH];
233 bool isPhysicalPort;
234 bool isSystem;
235 UINT32 ifTableSuffix[16]; // actual ifTable suffix
236 int ifTableSuffixLength;
237 UINT32 parentIndex;
238
239 InterfaceInfo(UINT32 ifIndex)
240 {
241 index = ifIndex;
242 ifTableSuffixLength = 0;
243 init();
244 }
245
246 InterfaceInfo(UINT32 ifIndex, int suffixLen, const UINT32 *suffix)
247 {
248 index = ifIndex;
249 ifTableSuffixLength = ((suffixLen >= 0) && (suffixLen < 16)) ? suffixLen : 0;
250 memcpy(ifTableSuffix, suffix, ifTableSuffixLength * sizeof(UINT32));
251 init();
252 }
253
254 bool hasAddress(const InetAddress& addr) { return ipAddrList.hasAddress(addr); }
255 };
256
257 /**
258 * Interface list used by discovery functions and AgentConnection class
259 */
260 class LIBNXSRV_EXPORTABLE InterfaceList
261 {
262 private:
263 ObjectArray<InterfaceInfo> *m_interfaces;
264 void *m_data; // Can be used by custom enumeration handlers
265 bool m_needPrefixWalk;
266
267 public:
268 InterfaceList(int initialAlloc = 8);
269 ~InterfaceList();
270
271 void add(InterfaceInfo *iface) { m_interfaces->add(iface); }
272 void remove(int index) { m_interfaces->remove(index); }
273
274 int size() { return m_interfaces->size(); }
275 InterfaceInfo *get(int index) { return m_interfaces->get(index); }
276 InterfaceInfo *findByIfIndex(UINT32 ifIndex);
277 InterfaceInfo *findByPhyPosition(int slot, int port);
278
279 void setData(void *data) { m_data = data; }
280 void *getData() { return m_data; }
281
282 bool isPrefixWalkNeeded() { return m_needPrefixWalk; }
283 void setPrefixWalkNeeded() { m_needPrefixWalk = true; }
284 };
285
286 /**
287 * Vlan information
288 */
289 #define VLAN_PRM_IFINDEX 0
290 #define VLAN_PRM_SLOTPORT 1
291 #define VLAN_PRM_BPORT 2
292
293 class LIBNXSRV_EXPORTABLE VlanInfo
294 {
295 private:
296 int m_vlanId;
297 TCHAR *m_name;
298 int m_portRefMode; // Port reference mode - by ifIndex or by slot/port
299 int m_allocated;
300 int m_numPorts; // Number of ports in VLAN
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
304
305 public:
306 VlanInfo(int vlanId, int prm);
307 ~VlanInfo();
308
309 int getVlanId() { return m_vlanId; }
310 int getPortReferenceMode() { return m_portRefMode; }
311 const TCHAR *getName() { return CHECK_NULL_EX(m_name); }
312 int getNumPorts() { return m_numPorts; }
313 UINT32 *getPorts() { return m_ports; }
314 UINT32 *getIfIndexes() { return m_indexes; }
315 UINT32 *getIfIds() { return m_ids; }
316
317 void add(UINT32 slot, UINT32 port);
318 void add(UINT32 ifIndex);
319 void setName(const TCHAR *name);
320
321 void prepareForResolve();
322 void resolvePort(int index, UINT32 sp, UINT32 ifIndex, UINT32 id);
323 };
324
325 /**
326 * Vlan list
327 */
328 class LIBNXSRV_EXPORTABLE VlanList : public RefCountObject
329 {
330 private:
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
336 public:
337 VlanList(int initialAlloc = 8);
338 virtual ~VlanList();
339
340 void add(VlanInfo *vlan);
341 void addMemberPort(int vlanId, UINT32 portId);
342
343 int size() { return m_size; }
344 VlanInfo *get(int index) { return ((index >= 0) && (index < m_size)) ? m_vlans[index] : NULL; }
345 VlanInfo *findById(int id);
346 VlanInfo *findByName(const TCHAR *name);
347
348 void setData(void *data) { m_data = data; }
349 void *getData() { return m_data; }
350
351 void fillMessage(NXCPMessage *msg);
352 };
353
354 /**
355 * Route information
356 */
357 typedef struct
358 {
359 UINT32 dwDestAddr;
360 UINT32 dwDestMask;
361 UINT32 dwNextHop;
362 UINT32 dwIfIndex;
363 UINT32 dwRouteType;
364 } ROUTE;
365
366 /**
367 * Routing table
368 */
369 typedef struct
370 {
371 int iNumEntries; // Number of entries
372 ROUTE *pRoutes; // Route list
373 } ROUTING_TABLE;
374
375 /**
376 * Information about policies installed on agent
377 */
378 class LIBNXSRV_EXPORTABLE AgentPolicyInfo
379 {
380 private:
381 int m_size;
382 BYTE *m_guidList;
383 int *m_typeList;
384 TCHAR **m_serverInfoList;
385 UINT64 *m_serverIdList;
386 int *m_version;
387
388 public:
389 AgentPolicyInfo(NXCPMessage *msg);
390 ~AgentPolicyInfo();
391
392 int size() { return m_size; }
393 uuid getGuid(int index);
394 int getType(int index) { return ((index >= 0) && (index < m_size)) ? m_typeList[index] : -1; }
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; }
398 };
399
400 /**
401 * Agent parameter definition
402 */
403 class LIBNXSRV_EXPORTABLE AgentParameterDefinition
404 {
405 private:
406 TCHAR *m_name;
407 TCHAR *m_description;
408 int m_dataType;
409
410 public:
411 AgentParameterDefinition(NXCPMessage *msg, UINT32 baseId);
412 AgentParameterDefinition(AgentParameterDefinition *src);
413 ~AgentParameterDefinition();
414
415 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
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 */
425 struct 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 */
440 class LIBNXSRV_EXPORTABLE AgentTableDefinition
441 {
442 private:
443 TCHAR *m_name;
444 TCHAR *m_description;
445 StringList *m_instanceColumns;
446 ObjectArray<AgentTableColumnDefinition> *m_columns;
447
448 public:
449 AgentTableDefinition(NXCPMessage *msg, UINT32 baseId);
450 AgentTableDefinition(AgentTableDefinition *src);
451 ~AgentTableDefinition();
452
453 UINT32 fillMessage(NXCPMessage *msg, UINT32 baseId);
454
455 const TCHAR *getName() { return m_name; }
456 const TCHAR *getDescription() { return m_description; }
457 };
458
459 /**
460 * Agent connection
461 */
462 class LIBNXSRV_EXPORTABLE AgentConnection
463 {
464 private:
465 VolatileCounter m_userRefCount;
466 VolatileCounter m_internalRefCount;
467 UINT32 m_debugId;
468 InetAddress m_addr;
469 int m_nProtocolVersion;
470 int m_iAuthMethod;
471 char m_szSecret[MAX_SECRET_LENGTH];
472 time_t m_tLastCommandTime;
473 AbstractCommChannel *m_channel;
474 UINT32 m_dwNumDataLines;
475 VolatileCounter m_requestId;
476 UINT32 m_dwCommandTimeout;
477 UINT32 m_connectionTimeout;
478 UINT32 m_dwRecvTimeout;
479 TCHAR **m_ppDataLines;
480 MsgWaitQueue *m_pMsgWaitQueue;
481 bool m_isConnected;
482 MUTEX m_mutexDataLock;
483 MUTEX m_mutexSocketWrite;
484 THREAD m_hReceiverThread;
485 NXCPEncryptionContext *m_pCtx;
486 int m_iEncryptionPolicy;
487 bool m_useProxy;
488 InetAddress m_proxyAddr;
489 WORD m_wPort;
490 WORD m_wProxyPort;
491 int m_iProxyAuth;
492 char m_szProxySecret[MAX_SECRET_LENGTH];
493 int m_hCurrFile;
494 TCHAR m_currentFileName[MAX_PATH];
495 UINT32 m_dwDownloadRequestId;
496 CONDITION m_condFileDownload;
497 bool m_fileDownloadSucceeded;
498 void (*m_downloadProgressCallback)(size_t, void *);
499 void *m_downloadProgressCallbackArg;
500 bool m_deleteFileOnDownloadFailure;
501 void (*m_sendToClientMessageCallback)(NXCP_MESSAGE *, void *);
502 bool m_fileUploadInProgress;
503 bool m_allowCompression;
504
505 void receiverThread();
506 static THREAD_RESULT THREAD_CALL receiverThreadStarter(void *);
507
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();
514
515 void processCollectedDataCallback(NXCPMessage *msg);
516 void onDataPushCallback(NXCPMessage *msg);
517 void onSnmpTrapCallback(NXCPMessage *msg);
518 void onTrapCallback(NXCPMessage *msg);
519 void onSyslogMessageCallback(NXCPMessage *msg);
520
521 protected:
522 virtual ~AgentConnection();
523
524 virtual AbstractCommChannel *createChannel();
525 virtual void onTrap(NXCPMessage *pMsg);
526 virtual void onSyslogMessage(NXCPMessage *pMsg);
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);
532 virtual UINT32 processBulkCollectedData(NXCPMessage *request, NXCPMessage *response);
533 virtual bool processCustomMessage(NXCPMessage *pMsg);
534
535 const InetAddress& getIpAddr() const { return m_addr; }
536
537 void debugPrintf(int level, const TCHAR *format, ...);
538
539 void lock() { MutexLock(m_mutexDataLock); }
540 void unlock() { MutexUnlock(m_mutexDataLock); }
541 NXCPEncryptionContext *acquireEncryptionContext();
542 AbstractCommChannel *acquireChannel();
543
544 UINT32 waitForRCC(UINT32 dwRqId, UINT32 dwTimeOut);
545
546 void incInternalRefCount() { InterlockedIncrement(&m_internalRefCount); }
547 void decInternalRefCount() { if (InterlockedDecrement(&m_internalRefCount) == 0) delete this; }
548
549 public:
550 AgentConnection(const InetAddress& addr, WORD port = AGENT_LISTEN_PORT, int authMethod = AUTH_NONE, const TCHAR *secret = NULL, bool allowCompression = true);
551
552 void incRefCount() { InterlockedIncrement(&m_userRefCount); }
553 void decRefCount() { if (InterlockedDecrement(&m_userRefCount) == 0) { disconnect(); decInternalRefCount(); } }
554
555 bool connect(RSA *pServerKey = NULL, UINT32 *pdwError = NULL, UINT32 *pdwSocketError = NULL, UINT64 serverId = 0);
556 void disconnect();
557 bool isConnected() const { return m_isConnected; }
558 bool isProxyMode() { return m_useProxy; }
559 int getProtocolVersion() const { return m_nProtocolVersion; }
560 bool isCompressionAllowed() const { return m_allowCompression && (m_nProtocolVersion >= 4); }
561
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
566 ARP_CACHE *getArpCache();
567 InterfaceList *getInterfaceList();
568 ROUTING_TABLE *getRoutingTable();
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();
573 UINT32 setServerCapabilities();
574 UINT32 setServerId(UINT64 serverId);
575 UINT32 execAction(const TCHAR *action, const StringList &list, bool withOutput = false,
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);
580 UINT32 startUpgrade(const TCHAR *pszPkgName);
581 UINT32 checkNetworkService(UINT32 *pdwStatus, const InetAddress& addr, int iServiceType, WORD wPort = 0,
582 WORD wProto = 0, const TCHAR *pszRequest = NULL, const TCHAR *pszResponse = NULL, UINT32 *responseTime = NULL);
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();
587 UINT32 enableFileUpdates();
588 UINT32 getPolicyInventory(AgentPolicyInfo **info);
589 UINT32 uninstallPolicy(const uuid& guid);
590 UINT32 takeScreenshot(const TCHAR *sessionName, BYTE **data, size_t *size);
591
592 UINT32 generateRequestId() { return (UINT32)InterlockedIncrement(&m_requestId); }
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);
596
597 UINT32 getNumDataLines() { return m_dwNumDataLines; }
598 const TCHAR *getDataLine(UINT32 dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
599
600 void setConnectionTimeout(UINT32 dwTimeout) { m_connectionTimeout = MAX(dwTimeout, 1000); }
601 UINT32 getConnectionTimeout() { return m_connectionTimeout; }
602 void setCommandTimeout(UINT32 dwTimeout) { m_dwCommandTimeout = MAX(dwTimeout, 500); }
603 UINT32 getCommandTimeout() { return m_dwCommandTimeout; }
604 void setRecvTimeout(UINT32 dwTimeout) { m_dwRecvTimeout = MAX(dwTimeout, 10000); }
605 void setEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
606 void setProxy(InetAddress addr, WORD wPort = AGENT_LISTEN_PORT,
607 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
608 void setPort(WORD wPort) { m_wPort = wPort; }
609 void setAuthData(int method, const TCHAR *secret);
610 void setDeleteFileOnDownloadFailure(bool flag) { m_deleteFileOnDownloadFailure = flag; }
611 };
612
613 /**
614 * Proxy SNMP transport
615 */
616 class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
617 {
618 protected:
619 AgentConnection *m_agentConnection;
620 NXCPMessage *m_response;
621 InetAddress m_ipAddr;
622 UINT16 m_port;
623 bool m_waitForResponse;
624
625 public:
626 SNMP_ProxyTransport(AgentConnection *conn, const InetAddress& ipAddr, UINT16 port);
627 virtual ~SNMP_ProxyTransport();
628
629 virtual int readMessage(SNMP_PDU **ppData, UINT32 dwTimeout = INFINITE,
630 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL,
631 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
632 virtual int sendMessage(SNMP_PDU *pdu);
633 virtual InetAddress getPeerIpAddress();
634 virtual UINT16 getPort();
635 virtual bool isProxyTransport();
636
637 void setWaitForResponse(bool wait) { m_waitForResponse = wait; }
638 };
639
640 /**
641 * ISC flags
642 */
643 #define ISCF_IS_CONNECTED ((UINT32)0x00000001)
644 #define ISCF_REQUIRE_ENCRYPTION ((UINT32)0x00000002)
645
646 /**
647 * Inter-server connection (ISC)
648 */
649 class LIBNXSRV_EXPORTABLE ISC
650 {
651 private:
652 UINT32 m_flags;
653 InetAddress m_addr;
654 WORD m_port;
655 SOCKET m_socket;
656 int m_protocolVersion;
657 VolatileCounter m_requestId;
658 UINT32 m_recvTimeout;
659 MsgWaitQueue *m_msgWaitQueue;
660 MUTEX m_mutexDataLock;
661 MUTEX m_socketLock;
662 THREAD m_hReceiverThread;
663 NXCPEncryptionContext *m_ctx;
664 UINT32 m_commandTimeout;
665
666 void receiverThread();
667 static THREAD_RESULT THREAD_CALL receiverThreadStarter(void *);
668
669 protected:
670 UINT32 setupEncryption(RSA *pServerKey);
671 UINT32 connectToService(UINT32 service);
672
673 void lock() { MutexLock(m_mutexDataLock); }
674 void unlock() { MutexUnlock(m_mutexDataLock); }
675
676 virtual void printMessage(const TCHAR *format, ...);
677 virtual void onBinaryMessage(NXCP_MESSAGE *rawMsg);
678 virtual bool onMessage(NXCPMessage *msg);
679
680 public:
681 ISC();
682 ISC(const InetAddress& addr, WORD port = NETXMS_ISC_PORT);
683 virtual ~ISC();
684
685 UINT32 connect(UINT32 service, RSA *serverKey = NULL, BOOL requireEncryption = FALSE);
686 void disconnect();
687 bool connected() { return m_flags & ISCF_IS_CONNECTED; };
688
689 BOOL sendMessage(NXCPMessage *msg);
690 NXCPMessage *waitForMessage(WORD code, UINT32 id, UINT32 timeOut) { return m_msgWaitQueue->waitForMessage(code, id, timeOut); }
691 UINT32 waitForRCC(UINT32 rqId, UINT32 timeOut);
692 UINT32 generateMessageId() { return (UINT32)InterlockedIncrement(&m_requestId); }
693
694 UINT32 nop();
695 };
696
697
698 //
699 // Functions
700 //
701
702 void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
703 void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
704 void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
705 const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(UINT32 err);
706 UINT32 LIBNXSRV_EXPORTABLE AgentErrorToRCC(UINT32 err);
707
708 void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...)
709 #if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
710 __attribute__ ((format(printf, 2, 3)))
711 #endif
712 ;
713
714 // for compatibility - new code should use nxlog_debug
715 #define DbgPrintf nxlog_debug
716
717 void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
718
719 const TCHAR LIBNXSRV_EXPORTABLE *ISCErrorCodeToText(UINT32 code);
720
721 /**
722 * Variables
723 */
724 extern UINT64 LIBNXSRV_EXPORTABLE g_flags;
725 extern ThreadPool LIBNXSRV_EXPORTABLE *g_agentConnectionThreadPool;
726
727 /**
728 * Helper finctions for checking server flags
729 */
730 inline bool IsStandalone()
731 {
732 return !(g_flags & AF_DAEMON) ? true : false;
733 }
734
735 inline bool IsZoningEnabled()
736 {
737 return (g_flags & AF_ENABLE_ZONING) ? true : false;
738 }
739
740 inline bool IsShutdownInProgress()
741 {
742 return (g_flags & AF_SHUTDOWN) ? true : false;
743 }
744
745 #endif /* _nxsrvapi_h_ */