728c14b417da6428ed5876a7e19e5cd05159e249
[public/netxms.git] / include / nms_common.h
1 /*
2 ** NetXMS - Network Management System
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_common.h
20 **
21 **/
22
23 #ifndef _nms_common_h_
24 #define _nms_common_h_
25
26 #include <ctype.h>
27
28
29 //
30 // Version constants
31 //
32
33 #define NETXMS_VERSION_MAJOR 1
34 #define NETXMS_VERSION_MINOR 0
35 #define NETXMS_VERSION_STRING "1.0"
36
37
38 //
39 // Common constants
40 //
41
42 #define MAX_SECRET_LENGTH 64
43 #define INVALID_POINTER_VALUE ((void *)0xFFFFFFFF)
44 #define MAX_DB_STRING 256
45
46
47 //
48 // Platform dependent includes and defines
49 //
50
51 #if defined(_WIN32)
52
53 /********** WINDOWS ********************/
54
55 #define FS_PATH_SEPARATOR "\\"
56
57 #include <windows.h>
58 #include <fcntl.h>
59
60 typedef unsigned __int64 QWORD;
61 typedef __int64 INT64;
62 typedef int socklen_t;
63
64 #define snprintf _snprintf
65 #define vsnprintf _vsnprintf
66
67 // Socket compatibility
68 #define SHUT_RD 0
69 #define SHUT_WR 1
70 #define SHUT_RDWR 2
71
72 #elif defined(_NETWARE)
73
74 /********** NETWARE ********************/
75
76 #include <unistd.h>
77 #include <stdlib.h>
78 #include <sys/types.h>
79 #include <sys/socket.h>
80 #include <sys/select.h>
81 #include <netinet/in.h>
82 #include <arpa/inet.h>
83 #include <stdint.h>
84 #include <fcntl.h>
85
86 typedef int BOOL;
87 typedef long int LONG;
88 typedef unsigned long DWORD;
89 typedef unsigned short WORD;
90 typedef unsigned char BYTE;
91 typedef void * HANDLE;
92 typedef void * HMODULE;
93
94 #ifdef X_INT64_X
95 typedef X_INT64_X INT64;
96 #else
97 #error Target system does not have signed 64bit integer type
98 #endif
99
100 #ifdef X_UINT64_X
101 typedef X_UINT64_X QWORD;
102 #else
103 #error Target system does not have unsigned 64bit integer type
104 #endif
105
106 #ifndef MAX_PATH
107 #define MAX_PATH 256
108 #endif
109
110 // Socket compatibility
111 typedef int SOCKET;
112
113 #define closesocket(x) close(x)
114 #define WSAGetLastError() (errno)
115
116 #define WSAEINTR EINTR
117
118 #else /* not _WIN32 and not _NETWARE */
119
120 /*********** UNIX *********************/
121
122 #define FS_PATH_SEPARATOR "/"
123
124 #include <config.h>
125
126 #if HAVE_UNISTD_H
127 #include <unistd.h>
128 #endif
129
130 #if HAVE_STDLIB_H
131 #include <stdlib.h>
132 #endif
133
134 #if HAVE_SYS_TYPES_H
135 #include <sys/types.h>
136 #endif
137
138 #if HAVE_SYS_INT_TYPES_H
139 #include <sys/int_types.h>
140 #endif
141
142 #if HAVE_SYS_SOCKET_H
143 #include <sys/socket.h>
144 #endif
145
146 #if HAVE_SYS_SELECT_H
147 #include <sys/select.h>
148 #endif
149
150 #if HAVE_NETINET_IN_H
151 #include <netinet/in.h>
152 #endif
153
154 #if HAVE_ARPA_INET_H
155 #include <arpa/inet.h>
156 #endif
157
158 #if HAVE_NETDB_H
159 #include <netdb.h>
160 #endif
161
162 typedef int BOOL;
163 typedef long int LONG;
164 typedef unsigned long DWORD;
165 typedef unsigned short WORD;
166 typedef unsigned char BYTE;
167 typedef void * HANDLE;
168 typedef void * HMODULE;
169
170 #if HAVE_INT64_T
171 typedef int64_t INT64;
172 #else
173 #error Target system does not have signed 64bit integer type
174 #endif
175
176 #if HAVE_UINT64_T
177 typedef uint64_t QWORD;
178 #elif HAVE_U_INT64_T
179 typedef u_int64_t QWORD;
180 #else
181 #error Target system does not have unsigned 64bit integer type
182 #endif
183
184
185 #define TRUE 1
186 #define FALSE 0
187
188 #ifndef MAX_PATH
189 #define MAX_PATH 256
190 #endif
191
192 // Socket compatibility
193 typedef int SOCKET;
194
195 #define closesocket(x) close(x)
196 #define WSAGetLastError() (errno)
197
198 #define WSAEINTR EINTR
199
200 #endif /* _WIN32 */
201
202
203 //
204 // open() flags compatibility
205 //
206
207 #ifndef O_BINARY
208 #define O_BINARY 0
209 #endif
210
211
212 //
213 // Event log severity codes (UNIX only)
214 //
215
216 #ifndef _WIN32
217 #define EVENTLOG_INFORMATION_TYPE 0
218 #define EVENTLOG_WARNING_TYPE 1
219 #define EVENTLOG_ERROR_TYPE 2
220 #endif /* _WIN32 */
221
222
223 //
224 // Interface types
225 //
226
227 #define IFTYPE_OTHER 1
228 #define IFTYPE_REGULAR1822 2
229 #define IFTYPE_HDH1822 3
230 #define IFTYPE_DDN_X25 4
231 #define IFTYPE_RFC877_X25 5
232 #define IFTYPE_ETHERNET_CSMACD 6
233 #define IFTYPE_ISO88023_CSMACD 7
234 #define IFTYPE_ISO88024_TOKENBUS 8
235 #define IFTYPE_ISO88025_TOKENRING 9
236 #define IFTYPE_ISO88026_MAN 10
237 #define IFTYPE_STARLAN 11
238 #define IFTYPE_PROTEON_10MBIT 12
239 #define IFTYPE_PROTEON_80MBIT 13
240 #define IFTYPE_HYPERCHANNEL 14
241 #define IFTYPE_FDDI 15
242 #define IFTYPE_LAPB 16
243 #define IFTYPE_SDLC 17
244 #define IFTYPE_DS1 18
245 #define IFTYPE_E1 19
246 #define IFTYPE_BASIC_ISDN 20
247 #define IFTYPE_PRIMARY_ISDN 21
248 #define IFTYPE_PROP_PTP_SERIAL 22
249 #define IFTYPE_PPP 23
250 #define IFTYPE_SOFTWARE_LOOPBACK 24
251 #define IFTYPE_EON 25
252 #define IFTYPE_ETHERNET_3MBIT 26
253 #define IFTYPE_NSIP 27
254 #define IFTYPE_SLIP 28
255 #define IFTYPE_ULTRA 29
256 #define IFTYPE_DS3 30
257 #define IFTYPE_SMDS 31
258 #define IFTYPE_FRAME_RELAY 32
259
260
261 //
262 // IP Header -- RFC 791
263 //
264
265 typedef struct tagIPHDR
266 {
267 BYTE m_cVIHL; // Version and IHL
268 BYTE m_cTOS; // Type Of Service
269 WORD m_wLen; // Total Length
270 WORD m_wId; // Identification
271 WORD m_wFlagOff; // Flags and Fragment Offset
272 BYTE m_cTTL; // Time To Live
273 BYTE m_cProtocol; // Protocol
274 WORD m_wChecksum; // Checksum
275 struct in_addr m_iaSrc; // Internet Address - Source
276 struct in_addr m_iaDst; // Internet Address - Destination
277 } IPHDR;
278
279
280 //
281 // ICMP Header - RFC 792
282 //
283
284 typedef struct tagICMPHDR
285 {
286 BYTE m_cType; // Type
287 BYTE m_cCode; // Code
288 WORD m_wChecksum; // Checksum
289 WORD m_wId; // Identification
290 WORD m_wSeq; // Sequence
291 char m_cData[1]; // Data
292 } ICMPHDR;
293
294
295 //
296 // INADDR_NONE can be undefined on some systems
297 //
298
299 #ifndef INADDR_NONE
300 #define INADDR_NONE (0xFFFFFFFF)
301 #endif
302
303
304 //
305 // Check if IP address is a broadcast
306 //
307
308 #define IsBroadcastAddress(addr, mask) (((addr) & (~(mask))) == (~(mask)))
309
310
311 //
312 // Check if given string is NULL and always return valid pointer
313 //
314
315 #define CHECK_NULL(x) ((x) == NULL ? ((char *)"(null)") : (x))
316 #define CHECK_NULL_EX(x) ((x) == NULL ? ((char *)"") : (x))
317
318
319 //
320 // Free memory block if it isn't NULL
321 //
322
323 #define safe_free(x) { if ((x) != NULL) free(x); }
324
325
326 //
327 // Convert half-byte's value to hex digit and vice versa
328 //
329
330 #define bin2hex(x) ((x) < 10 ? ((x) + '0') : ((x) + ('A' - 10)))
331 #define hex2bin(x) ((((x) >= '0') && ((x) <= '9')) ? ((x) - '0') : \
332 (((toupper(x) >= 'A') && (toupper(x) <= 'F')) ? (toupper(x) - 'A' + 10) : 0))
333
334
335 //
336 // Define min() and max() if needed
337 //
338
339 #ifndef min
340 # define min(a, b) ((a) < (b) ? (a) : (b))
341 #endif
342 #ifndef max
343 # define max(a, b) ((a) > (b) ? (a) : (b))
344 #endif
345
346
347 //
348 // Define stricmp() for non-windows
349 //
350
351 #ifndef _WIN32
352 #define stricmp strcasecmp
353 #endif
354
355
356 //
357 // Compare two numbers and return -1, 0, or 1
358 //
359
360 #define COMPARE_NUMBERS(n1,n2) (((n1) < (n2)) ? -1 : (((n1) > (n2)) ? 1 : 0))
361
362
363 //
364 // Increment pointer for given number of bytes
365 //
366
367 #define inc_ptr(ptr, step, ptype) ptr = (ptype *)(((char *)ptr) + step)
368
369 #endif /* _nms_common_h_ */