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