Implemented Disk.xxx parameters
[public/netxms.git] / include / nms_util.h
CommitLineData
e25cadc9 1/* $Id: nms_util.h,v 1.87 2006-09-27 04:15: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
80//
66c04cc7
VK
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
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
112//
b160350d
VK
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
176//
4826c8a5
VK
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
192//
3046f9a4
VK
193// getopt() prototype if needed
194//
195
196#ifdef _WIN32
197#include <getopt.h>
198#endif
199
200
201//
f2fdf1b5
VK
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
218//
3d1cb7d6
VK
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
247//
e9246d26
VK
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
259#define htonq(x) __bswap_64(x)
260#define ntohq(x) __bswap_64(x)
261#define htond(x) __bswap_double(x)
262#define ntohd(x) __bswap_double(x)
263#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 264#endif
15929b31 265
55b33d4c
VK
266#ifdef UNDER_CE
267#define close(x) CloseHandle((HANDLE)(x))
268#endif
269
b50f1100 270#ifdef __cplusplus
c81979bc
VK
271#ifndef LIBNETXMS_INLINE
272 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, int nLen)
273 {
274 _tcsncpy(pszDest, pszSrc, nLen - 1);
275 pszDest[nLen - 1] = 0;
276 return pszDest;
277 }
278#endif
279#else
280 TCHAR LIBNETXMS_EXPORTABLE *nx_strncpy(TCHAR *pszDest, TCHAR *pszSrc, int nLen);
281#endif
282
283#ifdef __cplusplus
e9246d26
VK
284extern "C"
285{
b50f1100 286#endif
08043edf
VK
287 int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int);
288 int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
289 size_t nSize, int nFlags, DWORD dwTimeout);
b24955fb 290
c5b523e2
VK
291#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
292 DWORD LIBNETXMS_EXPORTABLE __bswap_32(DWORD dwVal);
293#endif
f52be742 294#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
64584976
VK
295 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
296#endif
9f20696e 297 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
babb8280 298 void LIBNETXMS_EXPORTABLE __bswap_wstr(WCHAR *pStr);
64584976 299
076a55c4 300#if !defined(_WIN32) && !defined(_NETWARE)
018fda4d 301 void LIBNETXMS_EXPORTABLE strupr(TCHAR *in);
7e679c4b 302#endif
ab588c84 303
670c7f3d 304 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
ddce77b0 305 QWORD LIBNETXMS_EXPORTABLE FileSize(TCHAR *pszFileName);
670c7f3d 306
64584976 307 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
018fda4d 308 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
d0fc56c6 309 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(TCHAR *pszName);
a1dc75b0 310
64584976 311 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
f218dc6d 312 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
31521248 313
e25cadc9 314 TCHAR LIBNETXMS_EXPORTABLE *BinToStr(BYTE *pData, DWORD dwSize, TCHAR *pStr);
4075c782 315 DWORD LIBNETXMS_EXPORTABLE StrToBin(TCHAR *pStr, BYTE *pData, DWORD dwSize);
c61f5190 316 void LIBNETXMS_EXPORTABLE MACToStr(BYTE *pData, TCHAR *pStr);
b50f1100 317
018fda4d
AK
318 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
319 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
320 TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(TCHAR *line, TCHAR *buffer);
fa2b47a7
VK
321 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName);
322 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
ff5e8be5 323 void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, TCHAR *pszSubStr, TCHAR *pszReplace);
257defd7 324 TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(TCHAR *pszFileName);
03ed7e4f 325 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer);
64584976 326
6120112b 327 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
a3c76aba
VK
328 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
329 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
b40371d4
AK
330 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(TCHAR *pszFileName, BYTE *pHash);
331 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(TCHAR *pszFileName, BYTE *pHash);
6120112b 332 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(TCHAR *pszFileName, DWORD *pResult);
66c04cc7 333
46ef501a
VK
334 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
335 DWORD *pdwRTT, DWORD dwPacketSize);
4826c8a5 336
018fda4d 337 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(TCHAR *pszFileName, TCHAR *pszSection,
6849d9be 338 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);
125c0e81 339 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(TCHAR *pszText);
aa13cfd9 340
018fda4d 341 HMODULE LIBNETXMS_EXPORTABLE DLOpen(TCHAR *szLibName, TCHAR *pszErrorText);
aa13cfd9 342 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
018fda4d 343 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, TCHAR *szSymbol, TCHAR *pszErrorText);
aa13cfd9 344
ae9cddaf
VK
345 void LIBNETXMS_EXPORTABLE InitSubAgentsLogger(void (* pFunc)(int, TCHAR *));
346
aa13cfd9 347#ifdef _WIN32
018fda4d 348 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, int iBufSize);
aa13cfd9 349#endif
696fc54f
VK
350
351#if !(HAVE_DAEMON)
b1a57444 352 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 353#endif
4332e9d8 354
268774a0
VK
355 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(WCHAR *pszAddr);
356
4332e9d8 357#ifndef _WIN32
4332e9d8
VK
358 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, WCHAR *pWideCharStr,
359 int cchWideChar, char *pByteStr, int cchByteChar,
360 char *pDefaultChar, BOOL *pbUsedDefChar);
361 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, char *pByteStr,
362 int cchByteChar, WCHAR *pWideCharStr,
363 int cchWideChar);
364#endif
b50f1100 365
dde96745
VK
366#ifdef UNICODE
367INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
368QWORD LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
369#else
b50f1100
VK
370#if !(HAVE_STRTOLL)
371 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
372#endif
373#if !(HAVE_STRTOULL)
374 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
375#endif
dde96745 376#endif
b50f1100 377
3d1cb7d6 378#ifdef _WIN32
37d62364 379#ifndef SWIGPERL
3d1cb7d6 380 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 381 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6
VK
382 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
383#endif
37d62364 384#endif
3d1cb7d6 385
7f9e0c00
VK
386#if defined(_WIN32) || !(HAVE_SCANDIR)
387 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
388 int (*select)(const struct dirent *),
389 int (*compar)(const struct dirent **, const struct dirent **));
390 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
391#endif
392
55b33d4c
VK
393#ifdef UNDER_CE
394 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
395 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
396 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
397#endif
398
b71bff93 399#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
953dfe76 400void LIBNETXMS_EXPORTABLE StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
45c13da4 401 THREAD_RESULT (THREAD_CALL * pfMain)(void *));
953dfe76
VK
402#endif
403
b50f1100 404#ifdef __cplusplus
e9246d26 405}
b50f1100 406#endif
e9246d26
VK
407
408#endif /* _nms_util_h_ */
70fed300
AK
409
410///////////////////////////////////////////////////////////////////////////////
411/*
412
413$Log: not supported by cvs2svn $
e25cadc9
VK
414Revision 1.86 2006/09/23 23:49:41 victor
415Console working with UNICODE!
416
dde96745
VK
417Revision 1.85 2006/09/10 06:59:36 victor
418Fixed problmes with Win32 build
419
6d8aee95
VK
420Revision 1.84 2006/09/07 22:02:06 alk
421UNIX version of Serial rewritten
422termio removed from configure (depricated in favour of termio_s_?)
423
70fed300
AK
424
425*/