fix for Windows build (free is NULL safe on Windows)
[public/netxms.git] / include / nms_common.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2014 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published by
7 ** the Free Software Foundation; either version 3 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 Lesser 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 #if !defined(_WIN32) && !defined(UNDER_CE)
33
34 #include <config.h>
35 #if defined(WITH_OPENSSL) || defined(WITH_COMMONCRYPTO)
36 #define _WITH_ENCRYPTION 1
37 #endif
38
39 #else /* _WIN32 */
40
41 #ifndef UNDER_CE
42 #define _WITH_ENCRYPTION 1
43 #define WITH_OPENSSL 1
44 #define WITH_LDAP 1
45 #if !defined(WINDOWS_ONLY) && !defined(_CRT_SECURE_NO_DEPRECATE)
46 #define _CRT_SECURE_NO_DEPRECATE
47 #endif
48 #endif
49
50 // prevent defining min and max macros in Windows headers
51 #ifndef NOMINMAX
52 #define NOMINMAX
53 #endif
54
55 // prevent defining ETIMEDOUT, ECONNRESET, etc. to wrong values
56 #define _CRT_NO_POSIX_ERROR_CODES
57
58 #endif /* _WIN32 */
59
60 #if HAVE_JEMALLOC_JEMALLOC_H
61 #include <jemalloc/jemalloc.h>
62 #endif
63
64 #ifdef HAVE_STDARG_H
65 #include <stdarg.h>
66 #endif
67
68 #include <unicode.h>
69
70 #include <stdio.h>
71 #include <string.h>
72 #include <limits.h>
73
74 #include <netxms-version.h>
75
76 #if HAVE_WIDEC_H
77 #include <widec.h>
78 #endif /* HAVE_WIDEC */
79
80 /**
81 * Define __64BIT__ if compiling for 64bit platform with Visual C++
82 */
83 #if defined(_M_X64) || defined(_M_IA64) || defined(__LP64__) || defined(__PPC64__) || defined(__x86_64__)
84 #ifndef __64BIT__
85 #define __64BIT__
86 #endif
87 #endif
88
89 /**
90 * Wrappers for 64-bit integer constants
91 */
92 #if defined(__GNUC__) || defined(__HP_aCC) || defined(__IBMC__) || defined(__IBMCPP__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC)
93 #define _LL(x) (x ## LL)
94 #define _ULL(x) (x ## ULL)
95 #elif defined(_MSC_VER)
96 #define _LL(x) (x ## i64)
97 #define _ULL(x) (x ## ui64)
98 #else
99 #define _LL(x) (x)
100 #define _ULL(x) (x)
101 #endif
102
103 /**
104 * Common constants
105 */
106 #define MAX_SECRET_LENGTH 64
107 #define MAX_DB_STRING 256
108 #define MAX_PARAM_NAME 256
109 #define MAX_CONFIG_VALUE 2000
110 #define MAX_COLUMN_NAME 64
111 #define MAX_DNS_NAME 256
112 #define MAX_HELPDESK_REF_LEN 64
113 #define MAX_PASSWORD 256
114 #define MAX_SSH_LOGIN_LEN 64
115 #define MAX_SSH_PASSWORD_LEN 64
116 #define GROUP_FLAG ((UINT32)0x80000000)
117
118 #define NETXMS_MAX_CIPHERS 6
119 #define NETXMS_RSA_KEYLEN 2048
120
121 #ifndef LLONG_MAX
122 #define LLONG_MAX _LL(9223372036854775807)
123 #endif
124
125 #ifndef LLONG_MIN
126 #define LLONG_MIN (-LLONG_MAX - 1)
127 #endif
128
129 #ifndef ULLONG_MAX
130 #define ULLONG_MAX _ULL(18446744073709551615)
131 #endif
132
133 #ifndef EVENTLOG_DEBUG_TYPE
134 #define EVENTLOG_DEBUG_TYPE 0x0080
135 #endif
136
137 #define INVALID_INDEX 0xFFFFFFFF
138 #define MD5_DIGEST_SIZE 16
139 #define SHA1_DIGEST_SIZE 20
140 #define SHA256_DIGEST_SIZE 32
141
142 #define FILE_BUFFER_SIZE 32768
143
144 /**
145 * Compatibility defines for C sources
146 */
147 #if !defined(__cplusplus) && !defined(CORTEX) && !HAVE_BOOL
148 typedef int bool;
149 #endif
150
151 /**
152 * Oracle Pro*C compatibility
153 */
154 #ifdef ORA_PROC
155 #undef BYTE
156 #undef DWORD
157 #endif
158
159 /**
160 * Java class path separator character
161 */
162 #ifdef _WIN32
163 #define JAVA_CLASSPATH_SEPARATOR _T(';')
164 #else
165 #define JAVA_CLASSPATH_SEPARATOR _T(':')
166 #endif
167
168
169 /***** Platform dependent includes and defines *****/
170
171 #if defined(_WIN32) || defined(UNDER_CE)
172
173 /********** WINDOWS ********************/
174
175 #ifdef _MSC_FULL_VER
176 #define __BUILD_VERSION_STRING(s,v) s v
177 #define __STR_NX(x) #x
178 #define __STR(x) __STR_NX(x)
179 #define CPP_COMPILER_VERSION __STR(__BUILD_VERSION_STRING(Microsoft C/C++ Optimizing Compiler Version,_MSC_FULL_VER))
180 #endif
181
182 // Disable some warnings:
183 // 4577 - 'noexcept' used with no exception handling mode specified
184 #pragma warning(disable: 4577)
185
186 #ifndef _WIN32_WINNT
187 #define _WIN32_WINNT 0x0501
188 #endif
189
190 #define WITH_IPV6 1
191
192 #define USE_BUNDLED_LIBTRE 1
193 #define USE_BUNDLED_GETOPT 1
194
195 #define SAFE_FGETWS_WITH_POPEN 1
196
197 #define FREE_IS_NULL_SAFE 1
198
199 #define FS_PATH_SEPARATOR _T("\\")
200 #define FS_PATH_SEPARATOR_A "\\"
201 #define FS_PATH_SEPARATOR_W L"\\"
202 #define FS_PATH_SEPARATOR_CHAR _T('\\')
203
204 #define WEXITSTATUS(x) (x)
205
206 #if _MSC_VER >= 1310
207 #define HAVE_SCPRINTF 1
208 #define HAVE_VSCPRINTF 1
209 #define HAVE_SCWPRINTF 1
210 #define HAVE_VSCWPRINTF 1
211 #endif
212
213 #if _MSC_VER >= 1800
214 #define HAVE_STRTOLL 1
215 #define HAVE_STRTOULL 1
216 #define HAVE_WCSTOLL 1
217 #define HAVE_WCSTOULL 1
218 #endif
219
220 #define HAVE_SNPRINTF 1
221 #define HAVE_VSNPRINTF 1
222
223 #define HAVE_GETADDRINFO 1
224
225 #define HAVE_STDARG_H 1
226
227 #define HAVE_ALLOCA 1
228
229 #define HAVE_WCSLEN 1
230 #define HAVE_WCSNCPY 1
231 #define HAVE_WCSDUP 1
232 #define HAVE_WUTIME 1
233
234 #define HAVE_LIBCURL 1
235
236 #define HAVE_DIRENT_D_TYPE 1
237
238 #include <winsock2.h>
239 #include <windows.h>
240 #include <ws2tcpip.h>
241 #include <stdlib.h>
242 #include <malloc.h>
243
244 #include <stdarg.h>
245 #ifndef va_copy
246 #define va_copy(x,y) (x = y)
247 #endif
248 #define HAVE_DECL_VA_COPY 1
249
250 #ifndef UNDER_CE
251 #include <sys/stat.h>
252 #include <sys/utime.h>
253 #include <process.h>
254 #include <io.h>
255 #include <fcntl.h>
256 #include <direct.h>
257 #endif
258
259 #ifndef S_IRUSR
260 # define S_IRUSR 0400
261 #endif
262 #ifndef S_IWUSR
263 # define S_IWUSR 0200
264 #endif
265
266 #define snprintf _snprintf
267 #define vsnprintf _vsnprintf
268 #define snwprintf _snwprintf
269 #define vsnwprintf _vsnwprintf
270 #define scprintf _scprintf
271 #define vscprintf _vscprintf
272 #define scwprintf _scwprintf
273 #define vscwprintf _vscwprintf
274 #define popen _popen
275 #define pclose _pclose
276 #define stricmp _stricmp
277 #define strnicmp _strnicmp
278 #define wcsicmp _wcsicmp
279 #define wcsnicmp _wcsnicmp
280 #define strlwr(s) _strlwr(s)
281 #define strupr(s) _strupr(s)
282 #define putenv(s) _putenv(s)
283 #define fileno(f) _fileno(f)
284 #define chdir(p) _chdir(p)
285 #define mkdir(p,m) _mkdir(p,m)
286 #define lseek(f,o,w) _lseek(f,o,w)
287 #define unlink(x) _unlink(x)
288
289 typedef UINT64 QWORD; // for compatibility
290 typedef int socklen_t;
291 typedef long pid_t;
292 typedef LONG ssize_t;
293
294 typedef signed __int8 int8_t;
295 typedef signed __int16 int16_t;
296 typedef signed __int32 int32_t;
297 typedef signed __int64 int64_t;
298 typedef unsigned __int8 uint8_t;
299 typedef unsigned __int16 uint16_t;
300 typedef unsigned __int32 uint32_t;
301 typedef unsigned __int64 uint64_t;
302
303 #define INT64_FMT _T("%I64d")
304 #define INT64_FMTA "%I64d"
305 #define INT64_FMTW L"%I64d"
306 #define UINT64_FMT _T("%I64u")
307 #define UINT64_FMTA "%I64u"
308 #define UINT64_FMTW L"%I64u"
309 #define UINT64X_FMT(m) _T("%") m _T("I64X")
310 #if defined(__64BIT__) || (_MSC_VER > 1300)
311 #define TIME_T_FMT _T("%I64u")
312 #define TIME_T_FCAST(x) ((UINT64)(x))
313 #else
314 #define TIME_T_FMT _T("%u")
315 #define TIME_T_FCAST(x) ((UINT32)(x))
316 #endif
317
318 #ifndef __clang__
319
320 #define HAVE_DECL_BSWAP_16 1
321 #define HAVE_DECL_BSWAP_32 1
322 #define HAVE_DECL_BSWAP_64 1
323
324 #define bswap_16(n) _byteswap_ushort(n)
325 #define bswap_32(n) _byteswap_ulong(n)
326 #define bswap_64(n) _byteswap_uint64(n)
327
328 #endif
329
330 // Socket compatibility
331 #define SHUT_RD 0
332 #define SHUT_WR 1
333 #define SHUT_RDWR 2
334
335 #define SetSocketReuseFlag(s) { \
336 BOOL val = TRUE; \
337 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(BOOL)); \
338 }
339 #define SetSocketExclusiveAddrUse(s) { \
340 BOOL val = TRUE; \
341 setsockopt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *)&val, sizeof(BOOL)); \
342 }
343 #define SELECT_NFDS(x) ((int)(x))
344 #define SetSocketNonBlocking(s) { \
345 u_long one = 1; \
346 ioctlsocket(s, FIONBIO, &one); \
347 }
348 #define SetSocketBlocking(s) { \
349 u_long zero = 0; \
350 ioctlsocket(s, FIONBIO, &zero); \
351 }
352 #define SetSocketNoDelay(s) { \
353 BOOL val = TRUE; \
354 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(BOOL)); \
355 }
356 #define SetSocketBroadcast(s) { \
357 BOOL val = TRUE; \
358 setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&val, sizeof(BOOL)); \
359 }
360
361 #ifdef UNDER_CE
362 #define O_RDONLY 0x0004
363 #define O_WRONLY 0x0001
364 #define O_RDWR 0x0002
365 #define O_CREAT 0x0100
366 #define O_EXCL 0x0200
367 #define O_TRUNC 0x0800
368 #endif
369
370 #if !defined(UNDER_CE)
371 #define HAVE_LIBEXPAT 1
372 #endif
373
374 #define XMPP_SUPPORTED 1
375
376 #define HAVE_LOCALE_H 1
377 #define HAVE_SETLOCALE 1
378
379 // Use Win32 API instead of msvcrt for memory allocation
380 #ifdef USE_WIN32_HEAP
381
382 #define malloc(n) HeapAlloc(GetProcessHeap(), 0, n)
383 #define realloc(p, n) (((p) == NULL) ? HeapAlloc(GetProcessHeap(), 0, n) : HeapReAlloc(GetProcessHeap(), 0, p, n))
384 #define free(p) HeapFree(GetProcessHeap(), 0, p)
385 #define _tcsdup(s) nx__tcsdup(s)
386 #define wcsdup(s) nx_wcsdup(s)
387
388 #undef _tcsdup
389 #ifdef UNICODE
390 #define _tcsdup nx_wcsdup
391 #else
392 #define _tcsdup nx_strdup
393 #endif
394
395 #else
396
397 #define strdup _strdup
398 #define wcsdup _wcsdup
399
400 #endif
401
402 #else /* not _WIN32 */
403
404 /*********** UNIX *********************/
405
406 #ifndef PREFIX
407 #ifdef UNICODE
408 #define PREFIX L"/usr/local"
409 #else
410 #define PREFIX "/usr/local"
411 #endif
412 #warning Installation prefix not defined, defaulting to /usr/local
413 #endif
414
415 #if HAVE_STDBOOL_H
416 #include <stdbool.h>
417 #endif
418
419 #if HAVE_ALLOCA_H
420 #include <alloca.h>
421 #endif
422
423 #if HAVE_WCHAR_H
424 #include <wchar.h>
425 #endif
426
427 #if HAVE_WCTYPE_H
428 #include <wctype.h>
429 #endif
430
431 // Fix for wcs* functions visibility in Solaris 11
432 #if defined(__sun) && (__cplusplus >= 199711L)
433 #if HAVE_WCSDUP
434 using std::wcsdup;
435 #endif
436 #if HAVE_WCSCASECMP
437 using std::wcscasecmp;
438 #endif
439 #if HAVE_WCSNCASECMP
440 using std::wcsncasecmp;
441 #endif
442 #endif
443
444 #include <errno.h>
445
446 #define FS_PATH_SEPARATOR _T("/")
447 #define FS_PATH_SEPARATOR_A "/"
448 #define FS_PATH_SEPARATOR_W L"/"
449 #define FS_PATH_SEPARATOR_CHAR _T('/')
450
451 #if HAVE_UNISTD_H
452 #include <unistd.h>
453 #endif
454
455 #if HAVE_STDLIB_H
456 #include <stdlib.h>
457 #endif
458
459 #if HAVE_SYS_TYPES_H
460 #include <sys/types.h>
461 #endif
462
463 #if HAVE_UTIME_H
464 #include <utime.h>
465 #endif
466
467 #if HAVE_SYS_INT_TYPES_H
468 #include <sys/int_types.h>
469 #endif
470
471 #if HAVE_SYS_STAT_H
472 #include <sys/stat.h>
473 #endif
474
475 #if HAVE_SYS_SOCKET_H
476 #include <sys/socket.h>
477 #endif
478
479 #if HAVE_SYS_SELECT_H
480 #include <sys/select.h>
481 #endif
482
483 #if HAVE_NETINET_IN_H
484 #include <netinet/in.h>
485 #endif
486
487 #if HAVE_NETINET_TCP_H
488 #include <netinet/tcp.h>
489 #endif
490
491 #if HAVE_NET_NH_H
492 #include <net/nh.h>
493 #endif
494
495 #if HAVE_ARPA_INET_H
496 #include <arpa/inet.h>
497 #endif
498
499 #if HAVE_NETDB_H
500 #include <netdb.h>
501 #endif
502
503 #if HAVE_FCNTL_H
504 #include <fcntl.h>
505 #endif
506
507 #include <dirent.h>
508
509 #if _USE_GNU_PTH
510 #include <pth.h>
511 #else
512 #include <pthread.h>
513 #endif
514
515 #ifdef __IBMCPP__
516 #include <builtins.h>
517 #endif
518
519 #ifdef __minix
520 #undef HAVE_ITOA /* Minix has non-compatible itoa() */
521 #undef HAVE_GETHOSTBYNAME2_R
522 #endif
523
524 #include <sys/socket.h>
525 #include <sys/un.h>
526
527 typedef int BOOL;
528 #if (SIZEOF_LONG == 4)
529 #if (SIZEOF_INT == 4)
530 typedef int INT32;
531 typedef unsigned int UINT32;
532 #else
533 typedef long INT32;
534 typedef unsigned long UINT32;
535 #endif
536 #undef __64BIT__
537 #else
538 typedef int INT32;
539 typedef unsigned int UINT32;
540 #ifndef __64BIT__
541 #define __64BIT__
542 #endif
543 #endif
544 typedef unsigned short UINT16;
545 typedef short INT16;
546 typedef unsigned char BYTE;
547 typedef INT32 LONG; // for compatibility
548 typedef UINT32 DWORD; // for compatibility
549 typedef UINT16 WORD; // for compatibility
550 typedef void * HANDLE;
551 typedef void * HMODULE;
552
553 #if !HAVE_MODE_T
554 typedef int mode_t;
555 #endif
556
557 // We have to use long as INT64 on HP-UX - otherwise
558 // there will be compilation errors because of type redefinition in
559 // system includes
560 #ifdef _HPUX
561
562 #ifdef __LP64__
563 typedef long INT64;
564 typedef unsigned long UINT64;
565 #else
566 typedef long long INT64;
567 typedef unsigned long long UINT64;
568 #endif
569
570 #else /* _HPUX */
571
572 #if HAVE_LONG_LONG && (SIZEOF_LONG_LONG == 8)
573 typedef long long INT64;
574 #elif HAVE_INT64_T
575 typedef int64_t INT64;
576 #elif SIZEOF_LONG == 8
577 typedef long INT64;
578 #else
579 #error Target system does not have signed 64bit integer type
580 #endif
581
582 #if HAVE_UNSIGNED_LONG_LONG && (SIZEOF_LONG_LONG == 8)
583 typedef unsigned long long UINT64;
584 #elif HAVE_UINT64_T
585 typedef uint64_t UINT64;
586 #elif HAVE_U_INT64_T
587 typedef u_int64_t UINT64;
588 #elif SIZEOF_LONG == 8
589 typedef unsigned long UINT64;
590 #else
591 #error Target system does not have unsigned 64bit integer type
592 #endif
593
594 #endif /* _HPUX */
595
596 typedef UINT64 QWORD; // for compatibility
597
598 #define INT64_FMT _T("%lld")
599 #define INT64_FMTW L"%lld"
600 #define INT64_FMTA "%lld"
601 #define UINT64_FMT _T("%llu")
602 #define UINT64_FMTW L"%llu"
603 #define UINT64_FMTA "%llu"
604 #define UINT64X_FMT(m) _T("%") m _T("llX")
605 #ifdef __64BIT__
606 #define TIME_T_FMT _T("%llu")
607 #define TIME_T_FCAST(x) ((UINT64)(x))
608 #else
609 #define TIME_T_FMT _T("%u")
610 #define TIME_T_FCAST(x) ((UINT32)(x))
611 #endif
612
613 #ifndef TRUE
614 #define TRUE 1
615 #endif
616 #ifndef FALSE
617 #define FALSE 0
618 #endif
619
620 // Some systems may define true and false which may break overloaded functions
621 #ifdef __cplusplus
622 #undef true
623 #undef false
624 #endif
625
626 #ifndef MAX_PATH
627 #ifdef PATH_MAX
628 #define MAX_PATH PATH_MAX
629 #else
630 #define MAX_PATH 1024
631 #endif
632 #endif
633
634 // Windows compatibility layer for standard C I/O functions
635 #ifdef __cplusplus
636
637 inline int _access(const char *pathname, int mode) { return ::access(pathname, mode); }
638 inline int _open(const char *pathname, int flags) { return ::open(pathname, flags); }
639 inline int _open(const char *pathname, int flags, mode_t mode) { return ::open(pathname, flags, mode); }
640 inline ssize_t _read(int fd, void *buf, size_t count) { return ::read(fd, buf, count); }
641 inline ssize_t _write(int fd, const void *buf, size_t count) { return ::write(fd, buf, count); }
642 inline int _close(int fd) { return ::close(fd); }
643
644 #else
645
646 #define _access(p, m) access((p), (m))
647 #define _open open
648 #define _close(f) close(f)
649 #define _read(f, b, l) read((f), (b), (l))
650 #define _write(f, b, l) write((f), (b), (l))
651 #define _close(f) close(f)
652
653 #endif
654
655 // Socket compatibility
656 typedef int SOCKET;
657
658 #define closesocket(x) _close(x)
659 #define WSAGetLastError() (errno)
660
661 #define WSAEINTR EINTR
662 #define WSAEWOULDBLOCK EWOULDBLOCK
663 #define WSAEINPROGRESS EINPROGRESS
664 #define WSAESHUTDOWN ESHUTDOWN
665 #define INVALID_SOCKET (-1)
666
667 #define SetSocketReuseFlag(sd) { \
668 int nVal = 1; \
669 setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (const void *)&nVal, \
670 (socklen_t)sizeof(nVal)); \
671 }
672
673 #define SetSocketExclusiveAddrUse(s)
674
675 #define SetSocketNonBlocking(s) { \
676 int f = fcntl(s, F_GETFL); \
677 if (f != -1) fcntl(s, F_SETFL, f | O_NONBLOCK); \
678 }
679
680 #define SetSocketNoDelay(s) { \
681 int val = 1; \
682 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const void *)&val, sizeof(int)); \
683 }
684
685 #define SetSocketBroadcast(s) { \
686 int val = 1; \
687 setsockopt(s, SOL_SOCKET, SO_BROADCAST, (const void *)&val, sizeof(int)); \
688 }
689
690 #define SELECT_NFDS(x) (x)
691
692 #if !(HAVE_SOCKLEN_T) && !defined(_USE_GNU_PTH)
693 typedef unsigned int socklen_t;
694 #endif
695
696 #ifndef SUN_LEN
697 #define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
698 #endif
699
700 // Shared library suffix
701 #ifndef SHLIB_SUFFIX
702 #if defined(_HPUX) && !defined(__64BIT__)
703 #define SHLIB_SUFFIX _T(".sl")
704 #else
705 #define SHLIB_SUFFIX _T(".so")
706 #endif
707 #endif
708
709 typedef struct hostent HOSTENT;
710
711 #if HAVE_DECL___VA_COPY && !HAVE_DECL_VA_COPY
712 #define HAVE_DECL_VA_COPY 1
713 #define va_copy(d,s) __va_copy(d,s)
714 #endif
715
716 #endif /* _WIN32 */
717
718 /**
719 * Value used to indicate invalid pointer where NULL is not appropriate
720 */
721 #ifdef __64BIT__
722 #define INVALID_POINTER_VALUE ((void *)_ULL(0xFFFFFFFFFFFFFFFF))
723 #else
724 #define INVALID_POINTER_VALUE ((void *)0xFFFFFFFF)
725 #endif
726
727 /**
728 * Casting between pointer and 32-bit integer
729 */
730 #ifdef __64BIT__
731 #define CAST_FROM_POINTER(p, t) ((t)((UINT64)(p)))
732 #define CAST_TO_POINTER(v, t) ((t)((UINT64)(v)))
733 #else
734 #define CAST_FROM_POINTER(p, t) ((t)(p))
735 #define CAST_TO_POINTER(v, t) ((t)(v))
736 #endif
737
738 /**
739 * open() flags compatibility
740 */
741 #ifndef O_BINARY
742 #define O_BINARY 0
743 #endif
744
745 /**
746 * Windows-specific structures for non-Windows platforms
747 */
748 #ifndef _WIN32
749
750 typedef struct tagPOINT
751 {
752 int x;
753 int y;
754 } POINT;
755
756 #endif
757
758 /**
759 * Event log severity codes
760 */
761 #ifndef _WIN32
762 #define EVENTLOG_SUCCESS 0x0000
763 #define EVENTLOG_ERROR_TYPE 0x0001
764 #define EVENTLOG_WARNING_TYPE 0x0002
765 #define EVENTLOG_INFORMATION_TYPE 0x0004
766 #define EVENTLOG_AUDIT_SUCCESS 0x0008
767 #define EVENTLOG_AUDIT_FAILURE 0x0010
768 #endif /* _WIN32 */
769
770 #define NXLOG_DEBUG EVENTLOG_DEBUG_TYPE
771 #define NXLOG_INFO EVENTLOG_INFORMATION_TYPE
772 #define NXLOG_WARNING EVENTLOG_WARNING_TYPE
773 #define NXLOG_ERROR EVENTLOG_ERROR_TYPE
774
775 /**
776 * Interface types
777 */
778 #define IFTYPE_OTHER 1
779 #define IFTYPE_REGULAR1822 2
780 #define IFTYPE_HDH1822 3
781 #define IFTYPE_DDN_X25 4
782 #define IFTYPE_RFC877_X25 5
783 #define IFTYPE_ETHERNET_CSMACD 6
784 #define IFTYPE_ISO88023_CSMACD 7
785 #define IFTYPE_ISO88024_TOKENBUS 8
786 #define IFTYPE_ISO88025_TOKENRING 9
787 #define IFTYPE_ISO88026_MAN 10
788 #define IFTYPE_STARLAN 11
789 #define IFTYPE_PROTEON_10MBIT 12
790 #define IFTYPE_PROTEON_80MBIT 13
791 #define IFTYPE_HYPERCHANNEL 14
792 #define IFTYPE_FDDI 15
793 #define IFTYPE_LAPB 16
794 #define IFTYPE_SDLC 17
795 #define IFTYPE_DS1 18
796 #define IFTYPE_E1 19
797 #define IFTYPE_BASIC_ISDN 20
798 #define IFTYPE_PRIMARY_ISDN 21
799 #define IFTYPE_PROP_PTP_SERIAL 22
800 #define IFTYPE_PPP 23
801 #define IFTYPE_SOFTWARE_LOOPBACK 24
802 #define IFTYPE_EON 25
803 #define IFTYPE_ETHERNET_3MBIT 26
804 #define IFTYPE_NSIP 27
805 #define IFTYPE_SLIP 28
806 #define IFTYPE_ULTRA 29
807 #define IFTYPE_DS3 30
808 #define IFTYPE_SMDS 31
809 #define IFTYPE_FRAME_RELAY 32
810 #define IFTYPE_RS232 33
811 #define IFTYPE_PARA 34
812 #define IFTYPE_ARCNET 35
813 #define IFTYPE_ARCNET_PLUS 36
814 #define IFTYPE_ATM 37
815 #define IFTYPE_MIOX25 38
816 #define IFTYPE_SONET 39
817 #define IFTYPE_X25PLE 40
818 #define IFTYPE_ISO88022LLC 41
819 #define IFTYPE_LOCALTALK 42
820 #define IFTYPE_SMDS_DXI 43
821 #define IFTYPE_FRAME_RELAY_SERVICE 44
822 #define IFTYPE_V35 45
823 #define IFTYPE_HSSI 46
824 #define IFTYPE_HIPPI 47
825 #define IFTYPE_MODEM 48
826 #define IFTYPE_AAL5 49
827 #define IFTYPE_SONET_PATH 50
828 #define IFTYPE_SONET_VT 51
829 #define IFTYPE_SMDS_ICIP 52
830 #define IFTYPE_PROP_VIRTUAL 53
831 #define IFTYPE_PROP_MULTIPLEXOR 54
832 #define IFTYPE_IEEE80212 55
833 #define IFTYPE_FIBRECHANNEL 56
834 #define IFTYPE_HIPPIINTERFACE 57
835 #define IFTYPE_FRAME_RELAY_INTERCONNECT 58
836 #define IFTYPE_AFLANE8023 59
837 #define IFTYPE_AFLANE8025 60
838 #define IFTYPE_CCTEMUL 61
839 #define IFTYPE_FAST_ETHERNET 62
840 #define IFTYPE_ISDN 63
841 #define IFTYPE_V11 64
842 #define IFTYPE_V36 65
843 #define IFTYPE_G703_AT64K 66
844 #define IFTYPE_G703_AT2MB 67
845 #define IFTYPE_QLLC 68
846 #define IFTYPE_FASTETHERFX 69
847 #define IFTYPE_CHANNEL 70
848 #define IFTYPE_IEEE80211 71
849 #define IFTYPE_IBM370_PARCHAN 72
850 #define IFTYPE_ESCON 73
851 #define IFTYPE_DLSW 74
852 #define IFTYPE_ISDNS 75
853 #define IFTYPE_ISDNU 76
854 #define IFTYPE_LAPD 77
855 #define IFTYPE_IPSWITCH 78
856 #define IFTYPE_RSRB 79
857 #define IFTYPE_ATMLOGICAL 80
858 #define IFTYPE_DS0 81
859 #define IFTYPE_DS0_BUNDLE 82
860 #define IFTYPE_BSC 83
861 #define IFTYPE_ASYNC 84
862 #define IFTYPE_CNR 85
863 #define IFTYPE_ISO88025DTR 86
864 #define IFTYPE_EPLRS 87
865 #define IFTYPE_ARAP 88
866 #define IFTYPE_PROPCNLS 89
867 #define IFTYPE_HOSTPAD 90
868 #define IFTYPE_TERMPAD 91
869 #define IFTYPE_FRAME_RELAY_MPI 92
870 #define IFTYPE_X213 93
871 #define IFTYPE_ADSL 94
872 #define IFTYPE_RADSL 95
873 #define IFTYPE_SDSL 96
874 #define IFTYPE_VDSL 97
875 #define IFTYPE_ISO88025CRFPINT 98
876 #define IFTYPE_MYRINET 99
877 #define IFTYPE_VOICEEM 100
878 #define IFTYPE_VOICEFXO 101
879 #define IFTYPE_VOICEFXS 102
880 #define IFTYPE_VOICEENCAP 103
881 #define IFTYPE_VOICEOVERIP 104
882 #define IFTYPE_ATMDXI 105
883 #define IFTYPE_ATMFUNI 106
884 #define IFTYPE_ATMIMA 107
885 #define IFTYPE_PPPMULTILINKBUNDLE 108
886 #define IFTYPE_IPOVERCDLC 109
887 #define IFTYPE_IPOVERCLAW 110
888 #define IFTYPE_STACKTOSTACK 111
889 #define IFTYPE_VIRTUAL_IP_ADDRESS 112
890 #define IFTYPE_MPC 113
891 #define IFTYPE_IPOVERATM 114
892 #define IFTYPE_ISO88025FIBER 115
893 #define IFTYPE_TDLC 116
894 #define IFTYPE_GIGABIT_ETHERNET 117
895 #define IFTYPE_HDLC 118
896 #define IFTYPE_LAPF 119
897 #define IFTYPE_V37 120
898 #define IFTYPE_X25MLP 121
899 #define IFTYPE_X25_HUNT_GROUP 122
900 #define IFTYPE_TRANSPHDLC 123
901 #define IFTYPE_INTERLEAVE 124
902 #define IFTYPE_FAST 125
903 #define IFTYPE_IP 126
904 #define IFTYPE_DOCSCABLE_MACLAYER 127
905 #define IFTYPE_DOCSCABLE_DOWNSTREAM 128
906 #define IFTYPE_DOCSCABLE_UPSTREAM 129
907 #define IFTYPE_A12MPPSWITCH 130
908 #define IFTYPE_TUNNEL 131
909 #define IFTYPE_COFFEE 132
910 #define IFTYPE_CES 133
911 #define IFTYPE_ATM_SUBINTERFACE 134
912 #define IFTYPE_L2VLAN 135
913 #define IFTYPE_L3IPVLAN 136
914 #define IFTYPE_L3IPXVLAN 137
915 #define IFTYPE_DIGITAL_POWERLINE 138
916 #define IFTYPE_MEDIAMAIL_OVER_IP 139
917 #define IFTYPE_DTM 140
918 #define IFTYPE_DCN 141
919 #define IFTYPE_IPFORWARD 142
920 #define IFTYPE_MSDSL 143
921 #define IFTYPE_IEEE1394 144
922 #define IFTYPE_GSN 145
923 #define IFTYPE_DVBRCC_MACLAYER 146
924 #define IFTYPE_DVBRCC_DOWNSTREAM 147
925 #define IFTYPE_DVBRCC_UPSTREAM 148
926 #define IFTYPE_ATM_VIRTUAL 149
927 #define IFTYPE_MPLS_TUNNEL 150
928 #define IFTYPE_SRP 151
929 #define IFTYPE_VOICE_OVER_ATM 152
930 #define IFTYPE_VOICE_OVER_FRAME_RELAY 153
931 #define IFTYPE_IDSL 154
932 #define IFTYPE_COMPOSITE_LINK 155
933 #define IFTYPE_SS7_SIGLINK 156
934 #define IFTYPE_PROPWIRELESSP2P 157
935 #define IFTYPE_FRFORWARD 158
936 #define IFTYPE_RFC1483 159
937 #define IFTYPE_USB 160
938 #define IFTYPE_IEEE8023ADLAG 161
939 #define IFTYPE_BGP_POLICY_ACCOUNTING 162
940 #define IFTYPE_FRF16MFR_BUNDLE 163
941 #define IFTYPE_H323_GATEKEEPER 164
942 #define IFTYPE_H323_PROXY 165
943 #define IFTYPE_MPLS 166
944 #define IFTYPE_MFSIGLINK 167
945 #define IFTYPE_HDSL2 168
946 #define IFTYPE_SHDSL 169
947 #define IFTYPE_DS1FDL 170
948 #define IFTYPE_POS 171
949 #define IFTYPE_DVBASI_IN 172
950 #define IFTYPE_DVBASI_OUT 173
951 #define IFTYPE_PLC 174
952 #define IFTYPE_NFAS 175
953 #define IFTYPE_TR008 176
954 #define IFTYPE_GR303RDT 177
955 #define IFTYPE_GR303IDT 178
956 #define IFTYPE_ISUP 179
957 #define IFTYPE_PROPDOCSWIRELESSMACLAYER 180
958 #define IFTYPE_PROPDOCSWIRELESSDOWNSTREAM 181
959 #define IFTYPE_PROPDOCSWIRELESSUPSTREAM 182
960 #define IFTYPE_HIPERLAN2 183
961 #define IFTYPE_PROPBWAP2MP 184
962 #define IFTYPE_SONET_OVERHEAD_CHANNEL 185
963 #define IFTYPE_DW_OVERHEAD_CHANNEL 186
964 #define IFTYPE_AAL2 187
965 #define IFTYPE_RADIOMAC 188
966 #define IFTYPE_ATMRADIO 189
967 #define IFTYPE_IMT 190
968 #define IFTYPE_MVL 191
969 #define IFTYPE_REACHDSL 192
970 #define IFTYPE_FRDLCIENDPT 193
971 #define IFTYPE_ATMVCIENDPT 194
972 #define IFTYPE_OPTICAL_CHANNEL 195
973 #define IFTYPE_OPTICAL_TRANSPORT 196
974 #define IFTYPE_PROPATM 197
975 #define IFTYPE_VOICE_OVER_CABLE 198
976 #define IFTYPE_INFINIBAND 199
977 #define IFTYPE_TELINK 200
978 #define IFTYPE_Q2931 201
979 #define IFTYPE_VIRTUALTG 202
980 #define IFTYPE_SIPTG 203
981 #define IFTYPE_SIPSIG 204
982 #define IFTYPE_DOCSCABLEUPSTREAMCHANNEL 205
983 #define IFTYPE_ECONET 206
984 #define IFTYPE_PON155 207
985 #define IFTYPE_PON622 208
986 #define IFTYPE_BRIDGE 209
987 #define IFTYPE_LINEGROUP 210
988 #define IFTYPE_VOICEEMFGD 211
989 #define IFTYPE_VOICEFGDEANA 212
990 #define IFTYPE_VOICEDID 213
991 #define IFTYPE_MPEG_TRANSPORT 214
992 #define IFTYPE_SIXTOFOUR 215
993 #define IFTYPE_GTP 216
994 #define IFTYPE_PDNETHERLOOP1 217
995 #define IFTYPE_PDNETHERLOOP2 218
996 #define IFTYPE_OPTICAL_CHANNEL_GROUP 219
997 #define IFTYPE_HOMEPNA 220
998 #define IFTYPE_GFP 221
999 #define IFTYPE_CISCO_ISL_VLAN 222
1000 #define IFTYPE_ACTELIS_METALOOP 223
1001 #define IFTYPE_FCIPLINK 224
1002 #define IFTYPE_RPR 225
1003 #define IFTYPE_QAM 226
1004 #define IFTYPE_LMP 227
1005 #define IFTYPE_CBLVECTASTAR 228
1006 #define IFTYPE_DOCSCABLEMCMTSDOWNSTREAM 229
1007 #define IFTYPE_ADSL2 230
1008 #define IFTYPE_MACSECCONTROLLEDIF 231
1009 #define IFTYPE_MACSECUNCONTROLLEDIF 232
1010 #define IFTYPE_AVICIOPTICALETHER 233
1011 #define IFTYPE_ATM_BOND 234
1012 #define IFTYPE_VOICEFGDOS 235
1013 #define IFTYPE_MOCA_VERSION1 236
1014 #define IFTYPE_IEEE80216WMAN 237
1015 #define IFTYPE_ADSL2PLUS 238
1016 #define IFTYPE_DVBRCSMACLAYER 239
1017 #define IFTYPE_DVBTDM 240
1018 #define IFTYPE_DVBRCSTDMA 241
1019 #define IFTYPE_X86LAPS 242
1020 #define IFTYPE_WWANPP 243
1021 #define IFTYPE_WWANPP2 244
1022 #define IFTYPE_VOICEEBS 245
1023 #define IFTYPE_IFPWTYPE 246
1024 #define IFTYPE_ILAN 247
1025 #define IFTYPE_PIP 248
1026 #define IFTYPE_ALUELP 249
1027 #define IFTYPE_GPON 250
1028 #define IFTYPE_VDSL2 251
1029 #define IFTYPE_CAPWAP_DOT11_PROFILE 252
1030 #define IFTYPE_CAPWAP_DOT11_BSS 253
1031 #define IFTYPE_CAPWAP_WTP_VIRTUAL_RADIO 254
1032 #define IFTYPE_BITS 255
1033 #define IFTYPE_DOCSCABLEUPSTREAMRFPORT 256
1034 #define IFTYPE_CABLEDOWNSTREAMRFPORT 257
1035 #define IFTYPE_VMWARE_VIRTUAL_NIC 258
1036 #define IFTYPE_IEEE802154 259
1037 #define IFTYPE_OTNODU 260
1038 #define IFTYPE_OTNOTU 261
1039 #define IFTYPE_IFVFITYPE 262
1040 #define IFTYPE_G9981 263
1041 #define IFTYPE_G9982 264
1042 #define IFTYPE_G9983 265
1043 #define IFTYPE_ALUEPON 266
1044 #define IFTYPE_ALUEPONONU 267
1045 #define IFTYPE_ALUEPONPHYSICALUNI 268
1046 #define IFTYPE_ALUEPONLOGICALLINK 269
1047 #define IFTYPE_ALUGPONONU 270
1048 #define IFTYPE_ALUGPONPHYSICALUNI 271
1049 #define IFTYPE_VMWARE_NIC_TEAM 272
1050
1051 /**
1052 * IP Header -- RFC 791
1053 */
1054 typedef struct tagIPHDR
1055 {
1056 BYTE m_cVIHL; // Version and IHL
1057 BYTE m_cTOS; // Type Of Service
1058 WORD m_wLen; // Total Length
1059 WORD m_wId; // Identification
1060 WORD m_wFlagOff; // Flags and Fragment Offset
1061 BYTE m_cTTL; // Time To Live
1062 BYTE m_cProtocol; // Protocol
1063 WORD m_wChecksum; // Checksum
1064 struct in_addr m_iaSrc; // Internet Address - Source
1065 struct in_addr m_iaDst; // Internet Address - Destination
1066 } IPHDR;
1067
1068 /**
1069 * ICMP Header - RFC 792
1070 */
1071 typedef struct tagICMPHDR
1072 {
1073 BYTE m_cType; // Type
1074 BYTE m_cCode; // Code
1075 WORD m_wChecksum; // Checksum
1076 WORD m_wId; // Identification
1077 WORD m_wSeq; // Sequence
1078 } ICMPHDR;
1079
1080 /**
1081 * INADDR_NONE can be undefined on some systems
1082 */
1083 #ifndef INADDR_NONE
1084 #define INADDR_NONE (0xFFFFFFFF)
1085 #endif
1086
1087 /**
1088 * Check if given string is NULL and always return valid pointer
1089 */
1090 #define CHECK_NULL(x) ((x) == NULL ? _T("(null)") : (x))
1091 #define CHECK_NULL_A(x) ((x) == NULL ? "(null)" : (x))
1092 #define CHECK_NULL_EX(x) ((x) == NULL ? _T("") : (x))
1093 #define CHECK_NULL_EX_A(x) ((x) == NULL ? "" : (x))
1094
1095 /**
1096 * Free memory block if it isn't NULL
1097 */
1098 #if FREE_IS_NULL_SAFE
1099 #define safe_free(x) free(x);
1100 #define safe_free_and_null(x) { free(x); x = NULL; }
1101 #else
1102 #define safe_free(x) { if ((x) != NULL) free(x); }
1103 #define safe_free_and_null(x) { if ((x) != NULL) { free(x); x = NULL; } }
1104 #endif
1105
1106 /**
1107 * free() wrapper if it isn't NULL safe
1108 */
1109 #if defined(__cplusplus) && !FREE_IS_NULL_SAFE
1110 inline void nx_free(void *p)
1111 {
1112 if (p != NULL)
1113 free(p);
1114 }
1115 #define free nx_free
1116 #endif
1117
1118 /**
1119 * delete object and nullify pointer
1120 */
1121 #define delete_and_null(x) { delete x; x = NULL; }
1122
1123 /**
1124 * Convert half-byte's value to hex digit and vice versa
1125 */
1126 #define bin2hex(x) ((x) < 10 ? ((x) + _T('0')) : ((x) + (_T('A') - 10)))
1127 #ifdef UNICODE
1128 #define hex2bin(x) ((((x) >= _T('0')) && ((x) <= _T('9'))) ? ((x) - _T('0')) : \
1129 (((towupper(x) >= _T('A')) && (towupper(x) <= _T('F'))) ? (towupper(x) - _T('A') + 10) : 0))
1130 #else
1131 #define hex2bin(x) ((((x) >= '0') && ((x) <= '9')) ? ((x) - '0') : \
1132 (((toupper(x) >= 'A') && (toupper(x) <= 'F')) ? (toupper(x) - 'A' + 10) : 0))
1133 #endif
1134
1135 /**
1136 * Define MIN() and MAX()
1137 */
1138 #undef MIN
1139 #define MIN(a, b) ((a) < (b) ? (a) : (b))
1140 #undef MAX
1141 #define MAX(a, b) ((a) > (b) ? (a) : (b))
1142
1143 /**
1144 * Define _tcsicmp() for non-windows
1145 */
1146 #ifndef _WIN32
1147 #define stricmp strcasecmp
1148 #define strnicmp strncasecmp
1149 #define wcsicmp wcscasecmp
1150 #define wcsnicmp wcsncasecmp
1151 #endif
1152
1153 /**
1154 * Compare two numbers and return -1, 0, or 1
1155 */
1156 #define COMPARE_NUMBERS(n1,n2) (((n1) < (n2)) ? -1 : (((n1) > (n2)) ? 1 : 0))
1157
1158 /**
1159 * Increment pointer for given number of bytes
1160 */
1161 #define inc_ptr(ptr, step, ptype) ptr = (ptype *)(((char *)ptr) + step)
1162
1163 /**
1164 * Validate numerical value
1165 */
1166 #define VALIDATE_VALUE(var,x,y,z) { if ((var < x) || (var > y)) var = z; }
1167
1168 /**
1169 * DCI (data collection item) data types
1170 */
1171 #define DCI_DT_INT 0
1172 #define DCI_DT_UINT 1
1173 #define DCI_DT_INT64 2
1174 #define DCI_DT_UINT64 3
1175 #define DCI_DT_STRING 4
1176 #define DCI_DT_FLOAT 5
1177 #define DCI_DT_NULL 6
1178 #define DCI_DT_DEPRECATED 255 /* used internally by agent */
1179
1180 /**
1181 * Insert parameter as string
1182 */
1183 #ifdef UNICODE
1184 #define STRING(x) L#x
1185 #else
1186 #define STRING(x) #x
1187 #endif
1188
1189 /**
1190 * Pipe handle
1191 */
1192 #ifdef _WIN32
1193 #define HPIPE HANDLE
1194 #define INVALID_PIPE_HANDLE INVALID_HANDLE_VALUE
1195 #else
1196 #define HPIPE int
1197 #define INVALID_PIPE_HANDLE (-1)
1198 #endif
1199
1200 #ifdef __cplusplus
1201
1202 #include <algorithm>
1203
1204 /**
1205 * Session enumeration callback return codes
1206 */
1207 enum EnumerationCallbackResult
1208 {
1209 _STOP = 0,
1210 _CONTINUE = 1
1211 };
1212
1213 /**
1214 * Disable copy constructor if compiler supports it
1215 */
1216 #if CAN_DELETE_COPY_CTOR
1217 #define DISABLE_COPY_CTOR(c) c (const c &s) = delete;
1218 #else
1219 #define DISABLE_COPY_CTOR(c)
1220 #endif
1221
1222 #endif
1223
1224 #endif /* _nms_common_h_ */