fixed some Windows makefile build issues; added code signing to Windows makefiles
[public/netxms.git] / include / nms_util.h
CommitLineData
cc022855 1/*
7ac71e60 2** NetXMS - Network Management System
7768852d 3** Copyright (C) 2003-2016 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
e9246d26 36#include <nms_common.h>
5023a194 37#include <nxatomic.h>
e9246d26 38#include <nms_cscp.h>
953dfe76 39#include <nms_threads.h>
64584976 40#include <time.h>
e9ef47dd 41#include <jansson.h>
e9246d26 42
bbaae283
VK
43// JSON_EMBED was added in jansson 2.10 - ignore it for older version
44#ifndef JSON_EMBED
45#define JSON_EMBED 0
46#endif
47
1668f7ed
VK
48#if HAVE_POLL_H
49#include <poll.h>
50#endif
51
6d8aee95
VK
52#if HAVE_BYTESWAP_H
53#include <byteswap.h>
54#endif
55
92502d5f
VK
56#include <base64.h>
57
4b47d7d7
VK
58/*** Byte swapping ***/
59#if WORDS_BIGENDIAN
60#define htonq(x) (x)
61#define ntohq(x) (x)
62#define htond(x) (x)
63#define ntohd(x) (x)
c62d158b 64#define SwapUCS2String(x)
4b47d7d7 65#else
c62d158b 66#if HAVE_HTONLL
4b47d7d7
VK
67#define htonq(x) htonll(x)
68#else
c62d158b 69#define htonq(x) bswap_64(x)
4b47d7d7 70#endif
c62d158b 71#if HAVE_NTOHLL
4b47d7d7
VK
72#define ntohq(x) ntohll(x)
73#else
c62d158b 74#define ntohq(x) bswap_64(x)
4b47d7d7 75#endif
c62d158b
VK
76#define htond(x) bswap_double(x)
77#define ntohd(x) bswap_double(x)
669fcb92 78#define SwapUCS2String(x) bswap_array_16((UINT16 *)(x), -1)
4b47d7d7 79#endif
6d8aee95 80
4b47d7d7 81#ifdef __cplusplus
c62d158b
VK
82
83#if !(HAVE_DECL_BSWAP_16)
84inline UINT16 bswap_16(UINT16 val)
85{
86 return (val >> 8) | ((val << 8) & 0xFF00);
87}
4b47d7d7
VK
88#endif
89
c62d158b
VK
90#if !(HAVE_DECL_BSWAP_32)
91inline UINT32 bswap_32(UINT32 val)
92{
93 UINT32 result;
94 BYTE *sptr = (BYTE *)&val;
3c15eedb 95 BYTE *dptr = (BYTE *)&result + 3;
c62d158b
VK
96 for(int i = 0; i < 4; i++, sptr++, dptr--)
97 *dptr = *sptr;
98 return result;
99}
4b47d7d7 100#endif
c62d158b
VK
101
102#if !(HAVE_DECL_BSWAP_64)
103inline UINT64 bswap_64(UINT64 val)
104{
105 UINT64 result;
106 BYTE *sptr = (BYTE *)&val;
107 BYTE *dptr = (BYTE *)&result + 7;
108 for(int i = 0; i < 8; i++, sptr++, dptr--)
109 *dptr = *sptr;
110 return result;
111}
4b47d7d7 112#endif
6d8aee95 113
c62d158b
VK
114inline double bswap_double(double val)
115{
116 double result;
117 BYTE *sptr = (BYTE *)&val;
118 BYTE *dptr = (BYTE *)&result + 7;
119 for(int i = 0; i < 8; i++, sptr++, dptr--)
120 *dptr = *sptr;
121 return result;
4b47d7d7 122}
c62d158b 123
96772780
VK
124#else
125
126#if !(HAVE_DECL_BSWAP_16)
127UINT16 LIBNETXMS_EXPORTABLE __bswap_16(UINT16 val);
128#define bswap_16 __bswap_16
129#endif
130
131#if !(HAVE_DECL_BSWAP_32)
132UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 val);
133#define bswap_32 __bswap_32
134#endif
135
136#if !(HAVE_DECL_BSWAP_64)
137UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 val);
138#define bswap_64 __bswap_64
139#endif
140
141#endif
142
143#ifdef __cplusplus
144extern "C" {
145#endif
c62d158b
VK
146void LIBNETXMS_EXPORTABLE bswap_array_16(UINT16 *v, int len);
147void LIBNETXMS_EXPORTABLE bswap_array_32(UINT32 *v, int len);
96772780
VK
148#ifdef __cplusplus
149}
4b47d7d7
VK
150#endif
151
152
153/*** Serial communications ***/
6d8aee95
VK
154#ifdef _WIN32
155
156#define FLOW_NONE 0
157#define FLOW_SOFTWARE 1
158#define FLOW_HARDWARE 2
159
160#else /* _WIN32 */
161
70fed300
AK
162#ifdef HAVE_TERMIOS_H
163# include <termios.h>
164#else
165# error termios.h not found
166#endif
167
6d8aee95
VK
168#endif /* _WIN32 */
169
e2e66145
VK
170/**
171 * Return codes for IcmpPing()
172 */
66c04cc7
VK
173#define ICMP_SUCCESS 0
174#define ICMP_UNREACHEABLE 1
175#define ICMP_TIMEOUT 2
176#define ICMP_RAW_SOCK_FAILED 3
5418d4bb 177#define ICMP_API_ERROR 4
66c04cc7 178
e2e66145
VK
179/**
180 * Token types for configuration loader
181 */
4826c8a5
VK
182#define CT_LONG 0
183#define CT_STRING 1
184#define CT_STRING_LIST 2
185#define CT_END_OF_LIST 3
186#define CT_BOOLEAN 4
187#define CT_WORD 5
fc935a60 188#define CT_IGNORE 6
dda7c270 189#define CT_MB_STRING 7
e95680e5 190#define CT_BOOLEAN64 8
12f43871
VK
191#define CT_SIZE_BYTES 9 /* 64 bit integer, automatically converts K, M, G, T suffixes using 1024 as base) */
192#define CT_SIZE_UNITS 10 /* 64 bit integer, automatically converts K, M, G, T suffixes using 1000 as base) */
4826c8a5 193
605fc935 194/**
e2e66145
VK
195 * Uninitialized value for override indicator
196 */
197#define NXCONFIG_UNINITIALIZED_VALUE (-1)
198
199/**
605fc935
VK
200 * Return codes for NxLoadConfig()
201 */
4826c8a5
VK
202#define NXCFG_ERR_OK 0
203#define NXCFG_ERR_NOFILE 1
204#define NXCFG_ERR_SYNTAX 2
205
605fc935
VK
206/**
207 * nxlog_open() flags
208 */
4e0e77e6
VK
209#define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
210#define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
211#define NXLOG_BACKGROUND_WRITER ((UINT32)0x00000004)
70be5423 212#define NXLOG_DEBUG_MODE ((UINT32)0x00000008)
4e0e77e6 213#define NXLOG_IS_OPEN ((UINT32)0x80000000)
9f2902ac 214
605fc935
VK
215/**
216 * nxlog rotation policy
217 */
5945d50a
VK
218#define NXLOG_ROTATION_DISABLED 0
219#define NXLOG_ROTATION_DAILY 1
220#define NXLOG_ROTATION_BY_SIZE 2
221
605fc935 222/**
7768852d
VK
223 * Custom wcslen()
224 */
225#if !HAVE_WCSLEN
226
227#ifdef __cplusplus
228extern "C" {
229#endif
230size_t LIBNETXMS_EXPORTABLE wcslen(const WCHAR *s);
231#ifdef __cplusplus
232}
233#endif
234
235#endif
236
237/**
238 * Custom wcsncpy()
239 */
240#if !HAVE_WCSNCPY
241
242#ifdef __cplusplus
243extern "C" {
244#endif
245WCHAR LIBNETXMS_EXPORTABLE *wcsncpy(WCHAR *dest, const WCHAR *src, size_t n);
246#ifdef __cplusplus
247}
248#endif
249
250#endif
251
252/**
605fc935
VK
253 * _tcsdup() replacement
254 */
8ff13508
VK
255#if defined(_WIN32) && defined(USE_WIN32_HEAP)
256#ifdef __cplusplus
257extern "C" {
258#endif
fd72502a 259char LIBNETXMS_EXPORTABLE *nx_strdup(const char *src);
8ff13508
VK
260WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
261#ifdef __cplusplus
262}
263#endif
264#endif
265
605fc935 266/**
f4d2c9ba
VK
267 * Custom wcsdup
268 */
7768852d 269#if !HAVE_WCSDUP
fd72502a 270
f4d2c9ba
VK
271#ifdef __cplusplus
272extern "C" {
273#endif
7768852d 274WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
f4d2c9ba
VK
275#ifdef __cplusplus
276}
277#endif
fd72502a
VK
278
279#elif defined(_AIX)
280
281// Some AIX versions have broken wcsdup() so we use internal implementation
282#ifdef __cplusplus
283extern "C" {
284#endif
7768852d 285WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
fd72502a
VK
286#ifdef __cplusplus
287}
288#endif
289
290#define wcsdup nx_wcsdup
291
f4d2c9ba
VK
292#endif
293
c012b7b7
VK
294#ifdef __cplusplus
295
f4d2c9ba 296/**
c83f6449
VK
297 * Extended tcsdup: returns NULL if NULL pointer passed
298 */
299inline TCHAR *_tcsdup_ex(const TCHAR *s)
300{
301 return (s != NULL) ? _tcsdup(s) : NULL;
302}
303
c012b7b7
VK
304#endif
305
7768852d
VK
306/******* UNICODE related conversion and helper functions *******/
307
d65c16a8
VK
308#ifdef __cplusplus
309extern "C" {
310#endif
7768852d
VK
311
312// Basic string functions
313#ifdef UNICODE_UCS2
314
315#define ucs2_strlen wcslen
316#define ucs2_strncpy wcsncpy
317#define ucs2_strdup wcsdup
318
319size_t LIBNETXMS_EXPORTABLE ucs4_strlen(const UCS4CHAR *s);
320UCS4CHAR LIBNETXMS_EXPORTABLE *ucs4_strncpy(UCS4CHAR *dest, const UCS2CHAR *src, size_t n);
321UCS4CHAR LIBNETXMS_EXPORTABLE *ucs4_strdup(const UCS4CHAR *src);
322
323#else
324
325size_t LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *s);
326UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *dest, const UCS2CHAR *src, size_t n);
327UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strdup(const UCS2CHAR *src);
328
329#define ucs4_strlen wcslen
330#define ucs4_strncpy wcsncpy
331#define ucs4_strdup wcsdup
332
333#endif
334
335// Character conversion functions
336int LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, UCS4CHAR *dst, int dstLen);
337#if defined(_WIN32) || defined(UNICODE_UCS2)
338#define ucs2_to_utf8(wstr, wlen, mstr, mlen) WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, mstr, mlen, NULL, NULL)
339#define ucs2_to_mb(wstr, wlen, mstr, mlen) WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wstr, wlen, mstr, mlen, NULL, NULL)
340#else
341int LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
342int LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
343#endif
344
345int LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const UCS4CHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
346#ifdef UNICODE_UCS4
347#define ucs4_to_utf8(wstr, wlen, mstr, mlen) WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, mstr, mlen, NULL, NULL)
348#define ucs4_to_mb(wstr, wlen, mstr, mlen) WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wstr, wlen, mstr, mlen, NULL, NULL)
349#else
350int LIBNETXMS_EXPORTABLE ucs4_to_utf8(const UCS4CHAR *src, int srcLen, char *dst, int dstLen);
351int LIBNETXMS_EXPORTABLE ucs4_to_mb(const UCS4CHAR *src, int srcLen, char *dst, int dstLen);
352#endif
353
354#ifdef UNICODE_UCS4
355#define utf8_to_ucs4(mstr, mlen, wstr, wlen) MultiByteToWideChar(CP_UTF8, 0, mstr, mlen, wstr, wlen)
356#else
357int LIBNETXMS_EXPORTABLE utf8_to_ucs4(const char *src, int srcLen, UCS4CHAR *dst, int dstLen);
358#endif
359#if defined(_WIN32) || defined(UNICODE_UCS2)
360#define utf8_to_ucs2(mstr, mlen, wstr, wlen) MultiByteToWideChar(CP_UTF8, 0, mstr, mlen, wstr, wlen)
361#else
362int LIBNETXMS_EXPORTABLE utf8_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
363#endif
364int LIBNETXMS_EXPORTABLE utf8_to_mb(const char *src, int srcLen, char *dst, int dstLen);
365
366#ifdef UNICODE_UCS4
367#define mb_to_ucs4(mstr, mlen, wstr, wlen) MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mstr, mlen, wstr, wlen)
368#else
369int LIBNETXMS_EXPORTABLE mb_to_ucs4(const char *src, int srcLen, UCS4CHAR *dst, int dstLen);
370#endif
371#if defined(_WIN32) || defined(UNICODE_UCS2)
372#define mb_to_ucs2(mstr, mlen, wstr, wlen) MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mstr, mlen, wstr, wlen)
373#else
374int LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
375#endif
376int LIBNETXMS_EXPORTABLE mb_to_utf8(const char *src, int srcLen, char *dst, int dstLen);
377
378// Conversion helpers
379WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *src);
380WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBStringSysLocale(const char *src);
381WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *src);
382char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *src);
383char LIBNETXMS_EXPORTABLE *MBStringFromWideStringSysLocale(const WCHAR *src);
f25381f3 384char LIBNETXMS_EXPORTABLE *MBStringFromUTF8String(const char *src);
7768852d 385char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *src);
f25381f3 386char LIBNETXMS_EXPORTABLE *UTF8StringFromMBString(const char *src);
7768852d
VK
387UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const UCS4CHAR *src);
388UCS4CHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *src);
389
390#ifdef UNICODE_UCS2
391#define UCS2StringFromMBString WideStringFromMBString
392#define MBStringFromUCS2String MBStringFromWideString
393#define UCS2StringFromUTF8String WideStringFromUTF8String
394#define UTF8StringFromUCS2String UTF8StringFromWideString
395#else
396UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *src);
397char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *src);
398UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUTF8String(const char *src);
399char LIBNETXMS_EXPORTABLE *UTF8StringFromUCS2String(const UCS2CHAR *src);
400#endif
401
402#ifdef UNICODE_UCS4
403#define UCS4StringFromMBString WideStringFromMBString
404#define MBStringFromUCS4String MBStringFromWideString
405#else
406UCS4CHAR LIBNETXMS_EXPORTABLE *UCS4StringFromMBString(const char *src);
407char LIBNETXMS_EXPORTABLE *MBStringFromUCS4String(const UCS4CHAR *src);
d65c16a8
VK
408#endif
409
f38a7636 410#ifdef UNICODE
7fcdcb0c 411#define UTF8StringFromTString(s) UTF8StringFromWideString(s)
f38a7636 412#else
7fcdcb0c 413#define UTF8StringFromTString(s) UTF8StringFromMBString(s)
f38a7636
AK
414#endif
415
7768852d
VK
416#ifdef __cplusplus
417}
418#endif
419
420/******* end of UNICODE related conversion and helper functions *******/
421
820fd491 422
d65c16a8 423/**
605fc935
VK
424 * Class for serial communications
425 */
b160350d
VK
426#ifdef __cplusplus
427
428#ifndef _WIN32
429enum
430{
431 NOPARITY,
432 ODDPARITY,
433 EVENPARITY,
434 ONESTOPBIT,
435 TWOSTOPBITS
436};
437
70fed300
AK
438enum
439{
440 FLOW_NONE,
441 FLOW_HARDWARE,
442 FLOW_SOFTWARE
443};
444
b160350d
VK
445#ifndef INVALID_HANDLE_VALUE
446#define INVALID_HANDLE_VALUE (-1)
447#endif
448#endif /* _WIN32 */
449
450class LIBNETXMS_EXPORTABLE Serial
451{
b160350d
VK
452private:
453 TCHAR *m_pszPort;
454 int m_nTimeout;
455 int m_nSpeed;
456 int m_nDataBits;
457 int m_nStopBits;
458 int m_nParity;
70fed300 459 int m_nFlowControl;
96784db1
VK
460 int m_writeBlockSize;
461 int m_writeDelay;
b160350d
VK
462
463#ifndef _WIN32
464 int m_hPort;
70fed300 465 struct termios m_originalSettings;
b160350d
VK
466#else
467 HANDLE m_hPort;
468#endif
b160350d 469
96784db1
VK
470 bool writeBlock(const char *data, int length);
471
826fcc1f
VK
472public:
473 Serial();
474 ~Serial();
475
476 bool open(const TCHAR *pszPort);
477 void close();
478 void setTimeout(int nTimeout);
479 int read(char *pBuff, int nSize); /* waits up to timeout and do single read */
480 int readAll(char *pBuff, int nSize); /* read until timeout or out of space */
c3f71625 481 int readToMark(char *buff, int size, const char **marks, char **occurence);
96784db1 482 bool write(const char *buffer, int length);
826fcc1f
VK
483 void flush();
484 bool set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl = FLOW_NONE);
96784db1
VK
485 void setWriteBlockSize(int bs) { m_writeBlockSize = bs; }
486 void setWriteDelay(int delay) { m_writeDelay = delay; }
826fcc1f
VK
487 bool restart();
488};
ff214673 489
3fe40613 490/**
600c3522 491 * Class that can store any object with connected to it mutex
492 */
493template <class T> class ObjectLock
494{
495private:
a7f8424a
VK
496 Mutex m_mutex;
497 T *m_object;
498
600c3522 499public:
a7f8424a
VK
500 ObjectLock() { m_object = NULL; }
501 ObjectLock(T *object) { m_object = object; }
502 ObjectLock(const ObjectLock<T> &src) { m_object = src.m_object; m_mutex = src.m_mutex; }
503
504 void lock() { m_mutex.lock(); }
505 void unlock() { m_mutex.unlock(); }
506
507 T *get() { return m_object; }
508 void set(T *object) { m_object = object; }
509
510 operator T*() { return m_object; }
511 ObjectLock<T>& operator =(const ObjectLock<T> &src)
512 {
513 m_object = src.m_object;
514 m_mutex = src.m_mutex;
515 return *this;
516 }
600c3522 517};
518
519/**
3f088d52
VK
520 * Pair class (stores pair of items)
521 */
522template <typename T1, typename T2> class Pair
523{
524private:
525 T1 value1;
526 T2 value2;
527
528public:
529 Pair() { }
530 Pair(const T1& v1, const T2& v2)
531 {
532 value1 = v1;
533 value2 = v2;
534 }
535 Pair(const Pair<T1, T2>& src)
536 {
537 value1 = src.value1;
538 value2 = src.value2;
539 }
540
541 Pair<T1, T2>& operator=(const Pair<T1, T2> &src)
542 {
543 return Pair<T1, T2>(src.value1, src.value2);
544 }
545
546 const T1& first() const { return value1; }
547 const T2& second() const { return value2; }
548};
549
550/**
3fe40613
VK
551 * Dynamic string class
552 */
8bad8476
VK
553class LIBNETXMS_EXPORTABLE String
554{
555protected:
4e0e77e6
VK
556 TCHAR *m_buffer;
557 size_t m_length;
558 size_t m_allocated;
dcc7a2b0 559 size_t m_allocationStep;
8bad8476
VK
560
561public:
c577e360
VK
562 static const int npos;
563
8bad8476 564 String();
f3cdd1c5 565 String(const TCHAR *init);
643c9dcb 566 String(const String &src);
8bad8476
VK
567 ~String();
568
4e0e77e6
VK
569 TCHAR *getBuffer() { return m_buffer; }
570 void setBuffer(TCHAR *buffer);
8bad8476 571
dcc7a2b0
VK
572 size_t getAllocationStep() { return m_allocationStep; }
573 void setAllocationStep(size_t step) { m_allocationStep = step; }
4e0e77e6 574
3f088d52
VK
575 String& operator =(const TCHAR *str);
576 String& operator =(const String &src);
577 String& operator +=(const TCHAR *str);
578 String& operator +=(const String &str);
18a7c1e9
VK
579 String operator +(const String &right) const;
580 String operator +(const TCHAR *right) const;
5508ef9f 581 operator const TCHAR*() const { return CHECK_NULL_EX(m_buffer); }
8bad8476 582
3f088d52
VK
583 bool operator ==(const String &s) const { return equals(s); }
584 bool operator !=(const String &s) const { return !equals(s); }
585
08b214c6
VK
586 char *getUTF8String();
587
4e0e77e6
VK
588 void append(const TCHAR *str) { if (str != NULL) append(str, _tcslen(str)); }
589 void append(const TCHAR *str, size_t len);
f98a8402 590 void append(const TCHAR c) { append(&c, 1); }
5f648670
VK
591 void append(INT32 n);
592 void append(UINT32 n);
593 void append(INT64 n);
594 void append(UINT64 n);
4e0e77e6
VK
595
596 void appendPreallocated(TCHAR *str) { if (str != NULL) { append(str); free(str); } }
597
598 void appendMBString(const char *str, size_t len, int nCodePage);
599 void appendWideString(const WCHAR *str, size_t len);
c577e360 600
4e0e77e6
VK
601 void appendFormattedString(const TCHAR *format, ...);
602 void appendFormattedStringV(const TCHAR *format, va_list args);
ff175e91 603
4e0e77e6 604 void clear();
c577e360 605
4e0e77e6
VK
606 size_t length() const { return m_length; }
607 bool isEmpty() const { return m_length == 0; }
c577e360 608
3f088d52 609 bool equals(const String& s) const;
18a7c1e9
VK
610 bool equals(const TCHAR *s) const;
611 bool startsWith(const String& s) const;
612 bool startsWith(const TCHAR *s) const;
613 bool endsWith(const String& s) const;
614 bool endsWith(const TCHAR *s) const;
3f088d52 615
7ac965a4 616 wchar_t charAt(size_t pos) const { return (pos < m_length) ? m_buffer[pos] : 0; }
807a4c40 617
3f088d52
VK
618 String substring(size_t start, int len) const;
619 TCHAR *substring(size_t start, int len, TCHAR *buffer) const;
c3c060b8 620 String left(size_t len) const { return substring(0, (int)len); }
ed4e267e 621 String right(size_t len) const { return substring((m_length > len) ? m_length - len : 0, (int)len); }
3f088d52 622
18a7c1e9
VK
623 StringList *split(const TCHAR *separator) const;
624
3f088d52 625 int find(const TCHAR *str, size_t start = 0) const;
1e1450ef 626
6bcafa2d
VK
627 void escapeCharacter(int ch, int esc);
628 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
0bbab9d3
VK
629 void trim();
630 void shrink(int chars = 1);
c577e360
VK
631};
632
3fe40613 633/**
df26f039
VK
634 * Abstract iterator class (to be implemented by actual iterable class)
635 */
636class LIBNETXMS_EXPORTABLE AbstractIterator
637{
638public:
c5e1f0bb
VK
639 virtual ~AbstractIterator();
640
df26f039
VK
641 virtual bool hasNext() = 0;
642 virtual void *next() = 0;
e0f7dbec 643 virtual void remove() = 0;
df26f039
VK
644};
645
646/**
647 * Iterator class (public interface for iteration)
648 */
649template <class T> class Iterator
650{
651private:
652 AbstractIterator *m_worker;
653
654public:
655 Iterator(AbstractIterator *worker) { m_worker = worker; }
656 ~Iterator() { delete m_worker; }
657
658 bool hasNext() { return m_worker->hasNext(); }
659 T *next() { return (T *)m_worker->next(); }
e0f7dbec 660 void remove() { m_worker->remove(); }
df26f039
VK
661};
662
663/**
f9861ff9
VK
664 * Dynamic array class
665 */
666class LIBNETXMS_EXPORTABLE Array
667{
668private:
669 int m_size;
670 int m_allocated;
671 int m_grow;
672 size_t m_elementSize;
673 void **m_data;
674 bool m_objectOwner;
675
676 void internalRemove(int index, bool allowDestruction);
677 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
678
679protected:
680 bool m_storePointers;
681 void (*m_objectDestructor)(void *);
682
683 Array(void *data, int initial, int grow, size_t elementSize);
9f24dfe8
EJ
684 Array(const Array *src);
685
dcc7a2b0 686 void *__getBuffer() const { return m_data; }
f9861ff9
VK
687
688public:
689 Array(int initial = 0, int grow = 16, bool owner = false);
690 virtual ~Array();
691
692 int add(void *element);
dcc7a2b0
VK
693 void *get(int index) const { return ((index >= 0) && (index < m_size)) ? (m_storePointers ? m_data[index] : (void *)((char *)m_data + index * m_elementSize)): NULL; }
694 int indexOf(void *element) const;
f9861ff9
VK
695 void set(int index, void *element);
696 void replace(int index, void *element);
3f088d52 697 void insert(int index, void *element);
f9861ff9
VK
698 void remove(int index) { internalRemove(index, true); }
699 void remove(void *element) { internalRemove(indexOf(element), true); }
700 void unlink(int index) { internalRemove(index, false); }
701 void unlink(void *element) { internalRemove(indexOf(element), false); }
702 void clear();
703 void sort(int (*cb)(const void *, const void *));
dcc7a2b0 704 void *find(const void *key, int (*cb)(const void *, const void *)) const;
f9861ff9 705
dcc7a2b0 706 int size() const { return m_size; }
18a7c1e9 707 bool isEmpty() const { return m_size == 0; }
f9861ff9
VK
708
709 void setOwner(bool owner) { m_objectOwner = owner; }
dcc7a2b0 710 bool isOwner() const { return m_objectOwner; }
f9861ff9
VK
711};
712
713/**
df26f039
VK
714 * Array iterator class
715 */
716class LIBNETXMS_EXPORTABLE ArrayIterator : public AbstractIterator
717{
718private:
e0f7dbec 719 Array *m_array;
df26f039
VK
720 int m_pos;
721
722public:
e0f7dbec 723 ArrayIterator(Array *array);
df26f039
VK
724
725 virtual bool hasNext();
726 virtual void *next();
e0f7dbec 727 virtual void remove();
df26f039
VK
728};
729
730/**
f9861ff9
VK
731 * Template class for dynamic array which holds objects
732 */
733template <class T> class ObjectArray : public Array
734{
735private:
736 static void destructor(void *object) { delete (T*)object; }
737
738public:
739 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
740 virtual ~ObjectArray() { }
741
742 int add(T *object) { return Array::add((void *)object); }
dcc7a2b0 743 T *get(int index) const { return (T*)Array::get(index); }
18a7c1e9
VK
744 T *first() const { return get(0); }
745 T *last() const { return get(size() - 1); }
dcc7a2b0 746 int indexOf(T *object) const { return Array::indexOf((void *)object); }
dfed892b 747 bool contains(T *object) const { return indexOf(object) >= 0; }
f9861ff9
VK
748 void set(int index, T *object) { Array::set(index, (void *)object); }
749 void replace(int index, T *object) { Array::replace(index, (void *)object); }
3f088d52 750 void insert(int index, T *object) { Array::insert(index, (void *)object); }
f9861ff9
VK
751 void remove(int index) { Array::remove(index); }
752 void remove(T *object) { Array::remove((void *)object); }
753 void unlink(int index) { Array::unlink(index); }
754 void unlink(T *object) { Array::unlink((void *)object); }
df26f039 755
fa01c3b6
VK
756 void sort(int (*cb)(const T **, const T **)) { Array::sort((int (*)(const void *, const void *))cb); }
757 T *find(const T *key, int (*cb)(const T **, const T **)) const
758 {
759 T **result = (T **)Array::find(&key, (int (*)(const void *, const void *))cb);
760 return (result != NULL) ? *result : NULL;
761 }
762
e0f7dbec 763 Iterator<T> *iterator() { return new Iterator<T>(new ArrayIterator(this)); }
f9861ff9
VK
764};
765
766/**
9c144154
VK
767 * Template class for dynamic array which holds references to objects with inaccessible destructors
768 */
769template <class T> class ObjectRefArray : public Array
770{
771private:
772 static void destructor(void *object) { }
773
774public:
775 ObjectRefArray(int initial = 0, int grow = 16) : Array(initial, grow, false) { m_objectDestructor = destructor; }
776 virtual ~ObjectRefArray() { }
777
778 int add(T *object) { return Array::add((void *)object); }
779 T *get(int index) const { return (T*)Array::get(index); }
780 int indexOf(T *object) const { return Array::indexOf((void *)object); }
781 bool contains(T *object) const { return indexOf(object) >= 0; }
782 void set(int index, T *object) { Array::set(index, (void *)object); }
783 void replace(int index, T *object) { Array::replace(index, (void *)object); }
784 void remove(int index) { Array::remove(index); }
785 void remove(T *object) { Array::remove((void *)object); }
786 void unlink(int index) { Array::unlink(index); }
787 void unlink(T *object) { Array::unlink((void *)object); }
788
789 Iterator<T> *iterator() { return new Iterator<T>(new ArrayIterator(this)); }
790};
791
792/**
f9861ff9
VK
793 * Template class for dynamic array which holds scalar values
794 */
795template <class T> class IntegerArray : public Array
796{
797private:
798 static void destructor(void *element) { }
799
800public:
801 IntegerArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; m_storePointers = (sizeof(T) == sizeof(void *)); }
9f24dfe8 802 IntegerArray(const IntegerArray<T> *src) : Array(src) { }
f9861ff9
VK
803 virtual ~IntegerArray() { }
804
805 int add(T value) { return Array::add(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
e5390fb5 806 T get(int index) const { if (m_storePointers) return CAST_FROM_POINTER(Array::get(index), T); T *p = (T*)Array::get(index); return (p != NULL) ? *p : 0; }
dcc7a2b0 807 int indexOf(T value) const { return Array::indexOf(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
dfed892b 808 bool contains(T value) const { return indexOf(value) >= 0; }
f9861ff9
VK
809 void set(int index, T value) { Array::set(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
810 void replace(int index, T value) { Array::replace(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
811
dcc7a2b0 812 T *getBuffer() const { return (T*)__getBuffer(); }
e9ef47dd 813
6336bba3 814 json_t *toJson() const { json_t *a = json_array(); for(int i = 0; i < size(); i++) json_array_append_new(a, json_integer(get(i))); return a; }
f9861ff9
VK
815};
816
817/**
818 * Auxilliary class to hold dynamically allocated array of structures
819 */
820template <class T> class StructArray : public Array
821{
822private:
823 static void destructor(void *element) { }
824
825public:
826 StructArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; }
827 StructArray(T *data, int size) : Array(data, size, 16, sizeof(T)) { m_objectDestructor = destructor; }
9f24dfe8 828 StructArray(const StructArray<T> *src) : Array(src) { }
f9861ff9
VK
829 virtual ~StructArray() { }
830
831 int add(T *element) { return Array::add((void *)element); }
dcc7a2b0
VK
832 T *get(int index) const { return (T*)Array::get(index); }
833 int indexOf(T *element) const { return Array::indexOf((void *)element); }
dfed892b 834 bool contains(T *element) const { return indexOf(element) >= 0; }
f9861ff9
VK
835 void set(int index, T *element) { Array::set(index, (void *)element); }
836 void replace(int index, T *element) { Array::replace(index, (void *)element); }
837 void remove(int index) { Array::remove(index); }
838 void remove(T *element) { Array::remove((void *)element); }
839 void unlink(int index) { Array::unlink(index); }
840 void unlink(T *element) { Array::unlink((void *)element); }
841
dcc7a2b0 842 T *getBuffer() const { return (T*)__getBuffer(); }
f9861ff9
VK
843};
844
845/**
846 * Entry of string map (internal)
400e55c4
VK
847 */
848struct StringMapEntry;
849
850/**
f9861ff9
VK
851 * Key/value pair
852 */
853struct KeyValuePair
854{
855 const TCHAR *key;
856 const void *value;
857};
858
859/**
3fe40613
VK
860 * String maps base class
861 */
862class LIBNETXMS_EXPORTABLE StringMapBase
c577e360
VK
863{
864protected:
400e55c4 865 StringMapEntry *m_data;
3fe40613 866 bool m_objectOwner;
55bdca5a 867 bool m_ignoreCase;
3fe40613 868 void (*m_objectDestructor)(void *);
c577e360 869
93b48f0e 870 StringMapEntry *find(const TCHAR *key, int keyLen) const;
3fe40613 871 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
6e14de7c 872 void *getObject(const TCHAR *key) const;
93b48f0e 873 void *getObject(const TCHAR *key, size_t len) const;
3fe40613 874 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
c577e360
VK
875
876public:
3fe40613
VK
877 StringMapBase(bool objectOwner);
878 virtual ~StringMapBase();
879
55bdca5a 880 void setOwner(bool owner) { m_objectOwner = owner; }
91a581c5 881 void setIgnoreCase(bool ignore);
55bdca5a 882
3fe40613
VK
883 void remove(const TCHAR *key);
884 void clear();
c07cf3be 885 void filterElements(bool (*filter)(const TCHAR *, const void *, void *), void *userData);
3fe40613 886
6e14de7c 887 int size() const;
93b48f0e
EJ
888 bool contains(const TCHAR *key) const { return (key != NULL) ? (find(key, (int)_tcslen(key) * sizeof(TCHAR)) != NULL) : false; }
889 bool contains(const TCHAR *key, size_t len) const { return (key != NULL) ? (find(key, (int)len * sizeof(TCHAR)) != NULL) : false; }
400e55c4 890
6e14de7c
VK
891 EnumerationCallbackResult forEach(EnumerationCallbackResult (*cb)(const TCHAR *, const void *, void *), void *userData) const;
892 const void *findElement(bool (*comparator)(const TCHAR *, const void *, void *), void *userData) const;
f9861ff9 893
6e14de7c
VK
894 StructArray<KeyValuePair> *toArray() const;
895 StringList *keys() const;
3fe40613
VK
896};
897
898/**
400e55c4
VK
899 * NXCP message class
900 */
b368969c 901class NXCPMessage;
400e55c4
VK
902
903/**
3fe40613
VK
904 * String map class
905 */
906class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
907{
908public:
909 StringMap() : StringMapBase(true) { }
fb986055 910 StringMap(const StringMap &src);
3fe40613 911 virtual ~StringMap();
c577e360 912
fb986055 913 StringMap& operator =(const StringMap &src);
40b7416c 914
3fe40613
VK
915 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
916 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
967893bb 917 void set(const TCHAR *key, UINT32 value);
5ad2167d 918
dedeffb3 919 void addAll(const StringMap *src);
a6312bd6 920
6e14de7c 921 const TCHAR *get(const TCHAR *key) const { return (const TCHAR *)getObject(key); }
93b48f0e 922 const TCHAR *get(const TCHAR *key, size_t len) const { return (const TCHAR *)getObject(key, len); }
7ec22f81
VK
923 INT32 getInt32(const TCHAR *key, INT32 defaultValue) const;
924 UINT32 getUInt32(const TCHAR *key, UINT32 defaultValue) const;
925 INT64 getInt64(const TCHAR *key, INT64 defaultValue) const;
926 UINT64 getUInt64(const TCHAR *key, UINT64 defaultValue) const;
927 double getDouble(const TCHAR *key, double defaultValue) const;
6e14de7c 928 bool getBoolean(const TCHAR *key, bool defaultValue) const;
5ad2167d 929
6e14de7c 930 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
ca0a165b 931 void loadMessage(const NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId);
6336bba3 932
88e14cac 933 json_t *toJson() const;
8bad8476
VK
934};
935
3fe40613
VK
936/**
937 * String map template for holding objects as values
938 */
939template <class T> class StringObjectMap : public StringMapBase
940{
941private:
942 static void destructor(void *object) { delete (T*)object; }
6173bea8 943
3fe40613
VK
944public:
945 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
946
947 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
b7c8729b 948 void setPreallocated(TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, true); }
6e14de7c 949 T *get(const TCHAR *key) const { return (T*)getObject(key); }
93b48f0e 950 T *get(const TCHAR *key, size_t len) const { return (T*)getObject(key, len); }
3fe40613 951};
6173bea8 952
3fe40613
VK
953/**
954 * String list class
955 */
6173bea8
VK
956class LIBNETXMS_EXPORTABLE StringList
957{
958private:
959 int m_count;
960 int m_allocated;
961 TCHAR **m_values;
962
963public:
964 StringList();
01116597 965 StringList(const StringList *src);
22ee6d97 966 StringList(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
967 ~StringList();
968
969 void add(const TCHAR *value);
970 void addPreallocated(TCHAR *value);
967893bb
VK
971 void add(INT32 value);
972 void add(UINT32 value);
6173bea8 973 void add(INT64 value);
967893bb 974 void add(UINT64 value);
6173bea8 975 void add(double value);
24dc5346 976 void replace(int index, const TCHAR *value);
b0353a33
VK
977 void addOrReplace(int index, const TCHAR *value);
978 void addOrReplacePreallocated(int index, TCHAR *value);
e2dc0372
VK
979#ifdef UNICODE
980 void addMBString(const char *value) { addPreallocated(WideStringFromMBString(value)); }
981#else
982 void addMBString(const char *value) { add(value); }
983#endif
a31a2d32
VK
984 void addAll(const StringList *src);
985 void merge(const StringList *src, bool matchCase);
986 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
987 void remove(int index);
6173bea8 988 void clear();
a31a2d32
VK
989
990 void sort(bool ascending = true, bool caseSensitive = false);
991
01116597 992 int size() const { return m_count; }
18a7c1e9 993 bool isEmpty() const { return m_count == 0; }
01116597
VK
994 const TCHAR *get(int index) const { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
995 int indexOf(const TCHAR *value) const;
e2845745 996 bool contains(const TCHAR *value) const { return indexOf(value) != -1; }
01116597 997 int indexOfIgnoreCase(const TCHAR *value) const;
e2845745 998 bool containsIgnoreCase(const TCHAR *value) const { return indexOfIgnoreCase(value) != -1; }
7d25a5c5 999 TCHAR *join(const TCHAR *separator);
a31a2d32 1000
6fbaa926 1001 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId);
a31a2d32 1002 json_t *toJson() const;
6173bea8
VK
1003};
1004
3fe40613 1005/**
a58501c0
VK
1006 * Entry of string set
1007 */
1008struct StringSetEntry;
1009
1010/**
f3603b79
VK
1011 * NXCP message
1012 */
b368969c 1013class NXCPMessage;
f3603b79
VK
1014
1015/**
8100b554
VK
1016 * String set
1017 */
1018class StringSet;
1019
1020/**
1021 * String set iterator
1022 */
1023class LIBNETXMS_EXPORTABLE StringSetIterator : public AbstractIterator
1024{
1025private:
1026 StringSet *m_stringSet;
1027 StringSetEntry *m_curr;
1028 StringSetEntry *m_next;
1029
1030public:
1031 StringSetIterator(StringSet *stringSet);
1032
1033 virtual bool hasNext();
1034 virtual void *next();
1035 virtual void remove();
1036};
1037
1038/**
a58501c0
VK
1039 * String set class
1040 */
1041class LIBNETXMS_EXPORTABLE StringSet
1042{
8100b554
VK
1043 friend class StringSetIterator;
1044
a58501c0
VK
1045private:
1046 StringSetEntry *m_data;
1047
1048public:
1049 StringSet();
1050 ~StringSet();
1051
1052 void add(const TCHAR *str);
f3603b79 1053 void addPreallocated(TCHAR *str);
a58501c0
VK
1054 void remove(const TCHAR *str);
1055 void clear();
1056
8100b554
VK
1057 int size() const;
1058 bool contains(const TCHAR *str) const;
1059 bool equals(const StringSet *s) const;
a58501c0 1060
fa493140 1061 void addAll(StringSet *src);
f3603b79 1062 void addAll(TCHAR **strings, int count);
d45f9179 1063 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
d6c4cdc1 1064 void addAllPreallocated(TCHAR **strings, int count);
f3603b79 1065
8100b554
VK
1066 void forEach(bool (*cb)(const TCHAR *, void *), void *userData) const;
1067
1068 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId) const;
1069 void addAllFromMessage(const NXCPMessage *msg, UINT32 baseId, UINT32 countId, bool clearBeforeAdd, bool toUppercase);
c262bc08 1070
d45f9179 1071 String join(const TCHAR *separator);
8100b554
VK
1072
1073 Iterator<const TCHAR> *iterator() { return new Iterator<const TCHAR>(new StringSetIterator(this)); }
a58501c0
VK
1074};
1075
1076/**
296ae03d
VK
1077 * Opaque hash map entry structure
1078 */
1079struct HashMapEntry;
1080
1081/**
1082 * Hash map base class (for fixed size non-pointer keys)
1083 */
1084class LIBNETXMS_EXPORTABLE HashMapBase
1085{
df26f039
VK
1086 friend class HashMapIterator;
1087
296ae03d
VK
1088private:
1089 HashMapEntry *m_data;
1090 bool m_objectOwner;
1091 unsigned int m_keylen;
1092
df26f039 1093 HashMapEntry *find(const void *key) const;
296ae03d
VK
1094 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
1095
1096protected:
1097 void (*m_objectDestructor)(void *);
1098
1099 HashMapBase(bool objectOwner, unsigned int keylen);
1100
df26f039 1101 void *_get(const void *key) const;
296ae03d
VK
1102 void _set(const void *key, void *value);
1103 void _remove(const void *key);
1104
df26f039 1105 bool _contains(const void *key) const { return find(key) != NULL; }
296ae03d
VK
1106
1107public:
1108 virtual ~HashMapBase();
1109
1110 void setOwner(bool owner) { m_objectOwner = owner; }
1111 void clear();
1112
df26f039
VK
1113 int size() const;
1114
1115 EnumerationCallbackResult forEach(EnumerationCallbackResult (*cb)(const void *, const void *, void *), void *userData) const;
1116 const void *findElement(bool (*comparator)(const void *, const void *, void *), void *userData) const;
1117};
296ae03d 1118
df26f039
VK
1119/**
1120 * Hash map iterator
1121 */
1122class LIBNETXMS_EXPORTABLE HashMapIterator : public AbstractIterator
1123{
1124private:
1125 HashMapBase *m_hashMap;
1126 HashMapEntry *m_curr;
1127 HashMapEntry *m_next;
1128
1129public:
1130 HashMapIterator(HashMapBase *hashMap);
1131
1132 virtual bool hasNext();
1133 virtual void *next();
e0f7dbec 1134 virtual void remove();
296ae03d
VK
1135};
1136
1137/**
1138 * Hash map template for holding objects as values
1139 */
1140template <class K, class V> class HashMap : public HashMapBase
1141{
1142private:
1143 static void destructor(void *object) { delete (V*)object; }
1144
1145public:
1146 HashMap(bool objectOwner = false) : HashMapBase(objectOwner, sizeof(K)) { m_objectDestructor = destructor; }
1147
1148 V *get(const K& key) { return (V*)_get(&key); }
1149 void set(const K& key, V *value) { _set(&key, (void *)value); }
1150 void remove(const K& key) { _remove(&key); }
1151 bool contains(const K& key) { return _contains(&key); }
df26f039
VK
1152
1153 Iterator<V> *iterator() { return new Iterator<V>(new HashMapIterator(this)); }
296ae03d
VK
1154};
1155
1156/**
50963ced
VK
1157 * Hash map template for holding reference counting objects as values
1158 */
1159template <class K, class V> class RefCountHashMap : public HashMapBase
1160{
1161private:
87c91211 1162 static void destructor(void *object) { if (object != NULL) ((V*)object)->decRefCount(); }
50963ced
VK
1163
1164public:
1165 RefCountHashMap(bool objectOwner = false) : HashMapBase(objectOwner, sizeof(K)) { m_objectDestructor = destructor; }
1166
87c91211 1167 V *get(const K& key) { V *v = (V*)_get(&key); if (v != NULL) v->incRefCount(); return v; }
5c25a3d2 1168 V *peek(const K& key) { return (V*)_get(&key); }
87c91211 1169 void set(const K& key, V *value) { if (value != NULL) value->incRefCount(); _set(&key, (void *)value); }
50963ced
VK
1170 void remove(const K& key) { _remove(&key); }
1171 bool contains(const K& key) { return _contains(&key); }
1172
1173 Iterator<V> *iterator() { return new Iterator<V>(new HashMapIterator(this)); }
1174};
1175
1176/**
3dc1af83
VK
1177 * Ring buffer
1178 */
1179class LIBNETXMS_EXPORTABLE RingBuffer
1180{
1181private:
1182 BYTE *m_data;
1183 size_t m_size;
1184 size_t m_allocated;
1185 size_t m_allocationStep;
1186 size_t m_readPos;
1187 size_t m_writePos;
1188
1189public:
1190 RingBuffer(size_t initial = 8192, size_t allocationStep = 8192);
1191 ~RingBuffer();
1192
1193 void write(const BYTE *data, size_t dataSize);
1194 size_t read(BYTE *buffer, size_t bufferSize);
1195
1196 size_t size() const { return m_size; }
b0039c52 1197 bool isEmpty() const { return m_size == 0; }
3dc1af83
VK
1198};
1199
1200/**
57f34c0a
VK
1201 * Byte stream
1202 */
1203class LIBNETXMS_EXPORTABLE ByteStream
1204{
1205private:
1206 BYTE *m_data;
1207 size_t m_size;
1208 size_t m_allocated;
1209 size_t m_pos;
3cf3dedd 1210 size_t m_allocationStep;
57f34c0a
VK
1211
1212public:
1213 ByteStream(size_t initial = 8192);
1214 ByteStream(const void *data, size_t size);
1215 virtual ~ByteStream();
1216
4b47d7d7
VK
1217 static ByteStream *load(const TCHAR *file);
1218
1219 void seek(size_t pos) { if (pos <= m_size) m_pos = pos; }
57f34c0a
VK
1220 size_t pos() { return m_pos; }
1221 size_t size() { return m_size; }
1222 bool eos() { return m_pos == m_size; }
1223
3cf3dedd
VK
1224 void setAllocationStep(size_t s) { m_allocationStep = s; }
1225
1226 const BYTE *buffer(size_t *size) { *size = m_size; return m_data; }
57f34c0a 1227
4b47d7d7 1228 void write(const void *data, size_t size);
57f34c0a
VK
1229 void write(char c) { write(&c, 1); }
1230 void write(BYTE b) { write(&b, 1); }
1231 void write(INT16 n) { UINT16 x = htons((UINT16)n); write(&x, 2); }
1232 void write(UINT16 n) { UINT16 x = htons(n); write(&x, 2); }
1233 void write(INT32 n) { UINT32 x = htonl((UINT32)n); write(&x, 4); }
1234 void write(UINT32 n) { UINT32 x = htonl(n); write(&x, 4); }
4b47d7d7
VK
1235 void write(INT64 n) { UINT64 x = htonq((UINT64)n); write(&x, 8); }
1236 void write(UINT64 n) { UINT64 x = htonq(n); write(&x, 8); }
1237 void write(double n) { double x = htond(n); write(&x, 8); }
1238 void writeString(const TCHAR *s);
57f34c0a 1239
4b47d7d7 1240 size_t read(void *buffer, size_t count);
57f34c0a
VK
1241 char readChar() { return !eos() ? (char)m_data[m_pos++] : 0; }
1242 BYTE readByte() { return !eos() ? m_data[m_pos++] : 0; }
1243 INT16 readInt16();
1244 UINT16 readUInt16();
1245 INT32 readInt32();
1246 UINT32 readUInt32();
4b47d7d7
VK
1247 INT64 readInt64();
1248 UINT64 readUInt64();
1249 double readDouble();
1250 TCHAR *readString();
1251
1252 bool save(int f);
57f34c0a
VK
1253};
1254
1255/**
25fe6265
VK
1256 * Auxilliary class for objects which counts references and
1257 * destroys itself wheren reference count falls to 0
1258 */
7f632dfe
VK
1259class LIBNETXMS_EXPORTABLE RefCountObject
1260{
1261private:
7785322f 1262 VolatileCounter m_refCount;
7f632dfe 1263
7dd6369a
VK
1264protected:
1265 virtual ~RefCountObject();
1266
7f632dfe 1267public:
7785322f
VK
1268 RefCountObject()
1269 {
1270 m_refCount = 1;
1271 }
1272
1273 void incRefCount()
1274 {
1275 InterlockedIncrement(&m_refCount);
1276 }
1277
1278 void decRefCount()
1279 {
1280 if (InterlockedDecrement(&m_refCount) == 0)
1281 delete this;
1282 }
7f632dfe
VK
1283};
1284
b368969c 1285class NXCPMessage;
967893bb
VK
1286
1287/**
1288 * Table column definition
1289 */
1290class LIBNETXMS_EXPORTABLE TableColumnDefinition
1291{
1292private:
1293 TCHAR *m_name;
1294 TCHAR *m_displayName;
1295 INT32 m_dataType;
1296 bool m_instanceColumn;
1297
1298public:
1299 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
9e731f19
VK
1300 TableColumnDefinition(const NXCPMessage *msg, UINT32 baseId);
1301 TableColumnDefinition(const TableColumnDefinition *src);
967893bb
VK
1302 ~TableColumnDefinition();
1303
9e731f19 1304 void fillMessage(NXCPMessage *msg, UINT32 baseId) const;
967893bb 1305
9e731f19
VK
1306 const TCHAR *getName() const { return m_name; }
1307 const TCHAR *getDisplayName() const { return m_displayName; }
1308 INT32 getDataType() const { return m_dataType; }
1309 bool isInstanceColumn() const { return m_instanceColumn; }
967893bb
VK
1310
1311 void setDataType(INT32 type) { m_dataType = type; }
1312 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
a0ddfb29 1313 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
967893bb
VK
1314};
1315
1316/**
d6bbfa4e
VK
1317 * Table cell
1318 */
1319class TableCell
1320{
1321private:
1322 TCHAR *m_value;
1323 int m_status;
43666be9 1324 UINT32 m_objectId;
d6bbfa4e
VK
1325
1326public:
43666be9
VK
1327 TableCell() { m_value = NULL; m_status = -1; m_objectId = 0; }
1328 TableCell(const TCHAR *value) { m_value = _tcsdup_ex(value); m_status = -1; m_objectId = 0; }
1329 TableCell(const TCHAR *value, int status) { m_value = _tcsdup_ex(value); m_status = status; m_objectId = 0; }
1330 TableCell(TableCell *src) { m_value = _tcsdup_ex(src->m_value); m_status = src->m_status; m_objectId = src->m_objectId; }
d6bbfa4e
VK
1331 ~TableCell() { safe_free(m_value); }
1332
43666be9
VK
1333 void set(const TCHAR *value, int status, UINT32 objectId) { safe_free(m_value); m_value = _tcsdup_ex(value); m_status = status; m_objectId = objectId; }
1334 void setPreallocated(TCHAR *value, int status, UINT32 objectId) { safe_free(m_value); m_value = value; m_status = status; m_objectId = objectId; }
d6bbfa4e 1335
0a290117 1336 const TCHAR *getValue() const { return m_value; }
c83f6449 1337 void setValue(const TCHAR *value) { safe_free(m_value); m_value = _tcsdup_ex(value); }
e40e0fdc 1338 void setPreallocatedValue(TCHAR *value) { free(m_value); m_value = value; }
d6bbfa4e 1339
0a290117 1340 int getStatus() const { return m_status; }
d6bbfa4e 1341 void setStatus(int status) { m_status = status; }
43666be9 1342
0a290117 1343 int getObjectId() const { return m_objectId; }
43666be9 1344 void setObjectId(UINT32 id) { m_objectId = id; }
d6bbfa4e
VK
1345};
1346
1347/**
1348 * Table row
1349 */
1350class TableRow
1351{
1352private:
1353 ObjectArray<TableCell> *m_cells;
1354 UINT32 m_objectId;
0a290117 1355 int m_baseRow;
bac74bf7 1356
d6bbfa4e
VK
1357public:
1358 TableRow(int columnCount);
1359 TableRow(TableRow *src);
1360 ~TableRow() { delete m_cells; }
1361
1362 void addColumn() { m_cells->add(new TableCell); }
1363 void deleteColumn(int index) { m_cells->remove(index); }
bac74bf7 1364
43666be9
VK
1365 void set(int index, const TCHAR *value, int status, UINT32 objectId) { TableCell *c = m_cells->get(index); if (c != NULL) c->set(value, status, objectId); }
1366 void setPreallocated(int index, TCHAR *value, int status, UINT32 objectId) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocated(value, status, objectId); }
d6bbfa4e
VK
1367
1368 void setValue(int index, const TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setValue(value); }
e40e0fdc 1369 void setPreallocatedValue(int index, TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocatedValue(value); else free(value); }
d6bbfa4e
VK
1370
1371 void setStatus(int index, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setStatus(status); }
1372
0a290117
VK
1373 const TCHAR *getValue(int index) const { const TableCell *c = m_cells->get(index); return (c != NULL) ? c->getValue() : NULL; }
1374 int getStatus(int index) const { const TableCell *c = m_cells->get(index); return (c != NULL) ? c->getStatus() : -1; }
d6bbfa4e 1375
0a290117 1376 UINT32 getObjectId() const { return m_objectId; }
d6bbfa4e 1377 void setObjectId(UINT32 id) { m_objectId = id; }
43666be9 1378
0a290117
VK
1379 int getBaseRow() const { return m_baseRow; }
1380 void setBaseRow(int baseRow) { m_baseRow = baseRow; }
1381
1382 UINT32 getCellObjectId(int index) const { const TableCell *c = m_cells->get(index); return (c != NULL) ? c->getObjectId() : 0; }
43666be9 1383 void setCellObjectId(int index, UINT32 id) { TableCell *c = m_cells->get(index); if (c != NULL) c->setObjectId(id); }
d6bbfa4e
VK
1384};
1385
1386/**
967893bb
VK
1387 * Class for table data storage
1388 */
a0ddfb29 1389class LIBNETXMS_EXPORTABLE Table : public RefCountObject
967893bb
VK
1390{
1391private:
d6bbfa4e 1392 ObjectArray<TableRow> *m_data;
967893bb
VK
1393 ObjectArray<TableColumnDefinition> *m_columns;
1394 TCHAR *m_title;
1395 int m_source;
d6bbfa4e 1396 bool m_extendedFormat;
967893bb 1397
b368969c 1398 void createFromMessage(NXCPMessage *msg);
967893bb 1399 void destroy();
63ff3c9d 1400 bool parseXML(const char *xml);
967893bb
VK
1401
1402public:
1403 Table();
a0ddfb29 1404 Table(Table *src);
b368969c 1405 Table(NXCPMessage *msg);
b549a0f8 1406 virtual ~Table();
967893bb 1407
b368969c
VK
1408 int fillMessage(NXCPMessage &msg, int offset, int rowLimit);
1409 void updateFromMessage(NXCPMessage *msg);
967893bb 1410
a0ddfb29
VK
1411 void addAll(Table *src);
1412 void copyRow(Table *src, int row);
967893bb 1413
d6bbfa4e 1414 int getNumRows() { return m_data->size(); }
62485dab 1415 int getNumColumns() const { return m_columns->size(); }
967893bb
VK
1416 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
1417 int getSource() { return m_source; }
1418
d6bbfa4e
VK
1419 bool isExtendedFormat() { return m_extendedFormat; }
1420 void setExtendedFormat(bool ext) { m_extendedFormat = ext; }
1421
62485dab
EJ
1422 const TCHAR *getColumnName(int col) const { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
1423 INT32 getColumnDataType(int col) const { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
9e731f19 1424 const TableColumnDefinition *getColumnDefinition(int col) const { return m_columns->get(col); }
62485dab 1425 int getColumnIndex(const TCHAR *name) const;
967893bb
VK
1426 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
1427
c83f6449 1428 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = _tcsdup_ex(title); }
967893bb 1429 void setSource(int source) { m_source = source; }
8dba1eac 1430 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
9e731f19 1431 int addColumn(const TableColumnDefinition *d);
967893bb
VK
1432 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
1433 int addRow();
1434
25076d53
VK
1435 void deleteRow(int row);
1436 void deleteColumn(int col);
1437
967893bb
VK
1438 void setAt(int nRow, int nCol, INT32 nData);
1439 void setAt(int nRow, int nCol, UINT32 dwData);
1440 void setAt(int nRow, int nCol, double dData);
1441 void setAt(int nRow, int nCol, INT64 nData);
1442 void setAt(int nRow, int nCol, UINT64 qwData);
1443 void setAt(int nRow, int nCol, const TCHAR *pszData);
1444 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
1445
d6bbfa4e
VK
1446 void set(int nCol, INT32 nData) { setAt(getNumRows() - 1, nCol, nData); }
1447 void set(int nCol, UINT32 dwData) { setAt(getNumRows() - 1, nCol, dwData); }
1448 void set(int nCol, double dData) { setAt(getNumRows() - 1, nCol, dData); }
1449 void set(int nCol, INT64 nData) { setAt(getNumRows() - 1, nCol, nData); }
1450 void set(int nCol, UINT64 qwData) { setAt(getNumRows() - 1, nCol, qwData); }
d65c16a8
VK
1451 void set(int nCol, const TCHAR *data) { setAt(getNumRows() - 1, nCol, data); }
1452#ifdef UNICODE
1453 void set(int nCol, const char *data) { setPreallocatedAt(getNumRows() - 1, nCol, WideStringFromMBString(data)); }
1454#else
1455 void set(int nCol, const WCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, MBStringFromWideString(data)); }
1456#endif
1457 void setPreallocated(int nCol, TCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, data); }
d6bbfa4e
VK
1458
1459 void setStatusAt(int row, int col, int status);
1460 void setStatus(int col, int status) { setStatusAt(getNumRows() - 1, col, status); }
967893bb 1461
816a25f5 1462 const TCHAR *getAsString(int nRow, int nCol, const TCHAR *defaultValue = NULL);
967893bb
VK
1463 INT32 getAsInt(int nRow, int nCol);
1464 UINT32 getAsUInt(int nRow, int nCol);
1465 INT64 getAsInt64(int nRow, int nCol);
1466 UINT64 getAsUInt64(int nRow, int nCol);
1467 double getAsDouble(int nRow, int nCol);
22aaa779 1468
d6bbfa4e
VK
1469 int getStatus(int nRow, int nCol);
1470
22aaa779 1471 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
a0ddfb29 1472 int findRowByInstance(const TCHAR *instance);
d6bbfa4e 1473
0a290117
VK
1474 UINT32 getObjectId(int row) const { const TableRow *r = m_data->get(row); return (r != NULL) ? r->getObjectId() : 0; }
1475 void setObjectIdAt(int row, UINT32 id) { TableRow *r = m_data->get(row); if (r != NULL) r->setObjectId(id); }
1476 void setObjectId(UINT32 id) { setObjectIdAt(getNumRows() - 1, id); }
a3d3f9d5 1477
43666be9
VK
1478 void setCellObjectIdAt(int row, int col, UINT32 objectId);
1479 void setCellObjectId(int col, UINT32 objectId) { setCellObjectIdAt(getNumRows() - 1, col, objectId); }
0a290117
VK
1480 UINT32 getCellObjectId(int row, int col) const { const TableRow *r = m_data->get(row); return (r != NULL) ? r->getCellObjectId(col) : 0; }
1481
1482 void setBaseRowAt(int row, int baseRow);
1483 void setBaseRow(int baseRow) { setBaseRowAt(getNumRows() - 1, baseRow); }
1484 int getBaseRow(int row) const { const TableRow *r = m_data->get(row); return (r != NULL) ? r->getBaseRow() : 0; }
43666be9 1485
63ff3c9d
VK
1486 static Table *createFromXML(const char *xml);
1487 TCHAR *createXML();
f1b8f11c
VK
1488
1489 static Table *createFromPackedXML(const char *packedXml);
1490 char *createPackedXML();
967893bb
VK
1491};
1492
25fe6265 1493/**
a31a2d32
VK
1494 * Create JSON string with null check
1495 */
1496inline json_t *json_string_a(const char *s)
1497{
1498 return (s != NULL) ? json_string(s) : json_null();
1499}
1500
88e14cac
VK
1501/**
1502 * Create JSON string from wide character string
1503 */
1504inline json_t *json_string_w(const WCHAR *s)
1505{
1506 if (s == NULL)
1507 return json_null();
1508 char *us = UTF8StringFromWideString(s);
1509 json_t *js = json_string(us);
1510 free(us);
1511 return js;
1512}
1513
820fd491
VK
1514#ifdef UNICODE
1515#define json_string_t json_string_w
1516#else
a31a2d32 1517#define json_string_t json_string_a
820fd491
VK
1518#endif
1519
1520/**
1521 * Create JSON array from integer array
1522 */
c3c060b8 1523template<typename T> json_t *json_integer_array(const T *values, size_t size)
820fd491
VK
1524{
1525 json_t *a = json_array();
1526 for(size_t i = 0; i < size; i++)
1527 json_array_append_new(a, json_integer(values[i]));
1528 return a;
1529}
1530
1531/**
1532 * Serialize ObjectArray as JSON
1533 */
1534template<typename T> json_t *json_object_array(ObjectArray<T> *a)
1535{
1536 json_t *root = json_array();
a31a2d32
VK
1537 if (a != NULL)
1538 {
1539 for(int i = 0; i < a->size(); i++)
1540 {
1541 T *e = a->get(i);
1542 if (e != NULL)
1543 json_array_append_new(root, e->toJson());
1544 }
1545 }
820fd491
VK
1546 return root;
1547}
1548
1549/**
c11eee9b
VK
1550 * sockaddr buffer
1551 */
1552union SockAddrBuffer
1553{
1554 struct sockaddr_in sa4;
1555#ifdef WITH_IPV6
1556 struct sockaddr_in6 sa6;
1557#endif
1558};
1559
1560/**
1561 * sockaddr length calculation
1562 */
abac7ffe 1563#undef SA_LEN
c11eee9b
VK
1564#ifdef WITH_IPV6
1565#define SA_LEN(sa) (((sa)->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
1566#else
1567#define SA_LEN(sa) sizeof(struct sockaddr_in)
1568#endif
1569
1570/**
5f6e8d09
VK
1571 * Get port number from SockAddrBuffer
1572 */
1573#ifdef WITH_IPV6
1574#define SA_PORT(sa) ((((struct sockaddr *)sa)->sa_family == AF_INET) ? ((struct sockaddr_in *)sa)->sin_port : ((struct sockaddr_in6 *)sa)->sin6_port)
1575#else
1576#define SA_PORT(sa) (((struct sockaddr_in *)sa)->sin_port)
1577#endif
1578
1579/**
30592fa6
VK
1580 * Compare addresses in sockaddr
1581 */
1582inline bool SocketAddressEquals(struct sockaddr *a1, struct sockaddr *a2)
1583{
1584 if (a1->sa_family != a2->sa_family)
1585 return false;
1586 if (a1->sa_family == AF_INET)
1587 return ((struct sockaddr_in *)a1)->sin_addr.s_addr == ((struct sockaddr_in *)a2)->sin_addr.s_addr;
1588#ifdef WITH_IPV6
1589 if (a1->sa_family == AF_INET6)
1590 return !memcmp(((struct sockaddr_in6 *)a1)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)a2)->sin6_addr.s6_addr, 16);
1591#endif
1592 return false;
1593}
1594
ce9e00cc
VK
1595enum MacAddressNotation
1596{
1597 MAC_ADDR_FLAT_STRING = 0,
1598 MAC_ADDR_COLON_SEPARATED = 1,
1599 MAC_ADDR_BYTEPAIR_COLON_SEPARATED = 2,
1600 MAC_ADDR_HYPHEN_SEPARATED = 3,
1601 MAC_ADDR_DOT_SEPARATED = 4,
1602 MAC_ADDR_BYTEPAIR_DOT_SEPARATED = 5
1603};
1604
1605/**
1606 * MAC address
1607 */
1608class LIBNETXMS_EXPORTABLE MacAddress
1609{
1610private:
1611 BYTE m_value[16];
1612 size_t m_length;
1613
1614 TCHAR *toStringInternal(TCHAR *buffer, const TCHAR separator, bool bytePair = false) const;
709cd5a4 1615 TCHAR *toStringInternal3(TCHAR *buffer, const TCHAR separator) const;
ce9e00cc
VK
1616
1617public:
1618 MacAddress() { m_length = 0; memset(m_value, 0, 16); }
1619 MacAddress(const BYTE *value, size_t length) { m_length = min(length, 16); memcpy(m_value, value, m_length); }
1620 MacAddress(const MacAddress& src) { memcpy(m_value, src.m_value, src.m_length); m_length = src.m_length; }
1621
1622 static MacAddress parse(const char *str);
e590796d 1623 static MacAddress parse(const WCHAR *str);
ce9e00cc
VK
1624
1625 const BYTE *value() const { return m_value; }
1626 size_t length() const { return m_length; }
1627
1628 bool isMulticast() const;
1629 bool isBroadcast() const;
1630 bool equals(const MacAddress &a) const;
1631
1632 TCHAR *toString(TCHAR *buffer, MacAddressNotation notation = MAC_ADDR_COLON_SEPARATED) const;
1633 String toString(MacAddressNotation notation = MAC_ADDR_COLON_SEPARATED) const;
1634};
1635
30592fa6 1636/**
9319c166
VK
1637 * IP address
1638 */
1639class LIBNETXMS_EXPORTABLE InetAddress
1640{
9319c166 1641private:
8c75ad41
VK
1642 short m_maskBits;
1643 short m_family;
1644 union
1645 {
1646 UINT32 v4;
1647 BYTE v6[16];
1648 } m_addr;
9319c166
VK
1649
1650public:
8c75ad41
VK
1651 InetAddress();
1652 InetAddress(UINT32 addr);
c75e9ee4 1653 InetAddress(UINT32 addr, UINT32 mask);
4203dcb5 1654 InetAddress(const BYTE *addr, int maskBits = 128);
9319c166 1655
8c75ad41
VK
1656 bool isAnyLocal() const;
1657 bool isLoopback() const;
1658 bool isMulticast() const;
1659 bool isBroadcast() const;
4203dcb5 1660 bool isLinkLocal() const;
8c75ad41 1661 bool isValid() const { return m_family != AF_UNSPEC; }
4203dcb5 1662 bool isValidUnicast() const { return isValid() && !isAnyLocal() && !isLoopback() && !isMulticast() && !isBroadcast() && !isLinkLocal(); }
9319c166 1663
8c75ad41 1664 int getFamily() const { return m_family; }
c7853753
VK
1665 UINT32 getAddressV4() const { return (m_family == AF_INET) ? m_addr.v4 : 0; }
1666 const BYTE *getAddressV6() const { return (m_family == AF_INET6) ? m_addr.v6 : (const BYTE *)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; }
9319c166 1667
8c75ad41 1668 bool contain(const InetAddress &a) const;
43709a0c 1669 bool sameSubnet(const InetAddress &a) const;
8c75ad41
VK
1670 bool equals(const InetAddress &a) const;
1671 int compareTo(const InetAddress &a) const;
9319c166 1672
8c75ad41
VK
1673 void setMaskBits(int m) { m_maskBits = m; }
1674 int getMaskBits() const { return m_maskBits; }
c75e9ee4 1675 int getHostBits() const { return (m_family == AF_INET) ? (32 - m_maskBits) : (128 - m_maskBits); }
9319c166 1676
463b1d34 1677 InetAddress getSubnetAddress() const;
ba889094 1678 InetAddress getSubnetBroadcast() const;
463b1d34 1679 bool isSubnetBroadcast(int maskBits) const;
ffabd7f8 1680
8c75ad41
VK
1681 String toString() const;
1682 TCHAR *toString(TCHAR *buffer) const;
af4a09df
VK
1683#ifdef UNICODE
1684 char *toStringA(char *buffer) const;
1685#else
1686 char *toStringA(char *buffer) const { return toString(buffer); }
1687#endif
9319c166 1688
e9ef47dd
VK
1689 json_t *toJson() const;
1690
43709a0c 1691 BYTE *buildHashKey(BYTE *key) const;
f3b94fc5 1692
43709a0c
VK
1693 TCHAR *getHostByAddr(TCHAR *buffer, size_t buflen) const;
1694
1695 struct sockaddr *fillSockAddr(SockAddrBuffer *buffer, UINT16 port = 0) const;
c11eee9b 1696
c7853753
VK
1697 static InetAddress resolveHostName(const WCHAR *hostname, int af = AF_INET);
1698 static InetAddress resolveHostName(const char *hostname, int af = AF_INET);
f3b94fc5
VK
1699 static InetAddress parse(const WCHAR *str);
1700 static InetAddress parse(const char *str);
8c75ad41 1701 static InetAddress createFromSockaddr(struct sockaddr *s);
dcc7a2b0
VK
1702
1703 static const InetAddress INVALID;
48313f37 1704 static const InetAddress LOOPBACK;
dcc7a2b0
VK
1705};
1706
1707/**
1708 * IP address list
1709 */
1710class LIBNETXMS_EXPORTABLE InetAddressList
1711{
1712private:
1713 ObjectArray<InetAddress> *m_list;
1714
1715 int indexOf(const InetAddress& addr) const;
1716
1717public:
1718 InetAddressList();
1719 ~InetAddressList();
1720
1721 void add(const InetAddress& addr);
c30c0c0f
VK
1722 void add(const InetAddressList& addrList);
1723 void replace(const InetAddress& addr);
dcc7a2b0 1724 void remove(const InetAddress& addr);
c30c0c0f 1725 void clear() { m_list->clear(); }
4203dcb5 1726 const InetAddress& get(int index) const { const InetAddress *a = m_list->get(index); return (a != NULL) ? *a : InetAddress::INVALID; }
dcc7a2b0
VK
1727
1728 int size() const { return m_list->size(); }
1729 bool hasAddress(const InetAddress& addr) const { return indexOf(addr) != -1; }
4203dcb5 1730 const InetAddress& findAddress(const InetAddress& addr) const { int idx = indexOf(addr); return (idx != -1) ? *m_list->get(idx) : InetAddress::INVALID; }
c34b611c 1731 const InetAddress& findSameSubnetAddress(const InetAddress& addr) const;
dcc7a2b0 1732 const InetAddress& getFirstUnicastAddress() const;
c34b611c
VK
1733 const InetAddress& getFirstUnicastAddressV4() const;
1734 bool hasValidUnicastAddress() const { return getFirstUnicastAddress().isValid(); }
c30c0c0f 1735 bool isLoopbackOnly() const;
dcc7a2b0
VK
1736
1737 const ObjectArray<InetAddress> *getList() const { return m_list; }
40c8f341
VK
1738
1739 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
820fd491
VK
1740
1741 json_t *toJson() const { return json_object_array(m_list); }
9319c166
VK
1742};
1743
1744/**
25fe6265
VK
1745 * Network connection
1746 */
58ee2f43
VK
1747class LIBNETXMS_EXPORTABLE SocketConnection
1748{
1749protected:
1750 SOCKET m_socket;
1751 char m_data[4096];
1752 int m_dataPos;
1753
1754public:
1755 SocketConnection();
1756 virtual ~SocketConnection();
1757
967893bb 1758 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
b4cf3199 1759 bool connectTCP(const InetAddress& ip, WORD port, UINT32 timeout);
58ee2f43
VK
1760 void disconnect();
1761
967893bb
VK
1762 bool canRead(UINT32 timeout);
1763 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43 1764 bool waitForText(const char *text, int timeout);
cc022855 1765
58ee2f43 1766 int write(const char *pBuff, int nSize);
1671f85d
VK
1767 bool writeLine(const char *line);
1768
967893bb 1769 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
1770};
1771
030a0779
VK
1772/**
1773 * Telnet connection - handles all telnet negotiation
1774 */
def5b792
AK
1775class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
1776{
030a0779 1777protected:
967893bb 1778 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
b4cf3199 1779 bool connectTCP(const InetAddress& ip, WORD port, UINT32 timeout);
030a0779 1780
def5b792 1781public:
967893bb 1782 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 1783
967893bb 1784 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
b4cf3199 1785 bool connect(const InetAddress& ip, WORD port, UINT32 timeout);
967893bb
VK
1786 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
1787 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
1788};
1789
56fa1092
VK
1790/**
1791 * Postal address representation
1792 */
1793class LIBNETXMS_EXPORTABLE PostalAddress
1794{
1795private:
1796 TCHAR *m_country;
1797 TCHAR *m_city;
1798 TCHAR *m_streetAddress;
1799 TCHAR *m_postcode;
1800
1801public:
1802 PostalAddress();
1803 PostalAddress(const TCHAR *country, const TCHAR *city, const TCHAR *streetAddress, const TCHAR *postcode);
1804 ~PostalAddress();
1805
63e99e56
VK
1806 const TCHAR *getCountry() const { return CHECK_NULL_EX(m_country); }
1807 const TCHAR *getCity() const { return CHECK_NULL_EX(m_city); }
1808 const TCHAR *getStreetAddress() const { return CHECK_NULL_EX(m_streetAddress); }
1809 const TCHAR *getPostCode() const { return CHECK_NULL_EX(m_postcode); }
56fa1092 1810
6336bba3
VK
1811 json_t *toJson() const;
1812
cbc8c849
VK
1813 void setCountry(const TCHAR *country) { free(m_country); m_country = _tcsdup_ex(country); }
1814 void setCity(const TCHAR *city) { free(m_city); m_city = _tcsdup_ex(city); }
1815 void setStreetAddress(const TCHAR *streetAddress) { free(m_streetAddress); m_streetAddress = _tcsdup_ex(streetAddress); }
1816 void setPostCode(const TCHAR *postcode) { free(m_postcode); m_postcode = _tcsdup_ex(postcode); }
56fa1092
VK
1817};
1818
1668f7ed
VK
1819/**
1820 * Max number of polled sockets
1821 */
1822#define SOCKET_POLLER_MAX_SOCKETS 16
1823
1824/**
1825 * Socket poller
1826 */
1827class LIBNETXMS_EXPORTABLE SocketPoller
1828{
1829private:
1830 bool m_write;
1831 int m_count;
94f7aa43 1832#if HAVE_POLL
1668f7ed
VK
1833 struct pollfd m_sockets[SOCKET_POLLER_MAX_SOCKETS];
1834#else
1835 fd_set m_sockets;
4c5e8456 1836#ifndef _WIN32
1668f7ed
VK
1837 SOCKET m_maxfd;
1838#endif
4c5e8456 1839#endif
1668f7ed
VK
1840
1841public:
1842 SocketPoller(bool write = false);
1843 ~SocketPoller();
1844
1845 bool add(SOCKET s);
1846 int poll(UINT32 timeout);
1847 bool isSet(SOCKET s);
4c5e8456 1848 void reset();
1668f7ed
VK
1849};
1850
eae56922
VK
1851/**
1852 * Abstract communication channel
1853 */
1854class LIBNETXMS_EXPORTABLE AbstractCommChannel : public RefCountObject
1855{
1856protected:
1857 virtual ~AbstractCommChannel();
1858
1859public:
1860 AbstractCommChannel();
1861
1862 virtual int send(const void *data, size_t size, MUTEX mutex = INVALID_MUTEX_HANDLE) = 0;
1863 virtual int recv(void *buffer, size_t size, UINT32 timeout = INFINITE) = 0;
1864 virtual int poll(UINT32 timeout, bool write = false) = 0;
1865 virtual int shutdown() = 0;
1866 virtual void close() = 0;
1867};
1868
1869/**
1870 * Socket communication channel
1871 */
1872class LIBNETXMS_EXPORTABLE SocketCommChannel : public AbstractCommChannel
1873{
1874private:
1875 SOCKET m_socket;
1876 bool m_owner;
1877
1878protected:
1879 virtual ~SocketCommChannel();
1880
1881public:
1882 SocketCommChannel(SOCKET socket, bool owner = true);
1883
1884 virtual int send(const void *data, size_t size, MUTEX mutex = INVALID_MUTEX_HANDLE);
1885 virtual int recv(void *buffer, size_t size, UINT32 timeout = INFINITE);
1886 virtual int poll(UINT32 timeout, bool write = false);
1887 virtual int shutdown();
1888 virtual void close();
1889};
1890
b160350d
VK
1891#endif /* __cplusplus */
1892
030a0779
VK
1893/**
1894 * Configuration item template for configuration loader
1895 */
4826c8a5
VK
1896typedef struct
1897{
e2e66145
VK
1898 TCHAR token[64];
1899 BYTE type;
1900 BYTE separator; // Separator character for lists
1901 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
e95680e5 1902 UINT64 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
e2e66145
VK
1903 UINT32 bufferPos; // Should be set to 0
1904 void *buffer;
1905 void *overrideIndicator;
4826c8a5 1906} NX_CFG_TEMPLATE;
dcb44c9a 1907
030a0779
VK
1908/**
1909 * Code translation structure
1910 */
1b56f3c5 1911typedef struct __CODE_TO_TEXT
bc858635
VK
1912{
1913 int code;
1914 const TCHAR *text;
1915} CODE_TO_TEXT;
1916
e2e66145
VK
1917/**
1918 * getopt() prototype if needed
1919 */
df9812a9 1920#if USE_BUNDLED_GETOPT
99e50bb7 1921#include <netxms_getopt.h>
3046f9a4
VK
1922#endif
1923
f2fdf1b5 1924//
3d1cb7d6
VK
1925// Structures for opendir() / readdir() / closedir()
1926//
1927
1928#ifdef _WIN32
1929
1a459ba3
VK
1930#define DT_UNKNOWN 0
1931#define DT_REG 1
1932#define DT_DIR 2
1933
7f9e0c00 1934typedef struct dirent
3d1cb7d6
VK
1935{
1936 long d_ino; /* inode number (not used by MS-DOS) */
1a459ba3 1937 unsigned char d_type; /* file type */
3d1cb7d6 1938 int d_namlen; /* Name length */
1f704f19 1939 char d_name[MAX_PATH]; /* file name */
3d1cb7d6
VK
1940} _DIRECT;
1941
1942typedef struct _dir_struc
1943{
1f704f19
VK
1944 HANDLE handle;
1945 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
1946} DIR;
1947
5ea2db30
VK
1948#ifdef UNICODE
1949
1950typedef struct dirent_w
1951{
1952 long d_ino; /* inode number (not used by MS-DOS) */
1a459ba3 1953 unsigned char d_type; /* file type */
5ea2db30 1954 int d_namlen; /* Name length */
1f704f19 1955 WCHAR d_name[MAX_PATH]; /* file name */
5ea2db30
VK
1956} _DIRECTW;
1957
1958typedef struct _dir_struc_w
1959{
1f704f19 1960 HANDLE handle;
5ea2db30
VK
1961 struct dirent_w dirstr; /* Directory structure to return */
1962} DIRW;
1963
1964#define _TDIR DIRW
1965#define _TDIRECT _DIRECTW
1966#define _tdirent dirent_w
1967
1968#else
1969
1970#define _TDIR DIR
1971#define _TDIRECT _DIRECT
1972#define _tdirent dirent
1973
37d62364
AK
1974#endif
1975
76b1d3d8
VK
1976#else /* not _WIN32 */
1977
1978typedef struct dirent_w
1979{
1980 long d_ino; /* inode number */
5f77533a
VK
1981#if HAVE_DIRENT_D_TYPE
1982 unsigned char d_type; /* file type */
1983#endif
76b1d3d8
VK
1984 WCHAR d_name[257]; /* file name */
1985} _DIRECTW;
1986
1987typedef struct _dir_struc_w
1988{
1989 DIR *dir; /* Original non-unicode structure */
1990 struct dirent_w dirstr; /* Directory structure to return */
1991} DIRW;
1992
3d1cb7d6
VK
1993#endif /* _WIN32 */
1994
1995
3a4c13cc
VK
1996/***** For compatibility *****/
1997#ifdef UNICODE
1998#define nx_strncpy wcslcpy
1999#else
2000#define nx_strncpy strlcpy
2001#endif
2002
3d1cb7d6 2003//
e9246d26
VK
2004// Functions
2005//
2006
b50f1100 2007#ifdef __cplusplus
18c575eb 2008
967893bb 2009int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
5e60b759
VK
2010int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
2011int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
18c575eb
VK
2012bool LIBNETXMS_EXPORTABLE RecvAll(SOCKET s, void *buffer, size_t size, UINT32 timeout);
2013
b42c9187
VK
2014SOCKET LIBNETXMS_EXPORTABLE ConnectToHost(const InetAddress& addr, UINT16 port, UINT32 timeout);
2015
b0608204 2016#endif /* __cplusplus */
c81979bc
VK
2017
2018#ifdef __cplusplus
e9246d26
VK
2019extern "C"
2020{
b50f1100 2021#endif
735bac5c 2022
d37e7f10 2023void LIBNETXMS_EXPORTABLE InitNetXMSProcess(bool commandLineTool);
fe8ea784 2024
7768852d 2025#ifndef _WIN32
8b86c5dc 2026#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
735bac5c
VK
2027void LIBNETXMS_EXPORTABLE __wcsupr(WCHAR *in);
2028#define wcsupr __wcsupr
cc022855 2029#endif
735bac5c
VK
2030void LIBNETXMS_EXPORTABLE __strupr(char *in);
2031#define strupr __strupr
7768852d 2032#endif /* _WIN32 */
cc022855 2033
735bac5c 2034void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
b8a8d064
VK
2035 int (*compare)(const void *, const void *, void *));
2036
735bac5c 2037INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
d35ed0aa 2038
735bac5c
VK
2039UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
2040UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
2041#ifdef UNICODE
2042#define FileSize FileSizeW
2043#else
2044#define FileSize FileSizeA
2045#endif
670c7f3d 2046
735bac5c
VK
2047int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
2048TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 2049#ifdef UNICODE
735bac5c 2050char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
2051#else
2052#define IpToStrA IpToStr
e0f99bf0 2053#endif
8c75ad41 2054TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(const BYTE *addr, TCHAR *buffer);
af4a09df
VK
2055#ifdef UNICODE
2056char LIBNETXMS_EXPORTABLE *Ip6ToStrA(const BYTE *addr, char *buffer);
2057#else
2058#define Ip6ToStrA Ip6ToStr
2059#endif
735bac5c 2060TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
60557d06 2061
735bac5c
VK
2062void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
2063void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
31521248 2064
73a67950
VK
2065WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const void *data, size_t size, WCHAR *str);
2066char LIBNETXMS_EXPORTABLE *BinToStrA(const void *data, size_t size, char *str);
d7970ee7
VK
2067#ifdef UNICODE
2068#define BinToStr BinToStrW
2069#else
2070#define BinToStr BinToStrA
2071#endif
2072
735bac5c
VK
2073size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
2074size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
52db3ed1
VK
2075#ifdef UNICODE
2076#define StrToBin StrToBinW
2077#else
2078#define StrToBin StrToBinA
2079#endif
cc022855 2080
735bac5c 2081TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
b50f1100 2082
735bac5c
VK
2083void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
2084void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
b07c50cc
VK
2085#ifdef UNICODE
2086#define StrStrip StrStripW
2087#else
2088#define StrStrip StrStripA
2089#endif
2090
735bac5c
VK
2091const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
2092const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
2093#ifdef UNICODE
2094#define ExtractWord ExtractWordW
2095#else
2096#define ExtractWord ExtractWordA
2097#endif
2098
735bac5c
VK
2099int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
2100int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
3583a400
VK
2101#ifdef UNICODE
2102#define NumChars NumCharsW
2103#else
2104#define NumChars NumCharsA
2105#endif
ada97259 2106
735bac5c
VK
2107void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
2108void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
35f836fe
VK
2109#ifdef UNICODE
2110#define RemoveTrailingCRLF RemoveTrailingCRLFW
2111#else
2112#define RemoveTrailingCRLF RemoveTrailingCRLFA
2113#endif
2114
e5f88e5a
VK
2115bool LIBNETXMS_EXPORTABLE RegexpMatchA(const char *str, const char *expr, bool matchCase);
2116bool LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *str, const WCHAR *expr, bool matchCase);
cafe592d
VK
2117#ifdef UNICODE
2118#define RegexpMatch RegexpMatchW
2119#else
2120#define RegexpMatch RegexpMatchA
2121#endif
2122
735bac5c 2123const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
d77c193c 2124BOOL LIBNETXMS_EXPORTABLE CreateFolder(const TCHAR *directory);
7b45b90d 2125bool LIBNETXMS_EXPORTABLE SetLastModificationTime(TCHAR *fileName, time_t lastModDate);
818f8c94 2126TCHAR LIBNETXMS_EXPORTABLE *Trim(TCHAR *str);
735bac5c
VK
2127bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
2128TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
0a145c10 2129int LIBNETXMS_EXPORTABLE GetLastMonthDay(struct tm *currTime);
0a145c10 2130bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime);
073283c5 2131bool LIBNETXMS_EXPORTABLE MatchSchedule(const TCHAR *schedule, struct tm *currTime, time_t now);
ada97259 2132
f7794b68 2133#ifdef __cplusplus
735bac5c 2134BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
f7794b68 2135#endif
735bac5c
VK
2136BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
2137/* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
2138const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
2139void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
2140BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 2141#ifdef UNICODE
735bac5c 2142BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
2143#else
2144#define LoadFileA LoadFile
2145#endif
2146
735bac5c 2147UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
1e019efa
VK
2148BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
2149
2150typedef unsigned char MD5_STATE[128];
2151
2152void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const BYTE *data, size_t nbytes, BYTE *hash);
2153void LIBNETXMS_EXPORTABLE MD5HashForPattern(const BYTE *data, size_t patternSize, size_t fullSize, BYTE *hash);
735bac5c 2154BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
1e019efa
VK
2155void LIBNETXMS_EXPORTABLE MD5Init(MD5_STATE *state);
2156void LIBNETXMS_EXPORTABLE MD5Update(MD5_STATE *state, const BYTE *data, size_t size);
2157void LIBNETXMS_EXPORTABLE MD5Finish(MD5_STATE *state, BYTE *hash);
2158
2159typedef unsigned char SHA1_STATE[128];
2160
2161void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(const BYTE *data, size_t nbytes, BYTE *hash);
2162void LIBNETXMS_EXPORTABLE SHA1HashForPattern(const BYTE *data, size_t patternSize, size_t fullSize, BYTE *hash);
735bac5c 2163BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
1e019efa
VK
2164void LIBNETXMS_EXPORTABLE SHA1Init(SHA1_STATE *state);
2165void LIBNETXMS_EXPORTABLE SHA1Update(SHA1_STATE *state, const BYTE *data, size_t size);
2166void LIBNETXMS_EXPORTABLE SHA1Finish(SHA1_STATE *state, BYTE *hash);
2167
2168void LIBNETXMS_EXPORTABLE CalculateSHA256Hash(const BYTE *data, size_t len, BYTE *hash);
66c04cc7 2169
981d246a
VK
2170void LIBNETXMS_EXPORTABLE GenerateRandomBytes(BYTE *buffer, size_t size);
2171
7f9ff8a5
VK
2172void LIBNETXMS_EXPORTABLE LogOpenSSLErrorStack(int level);
2173
735bac5c
VK
2174void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
2175void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
4e2debcc 2176
11f1babb
VK
2177bool LIBNETXMS_EXPORTABLE DecryptPasswordW(const WCHAR *login, const WCHAR *encryptedPasswd, WCHAR *decryptedPasswd, size_t bufferLenght);
2178bool LIBNETXMS_EXPORTABLE DecryptPasswordA(const char *login, const char *encryptedPasswd, char *decryptedPasswd, size_t bufferLenght);
2179#ifdef UNICODE
2180#define DecryptPassword DecryptPasswordW
2181#else
2182#define DecryptPassword DecryptPasswordA
2183#endif
6b1282e7 2184
735bac5c 2185int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 2186
735bac5c
VK
2187HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
2188void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
2189void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 2190
735bac5c
VK
2191bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
2192bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
2193long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
289d126b 2194
735bac5c
VK
2195bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
2196bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
2197long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
289d126b
VK
2198
2199#ifdef UNICODE
2200#define ExtractNamedOptionValue ExtractNamedOptionValueW
2201#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
2202#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
2203#else
2204#define ExtractNamedOptionValue ExtractNamedOptionValueA
2205#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
2206#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
2207#endif
ae9cddaf 2208
bc858635 2209#ifdef __cplusplus
20d885d8 2210const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int code, CODE_TO_TEXT *translator, const TCHAR *defaultText = _T("Unknown"));
bc858635 2211#else
20d885d8
VK
2212const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int code, CODE_TO_TEXT *translator, const TCHAR *defaultText);
2213#endif
2214#ifdef __cplusplus
2215int LIBNETXMS_EXPORTABLE CodeFromText(const TCHAR *text, CODE_TO_TEXT *translator, int defaultCode = -1);
2216#else
2217int LIBNETXMS_EXPORTABLE CodeFromText(const TCHAR *text, CODE_TO_TEXT *translator, int defaultCode);
bc858635
VK
2218#endif
2219
aa13cfd9 2220#ifdef _WIN32
735bac5c
VK
2221TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
2222BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
2223INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 2224#endif
696fc54f 2225
ee3d42df
VK
2226TCHAR LIBNETXMS_EXPORTABLE *GetLastSocketErrorText(TCHAR *buffer, size_t size);
2227
7433df29
VK
2228TCHAR LIBNETXMS_EXPORTABLE *GetLocalHostName(TCHAR *buffer, size_t size, bool fqdn);
2229
de4c0511
VK
2230#if !HAVE_DAEMON || !HAVE_DECL_DAEMON
2231int LIBNETXMS_EXPORTABLE __daemon(int nochdir, int noclose);
2232#define daemon __daemon
696fc54f 2233#endif
4332e9d8 2234
735bac5c 2235UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 2236
4332e9d8 2237#ifndef _WIN32
7768852d
VK
2238
2239bool LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
735bac5c
VK
2240int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
2241 int cchWideChar, char *pByteStr, int cchByteChar,
2242 char *pDefaultChar, BOOL *pbUsedDefChar);
2243int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
2244 int cchByteChar, WCHAR *pWideCharStr,
2245 int cchWideChar);
04dbaf62 2246
c6d5c1b0 2247#ifdef UNICODE
735bac5c
VK
2248int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
2249int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
2250int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
2251int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
2252int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
2253int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 2254
735bac5c
VK
2255int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
2256int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
2257int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
2258int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
2259int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
2260int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
c6d5c1b0 2261#endif
25575d38 2262
3e39fcde 2263#endif /* _WIN32 */
8b86c5dc 2264
3d1058ed 2265#ifdef _WITH_ENCRYPTION
735bac5c 2266WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
3d1058ed
VK
2267#endif
2268
842c22f4 2269#if !defined(_WIN32) && !HAVE_WSTAT
735bac5c 2270int wstat(const WCHAR *_path, struct stat *_sbuf);
842c22f4
AK
2271#endif
2272
ef856d02 2273#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 2274
f3387429 2275#if !HAVE_WPOPEN
735bac5c 2276FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
f3387429 2277#endif
8b86c5dc 2278#if !HAVE_WFOPEN
735bac5c 2279FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
8b86c5dc 2280#endif
8c2489aa 2281#if HAVE_FOPEN64 && !HAVE_WFOPEN64
735bac5c 2282FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
8c2489aa 2283#endif
8b86c5dc 2284#if !HAVE_WOPEN
735bac5c 2285int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 2286#endif
f3387429 2287#if !HAVE_WCHMOD
735bac5c 2288int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
f3387429 2289#endif
b07c50cc 2290#if !HAVE_WCHDIR
735bac5c 2291int wchdir(const WCHAR *_path);
b07c50cc
VK
2292#endif
2293#if !HAVE_WMKDIR
735bac5c 2294int wmkdir(const WCHAR *_path, int mode);
b07c50cc 2295#endif
7b45b90d 2296#if !HAVE_WUTIME
2297int wutime(const WCHAR *_path, struct utimbuf *buf);
2298#endif
b07c50cc 2299#if !HAVE_WRMDIR
735bac5c 2300int wrmdir(const WCHAR *_path);
b07c50cc 2301#endif
ccc34207 2302#if !HAVE_WRENAME
735bac5c 2303int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
ccc34207
VK
2304#endif
2305#if !HAVE_WUNLINK
735bac5c 2306int wunlink(const WCHAR *_path);
ccc34207 2307#endif
b07c50cc 2308#if !HAVE_WREMOVE
735bac5c 2309int wremove(const WCHAR *_path);
b07c50cc
VK
2310#endif
2311#if !HAVE_WSYSTEM
735bac5c 2312int wsystem(const WCHAR *_cmd);
b07c50cc 2313#endif
dda7c270 2314#if !HAVE_WMKSTEMP
735bac5c 2315int wmkstemp(WCHAR *_template);
dda7c270 2316#endif
b07c50cc 2317#if !HAVE_WACCESS
735bac5c 2318int waccess(const WCHAR *_path, int mode);
b07c50cc 2319#endif
273da39a 2320#if !HAVE_WGETENV
735bac5c 2321WCHAR *wgetenv(const WCHAR *_string);
273da39a 2322#endif
f3387429 2323#if !HAVE_WCTIME
735bac5c 2324WCHAR *wctime(const time_t *timep);
f3387429 2325#endif
0186b5da 2326#if !HAVE_PUTWS
735bac5c 2327int putws(const WCHAR *s);
0186b5da 2328#endif
9757ca46 2329#if !HAVE_WCSERROR && (HAVE_STRERROR || HAVE_DECL_STRERROR)
735bac5c 2330WCHAR *wcserror(int errnum);
d49d7500
VK
2331#endif
2332#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06 2333#if HAVE_POSIX_STRERROR_R
735bac5c 2334int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
b0823d06 2335#else
735bac5c 2336WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
d49d7500 2337#endif
b0823d06 2338#endif
e05f8aaf 2339
ef856d02 2340#endif /* UNICODE && !_WIN32*/
e05f8aaf 2341
8b86c5dc 2342#if !HAVE_STRTOLL
735bac5c 2343INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
8b86c5dc
VK
2344#endif
2345#if !HAVE_STRTOULL
735bac5c 2346UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 2347#endif
e05f8aaf
VK
2348
2349#if !HAVE_WCSTOLL
735bac5c 2350INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf
VK
2351#endif
2352#if !HAVE_WCSTOULL
735bac5c 2353UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 2354#endif
b50f1100 2355
3b3ee220
VK
2356#if !HAVE_STRLWR && !defined(_WIN32)
2357char LIBNETXMS_EXPORTABLE *strlwr(char *str);
2358#endif
2359
2f56be29 2360#if !HAVE_WCSLWR && !defined(_WIN32)
735bac5c 2361WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
fdf1ed50
VK
2362#endif
2363
3a4c13cc
VK
2364#if !HAVE_STRLCPY
2365size_t LIBNETXMS_EXPORTABLE strlcpy(char *dst, const char *src, size_t size);
2366#endif
2367
2368#if !HAVE_WCSLCPY
2369size_t LIBNETXMS_EXPORTABLE wcslcpy(WCHAR *dst, const WCHAR *src, size_t size);
2370#endif
2371
2372#if !HAVE_STRLCAT
2373size_t LIBNETXMS_EXPORTABLE strlcat(char *dst, const char *src, size_t size);
2374#endif
2375
2376#if !HAVE_WCSLCAT
2377size_t LIBNETXMS_EXPORTABLE wcslcat(WCHAR *dst, const WCHAR *src, size_t size);
2378#endif
2379
4d978b4a 2380#if !HAVE_WCSCASECMP && !defined(_WIN32)
735bac5c 2381int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
4d978b4a
VK
2382#endif
2383
f4d2c9ba 2384#if !HAVE_WCSNCASECMP && !defined(_WIN32)
735bac5c 2385int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
f4d2c9ba
VK
2386#endif
2387
75b4d48e
VK
2388#if !defined(_WIN32) && (!HAVE_WCSFTIME || !WORKING_WCSFTIME)
2389size_t LIBNETXMS_EXPORTABLE nx_wcsftime(WCHAR *buffer, size_t bufsize, const WCHAR *format, const struct tm *t);
2390#undef wcsftime
2391#define wcsftime nx_wcsftime
2392#endif
2393
9138dfe2
VK
2394#ifndef _WIN32
2395
2396#if HAVE_ITOA && !HAVE__ITOA
2397#define _itoa itoa
2398#undef HAVE__ITOA
2399#define HAVE__ITOA 1
2400#endif
2401#if !HAVE__ITOA && !defined(_WIN32)
2402char LIBNETXMS_EXPORTABLE *_itoa(int value, char *str, int base);
2403#endif
2404
2405#if HAVE_ITOW && !HAVE__ITOW
2406#define _itow itow
2407#undef HAVE__ITOW
2408#define HAVE__ITOW 1
2409#endif
2410#if !HAVE__ITOW && !defined(_WIN32)
2411WCHAR LIBNETXMS_EXPORTABLE *_itow(int value, WCHAR *str, int base);
2412#endif
2413
2414#endif /* _WIN32 */
2415
3d1cb7d6 2416#ifdef _WIN32
5ea2db30 2417#ifdef UNICODE
1f704f19
VK
2418DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *path);
2419struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *p);
2420int LIBNETXMS_EXPORTABLE wclosedir(DIRW *p);
5ea2db30
VK
2421
2422#define _topendir wopendir
2423#define _treaddir wreaddir
2424#define _tclosedir wclosedir
2425#else
2426#define _topendir opendir
2427#define _treaddir readdir
2428#define _tclosedir closedir
2429#endif
2430
1f704f19
VK
2431DIR LIBNETXMS_EXPORTABLE *opendir(const char *path);
2432struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *p);
2433int LIBNETXMS_EXPORTABLE closedir(DIR *p);
5ea2db30 2434
76b1d3d8
VK
2435#else /* not _WIN32 */
2436
735bac5c
VK
2437DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
2438struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
2439int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
76b1d3d8 2440
37d62364 2441#endif
3d1cb7d6 2442
7f9e0c00 2443#if defined(_WIN32) || !(HAVE_SCANDIR)
735bac5c
VK
2444int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
2445 int (*select)(const struct dirent *),
2446 int (*compar)(const struct dirent **, const struct dirent **));
2447int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
7f9e0c00
VK
2448#endif
2449
3bf0df9f
VK
2450TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
2451
2df047f4 2452bool LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
90a9e65e 2453 unsigned int msgCount, const TCHAR **messages,
b41dff2a 2454 DWORD debugMsg, DWORD debugMsgTag, DWORD genericMsg);
90a9e65e 2455void LIBNETXMS_EXPORTABLE nxlog_close();
9f2902ac 2456void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
90a9e65e 2457void LIBNETXMS_EXPORTABLE nxlog_write_generic(WORD type, const TCHAR *format, ...);
2df047f4 2458void LIBNETXMS_EXPORTABLE nxlog_debug(int level, const TCHAR *format, ...);
b41dff2a 2459void LIBNETXMS_EXPORTABLE nxlog_debug_tag(const TCHAR *tag, int level, const TCHAR *format, ...);
2df047f4 2460void LIBNETXMS_EXPORTABLE nxlog_debug2(int level, const TCHAR *format, va_list args);
458b5d2b 2461bool LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, UINT64 maxLogSize, int historySize, const TCHAR *dailySuffix);
2df047f4
VK
2462bool LIBNETXMS_EXPORTABLE nxlog_rotate();
2463void LIBNETXMS_EXPORTABLE nxlog_set_debug_level(int level);
b41dff2a 2464void LIBNETXMS_EXPORTABLE nxlog_set_debug_level_tag(const TCHAR *tags, int level);
2df047f4 2465int LIBNETXMS_EXPORTABLE nxlog_get_debug_level();
b41dff2a 2466int LIBNETXMS_EXPORTABLE nxlog_get_debug_level_tag(const TCHAR *tag);
7c587518 2467
b41dff2a 2468typedef void (*NxLogDebugWriter)(const TCHAR *, const TCHAR *);
7c587518 2469void LIBNETXMS_EXPORTABLE nxlog_set_debug_writer(NxLogDebugWriter writer);
9113e749
VK
2470
2471typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
2472void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
2473
2474void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
6e383343
VK
2475void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
2476#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
2477 __attribute__ ((format(printf, 1, 2)))
2478#endif
2479;
9f2902ac 2480
4a64a795
VK
2481bool LIBNETXMS_EXPORTABLE ReadPassword(const TCHAR *prompt, TCHAR *buffer, size_t bufferSize);
2482
37a7a862
VK
2483#ifdef _WIN32
2484int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
2485int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
2486#ifdef UNICODE
2487#define _tmkstemp wmkstemp
2488#else
2489#define _tmkstemp mkstemp
2490#endif
2491#endif
2492
c57d020a
VK
2493#if !HAVE_STRPTIME
2494char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
2495#endif
2496
2497#if !HAVE_TIMEGM
2498time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
2499#endif
2500
6f95ef89
VK
2501#if !HAVE_INET_PTON
2502int LIBNETXMS_EXPORTABLE nx_inet_pton(int af, const char *src, void *dst);
2503#define inet_pton nx_inet_pton
2504#endif
2505
b06ae508 2506int LIBNETXMS_EXPORTABLE GetSleepTime(int hour, int minute, int second);
b60584cf
VK
2507time_t LIBNETXMS_EXPORTABLE ParseDateTimeA(const char *text, time_t defaultValue);
2508time_t LIBNETXMS_EXPORTABLE ParseDateTimeW(const WCHAR *text, time_t defaultValue);
2509
2510#ifdef UNICODE
2511#define ParseDateTime ParseDateTimeW
2512#else
2513#define ParseDateTime ParseDateTimeA
2514#endif
b06ae508 2515
b50f1100 2516#ifdef __cplusplus
e9246d26 2517}
b50f1100 2518#endif
e9246d26 2519
a7ff20a5
VK
2520
2521//
b06ae508 2522// C++ only functions
a7ff20a5
VK
2523//
2524
2525#ifdef __cplusplus
2526
a97f02e1
VK
2527enum nxDirectoryType
2528{
2529 nxDirBin = 0,
2530 nxDirData = 1,
2531 nxDirEtc = 2,
1039d7ee
VK
2532 nxDirLib = 3,
2533 nxDirShare = 4
a97f02e1
VK
2534};
2535
5962498b
VK
2536TCHAR LIBNETXMS_EXPORTABLE *GetHeapInfo();
2537
a97f02e1
VK
2538void LIBNETXMS_EXPORTABLE GetNetXMSDirectory(nxDirectoryType type, TCHAR *dir);
2539
43709a0c
VK
2540UINT32 LIBNETXMS_EXPORTABLE IcmpPing(const InetAddress& addr, int iNumRetries, UINT32 dwTimeout, UINT32 *pdwRTT, UINT32 dwPacketSize);
2541
240816b0
VK
2542TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
2543String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
2544const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
2545int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 2546UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
2547bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
2548
0356b995 2549String LIBNETXMS_EXPORTABLE EscapeStringForJSON(const TCHAR *s);
1d919454 2550String LIBNETXMS_EXPORTABLE EscapeStringForAgent(const TCHAR *s);
0356b995 2551
e9bf9011
VK
2552StringList LIBNETXMS_EXPORTABLE *ParseCommandLine(const TCHAR *cmdline);
2553
53c96e2d 2554#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
d37e7f10 2555void LIBNETXMS_EXPORTABLE BlockAllSignals(bool processWide, bool allowInterrupt);
9113e749 2556void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
2557#endif
2558
18a7c1e9
VK
2559String LIBNETXMS_EXPORTABLE GenerateLineDiff(const String& left, const String& right);
2560
a7ff20a5
VK
2561#endif
2562
e9246d26 2563#endif /* _nms_util_h_ */