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