- Performance view improved
[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
VK
29
30
31//
0d9c1b96
VK
32// Temporary buffer structure for RecvCSCPMessage() function
33//
34
35typedef struct
36{
37 DWORD dwBufSize;
38 DWORD dwBufPos;
39 char szBuffer[CSCP_TEMP_BUF_SIZE];
40} CSCP_BUFFER;
41
42
9f2ad16c
VK
43#ifdef __cplusplus
44
0d9c1b96
VK
45//
46// Class for holding CSCP messages
47//
48
9f2ad16c 49class LIBNETXMS_EXPORTABLE CSCPMessage
0d9c1b96
VK
50{
51private:
52 WORD m_wCode;
23b8cd4f 53 WORD m_wFlags;
0d9c1b96
VK
54 DWORD m_dwId;
55 DWORD m_dwNumVar; // Number of variables
56 CSCP_DF **m_ppVarList; // List of variables
4bd3d6c5 57 int m_nVersion; // Protocol version
0d9c1b96 58
a7d07192 59 void *Set(DWORD dwVarId, BYTE bType, void *pValue, DWORD dwSize = 0);
0d9c1b96
VK
60 void *Get(DWORD dwVarId, BYTE bType);
61 DWORD FindVariable(DWORD dwVarId);
62
63public:
4bd3d6c5 64 CSCPMessage(int nVersion = NXCP_VERSION);
30639d32 65 CSCPMessage(CSCPMessage *pMsg);
4bd3d6c5 66 CSCPMessage(CSCP_MESSAGE *pMsg, int nVersion = NXCP_VERSION);
0d9c1b96
VK
67 ~CSCPMessage();
68
69 CSCP_MESSAGE *CreateMessage(void);
70
71 WORD GetCode(void) { return m_wCode; }
72 void SetCode(WORD wCode) { m_wCode = wCode; }
73
74 DWORD GetId(void) { return m_dwId; }
75 void SetId(DWORD dwId) { m_dwId = dwId; }
76
77 BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
23b8cd4f 78 BOOL IsEndOfSequence(void) { return (m_wFlags & MF_END_OF_SEQUENCE) ? TRUE : FALSE; }
30639d32 79 BOOL IsReverseOrder(void) { return (m_wFlags & MF_REVERSE_ORDER) ? TRUE : FALSE; }
0d9c1b96 80
42d7ed00
VK
81 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, CSCP_DT_INT16, &wValue); }
82 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
83 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, CSCP_DT_INT64, &qwValue); }
84 void SetVariable(DWORD dwVarId, double dValue) { Set(dwVarId, CSCP_DT_FLOAT, &dValue); }
cb27dff1 85 void SetVariable(DWORD dwVarId, TCHAR *pszValue) { Set(dwVarId, CSCP_DT_STRING, pszValue); }
42d7ed00 86 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
a7d07192 87 void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwData);
cb27dff1 88 BOOL SetVariableFromFile(DWORD dwVarId, TCHAR *pszFileName);
0d9c1b96
VK
89
90 DWORD GetVariableLong(DWORD dwVarId);
91 QWORD GetVariableInt64(DWORD dwVarId);
92 WORD GetVariableShort(DWORD dwVarId);
cc2d5b36 93 LONG GetVariableShortAsInt32(DWORD dwVarId);
f86ffdd2 94 double GetVariableDouble(DWORD dwVarId);
4332e9d8 95 TCHAR *GetVariableStr(DWORD dwVarId, TCHAR *szBuffer = NULL, DWORD dwBufSize = 0);
0d9c1b96 96 DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
a7d07192 97 DWORD GetVariableInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwBuffer);
0d9c1b96
VK
98
99 void DeleteAllVariables(void);
e44ac467 100
23b8cd4f
VK
101 void DisableEncryption(void) { m_wFlags |= MF_DONT_ENCRYPT; }
102 void SetEndOfSequence(void) { m_wFlags |= MF_END_OF_SEQUENCE; }
30639d32 103 void SetReverseOrderFlag(void) { m_wFlags |= MF_REVERSE_ORDER; }
0d9c1b96
VK
104};
105
106
107//
deaa4491
VK
108// Message waiting queue element structure
109//
110
111typedef struct
112{
113 WORD wCode; // Message code
114 WORD wIsBinary; // 1 for binary (raw) messages
115 DWORD dwId; // Message ID
116 DWORD dwTTL; // Message time-to-live in milliseconds
117 void *pMsg; // Pointer to message, either to CSCPMessage object or raw message
118} WAIT_QUEUE_ELEMENT;
119
120
121//
122// Message waiting queue class
123//
124
9f2ad16c 125class LIBNETXMS_EXPORTABLE MsgWaitQueue
deaa4491 126{
deaa4491 127private:
5f743326
VK
128 MUTEX m_mutexDataAccess;
129 CONDITION m_condStop;
130 CONDITION m_condNewMsg;
deaa4491
VK
131 DWORD m_dwMsgHoldTime;
132 DWORD m_dwNumElements;
133 WAIT_QUEUE_ELEMENT *m_pElements;
ccdbbb52 134 THREAD m_hHkThread;
deaa4491 135
5f743326
VK
136 void Lock(void) { MutexLock(m_mutexDataAccess, INFINITE); }
137 void Unlock(void) { MutexUnlock(m_mutexDataAccess); }
deaa4491
VK
138 void HousekeeperThread(void);
139 void *WaitForMessageInternal(WORD wIsBinary, WORD wCode, DWORD dwId, DWORD dwTimeOut);
1ba9a162 140
ccdbbb52 141 static THREAD_RESULT THREAD_CALL MWQThreadStarter(void *);
deaa4491
VK
142
143public:
144 MsgWaitQueue();
145 ~MsgWaitQueue();
146
147 void Put(CSCPMessage *pMsg);
148 void Put(CSCP_MESSAGE *pMsg);
149 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
150 {
151 return (CSCPMessage *)WaitForMessageInternal(0, wCode, dwId, dwTimeOut);
152 }
153 CSCP_MESSAGE *WaitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
154 {
155 return (CSCP_MESSAGE *)WaitForMessageInternal(1, wCode, dwId, dwTimeOut);
156 }
157
158 void Clear(void);
159 void SetHoldTime(DWORD dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
160};
161
9f2ad16c
VK
162#else /* __cplusplus */
163
164typedef void CSCPMessage;
165
166#endif
167
deaa4491
VK
168
169//
0d9c1b96
VK
170// Functions
171//
172
173#ifdef __cplusplus
174extern "C" {
175#endif
176
9f2ad16c 177int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
e44ac467 178 CSCP_BUFFER *pBuffer, DWORD dwMaxMsgSize,
1ba3c17a 179 CSCP_ENCRYPTION_CONTEXT **ppCtx,
08043edf 180 BYTE *pDecryptionBuffer, DWORD dwTimeout);
9f2ad16c 181CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, DWORD dwId, WORD wFlags,
b9b4ec17 182 DWORD dwDataSize, void *pData,
0d9c1b96 183 CSCP_MESSAGE *pBuffer);
9f2ad16c
VK
184TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
185BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, DWORD dwId, TCHAR *pszFile,
1ba3c17a 186 CSCP_ENCRYPTION_CONTEXT *pCtx);
9f2ad16c 187BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion);
0d9c1b96 188
9f2ad16c
VK
189BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(DWORD dwEnabledCiphers);
190DWORD LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers(void);
191CSCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE
e44ac467 192 *CSCPEncryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx, CSCP_MESSAGE *pMsg);
9f2ad16c 193BOOL LIBNETXMS_EXPORTABLE CSCPDecryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx,
e44ac467
VK
194 CSCP_ENCRYPTED_MESSAGE *pMsg,
195 BYTE *pDecryptionBuffer);
9f2ad16c 196DWORD LIBNETXMS_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
e44ac467 197 CSCP_ENCRYPTION_CONTEXT **ppCtx,
1a17feb0 198 CSCPMessage **ppResponse,
4bd3d6c5 199 RSA *pPrivateKey, int nNXCPVersion);
9f2ad16c
VK
200void LIBNETXMS_EXPORTABLE DestroyEncryptionContext(CSCP_ENCRYPTION_CONTEXT *pCtx);
201void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey);
202RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(TCHAR *pszKeyFile);
15cf5acc 203
0d9c1b96
VK
204#ifdef __cplusplus
205}
206#endif
207
9f2ad16c 208#endif /* _nxcpapi_h_ */