MySQL driver now compiles under Linux
[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>
5fad5c32 39#include <nms_threads.h>
64584976 40#include <time.h>
e9246d26 41
5d63b3c1
VK
42#define INVALID_INDEX 0xFFFFFFFF
43#define CSCP_TEMP_BUF_SIZE 4096
44
45
66c04cc7
VK
46//
47// Return codes for IcmpPing()
48//
49
50#define ICMP_SUCCESS 0
51#define ICMP_UNREACHEABLE 1
52#define ICMP_TIMEOUT 2
53#define ICMP_RAW_SOCK_FAILED 3
54
55
3046f9a4
VK
56//
57// getopt() prototype if needed
58//
59
60#ifdef _WIN32
61#include <getopt.h>
62#endif
63
64
5d63b3c1
VK
65//
66// Temporary buffer structure for RecvCSCPMessage() function
67//
68
69typedef struct
70{
71 DWORD dwBufSize;
72 DWORD dwBufPos;
73 char szBuffer[CSCP_TEMP_BUF_SIZE];
74} CSCP_BUFFER;
f44f6a97 75
e9246d26
VK
76
77//
78// Class for holding CSCP messages
79//
80
64584976 81class LIBNETXMS_EXPORTABLE CSCPMessage
e9246d26
VK
82{
83private:
84 WORD m_wCode;
85 DWORD m_dwId;
86 DWORD m_dwNumVar; // Number of variables
87 CSCP_DF **m_ppVarList; // List of variables
88
93b18807 89 void Set(DWORD dwVarId, BYTE bType, void *pValue, DWORD dwSize = 0);
a5f8dbb8
VK
90 void *Get(DWORD dwVarId, BYTE bType);
91 DWORD FindVariable(DWORD dwVarId);
e9246d26
VK
92
93public:
94 CSCPMessage();
95 CSCPMessage(CSCP_MESSAGE *pMsg);
96 ~CSCPMessage();
97
98 CSCP_MESSAGE *CreateMessage(void);
99
100 WORD GetCode(void) { return m_wCode; }
101 void SetCode(WORD wCode) { m_wCode = wCode; }
102
103 DWORD GetId(void) { return m_dwId; }
104 void SetId(DWORD dwId) { m_dwId = dwId; }
105
a5f8dbb8 106 BOOL IsVariableExist(DWORD dwVarId) { return (FindVariable(dwVarId) != INVALID_INDEX) ? TRUE : FALSE; }
f44f6a97 107
a5f8dbb8
VK
108 void SetVariable(DWORD dwVarId, WORD wValue) { Set(dwVarId, DT_INT16, &wValue); }
109 void SetVariable(DWORD dwVarId, DWORD dwValue) { Set(dwVarId, DT_INTEGER, &dwValue); }
110 void SetVariable(DWORD dwVarId, QWORD qwValue) { Set(dwVarId, DT_INT64, &qwValue); }
111 void SetVariable(DWORD dwVarId, char *szValue) { Set(dwVarId, DT_STRING, szValue); }
93b18807 112 void SetVariable(DWORD dwVarId, BYTE *pValue, DWORD dwSize) { Set(dwVarId, DT_BINARY, pValue, dwSize); }
e9246d26 113
a5f8dbb8
VK
114 DWORD GetVariableLong(DWORD dwVarId);
115 QWORD GetVariableInt64(DWORD dwVarId);
116 WORD GetVariableShort(DWORD dwVarId);
117 char *GetVariableStr(DWORD dwVarId, char *szBuffer = NULL, DWORD dwBufSize = 0);
93b18807 118 DWORD GetVariableBinary(DWORD dwVarId, BYTE *pBuffer, DWORD dwBufSize);
b5972f5d
VK
119
120 void DeleteAllVariables(void);
e9246d26
VK
121};
122
123
5fad5c32
VK
124//
125// Queue class
126//
127
64584976 128class LIBNETXMS_EXPORTABLE Queue
5fad5c32
VK
129{
130private:
131 MUTEX m_hQueueAccess;
132 CONDITION m_hConditionNotEmpty;
133 void **m_pElements;
134 DWORD m_dwNumElements;
135 DWORD m_dwBufferSize;
136 DWORD m_dwFirst;
137 DWORD m_dwLast;
138 DWORD m_dwBufferIncrement;
139
140 void Lock(void) { MutexLock(m_hQueueAccess, INFINITE); }
141 void Unlock(void) { MutexUnlock(m_hQueueAccess); }
142
143public:
144 Queue(DWORD dwInitialSize = 256, DWORD dwBufferIncrement = 32);
145 ~Queue();
146
147 void Put(void *pObject);
148 void *Get(void);
149 void *GetOrBlock(void);
150 DWORD Size(void) { return m_dwNumElements; }
151};
152
153
e9246d26
VK
154//
155// Functions
156//
157
43d92b4c
VK
158
159#if __BYTE_ORDER == __LITTLE_ENDIAN
160#define htonq(x) __bswap_64(x)
161#define ntohq(x) __bswap_64(x)
162#else
163#define htonq(x) (x)
164#define ntohq(x) (x)
165#endif
15929b31 166
e9246d26
VK
167extern "C"
168{
7e679c4b 169#ifdef _WIN32
64584976
VK
170 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
171#endif
172
173#ifndef _WIN32
174 void LIBNETXMS_EXPORTABLE strupr(char *in);
7e679c4b 175#endif
ab588c84 176
670c7f3d
VK
177 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
178
64584976
VK
179 int LIBNETXMS_EXPORTABLE RecvCSCPMessage(SOCKET hSocket, CSCP_MESSAGE *pMsg, CSCP_BUFFER *pBuffer);
180 CSCP_MESSAGE LIBNETXMS_EXPORTABLE *CreateRawCSCPMessage(WORD wCode, DWORD dwId, DWORD dwDataSize, void *pData, CSCP_MESSAGE *pBuffer);
a1dc75b0 181
64584976
VK
182 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
183 char LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, char *szBuffer);
a1dc75b0 184
64584976
VK
185 void LIBNETXMS_EXPORTABLE *MemAlloc(DWORD dwSize);
186 void LIBNETXMS_EXPORTABLE *MemReAlloc(void *pBlock, DWORD dwNewSize);
187 void LIBNETXMS_EXPORTABLE MemFree(void *pBlock);
93b18807 188
64584976
VK
189 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
190 char LIBNETXMS_EXPORTABLE *nx_strdup(const char *pSrc);
31521248 191
64584976
VK
192 void LIBNETXMS_EXPORTABLE StrStrip(char *pszStr);
193 BOOL LIBNETXMS_EXPORTABLE MatchString(const char *pattern, const char *string, BOOL matchCase);
194
195 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, DWORD nbytes);
66c04cc7
VK
196
197 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout, DWORD *pdwRTT);
e9246d26
VK
198}
199
200#endif /* _nms_util_h_ */