Advanced status calculation fully implemented
[public/netxms.git] / include / nxcscpapi.h
1 /*
2 ** NetXMS - Network Management System
3 ** CSCP API Library
4 ** Copyright (C) 2003, 2004 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 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 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 ** $module: nxcscpapi.h
21 **
22 **/
23
24 #ifndef _nxcscpapi_h_
25 #define _nxcscpapi_h_
26
27 #include <nms_threads.h>
28
29
30 #ifdef _WIN32
31 #ifdef LIBNXCSCP_EXPORTS
32 #define LIBNXCSCP_EXPORTABLE __declspec(dllexport)
33 #else
34 #define LIBNXCSCP_EXPORTABLE __declspec(dllimport)
35 #endif
36 #else /* _WIN32 */
37 #define LIBNXCSCP_EXPORTABLE
38 #endif
39
40
41 //
42 // Encryption methods
43 //
44
45 #define CSCP_ENCRYPTION_NONE 0
46 #define CSCP_ENCRYPTION_BLOWFISH_SHA1 1
47
48
49 //
50 // Temporary buffer structure for RecvCSCPMessage() function
51 //
52
53 typedef struct
54 {
55 DWORD dwBufSize;
56 DWORD dwBufPos;
57 char szBuffer[CSCP_TEMP_BUF_SIZE];
58 } CSCP_BUFFER;
59
60
61 //
62 // Class for holding CSCP messages
63 //
64
65 class LIBNXCSCP_EXPORTABLE CSCPMessage
66 {
67 private:
68 WORD m_wCode;
69 WORD m_wFlags;
70 DWORD m_dwId;
71 DWORD m_dwNumVar; // Number of variables
72 CSCP_DF **m_ppVarList; // List of variables
73
74 void *Set(DWORD dwVarId, BYTE bType, void *pValue, DWORD dwSize = 0);
75 void *Get(DWORD dwVarId, BYTE bType);
76 DWORD FindVariable(DWORD dwVarId);
77
78 public:
79 CSCPMessage();
80 CSCPMessage(CSCP_MESSAGE *pMsg);
81 ~CSCPMessage();
82
83 CSCP_MESSAGE *CreateMessage(void);
84
85 WORD GetCode(void) { return m_wCode; }
86 void SetCode(WORD wCode) { m_wCode = wCode; }
87
88 DWORD GetId(void) { return m_dwId; }
89 void SetId(DWORD dwId) { m_dwId = dwId; }
90
91 BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
92 BOOL IsEndOfSequence(void) { return (m_wFlags & MF_END_OF_SEQUENCE) ? TRUE : FALSE; }
93
94 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, CSCP_DT_INT16, &wValue); }
95 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
96 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, CSCP_DT_INT64, &qwValue); }
97 void SetVariable(DWORD dwVarId, double dValue) { Set(dwVarId, CSCP_DT_FLOAT, &dValue); }
98 void SetVariable(DWORD dwVarId, TCHAR *pszValue) { Set(dwVarId, CSCP_DT_STRING, pszValue); }
99 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
100 void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwData);
101 BOOL SetVariableFromFile(DWORD dwVarId, TCHAR *pszFileName);
102
103 DWORD GetVariableLong(DWORD dwVarId);
104 QWORD GetVariableInt64(DWORD dwVarId);
105 WORD GetVariableShort(DWORD dwVarId);
106 LONG GetVariableShortAsInt32(DWORD dwVarId);
107 double GetVariableDouble(DWORD dwVarId);
108 TCHAR *GetVariableStr(DWORD dwVarId, TCHAR *szBuffer = NULL, DWORD dwBufSize = 0);
109 DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
110 DWORD GetVariableInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwBuffer);
111
112 void DeleteAllVariables(void);
113
114 void DisableEncryption(void) { m_wFlags |= MF_DONT_ENCRYPT; }
115 void SetEndOfSequence(void) { m_wFlags |= MF_END_OF_SEQUENCE; }
116 };
117
118
119 //
120 // Message waiting queue element structure
121 //
122
123 typedef struct
124 {
125 WORD wCode; // Message code
126 WORD wIsBinary; // 1 for binary (raw) messages
127 DWORD dwId; // Message ID
128 DWORD dwTTL; // Message time-to-live in milliseconds
129 void *pMsg; // Pointer to message, either to CSCPMessage object or raw message
130 } WAIT_QUEUE_ELEMENT;
131
132
133 //
134 // Message waiting queue class
135 //
136
137 class LIBNXCSCP_EXPORTABLE MsgWaitQueue
138 {
139 private:
140 MUTEX m_mutexDataAccess;
141 CONDITION m_condStop;
142 CONDITION m_condNewMsg;
143 DWORD m_dwMsgHoldTime;
144 DWORD m_dwNumElements;
145 WAIT_QUEUE_ELEMENT *m_pElements;
146 THREAD m_hHkThread;
147
148 void Lock(void) { MutexLock(m_mutexDataAccess, INFINITE); }
149 void Unlock(void) { MutexUnlock(m_mutexDataAccess); }
150 void HousekeeperThread(void);
151 void *WaitForMessageInternal(WORD wIsBinary, WORD wCode, DWORD dwId, DWORD dwTimeOut);
152
153 static THREAD_RESULT THREAD_CALL MWQThreadStarter(void *);
154
155 public:
156 MsgWaitQueue();
157 ~MsgWaitQueue();
158
159 void Put(CSCPMessage *pMsg);
160 void Put(CSCP_MESSAGE *pMsg);
161 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
162 {
163 return (CSCPMessage *)WaitForMessageInternal(0, wCode, dwId, dwTimeOut);
164 }
165 CSCP_MESSAGE *WaitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
166 {
167 return (CSCP_MESSAGE *)WaitForMessageInternal(1, wCode, dwId, dwTimeOut);
168 }
169
170 void Clear(void);
171 void SetHoldTime(DWORD dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
172 };
173
174
175 //
176 // Functions
177 //
178
179 #ifdef __cplusplus
180 extern "C" {
181 #endif
182
183 int LIBNXCSCP_EXPORTABLE RecvCSCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
184 CSCP_BUFFER *pBuffer, DWORD dwMaxMsgSize,
185 CSCP_ENCRYPTION_CONTEXT **ppCtx,
186 BYTE *pDecryptionBuffer);
187 CSCP_MESSAGE LIBNXCSCP_EXPORTABLE *CreateRawCSCPMessage(WORD wCode, DWORD dwId,
188 DWORD dwDataSize, void *pData,
189 CSCP_MESSAGE *pBuffer);
190 TCHAR LIBNXCSCP_EXPORTABLE *CSCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
191 BOOL LIBNXCSCP_EXPORTABLE SendFileOverCSCP(SOCKET hSocket, DWORD dwId, TCHAR *pszFile,
192 CSCP_ENCRYPTION_CONTEXT *pCtx);
193
194 BOOL LIBNXCSCP_EXPORTABLE InitCryptoLib(DWORD dwEnabledCiphers);
195 DWORD LIBNXCSCP_EXPORTABLE CSCPGetSupportedCiphers(void);
196 CSCP_ENCRYPTED_MESSAGE LIBNXCSCP_EXPORTABLE
197 *CSCPEncryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx, CSCP_MESSAGE *pMsg);
198 BOOL LIBNXCSCP_EXPORTABLE CSCPDecryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx,
199 CSCP_ENCRYPTED_MESSAGE *pMsg,
200 BYTE *pDecryptionBuffer);
201 DWORD LIBNXCSCP_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
202 CSCP_ENCRYPTION_CONTEXT **ppCtx,
203 CSCPMessage **ppResponse,
204 RSA *pPrivateKey);
205 void LIBNXCSCP_EXPORTABLE DestroyEncryptionContext(CSCP_ENCRYPTION_CONTEXT *pCtx);
206 void LIBNXCSCP_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey);
207 RSA LIBNXCSCP_EXPORTABLE *LoadRSAKeys(TCHAR *pszKeyFile);
208
209 #ifdef __cplusplus
210 }
211 #endif
212
213 #endif /* _nxcscpapi_h_ */