rack height and numbering direction can be changed
[public/netxms.git] / include / nms_util.h
CommitLineData
cc022855 1/*
7ac71e60 2** NetXMS - Network Management System
6f95ef89 3** Copyright (C) 2003-2015 Victor Kirhenshtein
e9246d26
VK
4**
5** This program is free software; you can redistribute it and/or modify
68f384ea
VK
6** it under the terms of the GNU Lesser General Public License as published
7** by the Free Software Foundation; either version 3 of the License, or
e9246d26
VK
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
68f384ea 15** You should have received a copy of the GNU Lesser General Public License
e9246d26
VK
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
fb7ce4b9 19** File: nms_util.h
e9246d26
VK
20**
21**/
22
23#ifndef _nms_util_h_
24#define _nms_util_h_
25
64584976
VK
26#ifdef _WIN32
27#ifdef LIBNETXMS_EXPORTS
28#define LIBNETXMS_EXPORTABLE __declspec(dllexport)
29#else
30#define LIBNETXMS_EXPORTABLE __declspec(dllimport)
31#endif
32#else /* _WIN32 */
33#define LIBNETXMS_EXPORTABLE
34#endif
35
36
e9246d26
VK
37#include <nms_common.h>
38#include <nms_cscp.h>
953dfe76 39#include <nms_threads.h>
64584976 40#include <time.h>
e9246d26 41
6d8aee95
VK
42#if HAVE_BYTESWAP_H
43#include <byteswap.h>
44#endif
45
92502d5f
VK
46#include <base64.h>
47
4b47d7d7
VK
48/*** Byte swapping ***/
49#if WORDS_BIGENDIAN
50#define htonq(x) (x)
51#define ntohq(x) (x)
52#define htond(x) (x)
53#define ntohd(x) (x)
54#define SwapWideString(x)
55#else
56#ifdef HAVE_HTONLL
57#define htonq(x) htonll(x)
58#else
59#define htonq(x) __bswap_64(x)
60#endif
61#ifdef HAVE_NTOHLL
62#define ntohq(x) ntohll(x)
63#else
64#define ntohq(x) __bswap_64(x)
65#endif
66#define htond(x) __bswap_double(x)
67#define ntohd(x) __bswap_double(x)
68#define SwapWideString(x) __bswap_wstr(x)
69#endif
6d8aee95 70
4b47d7d7
VK
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
76UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
77#endif
78#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
79UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
80#endif
81double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
82void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
6d8aee95 83
4b47d7d7
VK
84#ifdef __cplusplus
85}
86#endif
87
88
89/*** Serial communications ***/
6d8aee95
VK
90#ifdef _WIN32
91
92#define FLOW_NONE 0
93#define FLOW_SOFTWARE 1
94#define FLOW_HARDWARE 2
95
96#else /* _WIN32 */
97
70fed300
AK
98#ifdef HAVE_TERMIOS_H
99# include <termios.h>
100#else
101# error termios.h not found
102#endif
103
6d8aee95
VK
104#endif /* _WIN32 */
105
e2e66145
VK
106/**
107 * Return codes for IcmpPing()
108 */
66c04cc7
VK
109#define ICMP_SUCCESS 0
110#define ICMP_UNREACHEABLE 1
111#define ICMP_TIMEOUT 2
112#define ICMP_RAW_SOCK_FAILED 3
5418d4bb 113#define ICMP_API_ERROR 4
66c04cc7 114
e2e66145
VK
115/**
116 * Token types for configuration loader
117 */
4826c8a5
VK
118#define CT_LONG 0
119#define CT_STRING 1
120#define CT_STRING_LIST 2
121#define CT_END_OF_LIST 3
122#define CT_BOOLEAN 4
123#define CT_WORD 5
fc935a60 124#define CT_IGNORE 6
dda7c270 125#define CT_MB_STRING 7
e95680e5 126#define CT_BOOLEAN64 8
4826c8a5 127
605fc935 128/**
e2e66145
VK
129 * Uninitialized value for override indicator
130 */
131#define NXCONFIG_UNINITIALIZED_VALUE (-1)
132
133/**
605fc935
VK
134 * Return codes for NxLoadConfig()
135 */
4826c8a5
VK
136#define NXCFG_ERR_OK 0
137#define NXCFG_ERR_NOFILE 1
138#define NXCFG_ERR_SYNTAX 2
139
605fc935
VK
140/**
141 * nxlog_open() flags
142 */
4e0e77e6
VK
143#define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
144#define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
145#define NXLOG_BACKGROUND_WRITER ((UINT32)0x00000004)
146#define NXLOG_IS_OPEN ((UINT32)0x80000000)
9f2902ac 147
605fc935
VK
148/**
149 * nxlog rotation policy
150 */
5945d50a
VK
151#define NXLOG_ROTATION_DISABLED 0
152#define NXLOG_ROTATION_DAILY 1
153#define NXLOG_ROTATION_BY_SIZE 2
154
605fc935
VK
155/**
156 * _tcsdup() replacement
157 */
8ff13508
VK
158#if defined(_WIN32) && defined(USE_WIN32_HEAP)
159#ifdef __cplusplus
160extern "C" {
161#endif
fd72502a 162char LIBNETXMS_EXPORTABLE *nx_strdup(const char *src);
8ff13508
VK
163WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
164#ifdef __cplusplus
165}
166#endif
167#endif
168
605fc935 169/**
f4d2c9ba
VK
170 * Custom wcsdup
171 */
172#if !HAVE_WCSDUP && !defined(_WIN32)
fd72502a 173
f4d2c9ba
VK
174#ifdef __cplusplus
175extern "C" {
176#endif
177 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
178#ifdef __cplusplus
179}
180#endif
fd72502a
VK
181
182#elif defined(_AIX)
183
184// Some AIX versions have broken wcsdup() so we use internal implementation
185#ifdef __cplusplus
186extern "C" {
187#endif
188 WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
189#ifdef __cplusplus
190}
191#endif
192
193#define wcsdup nx_wcsdup
194
f4d2c9ba
VK
195#endif
196
c012b7b7
VK
197#ifdef __cplusplus
198
f4d2c9ba 199/**
c83f6449
VK
200 * Extended tcsdup: returns NULL if NULL pointer passed
201 */
202inline TCHAR *_tcsdup_ex(const TCHAR *s)
203{
204 return (s != NULL) ? _tcsdup(s) : NULL;
205}
206
c012b7b7
VK
207#endif
208
c83f6449 209/**
d65c16a8
VK
210 * String conversion helpers
211 */
212#ifdef __cplusplus
213extern "C" {
214#endif
215WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
216WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
217char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
218char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
219#ifdef __cplusplus
220}
221#endif
222
223/**
605fc935
VK
224 * Class for serial communications
225 */
b160350d
VK
226#ifdef __cplusplus
227
228#ifndef _WIN32
229enum
230{
231 NOPARITY,
232 ODDPARITY,
233 EVENPARITY,
234 ONESTOPBIT,
235 TWOSTOPBITS
236};
237
70fed300
AK
238enum
239{
240 FLOW_NONE,
241 FLOW_HARDWARE,
242 FLOW_SOFTWARE
243};
244
b160350d
VK
245#ifndef INVALID_HANDLE_VALUE
246#define INVALID_HANDLE_VALUE (-1)
247#endif
248#endif /* _WIN32 */
249
250class LIBNETXMS_EXPORTABLE Serial
251{
b160350d
VK
252private:
253 TCHAR *m_pszPort;
254 int m_nTimeout;
255 int m_nSpeed;
256 int m_nDataBits;
257 int m_nStopBits;
258 int m_nParity;
70fed300 259 int m_nFlowControl;
b160350d
VK
260
261#ifndef _WIN32
262 int m_hPort;
70fed300 263 struct termios m_originalSettings;
b160350d
VK
264#else
265 HANDLE m_hPort;
266#endif
b160350d 267
826fcc1f
VK
268public:
269 Serial();
270 ~Serial();
271
272 bool open(const TCHAR *pszPort);
273 void close();
274 void setTimeout(int nTimeout);
275 int read(char *pBuff, int nSize); /* waits up to timeout and do single read */
276 int readAll(char *pBuff, int nSize); /* read until timeout or out of space */
c3f71625 277 int readToMark(char *buff, int size, const char **marks, char **occurence);
826fcc1f
VK
278 bool write(const char *pBuff, int nSize);
279 void flush();
280 bool set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl = FLOW_NONE);
281 bool restart();
282};
ff214673 283
3fe40613
VK
284/**
285 * Dynamic string class
286 */
8bad8476
VK
287class LIBNETXMS_EXPORTABLE String
288{
289protected:
4e0e77e6
VK
290 TCHAR *m_buffer;
291 size_t m_length;
292 size_t m_allocated;
dcc7a2b0 293 size_t m_allocationStep;
8bad8476
VK
294
295public:
c577e360
VK
296 static const int npos;
297
8bad8476 298 String();
f3cdd1c5 299 String(const TCHAR *init);
643c9dcb 300 String(const String &src);
8bad8476
VK
301 ~String();
302
4e0e77e6
VK
303 TCHAR *getBuffer() { return m_buffer; }
304 void setBuffer(TCHAR *buffer);
8bad8476 305
dcc7a2b0
VK
306 size_t getAllocationStep() { return m_allocationStep; }
307 void setAllocationStep(size_t step) { m_allocationStep = step; }
4e0e77e6
VK
308
309 const String& operator =(const TCHAR *str);
49bf9d8d 310 const String& operator =(const String &src);
1d34c533
VK
311 const String& operator +=(const TCHAR *str);
312 const String& operator +=(const String &str);
4e0e77e6 313 operator const TCHAR*() { return CHECK_NULL_EX(m_buffer); }
8bad8476 314
08b214c6
VK
315 char *getUTF8String();
316
4e0e77e6
VK
317 void append(const TCHAR *str) { if (str != NULL) append(str, _tcslen(str)); }
318 void append(const TCHAR *str, size_t len);
f98a8402 319 void append(const TCHAR c) { append(&c, 1); }
5f648670
VK
320 void append(INT32 n);
321 void append(UINT32 n);
322 void append(INT64 n);
323 void append(UINT64 n);
4e0e77e6
VK
324
325 void appendPreallocated(TCHAR *str) { if (str != NULL) { append(str); free(str); } }
326
327 void appendMBString(const char *str, size_t len, int nCodePage);
328 void appendWideString(const WCHAR *str, size_t len);
c577e360 329
4e0e77e6
VK
330 void appendFormattedString(const TCHAR *format, ...);
331 void appendFormattedStringV(const TCHAR *format, va_list args);
ff175e91 332
4e0e77e6 333 void clear();
c577e360 334
4e0e77e6
VK
335 size_t length() const { return m_length; }
336 bool isEmpty() const { return m_length == 0; }
c577e360 337
7ac965a4 338 wchar_t charAt(size_t pos) const { return (pos < m_length) ? m_buffer[pos] : 0; }
807a4c40 339
4e0e77e6
VK
340 TCHAR *substring(int nStart, int nLen, TCHAR *pszBuffer = NULL);
341 int find(const TCHAR *str, int nStart = 0);
1e1450ef 342
6bcafa2d
VK
343 void escapeCharacter(int ch, int esc);
344 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
0bbab9d3
VK
345 void trim();
346 void shrink(int chars = 1);
c577e360
VK
347};
348
3fe40613 349/**
f9861ff9
VK
350 * Dynamic array class
351 */
352class LIBNETXMS_EXPORTABLE Array
353{
354private:
355 int m_size;
356 int m_allocated;
357 int m_grow;
358 size_t m_elementSize;
359 void **m_data;
360 bool m_objectOwner;
361
362 void internalRemove(int index, bool allowDestruction);
363 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
364
365protected:
366 bool m_storePointers;
367 void (*m_objectDestructor)(void *);
368
369 Array(void *data, int initial, int grow, size_t elementSize);
dcc7a2b0 370 void *__getBuffer() const { return m_data; }
f9861ff9
VK
371
372public:
373 Array(int initial = 0, int grow = 16, bool owner = false);
374 virtual ~Array();
375
376 int add(void *element);
dcc7a2b0
VK
377 void *get(int index) const { return ((index >= 0) && (index < m_size)) ? (m_storePointers ? m_data[index] : (void *)((char *)m_data + index * m_elementSize)): NULL; }
378 int indexOf(void *element) const;
f9861ff9
VK
379 void set(int index, void *element);
380 void replace(int index, void *element);
381 void remove(int index) { internalRemove(index, true); }
382 void remove(void *element) { internalRemove(indexOf(element), true); }
383 void unlink(int index) { internalRemove(index, false); }
384 void unlink(void *element) { internalRemove(indexOf(element), false); }
385 void clear();
386 void sort(int (*cb)(const void *, const void *));
dcc7a2b0 387 void *find(const void *key, int (*cb)(const void *, const void *)) const;
f9861ff9 388
dcc7a2b0 389 int size() const { return m_size; }
f9861ff9
VK
390
391 void setOwner(bool owner) { m_objectOwner = owner; }
dcc7a2b0 392 bool isOwner() const { return m_objectOwner; }
f9861ff9
VK
393};
394
395/**
396 * Template class for dynamic array which holds objects
397 */
398template <class T> class ObjectArray : public Array
399{
400private:
401 static void destructor(void *object) { delete (T*)object; }
402
403public:
404 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
405 virtual ~ObjectArray() { }
406
407 int add(T *object) { return Array::add((void *)object); }
dcc7a2b0
VK
408 T *get(int index) const { return (T*)Array::get(index); }
409 int indexOf(T *object) const { return Array::indexOf((void *)object); }
dfed892b 410 bool contains(T *object) const { return indexOf(object) >= 0; }
f9861ff9
VK
411 void set(int index, T *object) { Array::set(index, (void *)object); }
412 void replace(int index, T *object) { Array::replace(index, (void *)object); }
413 void remove(int index) { Array::remove(index); }
414 void remove(T *object) { Array::remove((void *)object); }
415 void unlink(int index) { Array::unlink(index); }
416 void unlink(T *object) { Array::unlink((void *)object); }
417};
418
419/**
420 * Template class for dynamic array which holds scalar values
421 */
422template <class T> class IntegerArray : public Array
423{
424private:
425 static void destructor(void *element) { }
426
427public:
428 IntegerArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; m_storePointers = (sizeof(T) == sizeof(void *)); }
429 virtual ~IntegerArray() { }
430
431 int add(T value) { return Array::add(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
e5390fb5 432 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 433 int indexOf(T value) const { return Array::indexOf(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
dfed892b 434 bool contains(T value) const { return indexOf(value) >= 0; }
f9861ff9
VK
435 void set(int index, T value) { Array::set(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
436 void replace(int index, T value) { Array::replace(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
437
dcc7a2b0 438 T *getBuffer() const { return (T*)__getBuffer(); }
f9861ff9
VK
439};
440
441/**
442 * Auxilliary class to hold dynamically allocated array of structures
443 */
444template <class T> class StructArray : public Array
445{
446private:
447 static void destructor(void *element) { }
448
449public:
450 StructArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; }
451 StructArray(T *data, int size) : Array(data, size, 16, sizeof(T)) { m_objectDestructor = destructor; }
452 virtual ~StructArray() { }
453
454 int add(T *element) { return Array::add((void *)element); }
dcc7a2b0
VK
455 T *get(int index) const { return (T*)Array::get(index); }
456 int indexOf(T *element) const { return Array::indexOf((void *)element); }
dfed892b 457 bool contains(T *element) const { return indexOf(element) >= 0; }
f9861ff9
VK
458 void set(int index, T *element) { Array::set(index, (void *)element); }
459 void replace(int index, T *element) { Array::replace(index, (void *)element); }
460 void remove(int index) { Array::remove(index); }
461 void remove(T *element) { Array::remove((void *)element); }
462 void unlink(int index) { Array::unlink(index); }
463 void unlink(T *element) { Array::unlink((void *)element); }
464
dcc7a2b0 465 T *getBuffer() const { return (T*)__getBuffer(); }
f9861ff9
VK
466};
467
468/**
469 * Entry of string map (internal)
400e55c4
VK
470 */
471struct StringMapEntry;
472
473/**
f9861ff9
VK
474 * Key/value pair
475 */
476struct KeyValuePair
477{
478 const TCHAR *key;
479 const void *value;
480};
481
482/**
3fe40613
VK
483 * String maps base class
484 */
485class LIBNETXMS_EXPORTABLE StringMapBase
c577e360
VK
486{
487protected:
400e55c4 488 StringMapEntry *m_data;
3fe40613 489 bool m_objectOwner;
55bdca5a 490 bool m_ignoreCase;
3fe40613 491 void (*m_objectDestructor)(void *);
c577e360 492
6e14de7c 493 StringMapEntry *find(const TCHAR *key) const;
3fe40613 494 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
6e14de7c 495 void *getObject(const TCHAR *key) const;
3fe40613 496 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
c577e360
VK
497
498public:
3fe40613
VK
499 StringMapBase(bool objectOwner);
500 virtual ~StringMapBase();
501
55bdca5a 502 void setOwner(bool owner) { m_objectOwner = owner; }
91a581c5 503 void setIgnoreCase(bool ignore);
55bdca5a 504
3fe40613
VK
505 void remove(const TCHAR *key);
506 void clear();
c07cf3be 507 void filterElements(bool (*filter)(const TCHAR *, const void *, void *), void *userData);
3fe40613 508
6e14de7c
VK
509 int size() const;
510 bool contains(const TCHAR *key) const { return find(key) != NULL; }
400e55c4 511
6e14de7c
VK
512 EnumerationCallbackResult forEach(EnumerationCallbackResult (*cb)(const TCHAR *, const void *, void *), void *userData) const;
513 const void *findElement(bool (*comparator)(const TCHAR *, const void *, void *), void *userData) const;
f9861ff9 514
6e14de7c
VK
515 StructArray<KeyValuePair> *toArray() const;
516 StringList *keys() const;
3fe40613
VK
517};
518
519/**
400e55c4
VK
520 * NXCP message class
521 */
b368969c 522class NXCPMessage;
400e55c4
VK
523
524/**
3fe40613
VK
525 * String map class
526 */
527class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
528{
529public:
530 StringMap() : StringMapBase(true) { }
fb986055 531 StringMap(const StringMap &src);
3fe40613 532 virtual ~StringMap();
c577e360 533
fb986055 534 StringMap& operator =(const StringMap &src);
40b7416c 535
3fe40613
VK
536 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
537 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
967893bb 538 void set(const TCHAR *key, UINT32 value);
5ad2167d 539
a6312bd6
VK
540 void addAll(StringMap *src);
541
6e14de7c
VK
542 const TCHAR *get(const TCHAR *key) const { return (const TCHAR *)getObject(key); }
543 UINT32 getULong(const TCHAR *key, UINT32 defaultValue) const;
544 bool getBoolean(const TCHAR *key, bool defaultValue) const;
5ad2167d 545
6e14de7c 546 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
8bad8476
VK
547};
548
3fe40613
VK
549/**
550 * String map template for holding objects as values
551 */
552template <class T> class StringObjectMap : public StringMapBase
553{
554private:
555 static void destructor(void *object) { delete (T*)object; }
6173bea8 556
3fe40613
VK
557public:
558 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
559
560 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
6e14de7c 561 T *get(const TCHAR *key) const { return (T*)getObject(key); }
3fe40613 562};
6173bea8 563
3fe40613
VK
564/**
565 * String list class
566 */
6173bea8
VK
567class LIBNETXMS_EXPORTABLE StringList
568{
569private:
570 int m_count;
571 int m_allocated;
572 TCHAR **m_values;
573
574public:
575 StringList();
01116597 576 StringList(const StringList *src);
22ee6d97 577 StringList(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
578 ~StringList();
579
580 void add(const TCHAR *value);
581 void addPreallocated(TCHAR *value);
967893bb
VK
582 void add(INT32 value);
583 void add(UINT32 value);
6173bea8 584 void add(INT64 value);
967893bb 585 void add(UINT64 value);
6173bea8 586 void add(double value);
24dc5346 587 void replace(int index, const TCHAR *value);
b0353a33
VK
588 void addOrReplace(int index, const TCHAR *value);
589 void addOrReplacePreallocated(int index, TCHAR *value);
6173bea8 590 void clear();
01116597
VK
591 int size() const { return m_count; }
592 const TCHAR *get(int index) const { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
593 int indexOf(const TCHAR *value) const;
594 int indexOfIgnoreCase(const TCHAR *value) const;
d51f2182 595 void remove(int index);
86c126f5 596 void addAll(const StringList *src);
93dbc1be 597 void merge(const StringList *src, bool matchCase);
7d25a5c5 598 TCHAR *join(const TCHAR *separator);
22ee6d97 599 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
7d37e727 600 void sort(bool ascending = true, bool caseSensitive = false);
6fbaa926 601 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId);
6173bea8
VK
602};
603
3fe40613 604/**
a58501c0
VK
605 * Entry of string set
606 */
607struct StringSetEntry;
608
609/**
f3603b79
VK
610 * NXCP message
611 */
b368969c 612class NXCPMessage;
f3603b79
VK
613
614/**
a58501c0
VK
615 * String set class
616 */
617class LIBNETXMS_EXPORTABLE StringSet
618{
619private:
620 StringSetEntry *m_data;
621
622public:
623 StringSet();
624 ~StringSet();
625
626 void add(const TCHAR *str);
f3603b79 627 void addPreallocated(TCHAR *str);
a58501c0
VK
628 void remove(const TCHAR *str);
629 void clear();
630
631 int size();
f3603b79 632 bool contains(const TCHAR *str);
5b521bfc 633 bool equals(StringSet *s);
a58501c0 634
fa493140 635 void addAll(StringSet *src);
f3603b79 636 void addAll(TCHAR **strings, int count);
d45f9179 637 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
d6c4cdc1 638 void addAllPreallocated(TCHAR **strings, int count);
a58501c0 639 void forEach(bool (*cb)(const TCHAR *, void *), void *userData);
f3603b79 640
b368969c
VK
641 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId);
642 void addAllFromMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId, bool clearBeforeAdd, bool toUppercase);
c262bc08 643
d45f9179 644 String join(const TCHAR *separator);
a58501c0
VK
645};
646
647/**
296ae03d
VK
648 * Opaque hash map entry structure
649 */
650struct HashMapEntry;
651
652/**
653 * Hash map base class (for fixed size non-pointer keys)
654 */
655class LIBNETXMS_EXPORTABLE HashMapBase
656{
657private:
658 HashMapEntry *m_data;
659 bool m_objectOwner;
660 unsigned int m_keylen;
661
662 HashMapEntry *find(const void *key);
663 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
664
665protected:
666 void (*m_objectDestructor)(void *);
667
668 HashMapBase(bool objectOwner, unsigned int keylen);
669
670 void *_get(const void *key);
671 void _set(const void *key, void *value);
672 void _remove(const void *key);
673
674 bool _contains(const void *key) { return find(key) != NULL; }
675
676public:
677 virtual ~HashMapBase();
678
679 void setOwner(bool owner) { m_objectOwner = owner; }
680 void clear();
681
682 int size();
683
53c3d1e7 684 EnumerationCallbackResult forEach(EnumerationCallbackResult (*cb)(const void *, const void *, void *), void *userData);
296ae03d
VK
685 const void *findElement(bool (*comparator)(const void *, const void *, void *), void *userData);
686};
687
688/**
689 * Hash map template for holding objects as values
690 */
691template <class K, class V> class HashMap : public HashMapBase
692{
693private:
694 static void destructor(void *object) { delete (V*)object; }
695
696public:
697 HashMap(bool objectOwner = false) : HashMapBase(objectOwner, sizeof(K)) { m_objectDestructor = destructor; }
698
699 V *get(const K& key) { return (V*)_get(&key); }
700 void set(const K& key, V *value) { _set(&key, (void *)value); }
701 void remove(const K& key) { _remove(&key); }
702 bool contains(const K& key) { return _contains(&key); }
703};
704
705/**
57f34c0a
VK
706 * Byte stream
707 */
708class LIBNETXMS_EXPORTABLE ByteStream
709{
710private:
711 BYTE *m_data;
712 size_t m_size;
713 size_t m_allocated;
714 size_t m_pos;
3cf3dedd 715 size_t m_allocationStep;
57f34c0a
VK
716
717public:
718 ByteStream(size_t initial = 8192);
719 ByteStream(const void *data, size_t size);
720 virtual ~ByteStream();
721
4b47d7d7
VK
722 static ByteStream *load(const TCHAR *file);
723
724 void seek(size_t pos) { if (pos <= m_size) m_pos = pos; }
57f34c0a
VK
725 size_t pos() { return m_pos; }
726 size_t size() { return m_size; }
727 bool eos() { return m_pos == m_size; }
728
3cf3dedd
VK
729 void setAllocationStep(size_t s) { m_allocationStep = s; }
730
731 const BYTE *buffer(size_t *size) { *size = m_size; return m_data; }
57f34c0a 732
4b47d7d7 733 void write(const void *data, size_t size);
57f34c0a
VK
734 void write(char c) { write(&c, 1); }
735 void write(BYTE b) { write(&b, 1); }
736 void write(INT16 n) { UINT16 x = htons((UINT16)n); write(&x, 2); }
737 void write(UINT16 n) { UINT16 x = htons(n); write(&x, 2); }
738 void write(INT32 n) { UINT32 x = htonl((UINT32)n); write(&x, 4); }
739 void write(UINT32 n) { UINT32 x = htonl(n); write(&x, 4); }
4b47d7d7
VK
740 void write(INT64 n) { UINT64 x = htonq((UINT64)n); write(&x, 8); }
741 void write(UINT64 n) { UINT64 x = htonq(n); write(&x, 8); }
742 void write(double n) { double x = htond(n); write(&x, 8); }
743 void writeString(const TCHAR *s);
57f34c0a 744
4b47d7d7 745 size_t read(void *buffer, size_t count);
57f34c0a
VK
746 char readChar() { return !eos() ? (char)m_data[m_pos++] : 0; }
747 BYTE readByte() { return !eos() ? m_data[m_pos++] : 0; }
748 INT16 readInt16();
749 UINT16 readUInt16();
750 INT32 readInt32();
751 UINT32 readUInt32();
4b47d7d7
VK
752 INT64 readInt64();
753 UINT64 readUInt64();
754 double readDouble();
755 TCHAR *readString();
756
757 bool save(int f);
57f34c0a
VK
758};
759
760/**
25fe6265
VK
761 * Auxilliary class for objects which counts references and
762 * destroys itself wheren reference count falls to 0
763 */
7f632dfe
VK
764class LIBNETXMS_EXPORTABLE RefCountObject
765{
766private:
7785322f 767 VolatileCounter m_refCount;
7f632dfe
VK
768
769public:
7785322f
VK
770 RefCountObject()
771 {
772 m_refCount = 1;
773 }
b549a0f8 774 virtual ~RefCountObject();
7785322f
VK
775
776 void incRefCount()
777 {
778 InterlockedIncrement(&m_refCount);
779 }
780
781 void decRefCount()
782 {
783 if (InterlockedDecrement(&m_refCount) == 0)
784 delete this;
785 }
7f632dfe
VK
786};
787
b368969c 788class NXCPMessage;
967893bb
VK
789
790/**
791 * Table column definition
792 */
793class LIBNETXMS_EXPORTABLE TableColumnDefinition
794{
795private:
796 TCHAR *m_name;
797 TCHAR *m_displayName;
798 INT32 m_dataType;
799 bool m_instanceColumn;
800
801public:
802 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
b368969c 803 TableColumnDefinition(NXCPMessage *msg, UINT32 baseId);
967893bb
VK
804 TableColumnDefinition(TableColumnDefinition *src);
805 ~TableColumnDefinition();
806
b368969c 807 void fillMessage(NXCPMessage *msg, UINT32 baseId);
967893bb
VK
808
809 const TCHAR *getName() { return m_name; }
810 const TCHAR *getDisplayName() { return m_displayName; }
811 INT32 getDataType() { return m_dataType; }
812 bool isInstanceColumn() { return m_instanceColumn; }
813
814 void setDataType(INT32 type) { m_dataType = type; }
815 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
a0ddfb29 816 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
967893bb
VK
817};
818
819/**
d6bbfa4e
VK
820 * Table cell
821 */
822class TableCell
823{
824private:
825 TCHAR *m_value;
826 int m_status;
43666be9 827 UINT32 m_objectId;
d6bbfa4e
VK
828
829public:
43666be9
VK
830 TableCell() { m_value = NULL; m_status = -1; m_objectId = 0; }
831 TableCell(const TCHAR *value) { m_value = _tcsdup_ex(value); m_status = -1; m_objectId = 0; }
832 TableCell(const TCHAR *value, int status) { m_value = _tcsdup_ex(value); m_status = status; m_objectId = 0; }
833 TableCell(TableCell *src) { m_value = _tcsdup_ex(src->m_value); m_status = src->m_status; m_objectId = src->m_objectId; }
d6bbfa4e
VK
834 ~TableCell() { safe_free(m_value); }
835
43666be9
VK
836 void set(const TCHAR *value, int status, UINT32 objectId) { safe_free(m_value); m_value = _tcsdup_ex(value); m_status = status; m_objectId = objectId; }
837 void setPreallocated(TCHAR *value, int status, UINT32 objectId) { safe_free(m_value); m_value = value; m_status = status; m_objectId = objectId; }
d6bbfa4e
VK
838
839 const TCHAR *getValue() { return m_value; }
c83f6449 840 void setValue(const TCHAR *value) { safe_free(m_value); m_value = _tcsdup_ex(value); }
d6bbfa4e
VK
841 void setPreallocatedValue(TCHAR *value) { safe_free(m_value); m_value = value; }
842
843 int getStatus() { return m_status; }
844 void setStatus(int status) { m_status = status; }
43666be9
VK
845
846 int getObjectId() { return m_objectId; }
847 void setObjectId(UINT32 id) { m_objectId = id; }
d6bbfa4e
VK
848};
849
850/**
851 * Table row
852 */
853class TableRow
854{
855private:
856 ObjectArray<TableCell> *m_cells;
857 UINT32 m_objectId;
bac74bf7 858
d6bbfa4e
VK
859public:
860 TableRow(int columnCount);
861 TableRow(TableRow *src);
862 ~TableRow() { delete m_cells; }
863
864 void addColumn() { m_cells->add(new TableCell); }
865 void deleteColumn(int index) { m_cells->remove(index); }
bac74bf7 866
43666be9
VK
867 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); }
868 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
869
870 void setValue(int index, const TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setValue(value); }
871 void setPreallocatedValue(int index, TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocatedValue(value); }
872
873 void setStatus(int index, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setStatus(status); }
874
875 const TCHAR *getValue(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getValue() : NULL; }
876 int getStatus(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getStatus() : -1; }
877
878 UINT32 getObjectId() { return m_objectId; }
879 void setObjectId(UINT32 id) { m_objectId = id; }
43666be9
VK
880
881 UINT32 getCellObjectId(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getObjectId() : 0; }
882 void setCellObjectId(int index, UINT32 id) { TableCell *c = m_cells->get(index); if (c != NULL) c->setObjectId(id); }
d6bbfa4e
VK
883};
884
885/**
967893bb
VK
886 * Class for table data storage
887 */
a0ddfb29 888class LIBNETXMS_EXPORTABLE Table : public RefCountObject
967893bb
VK
889{
890private:
d6bbfa4e 891 ObjectArray<TableRow> *m_data;
967893bb
VK
892 ObjectArray<TableColumnDefinition> *m_columns;
893 TCHAR *m_title;
894 int m_source;
d6bbfa4e 895 bool m_extendedFormat;
967893bb 896
b368969c 897 void createFromMessage(NXCPMessage *msg);
967893bb 898 void destroy();
63ff3c9d 899 bool parseXML(const char *xml);
967893bb
VK
900
901public:
902 Table();
a0ddfb29 903 Table(Table *src);
b368969c 904 Table(NXCPMessage *msg);
b549a0f8 905 virtual ~Table();
967893bb 906
b368969c
VK
907 int fillMessage(NXCPMessage &msg, int offset, int rowLimit);
908 void updateFromMessage(NXCPMessage *msg);
967893bb 909
a0ddfb29
VK
910 void addAll(Table *src);
911 void copyRow(Table *src, int row);
967893bb 912
d6bbfa4e
VK
913 int getNumRows() { return m_data->size(); }
914 int getNumColumns() { return m_columns->size(); }
967893bb
VK
915 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
916 int getSource() { return m_source; }
917
d6bbfa4e
VK
918 bool isExtendedFormat() { return m_extendedFormat; }
919 void setExtendedFormat(bool ext) { m_extendedFormat = ext; }
920
967893bb
VK
921 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
922 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
923 int getColumnIndex(const TCHAR *name);
924 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
925
c83f6449 926 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = _tcsdup_ex(title); }
967893bb 927 void setSource(int source) { m_source = source; }
8dba1eac 928 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
967893bb
VK
929 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
930 int addRow();
931
25076d53
VK
932 void deleteRow(int row);
933 void deleteColumn(int col);
934
967893bb
VK
935 void setAt(int nRow, int nCol, INT32 nData);
936 void setAt(int nRow, int nCol, UINT32 dwData);
937 void setAt(int nRow, int nCol, double dData);
938 void setAt(int nRow, int nCol, INT64 nData);
939 void setAt(int nRow, int nCol, UINT64 qwData);
940 void setAt(int nRow, int nCol, const TCHAR *pszData);
941 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
942
d6bbfa4e
VK
943 void set(int nCol, INT32 nData) { setAt(getNumRows() - 1, nCol, nData); }
944 void set(int nCol, UINT32 dwData) { setAt(getNumRows() - 1, nCol, dwData); }
945 void set(int nCol, double dData) { setAt(getNumRows() - 1, nCol, dData); }
946 void set(int nCol, INT64 nData) { setAt(getNumRows() - 1, nCol, nData); }
947 void set(int nCol, UINT64 qwData) { setAt(getNumRows() - 1, nCol, qwData); }
d65c16a8
VK
948 void set(int nCol, const TCHAR *data) { setAt(getNumRows() - 1, nCol, data); }
949#ifdef UNICODE
950 void set(int nCol, const char *data) { setPreallocatedAt(getNumRows() - 1, nCol, WideStringFromMBString(data)); }
951#else
952 void set(int nCol, const WCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, MBStringFromWideString(data)); }
953#endif
954 void setPreallocated(int nCol, TCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, data); }
d6bbfa4e
VK
955
956 void setStatusAt(int row, int col, int status);
957 void setStatus(int col, int status) { setStatusAt(getNumRows() - 1, col, status); }
967893bb
VK
958
959 const TCHAR *getAsString(int nRow, int nCol);
960 INT32 getAsInt(int nRow, int nCol);
961 UINT32 getAsUInt(int nRow, int nCol);
962 INT64 getAsInt64(int nRow, int nCol);
963 UINT64 getAsUInt64(int nRow, int nCol);
964 double getAsDouble(int nRow, int nCol);
22aaa779 965
d6bbfa4e
VK
966 int getStatus(int nRow, int nCol);
967
22aaa779 968 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
a0ddfb29 969 int findRowByInstance(const TCHAR *instance);
d6bbfa4e
VK
970
971 UINT32 getObjectId(int row) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getObjectId() : 0; }
972 void setObjectId(int row, UINT32 id) { TableRow *r = m_data->get(row); if (r != NULL) r->setObjectId(id); }
a3d3f9d5 973
43666be9
VK
974 void setCellObjectIdAt(int row, int col, UINT32 objectId);
975 void setCellObjectId(int col, UINT32 objectId) { setCellObjectIdAt(getNumRows() - 1, col, objectId); }
976 UINT32 getCellObjectId(int row, int col) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getCellObjectId(col) : 0; }
977
63ff3c9d
VK
978 static Table *createFromXML(const char *xml);
979 TCHAR *createXML();
967893bb
VK
980};
981
25fe6265 982/**
c11eee9b
VK
983 * sockaddr buffer
984 */
985union SockAddrBuffer
986{
987 struct sockaddr_in sa4;
988#ifdef WITH_IPV6
989 struct sockaddr_in6 sa6;
990#endif
991};
992
993/**
994 * sockaddr length calculation
995 */
996#ifdef WITH_IPV6
997#define SA_LEN(sa) (((sa)->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
998#else
999#define SA_LEN(sa) sizeof(struct sockaddr_in)
1000#endif
1001
1002/**
5f6e8d09
VK
1003 * Get port number from SockAddrBuffer
1004 */
1005#ifdef WITH_IPV6
1006#define SA_PORT(sa) ((((struct sockaddr *)sa)->sa_family == AF_INET) ? ((struct sockaddr_in *)sa)->sin_port : ((struct sockaddr_in6 *)sa)->sin6_port)
1007#else
1008#define SA_PORT(sa) (((struct sockaddr_in *)sa)->sin_port)
1009#endif
1010
1011/**
30592fa6
VK
1012 * Compare addresses in sockaddr
1013 */
1014inline bool SocketAddressEquals(struct sockaddr *a1, struct sockaddr *a2)
1015{
1016 if (a1->sa_family != a2->sa_family)
1017 return false;
1018 if (a1->sa_family == AF_INET)
1019 return ((struct sockaddr_in *)a1)->sin_addr.s_addr == ((struct sockaddr_in *)a2)->sin_addr.s_addr;
1020#ifdef WITH_IPV6
1021 if (a1->sa_family == AF_INET6)
1022 return !memcmp(((struct sockaddr_in6 *)a1)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)a2)->sin6_addr.s6_addr, 16);
1023#endif
1024 return false;
1025}
1026
1027/**
9319c166
VK
1028 * IP address
1029 */
1030class LIBNETXMS_EXPORTABLE InetAddress
1031{
9319c166 1032private:
8c75ad41
VK
1033 short m_maskBits;
1034 short m_family;
1035 union
1036 {
1037 UINT32 v4;
1038 BYTE v6[16];
1039 } m_addr;
9319c166
VK
1040
1041public:
8c75ad41
VK
1042 InetAddress();
1043 InetAddress(UINT32 addr);
c75e9ee4 1044 InetAddress(UINT32 addr, UINT32 mask);
4203dcb5 1045 InetAddress(const BYTE *addr, int maskBits = 128);
9319c166 1046
8c75ad41
VK
1047 bool isAnyLocal() const;
1048 bool isLoopback() const;
1049 bool isMulticast() const;
1050 bool isBroadcast() const;
4203dcb5 1051 bool isLinkLocal() const;
8c75ad41 1052 bool isValid() const { return m_family != AF_UNSPEC; }
4203dcb5 1053 bool isValidUnicast() const { return isValid() && !isAnyLocal() && !isLoopback() && !isMulticast() && !isBroadcast() && !isLinkLocal(); }
9319c166 1054
8c75ad41 1055 int getFamily() const { return m_family; }
c7853753
VK
1056 UINT32 getAddressV4() const { return (m_family == AF_INET) ? m_addr.v4 : 0; }
1057 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 1058
8c75ad41 1059 bool contain(const InetAddress &a) const;
43709a0c 1060 bool sameSubnet(const InetAddress &a) const;
8c75ad41
VK
1061 bool equals(const InetAddress &a) const;
1062 int compareTo(const InetAddress &a) const;
9319c166 1063
8c75ad41
VK
1064 void setMaskBits(int m) { m_maskBits = m; }
1065 int getMaskBits() const { return m_maskBits; }
c75e9ee4 1066 int getHostBits() const { return (m_family == AF_INET) ? (32 - m_maskBits) : (128 - m_maskBits); }
9319c166 1067
463b1d34
VK
1068 InetAddress getSubnetAddress() const;
1069 bool isSubnetBroadcast(int maskBits) const;
ffabd7f8 1070
8c75ad41
VK
1071 String toString() const;
1072 TCHAR *toString(TCHAR *buffer) const;
af4a09df
VK
1073#ifdef UNICODE
1074 char *toStringA(char *buffer) const;
1075#else
1076 char *toStringA(char *buffer) const { return toString(buffer); }
1077#endif
9319c166 1078
43709a0c 1079 BYTE *buildHashKey(BYTE *key) const;
f3b94fc5 1080
43709a0c
VK
1081 TCHAR *getHostByAddr(TCHAR *buffer, size_t buflen) const;
1082
1083 struct sockaddr *fillSockAddr(SockAddrBuffer *buffer, UINT16 port = 0) const;
c11eee9b 1084
c7853753
VK
1085 static InetAddress resolveHostName(const WCHAR *hostname, int af = AF_INET);
1086 static InetAddress resolveHostName(const char *hostname, int af = AF_INET);
f3b94fc5
VK
1087 static InetAddress parse(const WCHAR *str);
1088 static InetAddress parse(const char *str);
8c75ad41 1089 static InetAddress createFromSockaddr(struct sockaddr *s);
dcc7a2b0
VK
1090
1091 static const InetAddress INVALID;
48313f37 1092 static const InetAddress LOOPBACK;
dcc7a2b0
VK
1093};
1094
1095/**
1096 * IP address list
1097 */
1098class LIBNETXMS_EXPORTABLE InetAddressList
1099{
1100private:
1101 ObjectArray<InetAddress> *m_list;
1102
1103 int indexOf(const InetAddress& addr) const;
1104
1105public:
1106 InetAddressList();
1107 ~InetAddressList();
1108
1109 void add(const InetAddress& addr);
c30c0c0f
VK
1110 void add(const InetAddressList& addrList);
1111 void replace(const InetAddress& addr);
dcc7a2b0 1112 void remove(const InetAddress& addr);
c30c0c0f 1113 void clear() { m_list->clear(); }
4203dcb5 1114 const InetAddress& get(int index) const { const InetAddress *a = m_list->get(index); return (a != NULL) ? *a : InetAddress::INVALID; }
dcc7a2b0
VK
1115
1116 int size() const { return m_list->size(); }
1117 bool hasAddress(const InetAddress& addr) const { return indexOf(addr) != -1; }
4203dcb5 1118 const InetAddress& findAddress(const InetAddress& addr) const { int idx = indexOf(addr); return (idx != -1) ? *m_list->get(idx) : InetAddress::INVALID; }
c34b611c 1119 const InetAddress& findSameSubnetAddress(const InetAddress& addr) const;
dcc7a2b0 1120 const InetAddress& getFirstUnicastAddress() const;
c34b611c
VK
1121 const InetAddress& getFirstUnicastAddressV4() const;
1122 bool hasValidUnicastAddress() const { return getFirstUnicastAddress().isValid(); }
c30c0c0f 1123 bool isLoopbackOnly() const;
dcc7a2b0
VK
1124
1125 const ObjectArray<InetAddress> *getList() const { return m_list; }
40c8f341
VK
1126
1127 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
9319c166
VK
1128};
1129
1130/**
25fe6265
VK
1131 * Network connection
1132 */
58ee2f43
VK
1133class LIBNETXMS_EXPORTABLE SocketConnection
1134{
1135protected:
1136 SOCKET m_socket;
1137 char m_data[4096];
1138 int m_dataPos;
1139
1140public:
1141 SocketConnection();
1142 virtual ~SocketConnection();
1143
967893bb
VK
1144 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1145 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
58ee2f43
VK
1146 void disconnect();
1147
967893bb
VK
1148 bool canRead(UINT32 timeout);
1149 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43 1150 bool waitForText(const char *text, int timeout);
cc022855 1151
58ee2f43 1152 int write(const char *pBuff, int nSize);
1671f85d
VK
1153 bool writeLine(const char *line);
1154
967893bb 1155 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
1156};
1157
030a0779
VK
1158/**
1159 * Telnet connection - handles all telnet negotiation
1160 */
def5b792
AK
1161class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
1162{
030a0779 1163protected:
967893bb
VK
1164 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1165 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
030a0779 1166
def5b792 1167public:
967893bb 1168 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 1169
967893bb
VK
1170 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
1171 bool connect(UINT32 ip, WORD port, UINT32 timeout);
1172 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
1173 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
1174};
1175
56fa1092
VK
1176/**
1177 * Postal address representation
1178 */
1179class LIBNETXMS_EXPORTABLE PostalAddress
1180{
1181private:
1182 TCHAR *m_country;
1183 TCHAR *m_city;
1184 TCHAR *m_streetAddress;
1185 TCHAR *m_postcode;
1186
1187public:
1188 PostalAddress();
1189 PostalAddress(const TCHAR *country, const TCHAR *city, const TCHAR *streetAddress, const TCHAR *postcode);
1190 ~PostalAddress();
1191
1192 const TCHAR *getCountry() { return CHECK_NULL_EX(m_country); }
1193 const TCHAR *getCity() { return CHECK_NULL_EX(m_city); }
1194 const TCHAR *getStreetAddress() { return CHECK_NULL_EX(m_streetAddress); }
1195 const TCHAR *getPostCode() { return CHECK_NULL_EX(m_postcode); }
1196
1197 void setCountry(const TCHAR *country) { safe_free(m_country); m_country = _tcsdup_ex(country); }
1198 void setCity(const TCHAR *city) { safe_free(m_city); m_city = _tcsdup_ex(city); }
1199 void setStreetAddress(const TCHAR *streetAddress) { safe_free(m_streetAddress); m_streetAddress = _tcsdup_ex(streetAddress); }
1200 void setPostCode(const TCHAR *postcode) { safe_free(m_postcode); m_postcode = _tcsdup_ex(postcode); }
1201};
1202
b160350d
VK
1203#endif /* __cplusplus */
1204
030a0779
VK
1205/**
1206 * Configuration item template for configuration loader
1207 */
4826c8a5
VK
1208typedef struct
1209{
e2e66145
VK
1210 TCHAR token[64];
1211 BYTE type;
1212 BYTE separator; // Separator character for lists
1213 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
e95680e5 1214 UINT64 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
e2e66145
VK
1215 UINT32 bufferPos; // Should be set to 0
1216 void *buffer;
1217 void *overrideIndicator;
4826c8a5 1218} NX_CFG_TEMPLATE;
dcb44c9a 1219
030a0779
VK
1220/**
1221 * Code translation structure
1222 */
1b56f3c5 1223typedef struct __CODE_TO_TEXT
bc858635
VK
1224{
1225 int code;
1226 const TCHAR *text;
1227} CODE_TO_TEXT;
1228
e2e66145
VK
1229/**
1230 * getopt() prototype if needed
1231 */
df9812a9 1232#if USE_BUNDLED_GETOPT
99e50bb7 1233#include <netxms_getopt.h>
3046f9a4
VK
1234#endif
1235
f2fdf1b5 1236//
3d1cb7d6
VK
1237// Structures for opendir() / readdir() / closedir()
1238//
1239
1240#ifdef _WIN32
1241
7f9e0c00 1242typedef struct dirent
3d1cb7d6
VK
1243{
1244 long d_ino; /* inode number (not used by MS-DOS) */
1245 int d_namlen; /* Name length */
1246 char d_name[257]; /* file name */
1247} _DIRECT;
1248
1249typedef struct _dir_struc
1250{
1251 char *start; /* Starting position */
1252 char *curr; /* Current position */
1253 long size; /* Size of string table */
1254 long nfiles; /* number if filenames in table */
7f9e0c00 1255 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
1256} DIR;
1257
5ea2db30
VK
1258#ifdef UNICODE
1259
1260typedef struct dirent_w
1261{
1262 long d_ino; /* inode number (not used by MS-DOS) */
1263 int d_namlen; /* Name length */
1264 WCHAR d_name[257]; /* file name */
1265} _DIRECTW;
1266
1267typedef struct _dir_struc_w
1268{
1269 WCHAR *start; /* Starting position */
1270 WCHAR *curr; /* Current position */
1271 long size; /* Size of string table */
1272 long nfiles; /* number if filenames in table */
1273 struct dirent_w dirstr; /* Directory structure to return */
1274} DIRW;
1275
1276#define _TDIR DIRW
1277#define _TDIRECT _DIRECTW
1278#define _tdirent dirent_w
1279
1280#else
1281
1282#define _TDIR DIR
1283#define _TDIRECT _DIRECT
1284#define _tdirent dirent
1285
37d62364
AK
1286#endif
1287
76b1d3d8
VK
1288#else /* not _WIN32 */
1289
1290typedef struct dirent_w
1291{
1292 long d_ino; /* inode number */
1293 WCHAR d_name[257]; /* file name */
1294} _DIRECTW;
1295
1296typedef struct _dir_struc_w
1297{
1298 DIR *dir; /* Original non-unicode structure */
1299 struct dirent_w dirstr; /* Directory structure to return */
1300} DIRW;
1301
3d1cb7d6
VK
1302#endif /* _WIN32 */
1303
1304
1305//
e9246d26
VK
1306// Functions
1307//
1308
b50f1100 1309#ifdef __cplusplus
18c575eb 1310
2e590d1a
VK
1311inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
1312{
912b994d 1313#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1314 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
912b994d 1315#else
2e590d1a
VK
1316 _tcsncpy(pszDest, pszSrc, nLen - 1);
1317 pszDest[nLen - 1] = 0;
912b994d 1318#endif
2e590d1a
VK
1319 return pszDest;
1320}
f3387429
VK
1321
1322#ifdef UNICODE
2e590d1a
VK
1323inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
1324{
f3387429 1325#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1326 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429 1327#else
2e590d1a
VK
1328 strncpy(pszDest, pszSrc, nLen - 1);
1329 pszDest[nLen - 1] = 0;
c81979bc 1330#endif
2e590d1a
VK
1331 return pszDest;
1332}
c81979bc 1333#else
f3387429 1334#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
1335#endif
1336
967893bb 1337int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
5e60b759
VK
1338int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
1339int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
18c575eb
VK
1340bool LIBNETXMS_EXPORTABLE RecvAll(SOCKET s, void *buffer, size_t size, UINT32 timeout);
1341
b42c9187
VK
1342SOCKET LIBNETXMS_EXPORTABLE ConnectToHost(const InetAddress& addr, UINT16 port, UINT32 timeout);
1343
b0608204 1344#endif /* __cplusplus */
c81979bc
VK
1345
1346#ifdef __cplusplus
e9246d26
VK
1347extern "C"
1348{
b50f1100 1349#endif
735bac5c 1350
076a55c4 1351#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc 1352#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
735bac5c
VK
1353void LIBNETXMS_EXPORTABLE __wcsupr(WCHAR *in);
1354#define wcsupr __wcsupr
cc022855 1355#endif
735bac5c
VK
1356void LIBNETXMS_EXPORTABLE __strupr(char *in);
1357#define strupr __strupr
7e679c4b 1358#endif
cc022855 1359
735bac5c 1360void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
b8a8d064
VK
1361 int (*compare)(const void *, const void *, void *));
1362
735bac5c 1363INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
d35ed0aa 1364
735bac5c
VK
1365UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
1366UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
1367#ifdef UNICODE
1368#define FileSize FileSizeW
1369#else
1370#define FileSize FileSizeA
1371#endif
670c7f3d 1372
735bac5c
VK
1373int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
1374TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 1375#ifdef UNICODE
735bac5c 1376char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
1377#else
1378#define IpToStrA IpToStr
e0f99bf0 1379#endif
8c75ad41 1380TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(const BYTE *addr, TCHAR *buffer);
af4a09df
VK
1381#ifdef UNICODE
1382char LIBNETXMS_EXPORTABLE *Ip6ToStrA(const BYTE *addr, char *buffer);
1383#else
1384#define Ip6ToStrA Ip6ToStr
1385#endif
735bac5c 1386TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
60557d06 1387
735bac5c
VK
1388UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *name);
1389UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameW(const WCHAR *name);
08b214c6 1390#ifdef UNICODE
cfe20cce 1391#define ResolveHostName ResolveHostNameW
08b214c6 1392#else
cfe20cce 1393#define ResolveHostName ResolveHostNameA
08b214c6 1394#endif
a1dc75b0 1395
735bac5c
VK
1396void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
1397void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
31521248 1398
735bac5c
VK
1399WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *data, size_t size, WCHAR *pStr);
1400char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *data, size_t size, char *pStr);
d7970ee7
VK
1401#ifdef UNICODE
1402#define BinToStr BinToStrW
1403#else
1404#define BinToStr BinToStrA
1405#endif
1406
735bac5c
VK
1407size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
1408size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
52db3ed1
VK
1409#ifdef UNICODE
1410#define StrToBin StrToBinW
1411#else
1412#define StrToBin StrToBinA
1413#endif
cc022855 1414
735bac5c 1415TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
b50f1100 1416
735bac5c
VK
1417void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
1418void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
b07c50cc
VK
1419#ifdef UNICODE
1420#define StrStrip StrStripW
1421#else
1422#define StrStrip StrStripA
1423#endif
1424
735bac5c
VK
1425const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
1426const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
1427#ifdef UNICODE
1428#define ExtractWord ExtractWordW
1429#else
1430#define ExtractWord ExtractWordA
1431#endif
1432
735bac5c
VK
1433int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
1434int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
3583a400
VK
1435#ifdef UNICODE
1436#define NumChars NumCharsW
1437#else
1438#define NumChars NumCharsA
1439#endif
ada97259 1440
735bac5c
VK
1441void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
1442void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
35f836fe
VK
1443#ifdef UNICODE
1444#define RemoveTrailingCRLF RemoveTrailingCRLFW
1445#else
1446#define RemoveTrailingCRLF RemoveTrailingCRLFA
1447#endif
1448
4893b4aa
VK
1449BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *str, const char *expr, bool matchCase);
1450BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *str, const WCHAR *expr, bool matchCase);
cafe592d
VK
1451#ifdef UNICODE
1452#define RegexpMatch RegexpMatchW
1453#else
1454#define RegexpMatch RegexpMatchA
1455#endif
1456
735bac5c 1457const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
d77c193c 1458BOOL LIBNETXMS_EXPORTABLE CreateFolder(const TCHAR *directory);
818f8c94 1459TCHAR LIBNETXMS_EXPORTABLE *Trim(TCHAR *str);
735bac5c
VK
1460bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
1461TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
0a145c10 1462int LIBNETXMS_EXPORTABLE GetLastMonthDay(struct tm *currTime);
1463#ifdef __cplusplus
1464bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime = 0);
1465#else
1466bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime);
1467#endif
1468
ada97259 1469
f7794b68 1470#ifdef __cplusplus
735bac5c 1471BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
f7794b68 1472#endif
735bac5c
VK
1473BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
1474/* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1475const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
1476void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
1477BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 1478#ifdef UNICODE
735bac5c 1479BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
1480#else
1481#define LoadFileA LoadFile
1482#endif
1483
735bac5c
VK
1484UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
1485void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
1486void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
1487void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
1488void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
1718cc2a 1489void LIBNETXMS_EXPORTABLE CalculateSHA256Hash(const unsigned char *data, size_t len, unsigned char *hash);
735bac5c
VK
1490BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
1491BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
1492BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 1493
981d246a
VK
1494void LIBNETXMS_EXPORTABLE GenerateRandomBytes(BYTE *buffer, size_t size);
1495
735bac5c
VK
1496void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1497void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
4e2debcc 1498
f2436b76 1499bool LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd, size_t bufferLenght);
6b1282e7 1500
735bac5c 1501int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 1502
735bac5c
VK
1503HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
1504void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
1505void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 1506
735bac5c
VK
1507bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1508bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1509long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
289d126b 1510
735bac5c
VK
1511bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1512bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1513long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
289d126b
VK
1514
1515#ifdef UNICODE
1516#define ExtractNamedOptionValue ExtractNamedOptionValueW
1517#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1518#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1519#else
1520#define ExtractNamedOptionValue ExtractNamedOptionValueA
1521#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1522#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1523#endif
ae9cddaf 1524
bc858635 1525#ifdef __cplusplus
735bac5c 1526const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
bc858635 1527#else
735bac5c 1528const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
bc858635
VK
1529#endif
1530
aa13cfd9 1531#ifdef _WIN32
735bac5c
VK
1532TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
1533BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
1534INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 1535#endif
696fc54f 1536
de4c0511
VK
1537#if !HAVE_DAEMON || !HAVE_DECL_DAEMON
1538int LIBNETXMS_EXPORTABLE __daemon(int nochdir, int noclose);
1539#define daemon __daemon
696fc54f 1540#endif
4332e9d8 1541
735bac5c 1542UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 1543
4332e9d8 1544#ifndef _WIN32
735bac5c
VK
1545BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
1546int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
1547 int cchWideChar, char *pByteStr, int cchByteChar,
1548 char *pDefaultChar, BOOL *pbUsedDefChar);
1549int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
1550 int cchByteChar, WCHAR *pWideCharStr,
1551 int cchWideChar);
04dbaf62 1552
6b93c954 1553#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
735bac5c 1554int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
1555#endif
1556#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
735bac5c 1557UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
1558#endif
1559#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
b6ec31bb 1560UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strdup(const UCS2CHAR *pStr);
4332e9d8 1561#endif
8b86c5dc 1562
735bac5c
VK
1563size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1564size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
1565UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1566char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 1567
735bac5c
VK
1568int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1569int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1570int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1571int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1572int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1573int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 1574
735bac5c
VK
1575int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1576int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
1577int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
1578int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1579int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
1580int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1581
3e39fcde 1582#endif /* _WIN32 */
8b86c5dc 1583
3d1058ed 1584#ifdef _WITH_ENCRYPTION
735bac5c 1585WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
3d1058ed
VK
1586#endif
1587
8b86c5dc 1588#ifdef UNICODE_UCS4
735bac5c
VK
1589size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1590size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1591size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1592UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1593WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1594#endif
8b86c5dc 1595
ebe98224
VK
1596size_t LIBNETXMS_EXPORTABLE utf8_to_mb(const char *src, int srcLen, char *dst, int dstLen);
1597size_t LIBNETXMS_EXPORTABLE mb_to_utf8(const char *src, int srcLen, char *dst, int dstLen);
1598char LIBNETXMS_EXPORTABLE *MBStringFromUTF8String(const char *s);
1599char LIBNETXMS_EXPORTABLE *UTF8StringFromMBString(const char *s);
1600
842c22f4 1601#if !defined(_WIN32) && !HAVE_WSTAT
735bac5c 1602int wstat(const WCHAR *_path, struct stat *_sbuf);
842c22f4
AK
1603#endif
1604
ef856d02 1605#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1606
f3387429 1607#if !HAVE_WPOPEN
735bac5c 1608FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
f3387429 1609#endif
8b86c5dc 1610#if !HAVE_WFOPEN
735bac5c 1611FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
8b86c5dc 1612#endif
8c2489aa 1613#if HAVE_FOPEN64 && !HAVE_WFOPEN64
735bac5c 1614FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
8c2489aa 1615#endif
8b86c5dc 1616#if !HAVE_WOPEN
735bac5c 1617int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1618#endif
f3387429 1619#if !HAVE_WCHMOD
735bac5c 1620int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
f3387429 1621#endif
b07c50cc 1622#if !HAVE_WCHDIR
735bac5c 1623int wchdir(const WCHAR *_path);
b07c50cc
VK
1624#endif
1625#if !HAVE_WMKDIR
735bac5c 1626int wmkdir(const WCHAR *_path, int mode);
b07c50cc
VK
1627#endif
1628#if !HAVE_WRMDIR
735bac5c 1629int wrmdir(const WCHAR *_path);
b07c50cc 1630#endif
ccc34207 1631#if !HAVE_WRENAME
735bac5c 1632int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
ccc34207
VK
1633#endif
1634#if !HAVE_WUNLINK
735bac5c 1635int wunlink(const WCHAR *_path);
ccc34207 1636#endif
b07c50cc 1637#if !HAVE_WREMOVE
735bac5c 1638int wremove(const WCHAR *_path);
b07c50cc
VK
1639#endif
1640#if !HAVE_WSYSTEM
735bac5c 1641int wsystem(const WCHAR *_cmd);
b07c50cc 1642#endif
dda7c270 1643#if !HAVE_WMKSTEMP
735bac5c 1644int wmkstemp(WCHAR *_template);
dda7c270 1645#endif
b07c50cc 1646#if !HAVE_WACCESS
735bac5c 1647int waccess(const WCHAR *_path, int mode);
b07c50cc 1648#endif
273da39a 1649#if !HAVE_WGETENV
735bac5c 1650WCHAR *wgetenv(const WCHAR *_string);
273da39a 1651#endif
f3387429 1652#if !HAVE_WCTIME
735bac5c 1653WCHAR *wctime(const time_t *timep);
f3387429 1654#endif
0186b5da 1655#if !HAVE_PUTWS
735bac5c 1656int putws(const WCHAR *s);
0186b5da 1657#endif
9757ca46 1658#if !HAVE_WCSERROR && (HAVE_STRERROR || HAVE_DECL_STRERROR)
735bac5c 1659WCHAR *wcserror(int errnum);
d49d7500
VK
1660#endif
1661#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06 1662#if HAVE_POSIX_STRERROR_R
735bac5c 1663int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
b0823d06 1664#else
735bac5c 1665WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
d49d7500 1666#endif
b0823d06 1667#endif
e05f8aaf 1668
ef856d02 1669#endif /* UNICODE && !_WIN32*/
e05f8aaf 1670
8b86c5dc 1671#if !HAVE_STRTOLL
735bac5c 1672INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
8b86c5dc
VK
1673#endif
1674#if !HAVE_STRTOULL
735bac5c 1675UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1676#endif
e05f8aaf
VK
1677
1678#if !HAVE_WCSTOLL
735bac5c 1679INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf
VK
1680#endif
1681#if !HAVE_WCSTOULL
735bac5c 1682UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1683#endif
b50f1100 1684
2f56be29 1685#if !HAVE_WCSLWR && !defined(_WIN32)
735bac5c 1686WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
fdf1ed50
VK
1687#endif
1688
4d978b4a 1689#if !HAVE_WCSCASECMP && !defined(_WIN32)
735bac5c 1690int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
4d978b4a
VK
1691#endif
1692
f4d2c9ba 1693#if !HAVE_WCSNCASECMP && !defined(_WIN32)
735bac5c 1694int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
f4d2c9ba
VK
1695#endif
1696
9138dfe2
VK
1697#ifndef _WIN32
1698
1699#if HAVE_ITOA && !HAVE__ITOA
1700#define _itoa itoa
1701#undef HAVE__ITOA
1702#define HAVE__ITOA 1
1703#endif
1704#if !HAVE__ITOA && !defined(_WIN32)
1705char LIBNETXMS_EXPORTABLE *_itoa(int value, char *str, int base);
1706#endif
1707
1708#if HAVE_ITOW && !HAVE__ITOW
1709#define _itow itow
1710#undef HAVE__ITOW
1711#define HAVE__ITOW 1
1712#endif
1713#if !HAVE__ITOW && !defined(_WIN32)
1714WCHAR LIBNETXMS_EXPORTABLE *_itow(int value, WCHAR *str, int base);
1715#endif
1716
1717#endif /* _WIN32 */
1718
3d1cb7d6 1719#ifdef _WIN32
5ea2db30 1720#ifdef UNICODE
735bac5c
VK
1721DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1722struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1723int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
5ea2db30
VK
1724
1725#define _topendir wopendir
1726#define _treaddir wreaddir
1727#define _tclosedir wclosedir
1728#else
1729#define _topendir opendir
1730#define _treaddir readdir
1731#define _tclosedir closedir
1732#endif
1733
735bac5c
VK
1734DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
1735struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
1736int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1737
76b1d3d8
VK
1738#else /* not _WIN32 */
1739
735bac5c
VK
1740DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1741struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1742int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
76b1d3d8 1743
37d62364 1744#endif
3d1cb7d6 1745
7f9e0c00 1746#if defined(_WIN32) || !(HAVE_SCANDIR)
735bac5c
VK
1747int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1748 int (*select)(const struct dirent *),
1749 int (*compar)(const struct dirent **, const struct dirent **));
1750int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
7f9e0c00
VK
1751#endif
1752
3bf0df9f
VK
1753TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
1754
967893bb 1755BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
d49d7500 1756 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1757void LIBNETXMS_EXPORTABLE nxlog_close(void);
1758void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1759BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1760BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
9113e749
VK
1761
1762typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1763void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1764
1765void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
6e383343
VK
1766void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
1767#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
1768 __attribute__ ((format(printf, 1, 2)))
1769#endif
1770;
9f2902ac 1771
37a7a862
VK
1772#ifdef _WIN32
1773int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1774int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1775#ifdef UNICODE
1776#define _tmkstemp wmkstemp
1777#else
1778#define _tmkstemp mkstemp
1779#endif
1780#endif
1781
9b13553b
VK
1782#ifndef _WIN32
1783int strcat_s(char *dst, size_t dstSize, const char *src);
1784int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1785#endif
1786
c57d020a
VK
1787#if !HAVE_STRPTIME
1788char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
1789#endif
1790
1791#if !HAVE_TIMEGM
1792time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
1793#endif
1794
6f95ef89
VK
1795#if !HAVE_INET_PTON
1796int LIBNETXMS_EXPORTABLE nx_inet_pton(int af, const char *src, void *dst);
1797#define inet_pton nx_inet_pton
1798#endif
1799
b06ae508 1800int LIBNETXMS_EXPORTABLE GetSleepTime(int hour, int minute, int second);
b60584cf
VK
1801time_t LIBNETXMS_EXPORTABLE ParseDateTimeA(const char *text, time_t defaultValue);
1802time_t LIBNETXMS_EXPORTABLE ParseDateTimeW(const WCHAR *text, time_t defaultValue);
1803
1804#ifdef UNICODE
1805#define ParseDateTime ParseDateTimeW
1806#else
1807#define ParseDateTime ParseDateTimeA
1808#endif
b06ae508 1809
b50f1100 1810#ifdef __cplusplus
e9246d26 1811}
b50f1100 1812#endif
e9246d26 1813
a7ff20a5
VK
1814
1815//
b06ae508 1816// C++ only functions
a7ff20a5
VK
1817//
1818
1819#ifdef __cplusplus
1820
a97f02e1
VK
1821enum nxDirectoryType
1822{
1823 nxDirBin = 0,
1824 nxDirData = 1,
1825 nxDirEtc = 2,
1039d7ee
VK
1826 nxDirLib = 3,
1827 nxDirShare = 4
a97f02e1
VK
1828};
1829
5962498b
VK
1830TCHAR LIBNETXMS_EXPORTABLE *GetHeapInfo();
1831
a97f02e1
VK
1832void LIBNETXMS_EXPORTABLE GetNetXMSDirectory(nxDirectoryType type, TCHAR *dir);
1833
43709a0c
VK
1834UINT32 LIBNETXMS_EXPORTABLE IcmpPing(const InetAddress& addr, int iNumRetries, UINT32 dwTimeout, UINT32 *pdwRTT, UINT32 dwPacketSize);
1835
240816b0
VK
1836TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1837String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1838const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1839int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1840UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1841bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1842
1843#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 1844void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
1845#endif
1846
a7ff20a5
VK
1847#endif
1848
e9246d26 1849#endif /* _nms_util_h_ */