cosmetic changes
[public/netxms.git] / include / nms_util.h
CommitLineData
e9246d26 1/*
7ac71e60 2** NetXMS - Network Management System
36e44abe 3** Copyright (C) 2003-2011 Victor Kirhenshtein
e9246d26
VK
4**
5** This program is free software; you can redistribute it and/or modify
68f384ea
VK
6** it under the terms of the GNU Lesser General Public License as published
7** by the Free Software Foundation; either version 3 of the License, or
e9246d26
VK
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
68f384ea 15** You should have received a copy of the GNU Lesser General Public License
e9246d26
VK
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
fb7ce4b9 19** File: nms_util.h
e9246d26
VK
20**
21**/
22
23#ifndef _nms_util_h_
24#define _nms_util_h_
25
64584976
VK
26#ifdef _WIN32
27#ifdef LIBNETXMS_EXPORTS
28#define LIBNETXMS_EXPORTABLE __declspec(dllexport)
29#else
30#define LIBNETXMS_EXPORTABLE __declspec(dllimport)
31#endif
32#else /* _WIN32 */
33#define LIBNETXMS_EXPORTABLE
34#endif
35
36
e9246d26
VK
37#include <nms_common.h>
38#include <nms_cscp.h>
953dfe76 39#include <nms_threads.h>
64584976 40#include <time.h>
e9246d26 41
6d8aee95
VK
42#if HAVE_BYTESWAP_H
43#include <byteswap.h>
44#endif
45
92502d5f
VK
46#include <base64.h>
47
6d8aee95
VK
48
49//
92502d5f 50// Serial communications
6d8aee95
VK
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//
66c04cc7
VK
71// Return codes for IcmpPing()
72//
73
74#define ICMP_SUCCESS 0
75#define ICMP_UNREACHEABLE 1
76#define ICMP_TIMEOUT 2
77#define ICMP_RAW_SOCK_FAILED 3
416816ed 78#define ICMP_API_ERROR 3
66c04cc7
VK
79
80
81//
4826c8a5 82// Token types for configuration loader
dcb44c9a
VK
83//
84
4826c8a5
VK
85#define CT_LONG 0
86#define CT_STRING 1
87#define CT_STRING_LIST 2
88#define CT_END_OF_LIST 3
89#define CT_BOOLEAN 4
90#define CT_WORD 5
fc935a60 91#define CT_IGNORE 6
4826c8a5
VK
92
93
94//
95// Return codes for NxLoadConfig()
96//
97
98#define NXCFG_ERR_OK 0
99#define NXCFG_ERR_NOFILE 1
100#define NXCFG_ERR_SYNTAX 2
101
102
103//
9f2902ac
VK
104// nxlog_open() flags
105//
106
107#define NXLOG_USE_SYSLOG ((DWORD)0x00000001)
108#define NXLOG_PRINT_TO_STDOUT ((DWORD)0x00000002)
109#define NXLOG_IS_OPEN ((DWORD)0x80000000)
110
111
112//
5945d50a
VK
113// nxlog rotation policy
114//
115
116#define NXLOG_ROTATION_DISABLED 0
117#define NXLOG_ROTATION_DAILY 1
118#define NXLOG_ROTATION_BY_SIZE 2
119
120
121//
35f836fe 122// _tcsdup() replacement
8ff13508
VK
123//
124
125#if defined(_WIN32) && defined(USE_WIN32_HEAP)
126#ifdef __cplusplus
127extern "C" {
128#endif
35f836fe 129char LIBNETXMS_EXPORTABLE *nx__tcsdup(const char *src);
8ff13508
VK
130WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
131#ifdef __cplusplus
132}
133#endif
134#endif
135
136
137//
b160350d
VK
138// Class for serial communications
139//
140
141#ifdef __cplusplus
142
143#ifndef _WIN32
144enum
145{
146 NOPARITY,
147 ODDPARITY,
148 EVENPARITY,
149 ONESTOPBIT,
150 TWOSTOPBITS
151};
152
70fed300
AK
153enum
154{
155 FLOW_NONE,
156 FLOW_HARDWARE,
157 FLOW_SOFTWARE
158};
159
b160350d
VK
160#ifndef INVALID_HANDLE_VALUE
161#define INVALID_HANDLE_VALUE (-1)
162#endif
163#endif /* _WIN32 */
164
165class LIBNETXMS_EXPORTABLE Serial
166{
167public:
168 Serial(void);
169 ~Serial(void);
170
e2d68ddd 171 bool Open(const TCHAR *pszPort);
b160350d
VK
172 void Close(void);
173 void SetTimeout(int nTimeout);
f3a581d0
AK
174 int Read(char *pBuff, int nSize); /* waits up to timeout and do single read */
175 int ReadAll(char *pBuff, int nSize); /* read until timeout or out of space */
e2d68ddd 176 bool Write(const char *pBuff, int nSize);
b160350d
VK
177 void Flush(void);
178 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits);
70fed300 179 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl);
b160350d
VK
180 bool Restart(void);
181
182private:
183 TCHAR *m_pszPort;
184 int m_nTimeout;
185 int m_nSpeed;
186 int m_nDataBits;
187 int m_nStopBits;
188 int m_nParity;
70fed300 189 int m_nFlowControl;
b160350d
VK
190
191#ifndef _WIN32
192 int m_hPort;
70fed300 193 struct termios m_originalSettings;
b160350d
VK
194#else
195 HANDLE m_hPort;
196#endif
197};
198
ff214673
VK
199
200//
201// Class for table data storage
202//
203
ccc34207
VK
204class CSCPMessage;
205
ff214673
VK
206class LIBNETXMS_EXPORTABLE Table
207{
208private:
209 int m_nNumRows;
210 int m_nNumCols;
211 TCHAR **m_ppData;
212 TCHAR **m_ppColNames;
e31c8e60
VK
213 LONG *m_colFormats;
214 TCHAR *m_title;
ff214673
VK
215
216public:
217 Table();
ccc34207 218 Table(CSCPMessage *msg);
ff214673
VK
219 ~Table();
220
3d5610eb 221 int fillMessage(CSCPMessage &msg, int offset, int rowLimit);
9e126e57 222
e31c8e60
VK
223 int getNumRows() { return m_nNumRows; }
224 int getNumColumns() { return m_nNumCols; }
225 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
9e126e57 226
e31c8e60
VK
227 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_nNumCols)) ? m_ppColNames[col] : NULL; }
228 LONG getColumnFormat(int col) { return ((col >= 0) && (col < m_nNumCols)) ? m_colFormats[col] : 0; }
229
230 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = (title != NULL) ? _tcsdup(title) : NULL; }
231 int addColumn(const TCHAR *name, LONG format = 0);
232 int addRow();
9e126e57
VK
233
234 void setAt(int nRow, int nCol, LONG nData);
235 void setAt(int nRow, int nCol, DWORD dwData);
236 void setAt(int nRow, int nCol, double dData);
237 void setAt(int nRow, int nCol, INT64 nData);
238 void setAt(int nRow, int nCol, QWORD qwData);
239 void setAt(int nRow, int nCol, const TCHAR *pszData);
240 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
241
242 void set(int nCol, LONG nData) { setAt(m_nNumRows - 1, nCol, nData); }
243 void set(int nCol, DWORD dwData) { setAt(m_nNumRows - 1, nCol, dwData); }
244 void set(int nCol, double dData) { setAt(m_nNumRows - 1, nCol, dData); }
245 void set(int nCol, INT64 nData) { setAt(m_nNumRows - 1, nCol, nData); }
246 void set(int nCol, QWORD qwData) { setAt(m_nNumRows - 1, nCol, qwData); }
247 void set(int nCol, const TCHAR *pszData) { setAt(m_nNumRows - 1, nCol, pszData); }
248 void setPreallocated(int nCol, TCHAR *pszData) { setPreallocatedAt(m_nNumRows - 1, nCol, pszData); }
249
250 const TCHAR *getAsString(int nRow, int nCol);
251 LONG getAsInt(int nRow, int nCol);
252 DWORD getAsUInt(int nRow, int nCol);
253 INT64 getAsInt64(int nRow, int nCol);
254 QWORD getAsUInt64(int nRow, int nCol);
255 double getAsDouble(int nRow, int nCol);
ff214673
VK
256};
257
8bad8476
VK
258
259//
260// Dynamic string class
261//
262
263class LIBNETXMS_EXPORTABLE String
264{
265protected:
266 TCHAR *m_pszBuffer;
267 DWORD m_dwBufSize;
268
269public:
c577e360
VK
270 static const int npos;
271
8bad8476 272 String();
f3cdd1c5 273 String(const TCHAR *init);
643c9dcb 274 String(const String &src);
8bad8476
VK
275 ~String();
276
7618e362 277 TCHAR *getBuffer() { return m_pszBuffer; }
ce7565e7 278 void setBuffer(TCHAR *pszBuffer);
8bad8476 279
2391d4fd 280 const String& operator =(const TCHAR *pszStr);
49bf9d8d 281 const String& operator =(const String &src);
2391d4fd 282 const String& operator +=(const TCHAR *pszStr);
ce7565e7 283 operator const TCHAR*() { return CHECK_NULL_EX(m_pszBuffer); }
8bad8476 284
08b214c6
VK
285 char *getUTF8String();
286
ce7565e7
VK
287 void addString(const TCHAR *pStr, DWORD dwLen);
288 void addDynamicString(TCHAR *pszStr) { if (pszStr != NULL) { *this += pszStr; free(pszStr); } }
c577e360 289
ce7565e7
VK
290 void addMultiByteString(const char *pStr, DWORD dwSize, int nCodePage);
291 void addWideCharString(const WCHAR *pStr, DWORD dwSize);
ff175e91 292
ce7565e7 293 void addFormattedString(const TCHAR *format, ...);
893dfdd7 294 void addFormattedStringV(const TCHAR *format, va_list args);
ce7565e7
VK
295 void escapeCharacter(int ch, int esc);
296 void translate(const TCHAR *pszSrc, const TCHAR *pszDst);
c577e360 297
ce7565e7
VK
298 DWORD getSize() { return m_dwBufSize > 0 ? m_dwBufSize - 1 : 0; }
299 BOOL isEmpty() { return m_dwBufSize <= 1; }
c577e360 300
ce7565e7
VK
301 TCHAR *subStr(int nStart, int nLen, TCHAR *pszBuffer);
302 TCHAR *subStr(int nStart, int nLen) { return subStr(nStart, nLen, NULL); }
303 int find(const TCHAR *pszStr, int nStart = 0);
1e1450ef 304
0bbab9d3
VK
305 void trim();
306 void shrink(int chars = 1);
c577e360
VK
307};
308
309
310//
311// String map class
312//
313
314class LIBNETXMS_EXPORTABLE StringMap
315{
316protected:
fb986055
VK
317 DWORD m_size;
318 TCHAR **m_keys;
319 TCHAR **m_values;
c577e360 320
fb986055 321 DWORD find(const TCHAR *key);
c577e360
VK
322
323public:
324 StringMap();
fb986055 325 StringMap(const StringMap &src);
c577e360
VK
326 ~StringMap();
327
fb986055 328 StringMap& operator =(const StringMap &src);
40b7416c 329
fb986055
VK
330 void set(const TCHAR *key, const TCHAR *value);
331 void setPreallocated(TCHAR *key, TCHAR *value);
5ad2167d
VK
332 void set(const TCHAR *key, DWORD value);
333
fb986055 334 const TCHAR *get(const TCHAR *key);
5ad2167d 335 DWORD getULong(const TCHAR *key, DWORD defaultValue);
6acb8479 336 bool getBoolean(const TCHAR *key, bool defaultValue);
5ad2167d 337
fb986055
VK
338 void remove(const TCHAR *key);
339 void clear();
c577e360 340
fb986055
VK
341 DWORD getSize() { return m_size; }
342 const TCHAR *getKeyByIndex(DWORD idx) { return (idx < m_size) ? CHECK_NULL_EX(m_keys[idx]) : NULL; }
343 const TCHAR *getValueByIndex(DWORD idx) { return (idx < m_size) ? CHECK_NULL_EX(m_values[idx]) : NULL; }
8bad8476
VK
344};
345
6173bea8
VK
346
347//
348// String list class
349//
350
351class LIBNETXMS_EXPORTABLE StringList
352{
353private:
354 int m_count;
355 int m_allocated;
356 TCHAR **m_values;
357
358public:
359 StringList();
360 ~StringList();
361
362 void add(const TCHAR *value);
363 void addPreallocated(TCHAR *value);
364 void add(LONG value);
365 void add(DWORD value);
366 void add(INT64 value);
367 void add(QWORD value);
368 void add(double value);
24dc5346 369 void replace(int index, const TCHAR *value);
6173bea8
VK
370 void clear();
371 int getSize() { return m_count; }
372 const TCHAR *getValue(int index) { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
ed806a1a
VK
373 int getIndex(const TCHAR *value);
374 int getIndexIgnoreCase(const TCHAR *value);
6173bea8
VK
375};
376
0d75ea88
VK
377
378//
379// Dynamic array class
380//
381
382class LIBNETXMS_EXPORTABLE Array
383{
384private:
385 int m_size;
386 int m_allocated;
387 int m_grow;
388 void **m_data;
389 bool m_objectOwner;
390
16d6f798
VK
391 void internalRemove(int index, bool allowDestruction);
392
0d75ea88
VK
393protected:
394 virtual void destroyObject(void *object);
395
396public:
397 Array(int initial = 0, int grow = 16, bool owner = false);
398 virtual ~Array();
399
400 int add(void *object);
401 void *get(int index) { return ((index >= 0) && (index < m_size)) ? m_data[index] : NULL; }
47a89886 402 void set(int index, void *object);
24dc5346 403 void replace(int index, void *object);
16d6f798
VK
404 void remove(int index) { internalRemove(index, true); }
405 void unlink(int index) { internalRemove(index, false); }
0d75ea88
VK
406 void clear();
407
408 int size() { return m_size; }
409
410 void setOwner(bool owner) { m_objectOwner = owner; }
411 bool isOwner() { return m_objectOwner; }
412};
413
414template <class T> class ObjectArray : public Array
415{
416protected:
417 virtual void destroyObject(void *object) { delete (T*)object; }
418
419public:
926e8ce7 420 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { }
0d75ea88
VK
421 virtual ~ObjectArray() { }
422
47a89886 423 int add(T *object) { return Array::add((void *)object); }
0d75ea88 424 T *get(int index) { return (T*)Array::get(index); }
47a89886
VK
425 void set(int index, T *object) { Array::set(index, (void *)object); }
426 void replace(int index, T *object) { Array::replace(index, (void *)object); }
0d75ea88
VK
427};
428
7f632dfe
VK
429
430//
cc8ce218
VK
431// Auxilliary class to hold dynamically allocated array of structures
432//
433
434template <class T> class StructArray
435{
436private:
437 int m_count;
438 T *m_data;
439
440public:
441 StructArray(T *data, int count) { m_data = data; m_count = count; }
442 ~StructArray() { safe_free(m_data); }
443
444 int size() { return m_count; }
445 T *get(int index) { return ((index >= 0) && (index < m_count)) ? &m_data[index] : NULL; }
446};
447
448
449//
7f632dfe
VK
450// Auxilliary class for objects which counts references and
451// destroys itself wheren reference count falls to 0
452//
453
454class LIBNETXMS_EXPORTABLE RefCountObject
455{
456private:
457 int m_refCount;
458 MUTEX m_mutex;
459
460public:
461 RefCountObject();
462 virtual ~RefCountObject();
463
464 void incRefCount();
465 void decRefCount();
466};
467
58ee2f43
VK
468
469//
470// Network connection
471//
472
473class LIBNETXMS_EXPORTABLE SocketConnection
474{
475protected:
476 SOCKET m_socket;
477 char m_data[4096];
478 int m_dataPos;
479
480public:
481 SocketConnection();
482 virtual ~SocketConnection();
483
484 bool connectTCP(const TCHAR *hostName, WORD port, DWORD timeout);
485 void disconnect();
486
487 bool canRead(DWORD timeout);
37a83e60 488 int read(char *pBuff, int nSize, DWORD timeout = INFINITE);
58ee2f43
VK
489 bool waitForText(const char *text, int timeout);
490
491 int write(const char *pBuff, int nSize);
1671f85d
VK
492 bool writeLine(const char *line);
493
494 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, DWORD timeout);
58ee2f43
VK
495};
496
b160350d
VK
497#endif /* __cplusplus */
498
499
500//
4826c8a5
VK
501// Configuration item template for configuration loader
502//
503
504typedef struct
505{
018fda4d 506 TCHAR szToken[64];
4826c8a5
VK
507 BYTE iType;
508 BYTE cSeparator; // Separator character for lists
509 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
510 DWORD dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
511 DWORD dwBufferPos; // Should be set to 0
512 void *pBuffer;
513} NX_CFG_TEMPLATE;
dcb44c9a
VK
514
515
516//
bc858635
VK
517// Code translation structure
518//
519
520typedef struct __CODE_TO_TEXT
521{
522 int code;
523 const TCHAR *text;
524} CODE_TO_TEXT;
525
526
527//
3046f9a4
VK
528// getopt() prototype if needed
529//
530
df9812a9 531#if USE_BUNDLED_GETOPT
99e50bb7 532#include <netxms_getopt.h>
3046f9a4
VK
533#endif
534
535
536//
f2fdf1b5
VK
537// Win32 API functions missing under WinCE
538//
539
c32b805a 540#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
541
542inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
543{
544 SYSTEMTIME sysTime;
545
546 GetSystemTime(&sysTime);
547 SystemTimeToFileTime(&sysTime, pFt);
548}
549
550#endif // UNDER_CE
551
552
553//
3d1cb7d6
VK
554// Structures for opendir() / readdir() / closedir()
555//
556
557#ifdef _WIN32
558
7f9e0c00 559typedef struct dirent
3d1cb7d6
VK
560{
561 long d_ino; /* inode number (not used by MS-DOS) */
562 int d_namlen; /* Name length */
563 char d_name[257]; /* file name */
564} _DIRECT;
565
566typedef struct _dir_struc
567{
568 char *start; /* Starting position */
569 char *curr; /* Current position */
570 long size; /* Size of string table */
571 long nfiles; /* number if filenames in table */
7f9e0c00 572 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
573} DIR;
574
5ea2db30
VK
575#ifdef UNICODE
576
577typedef struct dirent_w
578{
579 long d_ino; /* inode number (not used by MS-DOS) */
580 int d_namlen; /* Name length */
581 WCHAR d_name[257]; /* file name */
582} _DIRECTW;
583
584typedef struct _dir_struc_w
585{
586 WCHAR *start; /* Starting position */
587 WCHAR *curr; /* Current position */
588 long size; /* Size of string table */
589 long nfiles; /* number if filenames in table */
590 struct dirent_w dirstr; /* Directory structure to return */
591} DIRW;
592
593#define _TDIR DIRW
594#define _TDIRECT _DIRECTW
595#define _tdirent dirent_w
596
597#else
598
599#define _TDIR DIR
600#define _TDIRECT _DIRECT
601#define _tdirent dirent
602
37d62364
AK
603#endif
604
3d1cb7d6
VK
605#endif /* _WIN32 */
606
607
608//
e9246d26
VK
609// Functions
610//
611
43d92b4c 612
babb8280 613#if WORDS_BIGENDIAN
43d92b4c
VK
614#define htonq(x) (x)
615#define ntohq(x) (x)
9f20696e
VK
616#define htond(x) (x)
617#define ntohd(x) (x)
babb8280
VK
618#define SwapWideString(x)
619#else
ca6ad6bb
VK
620#ifdef HAVE_HTONLL
621#define htonq(x) htonll(x)
622#else
babb8280 623#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
624#endif
625#ifdef HAVE_NTOHLL
626#define ntohq(x) ntohll(x)
627#else
babb8280 628#define ntohq(x) __bswap_64(x)
ca6ad6bb 629#endif
babb8280
VK
630#define htond(x) __bswap_double(x)
631#define ntohd(x) __bswap_double(x)
632#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 633#endif
15929b31 634
55b33d4c
VK
635#ifdef UNDER_CE
636#define close(x) CloseHandle((HANDLE)(x))
637#endif
638
b50f1100 639#ifdef __cplusplus
c81979bc 640#ifndef LIBNETXMS_INLINE
4299401e 641 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
c81979bc 642 {
912b994d
VK
643#if defined(_WIN32) && (_MSC_VER >= 1400)
644 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
645#else
c81979bc
VK
646 _tcsncpy(pszDest, pszSrc, nLen - 1);
647 pszDest[nLen - 1] = 0;
912b994d 648#endif
c81979bc
VK
649 return pszDest;
650 }
651#endif
652#else
7b8b337e
VK
653TCHAR LIBNETXMS_EXPORTABLE *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen);
654#endif
655
656#ifdef __cplusplus
657int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, DWORD timeout);
658int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int, MUTEX);
659int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
660 size_t nSize, int nFlags, DWORD dwTimeout);
c81979bc
VK
661#endif
662
663#ifdef __cplusplus
e9246d26
VK
664extern "C"
665{
b50f1100 666#endif
c5b523e2
VK
667#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
668 DWORD LIBNETXMS_EXPORTABLE __bswap_32(DWORD dwVal);
669#endif
f52be742 670#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
64584976
VK
671 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
672#endif
9f20696e 673 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
273da39a 674 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
64584976 675
076a55c4 676#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc
VK
677#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
678 void LIBNETXMS_EXPORTABLE wcsupr(WCHAR *in);
679#endif
680 void LIBNETXMS_EXPORTABLE strupr(char *in);
7e679c4b 681#endif
ab588c84 682
b8a8d064
VK
683 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
684 int (*compare)(const void *, const void *, void *));
685
670c7f3d 686 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
3aa0061c 687 QWORD LIBNETXMS_EXPORTABLE FileSize(const TCHAR *pszFileName);
670c7f3d 688
64584976 689 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
018fda4d 690 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
e0f99bf0
VK
691#ifdef UNICODE
692 char LIBNETXMS_EXPORTABLE *IpToStrA(DWORD dwAddr, char *szBuffer);
17b35ccc
VK
693#else
694#define IpToStrA IpToStr
e0f99bf0 695#endif
36e44abe 696 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(BYTE *addr, TCHAR *buffer);
60557d06
VK
697 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
698
3aa0061c 699 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(const TCHAR *pszName);
08b214c6
VK
700#ifdef UNICODE
701 DWORD LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *pszName);
702#else
703#define ResolveHostNameA ResolveHostName
704#endif
a1dc75b0 705
64584976 706 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
f218dc6d 707 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
31521248 708
f42b8099 709 TCHAR LIBNETXMS_EXPORTABLE *BinToStr(const BYTE *pData, DWORD dwSize, TCHAR *pStr);
3aa0061c 710 DWORD LIBNETXMS_EXPORTABLE StrToBin(const TCHAR *pStr, BYTE *pData, DWORD dwSize);
f42b8099 711 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *pData, TCHAR *pStr);
b50f1100 712
018fda4d 713 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
0bbab9d3 714 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
018fda4d 715 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
9eea20df 716 BOOL LIBNETXMS_EXPORTABLE RegexpMatch(const TCHAR *pszStr, const TCHAR *pszExpr, BOOL bMatchCase);
35f836fe 717 const TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(const TCHAR *line, TCHAR *buffer);
f3cdd1c5 718 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
3583a400
VK
719 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
720 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
721#ifdef UNICODE
722#define NumChars NumCharsW
723#else
724#define NumChars NumCharsA
725#endif
35f836fe
VK
726 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
727 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
728#ifdef UNICODE
729#define RemoveTrailingCRLF RemoveTrailingCRLFW
730#else
731#define RemoveTrailingCRLF RemoveTrailingCRLFA
732#endif
733
f7794b68
VK
734#ifdef __cplusplus
735 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
736#endif
fa2b47a7 737 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
7618e362 738 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1a5e0c22 739 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
c3cfc039 740 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
3aa0061c 741 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, DWORD *pdwFileSize);
08b214c6
VK
742#ifdef UNICODE
743 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, DWORD *pdwFileSize);
744#else
745#define LoadFileA LoadFile
746#endif
747
6120112b 748 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
a3c76aba 749 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 750 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
a3c76aba 751 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 752 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
3aa0061c
VK
753 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
754 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
755 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, DWORD *pResult);
66c04cc7 756
4e2debcc
VK
757 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
758 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
759
6b1282e7
VK
760 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
761
46ef501a
VK
762 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
763 DWORD *pdwRTT, DWORD dwPacketSize);
4826c8a5 764
e6c91aac
VK
765 /* deprecated:
766 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(const TCHAR *pszFileName, const TCHAR *pszSection,
767 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);*/
3aa0061c 768 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 769
4560d8bd 770 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
aa13cfd9 771 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
4560d8bd 772 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const TCHAR *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 773
2e06d08b
VK
774 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValue(const TCHAR *optString, const TCHAR *option, TCHAR *buffer, int bufSize);
775 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBool(const TCHAR *optString, const TCHAR *option, BOOL defVal);
776 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsInt(const TCHAR *optString, const TCHAR *option, long defVal);
ae9cddaf 777
bc858635
VK
778#ifdef __cplusplus
779 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
780#else
781 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
782#endif
783
aa13cfd9 784#ifdef _WIN32
4299401e 785 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, size_t iBufSize);
ccc34207 786 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
11606542 787 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 788#endif
696fc54f
VK
789
790#if !(HAVE_DAEMON)
b1a57444 791 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 792#endif
4332e9d8 793
3aa0061c 794 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 795
4332e9d8 796#ifndef _WIN32
664c7c49 797 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
3aa0061c 798 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, const WCHAR *pWideCharStr,
4332e9d8
VK
799 int cchWideChar, char *pByteStr, int cchByteChar,
800 char *pDefaultChar, BOOL *pbUsedDefChar);
3aa0061c 801 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, const char *pByteStr,
4332e9d8
VK
802 int cchByteChar, WCHAR *pWideCharStr,
803 int cchWideChar);
04dbaf62 804
6b93c954 805#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
8b86c5dc 806 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
807#endif
808#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
8b86c5dc 809 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
810#endif
811#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
35f836fe 812 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2__tcsdup(const UCS2CHAR *pStr);
4332e9d8 813#endif
8b86c5dc
VK
814
815#ifndef UNICODE
53c96e2d
VK
816 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
817 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
39945910
VK
818 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
819 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc
VK
820#endif
821
04dbaf62 822#ifdef UNICODE
6e17f325
VK
823 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
824 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
825 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
826 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
827 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
828 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62
VK
829#endif
830
3e39fcde 831#endif /* _WIN32 */
8b86c5dc 832
3aa0061c 833 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
c16afb3d 834 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
3aa0061c
VK
835 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
836 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
8b86c5dc 837
3d1058ed
VK
838#ifdef _WITH_ENCRYPTION
839 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
840#endif
841
8b86c5dc 842#ifdef UNICODE_UCS4
53c96e2d
VK
843 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
844 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
845 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
d9b66a56
VK
846 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
847 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 848#endif
8b86c5dc 849
d9b66a56
VK
850#ifdef UNICODE
851
8b86c5dc
VK
852#if !HAVE_WFOPEN
853 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
854#endif
855#if !HAVE_WOPEN
856 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 857#endif
8b86c5dc
VK
858#if !HAVE_WSTAT
859 int wstat(const WCHAR *_path, struct stat *_sbuf);
dde96745 860#endif
ccc34207
VK
861#if !HAVE_WRENAME
862 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
863#endif
864#if !HAVE_WUNLINK
865 int wunlink(const WCHAR *_path);
866#endif
273da39a
VK
867#if !HAVE_WGETENV
868 WCHAR *wgetenv(const WCHAR *_string);
869#endif
d49d7500
VK
870#if !HAVE_WCSERROR && HAVE_STRERROR
871 WCHAR *wcserror(int errnum);
872#endif
873#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06
VK
874#if HAVE_POSIX_STRERROR_R
875 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
876#else
d49d7500
VK
877 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
878#endif
b0823d06 879#endif
e05f8aaf
VK
880
881#endif /* UNICODE */
882
8b86c5dc
VK
883#if !HAVE_STRTOLL
884 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
885#endif
886#if !HAVE_STRTOULL
887 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
888#endif
e05f8aaf
VK
889
890#if !HAVE_WCSTOLL
891 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
892#endif
893#if !HAVE_WCSTOULL
894 QWORD LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
895#endif
b50f1100 896
fa88627b 897#if !HAVE_WCSDUP && !defined(_WIN32)
37861c72
VK
898 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
899#endif
900
3d1cb7d6 901#ifdef _WIN32
5ea2db30
VK
902#ifdef UNICODE
903 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
904 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
905 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
906
907#define _topendir wopendir
908#define _treaddir wreaddir
909#define _tclosedir wclosedir
910#else
911#define _topendir opendir
912#define _treaddir readdir
913#define _tclosedir closedir
914#endif
915
3d1cb7d6 916 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 917 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6 918 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 919
37d62364 920#endif
3d1cb7d6 921
7f9e0c00
VK
922#if defined(_WIN32) || !(HAVE_SCANDIR)
923 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
924 int (*select)(const struct dirent *),
925 int (*compar)(const struct dirent **, const struct dirent **));
926 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
927#endif
928
55b33d4c
VK
929#ifdef UNDER_CE
930 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
931 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
932 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
933#endif
934
9f2902ac 935BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, DWORD flags, const TCHAR *msgModule,
d49d7500 936 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
937void LIBNETXMS_EXPORTABLE nxlog_close(void);
938void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 939BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 940BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
f669df41
VK
941void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(void (*writer)(const TCHAR *, ...));
942
943void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
944void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...);
9f2902ac 945
37a7a862
VK
946#ifdef _WIN32
947int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
948int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
949#ifdef UNICODE
950#define _tmkstemp wmkstemp
951#else
952#define _tmkstemp mkstemp
953#endif
954#endif
955
b50f1100 956#ifdef __cplusplus
e9246d26 957}
b50f1100 958#endif
e9246d26 959
a7ff20a5
VK
960
961//
962// C++ only finctions
963//
964
965#ifdef __cplusplus
966
53c96e2d 967TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *string, int length);
a7ff20a5 968String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *string, int length = -1);
53c96e2d
VK
969const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
970int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
971DWORD LIBNETXMS_EXPORTABLE XMLGetAttrDWORD(const char **attrs, const char *name, DWORD defVal);
972bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
973
974#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
975void LIBNETXMS_EXPORTABLE StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
976 THREAD_RESULT (THREAD_CALL * pfMain)(void *));
977#endif
978
979void LIBNETXMS_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
980 void (* sendTrap1)(DWORD, const char *, va_list),
981 void (* sendTrap2)(DWORD, int, TCHAR **),
982 BOOL (* sendFile)(void *, DWORD, const TCHAR *, long),
983 BOOL (* pushData)(const TCHAR *, const TCHAR *));
a7ff20a5
VK
984
985#endif
986
e9246d26 987#endif /* _nms_util_h_ */