193bcf8b8f77af99d9dc540b472884ce9cfd4a8a
[public/netxms.git] / include / nxcpapi.h
1 /*
2 ** NetXMS - Network Management System
3 ** NXCP API
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published
8 ** by the Free Software Foundation; either version 3 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 Lesser 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 ** File: nxcpapi.h
21 **
22 **/
23
24 #ifndef _nxcpapi_h_
25 #define _nxcpapi_h_
26
27 #include <nms_threads.h>
28 #include <nms_util.h>
29
30 #ifdef _WIN32
31 #include <wincrypt.h>
32 #endif
33
34
35 //
36 // Temporary buffer structure for RecvCSCPMessage() function
37 //
38
39 typedef struct
40 {
41 DWORD dwBufSize;
42 DWORD dwBufPos;
43 char szBuffer[CSCP_TEMP_BUF_SIZE];
44 } CSCP_BUFFER;
45
46
47 #ifdef __cplusplus
48
49 //
50 // Class for holding CSCP messages
51 //
52
53 class LIBNETXMS_EXPORTABLE CSCPMessage
54 {
55 private:
56 WORD m_wCode;
57 WORD m_wFlags;
58 DWORD m_dwId;
59 DWORD m_dwNumVar; // Number of variables
60 CSCP_DF **m_ppVarList; // List of variables
61 int m_nVersion; // Protocol version
62
63 void *Set(DWORD dwVarId, BYTE bType, const void *pValue, DWORD dwSize = 0);
64 void *Get(DWORD dwVarId, BYTE bType);
65 DWORD FindVariable(DWORD dwVarId);
66
67 public:
68 CSCPMessage(int nVersion = NXCP_VERSION);
69 CSCPMessage(CSCPMessage *pMsg);
70 CSCPMessage(CSCP_MESSAGE *pMsg, int nVersion = NXCP_VERSION);
71 CSCPMessage(const char *xml);
72 ~CSCPMessage();
73
74 CSCP_MESSAGE *CreateMessage(void);
75 char *CreateXML(void);
76 void ProcessXMLToken(void *state, const char **attrs);
77 void ProcessXMLData(void *state);
78
79 WORD GetCode(void) { return m_wCode; }
80 void SetCode(WORD wCode) { m_wCode = wCode; }
81
82 DWORD GetId(void) { return m_dwId; }
83 void SetId(DWORD dwId) { m_dwId = dwId; }
84
85 BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
86 BOOL IsEndOfSequence(void) { return (m_wFlags & MF_END_OF_SEQUENCE) ? TRUE : FALSE; }
87 BOOL IsReverseOrder(void) { return (m_wFlags & MF_REVERSE_ORDER) ? TRUE : FALSE; }
88
89 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, CSCP_DT_INT16, &wValue); }
90 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
91 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, CSCP_DT_INT64, &qwValue); }
92 void SetVariable(DWORD dwVarId, double dValue) { Set(dwVarId, CSCP_DT_FLOAT, &dValue); }
93 void SetVariable(DWORD dwVarId, const TCHAR *pszValue) { Set(dwVarId, CSCP_DT_STRING, pszValue); }
94 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
95 void SetVariableToInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwData);
96 BOOL SetVariableFromFile(DWORD dwVarId, const TCHAR *pszFileName);
97
98 DWORD GetVariableLong(DWORD dwVarId);
99 QWORD GetVariableInt64(DWORD dwVarId);
100 WORD GetVariableShort(DWORD dwVarId);
101 LONG GetVariableShortAsInt32(DWORD dwVarId);
102 double GetVariableDouble(DWORD dwVarId);
103 TCHAR *GetVariableStr(DWORD dwVarId, TCHAR *szBuffer = NULL, DWORD dwBufSize = 0);
104 DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
105 DWORD GetVariableInt32Array(DWORD dwVarId, DWORD dwNumElements, DWORD *pdwBuffer);
106
107 void DeleteAllVariables(void);
108
109 void DisableEncryption(void) { m_wFlags |= MF_DONT_ENCRYPT; }
110 void SetEndOfSequence(void) { m_wFlags |= MF_END_OF_SEQUENCE; }
111 void SetReverseOrderFlag(void) { m_wFlags |= MF_REVERSE_ORDER; }
112 };
113
114
115 //
116 // Message waiting queue element structure
117 //
118
119 typedef 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
133 class LIBNETXMS_EXPORTABLE MsgWaitQueue
134 {
135 private:
136 MUTEX m_mutexDataAccess;
137 CONDITION m_condStop;
138 CONDITION m_condNewMsg;
139 DWORD m_dwMsgHoldTime;
140 DWORD m_dwNumElements;
141 WAIT_QUEUE_ELEMENT *m_pElements;
142 THREAD m_hHkThread;
143
144 void Lock(void) { MutexLock(m_mutexDataAccess, INFINITE); }
145 void Unlock(void) { MutexUnlock(m_mutexDataAccess); }
146 void HousekeeperThread(void);
147 void *WaitForMessageInternal(WORD wIsBinary, WORD wCode, DWORD dwId, DWORD dwTimeOut);
148
149 static THREAD_RESULT THREAD_CALL MWQThreadStarter(void *);
150
151 public:
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 #else /* __cplusplus */
171
172 typedef void CSCPMessage;
173
174 #endif
175
176
177 //
178 // Functions
179 //
180
181 #ifdef __cplusplus
182 extern "C" {
183 #endif
184
185 int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
186 CSCP_BUFFER *pBuffer, DWORD dwMaxMsgSize,
187 CSCP_ENCRYPTION_CONTEXT **ppCtx,
188 BYTE *pDecryptionBuffer, DWORD dwTimeout);
189 CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, DWORD dwId, WORD wFlags,
190 DWORD dwDataSize, void *pData,
191 CSCP_MESSAGE *pBuffer);
192 TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
193 BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, DWORD dwId, const TCHAR *pszFile,
194 CSCP_ENCRYPTION_CONTEXT *pCtx, long offset,
195 void (* progressCallback)(INT64, void *), void *cbArg);
196 BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion);
197
198 BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(DWORD dwEnabledCiphers);
199 DWORD LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers(void);
200 CSCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE
201 *CSCPEncryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx, CSCP_MESSAGE *pMsg);
202 BOOL LIBNETXMS_EXPORTABLE CSCPDecryptMessage(CSCP_ENCRYPTION_CONTEXT *pCtx,
203 CSCP_ENCRYPTED_MESSAGE *pMsg,
204 BYTE *pDecryptionBuffer);
205 DWORD LIBNETXMS_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
206 CSCP_ENCRYPTION_CONTEXT **ppCtx,
207 CSCPMessage **ppResponse,
208 RSA *pPrivateKey, int nNXCPVersion);
209 void LIBNETXMS_EXPORTABLE DestroyEncryptionContext(CSCP_ENCRYPTION_CONTEXT *pCtx);
210 void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey);
211 RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile);
212
213 #ifdef _WIN32
214 BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, DWORD dwMsgLen, const CERT_CONTEXT *pCert,
215 BYTE *pBuffer, DWORD dwBufSize, DWORD *pdwSigLen);
216 #endif
217
218 #ifdef __cplusplus
219 }
220 #endif
221
222 #endif /* _nxcpapi_h_ */