cosmetic changes
[public/netxms.git] / include / nms_util.h
CommitLineData
15c8cf46 1/* $Id: nms_util.h,v 1.95 2006-10-17 21:20:25 victor Exp $ */
70fed300 2
e9246d26 3/*
7ac71e60 4** NetXMS - Network Management System
e9246d26
VK
5** Copyright (C) 2003 Victor Kirhenshtein
6**
7** This program is free software; you can redistribute it and/or modify
8** it under the terms of the GNU General Public License as published by
9** the Free Software Foundation; either version 2 of the License, or
10** (at your option) any later version.
11**
12** This program is distributed in the hope that it will be useful,
13** but WITHOUT ANY WARRANTY; without even the implied warranty of
14** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15** GNU General Public License for more details.
16**
17** You should have received a copy of the GNU General Public License
18** along with this program; if not, write to the Free Software
19** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20**
21** $module: nms_util.h
22**
23**/
24
25#ifndef _nms_util_h_
26#define _nms_util_h_
27
64584976
VK
28#ifdef _WIN32
29#ifdef LIBNETXMS_EXPORTS
30#define LIBNETXMS_EXPORTABLE __declspec(dllexport)
31#else
32#define LIBNETXMS_EXPORTABLE __declspec(dllimport)
33#endif
34#else /* _WIN32 */
35#define LIBNETXMS_EXPORTABLE
36#endif
37
38
e9246d26
VK
39#include <nms_common.h>
40#include <nms_cscp.h>
953dfe76 41#include <nms_threads.h>
64584976 42#include <time.h>
e9246d26 43
6d8aee95
VK
44#if HAVE_BYTESWAP_H
45#include <byteswap.h>
46#endif
47
48
49//
50// Sirial 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
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//
71// Common constants
72//
f52be742 73
5d63b3c1 74#define INVALID_INDEX 0xFFFFFFFF
28dd92c9 75#define CSCP_TEMP_BUF_SIZE 65536
89bfdd9c 76#define MD5_DIGEST_SIZE 16
d0b249a8 77#define SHA1_DIGEST_SIZE 20
5d63b3c1
VK
78
79
66c04cc7
VK
80//
81// Return codes for IcmpPing()
82//
83
84#define ICMP_SUCCESS 0
85#define ICMP_UNREACHEABLE 1
86#define ICMP_TIMEOUT 2
87#define ICMP_RAW_SOCK_FAILED 3
88
89
dcb44c9a 90//
4826c8a5 91// Token types for configuration loader
dcb44c9a
VK
92//
93
4826c8a5
VK
94#define CT_LONG 0
95#define CT_STRING 1
96#define CT_STRING_LIST 2
97#define CT_END_OF_LIST 3
98#define CT_BOOLEAN 4
99#define CT_WORD 5
fc935a60 100#define CT_IGNORE 6
4826c8a5
VK
101
102
103//
104// Return codes for NxLoadConfig()
105//
106
107#define NXCFG_ERR_OK 0
108#define NXCFG_ERR_NOFILE 1
109#define NXCFG_ERR_SYNTAX 2
110
111
b160350d
VK
112//
113// Class for serial communications
114//
115
116#ifdef __cplusplus
117
118#ifndef _WIN32
119enum
120{
121 NOPARITY,
122 ODDPARITY,
123 EVENPARITY,
124 ONESTOPBIT,
125 TWOSTOPBITS
126};
127
70fed300
AK
128enum
129{
130 FLOW_NONE,
131 FLOW_HARDWARE,
132 FLOW_SOFTWARE
133};
134
b160350d
VK
135#ifndef INVALID_HANDLE_VALUE
136#define INVALID_HANDLE_VALUE (-1)
137#endif
138#endif /* _WIN32 */
139
140class LIBNETXMS_EXPORTABLE Serial
141{
142public:
143 Serial(void);
144 ~Serial(void);
145
146 bool Open(TCHAR *pszPort);
147 void Close(void);
148 void SetTimeout(int nTimeout);
2f06a66e 149 int Read(char *pBuff, int nSize);
b160350d
VK
150 bool Write(char *pBuff, int nSize);
151 void Flush(void);
152 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits);
70fed300 153 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl);
b160350d
VK
154 bool Restart(void);
155
156private:
157 TCHAR *m_pszPort;
158 int m_nTimeout;
159 int m_nSpeed;
160 int m_nDataBits;
161 int m_nStopBits;
162 int m_nParity;
70fed300 163 int m_nFlowControl;
b160350d
VK
164
165#ifndef _WIN32
166 int m_hPort;
70fed300 167 struct termios m_originalSettings;
b160350d
VK
168#else
169 HANDLE m_hPort;
170#endif
171};
172
173#endif /* __cplusplus */
174
175
4826c8a5
VK
176//
177// Configuration item template for configuration loader
178//
179
180typedef struct
181{
018fda4d 182 TCHAR szToken[64];
4826c8a5
VK
183 BYTE iType;
184 BYTE cSeparator; // Separator character for lists
185 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
186 DWORD dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
187 DWORD dwBufferPos; // Should be set to 0
188 void *pBuffer;
189} NX_CFG_TEMPLATE;
dcb44c9a
VK
190
191
3046f9a4
VK
192//
193// getopt() prototype if needed
194//
195
196#ifdef _WIN32
197#include <getopt.h>
198#endif
199
200
f2fdf1b5
VK
201//
202// Win32 API functions missing under WinCE
203//
204
c32b805a 205#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
206
207inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
208{
209 SYSTEMTIME sysTime;
210
211 GetSystemTime(&sysTime);
212 SystemTimeToFileTime(&sysTime, pFt);
213}
214
215#endif // UNDER_CE
216
217
3d1cb7d6
VK
218//
219// Structures for opendir() / readdir() / closedir()
220//
221
222#ifdef _WIN32
223
37d62364
AK
224#ifndef SWIGPERL
225
7f9e0c00 226typedef struct dirent
3d1cb7d6
VK
227{
228 long d_ino; /* inode number (not used by MS-DOS) */
229 int d_namlen; /* Name length */
230 char d_name[257]; /* file name */
231} _DIRECT;
232
233typedef struct _dir_struc
234{
235 char *start; /* Starting position */
236 char *curr; /* Current position */
237 long size; /* Size of string table */
238 long nfiles; /* number if filenames in table */
7f9e0c00 239 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
240} DIR;
241
37d62364
AK
242#endif
243
3d1cb7d6
VK
244#endif /* _WIN32 */
245
246
e9246d26
VK
247//
248// Functions
249//
250
43d92b4c 251
babb8280 252#if WORDS_BIGENDIAN
43d92b4c
VK
253#define htonq(x) (x)
254#define ntohq(x) (x)
9f20696e
VK
255#define htond(x) (x)
256#define ntohd(x) (x)
babb8280
VK
257#define SwapWideString(x)
258#else
ca6ad6bb
VK
259#ifdef HAVE_HTONLL
260#define htonq(x) htonll(x)
261#else
babb8280 262#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
263#endif
264#ifdef HAVE_NTOHLL
265#define ntohq(x) ntohll(x)
266#else
babb8280 267#define ntohq(x) __bswap_64(x)
ca6ad6bb 268#endif
babb8280
VK
269#define htond(x) __bswap_double(x)
270#define ntohd(x) __bswap_double(x)
271#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 272#endif
15929b31 273
55b33d4c
VK
274#ifdef UNDER_CE
275#define close(x) CloseHandle((HANDLE)(x))
276#endif
277
c81979bc
VK
278#ifdef __cplusplus
279#ifndef LIBNETXMS_INLINE
280 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, int nLen)
281 {
282 _tcsncpy(pszDest, pszSrc, nLen - 1);
283 pszDest[nLen - 1] = 0;
284 return pszDest;
285 }
286#endif
287#else
288 TCHAR LIBNETXMS_EXPORTABLE *nx_strncpy(TCHAR *pszDest, TCHAR *pszSrc, int nLen);
289#endif
290
b50f1100 291#ifdef __cplusplus
e9246d26
VK
292extern "C"
293{
b50f1100 294#endif
08043edf
VK
295 int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int);
296 int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
297 size_t nSize, int nFlags, DWORD dwTimeout);
b24955fb 298
c5b523e2
VK
299#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
300 DWORD LIBNETXMS_EXPORTABLE __bswap_32(DWORD dwVal);
301#endif
f52be742 302#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
64584976
VK
303 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
304#endif
9f20696e 305 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
babb8280 306 void LIBNETXMS_EXPORTABLE __bswap_wstr(WCHAR *pStr);
64584976 307
076a55c4 308#if !defined(_WIN32) && !defined(_NETWARE)
018fda4d 309 void LIBNETXMS_EXPORTABLE strupr(TCHAR *in);
7e679c4b 310#endif
ab588c84 311
670c7f3d 312 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
ddce77b0 313 QWORD LIBNETXMS_EXPORTABLE FileSize(TCHAR *pszFileName);
670c7f3d 314
64584976 315 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
018fda4d 316 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
d0fc56c6 317 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(TCHAR *pszName);
a1dc75b0 318
64584976 319 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
f218dc6d 320 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
31521248 321
e25cadc9 322 TCHAR LIBNETXMS_EXPORTABLE *BinToStr(BYTE *pData, DWORD dwSize, TCHAR *pStr);
4075c782 323 DWORD LIBNETXMS_EXPORTABLE StrToBin(TCHAR *pStr, BYTE *pData, DWORD dwSize);
c61f5190 324 void LIBNETXMS_EXPORTABLE MACToStr(BYTE *pData, TCHAR *pStr);
b50f1100 325
018fda4d
AK
326 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
327 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
328 TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(TCHAR *line, TCHAR *buffer);
fa2b47a7
VK
329 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName);
330 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
ff5e8be5 331 void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, TCHAR *pszSubStr, TCHAR *pszReplace);
257defd7 332 TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(TCHAR *pszFileName);
03ed7e4f 333 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer);
64584976 334
6120112b 335 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
a3c76aba
VK
336 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
337 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
b40371d4
AK
338 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(TCHAR *pszFileName, BYTE *pHash);
339 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(TCHAR *pszFileName, BYTE *pHash);
6120112b 340 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(TCHAR *pszFileName, DWORD *pResult);
66c04cc7 341
46ef501a
VK
342 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
343 DWORD *pdwRTT, DWORD dwPacketSize);
4826c8a5 344
018fda4d 345 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(TCHAR *pszFileName, TCHAR *pszSection,
6849d9be 346 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);
125c0e81 347 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(TCHAR *pszText);
aa13cfd9 348
018fda4d 349 HMODULE LIBNETXMS_EXPORTABLE DLOpen(TCHAR *szLibName, TCHAR *pszErrorText);
aa13cfd9 350 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
018fda4d 351 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, TCHAR *szSymbol, TCHAR *pszErrorText);
aa13cfd9 352
ae9cddaf 353 void LIBNETXMS_EXPORTABLE InitSubAgentsLogger(void (* pFunc)(int, TCHAR *));
15c8cf46
VK
354 void LIBNETXMS_EXPORTABLE InitSubAgentsTrapSender(void (* pFunc1)(DWORD, int, TCHAR **),
355 void (* pFunc2)(DWORD, char *, va_list));
ae9cddaf 356
aa13cfd9 357#ifdef _WIN32
018fda4d 358 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, int iBufSize);
aa13cfd9 359#endif
696fc54f
VK
360
361#if !(HAVE_DAEMON)
b1a57444 362 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 363#endif
4332e9d8 364
268774a0
VK
365 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(WCHAR *pszAddr);
366
4332e9d8 367#ifndef _WIN32
5f461694 368 void LIBNETXMS_EXPORTABLE SetDefaultCodepage(char *cp);
4332e9d8
VK
369 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, WCHAR *pWideCharStr,
370 int cchWideChar, char *pByteStr, int cchByteChar,
371 char *pDefaultChar, BOOL *pbUsedDefChar);
372 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, char *pByteStr,
373 int cchByteChar, WCHAR *pWideCharStr,
374 int cchWideChar);
1738fb2b 375#if !HAVE_USEABLE_WCHAR
556d9c45 376 int LIBNETXMS_EXPORTABLE nx_wcslen(WCHAR *pStr);
4332e9d8 377#endif
3e39fcde 378#endif /* _WIN32 */
7d4b78b3
VK
379 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(char *pszString);
380 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(WCHAR *pwszString);
381 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(WCHAR *pwszString);
b50f1100 382
dde96745
VK
383#ifdef UNICODE
384INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
385QWORD LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
386#else
b50f1100
VK
387#if !(HAVE_STRTOLL)
388 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
389#endif
390#if !(HAVE_STRTOULL)
391 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
392#endif
dde96745 393#endif
b50f1100 394
3d1cb7d6 395#ifdef _WIN32
37d62364 396#ifndef SWIGPERL
3d1cb7d6 397 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 398 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6
VK
399 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
400#endif
37d62364 401#endif
3d1cb7d6 402
7f9e0c00
VK
403#if defined(_WIN32) || !(HAVE_SCANDIR)
404 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
405 int (*select)(const struct dirent *),
406 int (*compar)(const struct dirent **, const struct dirent **));
407 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
408#endif
409
55b33d4c
VK
410#ifdef UNDER_CE
411 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
412 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
413 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
414#endif
415
b71bff93 416#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
953dfe76 417void LIBNETXMS_EXPORTABLE StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
45c13da4 418 THREAD_RESULT (THREAD_CALL * pfMain)(void *));
953dfe76
VK
419#endif
420
b50f1100 421#ifdef __cplusplus
e9246d26 422}
b50f1100 423#endif
e9246d26
VK
424
425#endif /* _nms_util_h_ */
70fed300
AK
426
427///////////////////////////////////////////////////////////////////////////////
428/*
429
430$Log: not supported by cvs2svn $
15c8cf46
VK
431Revision 1.94 2006/10/05 05:24:05 victor
432Minor changes
433
5f461694
VK
434Revision 1.93 2006/10/01 20:43:37 victor
435UNICODE-related fixes
436
6cdccf82
VK
437Revision 1.92 2006/10/01 16:01:09 victor
438Yet another Linux UNICODE fix
439
556d9c45
VK
440Revision 1.91 2006/10/01 15:47:39 victor
441More UNICODE fixes
442
1738fb2b
VK
443Revision 1.90 2006/10/01 15:26:28 victor
444Some UNICODE-related fixes
445
3e39fcde
VK
446Revision 1.89 2006/09/30 22:41:08 victor
447Database driver API changed: now all SQL queries and results passed as UNICODE strings
448
7d4b78b3
VK
449Revision 1.88 2006/09/27 13:04:45 victor
450Preparation for process information parameters
451
ca6ad6bb
VK
452Revision 1.87 2006/09/27 04:15:25 victor
453AIX: implemented Net.InterfaceList
454
e25cadc9
VK
455Revision 1.86 2006/09/23 23:49:41 victor
456Console working with UNICODE!
457
dde96745
VK
458Revision 1.85 2006/09/10 06:59:36 victor
459Fixed problmes with Win32 build
460
6d8aee95
VK
461Revision 1.84 2006/09/07 22:02:06 alk
462UNIX version of Serial rewritten
463termio removed from configure (depricated in favour of termio_s_?)
464
70fed300
AK
465
466*/