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