5178c348d5b68beff83abc32f07783f96e196efc
[public/netxms.git] / include / nms_common.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003, 2004, 2005, 2006, 2007 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 ** File: nms_common.h
20 **
21 **/
22
23 #ifndef _nms_common_h_
24 #define _nms_common_h_
25
26 #ifdef WIN32
27 #ifndef _WIN32
28 #define _WIN32
29 #endif
30 #endif
31
32 #ifdef __SYMBIAN32__
33
34 // Symbian compiler defines _WIN32 in builds for emulator.
35 #undef _WIN32
36
37 #else
38
39 #if !defined(_WIN32) && !defined(UNDER_CE)
40
41 #ifdef _NETWARE
42 #include <config-netware.h>
43 #else
44 #include "config.h"
45 #ifdef WITH_OPENSSL
46 #define _WITH_ENCRYPTION 1
47 #endif
48 #endif
49
50 #else /* _WIN32 */
51
52 #ifndef UNDER_CE
53 #define _WITH_ENCRYPTION 1
54 #if !defined(WINDOWS_ONLY) && !defined(_CRT_SECURE_NO_DEPRECATE)
55 #define _CRT_SECURE_NO_DEPRECATE
56 #endif
57 #endif
58
59 #endif
60
61 #endif /* __SYMBIAN32__ */
62
63 #ifdef HAVE_STDARG_H
64 #include <stdarg.h>
65 #endif
66
67 #include <unicode.h>
68
69 #include <stdio.h>
70 #include <string.h>
71 #include <limits.h>
72
73 #include <netxms-version.h>
74
75
76 //
77 // Define __64BIT__ if compiling for 64bit platform with Visual C++
78 //
79
80 #if defined(_M_X64) || defined(_M_IA64)
81 #ifndef __64BIT__
82 #define __64BIT__
83 #endif
84 #endif
85
86
87 //
88 // Wrappers for 64-bit constants
89 //
90
91 #if defined(__GNUC__) || defined(__HP_aCC)
92 #define _LL(x) (x ## LL)
93 #define _ULL(x) (x ## ULL)
94 #else
95 #define _LL(x) (x)
96 #define _ULL(x) (x)
97 #endif
98
99
100 //
101 // Common constants
102 //
103
104 #define MAX_SECRET_LENGTH 64
105 #define MAX_DB_STRING 256
106 #define MAX_PARAM_NAME 256
107 #define GROUP_FLAG ((DWORD)0x80000000)
108
109 #define NETXMS_MAX_CIPHERS 4
110 #define NETXMS_RSA_KEYLEN 2048
111
112 #ifndef LLONG_MAX
113 #define LLONG_MAX _LL(9223372036854775807)
114 #endif
115
116 #ifndef LLONG_MIN
117 #define LLONG_MIN (-LLONG_MAX - 1)
118 #endif
119
120 #ifndef ULLONG_MAX
121 #define ULLONG_MAX _ULL(18446744073709551615)
122 #endif
123
124 #ifndef EVENTLOG_DEBUG_TYPE
125 #define EVENTLOG_DEBUG_TYPE 0x0080
126 #endif
127
128 #define INVALID_INDEX 0xFFFFFFFF
129 #define MD5_DIGEST_SIZE 16
130 #define SHA1_DIGEST_SIZE 20
131
132
133 //
134 // Compatibility defines for C sources
135 //
136
137 #ifndef __cplusplus
138 typedef int bool;
139 #endif
140
141
142 //
143 // Platform dependent includes and defines
144 //
145
146 #if defined(_WIN32) || defined(UNDER_CE)
147
148 /********** WINDOWS ********************/
149
150 #ifndef _WIN32_WINNT
151 #ifdef _M_IX86
152 #define _WIN32_WINNT 0x0500
153 #else
154 #define _WIN32_WINNT 0x0501
155 #endif
156 #endif
157
158 #define FS_PATH_SEPARATOR _T("\\")
159 #define FS_PATH_SEPARATOR_CHAR _T('\\')
160
161 #if _MSC_VER > 1300
162 #define HAVE_SCPRINTF 1
163 #define HAVE_VSCPRINTF 1
164 #define HAVE_SCWPRINTF 1
165 #define HAVE_VSCWPRINTF 1
166 #endif
167
168 #ifndef va_copy
169 #define va_copy(x,y) (x = y)
170 #endif
171 #define HAVE_DECL_VA_COPY 1
172
173 #include <winsock2.h>
174 #include <windows.h>
175 #include <stdlib.h>
176
177 #ifndef UNDER_CE
178 #include <sys/stat.h>
179 #include <process.h>
180 #include <io.h>
181 #include <fcntl.h>
182 #include <direct.h>
183 #endif
184
185 #ifndef S_IRUSR
186 # define S_IRUSR 0400
187 #endif
188 #ifndef S_IWUSR
189 # define S_IWUSR 0200
190 #endif
191
192 #define snprintf _snprintf
193 #define vsnprintf _vsnprintf
194 #define snwprintf _snwprintf
195 #define vsnwprintf _vsnwprintf
196 #define scprintf _scprintf
197 #define vscprintf _vscprintf
198 #define scwprintf _scwprintf
199 #define vscwprintf _vscwprintf
200 #define popen _popen
201 #define pclose _pclose
202 #define strdup _strdup
203 #define wcsdup _wcsdup
204 #define stricmp _stricmp
205 #define strnicmp _strnicmp
206 #define strupr(s) _strupr(s)
207 #define getpid _getpid
208 #define fileno(f) _fileno(f)
209 #define chdir(p) _chdir(p)
210 #define mkdir(p,m) _mkdir(p,m)
211 #define lseek(f,o,w) _lseek(f,o,w)
212 #define unlink(x) _unlink(x)
213
214 typedef unsigned __int64 QWORD;
215 typedef __int64 INT64;
216 typedef int socklen_t;
217 typedef DWORD pid_t;
218 typedef LONG ssize_t;
219
220 typedef signed __int8 int8_t;
221 typedef signed __int16 int16_t;
222 typedef signed __int32 int32_t;
223 typedef signed __int64 int64_t;
224 typedef unsigned __int8 uint8_t;
225 typedef unsigned __int16 uint16_t;
226 typedef unsigned __int32 uint32_t;
227 typedef unsigned __int64 uint64_t;
228
229 #define INT64_FMT _T("%I64d")
230 #define UINT64_FMT _T("%I64u")
231 #define UINT64X_FMT(m) _T("%") m _T("I64X")
232 #if defined(__64BIT__) || (_MSC_VER > 1300)
233 #define TIME_T_FMT _T("%I64u")
234 #define TIME_T_FCAST(x) ((QWORD)(x))
235 #else
236 #define TIME_T_FMT _T("%u")
237 #define TIME_T_FCAST(x) ((DWORD)(x))
238 #endif
239
240 // Socket compatibility
241 #define SHUT_RD 0
242 #define SHUT_WR 1
243 #define SHUT_RDWR 2
244
245 #define SetSocketReuseFlag(sd)
246 #define SELECT_NFDS(x) ((int)(x))
247 #define SetSocketNonBlocking(s) { \
248 u_long one = 1; \
249 ioctlsocket(s, FIONBIO, &one); \
250 }
251 #define SetSocketBlocking(s) { \
252 u_long zero = 0; \
253 ioctlsocket(s, FIONBIO, &zero); \
254 }
255
256 #ifdef UNDER_CE
257 #define O_RDONLY 0x0004
258 #define O_WRONLY 0x0001
259 #define O_RDWR 0x0002
260 #define O_CREAT 0x0100
261 #define O_EXCL 0x0200
262 #define O_TRUNC 0x0800
263 #endif
264
265 #if !defined(UNDER_CE)
266 #define HAVE_LIBEXPAT 1
267 #endif
268
269 #elif defined(_NETWARE)
270
271 /********** NETWARE ********************/
272
273 #define FS_PATH_SEPARATOR _T("/")
274 #define FS_PATH_SEPARATOR_CHAR _T('/')
275
276 #include <unistd.h>
277 #include <stdlib.h>
278 #include <sys/types.h>
279 #include <sys/stat.h>
280 #include <sys/socket.h>
281 #include <sys/select.h>
282 #include <netinet/in.h>
283 #include <arpa/inet.h>
284 #include <stdint.h>
285 #include <fcntl.h>
286 #include <errno.h>
287 #include <dirent.h>
288 #include <wchar.h>
289 #include <netdb.h>
290
291 typedef int BOOL;
292 #if (SIZEOF_LONG == 4)
293 typedef long LONG;
294 #else
295 typedef int LONG;
296 #endif
297 #if (SIZEOF_LONG == 4)
298 typedef unsigned long DWORD;
299 #else
300 typedef unsigned int DWORD;
301 #endif
302 typedef unsigned short WORD;
303 typedef unsigned char BYTE;
304 typedef void * HANDLE;
305 typedef void * HMODULE;
306
307 #ifdef X_INT64_X
308 typedef X_INT64_X INT64;
309 #else
310 #error Target system does not have signed 64bit integer type
311 #endif
312
313 #ifdef X_UINT64_X
314 typedef X_UINT64_X QWORD;
315 #else
316 #error Target system does not have unsigned 64bit integer type
317 #endif
318
319 #define INT64_FMT _T("%lld")
320 #define UINT64_FMT _T("%llu")
321 #define UINT64X_FMT(m) _T("%") m _T("llX")
322 #define TIME_T_FMT _T("%u")
323 #define TIME_T_FCAST(x) ((DWORD)(x))
324
325 #ifndef MAX_PATH
326 #define MAX_PATH 256
327 #endif
328
329 // Socket compatibility
330 typedef int SOCKET;
331
332 #define closesocket(x) close(x)
333 #define WSAGetLastError() (errno)
334
335 #define WSAEINTR EINTR
336 #define WSAEWOULDBLOCK EWOULDBLOCK
337 #define WSAEINPROGRESS EINPROGRESS
338 #define INVALID_SOCKET (-1)
339
340 #define SetSocketReuseFlag(sd) { \
341 int nVal = 1; \
342 setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (const void *)&nVal, \
343 (socklen_t)sizeof(nVal)); \
344 }
345
346 #define SetSocketNonBlocking(s) { \
347 int f = fcntl(s, F_GETFL); \
348 if (f != -1) fcntl(s, F_SETFL, f | O_NONBLOCK); \
349 }
350
351 #define SELECT_NFDS(x) (x)
352
353 #elif defined(__SYMBIAN32__)
354
355 /********** SYMBIAN ********************/
356
357 #define _SYMBIAN
358
359 #include <e32base.h>
360 #include <e32std.h>
361
362 #include <sys/types.h>
363 #include <stdlib_r.h>
364 #include <sys/stat.h>
365 #include <netinet/in.h>
366
367 #define FS_PATH_SEPARATOR _T("\\")
368 #define FS_PATH_SEPARATOR_CHAR _T('\\')
369
370 typedef int BOOL;
371 typedef TInt32 LONG;
372 typedef TUint32 DWORD;
373 typedef TUint16 WORD;
374 typedef unsigned char BYTE;
375 typedef TInt64 INT64;
376 typedef TUint64 QWORD;
377
378 #define INT64_FMT _T("%lld")
379 #define UINT64_FMT _T("%llu")
380 #define UINT64X_FMT(m) _T("%") m _T("llX")
381 #define TIME_T_FMT _T("%u")
382 #define TIME_T_FCAST(x) ((DWORD)(x))
383
384 #ifndef MAX_PATH
385 #define MAX_PATH 256
386 #endif
387
388 #else /* not _WIN32, _NETWARE, or __SYMBIAN32__ */
389
390 /*********** UNIX *********************/
391
392 #ifndef PREFIX
393 #define PREFIX "/usr/local"
394 #define PREFIXW L"/usr/local"
395 #warning Installation prefix not defined, defaulting to /usr/local
396 #endif
397
398 #if HAVE_WCHAR_H
399 #include <wchar.h>
400 #endif
401
402 #if HAVE_WCTYPE_H
403 #include <wctype.h>
404 #endif
405
406 #include <errno.h>
407
408 #define FS_PATH_SEPARATOR _T("/")
409 #define FS_PATH_SEPARATOR_CHAR _T('/')
410
411 #if HAVE_UNISTD_H
412 #include <unistd.h>
413 #endif
414
415 #if HAVE_STDLIB_H
416 #include <stdlib.h>
417 #endif
418
419 #if HAVE_SYS_TYPES_H
420 #include <sys/types.h>
421 #endif
422
423 #if HAVE_SYS_INT_TYPES_H
424 #include <sys/int_types.h>
425 #endif
426
427 #if HAVE_SYS_STAT_H
428 #include <sys/stat.h>
429 #endif
430
431 #if HAVE_SYS_SOCKET_H
432 #include <sys/socket.h>
433 #endif
434
435 #if HAVE_SYS_SELECT_H
436 #include <sys/select.h>
437 #endif
438
439 #if HAVE_NETINET_IN_H
440 #include <netinet/in.h>
441 #endif
442
443 #if HAVE_NET_NH_H
444 #include <net/nh.h>
445 #endif
446
447 #if HAVE_ARPA_INET_H
448 #include <arpa/inet.h>
449 #endif
450
451 #if HAVE_NETDB_H
452 #include <netdb.h>
453 #endif
454
455 #if HAVE_FCNTL_H
456 #include <fcntl.h>
457 #endif
458
459 #include <dirent.h>
460
461 #if _USE_GNU_PTH
462 #include <pth.h>
463 #endif
464
465 typedef int BOOL;
466 #if (SIZEOF_LONG == 4)
467 typedef long LONG;
468 typedef unsigned long DWORD;
469 #undef __64BIT__
470 #else
471 typedef int LONG;
472 typedef unsigned int DWORD;
473 #ifndef __64BIT__
474 #define __64BIT__
475 #endif
476 #endif
477 typedef unsigned short WORD;
478 typedef unsigned char BYTE;
479 typedef void * HANDLE;
480 typedef void * HMODULE;
481
482 #if !HAVE_MODE_T
483 typedef int mode_t;
484 #endif
485
486 #if HAVE_LONG_LONG && (SIZEOF_LONG_LONG == 8)
487 typedef long long INT64;
488 #elif HAVE_INT64_T
489 typedef int64_t INT64;
490 #else
491 #error Target system does not have signed 64bit integer type
492 #endif
493
494 #if HAVE_UNSIGNED_LONG_LONG && (SIZEOF_LONG_LONG == 8)
495 typedef unsigned long long QWORD;
496 #elif HAVE_UINT64_T
497 typedef uint64_t QWORD;
498 #elif HAVE_U_INT64_T
499 typedef u_int64_t QWORD;
500 #else
501 #error Target system does not have unsigned 64bit integer type
502 #endif
503
504 #define INT64_FMT _T("%lld")
505 #define UINT64_FMT _T("%llu")
506 #define UINT64X_FMT(m) _T("%") m _T("llX")
507 #ifdef __64BIT__
508 #define TIME_T_FMT _T("%llu")
509 #define TIME_T_FCAST(x) ((QWORD)(x))
510 #else
511 #define TIME_T_FMT _T("%u")
512 #define TIME_T_FCAST(x) ((DWORD)(x))
513 #endif
514
515 #ifndef TRUE
516 #define TRUE 1
517 #endif
518 #ifndef FALSE
519 #define FALSE 0
520 #endif
521
522 #ifndef MAX_PATH
523 #define MAX_PATH 256
524 #endif
525
526 // Socket compatibility
527 typedef int SOCKET;
528
529 #define closesocket(x) close(x)
530 #define WSAGetLastError() (errno)
531
532 #define WSAEINTR EINTR
533 #define WSAEWOULDBLOCK EWOULDBLOCK
534 #define WSAEINPROGRESS EINPROGRESS
535 #define INVALID_SOCKET (-1)
536
537 #define SetSocketReuseFlag(sd) { \
538 int nVal = 1; \
539 setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (const void *)&nVal, \
540 (socklen_t)sizeof(nVal)); \
541 }
542
543 #define SetSocketNonBlocking(s) { \
544 int f = fcntl(s, F_GETFL); \
545 if (f != -1) fcntl(s, F_SETFL, f | O_NONBLOCK); \
546 }
547
548 #define SELECT_NFDS(x) (x)
549
550 #if !(HAVE_SOCKLEN_T) && !defined(_USE_GNU_PTH)
551 typedef unsigned int socklen_t;
552 #endif
553
554 // Shared library suffix
555 #ifndef SHL_SUFFIX
556 #if defined(_HPUX) && !defined(__64BIT__)
557 #define SHL_SUFFIX ".sl"
558 #else
559 #define SHL_SUFFIX ".so"
560 #endif
561 #endif
562
563 typedef struct hostent HOSTENT;
564
565 #if HAVE_DECL___VA_COPY && !HAVE_DECL_VA_COPY
566 #define HAVE_DECL_VA_COPY 1
567 #define va_copy(d,s) __va_copy(d,s)
568 #endif
569
570 #endif /* _WIN32 */
571
572
573 //
574 // Value used to indicate invalid pointer where NULL is not appropriate
575 //
576
577 #ifdef __64BIT__
578 #define INVALID_POINTER_VALUE ((void *)_ULL(0xFFFFFFFFFFFFFFFF))
579 #else
580 #define INVALID_POINTER_VALUE ((void *)0xFFFFFFFF)
581 #endif
582
583
584 //
585 // Casting between pointer and 32-bit integer
586 //
587
588 #ifdef __64BIT__
589 #define CAST_FROM_POINTER(p, t) ((t)((QWORD)(p)))
590 #define CAST_TO_POINTER(v, t) ((t)((QWORD)(v)))
591 #else
592 #define CAST_FROM_POINTER(p, t) ((t)(p))
593 #define CAST_TO_POINTER(v, t) ((t)(v))
594 #endif
595
596
597 //
598 // OpenSSL
599 //
600
601 #ifdef _WITH_ENCRYPTION
602
603 #include <openssl/crypto.h>
604 #include <openssl/rsa.h>
605 #include <openssl/pem.h>
606 #include <openssl/evp.h>
607 #include <openssl/rand.h>
608 #include <openssl/opensslv.h>
609 #include <openssl/err.h>
610
611 #ifdef NETXMS_NO_AES
612 #ifndef OPENSSL_NO_AES
613 #define OPENSSL_NO_AES
614 #endif
615 #endif
616
617 #ifdef NETXMS_NO_BF
618 #ifndef OPENSSL_NO_BF
619 #define OPENSSL_NO_BF
620 #endif
621 #endif
622
623 #ifdef NETXMS_NO_IDEA
624 #ifndef OPENSSL_NO_IDEA
625 #define OPENSSL_NO_IDEA
626 #endif
627 #endif
628
629 #ifdef NETXMS_NO_DES
630 #ifndef OPENSSL_NO_DES
631 #define OPENSSL_NO_DES
632 #endif
633 #endif
634
635 #if OPENSSL_VERSION_NUMBER >= 0x00907000
636 #define OPENSSL_CONST const
637 #else
638 #define OPENSSL_CONST
639 #endif
640
641 #else
642
643 // Prevent compilation errors on function prototypes
644 #define RSA void
645
646 #endif
647
648
649 //
650 // open() flags compatibility
651 //
652
653 #ifndef O_BINARY
654 #define O_BINARY 0
655 #endif
656
657
658 //
659 // Windows-specific structures for non-Windows platforms
660 //
661
662 #ifndef _WIN32
663
664 typedef struct tagPOINT
665 {
666 int x;
667 int y;
668 } POINT;
669
670 #endif
671
672
673 //
674 // Event log severity codes (UNIX only)
675 //
676
677 #ifndef _WIN32
678 #define EVENTLOG_SUCCESS 0x0000
679 #define EVENTLOG_ERROR_TYPE 0x0001
680 #define EVENTLOG_WARNING_TYPE 0x0002
681 #define EVENTLOG_INFORMATION_TYPE 0x0004
682 #define EVENTLOG_AUDIT_SUCCESS 0x0008
683 #define EVENTLOG_AUDIT_FAILURE 0x0010
684 #endif /* _WIN32 */
685
686
687 //
688 // Interface types
689 //
690
691 #define IFTYPE_OTHER 1
692 #define IFTYPE_REGULAR1822 2
693 #define IFTYPE_HDH1822 3
694 #define IFTYPE_DDN_X25 4
695 #define IFTYPE_RFC877_X25 5
696 #define IFTYPE_ETHERNET_CSMACD 6
697 #define IFTYPE_ISO88023_CSMACD 7
698 #define IFTYPE_ISO88024_TOKENBUS 8
699 #define IFTYPE_ISO88025_TOKENRING 9
700 #define IFTYPE_ISO88026_MAN 10
701 #define IFTYPE_STARLAN 11
702 #define IFTYPE_PROTEON_10MBIT 12
703 #define IFTYPE_PROTEON_80MBIT 13
704 #define IFTYPE_HYPERCHANNEL 14
705 #define IFTYPE_FDDI 15
706 #define IFTYPE_LAPB 16
707 #define IFTYPE_SDLC 17
708 #define IFTYPE_DS1 18
709 #define IFTYPE_E1 19
710 #define IFTYPE_BASIC_ISDN 20
711 #define IFTYPE_PRIMARY_ISDN 21
712 #define IFTYPE_PROP_PTP_SERIAL 22
713 #define IFTYPE_PPP 23
714 #define IFTYPE_SOFTWARE_LOOPBACK 24
715 #define IFTYPE_EON 25
716 #define IFTYPE_ETHERNET_3MBIT 26
717 #define IFTYPE_NSIP 27
718 #define IFTYPE_SLIP 28
719 #define IFTYPE_ULTRA 29
720 #define IFTYPE_DS3 30
721 #define IFTYPE_SMDS 31
722 #define IFTYPE_FRAME_RELAY 32
723 #define IFTYPE_RS232 33
724 #define IFTYPE_PARA 34
725 #define IFTYPE_ARCNET 35
726 #define IFTYPE_ARCNET_PLUS 36
727 #define IFTYPE_ATM 37
728 #define IFTYPE_MIOX25 38
729 #define IFTYPE_SONET 39
730 #define IFTYPE_X25PLE 40
731 #define IFTYPE_ISO88022LLC 41
732 #define IFTYPE_LOCALTALK 42
733 #define IFTYPE_SMDS_DXI 43
734 #define IFTYPE_FRAME_RELAY_SERVICE 44
735 #define IFTYPE_V35 45
736 #define IFTYPE_HSSI 46
737 #define IFTYPE_HIPPI 47
738 #define IFTYPE_MODEM 48
739 #define IFTYPE_AAL5 49
740 #define IFTYPE_SONET_PATH 50
741 #define IFTYPE_SONET_VT 51
742 #define IFTYPE_SMDS_ICIP 52
743 #define IFTYPE_PROP_VIRTUAL 53
744 #define IFTYPE_PROP_MULTIPLEXOR 54
745 #define IFTYPE_IEEE80212 55
746 #define IFTYPE_FIBRECHANNEL 56
747
748 #define IFTYPE_NETXMS_NAT_ADAPTER 65535
749
750
751 //
752 // IP Header -- RFC 791
753 //
754
755 typedef struct tagIPHDR
756 {
757 BYTE m_cVIHL; // Version and IHL
758 BYTE m_cTOS; // Type Of Service
759 WORD m_wLen; // Total Length
760 WORD m_wId; // Identification
761 WORD m_wFlagOff; // Flags and Fragment Offset
762 BYTE m_cTTL; // Time To Live
763 BYTE m_cProtocol; // Protocol
764 WORD m_wChecksum; // Checksum
765 struct in_addr m_iaSrc; // Internet Address - Source
766 struct in_addr m_iaDst; // Internet Address - Destination
767 } IPHDR;
768
769
770 //
771 // ICMP Header - RFC 792
772 //
773
774 typedef struct tagICMPHDR
775 {
776 BYTE m_cType; // Type
777 BYTE m_cCode; // Code
778 WORD m_wChecksum; // Checksum
779 WORD m_wId; // Identification
780 WORD m_wSeq; // Sequence
781 char m_cData[4]; // Data
782 } ICMPHDR;
783
784
785 //
786 // INADDR_NONE can be undefined on some systems
787 //
788
789 #ifndef INADDR_NONE
790 #define INADDR_NONE (0xFFFFFFFF)
791 #endif
792
793
794 //
795 // Check if IP address is a broadcast
796 //
797
798 #define IsBroadcastAddress(addr, mask) (((addr) & (~(mask))) == (~(mask)))
799
800
801 //
802 // Check if given string is NULL and always return valid pointer
803 //
804
805 #define CHECK_NULL(x) ((x) == NULL ? ((TCHAR *)_T("(null)")) : (x))
806 #define CHECK_NULL_A(x) ((x) == NULL ? ((char *)"(null)") : (x))
807 #define CHECK_NULL_EX(x) ((x) == NULL ? ((TCHAR *)_T("")) : (x))
808 #define CHECK_NULL_EX_A(x) ((x) == NULL ? ((char *)"") : (x))
809
810
811 //
812 // Free memory block if it isn't NULL
813 //
814
815 #define safe_free(x) { if ((x) != NULL) free(x); }
816 #define safe_free_and_null(x) { if ((x) != NULL) { free(x); x = NULL; } }
817
818
819 //
820 // delete object and nullify pointer
821 //
822
823 #define delete_and_null(x) { delete x; x = NULL; }
824 #define safe_delete_and_null(x) { if ((x) != NULL) { delete x; x = NULL; } }
825
826
827 //
828 // Convert half-byte's value to hex digit and vice versa
829 //
830
831 #define bin2hex(x) ((x) < 10 ? ((x) + _T('0')) : ((x) + (_T('A') - 10)))
832 #ifdef UNICODE
833 #define hex2bin(x) ((((x) >= _T('0')) && ((x) <= _T('9'))) ? ((x) - _T('0')) : \
834 (((towupper(x) >= _T('A')) && (towupper(x) <= _T('F'))) ? (towupper(x) - _T('A') + 10) : 0))
835 #else
836 #define hex2bin(x) ((((x) >= '0') && ((x) <= '9')) ? ((x) - '0') : \
837 (((toupper(x) >= 'A') && (toupper(x) <= 'F')) ? (toupper(x) - 'A' + 10) : 0))
838 #endif
839
840
841 //
842 // Define min() and max() if needed
843 //
844
845 #ifndef min
846 #define min(a, b) ((a) < (b) ? (a) : (b))
847 #endif
848 #ifndef max
849 #define max(a, b) ((a) > (b) ? (a) : (b))
850 #endif
851
852
853 //
854 // Define stricmp() for non-windows
855 //
856
857 #ifndef _WIN32
858 #define stricmp strcasecmp
859 #define strnicmp strncasecmp
860 #define wcsicmp wcscasecmp
861 #define wcsnicmp wcsncasecmp
862 #endif
863
864
865 //
866 // Compare two numbers and return -1, 0, or 1
867 //
868
869 #define COMPARE_NUMBERS(n1,n2) (((n1) < (n2)) ? -1 : (((n1) > (n2)) ? 1 : 0))
870
871
872 //
873 // Increment pointer for given number of bytes
874 //
875
876 #define inc_ptr(ptr, step, ptype) ptr = (ptype *)(((char *)ptr) + step)
877
878
879 //
880 // Validate numerical value
881 //
882
883 #define VALIDATE_VALUE(var,x,y,z) { if ((var < x) || (var > y)) var = z; }
884
885
886 //
887 // DCI (data collection item) data types
888 //
889
890 #define DCI_DT_INT 0
891 #define DCI_DT_UINT 1
892 #define DCI_DT_INT64 2
893 #define DCI_DT_UINT64 3
894 #define DCI_DT_STRING 4
895 #define DCI_DT_FLOAT 5
896 #define DCI_DT_NULL 6
897 #define DCI_DT_DEPRECATED 255 /* used internally by agent */
898
899
900 //
901 // Insert parameter as string
902 //
903
904 #ifdef UNICODE
905 #define STRING(x) L#x
906 #else
907 #define STRING(x) #x
908 #endif
909
910
911 //
912 // Memory debug
913 //
914
915 #ifdef NETXMS_MEMORY_DEBUG
916
917 #ifdef __cplusplus
918 extern "C" {
919 #endif
920
921 void *nx_malloc(size_t, char *, int);
922 void *nx_realloc(void *, size_t, char *, int);
923 void nx_free(void *, char *, int);
924
925 void InitMemoryDebugger(void);
926 void PrintMemoryBlocks(void);
927
928 #ifdef __cplusplus
929 }
930 #endif
931
932 #define malloc(x) nx_malloc(x, __FILE__, __LINE__)
933 #define realloc(p, x) nx_realloc(p, x, __FILE__, __LINE__)
934 #define free(p) nx_free(p, __FILE__, __LINE__)
935
936 #endif /* NETXMS_MEMORY_DEBUG */
937
938 #endif /* _nms_common_h_ */