2 ** NetXMS - Network Management System
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
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.
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.
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.
28 #ifdef LIBNXSRV_EXPORTS
29 #define LIBNXSRV_EXPORTABLE __declspec(dllexport)
31 #define LIBNXSRV_EXPORTABLE __declspec(dllimport)
34 #define LIBNXSRV_EXPORTABLE
37 #ifndef LIBNXCL_NO_DECLARATIONS
38 #define LIBNXCL_NO_DECLARATIONS 1
42 #include <nms_agent.h>
43 #include "../libnxsrv/messages.h"
45 #include <netxms_isc.h>
54 # define DEFAULT_CONFIG_FILE _T("C:\\netxmsd.conf")
56 # define DEFAULT_SHELL "cmd.exe"
57 # define DEFAULT_LOG_FILE "C:\\NetXMS.log"
58 # define DEFAULT_DATA_DIR "C:\\NetXMS\\var"
59 # define DEFAULT_DUMP_DIR "C:\\"
61 # define DDIR_MIBS "\\mibs"
62 # define DDIR_PACKAGES "\\packages"
63 # define DDIR_BACKGROUNDS "\\backgrounds"
64 # define DDIR_SHARED_FILES "\\shared"
65 # define DFILE_KEYS "\\server_key"
66 # define DFILE_COMPILED_MIB "\\mibs\\netxms.mib"
70 # define DEFAULT_CONFIG_FILE _T("{search}")
72 # define DEFAULT_SHELL "/bin/sh"
75 # define DATADIR "/var/netxms"
78 # define DEFAULT_LOG_FILE DATADIR"/log/netxmsd.log"
79 # define DEFAULT_DATA_DIR DATADIR
80 # define DEFAULT_DUMP_DIR "/"
82 # define DDIR_MIBS "/mibs"
83 # define DDIR_PACKAGES "/packages"
84 # define DDIR_BACKGROUNDS "/backgrounds"
85 # define DDIR_SHARED_FILES "/shared"
86 # define DFILE_KEYS "/.server_key"
87 # define DFILE_COMPILED_MIB "/mibs/netxms.mib"
96 #define AF_DAEMON 0x00000001
97 #define AF_USE_SYSLOG 0x00000002
98 #define AF_ENABLE_NETWORK_DISCOVERY 0x00000004
99 #define AF_ACTIVE_NETWORK_DISCOVERY 0x00000008
100 #define AF_LOG_SQL_ERRORS 0x00000010
101 #define AF_DELETE_EMPTY_SUBNETS 0x00000020
102 #define AF_ENABLE_SNMP_TRAPD 0x00000040
103 #define AF_ENABLE_ZONING 0x00000080
104 #define AF_SYNC_NODE_NAMES_WITH_DNS 0x00000100
105 #define AF_CHECK_TRUSTED_NODES 0x00000200
106 #define AF_WRITE_FULL_DUMP 0x00080000
107 #define AF_RESOLVE_NODE_NAMES 0x00100000
108 #define AF_CATCH_EXCEPTIONS 0x00200000
109 #define AF_INTERNAL_CA 0x00400000
110 #define AF_DB_LOCKED 0x01000000
111 #define AF_ENABLE_MULTIPLE_DB_CONN 0x02000000
112 #define AF_DB_CONNECTION_LOST 0x04000000
113 #define AF_NO_NETWORK_CONNECTIVITY 0x08000000
114 #define AF_EVENT_STORM_DETECTED 0x08000000
115 #define AF_SERVER_INITIALIZED 0x40000000
116 #define AF_SHUTDOWN 0x80000000
118 #define IsStandalone() (!(g_dwFlags & AF_DAEMON))
119 #define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
123 // Encryption usage policies
126 #define ENCRYPTION_DISABLED 0
127 #define ENCRYPTION_ALLOWED 1
128 #define ENCRYPTION_PREFERRED 2
129 #define ENCRYPTION_REQUIRED 3
133 // Win32 service and syslog constants
138 #define CORE_SERVICE_NAME _T("NetXMSCore")
139 #define CORE_EVENT_SOURCE _T("NetXMSCore")
140 #define NETXMSD_SYSLOG_NAME CORE_EVENT_SOURCE
144 #define NETXMSD_SYSLOG_NAME _T("netxmsd")
150 // Single ARP cache entry
155 DWORD dwIndex
; // Interface index
157 BYTE bMacAddr
[MAC_ADDR_LENGTH
];
162 // ARP cache structure used by discovery functions and AgentConnection class
173 // Interface information structure used by discovery functions and AgentConnection class
178 TCHAR szName
[MAX_DB_STRING
];
183 BYTE bMacAddr
[MAC_ADDR_LENGTH
];
184 int iNumSecondary
; // Number of secondary IP's on this interface
189 // Interface list used by discovery functions and AgentConnection class
194 int iNumEntries
; // Number of entries in pInterfaces
195 int iEnumPos
; // Used by index enumeration handler
196 void *pArg
; // Can be used by custom enumeration handlers
197 INTERFACE_INFO
*pInterfaces
; // Interface entries
221 int iNumEntries
; // Number of entries
222 ROUTE
*pRoutes
; // Route list
230 class LIBNXSRV_EXPORTABLE AgentConnection
234 int m_nProtocolVersion
;
236 TCHAR m_szSecret
[MAX_SECRET_LENGTH
];
237 time_t m_tLastCommandTime
;
239 DWORD m_dwNumDataLines
;
241 DWORD m_dwCommandTimeout
;
242 DWORD m_connectionTimeout
;
243 DWORD m_dwRecvTimeout
;
244 TCHAR
**m_ppDataLines
;
245 MsgWaitQueue
*m_pMsgWaitQueue
;
247 MUTEX m_mutexDataLock
;
248 THREAD m_hReceiverThread
;
249 CSCP_ENCRYPTION_CONTEXT
*m_pCtx
;
250 int m_iEncryptionPolicy
;
256 TCHAR m_szProxySecret
[MAX_SECRET_LENGTH
];
258 TCHAR m_currentFileName
[MAX_PATH
];
259 DWORD m_dwDownloadRequestId
;
260 CONDITION m_condFileDownload
;
261 BOOL m_fileDownloadSucceeded
;
262 void (*m_downloadProgressCallback
)(size_t, void *);
263 void *m_downloadProgressCallbackArg
;
264 bool m_deleteFileOnDownloadFailure
;
265 bool m_fileUploadInProgress
;
267 void ReceiverThread(void);
268 static THREAD_RESULT THREAD_CALL
ReceiverThreadStarter(void *);
271 void destroyResultData();
272 BOOL
sendMessage(CSCPMessage
*pMsg
);
273 CSCPMessage
*waitForMessage(WORD wCode
, DWORD dwId
, DWORD dwTimeOut
) { return m_pMsgWaitQueue
->WaitForMessage(wCode
, dwId
, dwTimeOut
); }
274 DWORD
waitForRCC(DWORD dwRqId
, DWORD dwTimeOut
);
275 DWORD
setupEncryption(RSA
*pServerKey
);
276 DWORD
authenticate(BOOL bProxyData
);
277 DWORD
setupProxyConnection();
278 DWORD
getIpAddr() { return ntohl(m_dwAddr
); }
279 DWORD
prepareFileDownload(const TCHAR
*fileName
, DWORD rqId
, bool append
, void (*downloadProgressCallback
)(size_t, void *), void *cbArg
);
281 virtual void PrintMsg(const TCHAR
*pszFormat
, ...);
282 virtual void onTrap(CSCPMessage
*pMsg
);
283 virtual void onDataPush(CSCPMessage
*msg
);
284 virtual void onFileDownload(BOOL success
);
286 void Lock(void) { MutexLock(m_mutexDataLock
, INFINITE
); }
287 void Unlock(void) { MutexUnlock(m_mutexDataLock
); }
290 AgentConnection(DWORD dwAddr
, WORD wPort
= AGENT_LISTEN_PORT
,
291 int iAuthMethod
= AUTH_NONE
, const TCHAR
*pszSecret
= NULL
);
292 virtual ~AgentConnection();
294 BOOL
connect(RSA
*pServerKey
= NULL
, BOOL bVerbose
= FALSE
, DWORD
*pdwError
= NULL
);
296 BOOL
isConnected() { return m_bIsConnected
; }
297 int getProtocolVersion() { return m_nProtocolVersion
; }
299 SOCKET
getSocket() { return m_hSocket
; }
301 ARP_CACHE
*GetArpCache(void);
302 INTERFACE_LIST
*GetInterfaceList();
303 ROUTING_TABLE
*GetRoutingTable();
304 DWORD
GetParameter(const TCHAR
*pszParam
, DWORD dwBufSize
, TCHAR
*pszBuffer
);
305 DWORD
GetList(const TCHAR
*pszParam
);
307 DWORD
ExecAction(const TCHAR
*pszAction
, int argc
, TCHAR
**argv
);
308 DWORD
UploadFile(const TCHAR
*pszFile
, void (* progressCallback
)(INT64
, void *) = NULL
, void *cbArg
= NULL
);
309 DWORD
StartUpgrade(const TCHAR
*pszPkgName
);
310 DWORD
CheckNetworkService(DWORD
*pdwStatus
, DWORD dwIpAddr
, int iServiceType
, WORD wPort
= 0,
311 WORD wProto
= 0, const TCHAR
*pszRequest
= NULL
, const TCHAR
*pszResponse
= NULL
);
312 DWORD
GetSupportedParameters(DWORD
*pdwNumParams
, NXC_AGENT_PARAM
**ppParamList
);
313 DWORD
GetConfigFile(TCHAR
**ppszConfig
, DWORD
*pdwSize
);
314 DWORD
updateConfigFile(const TCHAR
*pszConfig
);
317 DWORD
generateRequestId() { return m_dwRequestId
++; }
318 CSCPMessage
*customRequest(CSCPMessage
*pRequest
, const TCHAR
*recvFile
= NULL
, bool appendFile
= false,
319 void (*downloadProgressCallback
)(size_t, void *) = NULL
, void *cbArg
= NULL
);
321 DWORD
getNumDataLines() { return m_dwNumDataLines
; }
322 const TCHAR
*getDataLine(DWORD dwIndex
) { return dwIndex
< m_dwNumDataLines ? m_ppDataLines
[dwIndex
] : _T("(error)"); }
324 void setConnectionTimeout(DWORD dwTimeout
) { m_connectionTimeout
= max(dwTimeout
, 1000); }
325 DWORD
getConnectionTimeout() { return m_connectionTimeout
; }
326 void setCommandTimeout(DWORD dwTimeout
) { m_dwCommandTimeout
= max(dwTimeout
, 500); }
327 DWORD
getCommandTimeout() { return m_dwCommandTimeout
; }
328 void setRecvTimeout(DWORD dwTimeout
) { m_dwRecvTimeout
= max(dwTimeout
, 10000); }
329 void setEncryptionPolicy(int iPolicy
) { m_iEncryptionPolicy
= iPolicy
; }
330 void setProxy(DWORD dwAddr
, WORD wPort
= AGENT_LISTEN_PORT
,
331 int iAuthMethod
= AUTH_NONE
, const TCHAR
*pszSecret
= NULL
);
332 void setPort(WORD wPort
) { m_wPort
= wPort
; }
333 void setAuthData(int nMethod
, const char *pszSecret
) { m_iAuthMethod
= nMethod
; nx_strncpy(m_szSecret
, pszSecret
, MAX_SECRET_LENGTH
); }
334 void setDeleteFileOnDownloadFailure(bool flag
) { m_deleteFileOnDownloadFailure
= flag
; }
339 // Proxy SNMP transport
342 class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport
: public SNMP_Transport
345 AgentConnection
*m_pAgentConnection
;
346 CSCPMessage
*m_pResponse
;
351 SNMP_ProxyTransport(AgentConnection
*pConn
, DWORD dwIpAddr
, WORD wPort
);
352 virtual ~SNMP_ProxyTransport();
354 virtual int readMessage(SNMP_PDU
**ppData
, DWORD dwTimeout
= INFINITE
,
355 struct sockaddr
*pSender
= NULL
, socklen_t
*piAddrSize
= NULL
,
356 SNMP_SecurityContext
* (*contextFinder
)(struct sockaddr
*, socklen_t
) = NULL
);
357 virtual int sendMessage(SNMP_PDU
*pdu
);
365 #define ISCF_IS_CONNECTED ((DWORD)0x00000001)
366 #define ISCF_REQUIRE_ENCRYPTION ((DWORD)0x00000002)
370 // Inter-server connection (ISC)
373 class LIBNXSRV_EXPORTABLE ISC
380 int m_protocolVersion
;
383 MsgWaitQueue
*m_msgWaitQueue
;
384 MUTEX m_mutexDataLock
;
385 THREAD m_hReceiverThread
;
386 CSCP_ENCRYPTION_CONTEXT
*m_ctx
;
387 DWORD m_commandTimeout
;
389 void ReceiverThread(void);
390 static THREAD_RESULT THREAD_CALL
ReceiverThreadStarter(void *);
393 void DestroyResultData(void);
394 DWORD
SetupEncryption(RSA
*pServerKey
);
395 DWORD
ConnectToService(DWORD service
);
397 void Lock(void) { MutexLock(m_mutexDataLock
, INFINITE
); }
398 void Unlock(void) { MutexUnlock(m_mutexDataLock
); }
400 virtual void PrintMsg(const TCHAR
*format
, ...);
404 ISC(DWORD addr
, WORD port
= NETXMS_ISC_PORT
);
407 DWORD
Connect(DWORD service
, RSA
*serverKey
= NULL
, BOOL requireEncryption
= FALSE
);
410 BOOL
SendMessage(CSCPMessage
*msg
);
411 CSCPMessage
*WaitForMessage(WORD code
, DWORD id
, DWORD timeOut
) { return m_msgWaitQueue
->WaitForMessage(code
, id
, timeOut
); }
412 DWORD
WaitForRCC(DWORD rqId
, DWORD timeOut
);
422 void LIBNXSRV_EXPORTABLE
DestroyArpCache(ARP_CACHE
*pArpCache
);
423 void LIBNXSRV_EXPORTABLE
DestroyInterfaceList(INTERFACE_LIST
*pIfList
);
424 void LIBNXSRV_EXPORTABLE
DestroyRoutingTable(ROUTING_TABLE
*pRT
);
425 void LIBNXSRV_EXPORTABLE
SortRoutingTable(ROUTING_TABLE
*pRT
);
426 const TCHAR LIBNXSRV_EXPORTABLE
*AgentErrorCodeToText(int iError
);
428 void LIBNXSRV_EXPORTABLE
WriteLogOther(WORD wType
, const TCHAR
*format
, ...);
429 void LIBNXSRV_EXPORTABLE
DbgPrintf(int level
, const TCHAR
*format
, ...);
430 void LIBNXSRV_EXPORTABLE
DbgPrintf2(int level
, const TCHAR
*format
, va_list args
);
432 void LIBNXSRV_EXPORTABLE
SetAgentDEP(int iPolicy
);
434 const TCHAR LIBNXSRV_EXPORTABLE
*ISCErrorCodeToText(DWORD code
);
441 extern DWORD LIBNXSRV_EXPORTABLE g_dwFlags
;
442 extern int LIBNXSRV_EXPORTABLE g_nDebugLevel
;
444 #endif /* _nxsrvapi_h_ */