preparation for 1.1.6 release
[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); }
42d7ed00 94 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
35f836fe
VK
95#ifdef UNICODE
96 void SetVariableFromMBString(DWORD dwVarId, const char *pszValue);
97#else
98 void SetVariableFromMBString(DWORD dwVarId, const char *pszValue) { Set(dwVarId, CSCP_DT_STRING, pszValue); }
99#endif
a7d07192 100 void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwData);
3aa0061c 101 BOOL SetVariableFromFile(DWORD dwVarId, const TCHAR *pszFileName);
0d9c1b96
VK
102
103 DWORD GetVariableLong(DWORD dwVarId);
104 QWORD GetVariableInt64(DWORD dwVarId);
105 WORD GetVariableShort(DWORD dwVarId);
cc2d5b36 106 LONG GetVariableShortAsInt32(DWORD dwVarId);
f86ffdd2 107 double GetVariableDouble(DWORD dwVarId);
4332e9d8 108 TCHAR *GetVariableStr(DWORD dwVarId, TCHAR *szBuffer = NULL, DWORD dwBufSize = 0);
35f836fe
VK
109 char *GetVariableStrA(DWORD dwVarId, char *pszBuffer = NULL, DWORD dwBufSize = 0);
110 char *GetVariableStrUTF8(DWORD dwVarId, char *pszBuffer = NULL, DWORD dwBufSize = 0);
0d9c1b96 111 DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
a7d07192 112 DWORD GetVariableInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwBuffer);
0d9c1b96
VK
113
114 void DeleteAllVariables(void);
e44ac467 115
23b8cd4f
VK
116 void DisableEncryption(void) { m_wFlags |= MF_DONT_ENCRYPT; }
117 void SetEndOfSequence(void) { m_wFlags |= MF_END_OF_SEQUENCE; }
30639d32 118 void SetReverseOrderFlag(void) { m_wFlags |= MF_REVERSE_ORDER; }
0d9c1b96
VK
119};
120
121
122//
deaa4491
VK
123// Message waiting queue element structure
124//
125
126typedef struct
127{
128 WORD wCode; // Message code
129 WORD wIsBinary; // 1 for binary (raw) messages
130 DWORD dwId; // Message ID
131 DWORD dwTTL; // Message time-to-live in milliseconds
132 void *pMsg; // Pointer to message, either to CSCPMessage object or raw message
133} WAIT_QUEUE_ELEMENT;
134
135
136//
137// Message waiting queue class
138//
139
9f2ad16c 140class LIBNETXMS_EXPORTABLE MsgWaitQueue
deaa4491 141{
deaa4491 142private:
5f743326
VK
143 MUTEX m_mutexDataAccess;
144 CONDITION m_condStop;
145 CONDITION m_condNewMsg;
deaa4491
VK
146 DWORD m_dwMsgHoldTime;
147 DWORD m_dwNumElements;
148 WAIT_QUEUE_ELEMENT *m_pElements;
ccdbbb52 149 THREAD m_hHkThread;
deaa4491 150
5f743326
VK
151 void Lock(void) { MutexLock(m_mutexDataAccess, INFINITE); }
152 void Unlock(void) { MutexUnlock(m_mutexDataAccess); }
deaa4491
VK
153 void HousekeeperThread(void);
154 void *WaitForMessageInternal(WORD wIsBinary, WORD wCode, DWORD dwId, DWORD dwTimeOut);
1ba9a162 155
ccdbbb52 156 static THREAD_RESULT THREAD_CALL MWQThreadStarter(void *);
deaa4491
VK
157
158public:
159 MsgWaitQueue();
160 ~MsgWaitQueue();
161
162 void Put(CSCPMessage *pMsg);
163 void Put(CSCP_MESSAGE *pMsg);
164 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
165 {
166 return (CSCPMessage *)WaitForMessageInternal(0, wCode, dwId, dwTimeOut);
167 }
168 CSCP_MESSAGE *WaitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
169 {
170 return (CSCP_MESSAGE *)WaitForMessageInternal(1, wCode, dwId, dwTimeOut);
171 }
172
173 void Clear(void);
174 void SetHoldTime(DWORD dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
175};
176
9f2ad16c
VK
177#else /* __cplusplus */
178
179typedef void CSCPMessage;
180
181#endif
182
deaa4491
VK
183
184//
0d9c1b96
VK
185// Functions
186//
187
188#ifdef __cplusplus
0d9c1b96 189
9f2ad16c 190int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
e44ac467 191 CSCP_BUFFER *pBuffer, DWORD dwMaxMsgSize,
1ba3c17a 192 CSCP_ENCRYPTION_CONTEXT **ppCtx,
08043edf 193 BYTE *pDecryptionBuffer, DWORD dwTimeout);
b9b2b87b
VK
194int LIBNETXMS_EXPORTABLE RecvNXCPMessageEx(SOCKET hSocket, CSCP_MESSAGE **msgBuffer,
195 CSCP_BUFFER *nxcpBuffer, DWORD *bufferSize,
196 CSCP_ENCRYPTION_CONTEXT **ppCtx,
197 BYTE **decryptionBuffer, DWORD dwTimeout,
198 DWORD maxMsgSize);
9f2ad16c 199CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, DWORD dwId, WORD wFlags,
b9b4ec17 200 DWORD dwDataSize, void *pData,
0d9c1b96 201 CSCP_MESSAGE *pBuffer);
9f2ad16c 202TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
a765d97c 203BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, DWORD dwId, const TCHAR *pszFile,
6173bea8 204 CSCP_ENCRYPTION_CONTEXT *pCtx, long offset,
7b8b337e
VK
205 void (* progressCallback)(INT64, void *), void *cbArg,
206 MUTEX mutex);
207BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion, MUTEX mutex);
0d9c1b96 208
9f2ad16c
VK
209BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(DWORD dwEnabledCiphers);
210DWORD LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers(void);
211CSCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE
e44ac467 212 *CSCPEncryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx, CSCP_MESSAGE *pMsg);
9f2ad16c 213BOOL LIBNETXMS_EXPORTABLE CSCPDecryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx,
e44ac467
VK
214 CSCP_ENCRYPTED_MESSAGE *pMsg,
215 BYTE *pDecryptionBuffer);
9f2ad16c 216DWORD LIBNETXMS_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
e44ac467 217 CSCP_ENCRYPTION_CONTEXT **ppCtx,
1a17feb0 218 CSCPMessage **ppResponse,
4bd3d6c5 219 RSA *pPrivateKey, int nNXCPVersion);
9f2ad16c
VK
220void LIBNETXMS_EXPORTABLE DestroyEncryptionContext(CSCP_ENCRYPTION_CONTEXT *pCtx);
221void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey);
3aa0061c 222RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile);
15cf5acc 223
bb8136f7
VK
224#ifdef _WIN32
225BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, DWORD dwMsgLen, const CERT_CONTEXT *pCert,
226 BYTE *pBuffer, DWORD dwBufSize, DWORD *pdwSigLen);
227#endif
228
0d9c1b96
VK
229#endif
230
9f2ad16c 231#endif /* _nxcpapi_h_ */