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