encription enabled lib ready
[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;
69 DWORD m_dwId;
70 DWORD m_dwNumVar; // Number of variables
71 CSCP_DF **m_ppVarList; // List of variables
e44ac467 72 BOOL m_bDontEncrypt;
0d9c1b96 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();
80 CSCPMessage(CSCP_MESSAGE *pMsg);
81 ~CSCPMessage();
82
83 CSCP_MESSAGE *CreateMessage(void);
84
85 WORD GetCode(void) { return m_wCode; }
86 void SetCode(WORD wCode) { m_wCode = wCode; }
87
88 DWORD GetId(void) { return m_dwId; }
89 void SetId(DWORD dwId) { m_dwId = dwId; }
90
91 BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
92
42d7ed00
VK
93 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, CSCP_DT_INT16, &wValue); }
94 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
95 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, CSCP_DT_INT64, &qwValue); }
96 void SetVariable(DWORD dwVarId, double dValue) { Set(dwVarId, CSCP_DT_FLOAT, &dValue); }
4332e9d8 97 void SetVariable(DWORD dwVarId, TCHAR *szValue) { Set(dwVarId, CSCP_DT_STRING, szValue); }
42d7ed00 98 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
a7d07192 99 void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwData);
0d9c1b96
VK
100
101 DWORD GetVariableLong(DWORD dwVarId);
102 QWORD GetVariableInt64(DWORD dwVarId);
103 WORD GetVariableShort(DWORD dwVarId);
f86ffdd2 104 double GetVariableDouble(DWORD dwVarId);
4332e9d8 105 TCHAR *GetVariableStr(DWORD dwVarId, TCHAR *szBuffer = NULL, DWORD dwBufSize = 0);
0d9c1b96 106 DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
a7d07192 107 DWORD GetVariableInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwBuffer);
0d9c1b96
VK
108
109 void DeleteAllVariables(void);
e44ac467
VK
110
111 void DisableEncryption(void) { m_bDontEncrypt = TRUE; }
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
133class LIBNXCSCP_EXPORTABLE MsgWaitQueue
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
170
171//
0d9c1b96
VK
172// Functions
173//
174
175#ifdef __cplusplus
176extern "C" {
177#endif
178
e44ac467
VK
179int LIBNXCSCP_EXPORTABLE RecvCSCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
180 CSCP_BUFFER *pBuffer, DWORD dwMaxMsgSize,
1ba3c17a 181 CSCP_ENCRYPTION_CONTEXT **ppCtx,
e44ac467 182 BYTE *pDecryptionBuffer);
b9b4ec17
VK
183CSCP_MESSAGE LIBNXCSCP_EXPORTABLE *CreateRawCSCPMessage(WORD wCode, DWORD dwId,
184 DWORD dwDataSize, void *pData,
0d9c1b96 185 CSCP_MESSAGE *pBuffer);
4332e9d8 186TCHAR LIBNXCSCP_EXPORTABLE *CSCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
1ba3c17a
VK
187BOOL LIBNXCSCP_EXPORTABLE SendFileOverCSCP(SOCKET hSocket, DWORD dwId, TCHAR *pszFile,
188 CSCP_ENCRYPTION_CONTEXT *pCtx);
0d9c1b96 189
e44ac467 190BOOL LIBNXCSCP_EXPORTABLE InitCryptoLib(DWORD dwEnabledCiphers);
c14b66ca 191DWORD LIBNXCSCP_EXPORTABLE CSCPGetSupportedCiphers(void);
e44ac467
VK
192CSCP_ENCRYPTED_MESSAGE LIBNXCSCP_EXPORTABLE
193 *CSCPEncryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx, CSCP_MESSAGE *pMsg);
194BOOL LIBNXCSCP_EXPORTABLE CSCPDecryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx,
195 CSCP_ENCRYPTED_MESSAGE *pMsg,
196 BYTE *pDecryptionBuffer);
197DWORD LIBNXCSCP_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
198 CSCP_ENCRYPTION_CONTEXT **ppCtx,
199 CSCPMessage **ppResponce,
200 RSA *pPrivateKey);
201void LIBNXCSCP_EXPORTABLE DestroyEncryptionContext(CSCP_ENCRYPTION_CONTEXT *pCtx);
202void LIBNXCSCP_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey);
203RSA LIBNXCSCP_EXPORTABLE *LoadRSAKeys(TCHAR *pszKeyFile);
15cf5acc 204
0d9c1b96
VK
205#ifdef __cplusplus
206}
207#endif
208
209#endif /* _nxcscpapi_h_ */