Fixed some issues with char* -> const char* conversion
[public/netxms.git] / include / nxcpapi.h
CommitLineData
0d9c1b96
VK
1/*
2** NetXMS - Network Management System
9f2ad16c 3** NXCP API
0d9c1b96
VK
4** Copyright (C) 2003, 2004 Victor Kirhenshtein
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
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**
16** You should have received a copy of the GNU General Public License
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
a7d07192 63 void *Set(DWORD dwVarId, BYTE bType, 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);
0d9c1b96
VK
71 ~CSCPMessage();
72
73 CSCP_MESSAGE *CreateMessage(void);
74
75 WORD GetCode(void) { return m_wCode; }
76 void SetCode(WORD wCode) { m_wCode = wCode; }
77
78 DWORD GetId(void) { return m_dwId; }
79 void SetId(DWORD dwId) { m_dwId = dwId; }
80
81 BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
23b8cd4f 82 BOOL IsEndOfSequence(void) { return (m_wFlags & MF_END_OF_SEQUENCE) ? TRUE : FALSE; }
30639d32 83 BOOL IsReverseOrder(void) { return (m_wFlags & MF_REVERSE_ORDER) ? TRUE : FALSE; }
0d9c1b96 84
42d7ed00
VK
85 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, CSCP_DT_INT16, &wValue); }
86 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
87 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, CSCP_DT_INT64, &qwValue); }
88 void SetVariable(DWORD dwVarId, double dValue) { Set(dwVarId, CSCP_DT_FLOAT, &dValue); }
cb27dff1 89 void SetVariable(DWORD dwVarId, TCHAR *pszValue) { Set(dwVarId, CSCP_DT_STRING, pszValue); }
42d7ed00 90 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
a7d07192 91 void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwData);
cb27dff1 92 BOOL SetVariableFromFile(DWORD dwVarId, TCHAR *pszFileName);
0d9c1b96
VK
93
94 DWORD GetVariableLong(DWORD dwVarId);
95 QWORD GetVariableInt64(DWORD dwVarId);
96 WORD GetVariableShort(DWORD dwVarId);
cc2d5b36 97 LONG GetVariableShortAsInt32(DWORD dwVarId);
f86ffdd2 98 double GetVariableDouble(DWORD dwVarId);
4332e9d8 99 TCHAR *GetVariableStr(DWORD dwVarId, TCHAR *szBuffer = NULL, DWORD dwBufSize = 0);
0d9c1b96 100 DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
a7d07192 101 DWORD GetVariableInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwBuffer);
0d9c1b96
VK
102
103 void DeleteAllVariables(void);
e44ac467 104
23b8cd4f
VK
105 void DisableEncryption(void) { m_wFlags |= MF_DONT_ENCRYPT; }
106 void SetEndOfSequence(void) { m_wFlags |= MF_END_OF_SEQUENCE; }
30639d32 107 void SetReverseOrderFlag(void) { m_wFlags |= MF_REVERSE_ORDER; }
0d9c1b96
VK
108};
109
110
111//
deaa4491
VK
112// Message waiting queue element structure
113//
114
115typedef struct
116{
117 WORD wCode; // Message code
118 WORD wIsBinary; // 1 for binary (raw) messages
119 DWORD dwId; // Message ID
120 DWORD dwTTL; // Message time-to-live in milliseconds
121 void *pMsg; // Pointer to message, either to CSCPMessage object or raw message
122} WAIT_QUEUE_ELEMENT;
123
124
125//
126// Message waiting queue class
127//
128
9f2ad16c 129class LIBNETXMS_EXPORTABLE MsgWaitQueue
deaa4491 130{
deaa4491 131private:
5f743326
VK
132 MUTEX m_mutexDataAccess;
133 CONDITION m_condStop;
134 CONDITION m_condNewMsg;
deaa4491
VK
135 DWORD m_dwMsgHoldTime;
136 DWORD m_dwNumElements;
137 WAIT_QUEUE_ELEMENT *m_pElements;
ccdbbb52 138 THREAD m_hHkThread;
deaa4491 139
5f743326
VK
140 void Lock(void) { MutexLock(m_mutexDataAccess, INFINITE); }
141 void Unlock(void) { MutexUnlock(m_mutexDataAccess); }
deaa4491
VK
142 void HousekeeperThread(void);
143 void *WaitForMessageInternal(WORD wIsBinary, WORD wCode, DWORD dwId, DWORD dwTimeOut);
1ba9a162 144
ccdbbb52 145 static THREAD_RESULT THREAD_CALL MWQThreadStarter(void *);
deaa4491
VK
146
147public:
148 MsgWaitQueue();
149 ~MsgWaitQueue();
150
151 void Put(CSCPMessage *pMsg);
152 void Put(CSCP_MESSAGE *pMsg);
153 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
154 {
155 return (CSCPMessage *)WaitForMessageInternal(0, wCode, dwId, dwTimeOut);
156 }
157 CSCP_MESSAGE *WaitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
158 {
159 return (CSCP_MESSAGE *)WaitForMessageInternal(1, wCode, dwId, dwTimeOut);
160 }
161
162 void Clear(void);
163 void SetHoldTime(DWORD dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
164};
165
9f2ad16c
VK
166#else /* __cplusplus */
167
168typedef void CSCPMessage;
169
170#endif
171
deaa4491
VK
172
173//
0d9c1b96
VK
174// Functions
175//
176
177#ifdef __cplusplus
178extern "C" {
179#endif
180
9f2ad16c 181int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
e44ac467 182 CSCP_BUFFER *pBuffer, DWORD dwMaxMsgSize,
1ba3c17a 183 CSCP_ENCRYPTION_CONTEXT **ppCtx,
08043edf 184 BYTE *pDecryptionBuffer, DWORD dwTimeout);
9f2ad16c 185CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, DWORD dwId, WORD wFlags,
b9b4ec17 186 DWORD dwDataSize, void *pData,
0d9c1b96 187 CSCP_MESSAGE *pBuffer);
9f2ad16c
VK
188TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
189BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, DWORD dwId, TCHAR *pszFile,
1ba3c17a 190 CSCP_ENCRYPTION_CONTEXT *pCtx);
9f2ad16c 191BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion);
0d9c1b96 192
9f2ad16c
VK
193BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(DWORD dwEnabledCiphers);
194DWORD LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers(void);
195CSCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE
e44ac467 196 *CSCPEncryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx, CSCP_MESSAGE *pMsg);
9f2ad16c 197BOOL LIBNETXMS_EXPORTABLE CSCPDecryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx,
e44ac467
VK
198 CSCP_ENCRYPTED_MESSAGE *pMsg,
199 BYTE *pDecryptionBuffer);
9f2ad16c 200DWORD LIBNETXMS_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
e44ac467 201 CSCP_ENCRYPTION_CONTEXT **ppCtx,
1a17feb0 202 CSCPMessage **ppResponse,
4bd3d6c5 203 RSA *pPrivateKey, int nNXCPVersion);
9f2ad16c
VK
204void LIBNETXMS_EXPORTABLE DestroyEncryptionContext(CSCP_ENCRYPTION_CONTEXT *pCtx);
205void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey);
206RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(TCHAR *pszKeyFile);
15cf5acc 207
bb8136f7
VK
208#ifdef _WIN32
209BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, DWORD dwMsgLen, const CERT_CONTEXT *pCert,
210 BYTE *pBuffer, DWORD dwBufSize, DWORD *pdwSigLen);
211#endif
212
0d9c1b96
VK
213#ifdef __cplusplus
214}
215#endif
216
9f2ad16c 217#endif /* _nxcpapi_h_ */