Merge branch 'develop' of 10.3.0.4:public/netxms into develop
[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
VK
34/**
35 * Temporary buffer structure for RecvCSCPMessage() function
36 */
0d9c1b96
VK
37typedef struct
38{
967893bb
VK
39 UINT32 dwBufSize;
40 UINT32 dwBufPos;
0d9c1b96
VK
41 char szBuffer[CSCP_TEMP_BUF_SIZE];
42} CSCP_BUFFER;
43
44
9f2ad16c
VK
45#ifdef __cplusplus
46
4af351c7
VK
47/**
48 * Parsed NXCP message
49 */
9f2ad16c 50class LIBNETXMS_EXPORTABLE CSCPMessage
0d9c1b96
VK
51{
52private:
53 WORD m_wCode;
23b8cd4f 54 WORD m_wFlags;
967893bb
VK
55 UINT32 m_dwId;
56 UINT32 m_dwNumVar; // Number of variables
0d9c1b96 57 CSCP_DF **m_ppVarList; // List of variables
4bd3d6c5 58 int m_nVersion; // Protocol version
0d9c1b96 59
4af351c7
VK
60 void *set(UINT32 dwVarId, BYTE bType, const void *pValue, UINT32 dwSize = 0);
61 void *get(UINT32 dwVarId, BYTE bType);
62 UINT32 findVariable(UINT32 dwVarId);
0d9c1b96
VK
63
64public:
4bd3d6c5 65 CSCPMessage(int nVersion = NXCP_VERSION);
30639d32 66 CSCPMessage(CSCPMessage *pMsg);
4bd3d6c5 67 CSCPMessage(CSCP_MESSAGE *pMsg, int nVersion = NXCP_VERSION);
ff175e91 68 CSCPMessage(const char *xml);
0d9c1b96
VK
69 ~CSCPMessage();
70
4af351c7
VK
71 CSCP_MESSAGE *CreateMessage();
72 char *createXML();
73 void processXMLToken(void *state, const char **attrs);
74 void processXMLData(void *state);
0d9c1b96 75
4af351c7 76 WORD GetCode() { return m_wCode; }
0d9c1b96
VK
77 void SetCode(WORD wCode) { m_wCode = wCode; }
78
4af351c7 79 UINT32 GetId() { return m_dwId; }
967893bb 80 void SetId(UINT32 dwId) { m_dwId = dwId; }
0d9c1b96 81
4af351c7
VK
82 BOOL IsVariableExist(UINT32 dwVarId) { return (findVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
83 bool isEndOfSequence() { return (m_wFlags & MF_END_OF_SEQUENCE) ? TRUE : FALSE; }
84 bool isReverseOrder() { return (m_wFlags & MF_REVERSE_ORDER) ? TRUE : FALSE; }
85
86 void SetVariable(UINT32 dwVarId, INT16 wValue) { set(dwVarId, CSCP_DT_INT16, &wValue); }
87 void SetVariable(UINT32 dwVarId, UINT16 wValue) { set(dwVarId, CSCP_DT_INT16, &wValue); }
88 void SetVariable(UINT32 dwVarId, INT32 dwValue) { set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
89 void SetVariable(UINT32 dwVarId, UINT32 dwValue) { set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
90 void SetVariable(UINT32 dwVarId, INT64 qwValue) { set(dwVarId, CSCP_DT_INT64, &qwValue); }
91 void SetVariable(UINT32 dwVarId, UINT64 qwValue) { set(dwVarId, CSCP_DT_INT64, &qwValue); }
92 void SetVariable(UINT32 dwVarId, double dValue) { set(dwVarId, CSCP_DT_FLOAT, &dValue); }
93 void SetVariable(UINT32 dwVarId, const TCHAR *value) { if (value != NULL) set(dwVarId, CSCP_DT_STRING, value); }
94 void SetVariable(UINT32 dwVarId, const TCHAR *value, UINT32 maxLen) { if (value != NULL) set(dwVarId, CSCP_DT_STRING, value, maxLen); }
95 void SetVariable(UINT32 dwVarId, BYTE *pValue, UINT32 dwSize) { set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
35f836fe 96#ifdef UNICODE
967893bb 97 void SetVariableFromMBString(UINT32 dwVarId, const char *pszValue);
35f836fe 98#else
4af351c7 99 void SetVariableFromMBString(UINT32 dwVarId, const char *pszValue) { set(dwVarId, CSCP_DT_STRING, pszValue); }
35f836fe 100#endif
967893bb
VK
101 void SetVariableToInt32Array(UINT32 dwVarId, UINT32 dwNumElements, const UINT32 *pdwData);
102 BOOL SetVariableFromFile(UINT32 dwVarId, const TCHAR *pszFileName);
103
104 UINT32 GetVariableLong(UINT32 dwVarId);
105 UINT64 GetVariableInt64(UINT32 dwVarId);
106 UINT16 GetVariableShort(UINT32 dwVarId);
107 INT32 GetVariableShortAsInt32(UINT32 dwVarId);
108 double GetVariableDouble(UINT32 dwVarId);
109 TCHAR *GetVariableStr(UINT32 dwVarId, TCHAR *szBuffer = NULL, UINT32 dwBufSize = 0);
110 char *GetVariableStrA(UINT32 dwVarId, char *pszBuffer = NULL, UINT32 dwBufSize = 0);
111 char *GetVariableStrUTF8(UINT32 dwVarId, char *pszBuffer = NULL, UINT32 dwBufSize = 0);
112 UINT32 GetVariableBinary(UINT32 dwVarId, BYTE *pBuffer, UINT32 dwBufSize);
113 UINT32 GetVariableInt32Array(UINT32 dwVarId, UINT32 dwNumElements, UINT32 *pdwBuffer);
0d9c1b96 114
4af351c7 115 void deleteAllVariables();
e44ac467 116
4af351c7
VK
117 void disableEncryption() { m_wFlags |= MF_DONT_ENCRYPT; }
118 void setEndOfSequence() { m_wFlags |= MF_END_OF_SEQUENCE; }
119 void setReverseOrderFlag() { m_wFlags |= MF_REVERSE_ORDER; }
934f53da
VK
120
121 static String dump(CSCP_MESSAGE *msg, int version);
0d9c1b96
VK
122};
123
4af351c7
VK
124/**
125 * Message waiting queue element structure
126 */
deaa4491
VK
127typedef struct
128{
129 WORD wCode; // Message code
130 WORD wIsBinary; // 1 for binary (raw) messages
967893bb
VK
131 UINT32 dwId; // Message ID
132 UINT32 dwTTL; // Message time-to-live in milliseconds
deaa4491
VK
133 void *pMsg; // Pointer to message, either to CSCPMessage object or raw message
134} WAIT_QUEUE_ELEMENT;
135
4af351c7
VK
136/**
137 * Message waiting queue class
138 */
9f2ad16c 139class LIBNETXMS_EXPORTABLE MsgWaitQueue
deaa4491 140{
deaa4491 141private:
5f743326
VK
142 MUTEX m_mutexDataAccess;
143 CONDITION m_condStop;
144 CONDITION m_condNewMsg;
967893bb
VK
145 UINT32 m_dwMsgHoldTime;
146 UINT32 m_dwNumElements;
deaa4491 147 WAIT_QUEUE_ELEMENT *m_pElements;
ccdbbb52 148 THREAD m_hHkThread;
deaa4491 149
c17f6cbc
VK
150 void lock() { MutexLock(m_mutexDataAccess); }
151 void unlock() { MutexUnlock(m_mutexDataAccess); }
152 void housekeeperThread();
967893bb 153 void *waitForMessageInternal(UINT16 wIsBinary, UINT16 wCode, UINT32 dwId, UINT32 dwTimeOut);
1ba9a162 154
c17f6cbc 155 static THREAD_RESULT THREAD_CALL mwqThreadStarter(void *);
deaa4491
VK
156
157public:
158 MsgWaitQueue();
159 ~MsgWaitQueue();
160
c17f6cbc
VK
161 void put(CSCPMessage *pMsg);
162 void put(CSCP_MESSAGE *pMsg);
967893bb 163 CSCPMessage *waitForMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 164 {
c17f6cbc 165 return (CSCPMessage *)waitForMessageInternal(0, wCode, dwId, dwTimeOut);
deaa4491 166 }
967893bb 167 CSCP_MESSAGE *waitForRawMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 168 {
c17f6cbc 169 return (CSCP_MESSAGE *)waitForMessageInternal(1, wCode, dwId, dwTimeOut);
deaa4491
VK
170 }
171
c17f6cbc 172 void clear();
967893bb 173 void setHoldTime(UINT32 dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
deaa4491
VK
174};
175
b549a0f8
VK
176/**
177 * NXCP encryption context
178 */
98abc9f1
VK
179class LIBNETXMS_EXPORTABLE NXCPEncryptionContext : public RefCountObject
180{
181private:
182 int m_cipher;
183 BYTE *m_sessionKey;
184 int m_keyLength;
185 BYTE m_iv[EVP_MAX_IV_LENGTH];
e4e091f0 186#ifdef _WITH_ENCRYPTION
e6336a90
VK
187 EVP_CIPHER_CTX m_encryptor;
188 EVP_CIPHER_CTX m_decryptor;
db05c2af 189 MUTEX m_encryptorLock;
e4e091f0 190#endif
98abc9f1
VK
191
192 NXCPEncryptionContext();
e6336a90 193 bool initCipher(int cipher);
98abc9f1
VK
194
195public:
196 static NXCPEncryptionContext *create(CSCPMessage *msg, RSA *privateKey);
967893bb 197 static NXCPEncryptionContext *create(UINT32 ciphers);
98abc9f1
VK
198
199 virtual ~NXCPEncryptionContext();
200
e6336a90
VK
201 CSCP_ENCRYPTED_MESSAGE *encryptMessage(CSCP_MESSAGE *msg);
202 bool decryptMessage(CSCP_ENCRYPTED_MESSAGE *msg, BYTE *decryptionBuffer);
203
98abc9f1
VK
204 int getCipher() { return m_cipher; }
205 BYTE *getSessionKey() { return m_sessionKey; }
206 int getKeyLength() { return m_keyLength; }
207 BYTE *getIV() { return m_iv; }
208};
209
9f2ad16c
VK
210#else /* __cplusplus */
211
212typedef void CSCPMessage;
98abc9f1 213typedef void NXCPEncryptionContext;
9f2ad16c
VK
214
215#endif
216
deaa4491
VK
217
218//
0d9c1b96
VK
219// Functions
220//
221
222#ifdef __cplusplus
0d9c1b96 223
9f2ad16c 224int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
967893bb 225 CSCP_BUFFER *pBuffer, UINT32 dwMaxMsgSize,
98abc9f1 226 NXCPEncryptionContext **ppCtx,
967893bb 227 BYTE *pDecryptionBuffer, UINT32 dwTimeout);
b9b2b87b 228int LIBNETXMS_EXPORTABLE RecvNXCPMessageEx(SOCKET hSocket, CSCP_MESSAGE **msgBuffer,
967893bb 229 CSCP_BUFFER *nxcpBuffer, UINT32 *bufferSize,
98abc9f1 230 NXCPEncryptionContext **ppCtx,
967893bb
VK
231 BYTE **decryptionBuffer, UINT32 dwTimeout,
232 UINT32 maxMsgSize);
233CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, UINT32 dwId, WORD wFlags,
234 UINT32 dwDataSize, void *pData,
0d9c1b96 235 CSCP_MESSAGE *pBuffer);
9f2ad16c 236TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
967893bb 237BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, UINT32 dwId, const TCHAR *pszFile,
9fa031cd 238 NXCPEncryptionContext *pCtx, long offset, long sizeLimit,
7b8b337e
VK
239 void (* progressCallback)(INT64, void *), void *cbArg,
240 MUTEX mutex);
241BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion, MUTEX mutex);
0d9c1b96 242
6468147c 243BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(UINT32 dwEnabledCiphers, void (*debugCallback)(int, const TCHAR *, va_list args));
967893bb 244UINT32 LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers();
98abc9f1
VK
245CSCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE *CSCPEncryptMessage(NXCPEncryptionContext *pCtx, CSCP_MESSAGE *pMsg);
246BOOL LIBNETXMS_EXPORTABLE CSCPDecryptMessage(NXCPEncryptionContext *pCtx,
e44ac467
VK
247 CSCP_ENCRYPTED_MESSAGE *pMsg,
248 BYTE *pDecryptionBuffer);
967893bb 249UINT32 LIBNETXMS_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
98abc9f1 250 NXCPEncryptionContext **ppCtx,
1a17feb0 251 CSCPMessage **ppResponse,
4bd3d6c5 252 RSA *pPrivateKey, int nNXCPVersion);
9e9d631e 253void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey, bool useX509Format);
3aa0061c 254RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile);
15cf5acc 255
bb8136f7 256#ifdef _WIN32
967893bb
VK
257BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, UINT32 dwMsgLen, const CERT_CONTEXT *pCert,
258 BYTE *pBuffer, UINT32 dwBufSize, UINT32 *pdwSigLen);
bb8136f7
VK
259#endif
260
0d9c1b96
VK
261#endif
262
9f2ad16c 263#endif /* _nxcpapi_h_ */