- Added component locks
[public/netxms.git] / src / server / core / nms_core.h
CommitLineData
9a19737f
VK
1/*
2** Project X - Network Management System
3** Copyright (C) 2003 Victor Kirhenshtein
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
9a19737f 26#ifdef _WIN32
0ee0c807 27
9a19737f 28#include <windows.h>
0ee0c807 29
9a19737f 30#else /* _WIN32 */
0ee0c807 31
9a19737f 32#include <unistd.h>
3a2f672c 33#include <dlfcn.h>
0ee0c807
VK
34#include <sys/socket.h>
35#include <sys/select.h>
36
37#define closesocket(x) close(x)
5af4c41b 38#define WSAGetLastError() (errno)
0ee0c807 39
9a19737f
VK
40#endif /* _WIN32 */
41
4385fa12
VK
42#include <time.h>
43#include <stdio.h>
44#include <string.h>
f9b7e653 45#include <stdarg.h>
469b937c
VK
46
47#include <net-snmp/net-snmp-config.h>
48#include <net-snmp/net-snmp-includes.h>
49
e77547c2
VK
50#include <openssl/ssl.h>
51
9a19737f
VK
52#include <nms_common.h>
53#include <nms_threads.h>
3a2f672c 54#include <dbdrv.h>
5af4c41b 55#include <nms_cscp.h>
4a4afbd0 56#include <nms_util.h>
da54bd8d 57#include "nms_users.h"
3e4e127f 58#include "nms_objects.h"
1275c750 59#include "nms_dcoll.h"
3a2f672c 60#include "messages.h"
9a19737f
VK
61
62
63//
64// Common constants
65//
66
9a19737f 67#ifdef _WIN32
1275c750
VK
68#define DEFAULT_CONFIG_FILE "C:\\NetXMS.conf"
69#define DEFAULT_LOG_FILE "C:\\NetXMS.log"
4385fa12 70#define IsStandalone() (g_dwFlags & AF_STANDALONE)
9a19737f 71#else /* _WIN32 */
1275c750
VK
72#define DEFAULT_CONFIG_FILE "/etc/netxms.conf"
73#define DEFAULT_LOG_FILE "/var/log/netxms.log"
9a19737f
VK
74#define IsStandalone() (1)
75#endif /* _WIN32 */
76
3a2f672c
VK
77#define MAX_DB_LOGIN 64
78#define MAX_DB_PASSWORD 64
79#define MAX_DB_NAME 32
5811233f 80#define MAX_LINE_SIZE 4096
3a2f672c 81
c7ca9142
VK
82#define UNLOCKED 0xFFFFFFFF
83
9a19737f
VK
84
85//
4385fa12 86// Application flags
9a19737f
VK
87//
88
3e4e127f
VK
89#define AF_STANDALONE 0x00000001
90#define AF_USE_EVENT_LOG 0x00000002
91#define AF_ENABLE_ACCESS_CONTROL 0x00000004
92#define AF_ENABLE_EVENTS_ACCESS_CONTROL 0x00000008
78bf9c68 93#define AF_LOG_SQL_ERRORS 0x00000010
3e4e127f
VK
94#define AF_DEBUG_EVENTS 0x00000100
95#define AF_DEBUG_CSCP 0x00000200
96#define AF_DEBUG_DISCOVERY 0x00000400
97#define AF_DEBUG_DC 0x00000800
83f0529c 98#define AF_DEBUG_HOUSEKEEPER 0x00001000
3e4e127f
VK
99#define AF_DEBUG_ALL 0x0000FF00
100#define AF_SHUTDOWN 0x80000000
a68e217b
VK
101
102#define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
9a19737f
VK
103
104
105//
106// Win32 service constants
107//
108
109#ifdef _WIN32
110
da54bd8d
VK
111#define CORE_SERVICE_NAME "NetXMSCore"
112#define CORE_EVENT_SOURCE "NetXMSCore"
9a19737f
VK
113
114#endif /* _WIN32 */
115
116
da54bd8d
VK
117//
118// Client session states
119//
120
121#define STATE_CLOSED 0
122#define STATE_CONNECTED 1
123#define STATE_AUTHENTICATED 2
124
125
62f5857f
VK
126//
127// Information categories for UPDATE_INFO structure
128//
129
130#define INFO_CAT_EVENT 1
131#define INFO_CAT_OBJECT 2
132
133
c7ca9142
VK
134//
135// Component identifiers used for locking
136//
137
138#define CID_NETXMS_INSTANCE 0
139#define CID_EPP 1
140#define CID_USER_MGMT 2
141
142
f9b7e653
VK
143//
144// Event handling subsystem definitions
145//
146
147#include "nms_events.h"
148
149
5811233f
VK
150//
151// Agent connection
152//
153
154class AgentConnection
155{
156private:
157 DWORD m_dwAddr;
158 int m_iAuthMethod;
159 char m_szSecret[MAX_SECRET_LENGTH];
160 time_t m_tLastCommandTime;
161 SOCKET m_hSocket;
162 WORD m_wPort;
163 DWORD m_dwNumDataLines;
164 char **m_ppDataLines;
165 char m_szNetBuffer[MAX_LINE_SIZE * 2];
166
167 int RecvLine(int iBufSize, char *szBuffer);
168 DWORD ExecuteCommand(char *szCmd, BOOL bExpectData = FALSE, BOOL bMultiLineData = FALSE);
169 void AddDataLine(char *szLine);
170 void DestroyResultData(void);
171
172public:
173 AgentConnection();
174 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT, int iAuthMethod = AUTH_NONE, char *szSecret = NULL);
175 ~AgentConnection();
176
4ea67c99 177 BOOL Connect(BOOL bVerbose = FALSE);
5811233f
VK
178 void Disconnect(void);
179
ce19c304 180 ARP_CACHE *GetArpCache(void);
5811233f
VK
181 INTERFACE_LIST *GetInterfaceList(void);
182 DWORD Nop(void) { return ExecuteCommand("NOP"); }
183 DWORD GetParameter(char *szParam, DWORD dwBufSize, char *szBuffer);
a0495b6e
VK
184};
185
186
62f5857f
VK
187//
188// Data update structure for client sessions
189//
190
191typedef struct
192{
193 DWORD dwCategory; // Data category - event, network object, etc.
194 void *pData; // Pointer to data block
195} UPDATE_INFO;
196
197
5af4c41b
VK
198//
199// Client session
200//
201
202class ClientSession
203{
204private:
205 SOCKET m_hSocket;
206 Queue *m_pSendQueue;
4a4afbd0 207 Queue *m_pMessageQueue;
62f5857f 208 Queue *m_pUpdateQueue;
da54bd8d
VK
209 DWORD m_dwIndex;
210 int m_iState;
211 DWORD m_dwUserId;
4b8ef715 212 DWORD m_dwSystemAccess; // User's system access rights
3664c5ba 213 CSCP_BUFFER *m_pMsgBuffer;
ecb7e1ee
VK
214 CONDITION m_hCondWriteThreadStopped;
215 CONDITION m_hCondProcessingThreadStopped;
62f5857f
VK
216 CONDITION m_hCondUpdateThreadStopped;
217 MUTEX m_hMutexSendEvents;
da54bd8d
VK
218
219 void DebugPrintf(char *szFormat, ...);
b81249b7 220 void SendAllObjects(void);
4b8ef715
VK
221 void SendAllEvents(void);
222 void SendAllConfigVars(void);
223 void SetConfigVariable(CSCPMessage *pMsg);
5af4c41b
VK
224
225public:
226 ClientSession(SOCKET hSocket);
227 ~ClientSession();
228
8f849e80 229 void SendMessage(CSCPMessage *pMsg);
5af4c41b
VK
230 void DispatchMessage(CSCP_MESSAGE *pMsg);
231
232 void ReadThread(void);
233 void WriteThread(void);
4a4afbd0 234 void ProcessingThread(void);
62f5857f 235 void UpdateThread(void);
da54bd8d
VK
236
237 DWORD GetIndex(void) { return m_dwIndex; }
238 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
62f5857f 239 int GetState(void) { return m_iState; }
20177e8e
VK
240
241 void Kill(void);
83f0529c 242 void Notify(DWORD dwCode);
62f5857f
VK
243
244 void OnNewEvent(Event *pEvent);
245 void OnObjectChange(DWORD dwObjectId);
5af4c41b
VK
246};
247
248
9a19737f
VK
249//
250// Functions
251//
252
7084ac81
VK
253BOOL ConfigReadStr(char *szVar, char *szBuffer, int iBufSize, char *szDefault);
254int ConfigReadInt(char *szVar, int iDefault);
e77547c2 255DWORD ConfigReadULong(char *szVar, DWORD dwDefault);
7084ac81
VK
256BOOL ConfigWriteStr(char *szVar, char *szValue, BOOL bCreate);
257BOOL ConfigWriteInt(char *szVar, int iValue, BOOL bCreate);
e77547c2 258BOOL ConfigWriteULong(char *szVar, DWORD dwValue, BOOL bCreate);
7084ac81 259
3a2f672c
VK
260void InitLog(void);
261void CloseLog(void);
262void WriteLog(DWORD msg, WORD wType, char *format, ...);
263
c7ca9142
VK
264BOOL InitLocks(DWORD *pdwIpAddr, char *pszInfo);
265BOOL LockComponent(DWORD dwId, DWORD dwLockBy, char *pszOwnerInfo);
266void UnlockComponent(DWORD dwId);
267
4385fa12
VK
268BOOL ParseCommandLine(int argc, char *argv[]);
269BOOL LoadConfig(void);
9a19737f
VK
270
271void Shutdown(void);
272BOOL Initialize(void);
273void Main(void);
274
d627ae40
VK
275BOOL SleepAndCheckForShutdown(int iSeconds);
276
4385fa12
VK
277void StrStrip(char *str);
278
d627ae40
VK
279void SaveObjects(void);
280
3a2f672c
VK
281HMODULE DLOpen(char *szModule);
282void *DLGetSymbolAddr(HMODULE hModule, char *szSymbol);
283void DLClose(HMODULE hModule);
284
285BOOL DBInit(void);
286DB_HANDLE DBConnect(void);
287void DBDisconnect(DB_HANDLE hConn);
288BOOL DBQuery(DB_HANDLE hConn, char *szQuery);
289DB_RESULT DBSelect(DB_HANDLE hConn, char *szQuery);
20177e8e
VK
290DB_ASYNC_RESULT DBAsyncSelect(DB_HANDLE hConn, char *szQuery);
291BOOL DBFetch(DB_ASYNC_RESULT hResult);
3a2f672c 292char *DBGetField(DB_RESULT hResult, int iRow, int iColumn);
a68e217b
VK
293long DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
294DWORD DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
20177e8e
VK
295char *DBGetFieldAsync(DB_ASYNC_RESULT hResult, int iColumn, char *pBuffer, int iBufSize);
296long DBGetFieldAsyncLong(DB_RESULT hResult, int iColumn);
297DWORD DBGetFieldAsyncULong(DB_ASYNC_RESULT hResult, int iColumn);
3a2f672c
VK
298int DBGetNumRows(DB_RESULT hResult);
299void DBFreeResult(DB_RESULT hResult);
20177e8e 300void DBFreeAsyncResult(DB_ASYNC_RESULT hResult);
cc140cce 301void DBUnloadDriver(void);
3a2f672c 302
067f2689 303void QueueSQLRequest(char *szQuery);
83f0529c 304void StopDBWriter(void);
067f2689 305
0ee0c807
VK
306BOOL IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout);
307
be0a5a53 308void SnmpInit(void);
469b937c 309BOOL SnmpGet(DWORD dwAddr, char *szCommunity, char *szOidStr, oid *oidBinary,
1275c750 310 size_t iOidLen, void *pValue, DWORD dwBufferSize, BOOL bVerbose, BOOL bStringResult);
469b937c 311BOOL SnmpEnumerate(DWORD dwAddr, char *szCommunity, char *szRootOid,
4ea67c99
VK
312 void (* pHandler)(DWORD, char *, variable_list *, void *),
313 void *pUserArg, BOOL bVerbose);
d7d76e50 314
ce19c304 315INTERFACE_LIST *GetLocalInterfaceList(void);
a0495b6e 316INTERFACE_LIST *SnmpGetInterfaceList(DWORD dwAddr, char *szCommunity);
48b1c0ac 317void CleanInterfaceList(INTERFACE_LIST *pIfList);
a0495b6e
VK
318void DestroyInterfaceList(INTERFACE_LIST *pIfList);
319
ce19c304
VK
320ARP_CACHE *GetLocalArpCache(void);
321ARP_CACHE *SnmpGetArpCache(DWORD dwAddr, char *szCommunity);
322void DestroyArpCache(ARP_CACHE *pArpCache);
323
f91fa4c2
VK
324void WatchdogInit(void);
325DWORD WatchdogAddThread(char *szName);
326void WatchdogNotify(DWORD dwId);
327void WatchdogPrintStatus(void);
328
3e4e127f
VK
329void CheckForMgmtNode(void);
330
62f5857f
VK
331void EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
332
0fdc2761
VK
333void BinToStr(BYTE *pData, DWORD dwSize, char *pStr);
334DWORD StrToBin(char *pStr, BYTE *pData, DWORD dwSize);
e77547c2 335
c7ca9142
VK
336void GetSysInfoStr(char *pszBuffer);
337DWORD GetLocalIpAddr(void);
338
9a19737f
VK
339#ifdef _WIN32
340
341void InitService(void);
342void InstallService(char *execName);
343void RemoveService(void);
344void StartCoreService(void);
345void StopCoreService(void);
346void InstallEventSource(char *path);
347void RemoveEventSource(void);
348
349char *GetSystemErrorText(DWORD error);
350
351#endif /* _WIN32 */
352
be0a5a53 353void DbgTestMutex(MUTEX hMutex, char *szName);
7fdc8a45 354void DbgPrintf(DWORD dwFlags, char *szFormat, ...);
be0a5a53 355
9a19737f
VK
356
357//
358// Global variables
359//
360
4385fa12
VK
361extern DWORD g_dwFlags;
362extern char g_szConfigFile[];
363extern char g_szLogFile[];
3a2f672c
VK
364
365extern char g_szDbDriver[];
4385fa12 366extern char g_szDbDriver[];
3a2f672c
VK
367extern char g_szDbDrvParams[];
368extern char g_szDbServer[];
369extern char g_szDbLogin[];
370extern char g_szDbPassword[];
371extern char g_szDbName[];
372
373extern DB_HANDLE g_hCoreDB;
067f2689 374extern Queue *g_pLazyRequestQueue;
9a19737f
VK
375
376#endif /* _nms_core_h_ */