license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / libnxcl / libnxcl.h
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2003-2010 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: libnxcl.h
21 **
22 **/
23
24 #ifndef _libnxcl_h_
25 #define _libnxcl_h_
26
27 #include <nms_common.h>
28 #include <nxclapi.h>
29 #include <nms_util.h>
30 #include <nms_threads.h>
31 #include <nxcpapi.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdarg.h>
35
36
37 //
38 // Constants
39 //
40
41 #define OBJECT_CACHE_MAGIC 0x20090921
42
43 #define MAX_SERVER_NAME 64
44 #define MAX_LOGIN_NAME 64
45 #define MAX_PASSWORD_LEN 64
46 #define MAX_LOCKINFO_LEN 256
47 #define MAX_TZ_LEN 32
48
49
50 //
51 // Session flags
52 //
53
54 #define NXC_SF_USERDB_LOADED 0x0001
55 #define NXC_SF_HAS_OBJECT_CACHE 0x0002
56 #define NXC_SF_CHANGE_PASSWD 0x0004
57 #define NXC_SF_CONN_BROKEN 0x0008
58 #define NXC_SF_BAD_DBCONN 0x0010
59
60
61 //
62 // Sync operations
63 //
64
65 #define SYNC_EVENTS 0
66 #define SYNC_OBJECTS 1
67 #define SYNC_SYSLOG 2
68 #define SYNC_TRAP_LOG 3
69 #define SYNC_EVENT_DB 4
70 #define SYNC_USER_DB 5
71 #define SYNC_DCI_LIST 6
72
73 #define SYNC_OP_COUNT 7
74
75
76 //
77 // Index structure
78 //
79
80 typedef struct
81 {
82 DWORD dwKey;
83 NXC_OBJECT *pObject;
84 } INDEX;
85
86
87 //
88 // Object cache file header structure
89 //
90
91 typedef struct
92 {
93 DWORD dwMagic;
94 DWORD dwStructSize; // sizeof(NXC_OBJECT)
95 DWORD dwTimeStamp;
96 DWORD dwNumObjects;
97 BYTE bsServerId[8];
98 } OBJECT_CACHE_HEADER;
99
100
101 //
102 // Session class
103 //
104
105 class NXCL_Session
106 {
107 friend THREAD_RESULT THREAD_CALL NetReceiver(NXCL_Session *pSession);
108
109 // Attributes
110 private:
111 DWORD m_dwFlags;
112 DWORD m_dwMsgId;
113 DWORD m_dwState;
114 DWORD m_dwTimeStamp; // Last known server's timestamp
115 DWORD m_dwNumObjects;
116 INDEX *m_pIndexById;
117 MUTEX m_mutexIndexAccess;
118 SOCKET m_hSocket;
119 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
120 MsgWaitQueue m_msgWaitQueue;
121 DWORD m_dwReceiverBufferSize;
122 NXC_DCI_LIST *m_pItemList;
123 THREAD m_hRecvThread;
124 THREAD m_hWatchdogThread;
125 CONDITION m_condStopThreads;
126 TCHAR m_szLastLock[MAX_LOCKINFO_LEN];
127 void *m_pClientData; // Client-defined data
128 TCHAR m_szServerTimeZone[MAX_TZ_LEN];
129 MUTEX m_mutexSendMsg;
130
131 DWORD m_dwUserId; // Id of logged-in user
132 DWORD m_dwSystemAccess; // System access rights for current user
133
134 int m_hCurrFile;
135 DWORD m_dwFileRqId;
136 DWORD m_dwFileRqCompletion;
137 CONDITION m_condFileRq;
138 MUTEX m_mutexFileRq;
139
140 NXC_EVENT_TEMPLATE **m_ppEventTemplates;
141 DWORD m_dwNumTemplates;
142 MUTEX m_mutexEventAccess;
143
144 DWORD m_dwNumUsers;
145 NXC_USER *m_pUserList;
146
147 MUTEX m_mutexSyncOpAccess[SYNC_OP_COUNT];
148 DWORD m_dwSyncExitCode[SYNC_OP_COUNT];
149 #ifdef _WIN32
150 HANDLE m_condSyncOp[SYNC_OP_COUNT];
151 #else
152 pthread_cond_t m_condSyncOp[SYNC_OP_COUNT];
153 pthread_mutex_t m_mutexSyncOp[SYNC_OP_COUNT];
154 BOOL m_bSyncFinished[SYNC_OP_COUNT];
155 #endif
156
157 public:
158 DWORD m_dwCommandTimeout;
159 NXC_EVENT_HANDLER m_pEventHandler;
160 BYTE m_bsServerId[8];
161
162 // Methods
163 private:
164 void destroyAllObjects();
165 void processDCI(CSCPMessage *pMsg);
166 void destroyEventDB();
167 void destroyUserDB();
168 void processUserDBRecord(CSCPMessage *pMsg);
169 void processUserDBUpdate(CSCPMessage *pMsg);
170
171 void processObjectUpdate(CSCPMessage *pMsg);
172 void addObject(NXC_OBJECT *pObject, BOOL bSortIndex);
173 void loadObjectsFromCache(const TCHAR *pszCacheFile);
174
175 void watchdogThread();
176 static THREAD_RESULT THREAD_CALL watchdogThreadStarter(void *pArg);
177
178 public:
179 NXCL_Session();
180 ~NXCL_Session();
181
182 void attach(SOCKET hSocket) { m_hSocket = hSocket; }
183 void disconnect();
184
185 void SetRecvThread(THREAD hThread) { m_hRecvThread = hThread; }
186 void StartWatchdogThread(void);
187
188 BOOL SendMsg(CSCPMessage *pMsg);
189 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut = 0);
190 CSCP_MESSAGE *WaitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut = 0);
191 DWORD WaitForRCC(DWORD dwRqId, DWORD dwTimeOut = 0);
192 DWORD CreateRqId(void) { return m_dwMsgId++; }
193 DWORD SendFile(DWORD dwRqId, TCHAR *pszFileName);
194 DWORD SimpleCommand(WORD wCmd);
195
196 void callEventHandler(DWORD dwEvent, DWORD dwCode, void *pArg);
197
198 DWORD WaitForSync(int nSyncOp, DWORD dwTimeOut);
199 void PrepareForSync(int nSyncOp);
200 void CompleteSync(int nSyncOp, DWORD dwRetCode);
201 void UnlockSyncOp(int nSyncOp) { MutexUnlock(m_mutexSyncOpAccess[nSyncOp]); }
202
203 DWORD OpenNodeDCIList(DWORD dwNodeId, NXC_DCI_LIST **ppItemList);
204
205 DWORD LoadEventDB(void);
206 void AddEventTemplate(NXC_EVENT_TEMPLATE *pEventTemplate, BOOL bLock);
207 void DeleteEDBRecord(DWORD dwEventCode);
208 BOOL GetEventDB(NXC_EVENT_TEMPLATE ***pppTemplateList, DWORD *pdwNumRecords);
209 const TCHAR *GetEventName(DWORD dwId);
210 BOOL GetEventNameEx(DWORD dwId, TCHAR *pszBuffer, DWORD dwBufSize);
211 int GetEventSeverity(DWORD dwId);
212 BOOL GetEventText(DWORD dwId, TCHAR *pszBuffer, DWORD dwBufSize);
213
214 DWORD LoadUserDB(void);
215 BOOL GetUserDB(NXC_USER **ppUserList, DWORD *pdwNumUsers);
216 NXC_USER *FindUserById(DWORD dwId);
217
218 DWORD syncObjects(const TCHAR *pszCacheFile, BOOL bSyncComments);
219 void lockObjectIndex() { MutexLock(m_mutexIndexAccess, INFINITE); }
220 void unlockObjectIndex() { MutexUnlock(m_mutexIndexAccess); }
221 NXC_OBJECT *findObjectById(DWORD id, BOOL lock);
222 NXC_OBJECT *findObjectByName(const TCHAR *name, DWORD currObject);
223 NXC_OBJECT *findObjectByComments(const TCHAR *comments, DWORD currObject);
224 NXC_OBJECT *findObjectByIPAddress(DWORD ipAddr, DWORD currObject);
225 void EnumerateObjects(BOOL (* pHandler)(NXC_OBJECT *));
226 NXC_OBJECT *GetRootObject(DWORD dwId, DWORD dwIndex);
227 void *GetObjectIndex(DWORD *pdwNumObjects);
228
229 void SetTimeStamp(DWORD dwTimeStamp) { m_dwTimeStamp = dwTimeStamp; }
230 DWORD GetTimeStamp(void) { return m_dwTimeStamp; }
231
232 DWORD SetSubscriptionStatus(DWORD dwChannels, int nOperation);
233
234 DWORD PrepareFileTransfer(const TCHAR *pszFile, DWORD dwRqId);
235 DWORD WaitForFileTransfer(DWORD dwTimeout);
236 void AbortFileTransfer(void);
237
238 void SetClientData(void *pData) { m_pClientData = pData; }
239 void *GetClientData(void) { return m_pClientData; }
240
241 void OnNotify(CSCPMessage *pMsg);
242 void ParseLoginMessage(CSCPMessage *pMsg);
243 DWORD GetCurrentUserId(void) { return m_dwUserId; }
244 DWORD GetCurrentSystemAccess(void) { return m_dwSystemAccess; }
245 BOOL NeedPasswordChange(void) { return (m_dwFlags & NXC_SF_CHANGE_PASSWD) ? TRUE : FALSE; }
246 BOOL IsDBConnLost(void) { return (m_dwFlags & NXC_SF_BAD_DBCONN) ? TRUE : FALSE; }
247
248 void setLastLock(const TCHAR *pszLock) { nx_strncpy(m_szLastLock, pszLock, MAX_LOCKINFO_LEN); }
249 const TCHAR *getLastLock() { return m_szLastLock; }
250
251 TCHAR *getServerTimeZone() { return m_szServerTimeZone; }
252 };
253
254 inline void NXCL_Session::callEventHandler(DWORD dwEvent, DWORD dwCode, void *pArg)
255 {
256 if (m_pEventHandler != NULL)
257 m_pEventHandler(this, dwEvent, dwCode, pArg);
258 }
259
260 #define CHECK_SESSION_HANDLE() { if (hSession == NULL) return RCC_INVALID_SESSION_HANDLE; }
261
262
263 //
264 // Functions
265 //
266
267 void DestroyObject(NXC_OBJECT *pObject);
268 void UpdateUserFromMessage(CSCPMessage *pMsg, NXC_USER *pUser);
269
270 void ProcessAlarmUpdate(NXCL_Session *pSession, CSCPMessage *pMsg);
271 void ProcessEventDBUpdate(NXCL_Session *pSession, CSCPMessage *pMsg);
272 void ProcessEventLogRecords(NXCL_Session *pSession, CSCPMessage *pMsg);
273 void ProcessSyslogRecords(NXCL_Session *pSession, CSCPMessage *pMsg);
274 void ProcessTrapLogRecords(NXCL_Session *pSession, CSCPMessage *pMsg);
275 void ProcessTrapCfgUpdate(NXCL_Session *pSession, CSCPMessage *pMsg);
276 void ProcessActionUpdate(NXCL_Session *pSession, CSCPMessage *pMsg);
277 void ProcessEventDBRecord(NXCL_Session *pSession, CSCPMessage *pMsg);
278 void ProcessUserDBUpdate(CSCPMessage *pMsg);
279 void ProcessSituationChange(NXCL_Session *pSession, CSCPMessage *pMsg);
280 void ProcessDCI(NXCL_Session *pSession, CSCPMessage *pMsg);
281
282 void DebugPrintf(const TCHAR *format, ...);
283
284
285 //
286 // Global variables
287 //
288
289 extern NXC_DEBUG_CALLBACK g_pDebugCallBack;
290
291
292 #endif /* _libnxcl_h_ */