Fix access control for monitors (Bug #689)
[public/netxms.git] / include / nxcpapi.h
CommitLineData
9fa031cd 1/*
0d9c1b96 2** NetXMS - Network Management System
9f2ad16c 3** NXCP API
68f384ea 4** Copyright (C) 2003-2010 Victor Kirhenshtein
0d9c1b96
VK
5**
6** This program is free software; you can redistribute it and/or modify
65d2c384
VK
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
0d9c1b96
VK
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**
68f384ea 16** You should have received a copy of the GNU Lesser General Public License
0d9c1b96
VK
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
9f2ad16c 20** File: nxcpapi.h
0d9c1b96
VK
21**
22**/
23
9f2ad16c
VK
24#ifndef _nxcpapi_h_
25#define _nxcpapi_h_
0d9c1b96 26
b50f1100 27#include <nms_threads.h>
9f2ad16c 28#include <nms_util.h>
8c290af0 29
bb8136f7
VK
30#ifdef _WIN32
31#include <wincrypt.h>
32#endif
33
4af351c7 34/**
b368969c 35 * Temporary buffer structure for RecvNXCPMessage() function
4af351c7 36 */
0d9c1b96
VK
37typedef struct
38{
b368969c
VK
39 UINT32 bufferSize;
40 UINT32 bufferPos;
41 char buffer[CSCP_TEMP_BUF_SIZE];
42} NXCP_BUFFER;
0d9c1b96
VK
43
44
9f2ad16c
VK
45#ifdef __cplusplus
46
5c44534b
VK
47struct MessageField;
48
4af351c7
VK
49/**
50 * Parsed NXCP message
51 */
b368969c 52class LIBNETXMS_EXPORTABLE NXCPMessage
0d9c1b96
VK
53{
54private:
b368969c
VK
55 UINT16 m_code;
56 UINT16 m_flags;
5c44534b 57 UINT32 m_id;
6be0a20b
VK
58 MessageField *m_fields; // Message fields
59 int m_version; // Protocol version
60 BYTE *m_data; // binary data
61 size_t m_dataSize; // binary data size
0d9c1b96 62
b368969c 63 void *set(UINT32 fieldId, BYTE type, const void *value, size_t size = 0);
8f238fd7 64 void *get(UINT32 fieldId, BYTE requiredType, BYTE *fieldType = NULL);
b368969c 65 NXCP_MESSAGE_FIELD *find(UINT32 fieldId);
0d9c1b96
VK
66
67public:
b368969c
VK
68 NXCPMessage(int version = NXCP_VERSION);
69 NXCPMessage(NXCPMessage *msg);
70 NXCPMessage(NXCP_MESSAGE *rawMag, int version = NXCP_VERSION);
71 ~NXCPMessage();
0d9c1b96 72
b368969c 73 NXCP_MESSAGE *createMessage();
0d9c1b96 74
b368969c
VK
75 UINT16 getCode() { return m_code; }
76 void setCode(UINT16 code) { m_code = code; }
0d9c1b96 77
b368969c
VK
78 UINT32 getId() { return m_id; }
79 void setId(UINT32 id) { m_id = id; }
0d9c1b96 80
6be0a20b 81 bool isEndOfFile() { return (m_flags & MF_END_OF_FILE) ? true : false; }
5c44534b
VK
82 bool isEndOfSequence() { return (m_flags & MF_END_OF_SEQUENCE) ? true : false; }
83 bool isReverseOrder() { return (m_flags & MF_REVERSE_ORDER) ? true : false; }
6be0a20b
VK
84 bool isBinary() { return (m_flags & MF_BINARY) ? true : false; }
85
86 BYTE *getBinaryData() { return m_data; }
87 size_t getBinaryDataSize() { return m_dataSize; }
4af351c7 88
4101571e
VK
89 bool isFieldExist(UINT32 fieldId) { return find(fieldId) != NULL; }
90 int getFieldType(UINT32 fieldId);
91
b368969c
VK
92 void setField(UINT32 fieldId, INT16 value) { set(fieldId, NXCP_DT_INT16, &value); }
93 void setField(UINT32 fieldId, UINT16 value) { set(fieldId, NXCP_DT_INT16, &value); }
94 void setField(UINT32 fieldId, INT32 value) { set(fieldId, NXCP_DT_INT32, &value); }
95 void setField(UINT32 fieldId, UINT32 value) { set(fieldId, NXCP_DT_INT32, &value); }
96 void setField(UINT32 fieldId, INT64 value) { set(fieldId, NXCP_DT_INT64, &value); }
97 void setField(UINT32 fieldId, UINT64 value) { set(fieldId, NXCP_DT_INT64, &value); }
98 void setField(UINT32 fieldId, double value) { set(fieldId, NXCP_DT_FLOAT, &value); }
99 void setField(UINT32 fieldId, const TCHAR *value) { if (value != NULL) set(fieldId, NXCP_DT_STRING, value); }
100 void setField(UINT32 fieldId, const TCHAR *value, UINT32 maxLen) { if (value != NULL) set(fieldId, NXCP_DT_STRING, value, maxLen); }
101 void setField(UINT32 fieldId, BYTE *value, size_t size) { set(fieldId, NXCP_DT_BINARY, value, size); }
35f836fe 102#ifdef UNICODE
b368969c 103 void setFieldFromMBString(UINT32 fieldId, const char *value);
35f836fe 104#else
b368969c 105 void setFieldFromMBString(UINT32 fieldId, const char *value) { set(fieldId, NXCP_DT_STRING, value); }
35f836fe 106#endif
b368969c
VK
107 void setFieldFromTime(UINT32 fieldId, time_t value) { UINT64 t = (UINT64)value; set(fieldId, NXCP_DT_INT64, &t); }
108 void setFieldFromInt32Array(UINT32 fieldId, UINT32 dwNumElements, const UINT32 *pdwData);
109 void setFieldFromInt32Array(UINT32 fieldId, IntegerArray<UINT32> *data);
8f238fd7 110 bool setFieldFromFile(UINT32 fieldId, const TCHAR *pszFileName);
967893bb 111
4101571e 112 INT16 getFieldAsInt16(UINT32 fieldId);
b368969c 113 UINT16 getFieldAsUInt16(UINT32 fieldId);
4101571e 114 INT32 getFieldAsInt32(UINT32 fieldId);
b368969c 115 UINT32 getFieldAsUInt32(UINT32 fieldId);
4101571e 116 INT64 getFieldAsInt64(UINT32 fieldId);
b368969c 117 UINT64 getFieldAsUInt64(UINT32 fieldId);
4101571e 118 double getFieldAsDouble(UINT32 fieldId);
d6bbfa4e 119 bool getFieldAsBoolean(UINT32 fieldId);
8f238fd7 120 time_t getFieldAsTime(UINT32 fieldId);
9a68ca24
VK
121 UINT32 getFieldAsInt32Array(UINT32 fieldId, UINT32 numElements, UINT32 *buffer);
122 UINT32 getFieldAsInt32Array(UINT32 fieldId, IntegerArray<UINT32> *data);
ac14e3e6 123 BYTE *getBinaryFieldPtr(UINT32 fieldId, size_t *size);
b368969c
VK
124 TCHAR *getFieldAsString(UINT32 fieldId, TCHAR *buffer = NULL, size_t bufferSize = 0);
125 char *getFieldAsMBString(UINT32 fieldId, char *buffer = NULL, size_t bufferSize = 0);
126 char *getFieldAsUtf8String(UINT32 fieldId, char *buffer = NULL, size_t bufferSize = 0);
127 UINT32 getFieldAsBinary(UINT32 fieldId, BYTE *buffer, size_t bufferSize);
4101571e 128
b368969c 129 void deleteAllFields();
e44ac467 130
5c44534b
VK
131 void disableEncryption() { m_flags |= MF_DONT_ENCRYPT; }
132 void setEndOfSequence() { m_flags |= MF_END_OF_SEQUENCE; }
133 void setReverseOrderFlag() { m_flags |= MF_REVERSE_ORDER; }
934f53da 134
b368969c 135 static String dump(NXCP_MESSAGE *msg, int version);
0d9c1b96
VK
136};
137
4af351c7
VK
138/**
139 * Message waiting queue element structure
140 */
deaa4491
VK
141typedef struct
142{
b368969c 143 void *msg; // Pointer to message, either to NXCPMessage object or raw message
f128c07c
VK
144 UINT32 id; // Message ID
145 UINT32 ttl; // Message time-to-live in milliseconds
146 UINT16 code; // Message code
147 UINT16 isBinary; // 1 for binary (raw) messages
deaa4491
VK
148} WAIT_QUEUE_ELEMENT;
149
4af351c7 150/**
f128c07c
VK
151 * Max number of waiting threads in message queue
152 */
153#define MAX_MSGQUEUE_WAITERS 32
154
155/**
4af351c7
VK
156 * Message waiting queue class
157 */
9f2ad16c 158class LIBNETXMS_EXPORTABLE MsgWaitQueue
deaa4491 159{
deaa4491 160private:
f128c07c
VK
161#ifdef _WIN32
162 CRITICAL_SECTION m_mutex;
163 HANDLE m_wakeupEvents[MAX_MSGQUEUE_WAITERS];
164 BYTE m_waiters[MAX_MSGQUEUE_WAITERS];
165#else
166 pthread_mutex_t m_mutex;
167 pthread_cond_t m_wakeupCondition;
168#endif
169 CONDITION m_stopCondition;
170 UINT32 m_holdTime;
171 int m_size;
172 int m_allocated;
173 WAIT_QUEUE_ELEMENT *m_elements;
ccdbbb52 174 THREAD m_hHkThread;
deaa4491 175
c17f6cbc 176 void housekeeperThread();
f128c07c
VK
177 void *waitForMessageInternal(UINT16 isBinary, UINT16 code, UINT32 id, UINT32 timeout);
178
179 void lock()
180 {
181#ifdef _WIN32
182 EnterCriticalSection(&m_mutex);
183#else
184 pthread_mutex_lock(&m_mutex);
185#endif
186 }
187
188 void unlock()
189 {
190#ifdef _WIN32
191 LeaveCriticalSection(&m_mutex);
192#else
193 pthread_mutex_unlock(&m_mutex);
194#endif
195 }
cc022855 196
c17f6cbc 197 static THREAD_RESULT THREAD_CALL mwqThreadStarter(void *);
deaa4491
VK
198
199public:
200 MsgWaitQueue();
201 ~MsgWaitQueue();
202
b368969c
VK
203 void put(NXCPMessage *pMsg);
204 void put(NXCP_MESSAGE *pMsg);
205 NXCPMessage *waitForMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 206 {
b368969c 207 return (NXCPMessage *)waitForMessageInternal(0, wCode, dwId, dwTimeOut);
deaa4491 208 }
b368969c 209 NXCP_MESSAGE *waitForRawMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 210 {
b368969c 211 return (NXCP_MESSAGE *)waitForMessageInternal(1, wCode, dwId, dwTimeOut);
deaa4491 212 }
cc022855 213
c17f6cbc 214 void clear();
f128c07c 215 void setHoldTime(UINT32 holdTime) { m_holdTime = holdTime; }
deaa4491
VK
216};
217
b549a0f8
VK
218/**
219 * NXCP encryption context
220 */
98abc9f1
VK
221class LIBNETXMS_EXPORTABLE NXCPEncryptionContext : public RefCountObject
222{
223private:
224 int m_cipher;
225 BYTE *m_sessionKey;
226 int m_keyLength;
227 BYTE m_iv[EVP_MAX_IV_LENGTH];
e4e091f0 228#ifdef _WITH_ENCRYPTION
e6336a90
VK
229 EVP_CIPHER_CTX m_encryptor;
230 EVP_CIPHER_CTX m_decryptor;
db05c2af 231 MUTEX m_encryptorLock;
e4e091f0 232#endif
98abc9f1
VK
233
234 NXCPEncryptionContext();
e6336a90 235 bool initCipher(int cipher);
98abc9f1
VK
236
237public:
b368969c 238 static NXCPEncryptionContext *create(NXCPMessage *msg, RSA *privateKey);
967893bb 239 static NXCPEncryptionContext *create(UINT32 ciphers);
98abc9f1
VK
240
241 virtual ~NXCPEncryptionContext();
242
b368969c 243 NXCP_ENCRYPTED_MESSAGE *encryptMessage(NXCP_MESSAGE *msg);
6be0a20b 244 bool decryptMessage(NXCP_ENCRYPTED_MESSAGE *msg, BYTE *decryptionBuffer);
e6336a90 245
98abc9f1
VK
246 int getCipher() { return m_cipher; }
247 BYTE *getSessionKey() { return m_sessionKey; }
248 int getKeyLength() { return m_keyLength; }
249 BYTE *getIV() { return m_iv; }
250};
251
6be0a20b
VK
252/**
253 * Message receiver result codes
254 */
255enum MessageReceiverResult
256{
257 MSGRECV_SUCCESS = 0,
258 MSGRECV_CLOSED = 1,
259 MSGRECV_TIMEOUT = 2,
260 MSGRECV_COMM_FAILURE = 3,
261 MSGRECV_DECRYPTION_FAILURE = 4
262};
263
264/**
265 * Message receiver - abstract base class
266 */
267class LIBNETXMS_EXPORTABLE AbstractMessageReceiver
268{
269private:
270 BYTE *m_buffer;
271 BYTE *m_decryptionBuffer;
272 NXCPEncryptionContext *m_encryptionContext;
273 size_t m_initialSize;
274 size_t m_size;
275 size_t m_maxSize;
276 size_t m_dataSize;
277 size_t m_bytesToSkip;
278
b368969c 279 NXCPMessage *getMessageFromBuffer();
6be0a20b
VK
280
281protected:
282 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout) = 0;
283
284public:
285 AbstractMessageReceiver(size_t initialSize, size_t maxSize);
286 virtual ~AbstractMessageReceiver();
287
288 void setEncryptionContext(NXCPEncryptionContext *ctx) { m_encryptionContext = ctx; }
289
b368969c
VK
290 NXCPMessage *readMessage(UINT32 timeout, MessageReceiverResult *result);
291 NXCP_MESSAGE *getRawMessageBuffer() { return (NXCP_MESSAGE *)m_buffer; }
292
293 static const TCHAR *resultToText(MessageReceiverResult result);
6be0a20b
VK
294};
295
296/**
297 * Message receiver - socket implementation
298 */
299class LIBNETXMS_EXPORTABLE SocketMessageReceiver : public AbstractMessageReceiver
300{
301private:
302 SOCKET m_socket;
303
304protected:
305 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout);
306
307public:
308 SocketMessageReceiver(SOCKET socket, size_t initialSize, size_t maxSize);
309 virtual ~SocketMessageReceiver();
310};
311
312/**
313 * Message receiver - UNIX socket/named pipe implementation
314 */
315class LIBNETXMS_EXPORTABLE PipeMessageReceiver : public AbstractMessageReceiver
316{
317private:
318 HPIPE m_pipe;
319#ifdef _WIN32
320 HANDLE m_readEvent;
321#endif
322
323protected:
324 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout);
325
326public:
327 PipeMessageReceiver(HPIPE pipe, size_t initialSize, size_t maxSize);
328 virtual ~PipeMessageReceiver();
329};
330
9f2ad16c
VK
331#else /* __cplusplus */
332
b368969c 333typedef void NXCPMessage;
98abc9f1 334typedef void NXCPEncryptionContext;
9f2ad16c
VK
335
336#endif
337
deaa4491
VK
338
339//
0d9c1b96
VK
340// Functions
341//
342
343#ifdef __cplusplus
0d9c1b96 344
b368969c
VK
345int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, NXCP_MESSAGE *pMsg,
346 NXCP_BUFFER *pBuffer, UINT32 dwMaxMsgSize,
98abc9f1 347 NXCPEncryptionContext **ppCtx,
967893bb 348 BYTE *pDecryptionBuffer, UINT32 dwTimeout);
b368969c
VK
349int LIBNETXMS_EXPORTABLE RecvNXCPMessageEx(SOCKET hSocket, NXCP_MESSAGE **msgBuffer,
350 NXCP_BUFFER *nxcpBuffer, UINT32 *bufferSize,
cc022855 351 NXCPEncryptionContext **ppCtx,
967893bb
VK
352 BYTE **decryptionBuffer, UINT32 dwTimeout,
353 UINT32 maxMsgSize);
b368969c 354NXCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, UINT32 dwId, WORD flags,
967893bb 355 UINT32 dwDataSize, void *pData,
b368969c
VK
356 NXCP_MESSAGE *pBuffer);
357TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *buffer);
967893bb 358BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, UINT32 dwId, const TCHAR *pszFile,
cc022855 359 NXCPEncryptionContext *pCtx, long offset,
7b8b337e
VK
360 void (* progressCallback)(INT64, void *), void *cbArg,
361 MUTEX mutex);
362BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion, MUTEX mutex);
cc022855 363
6468147c 364BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(UINT32 dwEnabledCiphers, void (*debugCallback)(int, const TCHAR *, va_list args));
967893bb 365UINT32 LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers();
b368969c
VK
366NXCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE *NXCPEncryptMessage(NXCPEncryptionContext *pCtx, NXCP_MESSAGE *pMsg);
367BOOL LIBNETXMS_EXPORTABLE NXCPDecryptMessage(NXCPEncryptionContext *pCtx,
6be0a20b 368 NXCP_ENCRYPTED_MESSAGE *pMsg,
e44ac467 369 BYTE *pDecryptionBuffer);
b368969c 370UINT32 LIBNETXMS_EXPORTABLE SetupEncryptionContext(NXCPMessage *pMsg,
98abc9f1 371 NXCPEncryptionContext **ppCtx,
b368969c 372 NXCPMessage **ppResponse,
4bd3d6c5 373 RSA *pPrivateKey, int nNXCPVersion);
b368969c 374void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(NXCPMessage *pMsg, RSA *pServerKey, bool useX509Format);
3aa0061c 375RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile);
15cf5acc 376
bb8136f7 377#ifdef _WIN32
967893bb 378BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, UINT32 dwMsgLen, const CERT_CONTEXT *pCert,
b368969c 379 BYTE *pBuffer, size_t bufferSize, UINT32 *pdwSigLen);
bb8136f7
VK
380#endif
381
0d9c1b96
VK
382#endif
383
9f2ad16c 384#endif /* _nxcpapi_h_ */