Minor changes
[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
8c290af0
VK
41//
42// Encryption methods
43//
44
45#define CSCP_ENCRYPTION_NONE 0
46#define CSCP_ENCRYPTION_BLOWFISH_SHA1 1
47
48
0d9c1b96
VK
49//
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
73
a7d07192 74 void *Set(DWORD dwVarId, BYTE bType, void *pValue, DWORD dwSize = 0);
0d9c1b96
VK
75 void *Get(DWORD dwVarId, BYTE bType);
76 DWORD FindVariable(DWORD dwVarId);
77
78public:
79 CSCPMessage();
30639d32 80 CSCPMessage(CSCPMessage *pMsg);
0d9c1b96
VK
81 CSCPMessage(CSCP_MESSAGE *pMsg);
82 ~CSCPMessage();
83
84 CSCP_MESSAGE *CreateMessage(void);
85
86 WORD GetCode(void) { return m_wCode; }
87 void SetCode(WORD wCode) { m_wCode = wCode; }
88
89 DWORD GetId(void) { return m_dwId; }
90 void SetId(DWORD dwId) { m_dwId = dwId; }
91
92 BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
23b8cd4f 93 BOOL IsEndOfSequence(void) { return (m_wFlags & MF_END_OF_SEQUENCE) ? TRUE : FALSE; }
30639d32 94 BOOL IsReverseOrder(void) { return (m_wFlags & MF_REVERSE_ORDER) ? TRUE : FALSE; }
0d9c1b96 95
42d7ed00
VK
96 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, CSCP_DT_INT16, &wValue); }
97 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
98 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, CSCP_DT_INT64, &qwValue); }
99 void SetVariable(DWORD dwVarId, double dValue) { Set(dwVarId, CSCP_DT_FLOAT, &dValue); }
cb27dff1 100 void SetVariable(DWORD dwVarId, TCHAR *pszValue) { Set(dwVarId, CSCP_DT_STRING, pszValue); }
42d7ed00 101 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
a7d07192 102 void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwData);
cb27dff1 103 BOOL SetVariableFromFile(DWORD dwVarId, TCHAR *pszFileName);
0d9c1b96
VK
104
105 DWORD GetVariableLong(DWORD dwVarId);
106 QWORD GetVariableInt64(DWORD dwVarId);
107 WORD GetVariableShort(DWORD dwVarId);
cc2d5b36 108 LONG GetVariableShortAsInt32(DWORD dwVarId);
f86ffdd2 109 double GetVariableDouble(DWORD dwVarId);
4332e9d8 110 TCHAR *GetVariableStr(DWORD dwVarId, TCHAR *szBuffer = 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
deaa4491
VK
122//
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
140class LIBNXCSCP_EXPORTABLE MsgWaitQueue
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
177
0d9c1b96
VK
178//
179// Functions
180//
181
182#ifdef __cplusplus
183extern "C" {
184#endif
185
e44ac467
VK
186int LIBNXCSCP_EXPORTABLE RecvCSCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
187 CSCP_BUFFER *pBuffer, DWORD dwMaxMsgSize,
1ba3c17a 188 CSCP_ENCRYPTION_CONTEXT **ppCtx,
08043edf 189 BYTE *pDecryptionBuffer, DWORD dwTimeout);
30639d32 190CSCP_MESSAGE LIBNXCSCP_EXPORTABLE *CreateRawCSCPMessage(WORD wCode, DWORD dwId, WORD wFlags,
b9b4ec17 191 DWORD dwDataSize, void *pData,
0d9c1b96 192 CSCP_MESSAGE *pBuffer);
4332e9d8 193TCHAR LIBNXCSCP_EXPORTABLE *CSCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
1ba3c17a
VK
194BOOL LIBNXCSCP_EXPORTABLE SendFileOverCSCP(SOCKET hSocket, DWORD dwId, TCHAR *pszFile,
195 CSCP_ENCRYPTION_CONTEXT *pCtx);
0d9c1b96 196
e44ac467 197BOOL LIBNXCSCP_EXPORTABLE InitCryptoLib(DWORD dwEnabledCiphers);
c14b66ca 198DWORD LIBNXCSCP_EXPORTABLE CSCPGetSupportedCiphers(void);
e44ac467
VK
199CSCP_ENCRYPTED_MESSAGE LIBNXCSCP_EXPORTABLE
200 *CSCPEncryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx, CSCP_MESSAGE *pMsg);
201BOOL LIBNXCSCP_EXPORTABLE CSCPDecryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx,
202 CSCP_ENCRYPTED_MESSAGE *pMsg,
203 BYTE *pDecryptionBuffer);
204DWORD LIBNXCSCP_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
205 CSCP_ENCRYPTION_CONTEXT **ppCtx,
1a17feb0 206 CSCPMessage **ppResponse,
e44ac467
VK
207 RSA *pPrivateKey);
208void LIBNXCSCP_EXPORTABLE DestroyEncryptionContext(CSCP_ENCRYPTION_CONTEXT *pCtx);
209void LIBNXCSCP_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey);
210RSA LIBNXCSCP_EXPORTABLE *LoadRSAKeys(TCHAR *pszKeyFile);
15cf5acc 211
0d9c1b96
VK
212#ifdef __cplusplus
213}
214#endif
215
216#endif /* _nxcscpapi_h_ */