fixed Windows build errors
[public/netxms.git] / include / nxcpapi.h
1 /*
2 ** NetXMS - Network Management System
3 ** NXCP API
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: nxcpapi.h
21 **
22 **/
23
24 #ifndef _nxcpapi_h_
25 #define _nxcpapi_h_
26
27 #include <nms_util.h>
28 #include <nms_threads.h>
29 #include <uuid.h>
30
31 #ifdef _WIN32
32 #include <wincrypt.h>
33 #endif
34
35 /**
36 * Temporary buffer structure for RecvNXCPMessage() function
37 */
38 typedef struct
39 {
40 UINT32 bufferSize;
41 UINT32 bufferPos;
42 char buffer[NXCP_TEMP_BUF_SIZE];
43 } NXCP_BUFFER;
44
45
46 #ifdef __cplusplus
47
48 struct MessageField;
49
50 /**
51 * Parsed NXCP message
52 */
53 class LIBNETXMS_EXPORTABLE NXCPMessage
54 {
55 private:
56 UINT16 m_code;
57 UINT16 m_flags;
58 UINT32 m_id;
59 MessageField *m_fields; // Message fields
60 int m_version; // Protocol version
61 BYTE *m_data; // binary data
62 size_t m_dataSize; // binary data size
63
64 void *set(UINT32 fieldId, BYTE type, const void *value, bool isSigned = false, size_t size = 0);
65 void *get(UINT32 fieldId, BYTE requiredType, BYTE *fieldType = NULL) const;
66 NXCP_MESSAGE_FIELD *find(UINT32 fieldId) const;
67
68 public:
69 NXCPMessage(int version = NXCP_VERSION);
70 NXCPMessage(NXCPMessage *msg);
71 NXCPMessage(NXCP_MESSAGE *rawMag, int version = NXCP_VERSION);
72 ~NXCPMessage();
73
74 NXCP_MESSAGE *createMessage() const;
75
76 UINT16 getCode() const { return m_code; }
77 void setCode(UINT16 code) { m_code = code; }
78
79 UINT32 getId() const { return m_id; }
80 void setId(UINT32 id) { m_id = id; }
81
82 bool isEndOfFile() const { return (m_flags & MF_END_OF_FILE) ? true : false; }
83 bool isEndOfSequence() const { return (m_flags & MF_END_OF_SEQUENCE) ? true : false; }
84 bool isReverseOrder() const { return (m_flags & MF_REVERSE_ORDER) ? true : false; }
85 bool isBinary() const { return (m_flags & MF_BINARY) ? true : false; }
86 bool isControl() const { return (m_flags & MF_CONTROL) ? true : false; }
87 bool isCompressed() const { return (m_flags & MF_COMPRESSED) ? true : false; }
88
89 const BYTE *getBinaryData() const { return m_data; }
90 size_t getBinaryDataSize() const { return m_dataSize; }
91
92 bool isFieldExist(UINT32 fieldId) const { return find(fieldId) != NULL; }
93 int getFieldType(UINT32 fieldId) const;
94
95 void setField(UINT32 fieldId, INT16 value) { set(fieldId, NXCP_DT_INT16, &value, true); }
96 void setField(UINT32 fieldId, UINT16 value) { set(fieldId, NXCP_DT_INT16, &value, false); }
97 void setField(UINT32 fieldId, INT32 value) { set(fieldId, NXCP_DT_INT32, &value, true); }
98 void setField(UINT32 fieldId, UINT32 value) { set(fieldId, NXCP_DT_INT32, &value, false); }
99 void setField(UINT32 fieldId, INT64 value) { set(fieldId, NXCP_DT_INT64, &value, true); }
100 void setField(UINT32 fieldId, UINT64 value) { set(fieldId, NXCP_DT_INT64, &value, false); }
101 void setField(UINT32 fieldId, double value) { set(fieldId, NXCP_DT_FLOAT, &value); }
102 void setField(UINT32 fieldId, bool value) { INT16 v = value ? 1 : 0; set(fieldId, NXCP_DT_INT16, &v, true); }
103 void setField(UINT32 fieldId, const TCHAR *value) { if (value != NULL) set(fieldId, NXCP_DT_STRING, value); }
104 void setField(UINT32 fieldId, const TCHAR *value, size_t maxLen) { if (value != NULL) set(fieldId, NXCP_DT_STRING, value, false, maxLen); }
105 void setField(UINT32 fieldId, const BYTE *value, size_t size) { set(fieldId, NXCP_DT_BINARY, value, false, size); }
106 void setField(UINT32 fieldId, const InetAddress& value) { set(fieldId, NXCP_DT_INETADDR, &value); }
107 void setField(UINT32 fieldId, const uuid& value) { set(fieldId, NXCP_DT_BINARY, value.getValue(), false, UUID_LENGTH); }
108 #ifdef UNICODE
109 void setFieldFromMBString(UINT32 fieldId, const char *value);
110 #else
111 void setFieldFromMBString(UINT32 fieldId, const char *value) { set(fieldId, NXCP_DT_STRING, value); }
112 #endif
113 void setFieldFromTime(UINT32 fieldId, time_t value) { UINT64 t = (UINT64)value; set(fieldId, NXCP_DT_INT64, &t); }
114 void setFieldFromInt32Array(UINT32 fieldId, size_t numElements, const UINT32 *elements);
115 void setFieldFromInt32Array(UINT32 fieldId, const IntegerArray<UINT32> *data);
116 bool setFieldFromFile(UINT32 fieldId, const TCHAR *pszFileName);
117
118 INT16 getFieldAsInt16(UINT32 fieldId) const;
119 UINT16 getFieldAsUInt16(UINT32 fieldId) const;
120 INT32 getFieldAsInt32(UINT32 fieldId) const;
121 UINT32 getFieldAsUInt32(UINT32 fieldId) const;
122 INT64 getFieldAsInt64(UINT32 fieldId) const;
123 UINT64 getFieldAsUInt64(UINT32 fieldId) const;
124 double getFieldAsDouble(UINT32 fieldId) const;
125 bool getFieldAsBoolean(UINT32 fieldId) const;
126 time_t getFieldAsTime(UINT32 fieldId) const;
127 UINT32 getFieldAsInt32Array(UINT32 fieldId, UINT32 numElements, UINT32 *buffer) const;
128 UINT32 getFieldAsInt32Array(UINT32 fieldId, IntegerArray<UINT32> *data) const;
129 const BYTE *getBinaryFieldPtr(UINT32 fieldId, size_t *size) const;
130 TCHAR *getFieldAsString(UINT32 fieldId, TCHAR *buffer = NULL, size_t bufferSize = 0) const;
131 char *getFieldAsMBString(UINT32 fieldId, char *buffer = NULL, size_t bufferSize = 0) const;
132 char *getFieldAsUtf8String(UINT32 fieldId, char *buffer = NULL, size_t bufferSize = 0) const;
133 UINT32 getFieldAsBinary(UINT32 fieldId, BYTE *buffer, size_t bufferSize) const;
134 InetAddress getFieldAsInetAddress(UINT32 fieldId) const;
135 uuid getFieldAsGUID(UINT32 fieldId) const;
136
137 void deleteAllFields();
138
139 void disableEncryption() { m_flags |= MF_DONT_ENCRYPT; }
140 void setEndOfSequence() { m_flags |= MF_END_OF_SEQUENCE; }
141 void setReverseOrderFlag() { m_flags |= MF_REVERSE_ORDER; }
142
143 static String dump(const NXCP_MESSAGE *msg, int version);
144 };
145
146 /**
147 * Message waiting queue element structure
148 */
149 typedef struct
150 {
151 void *msg; // Pointer to message, either to NXCPMessage object or raw message
152 UINT64 sequence; // Sequence number
153 UINT32 id; // Message ID
154 UINT32 ttl; // Message time-to-live in milliseconds
155 UINT16 code; // Message code
156 UINT16 isBinary; // 1 for binary (raw) messages
157 } WAIT_QUEUE_ELEMENT;
158
159 /**
160 * Max number of waiting threads in message queue
161 */
162 #define MAX_MSGQUEUE_WAITERS 32
163
164 /**
165 * Message waiting queue class
166 */
167 class LIBNETXMS_EXPORTABLE MsgWaitQueue
168 {
169 private:
170 #if defined(_WIN32)
171 CRITICAL_SECTION m_mutex;
172 HANDLE m_wakeupEvents[MAX_MSGQUEUE_WAITERS];
173 BYTE m_waiters[MAX_MSGQUEUE_WAITERS];
174 #elif defined(_USE_GNU_PTH)
175 pth_mutex_t m_mutex;
176 pth_cond_t m_wakeupCondition;
177 #else
178 pthread_mutex_t m_mutex;
179 pthread_cond_t m_wakeupCondition;
180 #endif
181 UINT32 m_holdTime;
182 int m_size;
183 int m_allocated;
184 WAIT_QUEUE_ELEMENT *m_elements;
185 UINT64 m_sequence;
186
187 void *waitForMessageInternal(UINT16 isBinary, UINT16 code, UINT32 id, UINT32 timeout);
188
189 void lock()
190 {
191 #ifdef _WIN32
192 EnterCriticalSection(&m_mutex);
193 #elif defined(_USE_GNU_PTH)
194 pth_mutex_acquire(&m_mutex, FALSE, NULL);
195 #else
196 pthread_mutex_lock(&m_mutex);
197 #endif
198 }
199
200 void unlock()
201 {
202 #ifdef _WIN32
203 LeaveCriticalSection(&m_mutex);
204 #elif defined(_USE_GNU_PTH)
205 pth_mutex_release(&m_mutex);
206 #else
207 pthread_mutex_unlock(&m_mutex);
208 #endif
209 }
210
211 void housekeeperRun();
212
213 static Mutex m_housekeeperLock;
214 static HashMap<UINT64, MsgWaitQueue> *m_activeQueues;
215 static Condition m_shutdownCondition;
216 static THREAD m_housekeeperThread;
217 static EnumerationCallbackResult houseKeeperCallback(const void *key, const void *object, void *arg);
218 static THREAD_RESULT THREAD_CALL housekeeperThread(void *);
219 static EnumerationCallbackResult diagInfoCallback(const void *key, const void *object, void *arg);
220
221 public:
222 MsgWaitQueue();
223 ~MsgWaitQueue();
224
225 void put(NXCPMessage *pMsg);
226 void put(NXCP_MESSAGE *pMsg);
227 NXCPMessage *waitForMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
228 {
229 return (NXCPMessage *)waitForMessageInternal(0, wCode, dwId, dwTimeOut);
230 }
231 NXCP_MESSAGE *waitForRawMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
232 {
233 return (NXCP_MESSAGE *)waitForMessageInternal(1, wCode, dwId, dwTimeOut);
234 }
235
236 void clear();
237 void setHoldTime(UINT32 holdTime) { m_holdTime = holdTime; }
238
239 static void shutdown();
240 static String getDiagInfo();
241 };
242
243 /**
244 * NXCP encryption context
245 */
246 class LIBNETXMS_EXPORTABLE NXCPEncryptionContext : public RefCountObject
247 {
248 private:
249 int m_cipher;
250 BYTE *m_sessionKey;
251 int m_keyLength;
252 BYTE m_iv[EVP_MAX_IV_LENGTH];
253 #ifdef _WITH_ENCRYPTION
254 EVP_CIPHER_CTX m_encryptor;
255 EVP_CIPHER_CTX m_decryptor;
256 MUTEX m_encryptorLock;
257 #endif
258
259 NXCPEncryptionContext();
260 bool initCipher(int cipher);
261
262 public:
263 static NXCPEncryptionContext *create(NXCPMessage *msg, RSA *privateKey);
264 static NXCPEncryptionContext *create(UINT32 ciphers);
265
266 virtual ~NXCPEncryptionContext();
267
268 NXCP_ENCRYPTED_MESSAGE *encryptMessage(NXCP_MESSAGE *msg);
269 bool decryptMessage(NXCP_ENCRYPTED_MESSAGE *msg, BYTE *decryptionBuffer);
270
271 int getCipher() { return m_cipher; }
272 BYTE *getSessionKey() { return m_sessionKey; }
273 int getKeyLength() { return m_keyLength; }
274 BYTE *getIV() { return m_iv; }
275 };
276
277 /**
278 * Message receiver result codes
279 */
280 enum MessageReceiverResult
281 {
282 MSGRECV_SUCCESS = 0,
283 MSGRECV_CLOSED = 1,
284 MSGRECV_TIMEOUT = 2,
285 MSGRECV_COMM_FAILURE = 3,
286 MSGRECV_DECRYPTION_FAILURE = 4,
287 MSGRECV_PROTOCOL_ERROR = 5
288 };
289
290 /**
291 * Message receiver - abstract base class
292 */
293 class LIBNETXMS_EXPORTABLE AbstractMessageReceiver
294 {
295 private:
296 BYTE *m_buffer;
297 BYTE *m_decryptionBuffer;
298 NXCPEncryptionContext *m_encryptionContext;
299 size_t m_initialSize;
300 size_t m_size;
301 size_t m_maxSize;
302 size_t m_dataSize;
303 size_t m_bytesToSkip;
304
305 NXCPMessage *getMessageFromBuffer(bool *protocolError);
306
307 protected:
308 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout) = 0;
309
310 public:
311 AbstractMessageReceiver(size_t initialSize, size_t maxSize);
312 virtual ~AbstractMessageReceiver();
313
314 void setEncryptionContext(NXCPEncryptionContext *ctx) { m_encryptionContext = ctx; }
315
316 NXCPMessage *readMessage(UINT32 timeout, MessageReceiverResult *result);
317 NXCP_MESSAGE *getRawMessageBuffer() { return (NXCP_MESSAGE *)m_buffer; }
318
319 static const TCHAR *resultToText(MessageReceiverResult result);
320 };
321
322 /**
323 * Message receiver - socket implementation
324 */
325 class LIBNETXMS_EXPORTABLE SocketMessageReceiver : public AbstractMessageReceiver
326 {
327 private:
328 SOCKET m_socket;
329
330 protected:
331 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout);
332
333 public:
334 SocketMessageReceiver(SOCKET socket, size_t initialSize, size_t maxSize);
335 virtual ~SocketMessageReceiver();
336 };
337
338 /**
339 * Message receiver - UNIX socket/named pipe implementation
340 */
341 class LIBNETXMS_EXPORTABLE PipeMessageReceiver : public AbstractMessageReceiver
342 {
343 private:
344 HPIPE m_pipe;
345 #ifdef _WIN32
346 HANDLE m_readEvent;
347 #endif
348
349 protected:
350 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout);
351
352 public:
353 PipeMessageReceiver(HPIPE pipe, size_t initialSize, size_t maxSize);
354 virtual ~PipeMessageReceiver();
355 };
356
357 /**
358 * NXCP compression methods
359 */
360 enum NXCPCompressionMethod
361 {
362 NXCP_COMPRESSION_NONE = 0,
363 NXCP_COMPRESSION_LZ4 = 1
364 };
365
366 /**
367 * Abstract stream compressor
368 */
369 class LIBNETXMS_EXPORTABLE StreamCompressor
370 {
371 public:
372 virtual ~StreamCompressor();
373
374 virtual size_t compress(const BYTE *in, size_t inSize, BYTE *out, size_t maxOutSize) = 0;
375 virtual size_t decompress(const BYTE *in, size_t inSize, const BYTE **out) = 0;
376 virtual size_t compressBufferSize(size_t dataSize) = 0;
377
378 static StreamCompressor *create(NXCPCompressionMethod method, bool compress, size_t maxBlockSize);
379 };
380
381 /**
382 * Dummy stream compressor
383 */
384 class LIBNETXMS_EXPORTABLE DummyStreamCompressor : public StreamCompressor
385 {
386 public:
387 virtual ~DummyStreamCompressor();
388
389 virtual size_t compress(const BYTE *in, size_t inSize, BYTE *out, size_t maxOutSize);
390 virtual size_t decompress(const BYTE *in, size_t inSize, const BYTE **out);
391 virtual size_t compressBufferSize(size_t dataSize);
392 };
393
394 struct __LZ4_stream_t;
395 struct __LZ4_streamDecode_t;
396
397 /**
398 * LZ4 stream compressor
399 */
400 class LIBNETXMS_EXPORTABLE LZ4StreamCompressor : public StreamCompressor
401 {
402 private:
403 union
404 {
405 __LZ4_stream_t *encoder;
406 __LZ4_streamDecode_t *decoder;
407 } m_stream;
408 char *m_buffer;
409 size_t m_maxBlockSize;
410 size_t m_bufferSize;
411 size_t m_bufferPos;
412 bool m_compress;
413
414 public:
415 LZ4StreamCompressor(bool compress, size_t maxBlockSize);
416 virtual ~LZ4StreamCompressor();
417
418 virtual size_t compress(const BYTE *in, size_t inSize, BYTE *out, size_t maxOutSize);
419 virtual size_t decompress(const BYTE *in, size_t inSize, const BYTE **out);
420 virtual size_t compressBufferSize(size_t dataSize);
421 };
422
423 #if 0
424 /**
425 * NXCP message consumer interface
426 */
427 class LIBNETXMS_EXPORTABLE MessageConsumer
428 {
429 public:
430 virtual SOCKET getSocket() = 0;
431 virtual void processMessage(NXCPMessage *msg) = 0;
432 };
433
434 /**
435 * Socket receiver - manages receiving NXCP messages from multiple sockets
436 */
437 class LIBNETXMS_EXPORTABLE SocketReceiver
438 {
439 private:
440 THREAD m_thread;
441 HashMap<SOCKET, MessageConsumer> *m_consumers;
442
443 static int m_maxSocketsPerThread;
444 static ObjectArray<SocketReceiver> *m_receivers;
445
446 public:
447 static void start();
448 static void shutdown();
449
450 static void addConsumer(MessageConsumer *mc);
451 static void removeConsumer(MessageConsumer *mc);
452
453 static String getDiagInfo();
454 };
455 #endif
456
457 #else /* __cplusplus */
458
459 typedef void NXCPMessage;
460 typedef void NXCPEncryptionContext;
461
462 #endif
463
464 typedef bool (*NXCPMessageNameResolver)(UINT16 code, TCHAR *buffer);
465
466
467 //
468 // Functions
469 //
470
471 #ifdef __cplusplus
472
473 int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, NXCP_MESSAGE *pMsg,
474 NXCP_BUFFER *pBuffer, UINT32 dwMaxMsgSize,
475 NXCPEncryptionContext **ppCtx,
476 BYTE *pDecryptionBuffer, UINT32 dwTimeout);
477 int LIBNETXMS_EXPORTABLE RecvNXCPMessageEx(SOCKET hSocket, NXCP_MESSAGE **msgBuffer,
478 NXCP_BUFFER *nxcpBuffer, UINT32 *bufferSize,
479 NXCPEncryptionContext **ppCtx,
480 BYTE **decryptionBuffer, UINT32 dwTimeout,
481 UINT32 maxMsgSize);
482 NXCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, UINT32 dwId, WORD flags,
483 UINT32 dwDataSize, void *pData,
484 NXCP_MESSAGE *pBuffer);
485 BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, UINT32 dwId, const TCHAR *pszFile,
486 NXCPEncryptionContext *pCtx, long offset,
487 void (* progressCallback)(INT64, void *), void *cbArg,
488 MUTEX mutex, NXCPCompressionMethod compressionMethod = NXCP_COMPRESSION_NONE);
489 BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion, MUTEX mutex);
490
491 TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(UINT16 wCode, TCHAR *buffer);
492 void LIBNETXMS_EXPORTABLE NXCPRegisterMessageNameResolver(NXCPMessageNameResolver r);
493 void LIBNETXMS_EXPORTABLE NXCPUnregisterMessageNameResolver(NXCPMessageNameResolver r);
494
495 bool LIBNETXMS_EXPORTABLE InitCryptoLib(UINT32 dwEnabledCiphers);
496 UINT32 LIBNETXMS_EXPORTABLE NXCPGetSupportedCiphers();
497 String LIBNETXMS_EXPORTABLE NXCPGetSupportedCiphersAsText();
498 NXCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE *NXCPEncryptMessage(NXCPEncryptionContext *pCtx, NXCP_MESSAGE *pMsg);
499 bool LIBNETXMS_EXPORTABLE NXCPDecryptMessage(NXCPEncryptionContext *pCtx,
500 NXCP_ENCRYPTED_MESSAGE *pMsg,
501 BYTE *pDecryptionBuffer);
502 UINT32 LIBNETXMS_EXPORTABLE SetupEncryptionContext(NXCPMessage *pMsg,
503 NXCPEncryptionContext **ppCtx,
504 NXCPMessage **ppResponse,
505 RSA *pPrivateKey, int nNXCPVersion);
506 void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(NXCPMessage *pMsg, RSA *pServerKey, bool useX509Format);
507 RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile);
508
509 #ifdef _WIN32
510 BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, UINT32 dwMsgLen, const CERT_CONTEXT *pCert,
511 BYTE *pBuffer, size_t bufferSize, UINT32 *pdwSigLen);
512 #endif
513
514 #endif
515
516 #endif /* _nxcpapi_h_ */