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