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