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