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