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