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