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