Windows compatibility fixes
[public/netxms.git] / include / nms_util.h
1 /*
2 ** NetXMS - Network Management System
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
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
37 #include <nms_common.h>
38 #include <nms_cscp.h>
39 #include <time.h>
40
41 #if HAVE_BYTESWAP_H
42 #include <byteswap.h>
43 #endif
44
45 #define INVALID_INDEX 0xFFFFFFFF
46 #define CSCP_TEMP_BUF_SIZE 65536
47 #define MD5_DIGEST_SIZE 16
48 #define SHA1_DIGEST_SIZE 20
49
50
51 //
52 // Return codes for IcmpPing()
53 //
54
55 #define ICMP_SUCCESS 0
56 #define ICMP_UNREACHEABLE 1
57 #define ICMP_TIMEOUT 2
58 #define ICMP_RAW_SOCK_FAILED 3
59
60
61 //
62 // Token types for configuration loader
63 //
64
65 #define CT_LONG 0
66 #define CT_STRING 1
67 #define CT_STRING_LIST 2
68 #define CT_END_OF_LIST 3
69 #define CT_BOOLEAN 4
70 #define CT_WORD 5
71 #define CT_IGNORE 6
72
73
74 //
75 // Return codes for NxLoadConfig()
76 //
77
78 #define NXCFG_ERR_OK 0
79 #define NXCFG_ERR_NOFILE 1
80 #define NXCFG_ERR_SYNTAX 2
81
82
83 //
84 // Configuration item template for configuration loader
85 //
86
87 typedef struct
88 {
89 TCHAR szToken[64];
90 BYTE iType;
91 BYTE cSeparator; // Separator character for lists
92 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
93 DWORD dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
94 DWORD dwBufferPos; // Should be set to 0
95 void *pBuffer;
96 } NX_CFG_TEMPLATE;
97
98
99 //
100 // getopt() prototype if needed
101 //
102
103 #ifdef _WIN32
104 #include <getopt.h>
105 #endif
106
107
108 //
109 // Win32 API functions missing under WinCE
110 //
111
112 #ifdef UNDER_CE
113
114 inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
115 {
116 SYSTEMTIME sysTime;
117
118 GetSystemTime(&sysTime);
119 SystemTimeToFileTime(&sysTime, pFt);
120 }
121
122 #endif // UNDER_CE
123
124
125 //
126 // Structures for opendir() / readdir() / closedir()
127 //
128
129 #ifdef _WIN32
130
131 typedef struct dirent
132 {
133 long d_ino; /* inode number (not used by MS-DOS) */
134 int d_namlen; /* Name length */
135 char d_name[257]; /* file name */
136 } _DIRECT;
137
138 typedef struct _dir_struc
139 {
140 char *start; /* Starting position */
141 char *curr; /* Current position */
142 long size; /* Size of string table */
143 long nfiles; /* number if filenames in table */
144 struct dirent dirstr; /* Directory structure to return */
145 } DIR;
146
147 #endif /* _WIN32 */
148
149
150 //
151 // Functions
152 //
153
154
155 #if WORDS_BIGENDIAN
156 #define htonq(x) (x)
157 #define ntohq(x) (x)
158 #define htond(x) (x)
159 #define ntohd(x) (x)
160 #define SwapWideString(x)
161 #else
162 #define htonq(x) __bswap_64(x)
163 #define ntohq(x) __bswap_64(x)
164 #define htond(x) __bswap_double(x)
165 #define ntohd(x) __bswap_double(x)
166 #define SwapWideString(x) __bswap_wstr(x)
167 #endif
168
169 #ifdef __cplusplus
170 extern "C"
171 {
172 #endif
173 int LIBNETXMS_EXPORTABLE SendEx(int, const void *, size_t, int);
174
175 #if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
176 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
177 #endif
178 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
179 void LIBNETXMS_EXPORTABLE __bswap_wstr(WCHAR *pStr);
180
181 #if !defined(_WIN32) && !defined(_NETWARE)
182 void LIBNETXMS_EXPORTABLE strupr(TCHAR *in);
183 #endif
184
185 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
186 QWORD LIBNETXMS_EXPORTABLE FileSize(TCHAR *pszFileName);
187
188 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
189 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
190
191 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
192 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
193
194 void LIBNETXMS_EXPORTABLE BinToStr(BYTE *pData, DWORD dwSize, char *pStr);
195 DWORD LIBNETXMS_EXPORTABLE StrToBin(char *pStr, BYTE *pData, DWORD dwSize);
196
197 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
198 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
199 TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(TCHAR *line, TCHAR *buffer);
200 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(TCHAR *pszName);
201 void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, TCHAR *pszSubStr, TCHAR *pszReplace);
202 TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(TCHAR *pszFileName);
203
204 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
205 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, int nbytes, unsigned char *hash);
206 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, int nbytes, unsigned char *hash);
207 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(TCHAR *pszFileName, BYTE *pHash);
208 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(TCHAR *pszFileName, BYTE *pHash);
209 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(TCHAR *pszFileName, DWORD *pResult);
210
211 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout, DWORD *pdwRTT);
212
213 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(TCHAR *pszFileName, TCHAR *pszSection,
214 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);
215 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(TCHAR *pszText);
216
217 HMODULE LIBNETXMS_EXPORTABLE DLOpen(TCHAR *szLibName, TCHAR *pszErrorText);
218 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
219 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, TCHAR *szSymbol, TCHAR *pszErrorText);
220
221 void LIBNETXMS_EXPORTABLE InitSubAgentsLogger(void (* pFunc)(int, TCHAR *));
222
223 #ifdef _WIN32
224 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, int iBufSize);
225 #endif
226
227 #if !(HAVE_DAEMON)
228 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
229 #endif
230
231 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(WCHAR *pszAddr);
232
233 #ifndef _WIN32
234 #if !(HAVE_WCSLEN)
235 int LIBNETXMS_EXPORTABLE wcslen(WCHAR *pStr);
236 #endif
237 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, WCHAR *pWideCharStr,
238 int cchWideChar, char *pByteStr, int cchByteChar,
239 char *pDefaultChar, BOOL *pbUsedDefChar);
240 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, char *pByteStr,
241 int cchByteChar, WCHAR *pWideCharStr,
242 int cchWideChar);
243 #endif
244
245 #if !(HAVE_STRTOLL)
246 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
247 #endif
248 #if !(HAVE_STRTOULL)
249 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
250 #endif
251
252 #ifdef _WIN32
253 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
254 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
255 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
256 #endif
257
258 #if defined(_WIN32) || !(HAVE_SCANDIR)
259 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
260 int (*select)(const struct dirent *),
261 int (*compar)(const struct dirent **, const struct dirent **));
262 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
263 #endif
264
265 #ifdef __cplusplus
266 }
267 #endif
268
269 #endif /* _nms_util_h_ */