Fixed session agent build error
[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
9f2ad16c 27#include <nms_util.h>
bf6fb6c3 28#include <nms_threads.h>
9c3bf7f4 29#include <nxcrypto.h>
de4af576 30#include <uuid.h>
8c290af0 31
bb8136f7
VK
32#ifdef _WIN32
33#include <wincrypt.h>
34#endif
35
bf306af3
VK
36#ifdef _WITH_ENCRYPTION
37#include <openssl/ssl.h>
38#endif
39
4af351c7 40/**
b368969c 41 * Temporary buffer structure for RecvNXCPMessage() function
4af351c7 42 */
0d9c1b96
VK
43typedef struct
44{
b368969c
VK
45 UINT32 bufferSize;
46 UINT32 bufferPos;
43a6f3ca 47 char buffer[NXCP_TEMP_BUF_SIZE];
b368969c 48} NXCP_BUFFER;
0d9c1b96
VK
49
50
9f2ad16c
VK
51#ifdef __cplusplus
52
5c44534b
VK
53struct MessageField;
54
4af351c7
VK
55/**
56 * Parsed NXCP message
57 */
b368969c 58class LIBNETXMS_EXPORTABLE NXCPMessage
0d9c1b96
VK
59{
60private:
b368969c
VK
61 UINT16 m_code;
62 UINT16 m_flags;
5c44534b 63 UINT32 m_id;
6be0a20b
VK
64 MessageField *m_fields; // Message fields
65 int m_version; // Protocol version
66 BYTE *m_data; // binary data
67 size_t m_dataSize; // binary data size
0d9c1b96 68
198745dd
VK
69 NXCPMessage(const NXCP_MESSAGE *msg, int version);
70
712b2760 71 void *set(UINT32 fieldId, BYTE type, const void *value, bool isSigned = false, size_t size = 0);
ca0a165b
VK
72 void *get(UINT32 fieldId, BYTE requiredType, BYTE *fieldType = NULL) const;
73 NXCP_MESSAGE_FIELD *find(UINT32 fieldId) const;
198745dd 74 bool isValid() { return m_version != -1; }
0d9c1b96
VK
75
76public:
b368969c 77 NXCPMessage(int version = NXCP_VERSION);
3b89a4c1 78 NXCPMessage(UINT16 code, UINT32 id, int version = NXCP_VERSION);
b368969c 79 NXCPMessage(NXCPMessage *msg);
b368969c 80 ~NXCPMessage();
0d9c1b96 81
198745dd
VK
82 static NXCPMessage *deserialize(const NXCP_MESSAGE *rawMag, int version = NXCP_VERSION);
83 NXCP_MESSAGE *serialize(bool allowCompression = false) const;
0d9c1b96 84
ca0a165b 85 UINT16 getCode() const { return m_code; }
b368969c 86 void setCode(UINT16 code) { m_code = code; }
0d9c1b96 87
ca0a165b 88 UINT32 getId() const { return m_id; }
b368969c 89 void setId(UINT32 id) { m_id = id; }
0d9c1b96 90
4970ef34
VK
91 int getProtocolVersion() const { return m_version; }
92 void setProtocolVersion(int version) { m_version = version; }
93
ca0a165b
VK
94 bool isEndOfFile() const { return (m_flags & MF_END_OF_FILE) ? true : false; }
95 bool isEndOfSequence() const { return (m_flags & MF_END_OF_SEQUENCE) ? true : false; }
96 bool isReverseOrder() const { return (m_flags & MF_REVERSE_ORDER) ? true : false; }
97 bool isBinary() const { return (m_flags & MF_BINARY) ? true : false; }
98 bool isControl() const { return (m_flags & MF_CONTROL) ? true : false; }
3a1356ab 99 bool isCompressedStream() const { return ((m_flags & (MF_COMPRESSED | MF_STREAM)) == (MF_COMPRESSED | MF_STREAM)) ? true : false; }
6be0a20b 100
ca0a165b
VK
101 const BYTE *getBinaryData() const { return m_data; }
102 size_t getBinaryDataSize() const { return m_dataSize; }
4af351c7 103
ca0a165b
VK
104 bool isFieldExist(UINT32 fieldId) const { return find(fieldId) != NULL; }
105 int getFieldType(UINT32 fieldId) const;
4101571e 106
712b2760
VK
107 void setField(UINT32 fieldId, INT16 value) { set(fieldId, NXCP_DT_INT16, &value, true); }
108 void setField(UINT32 fieldId, UINT16 value) { set(fieldId, NXCP_DT_INT16, &value, false); }
109 void setField(UINT32 fieldId, INT32 value) { set(fieldId, NXCP_DT_INT32, &value, true); }
110 void setField(UINT32 fieldId, UINT32 value) { set(fieldId, NXCP_DT_INT32, &value, false); }
111 void setField(UINT32 fieldId, INT64 value) { set(fieldId, NXCP_DT_INT64, &value, true); }
112 void setField(UINT32 fieldId, UINT64 value) { set(fieldId, NXCP_DT_INT64, &value, false); }
b368969c 113 void setField(UINT32 fieldId, double value) { set(fieldId, NXCP_DT_FLOAT, &value); }
19a2cd39 114 void setField(UINT32 fieldId, bool value) { INT16 v = value ? 1 : 0; set(fieldId, NXCP_DT_INT16, &v, true); }
b368969c 115 void setField(UINT32 fieldId, const TCHAR *value) { if (value != NULL) set(fieldId, NXCP_DT_STRING, value); }
712b2760 116 void setField(UINT32 fieldId, const TCHAR *value, size_t maxLen) { if (value != NULL) set(fieldId, NXCP_DT_STRING, value, false, maxLen); }
5f573844 117 void setField(UINT32 fieldId, const BYTE *value, size_t size) { set(fieldId, NXCP_DT_BINARY, value, false, size); }
de4af576
VK
118 void setField(UINT32 fieldId, const InetAddress& value) { set(fieldId, NXCP_DT_INETADDR, &value); }
119 void setField(UINT32 fieldId, const uuid& value) { set(fieldId, NXCP_DT_BINARY, value.getValue(), false, UUID_LENGTH); }
ce9e00cc 120 void setField(UINT32 fieldId, const MacAddress& value) { set(fieldId, NXCP_DT_BINARY, value.value(), false, value.length()); }
35f836fe 121#ifdef UNICODE
b368969c 122 void setFieldFromMBString(UINT32 fieldId, const char *value);
35f836fe 123#else
b368969c 124 void setFieldFromMBString(UINT32 fieldId, const char *value) { set(fieldId, NXCP_DT_STRING, value); }
35f836fe 125#endif
b368969c 126 void setFieldFromTime(UINT32 fieldId, time_t value) { UINT64 t = (UINT64)value; set(fieldId, NXCP_DT_INT64, &t); }
14149881 127 void setFieldFromInt32Array(UINT32 fieldId, size_t numElements, const UINT32 *elements);
e30c2442 128 void setFieldFromInt32Array(UINT32 fieldId, const IntegerArray<UINT32> *data);
8f238fd7 129 bool setFieldFromFile(UINT32 fieldId, const TCHAR *pszFileName);
967893bb 130
ca0a165b
VK
131 INT16 getFieldAsInt16(UINT32 fieldId) const;
132 UINT16 getFieldAsUInt16(UINT32 fieldId) const;
133 INT32 getFieldAsInt32(UINT32 fieldId) const;
134 UINT32 getFieldAsUInt32(UINT32 fieldId) const;
135 INT64 getFieldAsInt64(UINT32 fieldId) const;
136 UINT64 getFieldAsUInt64(UINT32 fieldId) const;
137 double getFieldAsDouble(UINT32 fieldId) const;
138 bool getFieldAsBoolean(UINT32 fieldId) const;
139 time_t getFieldAsTime(UINT32 fieldId) const;
140 UINT32 getFieldAsInt32Array(UINT32 fieldId, UINT32 numElements, UINT32 *buffer) const;
141 UINT32 getFieldAsInt32Array(UINT32 fieldId, IntegerArray<UINT32> *data) const;
142 const BYTE *getBinaryFieldPtr(UINT32 fieldId, size_t *size) const;
143 TCHAR *getFieldAsString(UINT32 fieldId, TCHAR *buffer = NULL, size_t bufferSize = 0) const;
144 char *getFieldAsMBString(UINT32 fieldId, char *buffer = NULL, size_t bufferSize = 0) const;
145 char *getFieldAsUtf8String(UINT32 fieldId, char *buffer = NULL, size_t bufferSize = 0) const;
78032263 146 size_t getFieldAsBinary(UINT32 fieldId, BYTE *buffer, size_t bufferSize) const;
ca0a165b 147 InetAddress getFieldAsInetAddress(UINT32 fieldId) const;
ce9e00cc 148 MacAddress getFieldAsMacAddress(UINT32 fieldId) const;
ca0a165b 149 uuid getFieldAsGUID(UINT32 fieldId) const;
4101571e 150
b368969c 151 void deleteAllFields();
e44ac467 152
5c44534b
VK
153 void disableEncryption() { m_flags |= MF_DONT_ENCRYPT; }
154 void setEndOfSequence() { m_flags |= MF_END_OF_SEQUENCE; }
155 void setReverseOrderFlag() { m_flags |= MF_REVERSE_ORDER; }
934f53da 156
ca0a165b 157 static String dump(const NXCP_MESSAGE *msg, int version);
0d9c1b96
VK
158};
159
4af351c7
VK
160/**
161 * Message waiting queue element structure
162 */
deaa4491
VK
163typedef struct
164{
b368969c 165 void *msg; // Pointer to message, either to NXCPMessage object or raw message
14149881 166 UINT64 sequence; // Sequence number
f128c07c
VK
167 UINT32 id; // Message ID
168 UINT32 ttl; // Message time-to-live in milliseconds
169 UINT16 code; // Message code
170 UINT16 isBinary; // 1 for binary (raw) messages
deaa4491
VK
171} WAIT_QUEUE_ELEMENT;
172
4af351c7 173/**
f128c07c
VK
174 * Max number of waiting threads in message queue
175 */
176#define MAX_MSGQUEUE_WAITERS 32
177
178/**
4af351c7
VK
179 * Message waiting queue class
180 */
9f2ad16c 181class LIBNETXMS_EXPORTABLE MsgWaitQueue
deaa4491 182{
deaa4491 183private:
ef8a3c32 184#if defined(_WIN32)
f128c07c
VK
185 CRITICAL_SECTION m_mutex;
186 HANDLE m_wakeupEvents[MAX_MSGQUEUE_WAITERS];
187 BYTE m_waiters[MAX_MSGQUEUE_WAITERS];
ef8a3c32
VK
188#elif defined(_USE_GNU_PTH)
189 pth_mutex_t m_mutex;
190 pth_cond_t m_wakeupCondition;
f128c07c
VK
191#else
192 pthread_mutex_t m_mutex;
193 pthread_cond_t m_wakeupCondition;
194#endif
f128c07c
VK
195 UINT32 m_holdTime;
196 int m_size;
197 int m_allocated;
198 WAIT_QUEUE_ELEMENT *m_elements;
14149881 199 UINT64 m_sequence;
deaa4491 200
f128c07c
VK
201 void *waitForMessageInternal(UINT16 isBinary, UINT16 code, UINT32 id, UINT32 timeout);
202
203 void lock()
204 {
205#ifdef _WIN32
206 EnterCriticalSection(&m_mutex);
ef8a3c32
VK
207#elif defined(_USE_GNU_PTH)
208 pth_mutex_acquire(&m_mutex, FALSE, NULL);
f128c07c
VK
209#else
210 pthread_mutex_lock(&m_mutex);
211#endif
212 }
213
214 void unlock()
215 {
216#ifdef _WIN32
217 LeaveCriticalSection(&m_mutex);
ef8a3c32
VK
218#elif defined(_USE_GNU_PTH)
219 pth_mutex_release(&m_mutex);
f128c07c
VK
220#else
221 pthread_mutex_unlock(&m_mutex);
222#endif
223 }
cc022855 224
d87ddcc2
VK
225 void housekeeperRun();
226
c73c3ba9 227 static Mutex m_housekeeperLock;
669fcb92 228 static HashMap<UINT64, MsgWaitQueue> *m_activeQueues;
c73c3ba9 229 static Condition m_shutdownCondition;
d87ddcc2
VK
230 static THREAD m_housekeeperThread;
231 static EnumerationCallbackResult houseKeeperCallback(const void *key, const void *object, void *arg);
232 static THREAD_RESULT THREAD_CALL housekeeperThread(void *);
233 static EnumerationCallbackResult diagInfoCallback(const void *key, const void *object, void *arg);
deaa4491
VK
234
235public:
236 MsgWaitQueue();
237 ~MsgWaitQueue();
238
b368969c
VK
239 void put(NXCPMessage *pMsg);
240 void put(NXCP_MESSAGE *pMsg);
241 NXCPMessage *waitForMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 242 {
b368969c 243 return (NXCPMessage *)waitForMessageInternal(0, wCode, dwId, dwTimeOut);
deaa4491 244 }
b368969c 245 NXCP_MESSAGE *waitForRawMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 246 {
b368969c 247 return (NXCP_MESSAGE *)waitForMessageInternal(1, wCode, dwId, dwTimeOut);
deaa4491 248 }
cc022855 249
c17f6cbc 250 void clear();
f128c07c 251 void setHoldTime(UINT32 holdTime) { m_holdTime = holdTime; }
d87ddcc2
VK
252
253 static void shutdown();
254 static String getDiagInfo();
deaa4491
VK
255};
256
b549a0f8
VK
257/**
258 * NXCP encryption context
259 */
98abc9f1
VK
260class LIBNETXMS_EXPORTABLE NXCPEncryptionContext : public RefCountObject
261{
262private:
263 int m_cipher;
264 BYTE *m_sessionKey;
265 int m_keyLength;
266 BYTE m_iv[EVP_MAX_IV_LENGTH];
e4e091f0 267#ifdef _WITH_ENCRYPTION
9c3bf7f4
VK
268 MUTEX m_encryptorLock;
269#if WITH_OPENSSL
9c144154
VK
270 EVP_CIPHER_CTX *m_encryptor;
271 EVP_CIPHER_CTX *m_decryptor;
9c3bf7f4
VK
272#elif WITH_COMMONCRYPTO
273 CCCryptorRef m_encryptor;
274 CCCryptorRef m_decryptor;
275#endif
e4e091f0 276#endif
98abc9f1
VK
277
278 NXCPEncryptionContext();
e6336a90 279 bool initCipher(int cipher);
98abc9f1
VK
280
281public:
b368969c 282 static NXCPEncryptionContext *create(NXCPMessage *msg, RSA *privateKey);
967893bb 283 static NXCPEncryptionContext *create(UINT32 ciphers);
98abc9f1
VK
284
285 virtual ~NXCPEncryptionContext();
286
b368969c 287 NXCP_ENCRYPTED_MESSAGE *encryptMessage(NXCP_MESSAGE *msg);
6be0a20b 288 bool decryptMessage(NXCP_ENCRYPTED_MESSAGE *msg, BYTE *decryptionBuffer);
e6336a90 289
98abc9f1
VK
290 int getCipher() { return m_cipher; }
291 BYTE *getSessionKey() { return m_sessionKey; }
292 int getKeyLength() { return m_keyLength; }
293 BYTE *getIV() { return m_iv; }
294};
295
6be0a20b
VK
296/**
297 * Message receiver result codes
298 */
299enum MessageReceiverResult
300{
301 MSGRECV_SUCCESS = 0,
302 MSGRECV_CLOSED = 1,
303 MSGRECV_TIMEOUT = 2,
304 MSGRECV_COMM_FAILURE = 3,
1bb62997
VK
305 MSGRECV_DECRYPTION_FAILURE = 4,
306 MSGRECV_PROTOCOL_ERROR = 5
6be0a20b
VK
307};
308
309/**
310 * Message receiver - abstract base class
311 */
312class LIBNETXMS_EXPORTABLE AbstractMessageReceiver
313{
314private:
315 BYTE *m_buffer;
316 BYTE *m_decryptionBuffer;
317 NXCPEncryptionContext *m_encryptionContext;
318 size_t m_initialSize;
319 size_t m_size;
320 size_t m_maxSize;
321 size_t m_dataSize;
322 size_t m_bytesToSkip;
323
1bb62997 324 NXCPMessage *getMessageFromBuffer(bool *protocolError);
6be0a20b
VK
325
326protected:
327 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout) = 0;
328
329public:
330 AbstractMessageReceiver(size_t initialSize, size_t maxSize);
331 virtual ~AbstractMessageReceiver();
332
333 void setEncryptionContext(NXCPEncryptionContext *ctx) { m_encryptionContext = ctx; }
334
b368969c
VK
335 NXCPMessage *readMessage(UINT32 timeout, MessageReceiverResult *result);
336 NXCP_MESSAGE *getRawMessageBuffer() { return (NXCP_MESSAGE *)m_buffer; }
337
338 static const TCHAR *resultToText(MessageReceiverResult result);
6be0a20b
VK
339};
340
341/**
342 * Message receiver - socket implementation
343 */
344class LIBNETXMS_EXPORTABLE SocketMessageReceiver : public AbstractMessageReceiver
345{
346private:
347 SOCKET m_socket;
348
349protected:
350 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout);
351
352public:
353 SocketMessageReceiver(SOCKET socket, size_t initialSize, size_t maxSize);
354 virtual ~SocketMessageReceiver();
355};
356
eae56922
VK
357/**
358 * Message receiver - comm channel implementation
359 */
360class LIBNETXMS_EXPORTABLE CommChannelMessageReceiver : public AbstractMessageReceiver
361{
362private:
363 AbstractCommChannel *m_channel;
364
365protected:
366 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout);
367
368public:
369 CommChannelMessageReceiver(AbstractCommChannel *channel, size_t initialSize, size_t maxSize);
370 virtual ~CommChannelMessageReceiver();
371};
372
bf306af3
VK
373#ifdef _WITH_ENCRYPTION
374
375/**
376 * Message receiver - SSL/TLS implementation
377 */
378class LIBNETXMS_EXPORTABLE TlsMessageReceiver : public AbstractMessageReceiver
379{
380private:
381 SOCKET m_socket;
382 SSL *m_ssl;
cce82c3a 383 MUTEX m_mutex;
bf306af3
VK
384
385protected:
386 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout);
387
388public:
cce82c3a 389 TlsMessageReceiver(SOCKET socket, SSL *ssl, MUTEX mutex, size_t initialSize, size_t maxSize);
bf306af3
VK
390 virtual ~TlsMessageReceiver();
391};
392
393#endif /* _WITH_ENCRYPTION */
394
6be0a20b
VK
395/**
396 * Message receiver - UNIX socket/named pipe implementation
397 */
398class LIBNETXMS_EXPORTABLE PipeMessageReceiver : public AbstractMessageReceiver
399{
400private:
401 HPIPE m_pipe;
402#ifdef _WIN32
403 HANDLE m_readEvent;
404#endif
405
406protected:
407 virtual int readBytes(BYTE *buffer, size_t size, UINT32 timeout);
408
409public:
410 PipeMessageReceiver(HPIPE pipe, size_t initialSize, size_t maxSize);
411 virtual ~PipeMessageReceiver();
412};
413
503da871 414/**
d3a20572 415 * NXCP stresam compression methods
503da871 416 */
d3a20572 417enum NXCPStreamCompressionMethod
503da871 418{
d3a20572 419 NXCP_STREAM_COMPRESSION_NONE = 0,
90ed558d
VK
420 NXCP_STREAM_COMPRESSION_LZ4 = 1,
421 NXCP_STREAM_COMPRESSION_DEFLATE = 2
503da871
VK
422};
423
424/**
425 * Abstract stream compressor
426 */
427class LIBNETXMS_EXPORTABLE StreamCompressor
428{
429public:
9757ca46
VK
430 virtual ~StreamCompressor();
431
503da871
VK
432 virtual size_t compress(const BYTE *in, size_t inSize, BYTE *out, size_t maxOutSize) = 0;
433 virtual size_t decompress(const BYTE *in, size_t inSize, const BYTE **out) = 0;
434 virtual size_t compressBufferSize(size_t dataSize) = 0;
435
d3a20572 436 static StreamCompressor *create(NXCPStreamCompressionMethod method, bool compress, size_t maxBlockSize);
503da871
VK
437};
438
439/**
440 * Dummy stream compressor
441 */
442class LIBNETXMS_EXPORTABLE DummyStreamCompressor : public StreamCompressor
443{
444public:
9757ca46
VK
445 virtual ~DummyStreamCompressor();
446
503da871
VK
447 virtual size_t compress(const BYTE *in, size_t inSize, BYTE *out, size_t maxOutSize);
448 virtual size_t decompress(const BYTE *in, size_t inSize, const BYTE **out);
449 virtual size_t compressBufferSize(size_t dataSize);
450};
451
452struct __LZ4_stream_t;
453struct __LZ4_streamDecode_t;
454
455/**
456 * LZ4 stream compressor
457 */
458class LIBNETXMS_EXPORTABLE LZ4StreamCompressor : public StreamCompressor
459{
460private:
461 union
462 {
463 __LZ4_stream_t *encoder;
464 __LZ4_streamDecode_t *decoder;
465 } m_stream;
466 char *m_buffer;
467 size_t m_maxBlockSize;
468 size_t m_bufferSize;
469 size_t m_bufferPos;
470 bool m_compress;
471
472public:
473 LZ4StreamCompressor(bool compress, size_t maxBlockSize);
474 virtual ~LZ4StreamCompressor();
475
476 virtual size_t compress(const BYTE *in, size_t inSize, BYTE *out, size_t maxOutSize);
477 virtual size_t decompress(const BYTE *in, size_t inSize, const BYTE **out);
478 virtual size_t compressBufferSize(size_t dataSize);
479};
480
90ed558d
VK
481struct z_stream_s;
482
483/**
484 * Deflate stream compressor
485 */
486class LIBNETXMS_EXPORTABLE DeflateStreamCompressor : public StreamCompressor
487{
488private:
489 z_stream_s *m_stream;
490 BYTE *m_buffer;
491 size_t m_bufferSize;
492 bool m_compress;
493
494public:
495 DeflateStreamCompressor(bool compress, size_t maxBlockSize);
496 virtual ~DeflateStreamCompressor();
497
498 virtual size_t compress(const BYTE *in, size_t inSize, BYTE *out, size_t maxOutSize);
499 virtual size_t decompress(const BYTE *in, size_t inSize, const BYTE **out);
500 virtual size_t compressBufferSize(size_t dataSize);
501};
502
208d7427
VK
503#if 0
504/**
505 * NXCP message consumer interface
506 */
507class LIBNETXMS_EXPORTABLE MessageConsumer
508{
509public:
510 virtual SOCKET getSocket() = 0;
511 virtual void processMessage(NXCPMessage *msg) = 0;
512};
513
514/**
515 * Socket receiver - manages receiving NXCP messages from multiple sockets
516 */
517class LIBNETXMS_EXPORTABLE SocketReceiver
518{
519private:
520 THREAD m_thread;
521 HashMap<SOCKET, MessageConsumer> *m_consumers;
522
523 static int m_maxSocketsPerThread;
524 static ObjectArray<SocketReceiver> *m_receivers;
525
526public:
527 static void start();
528 static void shutdown();
529
530 static void addConsumer(MessageConsumer *mc);
531 static void removeConsumer(MessageConsumer *mc);
532
533 static String getDiagInfo();
534};
535#endif
536
9f2ad16c
VK
537#else /* __cplusplus */
538
b368969c 539typedef void NXCPMessage;
98abc9f1 540typedef void NXCPEncryptionContext;
9f2ad16c
VK
541
542#endif
543
da9ac8fc
VK
544typedef bool (*NXCPMessageNameResolver)(UINT16 code, TCHAR *buffer);
545
deaa4491
VK
546
547//
0d9c1b96
VK
548// Functions
549//
550
551#ifdef __cplusplus
0d9c1b96 552
efe30b82 553void LIBNETXMS_EXPORTABLE NXCPInitBuffer(NXCP_BUFFER *nxcpBuffer);
b368969c
VK
554int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, NXCP_MESSAGE *pMsg,
555 NXCP_BUFFER *pBuffer, UINT32 dwMaxMsgSize,
98abc9f1 556 NXCPEncryptionContext **ppCtx,
967893bb 557 BYTE *pDecryptionBuffer, UINT32 dwTimeout);
e3ff8ad1
VK
558int LIBNETXMS_EXPORTABLE RecvNXCPMessage(AbstractCommChannel *channel, NXCP_MESSAGE *pMsg,
559 NXCP_BUFFER *pBuffer, UINT32 dwMaxMsgSize,
560 NXCPEncryptionContext **ppCtx,
561 BYTE *pDecryptionBuffer, UINT32 dwTimeout);
b368969c
VK
562int LIBNETXMS_EXPORTABLE RecvNXCPMessageEx(SOCKET hSocket, NXCP_MESSAGE **msgBuffer,
563 NXCP_BUFFER *nxcpBuffer, UINT32 *bufferSize,
cc022855 564 NXCPEncryptionContext **ppCtx,
967893bb
VK
565 BYTE **decryptionBuffer, UINT32 dwTimeout,
566 UINT32 maxMsgSize);
e3ff8ad1
VK
567int LIBNETXMS_EXPORTABLE RecvNXCPMessageEx(AbstractCommChannel *channel, NXCP_MESSAGE **msgBuffer,
568 NXCP_BUFFER *nxcpBuffer, UINT32 *bufferSize,
569 NXCPEncryptionContext **ppCtx,
570 BYTE **decryptionBuffer, UINT32 dwTimeout,
571 UINT32 maxMsgSize);
d3a20572
VK
572NXCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(UINT16 code, UINT32 id, UINT16 flags,
573 const void *data, size_t dataSize,
574 NXCP_MESSAGE *buffer, bool allowCompression);
eae56922 575bool LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, UINT32 dwId, const TCHAR *pszFile,
cc022855 576 NXCPEncryptionContext *pCtx, long offset,
7b8b337e 577 void (* progressCallback)(INT64, void *), void *cbArg,
d3a20572 578 MUTEX mutex, NXCPStreamCompressionMethod compressionMethod = NXCP_STREAM_COMPRESSION_NONE);
eae56922
VK
579bool LIBNETXMS_EXPORTABLE SendFileOverNXCP(AbstractCommChannel *channel, UINT32 dwId, const TCHAR *pszFile,
580 NXCPEncryptionContext *pCtx, long offset,
581 void (* progressCallback)(INT64, void *), void *cbArg,
582 MUTEX mutex, NXCPStreamCompressionMethod compressionMethod = NXCP_STREAM_COMPRESSION_NONE);
e3ff8ad1
VK
583bool LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET s, int *pnVersion, MUTEX mutex);
584bool LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(AbstractCommChannel *channel, int *pnVersion, MUTEX mutex);
cc022855 585
da9ac8fc
VK
586TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(UINT16 wCode, TCHAR *buffer);
587void LIBNETXMS_EXPORTABLE NXCPRegisterMessageNameResolver(NXCPMessageNameResolver r);
588void LIBNETXMS_EXPORTABLE NXCPUnregisterMessageNameResolver(NXCPMessageNameResolver r);
589
2df047f4 590bool LIBNETXMS_EXPORTABLE InitCryptoLib(UINT32 dwEnabledCiphers);
e5390fb5
VK
591UINT32 LIBNETXMS_EXPORTABLE NXCPGetSupportedCiphers();
592String LIBNETXMS_EXPORTABLE NXCPGetSupportedCiphersAsText();
b368969c 593NXCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE *NXCPEncryptMessage(NXCPEncryptionContext *pCtx, NXCP_MESSAGE *pMsg);
bf6fb6c3 594bool LIBNETXMS_EXPORTABLE NXCPDecryptMessage(NXCPEncryptionContext *pCtx,
6be0a20b 595 NXCP_ENCRYPTED_MESSAGE *pMsg,
e44ac467 596 BYTE *pDecryptionBuffer);
b368969c 597UINT32 LIBNETXMS_EXPORTABLE SetupEncryptionContext(NXCPMessage *pMsg,
98abc9f1 598 NXCPEncryptionContext **ppCtx,
b368969c 599 NXCPMessage **ppResponse,
4bd3d6c5 600 RSA *pPrivateKey, int nNXCPVersion);
b368969c 601void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(NXCPMessage *pMsg, RSA *pServerKey, bool useX509Format);
3aa0061c 602RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile);
cce82c3a
VK
603RSA LIBNETXMS_EXPORTABLE *RSAKeyFromData(const BYTE *data, size_t size, bool withPrivate);
604void LIBNETXMS_EXPORTABLE RSAFree(RSA *key);
15cf5acc 605
bb8136f7 606#ifdef _WIN32
967893bb 607BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, UINT32 dwMsgLen, const CERT_CONTEXT *pCert,
b368969c 608 BYTE *pBuffer, size_t bufferSize, UINT32 *pdwSigLen);
bb8136f7
VK
609#endif
610
0d9c1b96
VK
611#endif
612
9f2ad16c 613#endif /* _nxcpapi_h_ */