log history size increased
[public/netxms.git] / include / nms_util.h
CommitLineData
e9246d26 1/*
7ac71e60 2** NetXMS - Network Management System
b0608204 3** Copyright (C) 2003-2013 Victor Kirhenshtein
e9246d26
VK
4**
5** This program is free software; you can redistribute it and/or modify
68f384ea
VK
6** it under the terms of the GNU Lesser General Public License as published
7** by the Free Software Foundation; either version 3 of the License, or
e9246d26
VK
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
68f384ea 15** You should have received a copy of the GNU Lesser General Public License
e9246d26
VK
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
fb7ce4b9 19** File: nms_util.h
e9246d26
VK
20**
21**/
22
23#ifndef _nms_util_h_
24#define _nms_util_h_
25
64584976
VK
26#ifdef _WIN32
27#ifdef LIBNETXMS_EXPORTS
28#define LIBNETXMS_EXPORTABLE __declspec(dllexport)
29#else
30#define LIBNETXMS_EXPORTABLE __declspec(dllimport)
31#endif
32#else /* _WIN32 */
33#define LIBNETXMS_EXPORTABLE
34#endif
35
36
e9246d26
VK
37#include <nms_common.h>
38#include <nms_cscp.h>
953dfe76 39#include <nms_threads.h>
64584976 40#include <time.h>
e9246d26 41
6d8aee95
VK
42#if HAVE_BYTESWAP_H
43#include <byteswap.h>
44#endif
45
92502d5f
VK
46#include <base64.h>
47
6d8aee95
VK
48
49//
92502d5f 50// Serial communications
6d8aee95
VK
51//
52
53#ifdef _WIN32
54
55#define FLOW_NONE 0
56#define FLOW_SOFTWARE 1
57#define FLOW_HARDWARE 2
58
59#else /* _WIN32 */
60
70fed300
AK
61#ifdef HAVE_TERMIOS_H
62# include <termios.h>
63#else
64# error termios.h not found
65#endif
66
6d8aee95
VK
67#endif /* _WIN32 */
68
69
70//
66c04cc7
VK
71// Return codes for IcmpPing()
72//
73
74#define ICMP_SUCCESS 0
75#define ICMP_UNREACHEABLE 1
76#define ICMP_TIMEOUT 2
77#define ICMP_RAW_SOCK_FAILED 3
416816ed 78#define ICMP_API_ERROR 3
66c04cc7
VK
79
80
81//
4826c8a5 82// Token types for configuration loader
dcb44c9a
VK
83//
84
4826c8a5
VK
85#define CT_LONG 0
86#define CT_STRING 1
87#define CT_STRING_LIST 2
88#define CT_END_OF_LIST 3
89#define CT_BOOLEAN 4
90#define CT_WORD 5
fc935a60 91#define CT_IGNORE 6
dda7c270 92#define CT_MB_STRING 7
4826c8a5 93
605fc935
VK
94/**
95 * Return codes for NxLoadConfig()
96 */
4826c8a5
VK
97#define NXCFG_ERR_OK 0
98#define NXCFG_ERR_NOFILE 1
99#define NXCFG_ERR_SYNTAX 2
100
605fc935
VK
101/**
102 * nxlog_open() flags
103 */
967893bb
VK
104#define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
105#define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
106#define NXLOG_IS_OPEN ((UINT32)0x80000000)
9f2902ac 107
605fc935
VK
108/**
109 * nxlog rotation policy
110 */
5945d50a
VK
111#define NXLOG_ROTATION_DISABLED 0
112#define NXLOG_ROTATION_DAILY 1
113#define NXLOG_ROTATION_BY_SIZE 2
114
605fc935
VK
115/**
116 * _tcsdup() replacement
117 */
8ff13508
VK
118#if defined(_WIN32) && defined(USE_WIN32_HEAP)
119#ifdef __cplusplus
120extern "C" {
121#endif
35f836fe 122char LIBNETXMS_EXPORTABLE *nx__tcsdup(const char *src);
8ff13508
VK
123WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
124#ifdef __cplusplus
125}
126#endif
127#endif
128
605fc935
VK
129/**
130 * Class for serial communications
131 */
b160350d
VK
132#ifdef __cplusplus
133
134#ifndef _WIN32
135enum
136{
137 NOPARITY,
138 ODDPARITY,
139 EVENPARITY,
140 ONESTOPBIT,
141 TWOSTOPBITS
142};
143
70fed300
AK
144enum
145{
146 FLOW_NONE,
147 FLOW_HARDWARE,
148 FLOW_SOFTWARE
149};
150
b160350d
VK
151#ifndef INVALID_HANDLE_VALUE
152#define INVALID_HANDLE_VALUE (-1)
153#endif
154#endif /* _WIN32 */
155
156class LIBNETXMS_EXPORTABLE Serial
157{
b160350d
VK
158private:
159 TCHAR *m_pszPort;
160 int m_nTimeout;
161 int m_nSpeed;
162 int m_nDataBits;
163 int m_nStopBits;
164 int m_nParity;
70fed300 165 int m_nFlowControl;
b160350d
VK
166
167#ifndef _WIN32
168 int m_hPort;
70fed300 169 struct termios m_originalSettings;
b160350d
VK
170#else
171 HANDLE m_hPort;
172#endif
b160350d 173
826fcc1f
VK
174public:
175 Serial();
176 ~Serial();
177
178 bool open(const TCHAR *pszPort);
179 void close();
180 void setTimeout(int nTimeout);
181 int read(char *pBuff, int nSize); /* waits up to timeout and do single read */
182 int readAll(char *pBuff, int nSize); /* read until timeout or out of space */
183 bool write(const char *pBuff, int nSize);
184 void flush();
185 bool set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl = FLOW_NONE);
186 bool restart();
187};
ff214673 188
3fe40613
VK
189/**
190 * Dynamic string class
191 */
8bad8476
VK
192class LIBNETXMS_EXPORTABLE String
193{
194protected:
195 TCHAR *m_pszBuffer;
967893bb 196 UINT32 m_dwBufSize;
8bad8476
VK
197
198public:
c577e360
VK
199 static const int npos;
200
8bad8476 201 String();
f3cdd1c5 202 String(const TCHAR *init);
643c9dcb 203 String(const String &src);
8bad8476
VK
204 ~String();
205
7618e362 206 TCHAR *getBuffer() { return m_pszBuffer; }
ce7565e7 207 void setBuffer(TCHAR *pszBuffer);
8bad8476 208
2391d4fd 209 const String& operator =(const TCHAR *pszStr);
49bf9d8d 210 const String& operator =(const String &src);
2391d4fd 211 const String& operator +=(const TCHAR *pszStr);
ce7565e7 212 operator const TCHAR*() { return CHECK_NULL_EX(m_pszBuffer); }
8bad8476 213
08b214c6
VK
214 char *getUTF8String();
215
967893bb 216 void addString(const TCHAR *pStr, UINT32 dwLen);
ce7565e7 217 void addDynamicString(TCHAR *pszStr) { if (pszStr != NULL) { *this += pszStr; free(pszStr); } }
c577e360 218
967893bb
VK
219 void addMultiByteString(const char *pStr, UINT32 dwSize, int nCodePage);
220 void addWideCharString(const WCHAR *pStr, UINT32 dwSize);
ff175e91 221
ce7565e7 222 void addFormattedString(const TCHAR *format, ...);
893dfdd7 223 void addFormattedStringV(const TCHAR *format, va_list args);
c577e360 224
967893bb 225 UINT32 getSize() { return m_dwBufSize > 0 ? m_dwBufSize - 1 : 0; }
ce7565e7 226 BOOL isEmpty() { return m_dwBufSize <= 1; }
c577e360 227
ce7565e7
VK
228 TCHAR *subStr(int nStart, int nLen, TCHAR *pszBuffer);
229 TCHAR *subStr(int nStart, int nLen) { return subStr(nStart, nLen, NULL); }
230 int find(const TCHAR *pszStr, int nStart = 0);
1e1450ef 231
6bcafa2d
VK
232 void escapeCharacter(int ch, int esc);
233 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
0bbab9d3
VK
234 void trim();
235 void shrink(int chars = 1);
c577e360
VK
236};
237
3fe40613
VK
238/**
239 * String maps base class
240 */
241class LIBNETXMS_EXPORTABLE StringMapBase
c577e360
VK
242{
243protected:
967893bb 244 UINT32 m_size;
fb986055 245 TCHAR **m_keys;
3fe40613
VK
246 void **m_values;
247 bool m_objectOwner;
55bdca5a 248 bool m_ignoreCase;
3fe40613 249 void (*m_objectDestructor)(void *);
c577e360 250
967893bb 251 UINT32 find(const TCHAR *key);
3fe40613
VK
252 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
253 void *getObject(const TCHAR *key);
254 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
c577e360
VK
255
256public:
3fe40613
VK
257 StringMapBase(bool objectOwner);
258 virtual ~StringMapBase();
259
55bdca5a
VK
260 void setOwner(bool owner) { m_objectOwner = owner; }
261 void setIgnoreCase(bool ignore) { m_ignoreCase = ignore; }
262
3fe40613
VK
263 void remove(const TCHAR *key);
264 void clear();
265
967893bb
VK
266 UINT32 getSize() { return m_size; }
267 const TCHAR *getKeyByIndex(UINT32 idx) { return (idx < m_size) ? CHECK_NULL_EX(m_keys[idx]) : NULL; }
3fe40613
VK
268};
269
270/**
271 * String map class
272 */
273class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
274{
275public:
276 StringMap() : StringMapBase(true) { }
fb986055 277 StringMap(const StringMap &src);
3fe40613 278 virtual ~StringMap();
c577e360 279
fb986055 280 StringMap& operator =(const StringMap &src);
40b7416c 281
3fe40613
VK
282 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
283 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
967893bb 284 void set(const TCHAR *key, UINT32 value);
5ad2167d 285
3fe40613 286 const TCHAR *get(const TCHAR *key) { return (const TCHAR *)getObject(key); }
967893bb 287 UINT32 getULong(const TCHAR *key, UINT32 defaultValue);
6acb8479 288 bool getBoolean(const TCHAR *key, bool defaultValue);
5ad2167d 289
967893bb 290 const TCHAR *getValueByIndex(UINT32 idx) { return (idx < m_size) ? CHECK_NULL_EX((TCHAR *)m_values[idx]) : NULL; }
8bad8476
VK
291};
292
3fe40613
VK
293/**
294 * String map template for holding objects as values
295 */
296template <class T> class StringObjectMap : public StringMapBase
297{
298private:
299 static void destructor(void *object) { delete (T*)object; }
6173bea8 300
3fe40613
VK
301public:
302 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
303
304 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
305 T *get(const TCHAR *key) { return (T*)getObject(key); }
967893bb 306 T *getValueByIndex(UINT32 idx) { return (idx < m_size) ? (T *)m_values[idx] : NULL; }
3fe40613 307};
6173bea8 308
3fe40613
VK
309/**
310 * String list class
311 */
6173bea8
VK
312class LIBNETXMS_EXPORTABLE StringList
313{
314private:
315 int m_count;
316 int m_allocated;
317 TCHAR **m_values;
318
319public:
320 StringList();
86c126f5 321 StringList(StringList *src);
22ee6d97 322 StringList(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
323 ~StringList();
324
325 void add(const TCHAR *value);
326 void addPreallocated(TCHAR *value);
967893bb
VK
327 void add(INT32 value);
328 void add(UINT32 value);
6173bea8 329 void add(INT64 value);
967893bb 330 void add(UINT64 value);
6173bea8 331 void add(double value);
24dc5346 332 void replace(int index, const TCHAR *value);
6173bea8
VK
333 void clear();
334 int getSize() { return m_count; }
335 const TCHAR *getValue(int index) { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
ed806a1a
VK
336 int getIndex(const TCHAR *value);
337 int getIndexIgnoreCase(const TCHAR *value);
d51f2182 338 void remove(int index);
86c126f5 339 void addAll(const StringList *src);
93dbc1be 340 void merge(const StringList *src, bool matchCase);
7d25a5c5 341 TCHAR *join(const TCHAR *separator);
22ee6d97 342 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
343};
344
3fe40613 345/**
a58501c0
VK
346 * Entry of string set
347 */
348struct StringSetEntry;
349
350/**
351 * String set class
352 */
353class LIBNETXMS_EXPORTABLE StringSet
354{
355private:
356 StringSetEntry *m_data;
357
358public:
359 StringSet();
360 ~StringSet();
361
362 void add(const TCHAR *str);
363 void remove(const TCHAR *str);
364 void clear();
365
366 int size();
367 bool exist(const TCHAR *str);
368
fa493140 369 void addAll(StringSet *src);
a58501c0
VK
370 void forEach(bool (*cb)(const TCHAR *, void *), void *userData);
371};
372
373/**
3fe40613
VK
374 * Dynamic array class
375 */
0d75ea88
VK
376class LIBNETXMS_EXPORTABLE Array
377{
378private:
379 int m_size;
380 int m_allocated;
381 int m_grow;
382 void **m_data;
383 bool m_objectOwner;
384
16d6f798 385 void internalRemove(int index, bool allowDestruction);
cfa7c21f 386 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
16d6f798 387
0d75ea88 388protected:
cfa7c21f 389 void (*m_objectDestructor)(void *);
0d75ea88
VK
390
391public:
392 Array(int initial = 0, int grow = 16, bool owner = false);
393 virtual ~Array();
394
395 int add(void *object);
396 void *get(int index) { return ((index >= 0) && (index < m_size)) ? m_data[index] : NULL; }
47a89886 397 void set(int index, void *object);
24dc5346 398 void replace(int index, void *object);
16d6f798
VK
399 void remove(int index) { internalRemove(index, true); }
400 void unlink(int index) { internalRemove(index, false); }
0d75ea88
VK
401 void clear();
402
403 int size() { return m_size; }
404
405 void setOwner(bool owner) { m_objectOwner = owner; }
406 bool isOwner() { return m_objectOwner; }
407};
408
3fe40613
VK
409/**
410 * Template class for dynamic array which holds objects
411 */
0d75ea88
VK
412template <class T> class ObjectArray : public Array
413{
cfa7c21f
VK
414private:
415 static void destructor(void *object) { delete (T*)object; }
0d75ea88
VK
416
417public:
cfa7c21f 418 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
0d75ea88
VK
419 virtual ~ObjectArray() { }
420
47a89886 421 int add(T *object) { return Array::add((void *)object); }
0d75ea88 422 T *get(int index) { return (T*)Array::get(index); }
47a89886
VK
423 void set(int index, T *object) { Array::set(index, (void *)object); }
424 void replace(int index, T *object) { Array::replace(index, (void *)object); }
0d75ea88
VK
425};
426
25fe6265 427/**
e8e76b4f
VK
428 * Template class for dynamic array which holds scalar values
429 */
430template <class T> class IntegerArray : public Array
431{
432private:
433 static void destructor(void *object) { }
434
435public:
436 IntegerArray(int initial = 0, int grow = 16) : Array(initial, grow, false) { m_objectDestructor = destructor; }
437 virtual ~IntegerArray() { }
438
439 int add(T value) { return Array::add(CAST_TO_POINTER(value, void *)); }
440 T get(int index) { return CAST_FROM_POINTER(Array::get(index), T); }
441 void set(int index, T value) { Array::set(index, CAST_TO_POINTER(value, void *)); }
442 void replace(int index, T value) { Array::replace(index, CAST_TO_POINTER(value, void *)); }
443};
444
445/**
25fe6265
VK
446 * Auxilliary class to hold dynamically allocated array of structures
447 */
cc8ce218
VK
448template <class T> class StructArray
449{
450private:
451 int m_count;
452 T *m_data;
453
454public:
455 StructArray(T *data, int count) { m_data = data; m_count = count; }
456 ~StructArray() { safe_free(m_data); }
457
458 int size() { return m_count; }
459 T *get(int index) { return ((index >= 0) && (index < m_count)) ? &m_data[index] : NULL; }
460};
461
25fe6265
VK
462/**
463 * Auxilliary class for objects which counts references and
464 * destroys itself wheren reference count falls to 0
465 */
7f632dfe
VK
466class LIBNETXMS_EXPORTABLE RefCountObject
467{
468private:
7785322f 469 VolatileCounter m_refCount;
7f632dfe
VK
470
471public:
7785322f
VK
472 RefCountObject()
473 {
474 m_refCount = 1;
475 }
b549a0f8 476 virtual ~RefCountObject();
7785322f
VK
477
478 void incRefCount()
479 {
480 InterlockedIncrement(&m_refCount);
481 }
482
483 void decRefCount()
484 {
485 if (InterlockedDecrement(&m_refCount) == 0)
486 delete this;
487 }
7f632dfe
VK
488};
489
967893bb
VK
490class CSCPMessage;
491
492/**
493 * Table column definition
494 */
495class LIBNETXMS_EXPORTABLE TableColumnDefinition
496{
497private:
498 TCHAR *m_name;
499 TCHAR *m_displayName;
500 INT32 m_dataType;
501 bool m_instanceColumn;
502
503public:
504 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
505 TableColumnDefinition(CSCPMessage *msg, UINT32 baseId);
506 TableColumnDefinition(TableColumnDefinition *src);
507 ~TableColumnDefinition();
508
509 void fillMessage(CSCPMessage *msg, UINT32 baseId);
510
511 const TCHAR *getName() { return m_name; }
512 const TCHAR *getDisplayName() { return m_displayName; }
513 INT32 getDataType() { return m_dataType; }
514 bool isInstanceColumn() { return m_instanceColumn; }
515
516 void setDataType(INT32 type) { m_dataType = type; }
517 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
a0ddfb29 518 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
967893bb
VK
519};
520
521/**
522 * Class for table data storage
523 */
a0ddfb29 524class LIBNETXMS_EXPORTABLE Table : public RefCountObject
967893bb
VK
525{
526private:
527 int m_nNumRows;
528 int m_nNumCols;
529 TCHAR **m_ppData;
530 ObjectArray<TableColumnDefinition> *m_columns;
531 TCHAR *m_title;
532 int m_source;
533
534 void createFromMessage(CSCPMessage *msg);
535 void destroy();
536
537public:
538 Table();
a0ddfb29 539 Table(Table *src);
967893bb 540 Table(CSCPMessage *msg);
b549a0f8 541 virtual ~Table();
967893bb
VK
542
543 int fillMessage(CSCPMessage &msg, int offset, int rowLimit);
544 void updateFromMessage(CSCPMessage *msg);
545
a0ddfb29
VK
546 void addAll(Table *src);
547 void copyRow(Table *src, int row);
967893bb
VK
548
549 int getNumRows() { return m_nNumRows; }
550 int getNumColumns() { return m_nNumCols; }
551 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
552 int getSource() { return m_source; }
553
554 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
555 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
556 int getColumnIndex(const TCHAR *name);
557 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
558
559 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = (title != NULL) ? _tcsdup(title) : NULL; }
560 void setSource(int source) { m_source = source; }
8dba1eac 561 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
967893bb
VK
562 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
563 int addRow();
564
565 void setAt(int nRow, int nCol, INT32 nData);
566 void setAt(int nRow, int nCol, UINT32 dwData);
567 void setAt(int nRow, int nCol, double dData);
568 void setAt(int nRow, int nCol, INT64 nData);
569 void setAt(int nRow, int nCol, UINT64 qwData);
570 void setAt(int nRow, int nCol, const TCHAR *pszData);
571 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
572
573 void set(int nCol, INT32 nData) { setAt(m_nNumRows - 1, nCol, nData); }
574 void set(int nCol, UINT32 dwData) { setAt(m_nNumRows - 1, nCol, dwData); }
575 void set(int nCol, double dData) { setAt(m_nNumRows - 1, nCol, dData); }
576 void set(int nCol, INT64 nData) { setAt(m_nNumRows - 1, nCol, nData); }
577 void set(int nCol, UINT64 qwData) { setAt(m_nNumRows - 1, nCol, qwData); }
578 void set(int nCol, const TCHAR *pszData) { setAt(m_nNumRows - 1, nCol, pszData); }
579 void setPreallocated(int nCol, TCHAR *pszData) { setPreallocatedAt(m_nNumRows - 1, nCol, pszData); }
580
581 const TCHAR *getAsString(int nRow, int nCol);
582 INT32 getAsInt(int nRow, int nCol);
583 UINT32 getAsUInt(int nRow, int nCol);
584 INT64 getAsInt64(int nRow, int nCol);
585 UINT64 getAsUInt64(int nRow, int nCol);
586 double getAsDouble(int nRow, int nCol);
22aaa779
VK
587
588 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
a0ddfb29 589 int findRowByInstance(const TCHAR *instance);
967893bb
VK
590};
591
25fe6265
VK
592/**
593 * Network connection
594 */
58ee2f43
VK
595class LIBNETXMS_EXPORTABLE SocketConnection
596{
597protected:
598 SOCKET m_socket;
599 char m_data[4096];
600 int m_dataPos;
601
602public:
603 SocketConnection();
604 virtual ~SocketConnection();
605
967893bb
VK
606 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
607 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
58ee2f43
VK
608 void disconnect();
609
967893bb
VK
610 bool canRead(UINT32 timeout);
611 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43
VK
612 bool waitForText(const char *text, int timeout);
613
614 int write(const char *pBuff, int nSize);
1671f85d
VK
615 bool writeLine(const char *line);
616
967893bb 617 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
618};
619
030a0779
VK
620/**
621 * Telnet connection - handles all telnet negotiation
622 */
def5b792
AK
623class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
624{
030a0779 625protected:
967893bb
VK
626 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
627 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
030a0779 628
def5b792 629public:
967893bb 630 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 631
967893bb
VK
632 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
633 bool connect(UINT32 ip, WORD port, UINT32 timeout);
634 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
635 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
636};
637
b160350d
VK
638#endif /* __cplusplus */
639
030a0779
VK
640/**
641 * Configuration item template for configuration loader
642 */
4826c8a5
VK
643typedef struct
644{
018fda4d 645 TCHAR szToken[64];
4826c8a5
VK
646 BYTE iType;
647 BYTE cSeparator; // Separator character for lists
648 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
967893bb
VK
649 UINT32 dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
650 UINT32 dwBufferPos; // Should be set to 0
4826c8a5
VK
651 void *pBuffer;
652} NX_CFG_TEMPLATE;
dcb44c9a 653
030a0779
VK
654/**
655 * Code translation structure
656 */
bc858635
VK
657typedef struct __CODE_TO_TEXT
658{
659 int code;
660 const TCHAR *text;
661} CODE_TO_TEXT;
662
663
664//
3046f9a4
VK
665// getopt() prototype if needed
666//
667
df9812a9 668#if USE_BUNDLED_GETOPT
99e50bb7 669#include <netxms_getopt.h>
3046f9a4
VK
670#endif
671
672
673//
f2fdf1b5
VK
674// Win32 API functions missing under WinCE
675//
676
c32b805a 677#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
678
679inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
680{
681 SYSTEMTIME sysTime;
682
683 GetSystemTime(&sysTime);
684 SystemTimeToFileTime(&sysTime, pFt);
685}
686
687#endif // UNDER_CE
688
689
690//
3d1cb7d6
VK
691// Structures for opendir() / readdir() / closedir()
692//
693
694#ifdef _WIN32
695
7f9e0c00 696typedef struct dirent
3d1cb7d6
VK
697{
698 long d_ino; /* inode number (not used by MS-DOS) */
699 int d_namlen; /* Name length */
700 char d_name[257]; /* file name */
701} _DIRECT;
702
703typedef struct _dir_struc
704{
705 char *start; /* Starting position */
706 char *curr; /* Current position */
707 long size; /* Size of string table */
708 long nfiles; /* number if filenames in table */
7f9e0c00 709 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
710} DIR;
711
5ea2db30
VK
712#ifdef UNICODE
713
714typedef struct dirent_w
715{
716 long d_ino; /* inode number (not used by MS-DOS) */
717 int d_namlen; /* Name length */
718 WCHAR d_name[257]; /* file name */
719} _DIRECTW;
720
721typedef struct _dir_struc_w
722{
723 WCHAR *start; /* Starting position */
724 WCHAR *curr; /* Current position */
725 long size; /* Size of string table */
726 long nfiles; /* number if filenames in table */
727 struct dirent_w dirstr; /* Directory structure to return */
728} DIRW;
729
730#define _TDIR DIRW
731#define _TDIRECT _DIRECTW
732#define _tdirent dirent_w
733
734#else
735
736#define _TDIR DIR
737#define _TDIRECT _DIRECT
738#define _tdirent dirent
739
37d62364
AK
740#endif
741
76b1d3d8
VK
742#else /* not _WIN32 */
743
744typedef struct dirent_w
745{
746 long d_ino; /* inode number */
747 WCHAR d_name[257]; /* file name */
748} _DIRECTW;
749
750typedef struct _dir_struc_w
751{
752 DIR *dir; /* Original non-unicode structure */
753 struct dirent_w dirstr; /* Directory structure to return */
754} DIRW;
755
3d1cb7d6
VK
756#endif /* _WIN32 */
757
758
759//
e9246d26
VK
760// Functions
761//
762
babb8280 763#if WORDS_BIGENDIAN
43d92b4c
VK
764#define htonq(x) (x)
765#define ntohq(x) (x)
9f20696e
VK
766#define htond(x) (x)
767#define ntohd(x) (x)
babb8280
VK
768#define SwapWideString(x)
769#else
ca6ad6bb
VK
770#ifdef HAVE_HTONLL
771#define htonq(x) htonll(x)
772#else
babb8280 773#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
774#endif
775#ifdef HAVE_NTOHLL
776#define ntohq(x) ntohll(x)
777#else
babb8280 778#define ntohq(x) __bswap_64(x)
ca6ad6bb 779#endif
babb8280
VK
780#define htond(x) __bswap_double(x)
781#define ntohd(x) __bswap_double(x)
782#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 783#endif
15929b31 784
55b33d4c
VK
785#ifdef UNDER_CE
786#define close(x) CloseHandle((HANDLE)(x))
787#endif
788
b50f1100 789#ifdef __cplusplus
4299401e 790 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
c81979bc 791 {
912b994d
VK
792#if defined(_WIN32) && (_MSC_VER >= 1400)
793 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
794#else
c81979bc
VK
795 _tcsncpy(pszDest, pszSrc, nLen - 1);
796 pszDest[nLen - 1] = 0;
912b994d 797#endif
c81979bc
VK
798 return pszDest;
799 }
f3387429
VK
800
801#ifdef UNICODE
802 inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
803 {
804#if defined(_WIN32) && (_MSC_VER >= 1400)
6646dca4 805 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429
VK
806#else
807 strncpy(pszDest, pszSrc, nLen - 1);
808 pszDest[nLen - 1] = 0;
c81979bc 809#endif
f3387429
VK
810 return pszDest;
811 }
c81979bc 812#else
f3387429 813#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
814#endif
815
967893bb 816int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
7b8b337e
VK
817int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int, MUTEX);
818int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
967893bb 819 size_t nSize, int nFlags, UINT32 dwTimeout);
b0608204 820#endif /* __cplusplus */
c81979bc
VK
821
822#ifdef __cplusplus
e9246d26
VK
823extern "C"
824{
b50f1100 825#endif
c5b523e2 826#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
967893bb 827 UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
c5b523e2 828#endif
f52be742 829#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
967893bb 830 UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
64584976 831#endif
9f20696e 832 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
273da39a 833 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
64584976 834
076a55c4 835#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc
VK
836#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
837 void LIBNETXMS_EXPORTABLE wcsupr(WCHAR *in);
838#endif
839 void LIBNETXMS_EXPORTABLE strupr(char *in);
7e679c4b 840#endif
ab588c84 841
b8a8d064
VK
842 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
843 int (*compare)(const void *, const void *, void *));
844
670c7f3d 845 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
d35ed0aa 846
967893bb
VK
847 UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
848 UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
849#ifdef UNICODE
850#define FileSize FileSizeW
851#else
852#define FileSize FileSizeA
853#endif
670c7f3d 854
967893bb
VK
855 int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
856 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 857#ifdef UNICODE
967893bb 858 char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
859#else
860#define IpToStrA IpToStr
e0f99bf0 861#endif
36e44abe 862 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(BYTE *addr, TCHAR *buffer);
60557d06
VK
863 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
864
967893bb 865 UINT32 LIBNETXMS_EXPORTABLE ResolveHostName(const TCHAR *pszName);
08b214c6 866#ifdef UNICODE
967893bb 867 UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *pszName);
08b214c6
VK
868#else
869#define ResolveHostNameA ResolveHostName
870#endif
a1dc75b0 871
967893bb
VK
872 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, UINT32 dwSize);
873 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, UINT32 dwSize);
31521248 874
967893bb
VK
875 WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *pData, UINT32 dwSize, WCHAR *pStr);
876 char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *pData, UINT32 dwSize, char *pStr);
d7970ee7
VK
877#ifdef UNICODE
878#define BinToStr BinToStrW
879#else
880#define BinToStr BinToStrA
881#endif
882
967893bb 883 UINT32 LIBNETXMS_EXPORTABLE StrToBin(const TCHAR *pStr, BYTE *pData, UINT32 dwSize);
f42b8099 884 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *pData, TCHAR *pStr);
b50f1100 885
b07c50cc
VK
886 void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
887 void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
888#ifdef UNICODE
889#define StrStrip StrStripW
890#else
891#define StrStrip StrStripA
892#endif
893
ada97259 894 const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
cafe592d 895 const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
896#ifdef UNICODE
897#define ExtractWord ExtractWordW
898#else
899#define ExtractWord ExtractWordA
900#endif
901
3583a400
VK
902 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
903 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
904#ifdef UNICODE
905#define NumChars NumCharsW
906#else
907#define NumChars NumCharsA
908#endif
ada97259 909
35f836fe
VK
910 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
911 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
912#ifdef UNICODE
913#define RemoveTrailingCRLF RemoveTrailingCRLFW
914#else
915#define RemoveTrailingCRLF RemoveTrailingCRLFA
916#endif
917
cafe592d
VK
918 BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *pszStr, const char *pszExpr, BOOL bMatchCase);
919 BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *pszStr, const WCHAR *pszExpr, BOOL bMatchCase);
920#ifdef UNICODE
921#define RegexpMatch RegexpMatchW
922#else
923#define RegexpMatch RegexpMatchA
924#endif
925
a73a4403 926 const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize);
ada97259 927 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
240816b0 928 bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
ada97259
VK
929 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
930
f7794b68
VK
931#ifdef __cplusplus
932 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
933#endif
fa2b47a7 934 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
7618e362 935 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1a5e0c22 936 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
c3cfc039 937 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
967893bb 938 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 939#ifdef UNICODE
967893bb 940 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
941#else
942#define LoadFileA LoadFile
943#endif
944
967893bb 945 UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, UINT32 dwSize, UINT32 dwCRC);
a3c76aba 946 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 947 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
a3c76aba 948 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 949 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
3aa0061c
VK
950 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
951 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
967893bb 952 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 953
4e2debcc
VK
954 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
955 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
956
6b1282e7
VK
957 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
958
967893bb
VK
959 UINT32 LIBNETXMS_EXPORTABLE IcmpPing(UINT32 dwAddr, int iNumRetries, UINT32 dwTimeout,
960 UINT32 *pdwRTT, UINT32 dwPacketSize);
4826c8a5 961
3aa0061c 962 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 963
4560d8bd 964 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
aa13cfd9 965 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
dda7c270 966 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 967
289d126b
VK
968 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
969 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
970 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
971
972 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
973 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
974 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
975
976#ifdef UNICODE
977#define ExtractNamedOptionValue ExtractNamedOptionValueW
978#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
979#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
980#else
981#define ExtractNamedOptionValue ExtractNamedOptionValueA
982#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
983#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
984#endif
ae9cddaf 985
bc858635
VK
986#ifdef __cplusplus
987 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
988#else
989 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
990#endif
991
aa13cfd9 992#ifdef _WIN32
967893bb 993 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
ccc34207 994 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
11606542 995 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 996#endif
696fc54f
VK
997
998#if !(HAVE_DAEMON)
b1a57444 999 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 1000#endif
4332e9d8 1001
967893bb 1002 UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 1003
4332e9d8 1004#ifndef _WIN32
664c7c49 1005 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
967893bb 1006 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
4332e9d8
VK
1007 int cchWideChar, char *pByteStr, int cchByteChar,
1008 char *pDefaultChar, BOOL *pbUsedDefChar);
967893bb 1009 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
4332e9d8
VK
1010 int cchByteChar, WCHAR *pWideCharStr,
1011 int cchWideChar);
04dbaf62 1012
6b93c954 1013#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
8b86c5dc 1014 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
1015#endif
1016#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
8b86c5dc 1017 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
1018#endif
1019#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
35f836fe 1020 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2__tcsdup(const UCS2CHAR *pStr);
4332e9d8 1021#endif
8b86c5dc 1022
53c96e2d
VK
1023 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1024 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
39945910
VK
1025 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1026 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 1027
6e17f325
VK
1028 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1029 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1030 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1031 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1032 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1033 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 1034
25575d38
VK
1035 int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1036 int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
7ff399a5 1037 int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
25575d38
VK
1038 int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1039 int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
7ff399a5 1040 int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1041
3e39fcde 1042#endif /* _WIN32 */
8b86c5dc 1043
3aa0061c 1044 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
c16afb3d 1045 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
3aa0061c
VK
1046 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
1047 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
8b86c5dc 1048
3d1058ed
VK
1049#ifdef _WITH_ENCRYPTION
1050 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
1051#endif
1052
8b86c5dc 1053#ifdef UNICODE_UCS4
53c96e2d
VK
1054 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1055 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1056 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
d9b66a56
VK
1057 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1058 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1059#endif
8b86c5dc 1060
842c22f4
AK
1061#if !defined(_WIN32) && !HAVE_WSTAT
1062 int wstat(const WCHAR *_path, struct stat *_sbuf);
1063#endif
1064
ef856d02 1065#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1066
f3387429
VK
1067#if !HAVE_WPOPEN
1068 FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
1069#endif
8b86c5dc
VK
1070#if !HAVE_WFOPEN
1071 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
1072#endif
1073#if !HAVE_WOPEN
1074 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1075#endif
f3387429
VK
1076#if !HAVE_WCHMOD
1077 int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
1078#endif
b07c50cc
VK
1079#if !HAVE_WCHDIR
1080 int wchdir(const WCHAR *_path);
1081#endif
1082#if !HAVE_WMKDIR
1083 int wmkdir(const WCHAR *_path, int mode);
1084#endif
1085#if !HAVE_WRMDIR
1086 int wrmdir(const WCHAR *_path);
1087#endif
ccc34207
VK
1088#if !HAVE_WRENAME
1089 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
1090#endif
1091#if !HAVE_WUNLINK
1092 int wunlink(const WCHAR *_path);
1093#endif
b07c50cc
VK
1094#if !HAVE_WREMOVE
1095 int wremove(const WCHAR *_path);
1096#endif
1097#if !HAVE_WSYSTEM
1098 int wsystem(const WCHAR *_cmd);
1099#endif
dda7c270
VK
1100#if !HAVE_WMKSTEMP
1101 int wmkstemp(const WCHAR *_template);
1102#endif
b07c50cc
VK
1103#if !HAVE_WACCESS
1104 int waccess(const WCHAR *_path, int mode);
1105#endif
273da39a
VK
1106#if !HAVE_WGETENV
1107 WCHAR *wgetenv(const WCHAR *_string);
1108#endif
f3387429
VK
1109#if !HAVE_WCTIME
1110 WCHAR *wctime(const time_t *timep);
1111#endif
0186b5da
VK
1112#if !HAVE_PUTWS
1113 int putws(const WCHAR *s);
1114#endif
d49d7500
VK
1115#if !HAVE_WCSERROR && HAVE_STRERROR
1116 WCHAR *wcserror(int errnum);
1117#endif
1118#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06
VK
1119#if HAVE_POSIX_STRERROR_R
1120 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1121#else
d49d7500
VK
1122 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1123#endif
b0823d06 1124#endif
e05f8aaf 1125
ef856d02 1126#endif /* UNICODE && !_WIN32*/
e05f8aaf 1127
8b86c5dc
VK
1128#if !HAVE_STRTOLL
1129 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
1130#endif
1131#if !HAVE_STRTOULL
967893bb 1132 UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1133#endif
e05f8aaf
VK
1134
1135#if !HAVE_WCSTOLL
1136 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
1137#endif
1138#if !HAVE_WCSTOULL
967893bb 1139 UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1140#endif
b50f1100 1141
fa88627b 1142#if !HAVE_WCSDUP && !defined(_WIN32)
37861c72
VK
1143 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
1144#endif
1145
4d978b4a
VK
1146#if !HAVE_WCSCASECMP && !defined(_WIN32)
1147 int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
1148#endif
1149
3d1cb7d6 1150#ifdef _WIN32
5ea2db30
VK
1151#ifdef UNICODE
1152 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1153 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1154 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1155
1156#define _topendir wopendir
1157#define _treaddir wreaddir
1158#define _tclosedir wclosedir
1159#else
1160#define _topendir opendir
1161#define _treaddir readdir
1162#define _tclosedir closedir
1163#endif
1164
3d1cb7d6 1165 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 1166 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6 1167 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1168
76b1d3d8
VK
1169#else /* not _WIN32 */
1170
1171 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1172 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1173 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1174
37d62364 1175#endif
3d1cb7d6 1176
7f9e0c00
VK
1177#if defined(_WIN32) || !(HAVE_SCANDIR)
1178 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1179 int (*select)(const struct dirent *),
1180 int (*compar)(const struct dirent **, const struct dirent **));
1181 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
1182#endif
1183
55b33d4c
VK
1184#ifdef UNDER_CE
1185 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
1186 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
1187 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
1188#endif
1189
967893bb 1190BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
d49d7500 1191 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1192void LIBNETXMS_EXPORTABLE nxlog_close(void);
1193void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1194BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1195BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
9113e749
VK
1196
1197typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1198void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1199
1200void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
1201void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...);
9f2902ac 1202
37a7a862
VK
1203#ifdef _WIN32
1204int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1205int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1206#ifdef UNICODE
1207#define _tmkstemp wmkstemp
1208#else
1209#define _tmkstemp mkstemp
1210#endif
1211#endif
1212
9b13553b
VK
1213#ifndef _WIN32
1214int strcat_s(char *dst, size_t dstSize, const char *src);
1215int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1216#endif
1217
b50f1100 1218#ifdef __cplusplus
e9246d26 1219}
b50f1100 1220#endif
e9246d26 1221
a7ff20a5
VK
1222
1223//
1224// C++ only finctions
1225//
1226
1227#ifdef __cplusplus
1228
240816b0
VK
1229TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1230String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1231const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1232int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1233UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1234bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1235
1236#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 1237void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
1238#endif
1239
1240void LIBNETXMS_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
967893bb
VK
1241 void (* sendTrap1)(UINT32, const TCHAR *, const char *, va_list),
1242 void (* sendTrap2)(UINT32, const TCHAR *, int, TCHAR **),
1243 BOOL (* sendFile)(void *, UINT32, const TCHAR *, long),
53c96e2d 1244 BOOL (* pushData)(const TCHAR *, const TCHAR *));
a7ff20a5
VK
1245
1246#endif
1247
e9246d26 1248#endif /* _nms_util_h_ */