Management pack creation UI almost working
[public/netxms.git] / include / nms_util.h
CommitLineData
ff214673 1/* $Id: nms_util.h,v 1.96 2006-11-03 08:58:56 victor Exp $ */
70fed300 2
e9246d26 3/*
7ac71e60 4** NetXMS - Network Management System
e9246d26
VK
5** Copyright (C) 2003 Victor Kirhenshtein
6**
7** This program is free software; you can redistribute it and/or modify
8** it under the terms of the GNU General Public License as published by
9** the Free Software Foundation; either version 2 of the License, or
10** (at your option) any later version.
11**
12** This program is distributed in the hope that it will be useful,
13** but WITHOUT ANY WARRANTY; without even the implied warranty of
14** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15** GNU General Public License for more details.
16**
17** You should have received a copy of the GNU General Public License
18** along with this program; if not, write to the Free Software
19** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20**
21** $module: nms_util.h
22**
23**/
24
25#ifndef _nms_util_h_
26#define _nms_util_h_
27
64584976
VK
28#ifdef _WIN32
29#ifdef LIBNETXMS_EXPORTS
30#define LIBNETXMS_EXPORTABLE __declspec(dllexport)
31#else
32#define LIBNETXMS_EXPORTABLE __declspec(dllimport)
33#endif
34#else /* _WIN32 */
35#define LIBNETXMS_EXPORTABLE
36#endif
37
38
e9246d26
VK
39#include <nms_common.h>
40#include <nms_cscp.h>
953dfe76 41#include <nms_threads.h>
64584976 42#include <time.h>
e9246d26 43
6d8aee95
VK
44#if HAVE_BYTESWAP_H
45#include <byteswap.h>
46#endif
47
48
49//
50// Sirial communications
51//
52
53#ifdef _WIN32
54
55#define FLOW_NONE 0
56#define FLOW_SOFTWARE 1
57#define FLOW_HARDWARE 2
58
59#else /* _WIN32 */
60
70fed300
AK
61#ifdef HAVE_TERMIOS_H
62# include <termios.h>
63#else
64# error termios.h not found
65#endif
66
6d8aee95
VK
67#endif /* _WIN32 */
68
69
70//
71// Common constants
72//
f52be742 73
5d63b3c1 74#define INVALID_INDEX 0xFFFFFFFF
28dd92c9 75#define CSCP_TEMP_BUF_SIZE 65536
89bfdd9c 76#define MD5_DIGEST_SIZE 16
d0b249a8 77#define SHA1_DIGEST_SIZE 20
5d63b3c1
VK
78
79
80//
66c04cc7
VK
81// Return codes for IcmpPing()
82//
83
84#define ICMP_SUCCESS 0
85#define ICMP_UNREACHEABLE 1
86#define ICMP_TIMEOUT 2
87#define ICMP_RAW_SOCK_FAILED 3
88
89
90//
4826c8a5 91// Token types for configuration loader
dcb44c9a
VK
92//
93
4826c8a5
VK
94#define CT_LONG 0
95#define CT_STRING 1
96#define CT_STRING_LIST 2
97#define CT_END_OF_LIST 3
98#define CT_BOOLEAN 4
99#define CT_WORD 5
fc935a60 100#define CT_IGNORE 6
4826c8a5
VK
101
102
103//
104// Return codes for NxLoadConfig()
105//
106
107#define NXCFG_ERR_OK 0
108#define NXCFG_ERR_NOFILE 1
109#define NXCFG_ERR_SYNTAX 2
110
111
112//
b160350d
VK
113// Class for serial communications
114//
115
116#ifdef __cplusplus
117
118#ifndef _WIN32
119enum
120{
121 NOPARITY,
122 ODDPARITY,
123 EVENPARITY,
124 ONESTOPBIT,
125 TWOSTOPBITS
126};
127
70fed300
AK
128enum
129{
130 FLOW_NONE,
131 FLOW_HARDWARE,
132 FLOW_SOFTWARE
133};
134
b160350d
VK
135#ifndef INVALID_HANDLE_VALUE
136#define INVALID_HANDLE_VALUE (-1)
137#endif
138#endif /* _WIN32 */
139
140class LIBNETXMS_EXPORTABLE Serial
141{
142public:
143 Serial(void);
144 ~Serial(void);
145
146 bool Open(TCHAR *pszPort);
147 void Close(void);
148 void SetTimeout(int nTimeout);
2f06a66e 149 int Read(char *pBuff, int nSize);
b160350d
VK
150 bool Write(char *pBuff, int nSize);
151 void Flush(void);
152 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits);
70fed300 153 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl);
b160350d
VK
154 bool Restart(void);
155
156private:
157 TCHAR *m_pszPort;
158 int m_nTimeout;
159 int m_nSpeed;
160 int m_nDataBits;
161 int m_nStopBits;
162 int m_nParity;
70fed300 163 int m_nFlowControl;
b160350d
VK
164
165#ifndef _WIN32
166 int m_hPort;
70fed300 167 struct termios m_originalSettings;
b160350d
VK
168#else
169 HANDLE m_hPort;
170#endif
171};
172
ff214673
VK
173
174//
175// Class for table data storage
176//
177
178class LIBNETXMS_EXPORTABLE Table
179{
180private:
181 int m_nNumRows;
182 int m_nNumCols;
183 TCHAR **m_ppData;
184 TCHAR **m_ppColNames;
185
186public:
187 Table();
188 ~Table();
189
190 int GetNumRows(void) { return m_nNumRows; }
191 int GetNumColumns(void) { return m_nNumCols; }
192
193 void AddColumn(TCHAR *pszName);
194 void AddRow(void);
195
196 void SetAt(int nRow, int nCol, TCHAR *pszData);
197 void SetAt(int nRow, int nCol, LONG nData);
198 void SetAt(int nRow, int nCol, DWORD dwData);
199 void SetAt(int nRow, int nCol, double dData);
200 void SetAt(int nRow, int nCol, INT64 nData);
201 void SetAt(int nRow, int nCol, QWORD qwData);
202
203 void Set(int nCol, TCHAR *pszData) { SetAt(m_nNumRows - 1, nCol, pszData); }
204 void Set(int nCol, LONG nData) { SetAt(m_nNumRows - 1, nCol, nData); }
205 void Set(int nCol, DWORD dwData) { SetAt(m_nNumRows - 1, nCol, dwData); }
206 void Set(int nCol, double dData) { SetAt(m_nNumRows - 1, nCol, dData); }
207 void Set(int nCol, INT64 nData) { SetAt(m_nNumRows - 1, nCol, nData); }
208 void Set(int nCol, QWORD qwData) { SetAt(m_nNumRows - 1, nCol, qwData); }
209
210 TCHAR *GetAsString(int nRow, int nCol);
211 LONG GetAsInt(int nRow, int nCol);
212 DWORD GetAsUInt(int nRow, int nCol);
213 INT64 GetAsInt64(int nRow, int nCol);
214 QWORD GetAsUInt64(int nRow, int nCol);
215 double GetAsDouble(int nRow, int nCol);
216};
217
b160350d
VK
218#endif /* __cplusplus */
219
220
221//
4826c8a5
VK
222// Configuration item template for configuration loader
223//
224
225typedef struct
226{
018fda4d 227 TCHAR szToken[64];
4826c8a5
VK
228 BYTE iType;
229 BYTE cSeparator; // Separator character for lists
230 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
231 DWORD dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
232 DWORD dwBufferPos; // Should be set to 0
233 void *pBuffer;
234} NX_CFG_TEMPLATE;
dcb44c9a
VK
235
236
237//
3046f9a4
VK
238// getopt() prototype if needed
239//
240
241#ifdef _WIN32
242#include <getopt.h>
243#endif
244
245
246//
f2fdf1b5
VK
247// Win32 API functions missing under WinCE
248//
249
c32b805a 250#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
251
252inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
253{
254 SYSTEMTIME sysTime;
255
256 GetSystemTime(&sysTime);
257 SystemTimeToFileTime(&sysTime, pFt);
258}
259
260#endif // UNDER_CE
261
262
263//
3d1cb7d6
VK
264// Structures for opendir() / readdir() / closedir()
265//
266
267#ifdef _WIN32
268
37d62364
AK
269#ifndef SWIGPERL
270
7f9e0c00 271typedef struct dirent
3d1cb7d6
VK
272{
273 long d_ino; /* inode number (not used by MS-DOS) */
274 int d_namlen; /* Name length */
275 char d_name[257]; /* file name */
276} _DIRECT;
277
278typedef struct _dir_struc
279{
280 char *start; /* Starting position */
281 char *curr; /* Current position */
282 long size; /* Size of string table */
283 long nfiles; /* number if filenames in table */
7f9e0c00 284 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
285} DIR;
286
37d62364
AK
287#endif
288
3d1cb7d6
VK
289#endif /* _WIN32 */
290
291
292//
e9246d26
VK
293// Functions
294//
295
43d92b4c 296
babb8280 297#if WORDS_BIGENDIAN
43d92b4c
VK
298#define htonq(x) (x)
299#define ntohq(x) (x)
9f20696e
VK
300#define htond(x) (x)
301#define ntohd(x) (x)
babb8280
VK
302#define SwapWideString(x)
303#else
ca6ad6bb
VK
304#ifdef HAVE_HTONLL
305#define htonq(x) htonll(x)
306#else
babb8280 307#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
308#endif
309#ifdef HAVE_NTOHLL
310#define ntohq(x) ntohll(x)
311#else
babb8280 312#define ntohq(x) __bswap_64(x)
ca6ad6bb 313#endif
babb8280
VK
314#define htond(x) __bswap_double(x)
315#define ntohd(x) __bswap_double(x)
316#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 317#endif
15929b31 318
55b33d4c
VK
319#ifdef UNDER_CE
320#define close(x) CloseHandle((HANDLE)(x))
321#endif
322
b50f1100 323#ifdef __cplusplus
c81979bc
VK
324#ifndef LIBNETXMS_INLINE
325 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, int nLen)
326 {
327 _tcsncpy(pszDest, pszSrc, nLen - 1);
328 pszDest[nLen - 1] = 0;
329 return pszDest;
330 }
331#endif
332#else
333 TCHAR LIBNETXMS_EXPORTABLE *nx_strncpy(TCHAR *pszDest, TCHAR *pszSrc, int nLen);
334#endif
335
336#ifdef __cplusplus
e9246d26
VK
337extern "C"
338{
b50f1100 339#endif
08043edf
VK
340 int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int);
341 int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
342 size_t nSize, int nFlags, DWORD dwTimeout);
b24955fb 343
c5b523e2
VK
344#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
345 DWORD LIBNETXMS_EXPORTABLE __bswap_32(DWORD dwVal);
346#endif
f52be742 347#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
64584976
VK
348 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
349#endif
9f20696e 350 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
babb8280 351 void LIBNETXMS_EXPORTABLE __bswap_wstr(WCHAR *pStr);
64584976 352
076a55c4 353#if !defined(_WIN32) && !defined(_NETWARE)
018fda4d 354 void LIBNETXMS_EXPORTABLE strupr(TCHAR *in);
7e679c4b 355#endif
ab588c84 356
670c7f3d 357 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
ddce77b0 358 QWORD LIBNETXMS_EXPORTABLE FileSize(TCHAR *pszFileName);
670c7f3d 359
64584976 360 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
018fda4d 361 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
d0fc56c6 362 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(TCHAR *pszName);
a1dc75b0 363
64584976 364 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
f218dc6d 365 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
31521248 366
e25cadc9 367 TCHAR LIBNETXMS_EXPORTABLE *BinToStr(BYTE *pData, DWORD dwSize, TCHAR *pStr);
4075c782 368 DWORD LIBNETXMS_EXPORTABLE StrToBin(TCHAR *pStr, BYTE *pData, DWORD dwSize);
c61f5190 369 void LIBNETXMS_EXPORTABLE MACToStr(BYTE *pData, TCHAR *pStr);
b50f1100 370
018fda4d
AK
371 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
372 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
373 TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(TCHAR *line, TCHAR *buffer);
fa2b47a7
VK
374 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName);
375 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
ff5e8be5 376 void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, TCHAR *pszSubStr, TCHAR *pszReplace);
257defd7 377 TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(TCHAR *pszFileName);
03ed7e4f 378 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer);
64584976 379
6120112b 380 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
a3c76aba
VK
381 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
382 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
b40371d4
AK
383 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(TCHAR *pszFileName, BYTE *pHash);
384 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(TCHAR *pszFileName, BYTE *pHash);
6120112b 385 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(TCHAR *pszFileName, DWORD *pResult);
66c04cc7 386
46ef501a
VK
387 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
388 DWORD *pdwRTT, DWORD dwPacketSize);
4826c8a5 389
018fda4d 390 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(TCHAR *pszFileName, TCHAR *pszSection,
6849d9be 391 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);
125c0e81 392 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(TCHAR *pszText);
aa13cfd9 393
018fda4d 394 HMODULE LIBNETXMS_EXPORTABLE DLOpen(TCHAR *szLibName, TCHAR *pszErrorText);
aa13cfd9 395 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
018fda4d 396 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, TCHAR *szSymbol, TCHAR *pszErrorText);
aa13cfd9 397
ae9cddaf 398 void LIBNETXMS_EXPORTABLE InitSubAgentsLogger(void (* pFunc)(int, TCHAR *));
15c8cf46
VK
399 void LIBNETXMS_EXPORTABLE InitSubAgentsTrapSender(void (* pFunc1)(DWORD, int, TCHAR **),
400 void (* pFunc2)(DWORD, char *, va_list));
ae9cddaf 401
aa13cfd9 402#ifdef _WIN32
018fda4d 403 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, int iBufSize);
aa13cfd9 404#endif
696fc54f
VK
405
406#if !(HAVE_DAEMON)
b1a57444 407 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 408#endif
4332e9d8 409
268774a0
VK
410 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(WCHAR *pszAddr);
411
4332e9d8 412#ifndef _WIN32
5f461694 413 void LIBNETXMS_EXPORTABLE SetDefaultCodepage(char *cp);
4332e9d8
VK
414 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, WCHAR *pWideCharStr,
415 int cchWideChar, char *pByteStr, int cchByteChar,
416 char *pDefaultChar, BOOL *pbUsedDefChar);
417 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, char *pByteStr,
418 int cchByteChar, WCHAR *pWideCharStr,
419 int cchWideChar);
1738fb2b 420#if !HAVE_USEABLE_WCHAR
556d9c45 421 int LIBNETXMS_EXPORTABLE nx_wcslen(WCHAR *pStr);
4332e9d8 422#endif
3e39fcde 423#endif /* _WIN32 */
7d4b78b3
VK
424 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(char *pszString);
425 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(WCHAR *pwszString);
426 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(WCHAR *pwszString);
b50f1100 427
dde96745
VK
428#ifdef UNICODE
429INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
430QWORD LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
431#else
b50f1100
VK
432#if !(HAVE_STRTOLL)
433 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
434#endif
435#if !(HAVE_STRTOULL)
436 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
437#endif
dde96745 438#endif
b50f1100 439
3d1cb7d6 440#ifdef _WIN32
37d62364 441#ifndef SWIGPERL
3d1cb7d6 442 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 443 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6
VK
444 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
445#endif
37d62364 446#endif
3d1cb7d6 447
7f9e0c00
VK
448#if defined(_WIN32) || !(HAVE_SCANDIR)
449 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
450 int (*select)(const struct dirent *),
451 int (*compar)(const struct dirent **, const struct dirent **));
452 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
453#endif
454
55b33d4c
VK
455#ifdef UNDER_CE
456 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
457 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
458 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
459#endif
460
b71bff93 461#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
953dfe76 462void LIBNETXMS_EXPORTABLE StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
45c13da4 463 THREAD_RESULT (THREAD_CALL * pfMain)(void *));
953dfe76
VK
464#endif
465
b50f1100 466#ifdef __cplusplus
e9246d26 467}
b50f1100 468#endif
e9246d26
VK
469
470#endif /* _nms_util_h_ */
70fed300
AK
471
472///////////////////////////////////////////////////////////////////////////////
473/*
474
475$Log: not supported by cvs2svn $
ff214673
VK
476Revision 1.95 2006/10/17 21:20:25 victor
477Finished Implementation of agent traps and added trap sending API for subagents
478
15c8cf46
VK
479Revision 1.94 2006/10/05 05:24:05 victor
480Minor changes
481
5f461694
VK
482Revision 1.93 2006/10/01 20:43:37 victor
483UNICODE-related fixes
484
6cdccf82
VK
485Revision 1.92 2006/10/01 16:01:09 victor
486Yet another Linux UNICODE fix
487
556d9c45
VK
488Revision 1.91 2006/10/01 15:47:39 victor
489More UNICODE fixes
490
1738fb2b
VK
491Revision 1.90 2006/10/01 15:26:28 victor
492Some UNICODE-related fixes
493
3e39fcde
VK
494Revision 1.89 2006/09/30 22:41:08 victor
495Database driver API changed: now all SQL queries and results passed as UNICODE strings
496
7d4b78b3
VK
497Revision 1.88 2006/09/27 13:04:45 victor
498Preparation for process information parameters
499
ca6ad6bb
VK
500Revision 1.87 2006/09/27 04:15:25 victor
501AIX: implemented Net.InterfaceList
502
e25cadc9
VK
503Revision 1.86 2006/09/23 23:49:41 victor
504Console working with UNICODE!
505
dde96745
VK
506Revision 1.85 2006/09/10 06:59:36 victor
507Fixed problmes with Win32 build
508
6d8aee95
VK
509Revision 1.84 2006/09/07 22:02:06 alk
510UNIX version of Serial rewritten
511termio removed from configure (depricated in favour of termio_s_?)
512
70fed300
AK
513
514*/