Implemented correct translation of SNMPv1 trap id to SNMPv2 trap id
[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>
64584976 39#include <time.h>
e9246d26 40
f52be742
VK
41#if HAVE_BYTESWAP_H
42#include <byteswap.h>
43#endif
44
5d63b3c1 45#define INVALID_INDEX 0xFFFFFFFF
28dd92c9 46#define CSCP_TEMP_BUF_SIZE 65536
89bfdd9c 47#define MD5_DIGEST_SIZE 16
d0b249a8 48#define SHA1_DIGEST_SIZE 20
5d63b3c1
VK
49
50
66c04cc7
VK
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
dcb44c9a 61//
4826c8a5 62// Token types for configuration loader
dcb44c9a
VK
63//
64
4826c8a5
VK
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
fc935a60 71#define CT_IGNORE 6
4826c8a5
VK
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
87typedef struct
88{
018fda4d 89 TCHAR szToken[64];
4826c8a5
VK
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;
dcb44c9a
VK
97
98
3046f9a4
VK
99//
100// getopt() prototype if needed
101//
102
103#ifdef _WIN32
104#include <getopt.h>
105#endif
106
107
f2fdf1b5
VK
108//
109// Win32 API functions missing under WinCE
110//
111
c32b805a 112#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
113
114inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
115{
116 SYSTEMTIME sysTime;
117
118 GetSystemTime(&sysTime);
119 SystemTimeToFileTime(&sysTime, pFt);
120}
121
122#endif // UNDER_CE
123
124
3d1cb7d6
VK
125//
126// Structures for opendir() / readdir() / closedir()
127//
128
129#ifdef _WIN32
130
7f9e0c00 131typedef struct dirent
3d1cb7d6
VK
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
138typedef 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 */
7f9e0c00 144 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
145} DIR;
146
147#endif /* _WIN32 */
148
149
e9246d26
VK
150//
151// Functions
152//
153
43d92b4c 154
babb8280 155#if WORDS_BIGENDIAN
43d92b4c
VK
156#define htonq(x) (x)
157#define ntohq(x) (x)
9f20696e
VK
158#define htond(x) (x)
159#define ntohd(x) (x)
babb8280
VK
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)
43d92b4c 167#endif
15929b31 168
c81979bc
VK
169#ifdef __cplusplus
170#ifndef LIBNETXMS_INLINE
171 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, int nLen)
172 {
173 _tcsncpy(pszDest, pszSrc, nLen - 1);
174 pszDest[nLen - 1] = 0;
175 return pszDest;
176 }
177#endif
178#else
179 TCHAR LIBNETXMS_EXPORTABLE *nx_strncpy(TCHAR *pszDest, TCHAR *pszSrc, int nLen);
180#endif
181
b50f1100 182#ifdef __cplusplus
e9246d26
VK
183extern "C"
184{
b50f1100 185#endif
b24955fb
VK
186 int LIBNETXMS_EXPORTABLE SendEx(int, const void *, size_t, int);
187
f52be742 188#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
64584976
VK
189 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
190#endif
9f20696e 191 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
babb8280 192 void LIBNETXMS_EXPORTABLE __bswap_wstr(WCHAR *pStr);
64584976 193
076a55c4 194#if !defined(_WIN32) && !defined(_NETWARE)
018fda4d 195 void LIBNETXMS_EXPORTABLE strupr(TCHAR *in);
7e679c4b 196#endif
ab588c84 197
670c7f3d 198 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
ddce77b0 199 QWORD LIBNETXMS_EXPORTABLE FileSize(TCHAR *pszFileName);
670c7f3d 200
64584976 201 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
018fda4d 202 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
d0fc56c6 203 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(TCHAR *pszName);
a1dc75b0 204
64584976 205 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
f218dc6d 206 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
31521248 207
4075c782
VK
208 void LIBNETXMS_EXPORTABLE BinToStr(BYTE *pData, DWORD dwSize, TCHAR *pStr);
209 DWORD LIBNETXMS_EXPORTABLE StrToBin(TCHAR *pStr, BYTE *pData, DWORD dwSize);
b50f1100 210
018fda4d
AK
211 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
212 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
213 TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(TCHAR *line, TCHAR *buffer);
214 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(TCHAR *pszName);
ff5e8be5 215 void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, TCHAR *pszSubStr, TCHAR *pszReplace);
257defd7 216 TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(TCHAR *pszFileName);
03ed7e4f 217 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer);
64584976 218
6120112b 219 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
a48063fc
VK
220 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, int nbytes, unsigned char *hash);
221 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, int nbytes, unsigned char *hash);
b40371d4
AK
222 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(TCHAR *pszFileName, BYTE *pHash);
223 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(TCHAR *pszFileName, BYTE *pHash);
6120112b 224 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(TCHAR *pszFileName, DWORD *pResult);
66c04cc7 225
46ef501a
VK
226 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
227 DWORD *pdwRTT, DWORD dwPacketSize);
4826c8a5 228
018fda4d 229 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(TCHAR *pszFileName, TCHAR *pszSection,
6849d9be 230 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);
125c0e81 231 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(TCHAR *pszText);
aa13cfd9 232
018fda4d 233 HMODULE LIBNETXMS_EXPORTABLE DLOpen(TCHAR *szLibName, TCHAR *pszErrorText);
aa13cfd9 234 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
018fda4d 235 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, TCHAR *szSymbol, TCHAR *pszErrorText);
aa13cfd9 236
ae9cddaf
VK
237 void LIBNETXMS_EXPORTABLE InitSubAgentsLogger(void (* pFunc)(int, TCHAR *));
238
aa13cfd9 239#ifdef _WIN32
018fda4d 240 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, int iBufSize);
aa13cfd9 241#endif
696fc54f
VK
242
243#if !(HAVE_DAEMON)
b1a57444 244 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 245#endif
4332e9d8 246
268774a0
VK
247 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(WCHAR *pszAddr);
248
4332e9d8 249#ifndef _WIN32
4332e9d8
VK
250 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, WCHAR *pWideCharStr,
251 int cchWideChar, char *pByteStr, int cchByteChar,
252 char *pDefaultChar, BOOL *pbUsedDefChar);
253 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, char *pByteStr,
254 int cchByteChar, WCHAR *pWideCharStr,
255 int cchWideChar);
256#endif
b50f1100
VK
257
258#if !(HAVE_STRTOLL)
259 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
260#endif
261#if !(HAVE_STRTOULL)
262 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
263#endif
264
3d1cb7d6
VK
265#ifdef _WIN32
266 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 267 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6
VK
268 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
269#endif
270
7f9e0c00
VK
271#if defined(_WIN32) || !(HAVE_SCANDIR)
272 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
273 int (*select)(const struct dirent *),
274 int (*compar)(const struct dirent **, const struct dirent **));
275 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
276#endif
277
b50f1100 278#ifdef __cplusplus
e9246d26 279}
b50f1100 280#endif
e9246d26
VK
281
282#endif /* _nms_util_h_ */