2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2015 Victor Kirhenshtein
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.
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.
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.
27 #ifdef LIBNETXMS_EXPORTS
28 #define LIBNETXMS_EXPORTABLE __declspec(dllexport)
30 #define LIBNETXMS_EXPORTABLE __declspec(dllimport)
33 #define LIBNETXMS_EXPORTABLE
37 #include <nms_common.h>
39 #include <nms_threads.h>
48 /*** Byte swapping ***/
54 #define SwapWideString(x)
57 #define htonq(x) htonll(x)
59 #define htonq(x) __bswap_64(x)
62 #define ntohq(x) ntohll(x)
64 #define ntohq(x) __bswap_64(x)
66 #define htond(x) __bswap_double(x)
67 #define ntohd(x) __bswap_double(x)
68 #define SwapWideString(x) __bswap_wstr(x)
75 #if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
76 UINT32 LIBNETXMS_EXPORTABLE
__bswap_32(UINT32 dwVal
);
78 #if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
79 UINT64 LIBNETXMS_EXPORTABLE
__bswap_64(UINT64 qwVal
);
81 double LIBNETXMS_EXPORTABLE
__bswap_double(double dVal
);
82 void LIBNETXMS_EXPORTABLE
__bswap_wstr(UCS2CHAR
*pStr
);
89 /*** Serial communications ***/
93 #define FLOW_SOFTWARE 1
94 #define FLOW_HARDWARE 2
101 # error termios.h not found
107 * Return codes for IcmpPing()
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
116 * Token types for configuration loader
120 #define CT_STRING_LIST 2
121 #define CT_END_OF_LIST 3
125 #define CT_MB_STRING 7
126 #define CT_BOOLEAN64 8
129 * Uninitialized value for override indicator
131 #define NXCONFIG_UNINITIALIZED_VALUE (-1)
134 * Return codes for NxLoadConfig()
136 #define NXCFG_ERR_OK 0
137 #define NXCFG_ERR_NOFILE 1
138 #define NXCFG_ERR_SYNTAX 2
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)
149 * nxlog rotation policy
151 #define NXLOG_ROTATION_DISABLED 0
152 #define NXLOG_ROTATION_DAILY 1
153 #define NXLOG_ROTATION_BY_SIZE 2
156 * _tcsdup() replacement
158 #if defined(_WIN32) && defined(USE_WIN32_HEAP)
162 char LIBNETXMS_EXPORTABLE
*nx_strdup(const char *src
);
163 WCHAR LIBNETXMS_EXPORTABLE
*nx_wcsdup(const WCHAR
*src
);
172 #if !HAVE_WCSDUP && !defined(_WIN32)
177 WCHAR LIBNETXMS_EXPORTABLE
*wcsdup(const WCHAR
*src
);
184 // Some AIX versions have broken wcsdup() so we use internal implementation
188 WCHAR LIBNETXMS_EXPORTABLE
*nx_wcsdup(const WCHAR
*src
);
193 #define wcsdup nx_wcsdup
200 * Extended tcsdup: returns NULL if NULL pointer passed
202 inline TCHAR
*_tcsdup_ex(const TCHAR
*s
)
204 return (s
!= NULL
) ?
_tcsdup(s
) : NULL
;
210 * String conversion helpers
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
);
224 #define UTF8StringFromTString(s) UTF8StringFromWideString(s)
226 #define UTF8StringFromTString(s) UTF8StringFromMBString(s)
230 * Class for serial communications
251 #ifndef INVALID_HANDLE_VALUE
252 #define INVALID_HANDLE_VALUE (-1)
256 class LIBNETXMS_EXPORTABLE Serial
269 struct termios m_originalSettings
;
278 bool open(const TCHAR
*pszPort
);
280 void setTimeout(int nTimeout
);
281 int read(char *pBuff
, int nSize
); /* waits up to timeout and do single read */
282 int readAll(char *pBuff
, int nSize
); /* read until timeout or out of space */
283 int readToMark(char *buff
, int size
, const char **marks
, char **occurence
);
284 bool write(const char *pBuff
, int nSize
);
286 bool set(int nSpeed
, int nDataBits
, int nParity
, int nStopBits
, int nFlowControl
= FLOW_NONE
);
291 * Dynamic string class
293 class LIBNETXMS_EXPORTABLE String
299 size_t m_allocationStep
;
302 static const int npos
;
305 String(const TCHAR
*init
);
306 String(const String
&src
);
309 TCHAR
*getBuffer() { return m_buffer
; }
310 void setBuffer(TCHAR
*buffer
);
312 size_t getAllocationStep() { return m_allocationStep
; }
313 void setAllocationStep(size_t step
) { m_allocationStep
= step
; }
315 const String
& operator =(const TCHAR
*str
);
316 const String
& operator =(const String
&src
);
317 const String
& operator +=(const TCHAR
*str
);
318 const String
& operator +=(const String
&str
);
319 operator const TCHAR
*() const { return CHECK_NULL_EX(m_buffer
); }
321 char *getUTF8String();
323 void append(const TCHAR
*str
) { if (str
!= NULL
) append(str
, _tcslen(str
)); }
324 void append(const TCHAR
*str
, size_t len
);
325 void append(const TCHAR c
) { append(&c
, 1); }
326 void append(INT32 n
);
327 void append(UINT32 n
);
328 void append(INT64 n
);
329 void append(UINT64 n
);
331 void appendPreallocated(TCHAR
*str
) { if (str
!= NULL
) { append(str
); free(str
); } }
333 void appendMBString(const char *str
, size_t len
, int nCodePage
);
334 void appendWideString(const WCHAR
*str
, size_t len
);
336 void appendFormattedString(const TCHAR
*format
, ...);
337 void appendFormattedStringV(const TCHAR
*format
, va_list args
);
341 size_t length() const { return m_length
; }
342 bool isEmpty() const { return m_length
== 0; }
344 wchar_t charAt(size_t pos
) const { return (pos
< m_length
) ? m_buffer
[pos
] : 0; }
346 TCHAR
*substring(int nStart
, int nLen
, TCHAR
*pszBuffer
= NULL
);
347 int find(const TCHAR
*str
, int nStart
= 0);
349 void escapeCharacter(int ch
, int esc
);
350 void replace(const TCHAR
*pszSrc
, const TCHAR
*pszDst
);
352 void shrink(int chars
= 1);
356 * Abstract iterator class (to be implemented by actual iterable class)
358 class LIBNETXMS_EXPORTABLE AbstractIterator
361 virtual ~AbstractIterator();
363 virtual bool hasNext() = 0;
364 virtual void *next() = 0;
365 virtual void remove() = 0;
369 * Iterator class (public interface for iteration)
371 template <class T
> class Iterator
374 AbstractIterator
*m_worker
;
377 Iterator(AbstractIterator
*worker
) { m_worker
= worker
; }
378 ~Iterator() { delete m_worker
; }
380 bool hasNext() { return m_worker
->hasNext(); }
381 T
*next() { return (T
*)m_worker
->next(); }
382 void remove() { m_worker
->remove(); }
386 * Dynamic array class
388 class LIBNETXMS_EXPORTABLE Array
394 size_t m_elementSize
;
398 void internalRemove(int index
, bool allowDestruction
);
399 void destroyObject(void *object
) { if (object
!= NULL
) m_objectDestructor(object
); }
402 bool m_storePointers
;
403 void (*m_objectDestructor
)(void *);
405 Array(void *data
, int initial
, int grow
, size_t elementSize
);
406 void *__getBuffer() const { return m_data
; }
409 Array(int initial
= 0, int grow
= 16, bool owner
= false);
412 int add(void *element
);
413 void *get(int index
) const { return ((index
>= 0) && (index
< m_size
)) ?
(m_storePointers ? m_data
[index
] : (void *)((char *)m_data
+ index
* m_elementSize
)): NULL
; }
414 int indexOf(void *element
) const;
415 void set(int index
, void *element
);
416 void replace(int index
, void *element
);
417 void remove(int index
) { internalRemove(index
, true); }
418 void remove(void *element
) { internalRemove(indexOf(element
), true); }
419 void unlink(int index
) { internalRemove(index
, false); }
420 void unlink(void *element
) { internalRemove(indexOf(element
), false); }
422 void sort(int (*cb
)(const void *, const void *));
423 void *find(const void *key
, int (*cb
)(const void *, const void *)) const;
425 int size() const { return m_size
; }
427 void setOwner(bool owner
) { m_objectOwner
= owner
; }
428 bool isOwner() const { return m_objectOwner
; }
432 * Array iterator class
434 class LIBNETXMS_EXPORTABLE ArrayIterator
: public AbstractIterator
441 ArrayIterator(Array
*array
);
443 virtual bool hasNext();
444 virtual void *next();
445 virtual void remove();
449 * Template class for dynamic array which holds objects
451 template <class T
> class ObjectArray
: public Array
454 static void destructor(void *object
) { delete (T
*)object
; }
457 ObjectArray(int initial
= 0, int grow
= 16, bool owner
= false) : Array(initial
, grow
, owner
) { m_objectDestructor
= destructor
; }
458 virtual ~ObjectArray() { }
460 int add(T
*object
) { return Array
::add((void *)object
); }
461 T
*get(int index
) const { return (T
*)Array
::get(index
); }
462 int indexOf(T
*object
) const { return Array
::indexOf((void *)object
); }
463 bool contains(T
*object
) const { return indexOf(object
) >= 0; }
464 void set(int index
, T
*object
) { Array
::set(index
, (void *)object
); }
465 void replace(int index
, T
*object
) { Array
::replace(index
, (void *)object
); }
466 void remove(int index
) { Array
::remove(index
); }
467 void remove(T
*object
) { Array
::remove((void *)object
); }
468 void unlink(int index
) { Array
::unlink(index
); }
469 void unlink(T
*object
) { Array
::unlink((void *)object
); }
471 Iterator
<T
> *iterator() { return new Iterator
<T
>(new ArrayIterator(this)); }
475 * Template class for dynamic array which holds scalar values
477 template <class T
> class IntegerArray
: public Array
480 static void destructor(void *element
) { }
483 IntegerArray(int initial
= 0, int grow
= 16) : Array(NULL
, initial
, grow
, sizeof(T
)) { m_objectDestructor
= destructor
; m_storePointers
= (sizeof(T
) == sizeof(void *)); }
484 virtual ~IntegerArray() { }
486 int add(T value
) { return Array
::add(m_storePointers ?
CAST_TO_POINTER(value
, void *) : &value
); }
487 T
get(int index
) const { if (m_storePointers
) return CAST_FROM_POINTER(Array
::get(index
), T
); T
*p
= (T
*)Array
::get(index
); return (p
!= NULL
) ?
*p
: 0; }
488 int indexOf(T value
) const { return Array
::indexOf(m_storePointers ?
CAST_TO_POINTER(value
, void *) : &value
); }
489 bool contains(T value
) const { return indexOf(value
) >= 0; }
490 void set(int index
, T value
) { Array
::set(index
, m_storePointers ?
CAST_TO_POINTER(value
, void *) : &value
); }
491 void replace(int index
, T value
) { Array
::replace(index
, m_storePointers ?
CAST_TO_POINTER(value
, void *) : &value
); }
493 T
*getBuffer() const { return (T
*)__getBuffer(); }
497 * Auxilliary class to hold dynamically allocated array of structures
499 template <class T
> class StructArray
: public Array
502 static void destructor(void *element
) { }
505 StructArray(int initial
= 0, int grow
= 16) : Array(NULL
, initial
, grow
, sizeof(T
)) { m_objectDestructor
= destructor
; }
506 StructArray(T
*data
, int size
) : Array(data
, size
, 16, sizeof(T
)) { m_objectDestructor
= destructor
; }
507 virtual ~StructArray() { }
509 int add(T
*element
) { return Array
::add((void *)element
); }
510 T
*get(int index
) const { return (T
*)Array
::get(index
); }
511 int indexOf(T
*element
) const { return Array
::indexOf((void *)element
); }
512 bool contains(T
*element
) const { return indexOf(element
) >= 0; }
513 void set(int index
, T
*element
) { Array
::set(index
, (void *)element
); }
514 void replace(int index
, T
*element
) { Array
::replace(index
, (void *)element
); }
515 void remove(int index
) { Array
::remove(index
); }
516 void remove(T
*element
) { Array
::remove((void *)element
); }
517 void unlink(int index
) { Array
::unlink(index
); }
518 void unlink(T
*element
) { Array
::unlink((void *)element
); }
520 T
*getBuffer() const { return (T
*)__getBuffer(); }
524 * Entry of string map (internal)
526 struct StringMapEntry
;
538 * String maps base class
540 class LIBNETXMS_EXPORTABLE StringMapBase
543 StringMapEntry
*m_data
;
546 void (*m_objectDestructor
)(void *);
548 StringMapEntry
*find(const TCHAR
*key
) const;
549 void setObject(TCHAR
*key
, void *value
, bool keyPreAlloc
);
550 void *getObject(const TCHAR
*key
) const;
551 void destroyObject(void *object
) { if (object
!= NULL
) m_objectDestructor(object
); }
554 StringMapBase(bool objectOwner
);
555 virtual ~StringMapBase();
557 void setOwner(bool owner
) { m_objectOwner
= owner
; }
558 void setIgnoreCase(bool ignore
);
560 void remove(const TCHAR
*key
);
562 void filterElements(bool (*filter
)(const TCHAR
*, const void *, void *), void *userData
);
565 bool contains(const TCHAR
*key
) const { return find(key
) != NULL
; }
567 EnumerationCallbackResult
forEach(EnumerationCallbackResult (*cb
)(const TCHAR
*, const void *, void *), void *userData
) const;
568 const void *findElement(bool (*comparator
)(const TCHAR
*, const void *, void *), void *userData
) const;
570 StructArray
<KeyValuePair
> *toArray() const;
571 StringList
*keys() const;
582 class LIBNETXMS_EXPORTABLE StringMap
: public StringMapBase
585 StringMap() : StringMapBase(true) { }
586 StringMap(const StringMap
&src
);
587 virtual ~StringMap();
589 StringMap
& operator =(const StringMap
&src
);
591 void set(const TCHAR
*key
, const TCHAR
*value
) { setObject((TCHAR
*)key
, _tcsdup(value
), false); }
592 void setPreallocated(TCHAR
*key
, TCHAR
*value
) { setObject(key
, value
, true); }
593 void set(const TCHAR
*key
, UINT32 value
);
595 void addAll(StringMap
*src
);
597 const TCHAR
*get(const TCHAR
*key
) const { return (const TCHAR
*)getObject(key
); }
598 UINT32
getULong(const TCHAR
*key
, UINT32 defaultValue
) const;
599 bool getBoolean(const TCHAR
*key
, bool defaultValue
) const;
601 void fillMessage(NXCPMessage
*msg
, UINT32 sizeFieldId
, UINT32 baseFieldId
) const;
602 void loadMessage(const NXCPMessage
*msg
, UINT32 sizeFieldId
, UINT32 baseFieldId
);
606 * String map template for holding objects as values
608 template <class T
> class StringObjectMap
: public StringMapBase
611 static void destructor(void *object
) { delete (T
*)object
; }
614 StringObjectMap(bool objectOwner
) : StringMapBase(objectOwner
) { m_objectDestructor
= destructor
; }
616 void set(const TCHAR
*key
, T
*object
) { setObject((TCHAR
*)key
, (void *)object
, false); }
617 T
*get(const TCHAR
*key
) const { return (T
*)getObject(key
); }
623 class LIBNETXMS_EXPORTABLE StringList
632 StringList(const StringList
*src
);
633 StringList(const TCHAR
*src
, const TCHAR
*separator
);
636 void add(const TCHAR
*value
);
637 void addPreallocated(TCHAR
*value
);
638 void add(INT32 value
);
639 void add(UINT32 value
);
640 void add(INT64 value
);
641 void add(UINT64 value
);
642 void add(double value
);
643 void replace(int index
, const TCHAR
*value
);
644 void addOrReplace(int index
, const TCHAR
*value
);
645 void addOrReplacePreallocated(int index
, TCHAR
*value
);
647 void addMBString(const char *value
) { addPreallocated(WideStringFromMBString(value
)); }
649 void addMBString(const char *value
) { add(value
); }
652 int size() const { return m_count
; }
653 const TCHAR
*get(int index
) const { return ((index
>=0) && (index
< m_count
)) ? m_values
[index
] : NULL
; }
654 int indexOf(const TCHAR
*value
) const;
655 int indexOfIgnoreCase(const TCHAR
*value
) const;
656 void remove(int index
);
657 void addAll(const StringList
*src
);
658 void merge(const StringList
*src
, bool matchCase
);
659 TCHAR
*join(const TCHAR
*separator
);
660 void splitAndAdd(const TCHAR
*src
, const TCHAR
*separator
);
661 void sort(bool ascending
= true, bool caseSensitive
= false);
662 void fillMessage(NXCPMessage
*msg
, UINT32 baseId
, UINT32 countId
);
666 * Entry of string set
668 struct StringSetEntry
;
678 class LIBNETXMS_EXPORTABLE StringSet
681 StringSetEntry
*m_data
;
687 void add(const TCHAR
*str
);
688 void addPreallocated(TCHAR
*str
);
689 void remove(const TCHAR
*str
);
693 bool contains(const TCHAR
*str
);
694 bool equals(StringSet
*s
);
696 void addAll(StringSet
*src
);
697 void addAll(TCHAR
**strings
, int count
);
698 void splitAndAdd(const TCHAR
*src
, const TCHAR
*separator
);
699 void addAllPreallocated(TCHAR
**strings
, int count
);
700 void forEach(bool (*cb
)(const TCHAR
*, void *), void *userData
);
702 void fillMessage(NXCPMessage
*msg
, UINT32 baseId
, UINT32 countId
);
703 void addAllFromMessage(NXCPMessage
*msg
, UINT32 baseId
, UINT32 countId
, bool clearBeforeAdd
, bool toUppercase
);
705 String
join(const TCHAR
*separator
);
709 * Opaque hash map entry structure
714 * Hash map base class (for fixed size non-pointer keys)
716 class LIBNETXMS_EXPORTABLE HashMapBase
718 friend class HashMapIterator
;
721 HashMapEntry
*m_data
;
723 unsigned int m_keylen
;
725 HashMapEntry
*find(const void *key
) const;
726 void destroyObject(void *object
) { if (object
!= NULL
) m_objectDestructor(object
); }
729 void (*m_objectDestructor
)(void *);
731 HashMapBase(bool objectOwner
, unsigned int keylen
);
733 void *_get(const void *key
) const;
734 void _set(const void *key
, void *value
);
735 void _remove(const void *key
);
737 bool _contains(const void *key
) const { return find(key
) != NULL
; }
740 virtual ~HashMapBase();
742 void setOwner(bool owner
) { m_objectOwner
= owner
; }
747 EnumerationCallbackResult
forEach(EnumerationCallbackResult (*cb
)(const void *, const void *, void *), void *userData
) const;
748 const void *findElement(bool (*comparator
)(const void *, const void *, void *), void *userData
) const;
754 class LIBNETXMS_EXPORTABLE HashMapIterator
: public AbstractIterator
757 HashMapBase
*m_hashMap
;
758 HashMapEntry
*m_curr
;
759 HashMapEntry
*m_next
;
762 HashMapIterator(HashMapBase
*hashMap
);
764 virtual bool hasNext();
765 virtual void *next();
766 virtual void remove();
770 * Hash map template for holding objects as values
772 template <class K
, class V
> class HashMap
: public HashMapBase
775 static void destructor(void *object
) { delete (V
*)object
; }
778 HashMap(bool objectOwner
= false) : HashMapBase(objectOwner
, sizeof(K
)) { m_objectDestructor
= destructor
; }
780 V
*get(const K
& key
) { return (V
*)_get(&key
); }
781 void set(const K
& key
, V
*value
) { _set(&key
, (void *)value
); }
782 void remove(const K
& key
) { _remove(&key
); }
783 bool contains(const K
& key
) { return _contains(&key
); }
785 Iterator
<V
> *iterator() { return new Iterator
<V
>(new HashMapIterator(this)); }
791 class LIBNETXMS_EXPORTABLE ByteStream
798 size_t m_allocationStep
;
801 ByteStream(size_t initial
= 8192);
802 ByteStream(const void *data
, size_t size
);
803 virtual ~ByteStream();
805 static ByteStream
*load(const TCHAR
*file
);
807 void seek(size_t pos
) { if (pos
<= m_size
) m_pos
= pos
; }
808 size_t pos() { return m_pos
; }
809 size_t size() { return m_size
; }
810 bool eos() { return m_pos
== m_size
; }
812 void setAllocationStep(size_t s
) { m_allocationStep
= s
; }
814 const BYTE
*buffer(size_t *size
) { *size
= m_size
; return m_data
; }
816 void write(const void *data
, size_t size
);
817 void write(char c
) { write(&c
, 1); }
818 void write(BYTE b
) { write(&b
, 1); }
819 void write(INT16 n
) { UINT16 x
= htons((UINT16
)n
); write(&x
, 2); }
820 void write(UINT16 n
) { UINT16 x
= htons(n
); write(&x
, 2); }
821 void write(INT32 n
) { UINT32 x
= htonl((UINT32
)n
); write(&x
, 4); }
822 void write(UINT32 n
) { UINT32 x
= htonl(n
); write(&x
, 4); }
823 void write(INT64 n
) { UINT64 x
= htonq((UINT64
)n
); write(&x
, 8); }
824 void write(UINT64 n
) { UINT64 x
= htonq(n
); write(&x
, 8); }
825 void write(double n
) { double x
= htond(n
); write(&x
, 8); }
826 void writeString(const TCHAR
*s
);
828 size_t read(void *buffer
, size_t count
);
829 char readChar() { return !eos() ?
(char)m_data
[m_pos
++] : 0; }
830 BYTE
readByte() { return !eos() ? m_data
[m_pos
++] : 0; }
844 * Auxilliary class for objects which counts references and
845 * destroys itself wheren reference count falls to 0
847 class LIBNETXMS_EXPORTABLE RefCountObject
850 VolatileCounter m_refCount
;
857 virtual ~RefCountObject();
861 InterlockedIncrement(&m_refCount
);
866 if (InterlockedDecrement(&m_refCount
) == 0)
874 * Table column definition
876 class LIBNETXMS_EXPORTABLE TableColumnDefinition
880 TCHAR
*m_displayName
;
882 bool m_instanceColumn
;
885 TableColumnDefinition(const TCHAR
*name
, const TCHAR
*displayName
, INT32 dataType
, bool isInstance
);
886 TableColumnDefinition(NXCPMessage
*msg
, UINT32 baseId
);
887 TableColumnDefinition(TableColumnDefinition
*src
);
888 ~TableColumnDefinition();
890 void fillMessage(NXCPMessage
*msg
, UINT32 baseId
);
892 const TCHAR
*getName() { return m_name
; }
893 const TCHAR
*getDisplayName() { return m_displayName
; }
894 INT32
getDataType() { return m_dataType
; }
895 bool isInstanceColumn() { return m_instanceColumn
; }
897 void setDataType(INT32 type
) { m_dataType
= type
; }
898 void setInstanceColumn(bool isInstance
) { m_instanceColumn
= isInstance
; }
899 void setDisplayName(const TCHAR
*name
) { safe_free(m_displayName
); m_displayName
= _tcsdup(CHECK_NULL_EX(name
)); }
913 TableCell() { m_value
= NULL
; m_status
= -1; m_objectId
= 0; }
914 TableCell(const TCHAR
*value
) { m_value
= _tcsdup_ex(value
); m_status
= -1; m_objectId
= 0; }
915 TableCell(const TCHAR
*value
, int status
) { m_value
= _tcsdup_ex(value
); m_status
= status
; m_objectId
= 0; }
916 TableCell(TableCell
*src
) { m_value
= _tcsdup_ex(src
->m_value
); m_status
= src
->m_status
; m_objectId
= src
->m_objectId
; }
917 ~TableCell() { safe_free(m_value
); }
919 void set(const TCHAR
*value
, int status
, UINT32 objectId
) { safe_free(m_value
); m_value
= _tcsdup_ex(value
); m_status
= status
; m_objectId
= objectId
; }
920 void setPreallocated(TCHAR
*value
, int status
, UINT32 objectId
) { safe_free(m_value
); m_value
= value
; m_status
= status
; m_objectId
= objectId
; }
922 const TCHAR
*getValue() { return m_value
; }
923 void setValue(const TCHAR
*value
) { safe_free(m_value
); m_value
= _tcsdup_ex(value
); }
924 void setPreallocatedValue(TCHAR
*value
) { safe_free(m_value
); m_value
= value
; }
926 int getStatus() { return m_status
; }
927 void setStatus(int status
) { m_status
= status
; }
929 int getObjectId() { return m_objectId
; }
930 void setObjectId(UINT32 id
) { m_objectId
= id
; }
939 ObjectArray
<TableCell
> *m_cells
;
943 TableRow(int columnCount
);
944 TableRow(TableRow
*src
);
945 ~TableRow() { delete m_cells
; }
947 void addColumn() { m_cells
->add(new TableCell
); }
948 void deleteColumn(int index
) { m_cells
->remove(index
); }
950 void set(int index
, const TCHAR
*value
, int status
, UINT32 objectId
) { TableCell
*c
= m_cells
->get(index
); if (c
!= NULL
) c
->set(value
, status
, objectId
); }
951 void setPreallocated(int index
, TCHAR
*value
, int status
, UINT32 objectId
) { TableCell
*c
= m_cells
->get(index
); if (c
!= NULL
) c
->setPreallocated(value
, status
, objectId
); }
953 void setValue(int index
, const TCHAR
*value
) { TableCell
*c
= m_cells
->get(index
); if (c
!= NULL
) c
->setValue(value
); }
954 void setPreallocatedValue(int index
, TCHAR
*value
) { TableCell
*c
= m_cells
->get(index
); if (c
!= NULL
) c
->setPreallocatedValue(value
); }
956 void setStatus(int index
, int status
) { TableCell
*c
= m_cells
->get(index
); if (c
!= NULL
) c
->setStatus(status
); }
958 const TCHAR
*getValue(int index
) { TableCell
*c
= m_cells
->get(index
); return (c
!= NULL
) ? c
->getValue() : NULL
; }
959 int getStatus(int index
) { TableCell
*c
= m_cells
->get(index
); return (c
!= NULL
) ? c
->getStatus() : -1; }
961 UINT32
getObjectId() { return m_objectId
; }
962 void setObjectId(UINT32 id
) { m_objectId
= id
; }
964 UINT32
getCellObjectId(int index
) { TableCell
*c
= m_cells
->get(index
); return (c
!= NULL
) ? c
->getObjectId() : 0; }
965 void setCellObjectId(int index
, UINT32 id
) { TableCell
*c
= m_cells
->get(index
); if (c
!= NULL
) c
->setObjectId(id
); }
969 * Class for table data storage
971 class LIBNETXMS_EXPORTABLE Table
: public RefCountObject
974 ObjectArray
<TableRow
> *m_data
;
975 ObjectArray
<TableColumnDefinition
> *m_columns
;
978 bool m_extendedFormat
;
980 void createFromMessage(NXCPMessage
*msg
);
982 bool parseXML(const char *xml
);
987 Table(NXCPMessage
*msg
);
990 int fillMessage(NXCPMessage
&msg
, int offset
, int rowLimit
);
991 void updateFromMessage(NXCPMessage
*msg
);
993 void addAll(Table
*src
);
994 void copyRow(Table
*src
, int row
);
996 int getNumRows() { return m_data
->size(); }
997 int getNumColumns() { return m_columns
->size(); }
998 const TCHAR
*getTitle() { return CHECK_NULL_EX(m_title
); }
999 int getSource() { return m_source
; }
1001 bool isExtendedFormat() { return m_extendedFormat
; }
1002 void setExtendedFormat(bool ext
) { m_extendedFormat
= ext
; }
1004 const TCHAR
*getColumnName(int col
) { return ((col
>= 0) && (col
< m_columns
->size())) ? m_columns
->get(col
)->getName() : NULL
; }
1005 INT32
getColumnDataType(int col
) { return ((col
>= 0) && (col
< m_columns
->size())) ? m_columns
->get(col
)->getDataType() : 0; }
1006 int getColumnIndex(const TCHAR
*name
);
1007 ObjectArray
<TableColumnDefinition
> *getColumnDefinitions() { return m_columns
; }
1009 void setTitle(const TCHAR
*title
) { safe_free(m_title
); m_title
= _tcsdup_ex(title
); }
1010 void setSource(int source
) { m_source
= source
; }
1011 int addColumn(const TCHAR
*name
, INT32 dataType
= 0, const TCHAR
*displayName
= NULL
, bool isInstance
= false);
1012 void setColumnDataType(int col
, INT32 dataType
) { if ((col
>= 0) && (col
< m_columns
->size())) m_columns
->get(col
)->setDataType(dataType
); }
1015 void deleteRow(int row
);
1016 void deleteColumn(int col
);
1018 void setAt(int nRow
, int nCol
, INT32 nData
);
1019 void setAt(int nRow
, int nCol
, UINT32 dwData
);
1020 void setAt(int nRow
, int nCol
, double dData
);
1021 void setAt(int nRow
, int nCol
, INT64 nData
);
1022 void setAt(int nRow
, int nCol
, UINT64 qwData
);
1023 void setAt(int nRow
, int nCol
, const TCHAR
*pszData
);
1024 void setPreallocatedAt(int nRow
, int nCol
, TCHAR
*pszData
);
1026 void set(int nCol
, INT32 nData
) { setAt(getNumRows() - 1, nCol
, nData
); }
1027 void set(int nCol
, UINT32 dwData
) { setAt(getNumRows() - 1, nCol
, dwData
); }
1028 void set(int nCol
, double dData
) { setAt(getNumRows() - 1, nCol
, dData
); }
1029 void set(int nCol
, INT64 nData
) { setAt(getNumRows() - 1, nCol
, nData
); }
1030 void set(int nCol
, UINT64 qwData
) { setAt(getNumRows() - 1, nCol
, qwData
); }
1031 void set(int nCol
, const TCHAR
*data
) { setAt(getNumRows() - 1, nCol
, data
); }
1033 void set(int nCol
, const char *data
) { setPreallocatedAt(getNumRows() - 1, nCol
, WideStringFromMBString(data
)); }
1035 void set(int nCol
, const WCHAR
*data
) { setPreallocatedAt(getNumRows() - 1, nCol
, MBStringFromWideString(data
)); }
1037 void setPreallocated(int nCol
, TCHAR
*data
) { setPreallocatedAt(getNumRows() - 1, nCol
, data
); }
1039 void setStatusAt(int row
, int col
, int status
);
1040 void setStatus(int col
, int status
) { setStatusAt(getNumRows() - 1, col
, status
); }
1042 const TCHAR
*getAsString(int nRow
, int nCol
);
1043 INT32
getAsInt(int nRow
, int nCol
);
1044 UINT32
getAsUInt(int nRow
, int nCol
);
1045 INT64
getAsInt64(int nRow
, int nCol
);
1046 UINT64
getAsUInt64(int nRow
, int nCol
);
1047 double getAsDouble(int nRow
, int nCol
);
1049 int getStatus(int nRow
, int nCol
);
1051 void buildInstanceString(int row
, TCHAR
*buffer
, size_t bufLen
);
1052 int findRowByInstance(const TCHAR
*instance
);
1054 UINT32
getObjectId(int row
) { TableRow
*r
= m_data
->get(row
); return (r
!= NULL
) ? r
->getObjectId() : 0; }
1055 void setObjectId(int row
, UINT32 id
) { TableRow
*r
= m_data
->get(row
); if (r
!= NULL
) r
->setObjectId(id
); }
1057 void setCellObjectIdAt(int row
, int col
, UINT32 objectId
);
1058 void setCellObjectId(int col
, UINT32 objectId
) { setCellObjectIdAt(getNumRows() - 1, col
, objectId
); }
1059 UINT32
getCellObjectId(int row
, int col
) { TableRow
*r
= m_data
->get(row
); return (r
!= NULL
) ? r
->getCellObjectId(col
) : 0; }
1061 static Table
*createFromXML(const char *xml
);
1068 union SockAddrBuffer
1070 struct sockaddr_in sa4
;
1072 struct sockaddr_in6 sa6
;
1077 * sockaddr length calculation
1080 #define SA_LEN(sa) (((sa)->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
1082 #define SA_LEN(sa) sizeof(struct sockaddr_in)
1086 * Get port number from SockAddrBuffer
1089 #define SA_PORT(sa) ((((struct sockaddr *)sa)->sa_family == AF_INET) ? ((struct sockaddr_in *)sa)->sin_port : ((struct sockaddr_in6 *)sa)->sin6_port)
1091 #define SA_PORT(sa) (((struct sockaddr_in *)sa)->sin_port)
1095 * Compare addresses in sockaddr
1097 inline bool SocketAddressEquals(struct sockaddr
*a1
, struct sockaddr
*a2
)
1099 if (a1
->sa_family
!= a2
->sa_family
)
1101 if (a1
->sa_family
== AF_INET
)
1102 return ((struct sockaddr_in
*)a1
)->sin_addr
.s_addr
== ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
;
1104 if (a1
->sa_family
== AF_INET6
)
1105 return !memcmp(((struct sockaddr_in6
*)a1
)->sin6_addr
.s6_addr
, ((struct sockaddr_in6
*)a2
)->sin6_addr
.s6_addr
, 16);
1113 class LIBNETXMS_EXPORTABLE InetAddress
1126 InetAddress(UINT32 addr
);
1127 InetAddress(UINT32 addr
, UINT32 mask
);
1128 InetAddress(const BYTE
*addr
, int maskBits
= 128);
1130 bool isAnyLocal() const;
1131 bool isLoopback() const;
1132 bool isMulticast() const;
1133 bool isBroadcast() const;
1134 bool isLinkLocal() const;
1135 bool isValid() const { return m_family
!= AF_UNSPEC
; }
1136 bool isValidUnicast() const { return isValid() && !isAnyLocal() && !isLoopback() && !isMulticast() && !isBroadcast() && !isLinkLocal(); }
1138 int getFamily() const { return m_family
; }
1139 UINT32
getAddressV4() const { return (m_family
== AF_INET
) ? m_addr
.v4
: 0; }
1140 const BYTE
*getAddressV6() const { return (m_family
== AF_INET6
) ? m_addr
.v6
: (const BYTE
*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; }
1142 bool contain(const InetAddress
&a
) const;
1143 bool sameSubnet(const InetAddress
&a
) const;
1144 bool equals(const InetAddress
&a
) const;
1145 int compareTo(const InetAddress
&a
) const;
1147 void setMaskBits(int m
) { m_maskBits
= m
; }
1148 int getMaskBits() const { return m_maskBits
; }
1149 int getHostBits() const { return (m_family
== AF_INET
) ?
(32 - m_maskBits
) : (128 - m_maskBits
); }
1151 InetAddress
getSubnetAddress() const;
1152 InetAddress
getSubnetBroadcast() const;
1153 bool isSubnetBroadcast(int maskBits
) const;
1155 String
toString() const;
1156 TCHAR
*toString(TCHAR
*buffer
) const;
1158 char *toStringA(char *buffer
) const;
1160 char *toStringA(char *buffer
) const { return toString(buffer
); }
1163 BYTE
*buildHashKey(BYTE
*key
) const;
1165 TCHAR
*getHostByAddr(TCHAR
*buffer
, size_t buflen
) const;
1167 struct sockaddr
*fillSockAddr(SockAddrBuffer
*buffer
, UINT16 port
= 0) const;
1169 static InetAddress
resolveHostName(const WCHAR
*hostname
, int af
= AF_INET
);
1170 static InetAddress
resolveHostName(const char *hostname
, int af
= AF_INET
);
1171 static InetAddress
parse(const WCHAR
*str
);
1172 static InetAddress
parse(const char *str
);
1173 static InetAddress
createFromSockaddr(struct sockaddr
*s
);
1175 static const InetAddress INVALID
;
1176 static const InetAddress LOOPBACK
;
1182 class LIBNETXMS_EXPORTABLE InetAddressList
1185 ObjectArray
<InetAddress
> *m_list
;
1187 int indexOf(const InetAddress
& addr
) const;
1193 void add(const InetAddress
& addr
);
1194 void add(const InetAddressList
& addrList
);
1195 void replace(const InetAddress
& addr
);
1196 void remove(const InetAddress
& addr
);
1197 void clear() { m_list
->clear(); }
1198 const InetAddress
& get(int index
) const { const InetAddress
*a
= m_list
->get(index
); return (a
!= NULL
) ?
*a
: InetAddress
::INVALID
; }
1200 int size() const { return m_list
->size(); }
1201 bool hasAddress(const InetAddress
& addr
) const { return indexOf(addr
) != -1; }
1202 const InetAddress
& findAddress(const InetAddress
& addr
) const { int idx
= indexOf(addr
); return (idx
!= -1) ?
*m_list
->get(idx
) : InetAddress
::INVALID
; }
1203 const InetAddress
& findSameSubnetAddress(const InetAddress
& addr
) const;
1204 const InetAddress
& getFirstUnicastAddress() const;
1205 const InetAddress
& getFirstUnicastAddressV4() const;
1206 bool hasValidUnicastAddress() const { return getFirstUnicastAddress().isValid(); }
1207 bool isLoopbackOnly() const;
1209 const ObjectArray
<InetAddress
> *getList() const { return m_list
; }
1211 void fillMessage(NXCPMessage
*msg
, UINT32 sizeFieldId
, UINT32 baseFieldId
) const;
1215 * Network connection
1217 class LIBNETXMS_EXPORTABLE SocketConnection
1226 virtual ~SocketConnection();
1228 bool connectTCP(const TCHAR
*hostName
, WORD port
, UINT32 timeout
);
1229 bool connectTCP(UINT32 ip
, WORD port
, UINT32 timeout
);
1232 bool canRead(UINT32 timeout
);
1233 virtual int read(char *pBuff
, int nSize
, UINT32 timeout
= INFINITE
);
1234 bool waitForText(const char *text
, int timeout
);
1236 int write(const char *pBuff
, int nSize
);
1237 bool writeLine(const char *line
);
1239 static SocketConnection
*createTCPConnection(const TCHAR
*hostName
, WORD port
, UINT32 timeout
);
1243 * Telnet connection - handles all telnet negotiation
1245 class LIBNETXMS_EXPORTABLE TelnetConnection
: public SocketConnection
1248 bool connectTCP(const TCHAR
*hostName
, WORD port
, UINT32 timeout
);
1249 bool connectTCP(UINT32 ip
, WORD port
, UINT32 timeout
);
1252 static TelnetConnection
*createConnection(const TCHAR
*hostName
, WORD port
, UINT32 timeout
);
1254 bool connect(const TCHAR
*hostName
, WORD port
, UINT32 timeout
);
1255 bool connect(UINT32 ip
, WORD port
, UINT32 timeout
);
1256 virtual int read(char *pBuff
, int nSize
, UINT32 timeout
= INFINITE
);
1257 int readLine(char *buffer
, int size
, UINT32 timeout
= INFINITE
);
1261 * Postal address representation
1263 class LIBNETXMS_EXPORTABLE PostalAddress
1268 TCHAR
*m_streetAddress
;
1273 PostalAddress(const TCHAR
*country
, const TCHAR
*city
, const TCHAR
*streetAddress
, const TCHAR
*postcode
);
1276 const TCHAR
*getCountry() { return CHECK_NULL_EX(m_country
); }
1277 const TCHAR
*getCity() { return CHECK_NULL_EX(m_city
); }
1278 const TCHAR
*getStreetAddress() { return CHECK_NULL_EX(m_streetAddress
); }
1279 const TCHAR
*getPostCode() { return CHECK_NULL_EX(m_postcode
); }
1281 void setCountry(const TCHAR
*country
) { safe_free(m_country
); m_country
= _tcsdup_ex(country
); }
1282 void setCity(const TCHAR
*city
) { safe_free(m_city
); m_city
= _tcsdup_ex(city
); }
1283 void setStreetAddress(const TCHAR
*streetAddress
) { safe_free(m_streetAddress
); m_streetAddress
= _tcsdup_ex(streetAddress
); }
1284 void setPostCode(const TCHAR
*postcode
) { safe_free(m_postcode
); m_postcode
= _tcsdup_ex(postcode
); }
1287 #endif /* __cplusplus */
1290 * Configuration item template for configuration loader
1296 BYTE separator
; // Separator character for lists
1297 WORD listElements
; // Number of list elements, should be set to 0 before calling NxLoadConfig()
1298 UINT64 bufferSize
; // Buffer size for strings or flag to be set for CT_BOOLEAN
1299 UINT32 bufferPos
; // Should be set to 0
1301 void *overrideIndicator
;
1305 * Code translation structure
1307 typedef struct __CODE_TO_TEXT
1314 * getopt() prototype if needed
1316 #if USE_BUNDLED_GETOPT
1317 #include <netxms_getopt.h>
1321 // Structures for opendir() / readdir() / closedir()
1326 typedef struct dirent
1328 long d_ino
; /* inode number (not used by MS-DOS) */
1329 int d_namlen
; /* Name length */
1330 char d_name
[257]; /* file name */
1333 typedef struct _dir_struc
1335 char *start
; /* Starting position */
1336 char *curr
; /* Current position */
1337 long size
; /* Size of string table */
1338 long nfiles
; /* number if filenames in table */
1339 struct dirent dirstr
; /* Directory structure to return */
1344 typedef struct dirent_w
1346 long d_ino
; /* inode number (not used by MS-DOS) */
1347 int d_namlen
; /* Name length */
1348 WCHAR d_name
[257]; /* file name */
1351 typedef struct _dir_struc_w
1353 WCHAR
*start
; /* Starting position */
1354 WCHAR
*curr
; /* Current position */
1355 long size
; /* Size of string table */
1356 long nfiles
; /* number if filenames in table */
1357 struct dirent_w dirstr
; /* Directory structure to return */
1361 #define _TDIRECT _DIRECTW
1362 #define _tdirent dirent_w
1367 #define _TDIRECT _DIRECT
1368 #define _tdirent dirent
1372 #else /* not _WIN32 */
1374 typedef struct dirent_w
1376 long d_ino
; /* inode number */
1377 WCHAR d_name
[257]; /* file name */
1380 typedef struct _dir_struc_w
1382 DIR *dir
; /* Original non-unicode structure */
1383 struct dirent_w dirstr
; /* Directory structure to return */
1395 inline TCHAR
*nx_strncpy(TCHAR
*pszDest
, const TCHAR
*pszSrc
, size_t nLen
)
1397 #if defined(_WIN32) && (_MSC_VER >= 1400)
1398 _tcsncpy_s(pszDest
, nLen
, pszSrc
, _TRUNCATE
);
1400 _tcsncpy(pszDest
, pszSrc
, nLen
- 1);
1401 pszDest
[nLen
- 1] = 0;
1407 inline char *nx_strncpy_mb(char *pszDest
, const char *pszSrc
, size_t nLen
)
1409 #if defined(_WIN32) && (_MSC_VER >= 1400)
1410 strncpy_s(pszDest
, nLen
, pszSrc
, _TRUNCATE
);
1412 strncpy(pszDest
, pszSrc
, nLen
- 1);
1413 pszDest
[nLen
- 1] = 0;
1418 #define nx_strncpy_mb nx_strncpy
1421 int LIBNETXMS_EXPORTABLE
ConnectEx(SOCKET s
, struct sockaddr
*addr
, int len
, UINT32 timeout
);
1422 int LIBNETXMS_EXPORTABLE
SendEx(SOCKET hSocket
, const void *data
, size_t len
, int flags
, MUTEX mutex
);
1423 int LIBNETXMS_EXPORTABLE
RecvEx(SOCKET hSocket
, void *data
, size_t len
, int flags
, UINT32 timeout
);
1424 bool LIBNETXMS_EXPORTABLE
RecvAll(SOCKET s
, void *buffer
, size_t size
, UINT32 timeout
);
1426 SOCKET LIBNETXMS_EXPORTABLE
ConnectToHost(const InetAddress
& addr
, UINT16 port
, UINT32 timeout
);
1428 #endif /* __cplusplus */
1435 #if !defined(_WIN32) && !defined(_NETWARE)
1436 #if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
1437 void LIBNETXMS_EXPORTABLE
__wcsupr(WCHAR
*in
);
1438 #define wcsupr __wcsupr
1440 void LIBNETXMS_EXPORTABLE
__strupr(char *in
);
1441 #define strupr __strupr
1444 void LIBNETXMS_EXPORTABLE
QSortEx(void *base
, size_t nmemb
, size_t size
, void *arg
,
1445 int (*compare
)(const void *, const void *, void *));
1447 INT64 LIBNETXMS_EXPORTABLE
GetCurrentTimeMs();
1449 UINT64 LIBNETXMS_EXPORTABLE
FileSizeW(const WCHAR
*pszFileName
);
1450 UINT64 LIBNETXMS_EXPORTABLE
FileSizeA(const char *pszFileName
);
1452 #define FileSize FileSizeW
1454 #define FileSize FileSizeA
1457 int LIBNETXMS_EXPORTABLE
BitsInMask(UINT32 dwMask
);
1458 TCHAR LIBNETXMS_EXPORTABLE
*IpToStr(UINT32 dwAddr
, TCHAR
*szBuffer
);
1460 char LIBNETXMS_EXPORTABLE
*IpToStrA(UINT32 dwAddr
, char *szBuffer
);
1462 #define IpToStrA IpToStr
1464 TCHAR LIBNETXMS_EXPORTABLE
*Ip6ToStr(const BYTE
*addr
, TCHAR
*buffer
);
1466 char LIBNETXMS_EXPORTABLE
*Ip6ToStrA(const BYTE
*addr
, char *buffer
);
1468 #define Ip6ToStrA Ip6ToStr
1470 TCHAR LIBNETXMS_EXPORTABLE
*SockaddrToStr(struct sockaddr
*addr
, TCHAR
*buffer
);
1472 UINT32 LIBNETXMS_EXPORTABLE
ResolveHostNameA(const char *name
);
1473 UINT32 LIBNETXMS_EXPORTABLE
ResolveHostNameW(const WCHAR
*name
);
1475 #define ResolveHostName ResolveHostNameW
1477 #define ResolveHostName ResolveHostNameA
1480 void LIBNETXMS_EXPORTABLE
*nx_memdup(const void *data
, size_t size
);
1481 void LIBNETXMS_EXPORTABLE
nx_memswap(void *block1
, void *block2
, size_t size
);
1483 WCHAR LIBNETXMS_EXPORTABLE
*BinToStrW(const BYTE
*data
, size_t size
, WCHAR
*pStr
);
1484 char LIBNETXMS_EXPORTABLE
*BinToStrA(const BYTE
*data
, size_t size
, char *pStr
);
1486 #define BinToStr BinToStrW
1488 #define BinToStr BinToStrA
1491 size_t LIBNETXMS_EXPORTABLE
StrToBinW(const WCHAR
*pStr
, BYTE
*data
, size_t size
);
1492 size_t LIBNETXMS_EXPORTABLE
StrToBinA(const char *pStr
, BYTE
*data
, size_t size
);
1494 #define StrToBin StrToBinW
1496 #define StrToBin StrToBinA
1499 TCHAR LIBNETXMS_EXPORTABLE
*MACToStr(const BYTE
*data
, TCHAR
*pStr
);
1501 void LIBNETXMS_EXPORTABLE
StrStripA(char *pszStr
);
1502 void LIBNETXMS_EXPORTABLE
StrStripW(WCHAR
*pszStr
);
1504 #define StrStrip StrStripW
1506 #define StrStrip StrStripA
1509 const char LIBNETXMS_EXPORTABLE
*ExtractWordA(const char *line
, char *buffer
);
1510 const WCHAR LIBNETXMS_EXPORTABLE
*ExtractWordW(const WCHAR
*line
, WCHAR
*buffer
);
1512 #define ExtractWord ExtractWordW
1514 #define ExtractWord ExtractWordA
1517 int LIBNETXMS_EXPORTABLE
NumCharsA(const char *pszStr
, char ch
);
1518 int LIBNETXMS_EXPORTABLE
NumCharsW(const WCHAR
*pszStr
, WCHAR ch
);
1520 #define NumChars NumCharsW
1522 #define NumChars NumCharsA
1525 void LIBNETXMS_EXPORTABLE
RemoveTrailingCRLFA(char *str
);
1526 void LIBNETXMS_EXPORTABLE
RemoveTrailingCRLFW(WCHAR
*str
);
1528 #define RemoveTrailingCRLF RemoveTrailingCRLFW
1530 #define RemoveTrailingCRLF RemoveTrailingCRLFA
1533 BOOL LIBNETXMS_EXPORTABLE
RegexpMatchA(const char *str
, const char *expr
, bool matchCase
);
1534 BOOL LIBNETXMS_EXPORTABLE
RegexpMatchW(const WCHAR
*str
, const WCHAR
*expr
, bool matchCase
);
1536 #define RegexpMatch RegexpMatchW
1538 #define RegexpMatch RegexpMatchA
1541 const TCHAR LIBNETXMS_EXPORTABLE
*ExpandFileName(const TCHAR
*name
, TCHAR
*buffer
, size_t bufSize
, bool allowShellCommand
);
1542 BOOL LIBNETXMS_EXPORTABLE
CreateFolder(const TCHAR
*directory
);
1543 TCHAR LIBNETXMS_EXPORTABLE
*Trim(TCHAR
*str
);
1544 bool LIBNETXMS_EXPORTABLE
MatchString(const TCHAR
*pattern
, const TCHAR
*str
, bool matchCase
);
1545 TCHAR LIBNETXMS_EXPORTABLE
**SplitString(const TCHAR
*source
, TCHAR sep
, int *numStrings
);
1546 int LIBNETXMS_EXPORTABLE
GetLastMonthDay(struct tm
*currTime
);
1548 bool LIBNETXMS_EXPORTABLE
MatchScheduleElement(TCHAR
*pszPattern
, int nValue
, int maxValue
, struct tm
*localTime
, time_t currTime
= 0);
1550 bool LIBNETXMS_EXPORTABLE
MatchScheduleElement(TCHAR
*pszPattern
, int nValue
, int maxValue
, struct tm
*localTime
, time_t currTime
);
1555 BOOL LIBNETXMS_EXPORTABLE
IsValidObjectName(const TCHAR
*pszName
, BOOL bExtendedChars
= FALSE
);
1557 BOOL LIBNETXMS_EXPORTABLE
IsValidScriptName(const TCHAR
*pszName
);
1558 /* deprecated */ void LIBNETXMS_EXPORTABLE
TranslateStr(TCHAR
*pszString
, const TCHAR
*pszSubStr
, const TCHAR
*pszReplace
);
1559 const TCHAR LIBNETXMS_EXPORTABLE
*GetCleanFileName(const TCHAR
*pszFileName
);
1560 void LIBNETXMS_EXPORTABLE
GetOSVersionString(TCHAR
*pszBuffer
, int nBufSize
);
1561 BYTE LIBNETXMS_EXPORTABLE
*LoadFile(const TCHAR
*pszFileName
, UINT32
*pdwFileSize
);
1563 BYTE LIBNETXMS_EXPORTABLE
*LoadFileA(const char *pszFileName
, UINT32
*pdwFileSize
);
1565 #define LoadFileA LoadFile
1568 UINT32 LIBNETXMS_EXPORTABLE
CalculateCRC32(const unsigned char *data
, UINT32 size
, UINT32 dwCRC
);
1569 void LIBNETXMS_EXPORTABLE
CalculateMD5Hash(const unsigned char *data
, size_t nbytes
, unsigned char *hash
);
1570 void LIBNETXMS_EXPORTABLE
MD5HashForPattern(const unsigned char *data
, size_t patternSize
, size_t fullSize
, BYTE
*hash
);
1571 void LIBNETXMS_EXPORTABLE
CalculateSHA1Hash(unsigned char *data
, size_t nbytes
, unsigned char *hash
);
1572 void LIBNETXMS_EXPORTABLE
SHA1HashForPattern(unsigned char *data
, size_t patternSize
, size_t fullSize
, unsigned char *hash
);
1573 void LIBNETXMS_EXPORTABLE
CalculateSHA256Hash(const unsigned char *data
, size_t len
, unsigned char *hash
);
1574 BOOL LIBNETXMS_EXPORTABLE
CalculateFileMD5Hash(const TCHAR
*pszFileName
, BYTE
*pHash
);
1575 BOOL LIBNETXMS_EXPORTABLE
CalculateFileSHA1Hash(const TCHAR
*pszFileName
, BYTE
*pHash
);
1576 BOOL LIBNETXMS_EXPORTABLE
CalculateFileCRC32(const TCHAR
*pszFileName
, UINT32
*pResult
);
1578 void LIBNETXMS_EXPORTABLE
GenerateRandomBytes(BYTE
*buffer
, size_t size
);
1580 void LIBNETXMS_EXPORTABLE
ICEEncryptData(const BYTE
*in
, int inLen
, BYTE
*out
, const BYTE
*key
);
1581 void LIBNETXMS_EXPORTABLE
ICEDecryptData(const BYTE
*in
, int inLen
, BYTE
*out
, const BYTE
*key
);
1583 bool LIBNETXMS_EXPORTABLE
DecryptPassword(const TCHAR
*login
, const TCHAR
*encryptedPasswd
, TCHAR
*decryptedPasswd
, size_t bufferLenght
);
1585 int LIBNETXMS_EXPORTABLE
NxDCIDataTypeFromText(const TCHAR
*pszText
);
1587 HMODULE LIBNETXMS_EXPORTABLE
DLOpen(const TCHAR
*pszLibName
, TCHAR
*pszErrorText
);
1588 void LIBNETXMS_EXPORTABLE
DLClose(HMODULE hModule
);
1589 void LIBNETXMS_EXPORTABLE
*DLGetSymbolAddr(HMODULE hModule
, const char *pszSymbol
, TCHAR
*pszErrorText
);
1591 bool LIBNETXMS_EXPORTABLE
ExtractNamedOptionValueW(const WCHAR
*optString
, const WCHAR
*option
, WCHAR
*buffer
, int bufSize
);
1592 bool LIBNETXMS_EXPORTABLE
ExtractNamedOptionValueAsBoolW(const WCHAR
*optString
, const WCHAR
*option
, bool defVal
);
1593 long LIBNETXMS_EXPORTABLE
ExtractNamedOptionValueAsIntW(const WCHAR
*optString
, const WCHAR
*option
, long defVal
);
1595 bool LIBNETXMS_EXPORTABLE
ExtractNamedOptionValueA(const char *optString
, const char *option
, char *buffer
, int bufSize
);
1596 bool LIBNETXMS_EXPORTABLE
ExtractNamedOptionValueAsBoolA(const char *optString
, const char *option
, bool defVal
);
1597 long LIBNETXMS_EXPORTABLE
ExtractNamedOptionValueAsIntA(const char *optString
, const char *option
, long defVal
);
1600 #define ExtractNamedOptionValue ExtractNamedOptionValueW
1601 #define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1602 #define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1604 #define ExtractNamedOptionValue ExtractNamedOptionValueA
1605 #define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1606 #define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1610 const TCHAR LIBNETXMS_EXPORTABLE
*CodeToText(int iCode
, CODE_TO_TEXT
*pTranslator
, const TCHAR
*pszDefaultText
= _T("Unknown"));
1612 const TCHAR LIBNETXMS_EXPORTABLE
*CodeToText(int iCode
, CODE_TO_TEXT
*pTranslator
, const TCHAR
*pszDefaultText
);
1616 TCHAR LIBNETXMS_EXPORTABLE
*GetSystemErrorText(UINT32 dwError
, TCHAR
*pszBuffer
, size_t iBufSize
);
1617 BOOL LIBNETXMS_EXPORTABLE
GetWindowsVersionString(TCHAR
*versionString
, int strSize
);
1618 INT64 LIBNETXMS_EXPORTABLE
GetProcessRSS();
1621 #if !HAVE_DAEMON || !HAVE_DECL_DAEMON
1622 int LIBNETXMS_EXPORTABLE
__daemon(int nochdir
, int noclose
);
1623 #define daemon __daemon
1626 UINT32 LIBNETXMS_EXPORTABLE
inet_addr_w(const WCHAR
*pszAddr
);
1629 BOOL LIBNETXMS_EXPORTABLE
SetDefaultCodepage(const char *cp
);
1630 int LIBNETXMS_EXPORTABLE
WideCharToMultiByte(int iCodePage
, UINT32 dwFlags
, const WCHAR
*pWideCharStr
,
1631 int cchWideChar
, char *pByteStr
, int cchByteChar
,
1632 char *pDefaultChar
, BOOL
*pbUsedDefChar
);
1633 int LIBNETXMS_EXPORTABLE
MultiByteToWideChar(int iCodePage
, UINT32 dwFlags
, const char *pByteStr
,
1634 int cchByteChar
, WCHAR
*pWideCharStr
,
1637 #if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
1638 int LIBNETXMS_EXPORTABLE
ucs2_strlen(const UCS2CHAR
*pStr
);
1640 #if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
1641 UCS2CHAR LIBNETXMS_EXPORTABLE
*ucs2_strncpy(UCS2CHAR
*pDst
, const UCS2CHAR
*pSrc
, int nDstLen
);
1643 #if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
1644 UCS2CHAR LIBNETXMS_EXPORTABLE
*ucs2_strdup(const UCS2CHAR
*pStr
);
1647 size_t LIBNETXMS_EXPORTABLE
ucs2_to_mb(const UCS2CHAR
*src
, int srcLen
, char *dst
, int dstLen
);
1648 size_t LIBNETXMS_EXPORTABLE
mb_to_ucs2(const char *src
, int srcLen
, UCS2CHAR
*dst
, int dstLen
);
1649 UCS2CHAR LIBNETXMS_EXPORTABLE
*UCS2StringFromMBString(const char *pszString
);
1650 char LIBNETXMS_EXPORTABLE
*MBStringFromUCS2String(const UCS2CHAR
*pszString
);
1652 int LIBNETXMS_EXPORTABLE
nx_wprintf(const WCHAR
*format
, ...);
1653 int LIBNETXMS_EXPORTABLE
nx_fwprintf(FILE *fp
, const WCHAR
*format
, ...);
1654 int LIBNETXMS_EXPORTABLE
nx_swprintf(WCHAR
*buffer
, size_t size
, const WCHAR
*format
, ...);
1655 int LIBNETXMS_EXPORTABLE
nx_vwprintf(const WCHAR
*format
, va_list args
);
1656 int LIBNETXMS_EXPORTABLE
nx_vfwprintf(FILE *fp
, const WCHAR
*format
, va_list args
);
1657 int LIBNETXMS_EXPORTABLE
nx_vswprintf(WCHAR
*buffer
, size_t size
, const WCHAR
*format
, va_list args
);
1659 int LIBNETXMS_EXPORTABLE
nx_wscanf(const WCHAR
*format
, ...);
1660 int LIBNETXMS_EXPORTABLE
nx_fwscanf(FILE *fp
, const WCHAR
*format
, ...);
1661 int LIBNETXMS_EXPORTABLE
nx_swscanf(const WCHAR
*str
, const WCHAR
*format
, ...);
1662 int LIBNETXMS_EXPORTABLE
nx_vwscanf(const WCHAR
*format
, va_list args
);
1663 int LIBNETXMS_EXPORTABLE
nx_vfwscanf(FILE *fp
, const WCHAR
*format
, va_list args
);
1664 int LIBNETXMS_EXPORTABLE
nx_vswscanf(const WCHAR
*str
, const WCHAR
*format
, va_list args
);
1668 #ifdef _WITH_ENCRYPTION
1669 WCHAR LIBNETXMS_EXPORTABLE
*ERR_error_string_W(int nError
, WCHAR
*pwszBuffer
);
1673 size_t LIBNETXMS_EXPORTABLE
ucs2_to_ucs4(const UCS2CHAR
*src
, int srcLen
, WCHAR
*dst
, int dstLen
);
1674 size_t LIBNETXMS_EXPORTABLE
ucs4_to_ucs2(const WCHAR
*src
, int srcLen
, UCS2CHAR
*dst
, int dstLen
);
1675 size_t LIBNETXMS_EXPORTABLE
ucs2_to_utf8(const UCS2CHAR
*src
, int srcLen
, char *dst
, int dstLen
);
1676 UCS2CHAR LIBNETXMS_EXPORTABLE
*UCS2StringFromUCS4String(const WCHAR
*pwszString
);
1677 WCHAR LIBNETXMS_EXPORTABLE
*UCS4StringFromUCS2String(const UCS2CHAR
*pszString
);
1680 size_t LIBNETXMS_EXPORTABLE
utf8_to_mb(const char *src
, int srcLen
, char *dst
, int dstLen
);
1681 size_t LIBNETXMS_EXPORTABLE
mb_to_utf8(const char *src
, int srcLen
, char *dst
, int dstLen
);
1682 char LIBNETXMS_EXPORTABLE
*MBStringFromUTF8String(const char *s
);
1683 char LIBNETXMS_EXPORTABLE
*UTF8StringFromMBString(const char *s
);
1685 #if !defined(_WIN32) && !HAVE_WSTAT
1686 int wstat(const WCHAR
*_path
, struct stat
*_sbuf
);
1689 #if defined(UNICODE) && !defined(_WIN32)
1692 FILE LIBNETXMS_EXPORTABLE
*wpopen(const WCHAR
*_command
, const WCHAR
*_type
);
1695 FILE LIBNETXMS_EXPORTABLE
*wfopen(const WCHAR
*_name
, const WCHAR
*_type
);
1697 #if HAVE_FOPEN64 && !HAVE_WFOPEN64
1698 FILE LIBNETXMS_EXPORTABLE
*wfopen64(const WCHAR
*_name
, const WCHAR
*_type
);
1701 int LIBNETXMS_EXPORTABLE
wopen(const WCHAR
*, int, ...);
1704 int LIBNETXMS_EXPORTABLE
wchmod(const WCHAR
*_name
, int mode
);
1707 int wchdir(const WCHAR
*_path
);
1710 int wmkdir(const WCHAR
*_path
, int mode
);
1713 int wrmdir(const WCHAR
*_path
);
1716 int wrename(const WCHAR
*_oldpath
, const WCHAR
*_newpath
);
1719 int wunlink(const WCHAR
*_path
);
1722 int wremove(const WCHAR
*_path
);
1725 int wsystem(const WCHAR
*_cmd
);
1728 int wmkstemp(WCHAR
*_template
);
1731 int waccess(const WCHAR
*_path
, int mode
);
1734 WCHAR
*wgetenv(const WCHAR
*_string
);
1737 WCHAR
*wctime(const time_t *timep
);
1740 int putws(const WCHAR
*s
);
1742 #if !HAVE_WCSERROR && (HAVE_STRERROR || HAVE_DECL_STRERROR)
1743 WCHAR
*wcserror(int errnum
);
1745 #if !HAVE_WCSERROR_R && HAVE_STRERROR_R
1746 #if HAVE_POSIX_STRERROR_R
1747 int wcserror_r(int errnum
, WCHAR
*strerrbuf
, size_t buflen
);
1749 WCHAR
*wcserror_r(int errnum
, WCHAR
*strerrbuf
, size_t buflen
);
1753 #endif /* UNICODE && !_WIN32*/
1756 INT64 LIBNETXMS_EXPORTABLE
strtoll(const char *nptr
, char **endptr
, int base
);
1759 UINT64 LIBNETXMS_EXPORTABLE
strtoull(const char *nptr
, char **endptr
, int base
);
1763 INT64 LIBNETXMS_EXPORTABLE
wcstoll(const WCHAR
*nptr
, WCHAR
**endptr
, int base
);
1766 UINT64 LIBNETXMS_EXPORTABLE
wcstoull(const WCHAR
*nptr
, WCHAR
**endptr
, int base
);
1769 #if !HAVE_WCSLWR && !defined(_WIN32)
1770 WCHAR LIBNETXMS_EXPORTABLE
*wcslwr(WCHAR
*str
);
1773 #if !HAVE_WCSCASECMP && !defined(_WIN32)
1774 int LIBNETXMS_EXPORTABLE
wcscasecmp(const wchar_t *s1
, const wchar_t *s2
);
1777 #if !HAVE_WCSNCASECMP && !defined(_WIN32)
1778 int LIBNETXMS_EXPORTABLE
wcsncasecmp(const wchar_t *s1
, const wchar_t *s2
, size_t n
);
1781 #if !defined(_WIN32) && (!HAVE_WCSFTIME || !WORKING_WCSFTIME)
1782 size_t LIBNETXMS_EXPORTABLE
nx_wcsftime(WCHAR
*buffer
, size_t bufsize
, const WCHAR
*format
, const struct tm
*t
);
1784 #define wcsftime nx_wcsftime
1789 #if HAVE_ITOA && !HAVE__ITOA
1792 #define HAVE__ITOA 1
1794 #if !HAVE__ITOA && !defined(_WIN32)
1795 char LIBNETXMS_EXPORTABLE
*_itoa(int value
, char *str
, int base
);
1798 #if HAVE_ITOW && !HAVE__ITOW
1801 #define HAVE__ITOW 1
1803 #if !HAVE__ITOW && !defined(_WIN32)
1804 WCHAR LIBNETXMS_EXPORTABLE
*_itow(int value
, WCHAR
*str
, int base
);
1811 DIRW LIBNETXMS_EXPORTABLE
*wopendir(const WCHAR
*filename
);
1812 struct dirent_w LIBNETXMS_EXPORTABLE
*wreaddir(DIRW
*dirp
);
1813 int LIBNETXMS_EXPORTABLE
wclosedir(DIRW
*dirp
);
1815 #define _topendir wopendir
1816 #define _treaddir wreaddir
1817 #define _tclosedir wclosedir
1819 #define _topendir opendir
1820 #define _treaddir readdir
1821 #define _tclosedir closedir
1824 DIR LIBNETXMS_EXPORTABLE
*opendir(const char *filename
);
1825 struct dirent LIBNETXMS_EXPORTABLE
*readdir(DIR *dirp
);
1826 int LIBNETXMS_EXPORTABLE
closedir(DIR *dirp
);
1828 #else /* not _WIN32 */
1830 DIRW LIBNETXMS_EXPORTABLE
*wopendir(const WCHAR
*filename
);
1831 struct dirent_w LIBNETXMS_EXPORTABLE
*wreaddir(DIRW
*dirp
);
1832 int LIBNETXMS_EXPORTABLE
wclosedir(DIRW
*dirp
);
1836 #if defined(_WIN32) || !(HAVE_SCANDIR)
1837 int LIBNETXMS_EXPORTABLE
scandir(const char *dir
, struct dirent
***namelist
,
1838 int (*select
)(const struct dirent
*),
1839 int (*compar
)(const struct dirent
**, const struct dirent
**));
1840 int LIBNETXMS_EXPORTABLE
alphasort(const struct dirent
**a
, const struct dirent
**b
);
1843 TCHAR LIBNETXMS_EXPORTABLE
*safe_fgetts(TCHAR
*buffer
, int len
, FILE *f
);
1845 BOOL LIBNETXMS_EXPORTABLE
nxlog_open(const TCHAR
*logName
, UINT32 flags
, const TCHAR
*msgModule
,
1846 unsigned int msgCount
, const TCHAR
**messages
);
1847 void LIBNETXMS_EXPORTABLE
nxlog_close(void);
1848 void LIBNETXMS_EXPORTABLE
nxlog_write(DWORD msg
, WORD wType
, const char *format
, ...);
1849 BOOL LIBNETXMS_EXPORTABLE
nxlog_set_rotation_policy(int rotationMode
, int maxLogSize
, int historySize
, const TCHAR
*dailySuffix
);
1850 BOOL LIBNETXMS_EXPORTABLE
nxlog_rotate();
1852 typedef void (*NxLogConsoleWriter
)(const TCHAR
*, ...);
1853 void LIBNETXMS_EXPORTABLE
nxlog_set_console_writer(NxLogConsoleWriter writer
);
1855 void LIBNETXMS_EXPORTABLE
WriteToTerminal(const TCHAR
*text
);
1856 void LIBNETXMS_EXPORTABLE
WriteToTerminalEx(const TCHAR
*format
, ...)
1857 #if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
1858 __attribute__ ((format(printf
, 1, 2)))
1863 int LIBNETXMS_EXPORTABLE
mkstemp(char *tmpl
);
1864 int LIBNETXMS_EXPORTABLE
wmkstemp(WCHAR
*tmpl
);
1866 #define _tmkstemp wmkstemp
1868 #define _tmkstemp mkstemp
1873 int strcat_s(char *dst
, size_t dstSize
, const char *src
);
1874 int wcscat_s(WCHAR
*dst
, size_t dstSize
, const WCHAR
*src
);
1878 char LIBNETXMS_EXPORTABLE
*strptime(const char *buf
, const char *fmt
, struct tm
*_tm
);
1882 time_t LIBNETXMS_EXPORTABLE
timegm(struct tm
*_tm
);
1886 int LIBNETXMS_EXPORTABLE
nx_inet_pton(int af
, const char *src
, void *dst
);
1887 #define inet_pton nx_inet_pton
1890 int LIBNETXMS_EXPORTABLE
GetSleepTime(int hour
, int minute
, int second
);
1891 time_t LIBNETXMS_EXPORTABLE
ParseDateTimeA(const char *text
, time_t defaultValue
);
1892 time_t LIBNETXMS_EXPORTABLE
ParseDateTimeW(const WCHAR
*text
, time_t defaultValue
);
1895 #define ParseDateTime ParseDateTimeW
1897 #define ParseDateTime ParseDateTimeA
1906 // C++ only functions
1911 enum nxDirectoryType
1920 TCHAR LIBNETXMS_EXPORTABLE
*GetHeapInfo();
1922 void LIBNETXMS_EXPORTABLE
GetNetXMSDirectory(nxDirectoryType type
, TCHAR
*dir
);
1924 UINT32 LIBNETXMS_EXPORTABLE
IcmpPing(const InetAddress
& addr
, int iNumRetries
, UINT32 dwTimeout
, UINT32
*pdwRTT
, UINT32 dwPacketSize
);
1926 TCHAR LIBNETXMS_EXPORTABLE
*EscapeStringForXML(const TCHAR
*str
, int length
);
1927 String LIBNETXMS_EXPORTABLE
EscapeStringForXML2(const TCHAR
*str
, int length
= -1);
1928 const char LIBNETXMS_EXPORTABLE
*XMLGetAttr(const char **attrs
, const char *name
);
1929 int LIBNETXMS_EXPORTABLE
XMLGetAttrInt(const char **attrs
, const char *name
, int defVal
);
1930 UINT32 LIBNETXMS_EXPORTABLE
XMLGetAttrUINT32(const char **attrs
, const char *name
, UINT32 defVal
);
1931 bool LIBNETXMS_EXPORTABLE
XMLGetAttrBoolean(const char **attrs
, const char *name
, bool defVal
);
1933 #if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
1934 void LIBNETXMS_EXPORTABLE
StartMainLoop(ThreadFunction pfSignalHandler
, ThreadFunction pfMain
);
1939 #endif /* _nms_util_h_ */