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