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