aa74c2c576c9fd35542e3f8470182f765f62b69a
[public/netxms.git] / include / nms_util.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2016 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
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
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 **
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: nms_util.h
20 **
21 **/
22
23 #ifndef _nms_util_h_
24 #define _nms_util_h_
25
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 #include <nms_common.h>
37 #include <nms_cscp.h>
38 #include <nms_threads.h>
39 #include <time.h>
40
41 #if HAVE_POLL_H
42 #include <poll.h>
43 #endif
44
45 #if HAVE_BYTESWAP_H
46 #include <byteswap.h>
47 #endif
48
49 #include <base64.h>
50
51 /*** Byte swapping ***/
52 #if WORDS_BIGENDIAN
53 #define htonq(x) (x)
54 #define ntohq(x) (x)
55 #define htond(x) (x)
56 #define ntohd(x) (x)
57 #define SwapUCS2String(x)
58 #else
59 #if HAVE_HTONLL
60 #define htonq(x) htonll(x)
61 #else
62 #define htonq(x) bswap_64(x)
63 #endif
64 #if HAVE_NTOHLL
65 #define ntohq(x) ntohll(x)
66 #else
67 #define ntohq(x) bswap_64(x)
68 #endif
69 #define htond(x) bswap_double(x)
70 #define ntohd(x) bswap_double(x)
71 #define SwapUCS2String(x) bswap_array_16((UINT16 *)(x), -1)
72 #endif
73
74 #ifdef __cplusplus
75
76 #if !(HAVE_DECL_BSWAP_16)
77 inline UINT16 bswap_16(UINT16 val)
78 {
79 return (val >> 8) | ((val << 8) & 0xFF00);
80 }
81 #endif
82
83 #if !(HAVE_DECL_BSWAP_32)
84 inline UINT32 bswap_32(UINT32 val)
85 {
86 UINT32 result;
87 BYTE *sptr = (BYTE *)&val;
88 BYTE *dptr = (BYTE *)&result + 3;
89 for(int i = 0; i < 4; i++, sptr++, dptr--)
90 *dptr = *sptr;
91 return result;
92 }
93 #endif
94
95 #if !(HAVE_DECL_BSWAP_64)
96 inline UINT64 bswap_64(UINT64 val)
97 {
98 UINT64 result;
99 BYTE *sptr = (BYTE *)&val;
100 BYTE *dptr = (BYTE *)&result + 7;
101 for(int i = 0; i < 8; i++, sptr++, dptr--)
102 *dptr = *sptr;
103 return result;
104 }
105 #endif
106
107 inline double bswap_double(double val)
108 {
109 double result;
110 BYTE *sptr = (BYTE *)&val;
111 BYTE *dptr = (BYTE *)&result + 7;
112 for(int i = 0; i < 8; i++, sptr++, dptr--)
113 *dptr = *sptr;
114 return result;
115 }
116
117 #else
118
119 #if !(HAVE_DECL_BSWAP_16)
120 UINT16 LIBNETXMS_EXPORTABLE __bswap_16(UINT16 val);
121 #define bswap_16 __bswap_16
122 #endif
123
124 #if !(HAVE_DECL_BSWAP_32)
125 UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 val);
126 #define bswap_32 __bswap_32
127 #endif
128
129 #if !(HAVE_DECL_BSWAP_64)
130 UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 val);
131 #define bswap_64 __bswap_64
132 #endif
133
134 #endif
135
136 #ifdef __cplusplus
137 extern "C" {
138 #endif
139 void LIBNETXMS_EXPORTABLE bswap_array_16(UINT16 *v, int len);
140 void LIBNETXMS_EXPORTABLE bswap_array_32(UINT32 *v, int len);
141 #ifdef __cplusplus
142 }
143 #endif
144
145
146 /*** Serial communications ***/
147 #ifdef _WIN32
148
149 #define FLOW_NONE 0
150 #define FLOW_SOFTWARE 1
151 #define FLOW_HARDWARE 2
152
153 #else /* _WIN32 */
154
155 #ifdef HAVE_TERMIOS_H
156 # include <termios.h>
157 #else
158 # error termios.h not found
159 #endif
160
161 #endif /* _WIN32 */
162
163 /**
164 * Return codes for IcmpPing()
165 */
166 #define ICMP_SUCCESS 0
167 #define ICMP_UNREACHEABLE 1
168 #define ICMP_TIMEOUT 2
169 #define ICMP_RAW_SOCK_FAILED 3
170 #define ICMP_API_ERROR 4
171
172 /**
173 * Token types for configuration loader
174 */
175 #define CT_LONG 0
176 #define CT_STRING 1
177 #define CT_STRING_LIST 2
178 #define CT_END_OF_LIST 3
179 #define CT_BOOLEAN 4
180 #define CT_WORD 5
181 #define CT_IGNORE 6
182 #define CT_MB_STRING 7
183 #define CT_BOOLEAN64 8
184 #define CT_SIZE_BYTES 9 /* 64 bit integer, automatically converts K, M, G, T suffixes using 1024 as base) */
185 #define CT_SIZE_UNITS 10 /* 64 bit integer, automatically converts K, M, G, T suffixes using 1000 as base) */
186
187 /**
188 * Uninitialized value for override indicator
189 */
190 #define NXCONFIG_UNINITIALIZED_VALUE (-1)
191
192 /**
193 * Return codes for NxLoadConfig()
194 */
195 #define NXCFG_ERR_OK 0
196 #define NXCFG_ERR_NOFILE 1
197 #define NXCFG_ERR_SYNTAX 2
198
199 /**
200 * nxlog_open() flags
201 */
202 #define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
203 #define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
204 #define NXLOG_BACKGROUND_WRITER ((UINT32)0x00000004)
205 #define NXLOG_DEBUG_MODE ((UINT32)0x00000008)
206 #define NXLOG_IS_OPEN ((UINT32)0x80000000)
207
208 /**
209 * nxlog rotation policy
210 */
211 #define NXLOG_ROTATION_DISABLED 0
212 #define NXLOG_ROTATION_DAILY 1
213 #define NXLOG_ROTATION_BY_SIZE 2
214
215 /**
216 * Custom wcslen()
217 */
218 #if !HAVE_WCSLEN
219
220 #ifdef __cplusplus
221 extern "C" {
222 #endif
223 size_t LIBNETXMS_EXPORTABLE wcslen(const WCHAR *s);
224 #ifdef __cplusplus
225 }
226 #endif
227
228 #endif
229
230 /**
231 * Custom wcsncpy()
232 */
233 #if !HAVE_WCSNCPY
234
235 #ifdef __cplusplus
236 extern "C" {
237 #endif
238 WCHAR LIBNETXMS_EXPORTABLE *wcsncpy(WCHAR *dest, const WCHAR *src, size_t n);
239 #ifdef __cplusplus
240 }
241 #endif
242
243 #endif
244
245 /**
246 * _tcsdup() replacement
247 */
248 #if defined(_WIN32) && defined(USE_WIN32_HEAP)
249 #ifdef __cplusplus
250 extern "C" {
251 #endif
252 char LIBNETXMS_EXPORTABLE *nx_strdup(const char *src);
253 WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
254 #ifdef __cplusplus
255 }
256 #endif
257 #endif
258
259 /**
260 * Custom wcsdup
261 */
262 #if !HAVE_WCSDUP
263
264 #ifdef __cplusplus
265 extern "C" {
266 #endif
267 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
268 #ifdef __cplusplus
269 }
270 #endif
271
272 #elif defined(_AIX)
273
274 // Some AIX versions have broken wcsdup() so we use internal implementation
275 #ifdef __cplusplus
276 extern "C" {
277 #endif
278 WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
279 #ifdef __cplusplus
280 }
281 #endif
282
283 #define wcsdup nx_wcsdup
284
285 #endif
286
287 #ifdef __cplusplus
288
289 /**
290 * Extended tcsdup: returns NULL if NULL pointer passed
291 */
292 inline TCHAR *_tcsdup_ex(const TCHAR *s)
293 {
294 return (s != NULL) ? _tcsdup(s) : NULL;
295 }
296
297 #endif
298
299 /******* UNICODE related conversion and helper functions *******/
300
301 #ifdef __cplusplus
302 extern "C" {
303 #endif
304
305 // Basic string functions
306 #ifdef UNICODE_UCS2
307
308 #define ucs2_strlen wcslen
309 #define ucs2_strncpy wcsncpy
310 #define ucs2_strdup wcsdup
311
312 size_t LIBNETXMS_EXPORTABLE ucs4_strlen(const UCS4CHAR *s);
313 UCS4CHAR LIBNETXMS_EXPORTABLE *ucs4_strncpy(UCS4CHAR *dest, const UCS2CHAR *src, size_t n);
314 UCS4CHAR LIBNETXMS_EXPORTABLE *ucs4_strdup(const UCS4CHAR *src);
315
316 #else
317
318 size_t LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *s);
319 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *dest, const UCS2CHAR *src, size_t n);
320 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strdup(const UCS2CHAR *src);
321
322 #define ucs4_strlen wcslen
323 #define ucs4_strncpy wcsncpy
324 #define ucs4_strdup wcsdup
325
326 #endif
327
328 // Character conversion functions
329 int LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, UCS4CHAR *dst, int dstLen);
330 #if defined(_WIN32) || defined(UNICODE_UCS2)
331 #define ucs2_to_utf8(wstr, wlen, mstr, mlen) WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, mstr, mlen, NULL, NULL)
332 #define ucs2_to_mb(wstr, wlen, mstr, mlen) WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wstr, wlen, mstr, mlen, NULL, NULL)
333 #else
334 int LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
335 int LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
336 #endif
337
338 int LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const UCS4CHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
339 #ifdef UNICODE_UCS4
340 #define ucs4_to_utf8(wstr, wlen, mstr, mlen) WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, mstr, mlen, NULL, NULL)
341 #define ucs4_to_mb(wstr, wlen, mstr, mlen) WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wstr, wlen, mstr, mlen, NULL, NULL)
342 #else
343 int LIBNETXMS_EXPORTABLE ucs4_to_utf8(const UCS4CHAR *src, int srcLen, char *dst, int dstLen);
344 int LIBNETXMS_EXPORTABLE ucs4_to_mb(const UCS4CHAR *src, int srcLen, char *dst, int dstLen);
345 #endif
346
347 #ifdef UNICODE_UCS4
348 #define utf8_to_ucs4(mstr, mlen, wstr, wlen) MultiByteToWideChar(CP_UTF8, 0, mstr, mlen, wstr, wlen)
349 #else
350 int LIBNETXMS_EXPORTABLE utf8_to_ucs4(const char *src, int srcLen, UCS4CHAR *dst, int dstLen);
351 #endif
352 #if defined(_WIN32) || defined(UNICODE_UCS2)
353 #define utf8_to_ucs2(mstr, mlen, wstr, wlen) MultiByteToWideChar(CP_UTF8, 0, mstr, mlen, wstr, wlen)
354 #else
355 int LIBNETXMS_EXPORTABLE utf8_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
356 #endif
357 int LIBNETXMS_EXPORTABLE utf8_to_mb(const char *src, int srcLen, char *dst, int dstLen);
358
359 #ifdef UNICODE_UCS4
360 #define mb_to_ucs4(mstr, mlen, wstr, wlen) MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mstr, mlen, wstr, wlen)
361 #else
362 int LIBNETXMS_EXPORTABLE mb_to_ucs4(const char *src, int srcLen, UCS4CHAR *dst, int dstLen);
363 #endif
364 #if defined(_WIN32) || defined(UNICODE_UCS2)
365 #define mb_to_ucs2(mstr, mlen, wstr, wlen) MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mstr, mlen, wstr, wlen)
366 #else
367 int LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
368 #endif
369 int LIBNETXMS_EXPORTABLE mb_to_utf8(const char *src, int srcLen, char *dst, int dstLen);
370
371 // Conversion helpers
372 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *src);
373 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBStringSysLocale(const char *src);
374 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *src);
375 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *src);
376 char LIBNETXMS_EXPORTABLE *MBStringFromWideStringSysLocale(const WCHAR *src);
377 char LIBNETXMS_EXPORTABLE *MBStringFromUTF8String(const char *src);
378 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *src);
379 char LIBNETXMS_EXPORTABLE *UTF8StringFromMBString(const char *src);
380 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const UCS4CHAR *src);
381 UCS4CHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *src);
382
383 #ifdef UNICODE_UCS2
384 #define UCS2StringFromMBString WideStringFromMBString
385 #define MBStringFromUCS2String MBStringFromWideString
386 #define UCS2StringFromUTF8String WideStringFromUTF8String
387 #define UTF8StringFromUCS2String UTF8StringFromWideString
388 #else
389 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *src);
390 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *src);
391 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUTF8String(const char *src);
392 char LIBNETXMS_EXPORTABLE *UTF8StringFromUCS2String(const UCS2CHAR *src);
393 #endif
394
395 #ifdef UNICODE_UCS4
396 #define UCS4StringFromMBString WideStringFromMBString
397 #define MBStringFromUCS4String MBStringFromWideString
398 #else
399 UCS4CHAR LIBNETXMS_EXPORTABLE *UCS4StringFromMBString(const char *src);
400 char LIBNETXMS_EXPORTABLE *MBStringFromUCS4String(const UCS4CHAR *src);
401 #endif
402
403 #ifdef UNICODE
404 #define UTF8StringFromTString(s) UTF8StringFromWideString(s)
405 #else
406 #define UTF8StringFromTString(s) UTF8StringFromMBString(s)
407 #endif
408
409 #ifdef __cplusplus
410 }
411 #endif
412
413 /******* end of UNICODE related conversion and helper functions *******/
414
415 /**
416 * Class for serial communications
417 */
418 #ifdef __cplusplus
419
420 #ifndef _WIN32
421 enum
422 {
423 NOPARITY,
424 ODDPARITY,
425 EVENPARITY,
426 ONESTOPBIT,
427 TWOSTOPBITS
428 };
429
430 enum
431 {
432 FLOW_NONE,
433 FLOW_HARDWARE,
434 FLOW_SOFTWARE
435 };
436
437 #ifndef INVALID_HANDLE_VALUE
438 #define INVALID_HANDLE_VALUE (-1)
439 #endif
440 #endif /* _WIN32 */
441
442 class LIBNETXMS_EXPORTABLE Serial
443 {
444 private:
445 TCHAR *m_pszPort;
446 int m_nTimeout;
447 int m_nSpeed;
448 int m_nDataBits;
449 int m_nStopBits;
450 int m_nParity;
451 int m_nFlowControl;
452 int m_writeBlockSize;
453 int m_writeDelay;
454
455 #ifndef _WIN32
456 int m_hPort;
457 struct termios m_originalSettings;
458 #else
459 HANDLE m_hPort;
460 #endif
461
462 bool writeBlock(const char *data, int length);
463
464 public:
465 Serial();
466 ~Serial();
467
468 bool open(const TCHAR *pszPort);
469 void close();
470 void setTimeout(int nTimeout);
471 int read(char *pBuff, int nSize); /* waits up to timeout and do single read */
472 int readAll(char *pBuff, int nSize); /* read until timeout or out of space */
473 int readToMark(char *buff, int size, const char **marks, char **occurence);
474 bool write(const char *buffer, int length);
475 void flush();
476 bool set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl = FLOW_NONE);
477 void setWriteBlockSize(int bs) { m_writeBlockSize = bs; }
478 void setWriteDelay(int delay) { m_writeDelay = delay; }
479 bool restart();
480 };
481
482 /**
483 * Dynamic string class
484 */
485 class LIBNETXMS_EXPORTABLE String
486 {
487 protected:
488 TCHAR *m_buffer;
489 size_t m_length;
490 size_t m_allocated;
491 size_t m_allocationStep;
492
493 public:
494 static const int npos;
495
496 String();
497 String(const TCHAR *init);
498 String(const String &src);
499 ~String();
500
501 TCHAR *getBuffer() { return m_buffer; }
502 void setBuffer(TCHAR *buffer);
503
504 size_t getAllocationStep() { return m_allocationStep; }
505 void setAllocationStep(size_t step) { m_allocationStep = step; }
506
507 const String& operator =(const TCHAR *str);
508 const String& operator =(const String &src);
509 const String& operator +=(const TCHAR *str);
510 const String& operator +=(const String &str);
511 operator const TCHAR*() const { return CHECK_NULL_EX(m_buffer); }
512
513 char *getUTF8String();
514
515 void append(const TCHAR *str) { if (str != NULL) append(str, _tcslen(str)); }
516 void append(const TCHAR *str, size_t len);
517 void append(const TCHAR c) { append(&c, 1); }
518 void append(INT32 n);
519 void append(UINT32 n);
520 void append(INT64 n);
521 void append(UINT64 n);
522
523 void appendPreallocated(TCHAR *str) { if (str != NULL) { append(str); free(str); } }
524
525 void appendMBString(const char *str, size_t len, int nCodePage);
526 void appendWideString(const WCHAR *str, size_t len);
527
528 void appendFormattedString(const TCHAR *format, ...);
529 void appendFormattedStringV(const TCHAR *format, va_list args);
530
531 void clear();
532
533 size_t length() const { return m_length; }
534 bool isEmpty() const { return m_length == 0; }
535
536 wchar_t charAt(size_t pos) const { return (pos < m_length) ? m_buffer[pos] : 0; }
537
538 TCHAR *substring(int nStart, int nLen, TCHAR *pszBuffer = NULL);
539 int find(const TCHAR *str, int nStart = 0);
540
541 void escapeCharacter(int ch, int esc);
542 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
543 void trim();
544 void shrink(int chars = 1);
545 };
546
547 /**
548 * Abstract iterator class (to be implemented by actual iterable class)
549 */
550 class LIBNETXMS_EXPORTABLE AbstractIterator
551 {
552 public:
553 virtual ~AbstractIterator();
554
555 virtual bool hasNext() = 0;
556 virtual void *next() = 0;
557 virtual void remove() = 0;
558 };
559
560 /**
561 * Iterator class (public interface for iteration)
562 */
563 template <class T> class Iterator
564 {
565 private:
566 AbstractIterator *m_worker;
567
568 public:
569 Iterator(AbstractIterator *worker) { m_worker = worker; }
570 ~Iterator() { delete m_worker; }
571
572 bool hasNext() { return m_worker->hasNext(); }
573 T *next() { return (T *)m_worker->next(); }
574 void remove() { m_worker->remove(); }
575 };
576
577 /**
578 * Dynamic array class
579 */
580 class LIBNETXMS_EXPORTABLE Array
581 {
582 private:
583 int m_size;
584 int m_allocated;
585 int m_grow;
586 size_t m_elementSize;
587 void **m_data;
588 bool m_objectOwner;
589
590 void internalRemove(int index, bool allowDestruction);
591 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
592
593 protected:
594 bool m_storePointers;
595 void (*m_objectDestructor)(void *);
596
597 Array(void *data, int initial, int grow, size_t elementSize);
598 Array(const Array *src);
599
600 void *__getBuffer() const { return m_data; }
601
602 public:
603 Array(int initial = 0, int grow = 16, bool owner = false);
604 virtual ~Array();
605
606 int add(void *element);
607 void *get(int index) const { return ((index >= 0) && (index < m_size)) ? (m_storePointers ? m_data[index] : (void *)((char *)m_data + index * m_elementSize)): NULL; }
608 int indexOf(void *element) const;
609 void set(int index, void *element);
610 void replace(int index, void *element);
611 void remove(int index) { internalRemove(index, true); }
612 void remove(void *element) { internalRemove(indexOf(element), true); }
613 void unlink(int index) { internalRemove(index, false); }
614 void unlink(void *element) { internalRemove(indexOf(element), false); }
615 void clear();
616 void sort(int (*cb)(const void *, const void *));
617 void *find(const void *key, int (*cb)(const void *, const void *)) const;
618
619 int size() const { return m_size; }
620
621 void setOwner(bool owner) { m_objectOwner = owner; }
622 bool isOwner() const { return m_objectOwner; }
623 };
624
625 /**
626 * Array iterator class
627 */
628 class LIBNETXMS_EXPORTABLE ArrayIterator : public AbstractIterator
629 {
630 private:
631 Array *m_array;
632 int m_pos;
633
634 public:
635 ArrayIterator(Array *array);
636
637 virtual bool hasNext();
638 virtual void *next();
639 virtual void remove();
640 };
641
642 /**
643 * Template class for dynamic array which holds objects
644 */
645 template <class T> class ObjectArray : public Array
646 {
647 private:
648 static void destructor(void *object) { delete (T*)object; }
649
650 public:
651 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
652 virtual ~ObjectArray() { }
653
654 int add(T *object) { return Array::add((void *)object); }
655 T *get(int index) const { return (T*)Array::get(index); }
656 int indexOf(T *object) const { return Array::indexOf((void *)object); }
657 bool contains(T *object) const { return indexOf(object) >= 0; }
658 void set(int index, T *object) { Array::set(index, (void *)object); }
659 void replace(int index, T *object) { Array::replace(index, (void *)object); }
660 void remove(int index) { Array::remove(index); }
661 void remove(T *object) { Array::remove((void *)object); }
662 void unlink(int index) { Array::unlink(index); }
663 void unlink(T *object) { Array::unlink((void *)object); }
664
665 Iterator<T> *iterator() { return new Iterator<T>(new ArrayIterator(this)); }
666 };
667
668 /**
669 * Template class for dynamic array which holds scalar values
670 */
671 template <class T> class IntegerArray : public Array
672 {
673 private:
674 static void destructor(void *element) { }
675
676 public:
677 IntegerArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; m_storePointers = (sizeof(T) == sizeof(void *)); }
678 IntegerArray(const IntegerArray<T> *src) : Array(src) { }
679 virtual ~IntegerArray() { }
680
681 int add(T value) { return Array::add(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
682 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; }
683 int indexOf(T value) const { return Array::indexOf(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
684 bool contains(T value) const { return indexOf(value) >= 0; }
685 void set(int index, T value) { Array::set(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
686 void replace(int index, T value) { Array::replace(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
687
688 T *getBuffer() const { return (T*)__getBuffer(); }
689 };
690
691 /**
692 * Auxilliary class to hold dynamically allocated array of structures
693 */
694 template <class T> class StructArray : public Array
695 {
696 private:
697 static void destructor(void *element) { }
698
699 public:
700 StructArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; }
701 StructArray(T *data, int size) : Array(data, size, 16, sizeof(T)) { m_objectDestructor = destructor; }
702 StructArray(const StructArray<T> *src) : Array(src) { }
703 virtual ~StructArray() { }
704
705 int add(T *element) { return Array::add((void *)element); }
706 T *get(int index) const { return (T*)Array::get(index); }
707 int indexOf(T *element) const { return Array::indexOf((void *)element); }
708 bool contains(T *element) const { return indexOf(element) >= 0; }
709 void set(int index, T *element) { Array::set(index, (void *)element); }
710 void replace(int index, T *element) { Array::replace(index, (void *)element); }
711 void remove(int index) { Array::remove(index); }
712 void remove(T *element) { Array::remove((void *)element); }
713 void unlink(int index) { Array::unlink(index); }
714 void unlink(T *element) { Array::unlink((void *)element); }
715
716 T *getBuffer() const { return (T*)__getBuffer(); }
717 };
718
719 /**
720 * Entry of string map (internal)
721 */
722 struct StringMapEntry;
723
724 /**
725 * Key/value pair
726 */
727 struct KeyValuePair
728 {
729 const TCHAR *key;
730 const void *value;
731 };
732
733 /**
734 * String maps base class
735 */
736 class LIBNETXMS_EXPORTABLE StringMapBase
737 {
738 protected:
739 StringMapEntry *m_data;
740 bool m_objectOwner;
741 bool m_ignoreCase;
742 void (*m_objectDestructor)(void *);
743
744 StringMapEntry *find(const TCHAR *key) const;
745 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
746 void *getObject(const TCHAR *key) const;
747 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
748
749 public:
750 StringMapBase(bool objectOwner);
751 virtual ~StringMapBase();
752
753 void setOwner(bool owner) { m_objectOwner = owner; }
754 void setIgnoreCase(bool ignore);
755
756 void remove(const TCHAR *key);
757 void clear();
758 void filterElements(bool (*filter)(const TCHAR *, const void *, void *), void *userData);
759
760 int size() const;
761 bool contains(const TCHAR *key) const { return find(key) != NULL; }
762
763 EnumerationCallbackResult forEach(EnumerationCallbackResult (*cb)(const TCHAR *, const void *, void *), void *userData) const;
764 const void *findElement(bool (*comparator)(const TCHAR *, const void *, void *), void *userData) const;
765
766 StructArray<KeyValuePair> *toArray() const;
767 StringList *keys() const;
768 };
769
770 /**
771 * NXCP message class
772 */
773 class NXCPMessage;
774
775 /**
776 * String map class
777 */
778 class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
779 {
780 public:
781 StringMap() : StringMapBase(true) { }
782 StringMap(const StringMap &src);
783 virtual ~StringMap();
784
785 StringMap& operator =(const StringMap &src);
786
787 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
788 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
789 void set(const TCHAR *key, UINT32 value);
790
791 void addAll(StringMap *src);
792
793 const TCHAR *get(const TCHAR *key) const { return (const TCHAR *)getObject(key); }
794 INT32 getInt32(const TCHAR *key, INT32 defaultValue) const;
795 UINT32 getUInt32(const TCHAR *key, UINT32 defaultValue) const;
796 INT64 getInt64(const TCHAR *key, INT64 defaultValue) const;
797 UINT64 getUInt64(const TCHAR *key, UINT64 defaultValue) const;
798 double getDouble(const TCHAR *key, double defaultValue) const;
799 bool getBoolean(const TCHAR *key, bool defaultValue) const;
800
801 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
802 void loadMessage(const NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId);
803 };
804
805 /**
806 * String map template for holding objects as values
807 */
808 template <class T> class StringObjectMap : public StringMapBase
809 {
810 private:
811 static void destructor(void *object) { delete (T*)object; }
812
813 public:
814 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
815
816 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
817 void setPreallocated(TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, true); }
818 T *get(const TCHAR *key) const { return (T*)getObject(key); }
819 };
820
821 /**
822 * String list class
823 */
824 class LIBNETXMS_EXPORTABLE StringList
825 {
826 private:
827 int m_count;
828 int m_allocated;
829 TCHAR **m_values;
830
831 public:
832 StringList();
833 StringList(const StringList *src);
834 StringList(const TCHAR *src, const TCHAR *separator);
835 ~StringList();
836
837 void add(const TCHAR *value);
838 void addPreallocated(TCHAR *value);
839 void add(INT32 value);
840 void add(UINT32 value);
841 void add(INT64 value);
842 void add(UINT64 value);
843 void add(double value);
844 void replace(int index, const TCHAR *value);
845 void addOrReplace(int index, const TCHAR *value);
846 void addOrReplacePreallocated(int index, TCHAR *value);
847 #ifdef UNICODE
848 void addMBString(const char *value) { addPreallocated(WideStringFromMBString(value)); }
849 #else
850 void addMBString(const char *value) { add(value); }
851 #endif
852 void clear();
853 int size() const { return m_count; }
854 const TCHAR *get(int index) const { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
855 int indexOf(const TCHAR *value) const;
856 bool contains(const TCHAR *value) const { return indexOf(value) != -1; }
857 int indexOfIgnoreCase(const TCHAR *value) const;
858 bool containsIgnoreCase(const TCHAR *value) const { return indexOfIgnoreCase(value) != -1; }
859 void remove(int index);
860 void addAll(const StringList *src);
861 void merge(const StringList *src, bool matchCase);
862 TCHAR *join(const TCHAR *separator);
863 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
864 void sort(bool ascending = true, bool caseSensitive = false);
865 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId);
866 };
867
868 /**
869 * Entry of string set
870 */
871 struct StringSetEntry;
872
873 /**
874 * NXCP message
875 */
876 class NXCPMessage;
877
878 /**
879 * String set
880 */
881 class StringSet;
882
883 /**
884 * String set iterator
885 */
886 class LIBNETXMS_EXPORTABLE StringSetIterator : public AbstractIterator
887 {
888 private:
889 StringSet *m_stringSet;
890 StringSetEntry *m_curr;
891 StringSetEntry *m_next;
892
893 public:
894 StringSetIterator(StringSet *stringSet);
895
896 virtual bool hasNext();
897 virtual void *next();
898 virtual void remove();
899 };
900
901 /**
902 * String set class
903 */
904 class LIBNETXMS_EXPORTABLE StringSet
905 {
906 friend class StringSetIterator;
907
908 private:
909 StringSetEntry *m_data;
910
911 public:
912 StringSet();
913 ~StringSet();
914
915 void add(const TCHAR *str);
916 void addPreallocated(TCHAR *str);
917 void remove(const TCHAR *str);
918 void clear();
919
920 int size() const;
921 bool contains(const TCHAR *str) const;
922 bool equals(const StringSet *s) const;
923
924 void addAll(StringSet *src);
925 void addAll(TCHAR **strings, int count);
926 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
927 void addAllPreallocated(TCHAR **strings, int count);
928
929 void forEach(bool (*cb)(const TCHAR *, void *), void *userData) const;
930
931 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId) const;
932 void addAllFromMessage(const NXCPMessage *msg, UINT32 baseId, UINT32 countId, bool clearBeforeAdd, bool toUppercase);
933
934 String join(const TCHAR *separator);
935
936 Iterator<const TCHAR> *iterator() { return new Iterator<const TCHAR>(new StringSetIterator(this)); }
937 };
938
939 /**
940 * Opaque hash map entry structure
941 */
942 struct HashMapEntry;
943
944 /**
945 * Hash map base class (for fixed size non-pointer keys)
946 */
947 class LIBNETXMS_EXPORTABLE HashMapBase
948 {
949 friend class HashMapIterator;
950
951 private:
952 HashMapEntry *m_data;
953 bool m_objectOwner;
954 unsigned int m_keylen;
955
956 HashMapEntry *find(const void *key) const;
957 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
958
959 protected:
960 void (*m_objectDestructor)(void *);
961
962 HashMapBase(bool objectOwner, unsigned int keylen);
963
964 void *_get(const void *key) const;
965 void _set(const void *key, void *value);
966 void _remove(const void *key);
967
968 bool _contains(const void *key) const { return find(key) != NULL; }
969
970 public:
971 virtual ~HashMapBase();
972
973 void setOwner(bool owner) { m_objectOwner = owner; }
974 void clear();
975
976 int size() const;
977
978 EnumerationCallbackResult forEach(EnumerationCallbackResult (*cb)(const void *, const void *, void *), void *userData) const;
979 const void *findElement(bool (*comparator)(const void *, const void *, void *), void *userData) const;
980 };
981
982 /**
983 * Hash map iterator
984 */
985 class LIBNETXMS_EXPORTABLE HashMapIterator : public AbstractIterator
986 {
987 private:
988 HashMapBase *m_hashMap;
989 HashMapEntry *m_curr;
990 HashMapEntry *m_next;
991
992 public:
993 HashMapIterator(HashMapBase *hashMap);
994
995 virtual bool hasNext();
996 virtual void *next();
997 virtual void remove();
998 };
999
1000 /**
1001 * Hash map template for holding objects as values
1002 */
1003 template <class K, class V> class HashMap : public HashMapBase
1004 {
1005 private:
1006 static void destructor(void *object) { delete (V*)object; }
1007
1008 public:
1009 HashMap(bool objectOwner = false) : HashMapBase(objectOwner, sizeof(K)) { m_objectDestructor = destructor; }
1010
1011 V *get(const K& key) { return (V*)_get(&key); }
1012 void set(const K& key, V *value) { _set(&key, (void *)value); }
1013 void remove(const K& key) { _remove(&key); }
1014 bool contains(const K& key) { return _contains(&key); }
1015
1016 Iterator<V> *iterator() { return new Iterator<V>(new HashMapIterator(this)); }
1017 };
1018
1019 /**
1020 * Hash map template for holding reference counting objects as values
1021 */
1022 template <class K, class V> class RefCountHashMap : public HashMapBase
1023 {
1024 private:
1025 static void destructor(void *object) { if (object != NULL) ((V*)object)->decRefCount(); }
1026
1027 public:
1028 RefCountHashMap(bool objectOwner = false) : HashMapBase(objectOwner, sizeof(K)) { m_objectDestructor = destructor; }
1029
1030 V *get(const K& key) { V *v = (V*)_get(&key); if (v != NULL) v->incRefCount(); return v; }
1031 void set(const K& key, V *value) { if (value != NULL) value->incRefCount(); _set(&key, (void *)value); }
1032 void remove(const K& key) { _remove(&key); }
1033 bool contains(const K& key) { return _contains(&key); }
1034
1035 Iterator<V> *iterator() { return new Iterator<V>(new HashMapIterator(this)); }
1036 };
1037
1038 /**
1039 * Byte stream
1040 */
1041 class LIBNETXMS_EXPORTABLE ByteStream
1042 {
1043 private:
1044 BYTE *m_data;
1045 size_t m_size;
1046 size_t m_allocated;
1047 size_t m_pos;
1048 size_t m_allocationStep;
1049
1050 public:
1051 ByteStream(size_t initial = 8192);
1052 ByteStream(const void *data, size_t size);
1053 virtual ~ByteStream();
1054
1055 static ByteStream *load(const TCHAR *file);
1056
1057 void seek(size_t pos) { if (pos <= m_size) m_pos = pos; }
1058 size_t pos() { return m_pos; }
1059 size_t size() { return m_size; }
1060 bool eos() { return m_pos == m_size; }
1061
1062 void setAllocationStep(size_t s) { m_allocationStep = s; }
1063
1064 const BYTE *buffer(size_t *size) { *size = m_size; return m_data; }
1065
1066 void write(const void *data, size_t size);
1067 void write(char c) { write(&c, 1); }
1068 void write(BYTE b) { write(&b, 1); }
1069 void write(INT16 n) { UINT16 x = htons((UINT16)n); write(&x, 2); }
1070 void write(UINT16 n) { UINT16 x = htons(n); write(&x, 2); }
1071 void write(INT32 n) { UINT32 x = htonl((UINT32)n); write(&x, 4); }
1072 void write(UINT32 n) { UINT32 x = htonl(n); write(&x, 4); }
1073 void write(INT64 n) { UINT64 x = htonq((UINT64)n); write(&x, 8); }
1074 void write(UINT64 n) { UINT64 x = htonq(n); write(&x, 8); }
1075 void write(double n) { double x = htond(n); write(&x, 8); }
1076 void writeString(const TCHAR *s);
1077
1078 size_t read(void *buffer, size_t count);
1079 char readChar() { return !eos() ? (char)m_data[m_pos++] : 0; }
1080 BYTE readByte() { return !eos() ? m_data[m_pos++] : 0; }
1081 INT16 readInt16();
1082 UINT16 readUInt16();
1083 INT32 readInt32();
1084 UINT32 readUInt32();
1085 INT64 readInt64();
1086 UINT64 readUInt64();
1087 double readDouble();
1088 TCHAR *readString();
1089
1090 bool save(int f);
1091 };
1092
1093 /**
1094 * Auxilliary class for objects which counts references and
1095 * destroys itself wheren reference count falls to 0
1096 */
1097 class LIBNETXMS_EXPORTABLE RefCountObject
1098 {
1099 private:
1100 VolatileCounter m_refCount;
1101
1102 protected:
1103 virtual ~RefCountObject();
1104
1105 public:
1106 RefCountObject()
1107 {
1108 m_refCount = 1;
1109 }
1110
1111 void incRefCount()
1112 {
1113 InterlockedIncrement(&m_refCount);
1114 }
1115
1116 void decRefCount()
1117 {
1118 if (InterlockedDecrement(&m_refCount) == 0)
1119 delete this;
1120 }
1121 };
1122
1123 class NXCPMessage;
1124
1125 /**
1126 * Table column definition
1127 */
1128 class LIBNETXMS_EXPORTABLE TableColumnDefinition
1129 {
1130 private:
1131 TCHAR *m_name;
1132 TCHAR *m_displayName;
1133 INT32 m_dataType;
1134 bool m_instanceColumn;
1135
1136 public:
1137 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
1138 TableColumnDefinition(NXCPMessage *msg, UINT32 baseId);
1139 TableColumnDefinition(TableColumnDefinition *src);
1140 ~TableColumnDefinition();
1141
1142 void fillMessage(NXCPMessage *msg, UINT32 baseId);
1143
1144 const TCHAR *getName() { return m_name; }
1145 const TCHAR *getDisplayName() { return m_displayName; }
1146 INT32 getDataType() { return m_dataType; }
1147 bool isInstanceColumn() { return m_instanceColumn; }
1148
1149 void setDataType(INT32 type) { m_dataType = type; }
1150 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
1151 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
1152 };
1153
1154 /**
1155 * Table cell
1156 */
1157 class TableCell
1158 {
1159 private:
1160 TCHAR *m_value;
1161 int m_status;
1162 UINT32 m_objectId;
1163
1164 public:
1165 TableCell() { m_value = NULL; m_status = -1; m_objectId = 0; }
1166 TableCell(const TCHAR *value) { m_value = _tcsdup_ex(value); m_status = -1; m_objectId = 0; }
1167 TableCell(const TCHAR *value, int status) { m_value = _tcsdup_ex(value); m_status = status; m_objectId = 0; }
1168 TableCell(TableCell *src) { m_value = _tcsdup_ex(src->m_value); m_status = src->m_status; m_objectId = src->m_objectId; }
1169 ~TableCell() { safe_free(m_value); }
1170
1171 void set(const TCHAR *value, int status, UINT32 objectId) { safe_free(m_value); m_value = _tcsdup_ex(value); m_status = status; m_objectId = objectId; }
1172 void setPreallocated(TCHAR *value, int status, UINT32 objectId) { safe_free(m_value); m_value = value; m_status = status; m_objectId = objectId; }
1173
1174 const TCHAR *getValue() { return m_value; }
1175 void setValue(const TCHAR *value) { safe_free(m_value); m_value = _tcsdup_ex(value); }
1176 void setPreallocatedValue(TCHAR *value) { free(m_value); m_value = value; }
1177
1178 int getStatus() { return m_status; }
1179 void setStatus(int status) { m_status = status; }
1180
1181 int getObjectId() { return m_objectId; }
1182 void setObjectId(UINT32 id) { m_objectId = id; }
1183 };
1184
1185 /**
1186 * Table row
1187 */
1188 class TableRow
1189 {
1190 private:
1191 ObjectArray<TableCell> *m_cells;
1192 UINT32 m_objectId;
1193
1194 public:
1195 TableRow(int columnCount);
1196 TableRow(TableRow *src);
1197 ~TableRow() { delete m_cells; }
1198
1199 void addColumn() { m_cells->add(new TableCell); }
1200 void deleteColumn(int index) { m_cells->remove(index); }
1201
1202 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); }
1203 void setPreallocated(int index, TCHAR *value, int status, UINT32 objectId) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocated(value, status, objectId); }
1204
1205 void setValue(int index, const TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setValue(value); }
1206 void setPreallocatedValue(int index, TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocatedValue(value); else free(value); }
1207
1208 void setStatus(int index, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setStatus(status); }
1209
1210 const TCHAR *getValue(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getValue() : NULL; }
1211 int getStatus(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getStatus() : -1; }
1212
1213 UINT32 getObjectId() { return m_objectId; }
1214 void setObjectId(UINT32 id) { m_objectId = id; }
1215
1216 UINT32 getCellObjectId(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getObjectId() : 0; }
1217 void setCellObjectId(int index, UINT32 id) { TableCell *c = m_cells->get(index); if (c != NULL) c->setObjectId(id); }
1218 };
1219
1220 /**
1221 * Class for table data storage
1222 */
1223 class LIBNETXMS_EXPORTABLE Table : public RefCountObject
1224 {
1225 private:
1226 ObjectArray<TableRow> *m_data;
1227 ObjectArray<TableColumnDefinition> *m_columns;
1228 TCHAR *m_title;
1229 int m_source;
1230 bool m_extendedFormat;
1231
1232 void createFromMessage(NXCPMessage *msg);
1233 void destroy();
1234 bool parseXML(const char *xml);
1235
1236 public:
1237 Table();
1238 Table(Table *src);
1239 Table(NXCPMessage *msg);
1240 virtual ~Table();
1241
1242 int fillMessage(NXCPMessage &msg, int offset, int rowLimit);
1243 void updateFromMessage(NXCPMessage *msg);
1244
1245 void addAll(Table *src);
1246 void copyRow(Table *src, int row);
1247
1248 int getNumRows() { return m_data->size(); }
1249 int getNumColumns() { return m_columns->size(); }
1250 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
1251 int getSource() { return m_source; }
1252
1253 bool isExtendedFormat() { return m_extendedFormat; }
1254 void setExtendedFormat(bool ext) { m_extendedFormat = ext; }
1255
1256 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
1257 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
1258 int getColumnIndex(const TCHAR *name);
1259 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
1260
1261 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = _tcsdup_ex(title); }
1262 void setSource(int source) { m_source = source; }
1263 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
1264 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
1265 int addRow();
1266
1267 void deleteRow(int row);
1268 void deleteColumn(int col);
1269
1270 void setAt(int nRow, int nCol, INT32 nData);
1271 void setAt(int nRow, int nCol, UINT32 dwData);
1272 void setAt(int nRow, int nCol, double dData);
1273 void setAt(int nRow, int nCol, INT64 nData);
1274 void setAt(int nRow, int nCol, UINT64 qwData);
1275 void setAt(int nRow, int nCol, const TCHAR *pszData);
1276 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
1277
1278 void set(int nCol, INT32 nData) { setAt(getNumRows() - 1, nCol, nData); }
1279 void set(int nCol, UINT32 dwData) { setAt(getNumRows() - 1, nCol, dwData); }
1280 void set(int nCol, double dData) { setAt(getNumRows() - 1, nCol, dData); }
1281 void set(int nCol, INT64 nData) { setAt(getNumRows() - 1, nCol, nData); }
1282 void set(int nCol, UINT64 qwData) { setAt(getNumRows() - 1, nCol, qwData); }
1283 void set(int nCol, const TCHAR *data) { setAt(getNumRows() - 1, nCol, data); }
1284 #ifdef UNICODE
1285 void set(int nCol, const char *data) { setPreallocatedAt(getNumRows() - 1, nCol, WideStringFromMBString(data)); }
1286 #else
1287 void set(int nCol, const WCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, MBStringFromWideString(data)); }
1288 #endif
1289 void setPreallocated(int nCol, TCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, data); }
1290
1291 void setStatusAt(int row, int col, int status);
1292 void setStatus(int col, int status) { setStatusAt(getNumRows() - 1, col, status); }
1293
1294 const TCHAR *getAsString(int nRow, int nCol, const TCHAR *defaultValue = NULL);
1295 INT32 getAsInt(int nRow, int nCol);
1296 UINT32 getAsUInt(int nRow, int nCol);
1297 INT64 getAsInt64(int nRow, int nCol);
1298 UINT64 getAsUInt64(int nRow, int nCol);
1299 double getAsDouble(int nRow, int nCol);
1300
1301 int getStatus(int nRow, int nCol);
1302
1303 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
1304 int findRowByInstance(const TCHAR *instance);
1305
1306 UINT32 getObjectId(int row) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getObjectId() : 0; }
1307 void setObjectId(int row, UINT32 id) { TableRow *r = m_data->get(row); if (r != NULL) r->setObjectId(id); }
1308
1309 void setCellObjectIdAt(int row, int col, UINT32 objectId);
1310 void setCellObjectId(int col, UINT32 objectId) { setCellObjectIdAt(getNumRows() - 1, col, objectId); }
1311 UINT32 getCellObjectId(int row, int col) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getCellObjectId(col) : 0; }
1312
1313 static Table *createFromXML(const char *xml);
1314 TCHAR *createXML();
1315
1316 static Table *createFromPackedXML(const char *packedXml);
1317 char *createPackedXML();
1318 };
1319
1320 /**
1321 * sockaddr buffer
1322 */
1323 union SockAddrBuffer
1324 {
1325 struct sockaddr_in sa4;
1326 #ifdef WITH_IPV6
1327 struct sockaddr_in6 sa6;
1328 #endif
1329 };
1330
1331 /**
1332 * sockaddr length calculation
1333 */
1334 #ifdef WITH_IPV6
1335 #define SA_LEN(sa) (((sa)->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
1336 #else
1337 #define SA_LEN(sa) sizeof(struct sockaddr_in)
1338 #endif
1339
1340 /**
1341 * Get port number from SockAddrBuffer
1342 */
1343 #ifdef WITH_IPV6
1344 #define SA_PORT(sa) ((((struct sockaddr *)sa)->sa_family == AF_INET) ? ((struct sockaddr_in *)sa)->sin_port : ((struct sockaddr_in6 *)sa)->sin6_port)
1345 #else
1346 #define SA_PORT(sa) (((struct sockaddr_in *)sa)->sin_port)
1347 #endif
1348
1349 /**
1350 * Compare addresses in sockaddr
1351 */
1352 inline bool SocketAddressEquals(struct sockaddr *a1, struct sockaddr *a2)
1353 {
1354 if (a1->sa_family != a2->sa_family)
1355 return false;
1356 if (a1->sa_family == AF_INET)
1357 return ((struct sockaddr_in *)a1)->sin_addr.s_addr == ((struct sockaddr_in *)a2)->sin_addr.s_addr;
1358 #ifdef WITH_IPV6
1359 if (a1->sa_family == AF_INET6)
1360 return !memcmp(((struct sockaddr_in6 *)a1)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)a2)->sin6_addr.s6_addr, 16);
1361 #endif
1362 return false;
1363 }
1364
1365 /**
1366 * IP address
1367 */
1368 class LIBNETXMS_EXPORTABLE InetAddress
1369 {
1370 private:
1371 short m_maskBits;
1372 short m_family;
1373 union
1374 {
1375 UINT32 v4;
1376 BYTE v6[16];
1377 } m_addr;
1378
1379 public:
1380 InetAddress();
1381 InetAddress(UINT32 addr);
1382 InetAddress(UINT32 addr, UINT32 mask);
1383 InetAddress(const BYTE *addr, int maskBits = 128);
1384
1385 bool isAnyLocal() const;
1386 bool isLoopback() const;
1387 bool isMulticast() const;
1388 bool isBroadcast() const;
1389 bool isLinkLocal() const;
1390 bool isValid() const { return m_family != AF_UNSPEC; }
1391 bool isValidUnicast() const { return isValid() && !isAnyLocal() && !isLoopback() && !isMulticast() && !isBroadcast() && !isLinkLocal(); }
1392
1393 int getFamily() const { return m_family; }
1394 UINT32 getAddressV4() const { return (m_family == AF_INET) ? m_addr.v4 : 0; }
1395 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"; }
1396
1397 bool contain(const InetAddress &a) const;
1398 bool sameSubnet(const InetAddress &a) const;
1399 bool equals(const InetAddress &a) const;
1400 int compareTo(const InetAddress &a) const;
1401
1402 void setMaskBits(int m) { m_maskBits = m; }
1403 int getMaskBits() const { return m_maskBits; }
1404 int getHostBits() const { return (m_family == AF_INET) ? (32 - m_maskBits) : (128 - m_maskBits); }
1405
1406 InetAddress getSubnetAddress() const;
1407 InetAddress getSubnetBroadcast() const;
1408 bool isSubnetBroadcast(int maskBits) const;
1409
1410 String toString() const;
1411 TCHAR *toString(TCHAR *buffer) const;
1412 #ifdef UNICODE
1413 char *toStringA(char *buffer) const;
1414 #else
1415 char *toStringA(char *buffer) const { return toString(buffer); }
1416 #endif
1417
1418 BYTE *buildHashKey(BYTE *key) const;
1419
1420 TCHAR *getHostByAddr(TCHAR *buffer, size_t buflen) const;
1421
1422 struct sockaddr *fillSockAddr(SockAddrBuffer *buffer, UINT16 port = 0) const;
1423
1424 static InetAddress resolveHostName(const WCHAR *hostname, int af = AF_INET);
1425 static InetAddress resolveHostName(const char *hostname, int af = AF_INET);
1426 static InetAddress parse(const WCHAR *str);
1427 static InetAddress parse(const char *str);
1428 static InetAddress createFromSockaddr(struct sockaddr *s);
1429
1430 static const InetAddress INVALID;
1431 static const InetAddress LOOPBACK;
1432 };
1433
1434 /**
1435 * IP address list
1436 */
1437 class LIBNETXMS_EXPORTABLE InetAddressList
1438 {
1439 private:
1440 ObjectArray<InetAddress> *m_list;
1441
1442 int indexOf(const InetAddress& addr) const;
1443
1444 public:
1445 InetAddressList();
1446 ~InetAddressList();
1447
1448 void add(const InetAddress& addr);
1449 void add(const InetAddressList& addrList);
1450 void replace(const InetAddress& addr);
1451 void remove(const InetAddress& addr);
1452 void clear() { m_list->clear(); }
1453 const InetAddress& get(int index) const { const InetAddress *a = m_list->get(index); return (a != NULL) ? *a : InetAddress::INVALID; }
1454
1455 int size() const { return m_list->size(); }
1456 bool hasAddress(const InetAddress& addr) const { return indexOf(addr) != -1; }
1457 const InetAddress& findAddress(const InetAddress& addr) const { int idx = indexOf(addr); return (idx != -1) ? *m_list->get(idx) : InetAddress::INVALID; }
1458 const InetAddress& findSameSubnetAddress(const InetAddress& addr) const;
1459 const InetAddress& getFirstUnicastAddress() const;
1460 const InetAddress& getFirstUnicastAddressV4() const;
1461 bool hasValidUnicastAddress() const { return getFirstUnicastAddress().isValid(); }
1462 bool isLoopbackOnly() const;
1463
1464 const ObjectArray<InetAddress> *getList() const { return m_list; }
1465
1466 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
1467 };
1468
1469 /**
1470 * Network connection
1471 */
1472 class LIBNETXMS_EXPORTABLE SocketConnection
1473 {
1474 protected:
1475 SOCKET m_socket;
1476 char m_data[4096];
1477 int m_dataPos;
1478
1479 public:
1480 SocketConnection();
1481 virtual ~SocketConnection();
1482
1483 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1484 bool connectTCP(const InetAddress& ip, WORD port, UINT32 timeout);
1485 void disconnect();
1486
1487 bool canRead(UINT32 timeout);
1488 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
1489 bool waitForText(const char *text, int timeout);
1490
1491 int write(const char *pBuff, int nSize);
1492 bool writeLine(const char *line);
1493
1494 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
1495 };
1496
1497 /**
1498 * Telnet connection - handles all telnet negotiation
1499 */
1500 class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
1501 {
1502 protected:
1503 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1504 bool connectTCP(const InetAddress& ip, WORD port, UINT32 timeout);
1505
1506 public:
1507 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
1508
1509 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
1510 bool connect(const InetAddress& ip, WORD port, UINT32 timeout);
1511 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
1512 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
1513 };
1514
1515 /**
1516 * Postal address representation
1517 */
1518 class LIBNETXMS_EXPORTABLE PostalAddress
1519 {
1520 private:
1521 TCHAR *m_country;
1522 TCHAR *m_city;
1523 TCHAR *m_streetAddress;
1524 TCHAR *m_postcode;
1525
1526 public:
1527 PostalAddress();
1528 PostalAddress(const TCHAR *country, const TCHAR *city, const TCHAR *streetAddress, const TCHAR *postcode);
1529 ~PostalAddress();
1530
1531 const TCHAR *getCountry() const { return CHECK_NULL_EX(m_country); }
1532 const TCHAR *getCity() const { return CHECK_NULL_EX(m_city); }
1533 const TCHAR *getStreetAddress() const { return CHECK_NULL_EX(m_streetAddress); }
1534 const TCHAR *getPostCode() const { return CHECK_NULL_EX(m_postcode); }
1535
1536 void setCountry(const TCHAR *country) { free(m_country); m_country = _tcsdup_ex(country); }
1537 void setCity(const TCHAR *city) { free(m_city); m_city = _tcsdup_ex(city); }
1538 void setStreetAddress(const TCHAR *streetAddress) { free(m_streetAddress); m_streetAddress = _tcsdup_ex(streetAddress); }
1539 void setPostCode(const TCHAR *postcode) { free(m_postcode); m_postcode = _tcsdup_ex(postcode); }
1540 };
1541
1542 /**
1543 * Max number of polled sockets
1544 */
1545 #define SOCKET_POLLER_MAX_SOCKETS 16
1546
1547 /**
1548 * Socket poller
1549 */
1550 class LIBNETXMS_EXPORTABLE SocketPoller
1551 {
1552 private:
1553 bool m_write;
1554 int m_count;
1555 #if HAVE_POLL
1556 struct pollfd m_sockets[SOCKET_POLLER_MAX_SOCKETS];
1557 #else
1558 fd_set m_sockets;
1559 #ifndef _WIN32
1560 SOCKET m_maxfd;
1561 #endif
1562 #endif
1563
1564 public:
1565 SocketPoller(bool write = false);
1566 ~SocketPoller();
1567
1568 bool add(SOCKET s);
1569 int poll(UINT32 timeout);
1570 bool isSet(SOCKET s);
1571 void reset();
1572 };
1573
1574 /**
1575 * Abstract communication channel
1576 */
1577 class LIBNETXMS_EXPORTABLE AbstractCommChannel : public RefCountObject
1578 {
1579 protected:
1580 virtual ~AbstractCommChannel();
1581
1582 public:
1583 AbstractCommChannel();
1584
1585 virtual int send(const void *data, size_t size, MUTEX mutex = INVALID_MUTEX_HANDLE) = 0;
1586 virtual int recv(void *buffer, size_t size, UINT32 timeout = INFINITE) = 0;
1587 virtual int poll(UINT32 timeout, bool write = false) = 0;
1588 virtual int shutdown() = 0;
1589 virtual void close() = 0;
1590 };
1591
1592 /**
1593 * Socket communication channel
1594 */
1595 class LIBNETXMS_EXPORTABLE SocketCommChannel : public AbstractCommChannel
1596 {
1597 private:
1598 SOCKET m_socket;
1599 bool m_owner;
1600
1601 protected:
1602 virtual ~SocketCommChannel();
1603
1604 public:
1605 SocketCommChannel(SOCKET socket, bool owner = true);
1606
1607 virtual int send(const void *data, size_t size, MUTEX mutex = INVALID_MUTEX_HANDLE);
1608 virtual int recv(void *buffer, size_t size, UINT32 timeout = INFINITE);
1609 virtual int poll(UINT32 timeout, bool write = false);
1610 virtual int shutdown();
1611 virtual void close();
1612 };
1613
1614 #endif /* __cplusplus */
1615
1616 /**
1617 * Configuration item template for configuration loader
1618 */
1619 typedef struct
1620 {
1621 TCHAR token[64];
1622 BYTE type;
1623 BYTE separator; // Separator character for lists
1624 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
1625 UINT64 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
1626 UINT32 bufferPos; // Should be set to 0
1627 void *buffer;
1628 void *overrideIndicator;
1629 } NX_CFG_TEMPLATE;
1630
1631 /**
1632 * Code translation structure
1633 */
1634 typedef struct __CODE_TO_TEXT
1635 {
1636 int code;
1637 const TCHAR *text;
1638 } CODE_TO_TEXT;
1639
1640 /**
1641 * getopt() prototype if needed
1642 */
1643 #if USE_BUNDLED_GETOPT
1644 #include <netxms_getopt.h>
1645 #endif
1646
1647 //
1648 // Structures for opendir() / readdir() / closedir()
1649 //
1650
1651 #ifdef _WIN32
1652
1653 #define DT_UNKNOWN 0
1654 #define DT_REG 1
1655 #define DT_DIR 2
1656
1657 typedef struct dirent
1658 {
1659 long d_ino; /* inode number (not used by MS-DOS) */
1660 unsigned char d_type; /* file type */
1661 int d_namlen; /* Name length */
1662 char d_name[MAX_PATH]; /* file name */
1663 } _DIRECT;
1664
1665 typedef struct _dir_struc
1666 {
1667 HANDLE handle;
1668 struct dirent dirstr; /* Directory structure to return */
1669 } DIR;
1670
1671 #ifdef UNICODE
1672
1673 typedef struct dirent_w
1674 {
1675 long d_ino; /* inode number (not used by MS-DOS) */
1676 unsigned char d_type; /* file type */
1677 int d_namlen; /* Name length */
1678 WCHAR d_name[MAX_PATH]; /* file name */
1679 } _DIRECTW;
1680
1681 typedef struct _dir_struc_w
1682 {
1683 HANDLE handle;
1684 struct dirent_w dirstr; /* Directory structure to return */
1685 } DIRW;
1686
1687 #define _TDIR DIRW
1688 #define _TDIRECT _DIRECTW
1689 #define _tdirent dirent_w
1690
1691 #else
1692
1693 #define _TDIR DIR
1694 #define _TDIRECT _DIRECT
1695 #define _tdirent dirent
1696
1697 #endif
1698
1699 #else /* not _WIN32 */
1700
1701 typedef struct dirent_w
1702 {
1703 long d_ino; /* inode number */
1704 WCHAR d_name[257]; /* file name */
1705 } _DIRECTW;
1706
1707 typedef struct _dir_struc_w
1708 {
1709 DIR *dir; /* Original non-unicode structure */
1710 struct dirent_w dirstr; /* Directory structure to return */
1711 } DIRW;
1712
1713 #endif /* _WIN32 */
1714
1715
1716 //
1717 // Functions
1718 //
1719
1720 #ifdef __cplusplus
1721
1722 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
1723 {
1724 #if defined(_WIN32) && (_MSC_VER >= 1400) && !defined(__clang__)
1725 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
1726 #else
1727 _tcsncpy(pszDest, pszSrc, nLen - 1);
1728 pszDest[nLen - 1] = 0;
1729 #endif
1730 return pszDest;
1731 }
1732
1733 #ifdef UNICODE
1734 inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
1735 {
1736 #if defined(_WIN32) && (_MSC_VER >= 1400) && !defined(__clang__)
1737 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
1738 #else
1739 strncpy(pszDest, pszSrc, nLen - 1);
1740 pszDest[nLen - 1] = 0;
1741 #endif
1742 return pszDest;
1743 }
1744 #else
1745 #define nx_strncpy_mb nx_strncpy
1746 #endif
1747
1748 int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
1749 int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
1750 int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
1751 bool LIBNETXMS_EXPORTABLE RecvAll(SOCKET s, void *buffer, size_t size, UINT32 timeout);
1752
1753 SOCKET LIBNETXMS_EXPORTABLE ConnectToHost(const InetAddress& addr, UINT16 port, UINT32 timeout);
1754
1755 #endif /* __cplusplus */
1756
1757 #ifdef __cplusplus
1758 extern "C"
1759 {
1760 #endif
1761
1762 void LIBNETXMS_EXPORTABLE InitNetXMSProcess(bool commandLineTool);
1763
1764 #ifndef _WIN32
1765 #if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
1766 void LIBNETXMS_EXPORTABLE __wcsupr(WCHAR *in);
1767 #define wcsupr __wcsupr
1768 #endif
1769 void LIBNETXMS_EXPORTABLE __strupr(char *in);
1770 #define strupr __strupr
1771 #endif /* _WIN32 */
1772
1773 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
1774 int (*compare)(const void *, const void *, void *));
1775
1776 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
1777
1778 UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
1779 UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
1780 #ifdef UNICODE
1781 #define FileSize FileSizeW
1782 #else
1783 #define FileSize FileSizeA
1784 #endif
1785
1786 int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
1787 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
1788 #ifdef UNICODE
1789 char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
1790 #else
1791 #define IpToStrA IpToStr
1792 #endif
1793 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(const BYTE *addr, TCHAR *buffer);
1794 #ifdef UNICODE
1795 char LIBNETXMS_EXPORTABLE *Ip6ToStrA(const BYTE *addr, char *buffer);
1796 #else
1797 #define Ip6ToStrA Ip6ToStr
1798 #endif
1799 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
1800
1801 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
1802 void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
1803
1804 WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const void *data, size_t size, WCHAR *str);
1805 char LIBNETXMS_EXPORTABLE *BinToStrA(const void *data, size_t size, char *str);
1806 #ifdef UNICODE
1807 #define BinToStr BinToStrW
1808 #else
1809 #define BinToStr BinToStrA
1810 #endif
1811
1812 size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
1813 size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
1814 #ifdef UNICODE
1815 #define StrToBin StrToBinW
1816 #else
1817 #define StrToBin StrToBinA
1818 #endif
1819
1820 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
1821
1822 void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
1823 void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
1824 #ifdef UNICODE
1825 #define StrStrip StrStripW
1826 #else
1827 #define StrStrip StrStripA
1828 #endif
1829
1830 const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
1831 const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
1832 #ifdef UNICODE
1833 #define ExtractWord ExtractWordW
1834 #else
1835 #define ExtractWord ExtractWordA
1836 #endif
1837
1838 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
1839 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
1840 #ifdef UNICODE
1841 #define NumChars NumCharsW
1842 #else
1843 #define NumChars NumCharsA
1844 #endif
1845
1846 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
1847 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
1848 #ifdef UNICODE
1849 #define RemoveTrailingCRLF RemoveTrailingCRLFW
1850 #else
1851 #define RemoveTrailingCRLF RemoveTrailingCRLFA
1852 #endif
1853
1854 bool LIBNETXMS_EXPORTABLE RegexpMatchA(const char *str, const char *expr, bool matchCase);
1855 bool LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *str, const WCHAR *expr, bool matchCase);
1856 #ifdef UNICODE
1857 #define RegexpMatch RegexpMatchW
1858 #else
1859 #define RegexpMatch RegexpMatchA
1860 #endif
1861
1862 const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
1863 BOOL LIBNETXMS_EXPORTABLE CreateFolder(const TCHAR *directory);
1864 bool LIBNETXMS_EXPORTABLE SetLastModificationTime(TCHAR *fileName, time_t lastModDate);
1865 TCHAR LIBNETXMS_EXPORTABLE *Trim(TCHAR *str);
1866 bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
1867 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
1868 int LIBNETXMS_EXPORTABLE GetLastMonthDay(struct tm *currTime);
1869 #ifdef __cplusplus
1870 bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime = 0);
1871 #else
1872 bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime);
1873 #endif
1874
1875
1876 #ifdef __cplusplus
1877 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
1878 #endif
1879 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
1880 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1881 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
1882 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
1883 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
1884 #ifdef UNICODE
1885 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
1886 #else
1887 #define LoadFileA LoadFile
1888 #endif
1889
1890 UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
1891 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
1892 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
1893 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
1894 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
1895 void LIBNETXMS_EXPORTABLE CalculateSHA256Hash(const unsigned char *data, size_t len, unsigned char *hash);
1896 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
1897 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
1898 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
1899
1900 void LIBNETXMS_EXPORTABLE GenerateRandomBytes(BYTE *buffer, size_t size);
1901
1902 void LIBNETXMS_EXPORTABLE LogOpenSSLErrorStack(int level);
1903
1904 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1905 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1906
1907 bool LIBNETXMS_EXPORTABLE DecryptPasswordW(const WCHAR *login, const WCHAR *encryptedPasswd, WCHAR *decryptedPasswd, size_t bufferLenght);
1908 bool LIBNETXMS_EXPORTABLE DecryptPasswordA(const char *login, const char *encryptedPasswd, char *decryptedPasswd, size_t bufferLenght);
1909 #ifdef UNICODE
1910 #define DecryptPassword DecryptPasswordW
1911 #else
1912 #define DecryptPassword DecryptPasswordA
1913 #endif
1914
1915 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
1916
1917 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
1918 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
1919 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
1920
1921 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1922 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1923 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
1924
1925 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1926 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1927 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
1928
1929 #ifdef UNICODE
1930 #define ExtractNamedOptionValue ExtractNamedOptionValueW
1931 #define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1932 #define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1933 #else
1934 #define ExtractNamedOptionValue ExtractNamedOptionValueA
1935 #define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1936 #define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1937 #endif
1938
1939 #ifdef __cplusplus
1940 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
1941 #else
1942 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
1943 #endif
1944
1945 #ifdef _WIN32
1946 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
1947 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
1948 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
1949 #endif
1950
1951 TCHAR LIBNETXMS_EXPORTABLE *GetLastSocketErrorText(TCHAR *buffer, size_t size);
1952
1953 #if !HAVE_DAEMON || !HAVE_DECL_DAEMON
1954 int LIBNETXMS_EXPORTABLE __daemon(int nochdir, int noclose);
1955 #define daemon __daemon
1956 #endif
1957
1958 UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
1959
1960 #ifndef _WIN32
1961
1962 bool LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
1963 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
1964 int cchWideChar, char *pByteStr, int cchByteChar,
1965 char *pDefaultChar, BOOL *pbUsedDefChar);
1966 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
1967 int cchByteChar, WCHAR *pWideCharStr,
1968 int cchWideChar);
1969
1970 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1971 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1972 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1973 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1974 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1975 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
1976
1977 int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1978 int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
1979 int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
1980 int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1981 int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
1982 int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
1983
1984 #endif /* _WIN32 */
1985
1986 #ifdef _WITH_ENCRYPTION
1987 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
1988 #endif
1989
1990 #if !defined(_WIN32) && !HAVE_WSTAT
1991 int wstat(const WCHAR *_path, struct stat *_sbuf);
1992 #endif
1993
1994 #if defined(UNICODE) && !defined(_WIN32)
1995
1996 #if !HAVE_WPOPEN
1997 FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
1998 #endif
1999 #if !HAVE_WFOPEN
2000 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
2001 #endif
2002 #if HAVE_FOPEN64 && !HAVE_WFOPEN64
2003 FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
2004 #endif
2005 #if !HAVE_WOPEN
2006 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
2007 #endif
2008 #if !HAVE_WCHMOD
2009 int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
2010 #endif
2011 #if !HAVE_WCHDIR
2012 int wchdir(const WCHAR *_path);
2013 #endif
2014 #if !HAVE_WMKDIR
2015 int wmkdir(const WCHAR *_path, int mode);
2016 #endif
2017 #if !HAVE_WUTIME
2018 int wutime(const WCHAR *_path, struct utimbuf *buf);
2019 #endif
2020 #if !HAVE_WRMDIR
2021 int wrmdir(const WCHAR *_path);
2022 #endif
2023 #if !HAVE_WRENAME
2024 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
2025 #endif
2026 #if !HAVE_WUNLINK
2027 int wunlink(const WCHAR *_path);
2028 #endif
2029 #if !HAVE_WREMOVE
2030 int wremove(const WCHAR *_path);
2031 #endif
2032 #if !HAVE_WSYSTEM
2033 int wsystem(const WCHAR *_cmd);
2034 #endif
2035 #if !HAVE_WMKSTEMP
2036 int wmkstemp(WCHAR *_template);
2037 #endif
2038 #if !HAVE_WACCESS
2039 int waccess(const WCHAR *_path, int mode);
2040 #endif
2041 #if !HAVE_WGETENV
2042 WCHAR *wgetenv(const WCHAR *_string);
2043 #endif
2044 #if !HAVE_WCTIME
2045 WCHAR *wctime(const time_t *timep);
2046 #endif
2047 #if !HAVE_PUTWS
2048 int putws(const WCHAR *s);
2049 #endif
2050 #if !HAVE_WCSERROR && (HAVE_STRERROR || HAVE_DECL_STRERROR)
2051 WCHAR *wcserror(int errnum);
2052 #endif
2053 #if !HAVE_WCSERROR_R && HAVE_STRERROR_R
2054 #if HAVE_POSIX_STRERROR_R
2055 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
2056 #else
2057 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
2058 #endif
2059 #endif
2060
2061 #endif /* UNICODE && !_WIN32*/
2062
2063 #if !HAVE_STRTOLL
2064 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
2065 #endif
2066 #if !HAVE_STRTOULL
2067 UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
2068 #endif
2069
2070 #if !HAVE_WCSTOLL
2071 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
2072 #endif
2073 #if !HAVE_WCSTOULL
2074 UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
2075 #endif
2076
2077 #if !HAVE_STRLWR && !defined(_WIN32)
2078 char LIBNETXMS_EXPORTABLE *strlwr(char *str);
2079 #endif
2080
2081 #if !HAVE_WCSLWR && !defined(_WIN32)
2082 WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
2083 #endif
2084
2085 #if !HAVE_WCSCASECMP && !defined(_WIN32)
2086 int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
2087 #endif
2088
2089 #if !HAVE_WCSNCASECMP && !defined(_WIN32)
2090 int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
2091 #endif
2092
2093 #if !defined(_WIN32) && (!HAVE_WCSFTIME || !WORKING_WCSFTIME)
2094 size_t LIBNETXMS_EXPORTABLE nx_wcsftime(WCHAR *buffer, size_t bufsize, const WCHAR *format, const struct tm *t);
2095 #undef wcsftime
2096 #define wcsftime nx_wcsftime
2097 #endif
2098
2099 #ifndef _WIN32
2100
2101 #if HAVE_ITOA && !HAVE__ITOA
2102 #define _itoa itoa
2103 #undef HAVE__ITOA
2104 #define HAVE__ITOA 1
2105 #endif
2106 #if !HAVE__ITOA && !defined(_WIN32)
2107 char LIBNETXMS_EXPORTABLE *_itoa(int value, char *str, int base);
2108 #endif
2109
2110 #if HAVE_ITOW && !HAVE__ITOW
2111 #define _itow itow
2112 #undef HAVE__ITOW
2113 #define HAVE__ITOW 1
2114 #endif
2115 #if !HAVE__ITOW && !defined(_WIN32)
2116 WCHAR LIBNETXMS_EXPORTABLE *_itow(int value, WCHAR *str, int base);
2117 #endif
2118
2119 #endif /* _WIN32 */
2120
2121 #ifdef _WIN32
2122 #ifdef UNICODE
2123 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *path);
2124 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *p);
2125 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *p);
2126
2127 #define _topendir wopendir
2128 #define _treaddir wreaddir
2129 #define _tclosedir wclosedir
2130 #else
2131 #define _topendir opendir
2132 #define _treaddir readdir
2133 #define _tclosedir closedir
2134 #endif
2135
2136 DIR LIBNETXMS_EXPORTABLE *opendir(const char *path);
2137 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *p);
2138 int LIBNETXMS_EXPORTABLE closedir(DIR *p);
2139
2140 #else /* not _WIN32 */
2141
2142 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
2143 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
2144 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
2145
2146 #endif
2147
2148 #if defined(_WIN32) || !(HAVE_SCANDIR)
2149 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
2150 int (*select)(const struct dirent *),
2151 int (*compar)(const struct dirent **, const struct dirent **));
2152 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
2153 #endif
2154
2155 TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
2156
2157 bool LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
2158 unsigned int msgCount, const TCHAR **messages, DWORD debugMsg);
2159 void LIBNETXMS_EXPORTABLE nxlog_close(void);
2160 void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
2161 void LIBNETXMS_EXPORTABLE nxlog_debug(int level, const TCHAR *format, ...);
2162 void LIBNETXMS_EXPORTABLE nxlog_debug2(int level, const TCHAR *format, va_list args);
2163 bool LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, UINT64 maxLogSize, int historySize, const TCHAR *dailySuffix);
2164 bool LIBNETXMS_EXPORTABLE nxlog_rotate();
2165 void LIBNETXMS_EXPORTABLE nxlog_set_debug_level(int level);
2166 int LIBNETXMS_EXPORTABLE nxlog_get_debug_level();
2167
2168 typedef void (*NxLogDebugWriter)(const TCHAR *);
2169 void LIBNETXMS_EXPORTABLE nxlog_set_debug_writer(NxLogDebugWriter writer);
2170
2171 typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
2172 void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
2173
2174 void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
2175 void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
2176 #if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
2177 __attribute__ ((format(printf, 1, 2)))
2178 #endif
2179 ;
2180
2181 #ifdef _WIN32
2182 int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
2183 int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
2184 #ifdef UNICODE
2185 #define _tmkstemp wmkstemp
2186 #else
2187 #define _tmkstemp mkstemp
2188 #endif
2189 #endif
2190
2191 #ifndef _WIN32
2192 int strcat_s(char *dst, size_t dstSize, const char *src);
2193 int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
2194 #endif
2195
2196 #if !HAVE_STRPTIME
2197 char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
2198 #endif
2199
2200 #if !HAVE_TIMEGM
2201 time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
2202 #endif
2203
2204 #if !HAVE_INET_PTON
2205 int LIBNETXMS_EXPORTABLE nx_inet_pton(int af, const char *src, void *dst);
2206 #define inet_pton nx_inet_pton
2207 #endif
2208
2209 int LIBNETXMS_EXPORTABLE GetSleepTime(int hour, int minute, int second);
2210 time_t LIBNETXMS_EXPORTABLE ParseDateTimeA(const char *text, time_t defaultValue);
2211 time_t LIBNETXMS_EXPORTABLE ParseDateTimeW(const WCHAR *text, time_t defaultValue);
2212
2213 #ifdef UNICODE
2214 #define ParseDateTime ParseDateTimeW
2215 #else
2216 #define ParseDateTime ParseDateTimeA
2217 #endif
2218
2219 #ifdef __cplusplus
2220 }
2221 #endif
2222
2223
2224 //
2225 // C++ only functions
2226 //
2227
2228 #ifdef __cplusplus
2229
2230 enum nxDirectoryType
2231 {
2232 nxDirBin = 0,
2233 nxDirData = 1,
2234 nxDirEtc = 2,
2235 nxDirLib = 3,
2236 nxDirShare = 4
2237 };
2238
2239 TCHAR LIBNETXMS_EXPORTABLE *GetHeapInfo();
2240
2241 void LIBNETXMS_EXPORTABLE GetNetXMSDirectory(nxDirectoryType type, TCHAR *dir);
2242 TCHAR LIBNETXMS_EXPORTABLE *FindJavaRuntime(TCHAR *buffer, size_t size);
2243
2244 UINT32 LIBNETXMS_EXPORTABLE IcmpPing(const InetAddress& addr, int iNumRetries, UINT32 dwTimeout, UINT32 *pdwRTT, UINT32 dwPacketSize);
2245
2246 TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
2247 String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
2248 const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
2249 int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
2250 UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
2251 bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
2252
2253 String LIBNETXMS_EXPORTABLE EscapeStringForJSON(const TCHAR *s);
2254 String LIBNETXMS_EXPORTABLE EscapeStringForAgent(const TCHAR *s);
2255
2256 StringList LIBNETXMS_EXPORTABLE *ParseCommandLine(const TCHAR *cmdline);
2257
2258 #if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
2259 void LIBNETXMS_EXPORTABLE BlockAllSignals(bool processWide, bool allowInterrupt);
2260 void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
2261 #endif
2262
2263 #endif
2264
2265 #endif /* _nms_util_h_ */