*** empty log message ***
[public/netxms.git] / include / nms_util.h
CommitLineData
70fed300
AK
1/* $Id: nms_util.h,v 1.84 2006-09-07 22:02:06 alk Exp $ */
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
70fed300
AK
44#ifdef HAVE_TERMIOS_H
45# include <termios.h>
46#else
47# error termios.h not found
48#endif
49
f52be742
VK
50#if HAVE_BYTESWAP_H
51#include <byteswap.h>
52#endif
53
5d63b3c1 54#define INVALID_INDEX 0xFFFFFFFF
28dd92c9 55#define CSCP_TEMP_BUF_SIZE 65536
89bfdd9c 56#define MD5_DIGEST_SIZE 16
d0b249a8 57#define SHA1_DIGEST_SIZE 20
5d63b3c1
VK
58
59
60//
66c04cc7
VK
61// Return codes for IcmpPing()
62//
63
64#define ICMP_SUCCESS 0
65#define ICMP_UNREACHEABLE 1
66#define ICMP_TIMEOUT 2
67#define ICMP_RAW_SOCK_FAILED 3
68
69
70//
4826c8a5 71// Token types for configuration loader
dcb44c9a
VK
72//
73
4826c8a5
VK
74#define CT_LONG 0
75#define CT_STRING 1
76#define CT_STRING_LIST 2
77#define CT_END_OF_LIST 3
78#define CT_BOOLEAN 4
79#define CT_WORD 5
fc935a60 80#define CT_IGNORE 6
4826c8a5
VK
81
82
83//
84// Return codes for NxLoadConfig()
85//
86
87#define NXCFG_ERR_OK 0
88#define NXCFG_ERR_NOFILE 1
89#define NXCFG_ERR_SYNTAX 2
90
91
92//
b160350d
VK
93// Class for serial communications
94//
95
96#ifdef __cplusplus
97
98#ifndef _WIN32
99enum
100{
101 NOPARITY,
102 ODDPARITY,
103 EVENPARITY,
104 ONESTOPBIT,
105 TWOSTOPBITS
106};
107
70fed300
AK
108enum
109{
110 FLOW_NONE,
111 FLOW_HARDWARE,
112 FLOW_SOFTWARE
113};
114
b160350d
VK
115#ifndef INVALID_HANDLE_VALUE
116#define INVALID_HANDLE_VALUE (-1)
117#endif
118#endif /* _WIN32 */
119
120class LIBNETXMS_EXPORTABLE Serial
121{
122public:
123 Serial(void);
124 ~Serial(void);
125
126 bool Open(TCHAR *pszPort);
127 void Close(void);
128 void SetTimeout(int nTimeout);
2f06a66e 129 int Read(char *pBuff, int nSize);
b160350d
VK
130 bool Write(char *pBuff, int nSize);
131 void Flush(void);
132 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits);
70fed300 133 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl);
b160350d
VK
134 bool Restart(void);
135
136private:
137 TCHAR *m_pszPort;
138 int m_nTimeout;
139 int m_nSpeed;
140 int m_nDataBits;
141 int m_nStopBits;
142 int m_nParity;
70fed300 143 int m_nFlowControl;
b160350d
VK
144
145#ifndef _WIN32
146 int m_hPort;
70fed300 147 struct termios m_originalSettings;
b160350d
VK
148#else
149 HANDLE m_hPort;
150#endif
151};
152
153#endif /* __cplusplus */
154
155
156//
4826c8a5
VK
157// Configuration item template for configuration loader
158//
159
160typedef struct
161{
018fda4d 162 TCHAR szToken[64];
4826c8a5
VK
163 BYTE iType;
164 BYTE cSeparator; // Separator character for lists
165 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
166 DWORD dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
167 DWORD dwBufferPos; // Should be set to 0
168 void *pBuffer;
169} NX_CFG_TEMPLATE;
dcb44c9a
VK
170
171
172//
3046f9a4
VK
173// getopt() prototype if needed
174//
175
176#ifdef _WIN32
177#include <getopt.h>
178#endif
179
180
181//
f2fdf1b5
VK
182// Win32 API functions missing under WinCE
183//
184
c32b805a 185#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
186
187inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
188{
189 SYSTEMTIME sysTime;
190
191 GetSystemTime(&sysTime);
192 SystemTimeToFileTime(&sysTime, pFt);
193}
194
195#endif // UNDER_CE
196
197
198//
3d1cb7d6
VK
199// Structures for opendir() / readdir() / closedir()
200//
201
202#ifdef _WIN32
203
37d62364
AK
204#ifndef SWIGPERL
205
7f9e0c00 206typedef struct dirent
3d1cb7d6
VK
207{
208 long d_ino; /* inode number (not used by MS-DOS) */
209 int d_namlen; /* Name length */
210 char d_name[257]; /* file name */
211} _DIRECT;
212
213typedef struct _dir_struc
214{
215 char *start; /* Starting position */
216 char *curr; /* Current position */
217 long size; /* Size of string table */
218 long nfiles; /* number if filenames in table */
7f9e0c00 219 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
220} DIR;
221
37d62364
AK
222#endif
223
3d1cb7d6
VK
224#endif /* _WIN32 */
225
226
227//
e9246d26
VK
228// Functions
229//
230
43d92b4c 231
babb8280 232#if WORDS_BIGENDIAN
43d92b4c
VK
233#define htonq(x) (x)
234#define ntohq(x) (x)
9f20696e
VK
235#define htond(x) (x)
236#define ntohd(x) (x)
babb8280
VK
237#define SwapWideString(x)
238#else
239#define htonq(x) __bswap_64(x)
240#define ntohq(x) __bswap_64(x)
241#define htond(x) __bswap_double(x)
242#define ntohd(x) __bswap_double(x)
243#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 244#endif
15929b31 245
55b33d4c
VK
246#ifdef UNDER_CE
247#define close(x) CloseHandle((HANDLE)(x))
248#endif
249
b50f1100 250#ifdef __cplusplus
c81979bc
VK
251#ifndef LIBNETXMS_INLINE
252 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, int nLen)
253 {
254 _tcsncpy(pszDest, pszSrc, nLen - 1);
255 pszDest[nLen - 1] = 0;
256 return pszDest;
257 }
258#endif
259#else
260 TCHAR LIBNETXMS_EXPORTABLE *nx_strncpy(TCHAR *pszDest, TCHAR *pszSrc, int nLen);
261#endif
262
263#ifdef __cplusplus
e9246d26
VK
264extern "C"
265{
b50f1100 266#endif
08043edf
VK
267 int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int);
268 int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
269 size_t nSize, int nFlags, DWORD dwTimeout);
b24955fb 270
c5b523e2
VK
271#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
272 DWORD LIBNETXMS_EXPORTABLE __bswap_32(DWORD dwVal);
273#endif
f52be742 274#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
64584976
VK
275 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
276#endif
9f20696e 277 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
babb8280 278 void LIBNETXMS_EXPORTABLE __bswap_wstr(WCHAR *pStr);
64584976 279
076a55c4 280#if !defined(_WIN32) && !defined(_NETWARE)
018fda4d 281 void LIBNETXMS_EXPORTABLE strupr(TCHAR *in);
7e679c4b 282#endif
ab588c84 283
670c7f3d 284 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
ddce77b0 285 QWORD LIBNETXMS_EXPORTABLE FileSize(TCHAR *pszFileName);
670c7f3d 286
64584976 287 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
018fda4d 288 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
d0fc56c6 289 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(TCHAR *pszName);
a1dc75b0 290
64584976 291 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
f218dc6d 292 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
31521248 293
4075c782
VK
294 void LIBNETXMS_EXPORTABLE BinToStr(BYTE *pData, DWORD dwSize, TCHAR *pStr);
295 DWORD LIBNETXMS_EXPORTABLE StrToBin(TCHAR *pStr, BYTE *pData, DWORD dwSize);
c61f5190 296 void LIBNETXMS_EXPORTABLE MACToStr(BYTE *pData, TCHAR *pStr);
b50f1100 297
018fda4d
AK
298 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
299 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
300 TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(TCHAR *line, TCHAR *buffer);
fa2b47a7
VK
301 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName);
302 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
ff5e8be5 303 void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, TCHAR *pszSubStr, TCHAR *pszReplace);
257defd7 304 TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(TCHAR *pszFileName);
03ed7e4f 305 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer);
64584976 306
6120112b 307 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
a3c76aba
VK
308 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
309 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
b40371d4
AK
310 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(TCHAR *pszFileName, BYTE *pHash);
311 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(TCHAR *pszFileName, BYTE *pHash);
6120112b 312 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(TCHAR *pszFileName, DWORD *pResult);
66c04cc7 313
46ef501a
VK
314 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
315 DWORD *pdwRTT, DWORD dwPacketSize);
4826c8a5 316
018fda4d 317 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(TCHAR *pszFileName, TCHAR *pszSection,
6849d9be 318 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);
125c0e81 319 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(TCHAR *pszText);
aa13cfd9 320
018fda4d 321 HMODULE LIBNETXMS_EXPORTABLE DLOpen(TCHAR *szLibName, TCHAR *pszErrorText);
aa13cfd9 322 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
018fda4d 323 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, TCHAR *szSymbol, TCHAR *pszErrorText);
aa13cfd9 324
ae9cddaf
VK
325 void LIBNETXMS_EXPORTABLE InitSubAgentsLogger(void (* pFunc)(int, TCHAR *));
326
aa13cfd9 327#ifdef _WIN32
018fda4d 328 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, int iBufSize);
aa13cfd9 329#endif
696fc54f
VK
330
331#if !(HAVE_DAEMON)
b1a57444 332 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 333#endif
4332e9d8 334
268774a0
VK
335 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(WCHAR *pszAddr);
336
4332e9d8 337#ifndef _WIN32
4332e9d8
VK
338 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, WCHAR *pWideCharStr,
339 int cchWideChar, char *pByteStr, int cchByteChar,
340 char *pDefaultChar, BOOL *pbUsedDefChar);
341 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, char *pByteStr,
342 int cchByteChar, WCHAR *pWideCharStr,
343 int cchWideChar);
344#endif
b50f1100
VK
345
346#if !(HAVE_STRTOLL)
347 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
348#endif
349#if !(HAVE_STRTOULL)
350 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
351#endif
352
3d1cb7d6 353#ifdef _WIN32
37d62364 354#ifndef SWIGPERL
3d1cb7d6 355 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 356 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6
VK
357 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
358#endif
37d62364 359#endif
3d1cb7d6 360
7f9e0c00
VK
361#if defined(_WIN32) || !(HAVE_SCANDIR)
362 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
363 int (*select)(const struct dirent *),
364 int (*compar)(const struct dirent **, const struct dirent **));
365 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
366#endif
367
55b33d4c
VK
368#ifdef UNDER_CE
369 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
370 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
371 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
372#endif
373
b71bff93 374#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
953dfe76 375void LIBNETXMS_EXPORTABLE StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
45c13da4 376 THREAD_RESULT (THREAD_CALL * pfMain)(void *));
953dfe76
VK
377#endif
378
b50f1100 379#ifdef __cplusplus
e9246d26 380}
b50f1100 381#endif
e9246d26
VK
382
383#endif /* _nms_util_h_ */
70fed300
AK
384
385///////////////////////////////////////////////////////////////////////////////
386/*
387
388$Log: not supported by cvs2svn $
389
390*/