changes from RCP console
[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{
41 DWORD dwBufSize;
42 DWORD dwBufPos;
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;
0d9c1b96
VK
58 DWORD m_dwId;
59 DWORD m_dwNumVar; // Number of variables
60 CSCP_DF **m_ppVarList; // List of variables
4bd3d6c5 61 int m_nVersion; // Protocol version
0d9c1b96 62
76154a95 63 void *Set(DWORD dwVarId, BYTE bType, const void *pValue, DWORD dwSize = 0);
0d9c1b96
VK
64 void *Get(DWORD dwVarId, BYTE bType);
65 DWORD FindVariable(DWORD dwVarId);
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
82 DWORD GetId(void) { return m_dwId; }
83 void SetId(DWORD dwId) { m_dwId = dwId; }
84
85 BOOL IsVariableExist(DWORD 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
42d7ed00
VK
89 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, CSCP_DT_INT16, &wValue); }
90 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
91 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, CSCP_DT_INT64, &qwValue); }
92 void SetVariable(DWORD dwVarId, double dValue) { Set(dwVarId, CSCP_DT_FLOAT, &dValue); }
76154a95 93 void SetVariable(DWORD dwVarId, const TCHAR *pszValue) { Set(dwVarId, CSCP_DT_STRING, pszValue); }
fb9441ee 94 void SetVariable(DWORD dwVarId, const TCHAR *pszValue, DWORD maxLen) { Set(dwVarId, CSCP_DT_STRING, pszValue, maxLen); }
42d7ed00 95 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
35f836fe
VK
96#ifdef UNICODE
97 void SetVariableFromMBString(DWORD dwVarId, const char *pszValue);
98#else
99 void SetVariableFromMBString(DWORD dwVarId, const char *pszValue) { Set(dwVarId, CSCP_DT_STRING, pszValue); }
100#endif
b4369830 101 void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, const DWORD *pdwData);
3aa0061c 102 BOOL SetVariableFromFile(DWORD dwVarId, const TCHAR *pszFileName);
0d9c1b96
VK
103
104 DWORD GetVariableLong(DWORD dwVarId);
105 QWORD GetVariableInt64(DWORD dwVarId);
106 WORD GetVariableShort(DWORD dwVarId);
cc2d5b36 107 LONG GetVariableShortAsInt32(DWORD dwVarId);
f86ffdd2 108 double GetVariableDouble(DWORD dwVarId);
4332e9d8 109 TCHAR *GetVariableStr(DWORD dwVarId, TCHAR *szBuffer = NULL, DWORD dwBufSize = 0);
35f836fe
VK
110 char *GetVariableStrA(DWORD dwVarId, char *pszBuffer = NULL, DWORD dwBufSize = 0);
111 char *GetVariableStrUTF8(DWORD dwVarId, char *pszBuffer = NULL, DWORD dwBufSize = 0);
0d9c1b96 112 DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
a7d07192 113 DWORD GetVariableInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwBuffer);
0d9c1b96
VK
114
115 void DeleteAllVariables(void);
e44ac467 116
23b8cd4f
VK
117 void DisableEncryption(void) { m_wFlags |= MF_DONT_ENCRYPT; }
118 void SetEndOfSequence(void) { m_wFlags |= MF_END_OF_SEQUENCE; }
30639d32 119 void SetReverseOrderFlag(void) { m_wFlags |= MF_REVERSE_ORDER; }
0d9c1b96
VK
120};
121
122
123//
deaa4491
VK
124// Message waiting queue element structure
125//
126
127typedef struct
128{
129 WORD wCode; // Message code
130 WORD wIsBinary; // 1 for binary (raw) messages
131 DWORD dwId; // Message ID
132 DWORD dwTTL; // Message time-to-live in milliseconds
133 void *pMsg; // Pointer to message, either to CSCPMessage object or raw message
134} WAIT_QUEUE_ELEMENT;
135
136
137//
138// Message waiting queue class
139//
140
9f2ad16c 141class LIBNETXMS_EXPORTABLE MsgWaitQueue
deaa4491 142{
deaa4491 143private:
5f743326
VK
144 MUTEX m_mutexDataAccess;
145 CONDITION m_condStop;
146 CONDITION m_condNewMsg;
deaa4491
VK
147 DWORD m_dwMsgHoldTime;
148 DWORD m_dwNumElements;
149 WAIT_QUEUE_ELEMENT *m_pElements;
ccdbbb52 150 THREAD m_hHkThread;
deaa4491 151
c17f6cbc
VK
152 void lock() { MutexLock(m_mutexDataAccess); }
153 void unlock() { MutexUnlock(m_mutexDataAccess); }
154 void housekeeperThread();
155 void *waitForMessageInternal(WORD wIsBinary, WORD wCode, DWORD dwId, DWORD dwTimeOut);
1ba9a162 156
c17f6cbc 157 static THREAD_RESULT THREAD_CALL mwqThreadStarter(void *);
deaa4491
VK
158
159public:
160 MsgWaitQueue();
161 ~MsgWaitQueue();
162
c17f6cbc
VK
163 void put(CSCPMessage *pMsg);
164 void put(CSCP_MESSAGE *pMsg);
165 CSCPMessage *waitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
deaa4491 166 {
c17f6cbc 167 return (CSCPMessage *)waitForMessageInternal(0, wCode, dwId, dwTimeOut);
deaa4491 168 }
c17f6cbc 169 CSCP_MESSAGE *waitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
deaa4491 170 {
c17f6cbc 171 return (CSCP_MESSAGE *)waitForMessageInternal(1, wCode, dwId, dwTimeOut);
deaa4491
VK
172 }
173
c17f6cbc
VK
174 void clear();
175 void setHoldTime(DWORD dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
deaa4491
VK
176};
177
98abc9f1
VK
178
179//
180// NXCP encryption context
181//
182
183class LIBNETXMS_EXPORTABLE NXCPEncryptionContext : public RefCountObject
184{
185private:
186 int m_cipher;
187 BYTE *m_sessionKey;
188 int m_keyLength;
189 BYTE m_iv[EVP_MAX_IV_LENGTH];
190
191 NXCPEncryptionContext();
192
193public:
194 static NXCPEncryptionContext *create(CSCPMessage *msg, RSA *privateKey);
195 static NXCPEncryptionContext *create(DWORD ciphers);
196
197 virtual ~NXCPEncryptionContext();
198
199 int getCipher() { return m_cipher; }
200 BYTE *getSessionKey() { return m_sessionKey; }
201 int getKeyLength() { return m_keyLength; }
202 BYTE *getIV() { return m_iv; }
203};
204
9f2ad16c
VK
205#else /* __cplusplus */
206
207typedef void CSCPMessage;
98abc9f1 208typedef void NXCPEncryptionContext;
9f2ad16c
VK
209
210#endif
211
deaa4491
VK
212
213//
0d9c1b96
VK
214// Functions
215//
216
217#ifdef __cplusplus
0d9c1b96 218
9f2ad16c 219int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
e44ac467 220 CSCP_BUFFER *pBuffer, DWORD dwMaxMsgSize,
98abc9f1 221 NXCPEncryptionContext **ppCtx,
08043edf 222 BYTE *pDecryptionBuffer, DWORD dwTimeout);
b9b2b87b
VK
223int LIBNETXMS_EXPORTABLE RecvNXCPMessageEx(SOCKET hSocket, CSCP_MESSAGE **msgBuffer,
224 CSCP_BUFFER *nxcpBuffer, DWORD *bufferSize,
98abc9f1 225 NXCPEncryptionContext **ppCtx,
b9b2b87b
VK
226 BYTE **decryptionBuffer, DWORD dwTimeout,
227 DWORD maxMsgSize);
9f2ad16c 228CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, DWORD dwId, WORD wFlags,
b9b4ec17 229 DWORD dwDataSize, void *pData,
0d9c1b96 230 CSCP_MESSAGE *pBuffer);
9f2ad16c 231TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
a765d97c 232BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, DWORD dwId, const TCHAR *pszFile,
98abc9f1 233 NXCPEncryptionContext *pCtx, long offset,
7b8b337e
VK
234 void (* progressCallback)(INT64, void *), void *cbArg,
235 MUTEX mutex);
236BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion, MUTEX mutex);
0d9c1b96 237
9f2ad16c 238BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(DWORD dwEnabledCiphers);
98abc9f1
VK
239DWORD LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers();
240CSCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE *CSCPEncryptMessage(NXCPEncryptionContext *pCtx, CSCP_MESSAGE *pMsg);
241BOOL LIBNETXMS_EXPORTABLE CSCPDecryptMessage(NXCPEncryptionContext *pCtx,
e44ac467
VK
242 CSCP_ENCRYPTED_MESSAGE *pMsg,
243 BYTE *pDecryptionBuffer);
9f2ad16c 244DWORD LIBNETXMS_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
98abc9f1 245 NXCPEncryptionContext **ppCtx,
1a17feb0 246 CSCPMessage **ppResponse,
4bd3d6c5 247 RSA *pPrivateKey, int nNXCPVersion);
9e9d631e 248void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey, bool useX509Format);
3aa0061c 249RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile);
15cf5acc 250
bb8136f7
VK
251#ifdef _WIN32
252BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, DWORD dwMsgLen, const CERT_CONTEXT *pCert,
253 BYTE *pBuffer, DWORD dwBufSize, DWORD *pdwSigLen);
254#endif
255
0d9c1b96
VK
256#endif
257
9f2ad16c 258#endif /* _nxcpapi_h_ */