Preparation for UNICODE support on UNIX
[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 #if !defined(_WIN32) && !defined(UNDER_CE)
33 #ifdef _NETWARE
34 #include <config-netware.h>
35 #else
36 #include "config.h"
37 #ifdef WITH_OPENSSL
38 #define _WITH_ENCRYPTION 1
39 #endif
40 #endif
41 #else /* _WIN32 */
42 #ifndef UNDER_CE
43 #define _WITH_ENCRYPTION 1
44 #endif
45 #define _CRT_SECURE_NO_DEPRECATE
46 #endif
47
48 #ifdef HAVE_STDARG_H
49 # include <stdarg.h>
50 #endif
51
52 #include <unicode.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <limits.h>
56
57 #include <netxms-version.h>
58
59
60 //
61 // Wrappers for 64-bit constants
62 //
63
64 #if defined(__GNUC__) || defined(__HP_aCC)
65 #define _LL(x) (x ## LL)
66 #define _ULL(x) (x ## ULL)
67 #else
68 #define _LL(x) (x)
69 #define _ULL(x) (x)
70 #endif
71
72
73 //
74 // Common constants
75 //
76
77 #define MAX_SECRET_LENGTH 64
78 #define INVALID_POINTER_VALUE ((void *)0xFFFFFFFF)
79 #define MAX_DB_STRING 256
80 #define MAX_PARAM_NAME 256
81 #define GROUP_FLAG ((DWORD)0x80000000)
82
83 #define NETXMS_MAX_CIPHERS 4
84 #define NETXMS_RSA_KEYLEN 2048
85
86 #ifndef LLONG_MAX
87 #define LLONG_MAX _LL(9223372036854775807)
88 #endif
89
90 #ifndef LLONG_MIN
91 #define LLONG_MIN (-LLONG_MAX - 1)
92 #endif
93
94 #ifndef ULLONG_MAX
95 #define ULLONG_MAX _ULL(18446744073709551615)
96 #endif
97
98
99 //
100 // Platform dependent includes and defines
101 //
102
103 #if defined(_WIN32) || defined(UNDER_CE)
104
105 /********** WINDOWS ********************/
106
107 #define FS_PATH_SEPARATOR _T("\\")
108 #define FS_PATH_SEPARATOR_CHAR _T('\\')
109
110 #include <winsock2.h>
111 #include <windows.h>
112 #include <stdlib.h>
113
114 #ifndef UNDER_CE
115 #include <sys/stat.h>
116 #include <process.h>
117 #include <io.h>
118 #include <fcntl.h>
119 #endif
120
121 #ifndef S_IRUSR
122 # define S_IRUSR 0400
123 #endif
124 #ifndef S_IWUSR
125 # define S_IWUSR 0200
126 #endif
127
128 #define snprintf _snprintf
129 #define vsnprintf _vsnprintf
130 #define popen _popen
131 #define pclose _pclose
132
133 typedef unsigned __int64 QWORD;
134 typedef __int64 INT64;
135 typedef int socklen_t;
136
137 #define INT64_FMT _T("%I64d")
138 #define UINT64_FMT _T("%I64u")
139 #ifdef __64BIT__
140 #define TIME_T_FMT _T("%I64u")
141 #else
142 #define TIME_T_FMT _T("%u")
143 #endif
144
145 // Socket compatibility
146 #define SHUT_RD 0
147 #define SHUT_WR 1
148 #define SHUT_RDWR 2
149
150 #define SetSocketReuseFlag(sd)
151 #define SELECT_NFDS(x) ((int)(x))
152
153 #ifdef UNDER_CE
154 #define O_RDONLY 0x0004
155 #define O_WRONLY 0x0001
156 #define O_RDWR 0x0002
157 #define O_CREAT 0x0100
158 #define O_EXCL 0x0200
159 #define O_TRUNC 0x0800
160 #endif
161
162 #elif defined(_NETWARE)
163
164 /********** NETWARE ********************/
165
166 #define FS_PATH_SEPARATOR _T("/")
167 #define FS_PATH_SEPARATOR_CHAR _T('/')
168
169 #include <unistd.h>
170 #include <stdlib.h>
171 #include <sys/types.h>
172 #include <sys/stat.h>
173 #include <sys/socket.h>
174 #include <sys/select.h>
175 #include <netinet/in.h>
176 #include <arpa/inet.h>
177 #include <stdint.h>
178 #include <fcntl.h>
179 #include <errno.h>
180 #include <dirent.h>
181 #include <wchar.h>
182 #include <netdb.h>
183
184 typedef int BOOL;
185 #if (SIZEOF_LONG == 4)
186 typedef long LONG;
187 #else
188 typedef int LONG;
189 #endif
190 #if (SIZEOF_LONG == 4)
191 typedef unsigned long DWORD;
192 #else
193 typedef unsigned int DWORD;
194 #endif
195 typedef unsigned short WORD;
196 typedef unsigned char BYTE;
197 typedef void * HANDLE;
198 typedef void * HMODULE;
199
200 #ifdef X_INT64_X
201 typedef X_INT64_X INT64;
202 #else
203 #error Target system does not have signed 64bit integer type
204 #endif
205
206 #ifdef X_UINT64_X
207 typedef X_UINT64_X QWORD;
208 #else
209 #error Target system does not have unsigned 64bit integer type
210 #endif
211
212 #define INT64_FMT _T("%lld")
213 #define UINT64_FMT _T("%llu")
214 #define TIME_T_FMT _T("%u")
215
216 #ifndef MAX_PATH
217 #define MAX_PATH 256
218 #endif
219
220 // Socket compatibility
221 typedef int SOCKET;
222
223 #define closesocket(x) close(x)
224 #define WSAGetLastError() (errno)
225
226 #define WSAEINTR EINTR
227 #define INVALID_SOCKET (-1)
228
229 //#define SetSocketReuseFlag(sd)
230 #define SetSocketReuseFlag(sd) { \
231 int nVal = 1; \
232 setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (const void *)&nVal, \
233 (socklen_t)sizeof(nVal)); \
234 }
235
236 #define SELECT_NFDS(x) (x)
237
238 #else /* not _WIN32 and not _NETWARE */
239
240 /*********** UNIX *********************/
241
242 #ifndef PREFIX
243 #define PREFIX "/usr/local"
244 #warning Installation prefix not defined, defaulting to /usr/local
245 #endif
246
247 #if HAVE_WCHAR_H
248 #include <wchar.h>
249 #endif
250
251 #if HAVE_WCTYPE_H
252 #include <wctype.h>
253 #endif
254
255 #include <errno.h>
256
257 #define FS_PATH_SEPARATOR _T("/")
258 #define FS_PATH_SEPARATOR_CHAR _T('/')
259
260 #if HAVE_UNISTD_H
261 #include <unistd.h>
262 #endif
263
264 #if HAVE_STDLIB_H
265 #include <stdlib.h>
266 #endif
267
268 #if HAVE_SYS_TYPES_H
269 #include <sys/types.h>
270 #endif
271
272 #if HAVE_SYS_INT_TYPES_H
273 #include <sys/int_types.h>
274 #endif
275
276 #if HAVE_SYS_STAT_H
277 #include <sys/stat.h>
278 #endif
279
280 #if HAVE_SYS_SOCKET_H
281 #include <sys/socket.h>
282 #endif
283
284 #if HAVE_SYS_SELECT_H
285 #include <sys/select.h>
286 #endif
287
288 #if HAVE_NETINET_IN_H
289 #include <netinet/in.h>
290 #endif
291
292 #if HAVE_NET_NH_H
293 #include <net/nh.h>
294 #endif
295
296 #if HAVE_ARPA_INET_H
297 #include <arpa/inet.h>
298 #endif
299
300 #if HAVE_NETDB_H
301 #include <netdb.h>
302 #endif
303
304 #if HAVE_FCNTL_H
305 #include <fcntl.h>
306 #endif
307
308 #include <dirent.h>
309
310 #if _USE_GNU_PTH
311 #include <pth.h>
312 #endif
313
314 typedef int BOOL;
315 #if (SIZEOF_LONG == 4)
316 typedef long LONG;
317 typedef unsigned long DWORD;
318 #undef __64BIT__
319 #else
320 typedef int LONG;
321 typedef unsigned int DWORD;
322 #ifndef __64BIT__
323 #define __64BIT__
324 #endif
325 #endif
326 typedef unsigned short WORD;
327 typedef unsigned char BYTE;
328 typedef void * HANDLE;
329 typedef void * HMODULE;
330
331 #if !HAVE_MODE_T
332 typedef int mode_t;
333 #endif
334
335 #if HAVE_INT64_T
336 typedef int64_t INT64;
337 #else
338 #error Target system does not have signed 64bit integer type
339 #endif
340
341 #if HAVE_UINT64_T
342 typedef uint64_t QWORD;
343 #elif HAVE_U_INT64_T
344 typedef u_int64_t QWORD;
345 #else
346 #error Target system does not have unsigned 64bit integer type
347 #endif
348
349 #define INT64_FMT _T("%lld")
350 #define UINT64_FMT _T("%llu")
351 #ifdef __64BIT__
352 #define TIME_T_FMT _T("%llu")
353 #else
354 #define TIME_T_FMT _T("%u")
355 #endif
356
357 #ifndef TRUE
358 #define TRUE 1
359 #endif
360 #ifndef FALSE
361 #define FALSE 0
362 #endif
363
364 #ifndef MAX_PATH
365 #define MAX_PATH 256
366 #endif
367
368 // Socket compatibility
369 typedef int SOCKET;
370
371 #define closesocket(x) close(x)
372 #define WSAGetLastError() (errno)
373
374 #define WSAEINTR EINTR
375 #define INVALID_SOCKET (-1)
376
377 #define SetSocketReuseFlag(sd) { \
378 int nVal = 1; \
379 setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (const void *)&nVal, \
380 (socklen_t)sizeof(nVal)); \
381 }
382
383 #define SELECT_NFDS(x) (x)
384
385 #if !(HAVE_SOCKLEN_T) && !defined(_USE_GNU_PTH)
386 typedef unsigned int socklen_t;
387 #endif
388
389 // Shared library suffix
390 #ifndef SHL_SUFFIX
391 #if defined(_HPUX) && !defined(__64BIT__)
392 #define SHL_SUFFIX ".sl"
393 #else
394 #define SHL_SUFFIX ".so"
395 #endif
396 #endif
397
398 typedef struct hostent HOSTENT;
399
400 #endif /* _WIN32 */
401
402
403 //
404 // Casting between pointer and 32-bit integer
405 //
406
407 #ifdef __64BIT__
408 #define CAST_FROM_POINTER(p, t) ((t)((QWORD)p))
409 #define CAST_TO_POINTER(v, t) ((t)((QWORD)v))
410 #else
411 #define CAST_FROM_POINTER(p, t) ((t)p)
412 #define CAST_TO_POINTER(v, t) ((t)v)
413 #endif
414
415
416 //
417 // OpenSSL
418 //
419
420 #ifdef _WITH_ENCRYPTION
421
422 #include <openssl/crypto.h>
423 #include <openssl/rsa.h>
424 #include <openssl/pem.h>
425 #include <openssl/evp.h>
426 #include <openssl/rand.h>
427 #include <openssl/opensslv.h>
428 #include <openssl/err.h>
429
430 #ifdef NETXMS_NO_AES
431 #ifndef OPENSSL_NO_AES
432 #define OPENSSL_NO_AES
433 #endif
434 #endif
435
436 #ifdef NETXMS_NO_BF
437 #ifndef OPENSSL_NO_BF
438 #define OPENSSL_NO_BF
439 #endif
440 #endif
441
442 #ifdef NETXMS_NO_IDEA
443 #ifndef OPENSSL_NO_IDEA
444 #define OPENSSL_NO_IDEA
445 #endif
446 #endif
447
448 #ifdef NETXMS_NO_DES
449 #ifndef OPENSSL_NO_DES
450 #define OPENSSL_NO_DES
451 #endif
452 #endif
453
454 #if OPENSSL_VERSION_NUMBER >= 0x00907000
455 #define OPENSSL_CONST const
456 #else
457 #define OPENSSL_CONST
458 #endif
459
460 #else
461
462 // Prevent compilation errors on function prototypes
463 #define RSA void
464
465 #endif
466
467
468 //
469 // open() flags compatibility
470 //
471
472 #ifndef O_BINARY
473 #define O_BINARY 0
474 #endif
475
476
477 //
478 // Windows-specific structures for non-Windows platforms
479 //
480
481 #ifndef _WIN32
482
483 typedef struct tagPOINT
484 {
485 int x;
486 int y;
487 } POINT;
488
489 #endif
490
491
492 //
493 // Event log severity codes (UNIX only)
494 //
495
496 #ifndef _WIN32
497 #define EVENTLOG_INFORMATION_TYPE 0
498 #define EVENTLOG_WARNING_TYPE 1
499 #define EVENTLOG_ERROR_TYPE 2
500 #endif /* _WIN32 */
501
502
503 //
504 // Interface types
505 //
506
507 #define IFTYPE_OTHER 1
508 #define IFTYPE_REGULAR1822 2
509 #define IFTYPE_HDH1822 3
510 #define IFTYPE_DDN_X25 4
511 #define IFTYPE_RFC877_X25 5
512 #define IFTYPE_ETHERNET_CSMACD 6
513 #define IFTYPE_ISO88023_CSMACD 7
514 #define IFTYPE_ISO88024_TOKENBUS 8
515 #define IFTYPE_ISO88025_TOKENRING 9
516 #define IFTYPE_ISO88026_MAN 10
517 #define IFTYPE_STARLAN 11
518 #define IFTYPE_PROTEON_10MBIT 12
519 #define IFTYPE_PROTEON_80MBIT 13
520 #define IFTYPE_HYPERCHANNEL 14
521 #define IFTYPE_FDDI 15
522 #define IFTYPE_LAPB 16
523 #define IFTYPE_SDLC 17
524 #define IFTYPE_DS1 18
525 #define IFTYPE_E1 19
526 #define IFTYPE_BASIC_ISDN 20
527 #define IFTYPE_PRIMARY_ISDN 21
528 #define IFTYPE_PROP_PTP_SERIAL 22
529 #define IFTYPE_PPP 23
530 #define IFTYPE_SOFTWARE_LOOPBACK 24
531 #define IFTYPE_EON 25
532 #define IFTYPE_ETHERNET_3MBIT 26
533 #define IFTYPE_NSIP 27
534 #define IFTYPE_SLIP 28
535 #define IFTYPE_ULTRA 29
536 #define IFTYPE_DS3 30
537 #define IFTYPE_SMDS 31
538 #define IFTYPE_FRAME_RELAY 32
539 #define IFTYPE_RS232 33
540 #define IFTYPE_PARA 34
541 #define IFTYPE_ARCNET 35
542 #define IFTYPE_ARCNET_PLUS 36
543 #define IFTYPE_ATM 37
544 #define IFTYPE_MIOX25 38
545 #define IFTYPE_SONET 39
546 #define IFTYPE_X25PLE 40
547 #define IFTYPE_ISO88022LLC 41
548 #define IFTYPE_LOCALTALK 42
549 #define IFTYPE_SMDS_DXI 43
550 #define IFTYPE_FRAME_RELAY_SERVICE 44
551 #define IFTYPE_V35 45
552 #define IFTYPE_HSSI 46
553 #define IFTYPE_HIPPI 47
554 #define IFTYPE_MODEM 48
555 #define IFTYPE_AAL5 49
556 #define IFTYPE_SONET_PATH 50
557 #define IFTYPE_SONET_VT 51
558 #define IFTYPE_SMDS_ICIP 52
559 #define IFTYPE_PROP_VIRTUAL 53
560 #define IFTYPE_PROP_MULTIPLEXOR 54
561 #define IFTYPE_IEEE80212 55
562 #define IFTYPE_FIBRECHANNEL 56
563
564 #define IFTYPE_NETXMS_NAT_ADAPTER 65535
565
566
567 //
568 // IP Header -- RFC 791
569 //
570
571 typedef struct tagIPHDR
572 {
573 BYTE m_cVIHL; // Version and IHL
574 BYTE m_cTOS; // Type Of Service
575 WORD m_wLen; // Total Length
576 WORD m_wId; // Identification
577 WORD m_wFlagOff; // Flags and Fragment Offset
578 BYTE m_cTTL; // Time To Live
579 BYTE m_cProtocol; // Protocol
580 WORD m_wChecksum; // Checksum
581 struct in_addr m_iaSrc; // Internet Address - Source
582 struct in_addr m_iaDst; // Internet Address - Destination
583 } IPHDR;
584
585
586 //
587 // ICMP Header - RFC 792
588 //
589
590 typedef struct tagICMPHDR
591 {
592 BYTE m_cType; // Type
593 BYTE m_cCode; // Code
594 WORD m_wChecksum; // Checksum
595 WORD m_wId; // Identification
596 WORD m_wSeq; // Sequence
597 char m_cData[4]; // Data
598 } ICMPHDR;
599
600
601 //
602 // INADDR_NONE can be undefined on some systems
603 //
604
605 #ifndef INADDR_NONE
606 #define INADDR_NONE (0xFFFFFFFF)
607 #endif
608
609
610 //
611 // Check if IP address is a broadcast
612 //
613
614 #define IsBroadcastAddress(addr, mask) (((addr) & (~(mask))) == (~(mask)))
615
616
617 //
618 // Check if given string is NULL and always return valid pointer
619 //
620
621 #define CHECK_NULL(x) ((x) == NULL ? ((TCHAR *)_T("(null)")) : (x))
622 #define CHECK_NULL_A(x) ((x) == NULL ? ((char *)"(null)") : (x))
623 #define CHECK_NULL_EX(x) ((x) == NULL ? ((TCHAR *)_T("")) : (x))
624
625
626 //
627 // Free memory block if it isn't NULL
628 //
629
630 #define safe_free(x) { if ((x) != NULL) free(x); }
631 #define safe_free_and_null(x) { if ((x) != NULL) { free(x); x = NULL; } }
632
633
634 //
635 // delete object and nullify pointer
636 //
637
638 #define delete_and_null(x) { delete x; x = NULL; }
639
640
641 //
642 // Convert half-byte's value to hex digit and vice versa
643 //
644
645 #define bin2hex(x) ((x) < 10 ? ((x) + _T('0')) : ((x) + (_T('A') - 10)))
646 #ifdef UNICODE
647 #define hex2bin(x) ((((x) >= _T('0')) && ((x) <= _T('9'))) ? ((x) - _T('0')) : \
648 (((towupper(x) >= _T('A')) && (towupper(x) <= _T('F'))) ? (towupper(x) - _T('A') + 10) : 0))
649 #else
650 #define hex2bin(x) ((((x) >= '0') && ((x) <= '9')) ? ((x) - '0') : \
651 (((toupper(x) >= 'A') && (toupper(x) <= 'F')) ? (toupper(x) - 'A' + 10) : 0))
652 #endif
653
654
655 //
656 // Define min() and max() if needed
657 //
658
659 #ifndef min
660 #define min(a, b) ((a) < (b) ? (a) : (b))
661 #endif
662 #ifndef max
663 #define max(a, b) ((a) > (b) ? (a) : (b))
664 #endif
665
666
667 //
668 // Define stricmp() for non-windows
669 //
670
671 #ifndef _WIN32
672 #define stricmp strcasecmp
673 #define strnicmp strncasecmp
674 #define wcsicmp wcscasecmp
675 #define wcsnicmp wcsncasecmp
676 #endif
677
678
679 //
680 // Compare two numbers and return -1, 0, or 1
681 //
682
683 #define COMPARE_NUMBERS(n1,n2) (((n1) < (n2)) ? -1 : (((n1) > (n2)) ? 1 : 0))
684
685
686 //
687 // Increment pointer for given number of bytes
688 //
689
690 #define inc_ptr(ptr, step, ptype) ptr = (ptype *)(((char *)ptr) + step)
691
692
693 //
694 // Validate numerical value
695 //
696
697 #define VALIDATE_VALUE(var,x,y,z) { if ((var < x) || (var > y)) var = z; }
698
699
700 //
701 // DCI (data collection item) data types
702 //
703
704 #define DCI_DT_INT 0
705 #define DCI_DT_UINT 1
706 #define DCI_DT_INT64 2
707 #define DCI_DT_UINT64 3
708 #define DCI_DT_STRING 4
709 #define DCI_DT_FLOAT 5
710 #define DCI_DT_NULL 6
711
712
713 //
714 // Insert parameter as string
715 //
716
717 #ifdef UNICODE
718 #define STRING(x) L#x
719 #else
720 #define STRING(x) #x
721 #endif
722
723
724 //
725 // Memory debug
726 //
727
728 #ifdef NETXMS_MEMORY_DEBUG
729
730 #ifdef __cplusplus
731 extern "C" {
732 #endif
733
734 void *nx_malloc(size_t, char *, int);
735 void *nx_realloc(void *, size_t, char *, int);
736 void nx_free(void *, char *, int);
737
738 void InitMemoryDebugger(void);
739 void PrintMemoryBlocks(void);
740
741 #ifdef __cplusplus
742 }
743 #endif
744
745 #define malloc(x) nx_malloc(x, __FILE__, __LINE__)
746 #define realloc(p, x) nx_realloc(p, x, __FILE__, __LINE__)
747 #define free(p) nx_free(p, __FILE__, __LINE__)
748
749 #endif /* NETXMS_MEMORY_DEBUG */
750
751 #endif /* _nms_common_h_ */