migrated to Zest 2.0; added support for different connection routing types on map...
[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 391 void internalRemove(int index, bool allowDestruction);
cfa7c21f 392 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
16d6f798 393
0d75ea88 394protected:
cfa7c21f 395 void (*m_objectDestructor)(void *);
0d75ea88
VK
396
397public:
398 Array(int initial = 0, int grow = 16, bool owner = false);
399 virtual ~Array();
400
401 int add(void *object);
402 void *get(int index) { return ((index >= 0) && (index < m_size)) ? m_data[index] : NULL; }
47a89886 403 void set(int index, void *object);
24dc5346 404 void replace(int index, void *object);
16d6f798
VK
405 void remove(int index) { internalRemove(index, true); }
406 void unlink(int index) { internalRemove(index, false); }
0d75ea88
VK
407 void clear();
408
409 int size() { return m_size; }
410
411 void setOwner(bool owner) { m_objectOwner = owner; }
412 bool isOwner() { return m_objectOwner; }
413};
414
415template <class T> class ObjectArray : public Array
416{
cfa7c21f
VK
417private:
418 static void destructor(void *object) { delete (T*)object; }
0d75ea88
VK
419
420public:
cfa7c21f 421 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
0d75ea88
VK
422 virtual ~ObjectArray() { }
423
47a89886 424 int add(T *object) { return Array::add((void *)object); }
0d75ea88 425 T *get(int index) { return (T*)Array::get(index); }
47a89886
VK
426 void set(int index, T *object) { Array::set(index, (void *)object); }
427 void replace(int index, T *object) { Array::replace(index, (void *)object); }
0d75ea88
VK
428};
429
7f632dfe
VK
430
431//
cc8ce218
VK
432// Auxilliary class to hold dynamically allocated array of structures
433//
434
435template <class T> class StructArray
436{
437private:
438 int m_count;
439 T *m_data;
440
441public:
442 StructArray(T *data, int count) { m_data = data; m_count = count; }
443 ~StructArray() { safe_free(m_data); }
444
445 int size() { return m_count; }
446 T *get(int index) { return ((index >= 0) && (index < m_count)) ? &m_data[index] : NULL; }
447};
448
449
450//
7f632dfe
VK
451// Auxilliary class for objects which counts references and
452// destroys itself wheren reference count falls to 0
453//
454
455class LIBNETXMS_EXPORTABLE RefCountObject
456{
457private:
458 int m_refCount;
459 MUTEX m_mutex;
460
461public:
462 RefCountObject();
463 virtual ~RefCountObject();
464
465 void incRefCount();
466 void decRefCount();
467};
468
58ee2f43
VK
469
470//
471// Network connection
472//
473
474class LIBNETXMS_EXPORTABLE SocketConnection
475{
476protected:
477 SOCKET m_socket;
478 char m_data[4096];
479 int m_dataPos;
480
481public:
482 SocketConnection();
483 virtual ~SocketConnection();
484
485 bool connectTCP(const TCHAR *hostName, WORD port, DWORD timeout);
486 void disconnect();
487
488 bool canRead(DWORD timeout);
37a83e60 489 int read(char *pBuff, int nSize, DWORD timeout = INFINITE);
58ee2f43
VK
490 bool waitForText(const char *text, int timeout);
491
492 int write(const char *pBuff, int nSize);
1671f85d
VK
493 bool writeLine(const char *line);
494
495 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, DWORD timeout);
58ee2f43
VK
496};
497
b160350d
VK
498#endif /* __cplusplus */
499
500
501//
4826c8a5
VK
502// Configuration item template for configuration loader
503//
504
505typedef struct
506{
018fda4d 507 TCHAR szToken[64];
4826c8a5
VK
508 BYTE iType;
509 BYTE cSeparator; // Separator character for lists
510 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
511 DWORD dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
512 DWORD dwBufferPos; // Should be set to 0
513 void *pBuffer;
514} NX_CFG_TEMPLATE;
dcb44c9a
VK
515
516
517//
bc858635
VK
518// Code translation structure
519//
520
521typedef struct __CODE_TO_TEXT
522{
523 int code;
524 const TCHAR *text;
525} CODE_TO_TEXT;
526
527
528//
3046f9a4
VK
529// getopt() prototype if needed
530//
531
df9812a9 532#if USE_BUNDLED_GETOPT
99e50bb7 533#include <netxms_getopt.h>
3046f9a4
VK
534#endif
535
536
537//
f2fdf1b5
VK
538// Win32 API functions missing under WinCE
539//
540
c32b805a 541#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
542
543inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
544{
545 SYSTEMTIME sysTime;
546
547 GetSystemTime(&sysTime);
548 SystemTimeToFileTime(&sysTime, pFt);
549}
550
551#endif // UNDER_CE
552
553
554//
3d1cb7d6
VK
555// Structures for opendir() / readdir() / closedir()
556//
557
558#ifdef _WIN32
559
7f9e0c00 560typedef struct dirent
3d1cb7d6
VK
561{
562 long d_ino; /* inode number (not used by MS-DOS) */
563 int d_namlen; /* Name length */
564 char d_name[257]; /* file name */
565} _DIRECT;
566
567typedef struct _dir_struc
568{
569 char *start; /* Starting position */
570 char *curr; /* Current position */
571 long size; /* Size of string table */
572 long nfiles; /* number if filenames in table */
7f9e0c00 573 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
574} DIR;
575
5ea2db30
VK
576#ifdef UNICODE
577
578typedef struct dirent_w
579{
580 long d_ino; /* inode number (not used by MS-DOS) */
581 int d_namlen; /* Name length */
582 WCHAR d_name[257]; /* file name */
583} _DIRECTW;
584
585typedef struct _dir_struc_w
586{
587 WCHAR *start; /* Starting position */
588 WCHAR *curr; /* Current position */
589 long size; /* Size of string table */
590 long nfiles; /* number if filenames in table */
591 struct dirent_w dirstr; /* Directory structure to return */
592} DIRW;
593
594#define _TDIR DIRW
595#define _TDIRECT _DIRECTW
596#define _tdirent dirent_w
597
598#else
599
600#define _TDIR DIR
601#define _TDIRECT _DIRECT
602#define _tdirent dirent
603
37d62364
AK
604#endif
605
76b1d3d8
VK
606#else /* not _WIN32 */
607
608typedef struct dirent_w
609{
610 long d_ino; /* inode number */
611 WCHAR d_name[257]; /* file name */
612} _DIRECTW;
613
614typedef struct _dir_struc_w
615{
616 DIR *dir; /* Original non-unicode structure */
617 struct dirent_w dirstr; /* Directory structure to return */
618} DIRW;
619
3d1cb7d6
VK
620#endif /* _WIN32 */
621
622
623//
e9246d26
VK
624// Functions
625//
626
43d92b4c 627
babb8280 628#if WORDS_BIGENDIAN
43d92b4c
VK
629#define htonq(x) (x)
630#define ntohq(x) (x)
9f20696e
VK
631#define htond(x) (x)
632#define ntohd(x) (x)
babb8280
VK
633#define SwapWideString(x)
634#else
ca6ad6bb
VK
635#ifdef HAVE_HTONLL
636#define htonq(x) htonll(x)
637#else
babb8280 638#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
639#endif
640#ifdef HAVE_NTOHLL
641#define ntohq(x) ntohll(x)
642#else
babb8280 643#define ntohq(x) __bswap_64(x)
ca6ad6bb 644#endif
babb8280
VK
645#define htond(x) __bswap_double(x)
646#define ntohd(x) __bswap_double(x)
647#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 648#endif
15929b31 649
55b33d4c
VK
650#ifdef UNDER_CE
651#define close(x) CloseHandle((HANDLE)(x))
652#endif
653
b50f1100 654#ifdef __cplusplus
c81979bc 655#ifndef LIBNETXMS_INLINE
4299401e 656 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
c81979bc 657 {
912b994d
VK
658#if defined(_WIN32) && (_MSC_VER >= 1400)
659 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
660#else
c81979bc
VK
661 _tcsncpy(pszDest, pszSrc, nLen - 1);
662 pszDest[nLen - 1] = 0;
912b994d 663#endif
c81979bc
VK
664 return pszDest;
665 }
666#endif
667#else
7b8b337e
VK
668TCHAR LIBNETXMS_EXPORTABLE *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen);
669#endif
670
671#ifdef __cplusplus
672int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, DWORD timeout);
673int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int, MUTEX);
674int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
675 size_t nSize, int nFlags, DWORD dwTimeout);
c81979bc
VK
676#endif
677
678#ifdef __cplusplus
e9246d26
VK
679extern "C"
680{
b50f1100 681#endif
c5b523e2
VK
682#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
683 DWORD LIBNETXMS_EXPORTABLE __bswap_32(DWORD dwVal);
684#endif
f52be742 685#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
64584976
VK
686 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
687#endif
9f20696e 688 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
273da39a 689 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
64584976 690
076a55c4 691#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc
VK
692#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
693 void LIBNETXMS_EXPORTABLE wcsupr(WCHAR *in);
694#endif
695 void LIBNETXMS_EXPORTABLE strupr(char *in);
7e679c4b 696#endif
ab588c84 697
b8a8d064
VK
698 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
699 int (*compare)(const void *, const void *, void *));
700
670c7f3d 701 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
3aa0061c 702 QWORD LIBNETXMS_EXPORTABLE FileSize(const TCHAR *pszFileName);
670c7f3d 703
64584976 704 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
018fda4d 705 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
e0f99bf0
VK
706#ifdef UNICODE
707 char LIBNETXMS_EXPORTABLE *IpToStrA(DWORD dwAddr, char *szBuffer);
17b35ccc
VK
708#else
709#define IpToStrA IpToStr
e0f99bf0 710#endif
36e44abe 711 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(BYTE *addr, TCHAR *buffer);
60557d06
VK
712 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
713
3aa0061c 714 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(const TCHAR *pszName);
08b214c6
VK
715#ifdef UNICODE
716 DWORD LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *pszName);
717#else
718#define ResolveHostNameA ResolveHostName
719#endif
a1dc75b0 720
64584976 721 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
f218dc6d 722 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
31521248 723
f42b8099 724 TCHAR LIBNETXMS_EXPORTABLE *BinToStr(const BYTE *pData, DWORD dwSize, TCHAR *pStr);
3aa0061c 725 DWORD LIBNETXMS_EXPORTABLE StrToBin(const TCHAR *pStr, BYTE *pData, DWORD dwSize);
f42b8099 726 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *pData, TCHAR *pStr);
b50f1100 727
018fda4d 728 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
0bbab9d3 729 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
018fda4d 730 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
9eea20df 731 BOOL LIBNETXMS_EXPORTABLE RegexpMatch(const TCHAR *pszStr, const TCHAR *pszExpr, BOOL bMatchCase);
35f836fe 732 const TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(const TCHAR *line, TCHAR *buffer);
f3cdd1c5 733 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
3583a400
VK
734 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
735 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
736#ifdef UNICODE
737#define NumChars NumCharsW
738#else
739#define NumChars NumCharsA
740#endif
35f836fe
VK
741 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
742 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
743#ifdef UNICODE
744#define RemoveTrailingCRLF RemoveTrailingCRLFW
745#else
746#define RemoveTrailingCRLF RemoveTrailingCRLFA
747#endif
748
f7794b68
VK
749#ifdef __cplusplus
750 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
751#endif
fa2b47a7 752 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
7618e362 753 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1a5e0c22 754 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
c3cfc039 755 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
3aa0061c 756 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, DWORD *pdwFileSize);
08b214c6
VK
757#ifdef UNICODE
758 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, DWORD *pdwFileSize);
759#else
760#define LoadFileA LoadFile
761#endif
762
6120112b 763 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
a3c76aba 764 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 765 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
a3c76aba 766 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 767 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
3aa0061c
VK
768 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
769 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
770 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, DWORD *pResult);
66c04cc7 771
4e2debcc
VK
772 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
773 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
774
6b1282e7
VK
775 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
776
46ef501a
VK
777 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
778 DWORD *pdwRTT, DWORD dwPacketSize);
4826c8a5 779
e6c91aac
VK
780 /* deprecated:
781 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(const TCHAR *pszFileName, const TCHAR *pszSection,
782 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);*/
3aa0061c 783 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 784
4560d8bd 785 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
aa13cfd9 786 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
4560d8bd 787 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const TCHAR *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 788
2e06d08b
VK
789 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValue(const TCHAR *optString, const TCHAR *option, TCHAR *buffer, int bufSize);
790 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBool(const TCHAR *optString, const TCHAR *option, BOOL defVal);
791 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsInt(const TCHAR *optString, const TCHAR *option, long defVal);
ae9cddaf 792
bc858635
VK
793#ifdef __cplusplus
794 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
795#else
796 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
797#endif
798
aa13cfd9 799#ifdef _WIN32
4299401e 800 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, size_t iBufSize);
ccc34207 801 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
11606542 802 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 803#endif
696fc54f
VK
804
805#if !(HAVE_DAEMON)
b1a57444 806 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 807#endif
4332e9d8 808
3aa0061c 809 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 810
4332e9d8 811#ifndef _WIN32
664c7c49 812 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
3aa0061c 813 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, const WCHAR *pWideCharStr,
4332e9d8
VK
814 int cchWideChar, char *pByteStr, int cchByteChar,
815 char *pDefaultChar, BOOL *pbUsedDefChar);
3aa0061c 816 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, const char *pByteStr,
4332e9d8
VK
817 int cchByteChar, WCHAR *pWideCharStr,
818 int cchWideChar);
04dbaf62 819
6b93c954 820#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
8b86c5dc 821 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
822#endif
823#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
8b86c5dc 824 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
825#endif
826#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
35f836fe 827 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2__tcsdup(const UCS2CHAR *pStr);
4332e9d8 828#endif
8b86c5dc 829
53c96e2d
VK
830 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
831 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
39945910
VK
832 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
833 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 834
04dbaf62 835#ifdef UNICODE
6e17f325
VK
836 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
837 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
838 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
839 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
840 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
841 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62
VK
842#endif
843
3e39fcde 844#endif /* _WIN32 */
8b86c5dc 845
3aa0061c 846 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
c16afb3d 847 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
3aa0061c
VK
848 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
849 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
8b86c5dc 850
3d1058ed
VK
851#ifdef _WITH_ENCRYPTION
852 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
853#endif
854
8b86c5dc 855#ifdef UNICODE_UCS4
53c96e2d
VK
856 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
857 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
858 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
d9b66a56
VK
859 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
860 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 861#endif
8b86c5dc 862
d9b66a56
VK
863#ifdef UNICODE
864
8b86c5dc
VK
865#if !HAVE_WFOPEN
866 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
867#endif
868#if !HAVE_WOPEN
869 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 870#endif
8b86c5dc
VK
871#if !HAVE_WSTAT
872 int wstat(const WCHAR *_path, struct stat *_sbuf);
dde96745 873#endif
ccc34207
VK
874#if !HAVE_WRENAME
875 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
876#endif
877#if !HAVE_WUNLINK
878 int wunlink(const WCHAR *_path);
879#endif
273da39a
VK
880#if !HAVE_WGETENV
881 WCHAR *wgetenv(const WCHAR *_string);
882#endif
d49d7500
VK
883#if !HAVE_WCSERROR && HAVE_STRERROR
884 WCHAR *wcserror(int errnum);
885#endif
886#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06
VK
887#if HAVE_POSIX_STRERROR_R
888 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
889#else
d49d7500
VK
890 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
891#endif
b0823d06 892#endif
e05f8aaf
VK
893
894#endif /* UNICODE */
895
8b86c5dc
VK
896#if !HAVE_STRTOLL
897 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
898#endif
899#if !HAVE_STRTOULL
900 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
901#endif
e05f8aaf
VK
902
903#if !HAVE_WCSTOLL
904 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
905#endif
906#if !HAVE_WCSTOULL
907 QWORD LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
908#endif
b50f1100 909
fa88627b 910#if !HAVE_WCSDUP && !defined(_WIN32)
37861c72
VK
911 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
912#endif
913
3d1cb7d6 914#ifdef _WIN32
5ea2db30
VK
915#ifdef UNICODE
916 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
917 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
918 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
919
920#define _topendir wopendir
921#define _treaddir wreaddir
922#define _tclosedir wclosedir
923#else
924#define _topendir opendir
925#define _treaddir readdir
926#define _tclosedir closedir
927#endif
928
3d1cb7d6 929 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 930 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6 931 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 932
76b1d3d8
VK
933#else /* not _WIN32 */
934
935 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
936 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
937 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
938
37d62364 939#endif
3d1cb7d6 940
7f9e0c00
VK
941#if defined(_WIN32) || !(HAVE_SCANDIR)
942 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
943 int (*select)(const struct dirent *),
944 int (*compar)(const struct dirent **, const struct dirent **));
945 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
946#endif
947
55b33d4c
VK
948#ifdef UNDER_CE
949 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
950 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
951 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
952#endif
953
9f2902ac 954BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, DWORD flags, const TCHAR *msgModule,
d49d7500 955 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
956void LIBNETXMS_EXPORTABLE nxlog_close(void);
957void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 958BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 959BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
f669df41
VK
960void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(void (*writer)(const TCHAR *, ...));
961
962void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
963void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...);
9f2902ac 964
37a7a862
VK
965#ifdef _WIN32
966int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
967int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
968#ifdef UNICODE
969#define _tmkstemp wmkstemp
970#else
971#define _tmkstemp mkstemp
972#endif
973#endif
974
9b13553b
VK
975#ifndef _WIN32
976int strcat_s(char *dst, size_t dstSize, const char *src);
977int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
978#endif
979
b50f1100 980#ifdef __cplusplus
e9246d26 981}
b50f1100 982#endif
e9246d26 983
a7ff20a5
VK
984
985//
986// C++ only finctions
987//
988
989#ifdef __cplusplus
990
53c96e2d 991TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *string, int length);
a7ff20a5 992String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *string, int length = -1);
53c96e2d
VK
993const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
994int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
995DWORD LIBNETXMS_EXPORTABLE XMLGetAttrDWORD(const char **attrs, const char *name, DWORD defVal);
996bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
997
998#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
999void LIBNETXMS_EXPORTABLE StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
1000 THREAD_RESULT (THREAD_CALL * pfMain)(void *));
1001#endif
1002
1003void LIBNETXMS_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
2dd24569
VK
1004 void (* sendTrap1)(DWORD, const TCHAR *, const char *, va_list),
1005 void (* sendTrap2)(DWORD, const TCHAR *, int, TCHAR **),
53c96e2d
VK
1006 BOOL (* sendFile)(void *, DWORD, const TCHAR *, long),
1007 BOOL (* pushData)(const TCHAR *, const TCHAR *));
a7ff20a5
VK
1008
1009#endif
1010
e9246d26 1011#endif /* _nms_util_h_ */