fixed deadlock
[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
5c44534b
VK
47struct MessageField;
48
4af351c7
VK
49/**
50 * Parsed NXCP message
51 */
9f2ad16c 52class LIBNETXMS_EXPORTABLE CSCPMessage
0d9c1b96
VK
53{
54private:
5c44534b
VK
55 WORD m_code;
56 WORD m_flags;
57 UINT32 m_id;
58 MessageField *m_fields; // Message fields
59 int m_version; // Protocol version
0d9c1b96 60
5c44534b 61 void *set(UINT32 fieldId, BYTE type, const void *value, UINT32 size = 0);
8f238fd7 62 void *get(UINT32 fieldId, BYTE requiredType, BYTE *fieldType = NULL);
5c44534b 63 CSCP_DF *find(UINT32 fieldId);
0d9c1b96
VK
64
65public:
4bd3d6c5 66 CSCPMessage(int nVersion = NXCP_VERSION);
30639d32 67 CSCPMessage(CSCPMessage *pMsg);
4bd3d6c5 68 CSCPMessage(CSCP_MESSAGE *pMsg, int nVersion = NXCP_VERSION);
ff175e91 69 CSCPMessage(const char *xml);
0d9c1b96
VK
70 ~CSCPMessage();
71
5c44534b 72 CSCP_MESSAGE *createMessage();
4af351c7
VK
73 char *createXML();
74 void processXMLToken(void *state, const char **attrs);
75 void processXMLData(void *state);
0d9c1b96 76
5c44534b
VK
77 WORD GetCode() { return m_code; }
78 void SetCode(WORD code) { m_code = code; }
0d9c1b96 79
5c44534b
VK
80 UINT32 GetId() { return m_id; }
81 void SetId(UINT32 id) { m_id = id; }
0d9c1b96 82
5c44534b
VK
83 bool isEndOfSequence() { return (m_flags & MF_END_OF_SEQUENCE) ? true : false; }
84 bool isReverseOrder() { return (m_flags & MF_REVERSE_ORDER) ? true : false; }
4af351c7 85
4101571e
VK
86 bool isFieldExist(UINT32 fieldId) { return find(fieldId) != NULL; }
87 int getFieldType(UINT32 fieldId);
88
8f238fd7
VK
89 void SetVariable(UINT32 fieldId, INT16 wValue) { set(fieldId, CSCP_DT_INT16, &wValue); }
90 void SetVariable(UINT32 fieldId, UINT16 wValue) { set(fieldId, CSCP_DT_INT16, &wValue); }
91 void SetVariable(UINT32 fieldId, INT32 dwValue) { set(fieldId, CSCP_DT_INT32, &dwValue); }
92 void SetVariable(UINT32 fieldId, UINT32 dwValue) { set(fieldId, CSCP_DT_INT32, &dwValue); }
93 void SetVariable(UINT32 fieldId, INT64 qwValue) { set(fieldId, CSCP_DT_INT64, &qwValue); }
94 void SetVariable(UINT32 fieldId, UINT64 qwValue) { set(fieldId, CSCP_DT_INT64, &qwValue); }
95 void SetVariable(UINT32 fieldId, double dValue) { set(fieldId, CSCP_DT_FLOAT, &dValue); }
96 void SetVariable(UINT32 fieldId, const TCHAR *value) { if (value != NULL) set(fieldId, CSCP_DT_STRING, value); }
97 void SetVariable(UINT32 fieldId, const TCHAR *value, UINT32 maxLen) { if (value != NULL) set(fieldId, CSCP_DT_STRING, value, maxLen); }
98 void SetVariable(UINT32 fieldId, BYTE *pValue, UINT32 dwSize) { set(fieldId, CSCP_DT_BINARY, pValue, dwSize); }
35f836fe 99#ifdef UNICODE
8f238fd7 100 void SetVariableFromMBString(UINT32 fieldId, const char *pszValue);
35f836fe 101#else
8f238fd7 102 void SetVariableFromMBString(UINT32 fieldId, const char *pszValue) { set(fieldId, CSCP_DT_STRING, pszValue); }
35f836fe 103#endif
8f238fd7
VK
104 void setField(UINT32 fieldId, time_t value) { UINT64 t = (UINT64)value; set(fieldId, CSCP_DT_INT64, &t); }
105 void setFieldInt32Array(UINT32 fieldId, UINT32 dwNumElements, const UINT32 *pdwData);
106 void setFieldInt32Array(UINT32 fieldId, IntegerArray<UINT32> *data);
107 bool setFieldFromFile(UINT32 fieldId, const TCHAR *pszFileName);
967893bb 108
4101571e
VK
109 INT16 getFieldAsInt16(UINT32 fieldId);
110 INT32 getFieldAsInt32(UINT32 fieldId);
111 INT64 getFieldAsInt64(UINT32 fieldId);
112 double getFieldAsDouble(UINT32 fieldId);
d6bbfa4e 113 bool getFieldAsBoolean(UINT32 fieldId);
8f238fd7 114 time_t getFieldAsTime(UINT32 fieldId);
9a68ca24
VK
115 UINT32 getFieldAsInt32Array(UINT32 fieldId, UINT32 numElements, UINT32 *buffer);
116 UINT32 getFieldAsInt32Array(UINT32 fieldId, IntegerArray<UINT32> *data);
ac14e3e6 117 BYTE *getBinaryFieldPtr(UINT32 fieldId, size_t *size);
4101571e 118
8f238fd7
VK
119 UINT32 GetVariableLong(UINT32 fieldId);
120 UINT64 GetVariableInt64(UINT32 fieldId);
121 UINT16 GetVariableShort(UINT32 fieldId);
122 TCHAR *GetVariableStr(UINT32 fieldId, TCHAR *szBuffer = NULL, UINT32 dwBufSize = 0);
123 char *GetVariableStrA(UINT32 fieldId, char *pszBuffer = NULL, UINT32 dwBufSize = 0);
124 char *GetVariableStrUTF8(UINT32 fieldId, char *pszBuffer = NULL, UINT32 dwBufSize = 0);
125 UINT32 GetVariableBinary(UINT32 fieldId, BYTE *pBuffer, UINT32 dwBufSize);
0d9c1b96 126
4af351c7 127 void deleteAllVariables();
e44ac467 128
5c44534b
VK
129 void disableEncryption() { m_flags |= MF_DONT_ENCRYPT; }
130 void setEndOfSequence() { m_flags |= MF_END_OF_SEQUENCE; }
131 void setReverseOrderFlag() { m_flags |= MF_REVERSE_ORDER; }
934f53da
VK
132
133 static String dump(CSCP_MESSAGE *msg, int version);
0d9c1b96
VK
134};
135
4af351c7
VK
136/**
137 * Message waiting queue element structure
138 */
deaa4491
VK
139typedef struct
140{
f128c07c
VK
141 void *msg; // Pointer to message, either to CSCPMessage object or raw message
142 UINT32 id; // Message ID
143 UINT32 ttl; // Message time-to-live in milliseconds
144 UINT16 code; // Message code
145 UINT16 isBinary; // 1 for binary (raw) messages
deaa4491
VK
146} WAIT_QUEUE_ELEMENT;
147
4af351c7 148/**
f128c07c
VK
149 * Max number of waiting threads in message queue
150 */
151#define MAX_MSGQUEUE_WAITERS 32
152
153/**
4af351c7
VK
154 * Message waiting queue class
155 */
9f2ad16c 156class LIBNETXMS_EXPORTABLE MsgWaitQueue
deaa4491 157{
deaa4491 158private:
f128c07c
VK
159#ifdef _WIN32
160 CRITICAL_SECTION m_mutex;
161 HANDLE m_wakeupEvents[MAX_MSGQUEUE_WAITERS];
162 BYTE m_waiters[MAX_MSGQUEUE_WAITERS];
163#else
164 pthread_mutex_t m_mutex;
165 pthread_cond_t m_wakeupCondition;
166#endif
167 CONDITION m_stopCondition;
168 UINT32 m_holdTime;
169 int m_size;
170 int m_allocated;
171 WAIT_QUEUE_ELEMENT *m_elements;
ccdbbb52 172 THREAD m_hHkThread;
deaa4491 173
c17f6cbc 174 void housekeeperThread();
f128c07c
VK
175 void *waitForMessageInternal(UINT16 isBinary, UINT16 code, UINT32 id, UINT32 timeout);
176
177 void lock()
178 {
179#ifdef _WIN32
180 EnterCriticalSection(&m_mutex);
181#else
182 pthread_mutex_lock(&m_mutex);
183#endif
184 }
185
186 void unlock()
187 {
188#ifdef _WIN32
189 LeaveCriticalSection(&m_mutex);
190#else
191 pthread_mutex_unlock(&m_mutex);
192#endif
193 }
cc022855 194
c17f6cbc 195 static THREAD_RESULT THREAD_CALL mwqThreadStarter(void *);
deaa4491
VK
196
197public:
198 MsgWaitQueue();
199 ~MsgWaitQueue();
200
c17f6cbc
VK
201 void put(CSCPMessage *pMsg);
202 void put(CSCP_MESSAGE *pMsg);
967893bb 203 CSCPMessage *waitForMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 204 {
c17f6cbc 205 return (CSCPMessage *)waitForMessageInternal(0, wCode, dwId, dwTimeOut);
deaa4491 206 }
967893bb 207 CSCP_MESSAGE *waitForRawMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 208 {
c17f6cbc 209 return (CSCP_MESSAGE *)waitForMessageInternal(1, wCode, dwId, dwTimeOut);
deaa4491 210 }
cc022855 211
c17f6cbc 212 void clear();
f128c07c 213 void setHoldTime(UINT32 holdTime) { m_holdTime = holdTime; }
deaa4491
VK
214};
215
b549a0f8
VK
216/**
217 * NXCP encryption context
218 */
98abc9f1
VK
219class LIBNETXMS_EXPORTABLE NXCPEncryptionContext : public RefCountObject
220{
221private:
222 int m_cipher;
223 BYTE *m_sessionKey;
224 int m_keyLength;
225 BYTE m_iv[EVP_MAX_IV_LENGTH];
e4e091f0 226#ifdef _WITH_ENCRYPTION
e6336a90
VK
227 EVP_CIPHER_CTX m_encryptor;
228 EVP_CIPHER_CTX m_decryptor;
db05c2af 229 MUTEX m_encryptorLock;
e4e091f0 230#endif
98abc9f1
VK
231
232 NXCPEncryptionContext();
e6336a90 233 bool initCipher(int cipher);
98abc9f1
VK
234
235public:
236 static NXCPEncryptionContext *create(CSCPMessage *msg, RSA *privateKey);
967893bb 237 static NXCPEncryptionContext *create(UINT32 ciphers);
98abc9f1
VK
238
239 virtual ~NXCPEncryptionContext();
240
e6336a90
VK
241 CSCP_ENCRYPTED_MESSAGE *encryptMessage(CSCP_MESSAGE *msg);
242 bool decryptMessage(CSCP_ENCRYPTED_MESSAGE *msg, BYTE *decryptionBuffer);
243
98abc9f1
VK
244 int getCipher() { return m_cipher; }
245 BYTE *getSessionKey() { return m_sessionKey; }
246 int getKeyLength() { return m_keyLength; }
247 BYTE *getIV() { return m_iv; }
248};
249
9f2ad16c
VK
250#else /* __cplusplus */
251
252typedef void CSCPMessage;
98abc9f1 253typedef void NXCPEncryptionContext;
9f2ad16c
VK
254
255#endif
256
deaa4491
VK
257
258//
0d9c1b96
VK
259// Functions
260//
261
262#ifdef __cplusplus
0d9c1b96 263
9f2ad16c 264int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
967893bb 265 CSCP_BUFFER *pBuffer, UINT32 dwMaxMsgSize,
98abc9f1 266 NXCPEncryptionContext **ppCtx,
967893bb 267 BYTE *pDecryptionBuffer, UINT32 dwTimeout);
b9b2b87b 268int LIBNETXMS_EXPORTABLE RecvNXCPMessageEx(SOCKET hSocket, CSCP_MESSAGE **msgBuffer,
967893bb 269 CSCP_BUFFER *nxcpBuffer, UINT32 *bufferSize,
cc022855 270 NXCPEncryptionContext **ppCtx,
967893bb
VK
271 BYTE **decryptionBuffer, UINT32 dwTimeout,
272 UINT32 maxMsgSize);
273CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, UINT32 dwId, WORD wFlags,
274 UINT32 dwDataSize, void *pData,
0d9c1b96 275 CSCP_MESSAGE *pBuffer);
9f2ad16c 276TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
967893bb 277BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, UINT32 dwId, const TCHAR *pszFile,
cc022855 278 NXCPEncryptionContext *pCtx, long offset,
7b8b337e
VK
279 void (* progressCallback)(INT64, void *), void *cbArg,
280 MUTEX mutex);
281BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion, MUTEX mutex);
cc022855 282
6468147c 283BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(UINT32 dwEnabledCiphers, void (*debugCallback)(int, const TCHAR *, va_list args));
967893bb 284UINT32 LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers();
98abc9f1
VK
285CSCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE *CSCPEncryptMessage(NXCPEncryptionContext *pCtx, CSCP_MESSAGE *pMsg);
286BOOL LIBNETXMS_EXPORTABLE CSCPDecryptMessage(NXCPEncryptionContext *pCtx,
e44ac467
VK
287 CSCP_ENCRYPTED_MESSAGE *pMsg,
288 BYTE *pDecryptionBuffer);
cc022855 289UINT32 LIBNETXMS_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
98abc9f1 290 NXCPEncryptionContext **ppCtx,
1a17feb0 291 CSCPMessage **ppResponse,
4bd3d6c5 292 RSA *pPrivateKey, int nNXCPVersion);
9e9d631e 293void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey, bool useX509Format);
3aa0061c 294RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile);
15cf5acc 295
bb8136f7 296#ifdef _WIN32
967893bb
VK
297BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, UINT32 dwMsgLen, const CERT_CONTEXT *pCert,
298 BYTE *pBuffer, UINT32 dwBufSize, UINT32 *pdwSigLen);
bb8136f7
VK
299#endif
300
0d9c1b96
VK
301#endif
302
9f2ad16c 303#endif /* _nxcpapi_h_ */