Fixed L2 topology
[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 isEndOfSequence() { return (m_flags & MF_END_OF_SEQUENCE) ? true : false; }
84 bool isReverseOrder() { return (m_flags & MF_REVERSE_ORDER) ? true : false; }
4af351c7 85
4101571e
VK
86 bool isFieldExist(UINT32 fieldId) { return find(fieldId) != NULL; }
87 int getFieldType(UINT32 fieldId);
88
4af351c7
VK
89 void SetVariable(UINT32 dwVarId, INT16 wValue) { set(dwVarId, CSCP_DT_INT16, &wValue); }
90 void SetVariable(UINT32 dwVarId, UINT16 wValue) { set(dwVarId, CSCP_DT_INT16, &wValue); }
91 void SetVariable(UINT32 dwVarId, INT32 dwValue) { set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
92 void SetVariable(UINT32 dwVarId, UINT32 dwValue) { set(dwVarId, CSCP_DT_INTEGER, &dwValue); }
93 void SetVariable(UINT32 dwVarId, INT64 qwValue) { set(dwVarId, CSCP_DT_INT64, &qwValue); }
94 void SetVariable(UINT32 dwVarId, UINT64 qwValue) { set(dwVarId, CSCP_DT_INT64, &qwValue); }
95 void SetVariable(UINT32 dwVarId, double dValue) { set(dwVarId, CSCP_DT_FLOAT, &dValue); }
96 void SetVariable(UINT32 dwVarId, const TCHAR *value) { if (value != NULL) set(dwVarId, CSCP_DT_STRING, value); }
97 void SetVariable(UINT32 dwVarId, const TCHAR *value, UINT32 maxLen) { if (value != NULL) set(dwVarId, CSCP_DT_STRING, value, maxLen); }
98 void SetVariable(UINT32 dwVarId, BYTE *pValue, UINT32 dwSize) { set(dwVarId, CSCP_DT_BINARY, pValue, dwSize); }
35f836fe 99#ifdef UNICODE
967893bb 100 void SetVariableFromMBString(UINT32 dwVarId, const char *pszValue);
35f836fe 101#else
4af351c7 102 void SetVariableFromMBString(UINT32 dwVarId, const char *pszValue) { set(dwVarId, CSCP_DT_STRING, pszValue); }
35f836fe 103#endif
9a68ca24
VK
104 void setFieldInt32Array(UINT32 dwVarId, UINT32 dwNumElements, const UINT32 *pdwData);
105 void setFieldInt32Array(UINT32 dwVarId, IntegerArray<UINT32> *data);
967893bb
VK
106 BOOL SetVariableFromFile(UINT32 dwVarId, const TCHAR *pszFileName);
107
4101571e
VK
108 INT16 getFieldAsInt16(UINT32 fieldId);
109 INT32 getFieldAsInt32(UINT32 fieldId);
110 INT64 getFieldAsInt64(UINT32 fieldId);
111 double getFieldAsDouble(UINT32 fieldId);
d6bbfa4e 112 bool getFieldAsBoolean(UINT32 fieldId);
9a68ca24
VK
113 UINT32 getFieldAsInt32Array(UINT32 fieldId, UINT32 numElements, UINT32 *buffer);
114 UINT32 getFieldAsInt32Array(UINT32 fieldId, IntegerArray<UINT32> *data);
ac14e3e6 115 BYTE *getBinaryFieldPtr(UINT32 fieldId, size_t *size);
4101571e 116
967893bb
VK
117 UINT32 GetVariableLong(UINT32 dwVarId);
118 UINT64 GetVariableInt64(UINT32 dwVarId);
119 UINT16 GetVariableShort(UINT32 dwVarId);
967893bb
VK
120 TCHAR *GetVariableStr(UINT32 dwVarId, TCHAR *szBuffer = NULL, UINT32 dwBufSize = 0);
121 char *GetVariableStrA(UINT32 dwVarId, char *pszBuffer = NULL, UINT32 dwBufSize = 0);
122 char *GetVariableStrUTF8(UINT32 dwVarId, char *pszBuffer = NULL, UINT32 dwBufSize = 0);
123 UINT32 GetVariableBinary(UINT32 dwVarId, BYTE *pBuffer, UINT32 dwBufSize);
0d9c1b96 124
4af351c7 125 void deleteAllVariables();
e44ac467 126
5c44534b
VK
127 void disableEncryption() { m_flags |= MF_DONT_ENCRYPT; }
128 void setEndOfSequence() { m_flags |= MF_END_OF_SEQUENCE; }
129 void setReverseOrderFlag() { m_flags |= MF_REVERSE_ORDER; }
934f53da
VK
130
131 static String dump(CSCP_MESSAGE *msg, int version);
0d9c1b96
VK
132};
133
4af351c7
VK
134/**
135 * Message waiting queue element structure
136 */
deaa4491
VK
137typedef struct
138{
139 WORD wCode; // Message code
140 WORD wIsBinary; // 1 for binary (raw) messages
967893bb
VK
141 UINT32 dwId; // Message ID
142 UINT32 dwTTL; // Message time-to-live in milliseconds
deaa4491
VK
143 void *pMsg; // Pointer to message, either to CSCPMessage object or raw message
144} WAIT_QUEUE_ELEMENT;
145
4af351c7
VK
146/**
147 * Message waiting queue class
148 */
9f2ad16c 149class LIBNETXMS_EXPORTABLE MsgWaitQueue
deaa4491 150{
deaa4491 151private:
5f743326
VK
152 MUTEX m_mutexDataAccess;
153 CONDITION m_condStop;
154 CONDITION m_condNewMsg;
967893bb
VK
155 UINT32 m_dwMsgHoldTime;
156 UINT32 m_dwNumElements;
deaa4491 157 WAIT_QUEUE_ELEMENT *m_pElements;
ccdbbb52 158 THREAD m_hHkThread;
deaa4491 159
c17f6cbc
VK
160 void lock() { MutexLock(m_mutexDataAccess); }
161 void unlock() { MutexUnlock(m_mutexDataAccess); }
162 void housekeeperThread();
967893bb 163 void *waitForMessageInternal(UINT16 wIsBinary, UINT16 wCode, UINT32 dwId, UINT32 dwTimeOut);
cc022855 164
c17f6cbc 165 static THREAD_RESULT THREAD_CALL mwqThreadStarter(void *);
deaa4491
VK
166
167public:
168 MsgWaitQueue();
169 ~MsgWaitQueue();
170
c17f6cbc
VK
171 void put(CSCPMessage *pMsg);
172 void put(CSCP_MESSAGE *pMsg);
967893bb 173 CSCPMessage *waitForMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 174 {
c17f6cbc 175 return (CSCPMessage *)waitForMessageInternal(0, wCode, dwId, dwTimeOut);
deaa4491 176 }
967893bb 177 CSCP_MESSAGE *waitForRawMessage(WORD wCode, UINT32 dwId, UINT32 dwTimeOut)
deaa4491 178 {
c17f6cbc 179 return (CSCP_MESSAGE *)waitForMessageInternal(1, wCode, dwId, dwTimeOut);
deaa4491 180 }
cc022855 181
c17f6cbc 182 void clear();
967893bb 183 void setHoldTime(UINT32 dwHoldTime) { m_dwMsgHoldTime = dwHoldTime; }
deaa4491
VK
184};
185
b549a0f8
VK
186/**
187 * NXCP encryption context
188 */
98abc9f1
VK
189class LIBNETXMS_EXPORTABLE NXCPEncryptionContext : public RefCountObject
190{
191private:
192 int m_cipher;
193 BYTE *m_sessionKey;
194 int m_keyLength;
195 BYTE m_iv[EVP_MAX_IV_LENGTH];
e4e091f0 196#ifdef _WITH_ENCRYPTION
e6336a90
VK
197 EVP_CIPHER_CTX m_encryptor;
198 EVP_CIPHER_CTX m_decryptor;
db05c2af 199 MUTEX m_encryptorLock;
e4e091f0 200#endif
98abc9f1
VK
201
202 NXCPEncryptionContext();
e6336a90 203 bool initCipher(int cipher);
98abc9f1
VK
204
205public:
206 static NXCPEncryptionContext *create(CSCPMessage *msg, RSA *privateKey);
967893bb 207 static NXCPEncryptionContext *create(UINT32 ciphers);
98abc9f1
VK
208
209 virtual ~NXCPEncryptionContext();
210
e6336a90
VK
211 CSCP_ENCRYPTED_MESSAGE *encryptMessage(CSCP_MESSAGE *msg);
212 bool decryptMessage(CSCP_ENCRYPTED_MESSAGE *msg, BYTE *decryptionBuffer);
213
98abc9f1
VK
214 int getCipher() { return m_cipher; }
215 BYTE *getSessionKey() { return m_sessionKey; }
216 int getKeyLength() { return m_keyLength; }
217 BYTE *getIV() { return m_iv; }
218};
219
9f2ad16c
VK
220#else /* __cplusplus */
221
222typedef void CSCPMessage;
98abc9f1 223typedef void NXCPEncryptionContext;
9f2ad16c
VK
224
225#endif
226
deaa4491
VK
227
228//
0d9c1b96
VK
229// Functions
230//
231
232#ifdef __cplusplus
0d9c1b96 233
9f2ad16c 234int LIBNETXMS_EXPORTABLE RecvNXCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg,
967893bb 235 CSCP_BUFFER *pBuffer, UINT32 dwMaxMsgSize,
98abc9f1 236 NXCPEncryptionContext **ppCtx,
967893bb 237 BYTE *pDecryptionBuffer, UINT32 dwTimeout);
b9b2b87b 238int LIBNETXMS_EXPORTABLE RecvNXCPMessageEx(SOCKET hSocket, CSCP_MESSAGE **msgBuffer,
967893bb 239 CSCP_BUFFER *nxcpBuffer, UINT32 *bufferSize,
cc022855 240 NXCPEncryptionContext **ppCtx,
967893bb
VK
241 BYTE **decryptionBuffer, UINT32 dwTimeout,
242 UINT32 maxMsgSize);
243CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawNXCPMessage(WORD wCode, UINT32 dwId, WORD wFlags,
244 UINT32 dwDataSize, void *pData,
0d9c1b96 245 CSCP_MESSAGE *pBuffer);
9f2ad16c 246TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer);
967893bb 247BOOL LIBNETXMS_EXPORTABLE SendFileOverNXCP(SOCKET hSocket, UINT32 dwId, const TCHAR *pszFile,
cc022855 248 NXCPEncryptionContext *pCtx, long offset,
7b8b337e
VK
249 void (* progressCallback)(INT64, void *), void *cbArg,
250 MUTEX mutex);
251BOOL LIBNETXMS_EXPORTABLE NXCPGetPeerProtocolVersion(SOCKET hSocket, int *pnVersion, MUTEX mutex);
cc022855 252
6468147c 253BOOL LIBNETXMS_EXPORTABLE InitCryptoLib(UINT32 dwEnabledCiphers, void (*debugCallback)(int, const TCHAR *, va_list args));
967893bb 254UINT32 LIBNETXMS_EXPORTABLE CSCPGetSupportedCiphers();
98abc9f1
VK
255CSCP_ENCRYPTED_MESSAGE LIBNETXMS_EXPORTABLE *CSCPEncryptMessage(NXCPEncryptionContext *pCtx, CSCP_MESSAGE *pMsg);
256BOOL LIBNETXMS_EXPORTABLE CSCPDecryptMessage(NXCPEncryptionContext *pCtx,
e44ac467
VK
257 CSCP_ENCRYPTED_MESSAGE *pMsg,
258 BYTE *pDecryptionBuffer);
cc022855 259UINT32 LIBNETXMS_EXPORTABLE SetupEncryptionContext(CSCPMessage *pMsg,
98abc9f1 260 NXCPEncryptionContext **ppCtx,
1a17feb0 261 CSCPMessage **ppResponse,
4bd3d6c5 262 RSA *pPrivateKey, int nNXCPVersion);
9e9d631e 263void LIBNETXMS_EXPORTABLE PrepareKeyRequestMsg(CSCPMessage *pMsg, RSA *pServerKey, bool useX509Format);
3aa0061c 264RSA LIBNETXMS_EXPORTABLE *LoadRSAKeys(const TCHAR *pszKeyFile);
15cf5acc 265
bb8136f7 266#ifdef _WIN32
967893bb
VK
267BOOL LIBNETXMS_EXPORTABLE SignMessageWithCAPI(BYTE *pMsg, UINT32 dwMsgLen, const CERT_CONTEXT *pCert,
268 BYTE *pBuffer, UINT32 dwBufSize, UINT32 *pdwSigLen);
bb8136f7
VK
269#endif
270
0d9c1b96
VK
271#endif
272
9f2ad16c 273#endif /* _nxcpapi_h_ */