- All component locks moved to memory
[public/netxms.git] / src / server / core / nms_core.h
CommitLineData
9a19737f 1/*
b54b2b11
VK
2** NetXMS - Network Management System
3** Copyright (C) 2003, 2004 Victor Kirhenshtein
9a19737f
VK
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** $module: nms_core.h
20**
21**/
22
23#ifndef _nms_core_h_
24#define _nms_core_h_
25
3c55b85d
VK
26#define LIBNXCL_NO_DECLARATIONS 1
27
e31784c0 28#include <nms_common.h>
0ee0c807 29
e31784c0 30#ifndef _WIN32
0ee0c807 31
c9ccd685
AK
32#if HAVE_SYS_TYPES_H
33# include <sys/types.h>
34#endif
35
e31784c0 36#if HAVE_DLFCN_H
3a2f672c 37#include <dlfcn.h>
e31784c0 38#endif
0ee0c807 39
5af4c41b 40#define WSAGetLastError() (errno)
0ee0c807 41
9a19737f
VK
42#endif /* _WIN32 */
43
4385fa12
VK
44#include <time.h>
45#include <stdio.h>
46#include <string.h>
f9b7e653 47#include <stdarg.h>
469b937c 48
e31784c0 49#ifdef _WIN32
66c04cc7 50#define _GETOPT_H_ 1 /* Prevent including getopt.h from net-snmp */
53512272 51#define HAVE_SOCKLEN_T /* Prevent defining socklen_t in net-snmp */
e31784c0 52#endif /* _WIN32 */
66c04cc7 53
3d1cb7d6
VK
54//#include <net-snmp/net-snmp-config.h>
55//#include <net-snmp/net-snmp-includes.h>
469b937c 56
9a19737f 57#include <nms_threads.h>
3a2f672c 58#include <dbdrv.h>
5af4c41b 59#include <nms_cscp.h>
4a4afbd0 60#include <nms_util.h>
f11f4edc 61#include <nxsrvapi.h>
b02279eb 62#include <nxcscpapi.h>
e641b7d0 63#include <nximage.h>
77312b6f 64#include <nxqueue.h>
3d1cb7d6 65#include <nxsnmp.h>
1275c750 66#include "nms_dcoll.h"
23a32988 67#include "nms_users.h"
3ea35b38 68#include "nms_objects.h"
3a2f672c 69#include "messages.h"
b54b2b11 70#include "nms_locks.h"
9a19737f
VK
71
72
73//
d2a1dd18 74// Common constants and macros
9a19737f
VK
75//
76
9a19737f 77#ifdef _WIN32
06e7be2f 78
4d5a05a0 79#define DEFAULT_SHELL "cmd.exe"
1275c750 80#define DEFAULT_LOG_FILE "C:\\NetXMS.log"
06e7be2f
VK
81#define DEFAULT_DATA_DIR "C:\\NetXMS\\var"
82
83#define DDIR_MIBS "\\mibs"
e641b7d0 84#define DDIR_IMAGES "\\images"
06e7be2f 85
9a19737f 86#else /* _WIN32 */
06e7be2f 87
4d5a05a0 88#define DEFAULT_SHELL "/bin/sh"
9e9d381c 89#define DEFAULT_LOG_FILE "/var/log/netxmsd.log"
06e7be2f
VK
90#define DEFAULT_DATA_DIR "/var/netxms"
91
92#define DDIR_MIBS "/mibs"
e641b7d0 93#define DDIR_IMAGES "/images"
06e7be2f 94
9a19737f
VK
95#endif /* _WIN32 */
96
64584976 97#define MAX_LINE_SIZE 4096
3a2f672c 98
4d5a05a0 99#define GROUP_FLAG_BIT ((DWORD)0x80000000)
c7ca9142 100
f92869c9
VK
101typedef void * HSNMPSESSION;
102
b7271ccb 103
3ea35b38
VK
104//
105// Unique identifier group codes
106//
107
108#define IDG_NETWORK_OBJECT 0
ef44d5ea 109#define IDG_CONTAINER_CAT 1
3ea35b38
VK
110#define IDG_EVENT 2
111#define IDG_ITEM 3
62d11997 112//#define IDG_DCT 4
a60d4f27 113#define IDG_IMAGE 5
3ea35b38
VK
114#define IDG_ACTION 6
115#define IDG_EVENT_GROUP 7
116#define IDG_THRESHOLD 8
de78f964
VK
117#define IDG_USER 9
118#define IDG_USER_GROUP 10
06e7be2f
VK
119#define IDG_ALARM 11
120#define IDG_ALARM_NOTE 12
3ea35b38
VK
121
122
9a19737f 123//
4385fa12 124// Application flags
9a19737f
VK
125//
126
3e4e127f
VK
127#define AF_STANDALONE 0x00000001
128#define AF_USE_EVENT_LOG 0x00000002
129#define AF_ENABLE_ACCESS_CONTROL 0x00000004
130#define AF_ENABLE_EVENTS_ACCESS_CONTROL 0x00000008
78bf9c68 131#define AF_LOG_SQL_ERRORS 0x00000010
3ea35b38 132#define AF_DELETE_EMPTY_SUBNETS 0x00000020
5add75d8 133#define AF_ENABLE_SNMP_TRAPD 0x00000040
3e4e127f
VK
134#define AF_DEBUG_EVENTS 0x00000100
135#define AF_DEBUG_CSCP 0x00000200
136#define AF_DEBUG_DISCOVERY 0x00000400
137#define AF_DEBUG_DC 0x00000800
83f0529c 138#define AF_DEBUG_HOUSEKEEPER 0x00001000
b54b2b11 139#define AF_DEBUG_LOCKS 0x00002000
4d5a05a0 140#define AF_DEBUG_ACTIONS 0x00004000
0c598d0e 141#define AF_DEBUG_MISC 0x00008000
b1dd534d 142#define AF_DEBUG_SQL 0x00010000
3aeed82c
VK
143#define AF_DEBUG_SNMP 0x00020000
144#define AF_DEBUG_ALL 0x0002FF00
2260ffe5 145#define AF_DB_LOCKED 0x01000000
3e4e127f 146#define AF_SHUTDOWN 0x80000000
a68e217b 147
1a26164f 148#define IsStandalone() (g_dwFlags & AF_STANDALONE)
a68e217b 149#define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
9a19737f
VK
150
151
62f5857f 152//
b54b2b11 153// Client session flags
62f5857f
VK
154//
155
b8bad201
VK
156#define CSF_EVENT_DB_LOCKED ((DWORD)0x0001)
157#define CSF_EPP_LOCKED ((DWORD)0x0002)
158#define CSF_EVENT_DB_MODIFIED ((DWORD)0x0004)
ff550544 159#define CSF_USER_DB_LOCKED ((DWORD)0x0008)
8f7e2482 160#define CSF_EPP_UPLOAD ((DWORD)0x0010)
c9363772 161#define CSF_ACTION_DB_LOCKED ((DWORD)0x0020)
62f5857f
VK
162
163
c7ca9142 164//
b54b2b11 165// Information categories for UPDATE_INFO structure
c7ca9142
VK
166//
167
53512272
VK
168#define INFO_CAT_EVENT 1
169#define INFO_CAT_OBJECT_CHANGE 2
03482383 170#define INFO_CAT_ALARM 3
c9363772 171#define INFO_CAT_ACTION 4
c7ca9142
VK
172
173
f9b7e653
VK
174//
175// Event handling subsystem definitions
176//
177
178#include "nms_events.h"
7e495d97 179#include "nms_actions.h"
2260ffe5 180#include "nms_alarm.h"
f9b7e653
VK
181
182
62f5857f
VK
183//
184// Data update structure for client sessions
185//
186
187typedef struct
188{
189 DWORD dwCategory; // Data category - event, network object, etc.
c9363772 190 DWORD dwCode; // Data-specific update code
62f5857f
VK
191 void *pData; // Pointer to data block
192} UPDATE_INFO;
193
194
5af4c41b
VK
195//
196// Client session
197//
198
199class ClientSession
200{
201private:
202 SOCKET m_hSocket;
203 Queue *m_pSendQueue;
4a4afbd0 204 Queue *m_pMessageQueue;
62f5857f 205 Queue *m_pUpdateQueue;
da54bd8d
VK
206 DWORD m_dwIndex;
207 int m_iState;
208 DWORD m_dwUserId;
4b8ef715 209 DWORD m_dwSystemAccess; // User's system access rights
b54b2b11 210 DWORD m_dwFlags; // Session flags
3664c5ba 211 CSCP_BUFFER *m_pMsgBuffer;
ccdbbb52
VK
212 THREAD m_hWriteThread;
213 THREAD m_hProcessingThread;
214 THREAD m_hUpdateThread;
03482383
VK
215 MUTEX m_mutexSendEvents;
216 MUTEX m_mutexSendObjects;
217 MUTEX m_mutexSendAlarms;
c9363772 218 MUTEX m_mutexSendActions;
d966d18a 219 MUTEX m_mutexPollerInit;
b54b2b11
VK
220 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
221 char m_szUserName[256]; // String in form login_name@host
338493a0
VK
222 DWORD m_dwOpenDCIListSize; // Number of open DCI lists
223 DWORD *m_pOpenDCIList; // List of nodes with DCI lists open
8f7e2482
VK
224 DWORD m_dwNumRecordsToUpload; // Number of records to be uploaded
225 DWORD m_dwRecordsUploaded;
226 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
da54bd8d 227
ccdbbb52
VK
228 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
229 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
230 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
231 static THREAD_RESULT THREAD_CALL UpdateThreadStarter(void *);
d966d18a 232 static THREAD_RESULT THREAD_CALL PollerThreadStarter(void *);
ccdbbb52
VK
233
234 void ReadThread(void);
235 void WriteThread(void);
236 void ProcessingThread(void);
237 void UpdateThread(void);
d966d18a 238 void PollerThread(Node *pNode, int iPollType, DWORD dwRqId);
ccdbbb52 239
605d2931
VK
240 BOOL CheckSysAccessRights(DWORD dwRequiredAccess)
241 {
242 return m_dwUserId == 0 ? TRUE :
243 ((dwRequiredAccess & m_dwSystemAccess) ? TRUE : FALSE);
244 }
245
da54bd8d 246 void DebugPrintf(char *szFormat, ...);
7257eb7d 247 void Login(CSCPMessage *pRequest);
e100f091 248 void SendAllObjects(DWORD dwRqId);
74a5aa17 249 void SendAllEvents(DWORD dwRqId);
4b8ef715 250 void SendAllConfigVars(void);
23a32988 251 void SendUserDB(DWORD dwRqId);
03482383 252 void SendAllAlarms(DWORD dwRqId, BOOL bIncludeAck);
7257eb7d
VK
253 void CreateUser(CSCPMessage *pRequest);
254 void UpdateUser(CSCPMessage *pRequest);
255 void DeleteUser(CSCPMessage *pRequest);
017e1e13 256 void SetPassword(CSCPMessage *pRequest);
ff550544 257 void LockUserDB(DWORD dwRqId, BOOL bLock);
7257eb7d 258 void SetConfigVariable(CSCPMessage *pRequest);
b54b2b11 259 void SendEventDB(DWORD dwRqId);
37c4d6aa
VK
260 void LockEventDB(DWORD dwRqId);
261 void UnlockEventDB(DWORD dwRqId);
7257eb7d 262 void SetEventInfo(CSCPMessage *pRequest);
37c4d6aa
VK
263 void DeleteEventTemplate(CSCPMessage *pRequest);
264 void GenerateEventId(DWORD dwRqId);
7257eb7d 265 void ModifyObject(CSCPMessage *pRequest);
c1c39152 266 void ChangeObjectMgmtStatus(CSCPMessage *pRequest);
7257eb7d
VK
267 void OpenNodeDCIList(CSCPMessage *pRequest);
268 void CloseNodeDCIList(CSCPMessage *pRequest);
9ed4eaff 269 void ModifyNodeDCI(CSCPMessage *pRequest);
933aee5c 270 void CopyDCI(CSCPMessage *pRequest);
ebebc8ea 271 void GetCollectedData(CSCPMessage *pRequest);
06e7be2f
VK
272 void OpenEPP(DWORD dwRqId);
273 void CloseEPP(DWORD dwRqId);
274 void SaveEPP(CSCPMessage *pRequest);
8f7e2482 275 void ProcessEPPRecord(CSCPMessage *pRequest);
06e7be2f
VK
276 void SendMIBList(DWORD dwRqId);
277 void SendMIB(CSCPMessage *pRequest);
e6b958c4
VK
278 void CreateObject(CSCPMessage *pRequest);
279 void ChangeObjectBinding(CSCPMessage *pRequest, BOOL bBind);
88039e6c 280 void DeleteObject(CSCPMessage *pRequest);
ee20e615 281 void AcknowlegeAlarm(CSCPMessage *pRequest);
c9363772
VK
282 void CreateAction(CSCPMessage *pRequest);
283 void UpdateAction(CSCPMessage *pRequest);
284 void DeleteAction(CSCPMessage *pRequest);
285 void LockActionDB(DWORD dwRqId, BOOL bLock);
e91cb822 286 void SendAllActions(DWORD dwRqId);
6849d9be 287 void SendContainerCategories(DWORD dwRqId);
0b89f5fb 288 void ForcedNodePoll(CSCPMessage *pRequest);
b562fca8 289 void OnTrap(CSCPMessage *pRequest);
ed1140dc 290 void OnWakeUpNode(CSCPMessage *pRequest);
b4895bbe
VK
291 void CreateTrap(CSCPMessage *pRequest);
292 void UpdateTrap(CSCPMessage *pRequest);
293 void DeleteTrap(CSCPMessage *pRequest);
294 void LockTrapCfg(DWORD dwRqId, BOOL bLock);
295 void SendAllTraps(DWORD dwRqId);
5af4c41b
VK
296
297public:
b54b2b11 298 ClientSession(SOCKET hSocket, DWORD dwHostAddr);
5af4c41b
VK
299 ~ClientSession();
300
ccdbbb52 301 void Run(void);
da54bd8d 302
3c55b85d 303 void SendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
d966d18a 304 void SendPollerMsg(DWORD dwRqId, TCHAR *pszMsg);
3c55b85d 305
da54bd8d
VK
306 DWORD GetIndex(void) { return m_dwIndex; }
307 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
62f5857f 308 int GetState(void) { return m_iState; }
c868331f 309 const char *GetUserName(void) { return m_szUserName; }
03482383 310 DWORD GetUserId(void) { return m_dwUserId; }
20177e8e
VK
311
312 void Kill(void);
4de204a3 313 void Notify(DWORD dwCode, DWORD dwData = 0);
62f5857f
VK
314
315 void OnNewEvent(Event *pEvent);
53512272 316 void OnObjectChange(NetObj *pObject);
984542f8 317 void OnUserDBUpdate(int iCode, DWORD dwUserId, NMS_USER *pUser, NMS_USER_GROUP *pGroup);
03482383 318 void OnAlarmUpdate(DWORD dwCode, NXC_ALARM *pAlarm);
c9363772 319 void OnActionDBUpdate(DWORD dwCode, NXC_ACTION *pAction);
5af4c41b
VK
320};
321
322
9a19737f
VK
323//
324// Functions
325//
326
62d11997 327BOOL ConfigReadStr(char *szVar, char *szBuffer, int iBufSize, const char *szDefault);
7084ac81 328int ConfigReadInt(char *szVar, int iDefault);
e77547c2 329DWORD ConfigReadULong(char *szVar, DWORD dwDefault);
7084ac81
VK
330BOOL ConfigWriteStr(char *szVar, char *szValue, BOOL bCreate);
331BOOL ConfigWriteInt(char *szVar, int iValue, BOOL bCreate);
e77547c2 332BOOL ConfigWriteULong(char *szVar, DWORD dwValue, BOOL bCreate);
7084ac81 333
4385fa12
VK
334BOOL ParseCommandLine(int argc, char *argv[]);
335BOOL LoadConfig(void);
9a19737f
VK
336
337void Shutdown(void);
338BOOL Initialize(void);
339void Main(void);
340
d627ae40
VK
341BOOL SleepAndCheckForShutdown(int iSeconds);
342
d627ae40
VK
343void SaveObjects(void);
344
067f2689 345void QueueSQLRequest(char *szQuery);
ccdbbb52 346void StartDBWriter(void);
83f0529c 347void StopDBWriter(void);
067f2689 348
be0a5a53 349void SnmpInit(void);
37c4d6aa 350BOOL SnmpGet(DWORD dwVersion, DWORD dwAddr, const char *szCommunity, const char *szOidStr,
3d1cb7d6 351 const DWORD *oidBinary, DWORD dwOidLen, void *pValue,
f92869c9 352 DWORD dwBufferSize, BOOL bVerbose, BOOL bStringResult);
37c4d6aa 353BOOL SnmpEnumerate(DWORD dwVersion, DWORD dwAddr, const char *szCommunity, const char *szRootOid,
3d1cb7d6 354 void (* pHandler)(DWORD, DWORD, const char *, SNMP_Variable *, void *),
4ea67c99 355 void *pUserArg, BOOL bVerbose);
b50f1100 356void StrToMac(char *pszStr, BYTE *pBuffer);
18e26ff8 357DWORD OidToType(TCHAR *pszOid, DWORD *pdwFlags);
d7d76e50 358
b688074e
VK
359void InitLocalNetInfo(void);
360
910ca685 361ARP_CACHE *GetLocalArpCache(void);
37c4d6aa 362ARP_CACHE *SnmpGetArpCache(DWORD dwVersion, DWORD dwAddr, const char *szCommunity);
910ca685 363
37c4d6aa 364INTERFACE_LIST *SnmpGetInterfaceList(DWORD dwVersion, DWORD dwAddr, const char *szCommunity, DWORD dwNodeType);
910ca685 365INTERFACE_LIST *GetLocalInterfaceList(void);
48b1c0ac 366void CleanInterfaceList(INTERFACE_LIST *pIfList);
a0495b6e 367
f91fa4c2 368void WatchdogInit(void);
a97797f8 369DWORD WatchdogAddThread(char *szName, time_t tNotifyInterval);
f91fa4c2
VK
370void WatchdogNotify(DWORD dwId);
371void WatchdogPrintStatus(void);
372
3e4e127f 373void CheckForMgmtNode(void);
b900a78b 374NetObj *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags, TCHAR *pszName);
3e4e127f 375
62f5857f 376void EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
3421c063 377void NotifyClient(ClientSession *pSession, void *pArg);
62f5857f 378
c7ca9142
VK
379void GetSysInfoStr(char *pszBuffer);
380DWORD GetLocalIpAddr(void);
381
4d5a05a0 382BOOL ExecCommand(char *pszCommand);
ed1140dc 383BOOL SendMagicPacket(DWORD dwIpAddr, BYTE *pbMacAddr, int iNumPackets);
4d5a05a0 384
3ea35b38
VK
385BOOL InitIdTable(void);
386DWORD CreateUniqueId(int iGroup);
387
06e7be2f
VK
388BYTE *LoadFile(char *pszFileName, DWORD *pdwFileSize);
389
e641b7d0 390void UpdateImageHashes(void);
aa3743b2
VK
391void SendImageCatalogue(ClientSession *pSession, DWORD dwRqId, WORD wFormat);
392void SendImageFile(ClientSession *pSession, DWORD dwRqId, DWORD dwImageId, WORD wFormat);
8bff53f6 393void SendDefaultImageList(ClientSession *pSession, DWORD dwRqId);
e641b7d0 394
8f7e2482
VK
395char *EncodeSQLString(const char *pszIn);
396void DecodeSQLString(char *pszStr);
397
b86ba1c0
VK
398void InitMailer(void);
399void ShutdownMailer(void);
400void PostMail(char *pszRcpt, char *pszSubject, char *pszText);
401
37c4d6aa 402void GetAccelarVLANIfList(DWORD dwVersion, DWORD dwIpAddr, const TCHAR *pszCommunity, INTERFACE_LIST *pIfList);
18e26ff8 403
9a19737f
VK
404#ifdef _WIN32
405
406void InitService(void);
9cc9ea72 407void InstallService(char *execName, char *dllName);
9a19737f
VK
408void RemoveService(void);
409void StartCoreService(void);
410void StopCoreService(void);
411void InstallEventSource(char *path);
412void RemoveEventSource(void);
413
414char *GetSystemErrorText(DWORD error);
415
416#endif /* _WIN32 */
417
dbe67493
VK
418void DbgTestMutex(MUTEX hMutex, TCHAR *szName);
419void DbgTestRWLock(RWLOCK hLock, TCHAR *szName);
420void DbgPrintf(DWORD dwFlags, TCHAR *szFormat, ...);
c868331f 421void DumpSessions(void);
be0a5a53 422
9a19737f
VK
423
424//
425// Global variables
426//
427
4385fa12
VK
428extern DWORD g_dwFlags;
429extern char g_szConfigFile[];
430extern char g_szLogFile[];
9e9d381c
VK
431#ifndef _WIN32
432extern char g_szPIDFile[];
433#endif
06e7be2f 434extern char g_szDataDir[];
3a2f672c 435
3a2f672c 436extern DB_HANDLE g_hCoreDB;
067f2689 437extern Queue *g_pLazyRequestQueue;
9a19737f 438
eb8b1960
VK
439extern char *g_pszStatusName[];
440
9a19737f 441#endif /* _nms_core_h_ */