more function and examples for embedded agent
[public/netxms.git] / include / nxcpapi.h
CommitLineData
9fa031cd 1/*
0d9c1b96 2** NetXMS - Network Management System
9f2ad16c 3** NXCP API
68f384ea 4** Copyright (C) 2003-2010 Victor Kirhenshtein
0d9c1b96
VK
5**
6** This program is free software; you can redistribute it and/or modify
65d2c384
VK
7** it under the terms of the GNU Lesser General Public License as published by
8** the Free Software Foundation; either version 3 of the License, or
0d9c1b96
VK
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**
68f384ea 16** You should have received a copy of the GNU Lesser General Public License
0d9c1b96
VK
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 29
bb8136f7
VK
30#ifdef _WIN32
31#include <wincrypt.h>
32#endif
33
4af351c7
VK
34/**
35 * Temporary buffer structure for RecvCSCPMessage() function
36 */
0d9c1b96
VK
37typedef struct
38{
967893bb
VK
39 UINT32 dwBufSize;
40 UINT32 dwBufPos;
0d9c1b96
VK
41 char szBuffer[CSCP_TEMP_BUF_SIZE];
42} CSCP_BUFFER;
43
44
9f2ad16c
VK
45#ifdef __cplusplus
46
5c44534b
VK
47struct MessageField;
48
4af351c7
VK
49/**
50 * Parsed NXCP message
51 */
9f2ad16c 52class LIBNETXMS_EXPORTABLE CSCPMessage
0d9c1b96
VK
53{
54private:
5c44534b
VK
55 WORD m_code;
56 WORD m_flags;
57 UINT32 m_id;
58 MessageField *m_fields; // Message fields
59 int m_version; // Protocol version
0d9c1b96 60
5c44534b
VK
61 void *set(UINT32 fieldId, BYTE type, const void *value, UINT32 size = 0);
62 void *get(UINT32 fieldId, BYTE type);
63 CSCP_DF *find(UINT32 fieldId);
0d9c1b96
VK
64
65public:
4bd3d6c5 66 CSCPMessage(int nVersion = NXCP_VERSION);
30639d32 67 CSCPMessage(CSCPMessage *pMsg);
4bd3d6c5 68 CSCPMessage(CSCP_MESSAGE *pMsg, int nVersion = NXCP_VERSION);
ff175e91 69 CSCPMessage(const char *xml);
0d9c1b96
VK
70 ~CSCPMessage();
71
5c44534b 72 CSCP_MESSAGE *createMessage();
4af351c7
VK
73 char *createXML();
74 void processXMLToken(void *state, const char **attrs);
75 void processXMLData(void *state);
0d9c1b96 76
5c44534b
VK
77 WORD GetCode() { return m_code; }
78 void SetCode(WORD code) { m_code = code; }
0d9c1b96 79
5c44534b
VK
80 UINT32 GetId() { return m_id; }
81 void SetId(UINT32 id) { m_id = id; }
0d9c1b96 82
5c44534b
VK
83 bool isFieldExist(UINT32 varId) { return find(varId) != NULL; }
84 bool isEndOfSequence() { return (m_flags & MF_END_OF_SEQUENCE) ? true : false; }
85 bool isReverseOrder() { return (m_flags & MF_REVERSE_ORDER) ? true : false; }
4af351c7
VK
86
87 void SetVariable(UINT32 dwVarId, INT16 wValue) { set(dwVarId, CSCP_DT_INT16, &wValue); }
88 void SetVariable(UINT32 dwVarId, UINT16 wValue) { set(dwVarId, CSCP_DT_INT16, &wValue); }
89 void SetVariable(UINT32 dwVarId, INT32 dwValue) { set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
90 void SetVariable(UINT32 dwVarId, UINT32 dwValue) { set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
91 void SetVariable(UINT32 dwVarId, INT64 qwValue) { set(dwVarId, CSCP_DT_INT64, &qwValue); }
92 void SetVariable(UINT32 dwVarId, UINT64 qwValue) { set(dwVarId, CSCP_DT_INT64, &qwValue); }
93 void SetVariable(UINT32 dwVarId, double dValue) { set(dwVarId, CSCP_DT_FLOAT, &dValue); }
94 void SetVariable(UINT32 dwVarId, const TCHAR *value) { if (value != NULL) set(dwVarId, CSCP_DT_STRING, value); }
95 void SetVariable(UINT32 dwVarId, const TCHAR *value, UINT32 maxLen) { if (value != NULL) set(dwVarId, CSCP_DT_STRING, value, maxLen); }
96 void SetVariable(UINT32 dwVarId, BYTE *pValue, UINT32 dwSize) { set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
35f836fe 97#ifdef UNICODE
967893bb 98 void SetVariableFromMBString(UINT32 dwVarId, const char *pszValue);
35f836fe 99#else
4af351c7 100 void SetVariableFromMBString(UINT32 dwVarId, const char *pszValue) { set(dwVarId, CSCP_DT_STRING, pszValue); }
35f836fe 101#endif
967893bb
VK
102 void SetVariableToInt32Array(UINT32 dwVarId, UINT32 dwNumElements, const UINT32 *pdwData);
103 BOOL SetVariableFromFile(UINT32 dwVarId, const TCHAR *pszFileName);
104
105 UINT32 GetVariableLong(UINT32 dwVarId);
106 UINT64 GetVariableInt64(UINT32 dwVarId);
107 UINT16 GetVariableShort(UINT32 dwVarId);
108 INT32 GetVariableShortAsInt32(UINT32 dwVarId);
109 double GetVariableDouble(UINT32 dwVarId);
110 TCHAR *GetVariableStr(UINT32 dwVarId, TCHAR *szBuffer = NULL, UINT32 dwBufSize = 0);
111 char *GetVariableStrA(UINT32 dwVarId, char *pszBuffer = NULL, UINT32 dwBufSize = 0);
112 char *GetVariableStrUTF8(UINT32 dwVarId, char *pszBuffer = NULL, UINT32 dwBufSize = 0);
113 UINT32 GetVariableBinary(UINT32 dwVarId, BYTE *pBuffer, UINT32 dwBufSize);
114 UINT32 GetVariableInt32Array(UINT32 dwVarId, UINT32 dwNumElements, UINT32 *pdwBuffer);
0d9c1b96 115
4af351c7 116 void deleteAllVariables();
e44ac467 117
5c44534b
VK
118 void disableEncryption() { m_flags |= MF_DONT_ENCRYPT; }
119 void setEndOfSequence() { m_flags |= MF_END_OF_SEQUENCE; }
120 void setReverseOrderFlag() { m_flags |= MF_REVERSE_ORDER; }
934f53da
VK
121
122 static String dump(CSCP_MESSAGE *msg, int version);
0d9c1b96
VK
123};
124
4af351c7
VK
125/**
126 * Message waiting queue element structure
127 */
deaa4491
VK
128typedef struct
129{
130 WORD wCode; // Message code
131 WORD wIsBinary; // 1 for binary (raw) messages
967893bb
VK
132 UINT32 dwId; // Message ID
133 UINT32 dwTTL; // Message time-to-live in milliseconds
deaa4491
VK
134 void *pMsg; // Pointer to message, either to CSCPMessage object or raw message
135} WAIT_QUEUE_ELEMENT;
136
4af351c7
VK
137/**
138 * Message waiting queue class
139 */
9f2ad16c 140class LIBNETXMS_EXPORTABLE MsgWaitQueue
deaa4491 141{
deaa4491 142private:
5f743326
VK
143 MUTEX m_mutexDataAccess;
144 CONDITION m_condStop;
145 CONDITION m_condNewMsg;
967893bb
VK
146 UINT32 m_dwMsgHoldTime;
147 UINT32 m_dwNumElements;
deaa4491 148 WAIT_QUEUE_ELEMENT *m_pElements;
ccdbbb52 149 THREAD m_hHkThread;
deaa4491 150
c17f6cbc
VK
151 void lock() { MutexLock(m_mutexDataAccess); }
152 void unlock() { MutexUnlock(m_mutexDataAccess); }
153 void housekeeperThread();
967893bb 154 void *waitForMessageInternal(UINT16 wIsBinary, UINT16 wCode, UINT32 dwId, UINT32 dwTimeOut);
cc022855 155
c17f6cbc 156 static THREAD_RESULT THREAD_CALL mwqThreadStarter(void *);
deaa4491
VK
157
158public:
159 MsgWaitQueue();
160 ~MsgWaitQueue();
161
c17f6cbc
VK
162 void put(CSCPMessage *pMsg);
163 void put(CSCP_MESSAGE *pMsg);
967893bb 164 CSCPMessage *waitForMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 165 {
c17f6cbc 166 return (CSCPMessage *)waitForMessageInternal(0, wCode, dwId, dwTimeOut);
deaa4491 167 }
967893bb 168 CSCP_MESSAGE *waitForRawMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 169 {
c17f6cbc 170 return (CSCP_MESSAGE *)waitForMessageInternal(1, wCode, dwId, dwTimeOut);
deaa4491 171 }
cc022855 172
c17f6cbc 173 void clear();
967893bb 174 void setHoldTime(UINT32 dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
deaa4491
VK
175};
176
b549a0f8
VK
177/**
178 * NXCP encryption context
179 */
98abc9f1
VK
180class LIBNETXMS_EXPORTABLE NXCPEncryptionContext : public RefCountObject
181{
182private:
183 int m_cipher;
184 BYTE *m_sessionKey;
185 int m_keyLength;
186 BYTE m_iv[EVP_MAX_IV_LENGTH];
e4e091f0 187#ifdef _WITH_ENCRYPTION
e6336a90
VK
188 EVP_CIPHER_CTX m_encryptor;
189 EVP_CIPHER_CTX m_decryptor;
db05c2af 190 MUTEX m_encryptorLock;
e4e091f0 191#endif
98abc9f1
VK
192
193 NXCPEncryptionContext();
e6336a90 194 bool initCipher(int cipher);
98abc9f1
VK
195
196public:
197 static NXCPEncryptionContext *create(CSCPMessage *msg, RSA *privateKey);
967893bb 198 static NXCPEncryptionContext *create(UINT32 ciphers);
98abc9f1
VK
199
200 virtual ~NXCPEncryptionContext();
201
e6336a90
VK
202 CSCP_ENCRYPTED_MESSAGE *encryptMessage(CSCP_MESSAGE *msg);
203 bool decryptMessage(CSCP_ENCRYPTED_MESSAGE *msg, BYTE *decryptionBuffer);
204
98abc9f1
VK
205 int getCipher() { return m_cipher; }
206 BYTE *getSessionKey() { return m_sessionKey; }
207 int getKeyLength() { return m_keyLength; }
208 BYTE *getIV() { return m_iv; }
209};
210
9f2ad16c
VK
211#else /* __cplusplus */
212
213typedef void CSCPMessage;
98abc9f1 214typedef void NXCPEncryptionContext;
9f2ad16c
VK
215
216#endif
217
deaa4491
VK
218
219//
0d9c1b96
VK
220// Functions
221//
222
223#ifdef __cplusplus
0d9c1b96 224
9f2ad16c 225int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
967893bb 226 CSCP_BUFFER *pBuffer, UINT32 dwMaxMsgSize,
98abc9f1 227 NXCPEncryptionContext **ppCtx,
967893bb 228 BYTE *pDecryptionBuffer, UINT32 dwTimeout);
b9b2b87b 229int LIBNETXMS_EXPORTABLE RecvNXCPMessageEx(SOCKET hSocket, CSCP_MESSAGE **msgBuffer,
967893bb 230 CSCP_BUFFER *nxcpBuffer, UINT32 *bufferSize,
cc022855 231 NXCPEncryptionContext **ppCtx,
967893bb
VK
232 BYTE **decryptionBuffer, UINT32 dwTimeout,
233 UINT32 maxMsgSize);
234CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, UINT32 dwId, WORD wFlags,
235 UINT32 dwDataSize, void *pData,
0d9c1b96 236 CSCP_MESSAGE *pBuffer);
9f2ad16c 237TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
967893bb 238BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, UINT32 dwId, const TCHAR *pszFile,
cc022855 239 NXCPEncryptionContext *pCtx, long offset,
7b8b337e
VK
240 void (* progressCallback)(INT64, void *), void *cbArg,
241 MUTEX mutex);
242BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion, MUTEX mutex);
cc022855 243
6468147c 244BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(UINT32 dwEnabledCiphers, void (*debugCallback)(int, const TCHAR *, va_list args));
967893bb 245UINT32 LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers();
98abc9f1
VK
246CSCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE *CSCPEncryptMessage(NXCPEncryptionContext *pCtx, CSCP_MESSAGE *pMsg);
247BOOL LIBNETXMS_EXPORTABLE CSCPDecryptMessage(NXCPEncryptionContext *pCtx,
e44ac467
VK
248 CSCP_ENCRYPTED_MESSAGE *pMsg,
249 BYTE *pDecryptionBuffer);
cc022855 250UINT32 LIBNETXMS_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
98abc9f1 251 NXCPEncryptionContext **ppCtx,
1a17feb0 252 CSCPMessage **ppResponse,
4bd3d6c5 253 RSA *pPrivateKey, int nNXCPVersion);
9e9d631e 254void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey, bool useX509Format);
3aa0061c 255RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile);
15cf5acc 256
bb8136f7 257#ifdef _WIN32
967893bb
VK
258BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, UINT32 dwMsgLen, const CERT_CONTEXT *pCert,
259 BYTE *pBuffer, UINT32 dwBufSize, UINT32 *pdwSigLen);
bb8136f7
VK
260#endif
261
0d9c1b96
VK
262#endif
263
9f2ad16c 264#endif /* _nxcpapi_h_ */