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