Finished Implementation of agent traps and added trap sending API for subagents
[public/netxms.git] / include / nxcscpapi.h
CommitLineData
0d9c1b96
VK
1/*
2** NetXMS - Network Management System
3** CSCP API Library
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**
20** $module: nxcscpapi.h
21**
22**/
23
24#ifndef _nxcscpapi_h_
25#define _nxcscpapi_h_
26
b50f1100
VK
27#include <nms_threads.h>
28
0d9c1b96
VK
29
30#ifdef _WIN32
31#ifdef LIBNXCSCP_EXPORTS
32#define LIBNXCSCP_EXPORTABLE __declspec(dllexport)
33#else
34#define LIBNXCSCP_EXPORTABLE __declspec(dllimport)
35#endif
36#else /* _WIN32 */
37#define LIBNXCSCP_EXPORTABLE
38#endif
39
40
41//
8c290af0
VK
42// Encryption methods
43//
44
45#define CSCP_ENCRYPTION_NONE 0
46#define CSCP_ENCRYPTION_BLOWFISH_SHA1 1
47
48
49//
0d9c1b96
VK
50// Temporary buffer structure for RecvCSCPMessage() function
51//
52
53typedef struct
54{
55 DWORD dwBufSize;
56 DWORD dwBufPos;
57 char szBuffer[CSCP_TEMP_BUF_SIZE];
58} CSCP_BUFFER;
59
60
61//
62// Class for holding CSCP messages
63//
64
65class LIBNXCSCP_EXPORTABLE CSCPMessage
66{
67private:
68 WORD m_wCode;
23b8cd4f 69 WORD m_wFlags;
0d9c1b96
VK
70 DWORD m_dwId;
71 DWORD m_dwNumVar; // Number of variables
72 CSCP_DF **m_ppVarList; // List of variables
4bd3d6c5 73 int m_nVersion; // Protocol version
0d9c1b96 74
a7d07192 75 void *Set(DWORD dwVarId, BYTE bType, void *pValue, DWORD dwSize = 0);
0d9c1b96
VK
76 void *Get(DWORD dwVarId, BYTE bType);
77 DWORD FindVariable(DWORD dwVarId);
78
79public:
4bd3d6c5 80 CSCPMessage(int nVersion = NXCP_VERSION);
30639d32 81 CSCPMessage(CSCPMessage *pMsg);
4bd3d6c5 82 CSCPMessage(CSCP_MESSAGE *pMsg, int nVersion = NXCP_VERSION);
0d9c1b96
VK
83 ~CSCPMessage();
84
85 CSCP_MESSAGE *CreateMessage(void);
86
87 WORD GetCode(void) { return m_wCode; }
88 void SetCode(WORD wCode) { m_wCode = wCode; }
89
90 DWORD GetId(void) { return m_dwId; }
91 void SetId(DWORD dwId) { m_dwId = dwId; }
92
93 BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
23b8cd4f 94 BOOL IsEndOfSequence(void) { return (m_wFlags & MF_END_OF_SEQUENCE) ? TRUE : FALSE; }
30639d32 95 BOOL IsReverseOrder(void) { return (m_wFlags & MF_REVERSE_ORDER) ? TRUE : FALSE; }
0d9c1b96 96
42d7ed00
VK
97 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, CSCP_DT_INT16, &wValue); }
98 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
99 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, CSCP_DT_INT64, &qwValue); }
100 void SetVariable(DWORD dwVarId, double dValue) { Set(dwVarId, CSCP_DT_FLOAT, &dValue); }
cb27dff1 101 void SetVariable(DWORD dwVarId, TCHAR *pszValue) { Set(dwVarId, CSCP_DT_STRING, pszValue); }
42d7ed00 102 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
a7d07192 103 void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwData);
cb27dff1 104 BOOL SetVariableFromFile(DWORD dwVarId, TCHAR *pszFileName);
0d9c1b96
VK
105
106 DWORD GetVariableLong(DWORD dwVarId);
107 QWORD GetVariableInt64(DWORD dwVarId);
108 WORD GetVariableShort(DWORD dwVarId);
cc2d5b36 109 LONG GetVariableShortAsInt32(DWORD dwVarId);
f86ffdd2 110 double GetVariableDouble(DWORD dwVarId);
4332e9d8 111 TCHAR *GetVariableStr(DWORD dwVarId, TCHAR *szBuffer = 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
141class LIBNXCSCP_EXPORTABLE MsgWaitQueue
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
5f743326
VK
152 void Lock(void) { MutexLock(m_mutexDataAccess, INFINITE); }
153 void Unlock(void) { MutexUnlock(m_mutexDataAccess); }
deaa4491
VK
154 void HousekeeperThread(void);
155 void *WaitForMessageInternal(WORD wIsBinary, WORD wCode, DWORD dwId, DWORD dwTimeOut);
1ba9a162 156
ccdbbb52 157 static THREAD_RESULT THREAD_CALL MWQThreadStarter(void *);
deaa4491
VK
158
159public:
160 MsgWaitQueue();
161 ~MsgWaitQueue();
162
163 void Put(CSCPMessage *pMsg);
164 void Put(CSCP_MESSAGE *pMsg);
165 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
166 {
167 return (CSCPMessage *)WaitForMessageInternal(0, wCode, dwId, dwTimeOut);
168 }
169 CSCP_MESSAGE *WaitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut)
170 {
171 return (CSCP_MESSAGE *)WaitForMessageInternal(1, wCode, dwId, dwTimeOut);
172 }
173
174 void Clear(void);
175 void SetHoldTime(DWORD dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
176};
177
178
179//
0d9c1b96
VK
180// Functions
181//
182
183#ifdef __cplusplus
184extern "C" {
185#endif
186
4bd3d6c5 187int LIBNXCSCP_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
e44ac467 188 CSCP_BUFFER *pBuffer, DWORD dwMaxMsgSize,
1ba3c17a 189 CSCP_ENCRYPTION_CONTEXT **ppCtx,
08043edf 190 BYTE *pDecryptionBuffer, DWORD dwTimeout);
4bd3d6c5 191CSCP_MESSAGE LIBNXCSCP_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, DWORD dwId, WORD wFlags,
b9b4ec17 192 DWORD dwDataSize, void *pData,
0d9c1b96 193 CSCP_MESSAGE *pBuffer);
4bd3d6c5
VK
194TCHAR LIBNXCSCP_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
195BOOL LIBNXCSCP_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, DWORD dwId, TCHAR *pszFile,
1ba3c17a 196 CSCP_ENCRYPTION_CONTEXT *pCtx);
4bd3d6c5 197BOOL LIBNXCSCP_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion);
0d9c1b96 198
e44ac467 199BOOL LIBNXCSCP_EXPORTABLE InitCryptoLib(DWORD dwEnabledCiphers);
c14b66ca 200DWORD LIBNXCSCP_EXPORTABLE CSCPGetSupportedCiphers(void);
e44ac467
VK
201CSCP_ENCRYPTED_MESSAGE LIBNXCSCP_EXPORTABLE
202 *CSCPEncryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx, CSCP_MESSAGE *pMsg);
203BOOL LIBNXCSCP_EXPORTABLE CSCPDecryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx,
204 CSCP_ENCRYPTED_MESSAGE *pMsg,
205 BYTE *pDecryptionBuffer);
206DWORD LIBNXCSCP_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
207 CSCP_ENCRYPTION_CONTEXT **ppCtx,
1a17feb0 208 CSCPMessage **ppResponse,
4bd3d6c5 209 RSA *pPrivateKey, int nNXCPVersion);
e44ac467
VK
210void LIBNXCSCP_EXPORTABLE DestroyEncryptionContext(CSCP_ENCRYPTION_CONTEXT *pCtx);
211void LIBNXCSCP_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey);
212RSA LIBNXCSCP_EXPORTABLE *LoadRSAKeys(TCHAR *pszKeyFile);
15cf5acc 213
0d9c1b96
VK
214#ifdef __cplusplus
215}
216#endif
217
218#endif /* _nxcscpapi_h_ */