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