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