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