Minor changes
[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//
85// Configuration item template for configuration loader
86//
87
88typedef struct
89{
018fda4d 90 TCHAR szToken[64];
4826c8a5
VK
91 BYTE iType;
92 BYTE cSeparator; // Separator character for lists
93 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
94 DWORD dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
95 DWORD dwBufferPos; // Should be set to 0
96 void *pBuffer;
97} NX_CFG_TEMPLATE;
dcb44c9a
VK
98
99
100//
3046f9a4
VK
101// getopt() prototype if needed
102//
103
104#ifdef _WIN32
105#include <getopt.h>
106#endif
107
108
109//
f2fdf1b5
VK
110// Win32 API functions missing under WinCE
111//
112
c32b805a 113#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
114
115inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
116{
117 SYSTEMTIME sysTime;
118
119 GetSystemTime(&sysTime);
120 SystemTimeToFileTime(&sysTime, pFt);
121}
122
123#endif // UNDER_CE
124
125
126//
3d1cb7d6
VK
127// Structures for opendir() / readdir() / closedir()
128//
129
130#ifdef _WIN32
131
37d62364
AK
132#ifndef SWIGPERL
133
7f9e0c00 134typedef struct dirent
3d1cb7d6
VK
135{
136 long d_ino; /* inode number (not used by MS-DOS) */
137 int d_namlen; /* Name length */
138 char d_name[257]; /* file name */
139} _DIRECT;
140
141typedef struct _dir_struc
142{
143 char *start; /* Starting position */
144 char *curr; /* Current position */
145 long size; /* Size of string table */
146 long nfiles; /* number if filenames in table */
7f9e0c00 147 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
148} DIR;
149
37d62364
AK
150#endif
151
3d1cb7d6
VK
152#endif /* _WIN32 */
153
154
155//
e9246d26
VK
156// Functions
157//
158
43d92b4c 159
babb8280 160#if WORDS_BIGENDIAN
43d92b4c
VK
161#define htonq(x) (x)
162#define ntohq(x) (x)
9f20696e
VK
163#define htond(x) (x)
164#define ntohd(x) (x)
babb8280
VK
165#define SwapWideString(x)
166#else
167#define htonq(x) __bswap_64(x)
168#define ntohq(x) __bswap_64(x)
169#define htond(x) __bswap_double(x)
170#define ntohd(x) __bswap_double(x)
171#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 172#endif
15929b31 173
55b33d4c
VK
174#ifdef UNDER_CE
175#define close(x) CloseHandle((HANDLE)(x))
176#endif
177
b50f1100 178#ifdef __cplusplus
c81979bc
VK
179#ifndef LIBNETXMS_INLINE
180 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, int nLen)
181 {
182 _tcsncpy(pszDest, pszSrc, nLen - 1);
183 pszDest[nLen - 1] = 0;
184 return pszDest;
185 }
186#endif
187#else
188 TCHAR LIBNETXMS_EXPORTABLE *nx_strncpy(TCHAR *pszDest, TCHAR *pszSrc, int nLen);
189#endif
190
191#ifdef __cplusplus
e9246d26
VK
192extern "C"
193{
b50f1100 194#endif
08043edf
VK
195 int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int);
196 int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
197 size_t nSize, int nFlags, DWORD dwTimeout);
b24955fb 198
f52be742 199#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
64584976
VK
200 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
201#endif
9f20696e 202 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
babb8280 203 void LIBNETXMS_EXPORTABLE __bswap_wstr(WCHAR *pStr);
64584976 204
076a55c4 205#if !defined(_WIN32) && !defined(_NETWARE)
018fda4d 206 void LIBNETXMS_EXPORTABLE strupr(TCHAR *in);
7e679c4b 207#endif
ab588c84 208
670c7f3d 209 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
ddce77b0 210 QWORD LIBNETXMS_EXPORTABLE FileSize(TCHAR *pszFileName);
670c7f3d 211
64584976 212 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
018fda4d 213 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
d0fc56c6 214 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(TCHAR *pszName);
a1dc75b0 215
64584976 216 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
f218dc6d 217 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
31521248 218
4075c782
VK
219 void LIBNETXMS_EXPORTABLE BinToStr(BYTE *pData, DWORD dwSize, TCHAR *pStr);
220 DWORD LIBNETXMS_EXPORTABLE StrToBin(TCHAR *pStr, BYTE *pData, DWORD dwSize);
b50f1100 221
018fda4d
AK
222 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
223 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
224 TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(TCHAR *line, TCHAR *buffer);
fa2b47a7
VK
225 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName);
226 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
ff5e8be5 227 void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, TCHAR *pszSubStr, TCHAR *pszReplace);
257defd7 228 TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(TCHAR *pszFileName);
03ed7e4f 229 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer);
64584976 230
6120112b 231 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
a48063fc
VK
232 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, int nbytes, unsigned char *hash);
233 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, int nbytes, unsigned char *hash);
b40371d4
AK
234 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(TCHAR *pszFileName, BYTE *pHash);
235 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(TCHAR *pszFileName, BYTE *pHash);
6120112b 236 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(TCHAR *pszFileName, DWORD *pResult);
66c04cc7 237
46ef501a
VK
238 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
239 DWORD *pdwRTT, DWORD dwPacketSize);
4826c8a5 240
018fda4d 241 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(TCHAR *pszFileName, TCHAR *pszSection,
6849d9be 242 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);
125c0e81 243 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(TCHAR *pszText);
aa13cfd9 244
018fda4d 245 HMODULE LIBNETXMS_EXPORTABLE DLOpen(TCHAR *szLibName, TCHAR *pszErrorText);
aa13cfd9 246 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
018fda4d 247 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, TCHAR *szSymbol, TCHAR *pszErrorText);
aa13cfd9 248
ae9cddaf
VK
249 void LIBNETXMS_EXPORTABLE InitSubAgentsLogger(void (* pFunc)(int, TCHAR *));
250
aa13cfd9 251#ifdef _WIN32
018fda4d 252 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, int iBufSize);
aa13cfd9 253#endif
696fc54f
VK
254
255#if !(HAVE_DAEMON)
b1a57444 256 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 257#endif
4332e9d8 258
268774a0
VK
259 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(WCHAR *pszAddr);
260
4332e9d8 261#ifndef _WIN32
4332e9d8
VK
262 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, WCHAR *pWideCharStr,
263 int cchWideChar, char *pByteStr, int cchByteChar,
264 char *pDefaultChar, BOOL *pbUsedDefChar);
265 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, char *pByteStr,
266 int cchByteChar, WCHAR *pWideCharStr,
267 int cchWideChar);
268#endif
b50f1100
VK
269
270#if !(HAVE_STRTOLL)
271 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
272#endif
273#if !(HAVE_STRTOULL)
274 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
275#endif
276
3d1cb7d6 277#ifdef _WIN32
37d62364 278#ifndef SWIGPERL
3d1cb7d6 279 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 280 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6
VK
281 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
282#endif
37d62364 283#endif
3d1cb7d6 284
7f9e0c00
VK
285#if defined(_WIN32) || !(HAVE_SCANDIR)
286 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
287 int (*select)(const struct dirent *),
288 int (*compar)(const struct dirent **, const struct dirent **));
289 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
290#endif
291
55b33d4c
VK
292#ifdef UNDER_CE
293 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
294 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
295 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
296#endif
297
b71bff93 298#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
953dfe76 299void LIBNETXMS_EXPORTABLE StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
45c13da4 300 THREAD_RESULT (THREAD_CALL * pfMain)(void *));
953dfe76
VK
301#endif
302
b50f1100 303#ifdef __cplusplus
e9246d26 304}
b50f1100 305#endif
e9246d26
VK
306
307#endif /* _nms_util_h_ */