make update
[openssl.git] / e_os.h
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef HEADER_E_OS_H
11 # define HEADER_E_OS_H
12
13 # include <openssl/opensslconf.h>
14
15 # include <openssl/e_os2.h>
16 /*
17  * <openssl/e_os2.h> contains what we can justify to make visible to the
18  * outside; this file e_os.h is not part of the exported interface.
19  */
20
21 #ifdef  __cplusplus
22 extern "C" {
23 #endif
24
25 /* Used to checking reference counts, most while doing perl5 stuff :-) */
26 # if defined(OPENSSL_NO_STDIO)
27 #  if defined(REF_PRINT)
28 #   error "REF_PRINT requires stdio"
29 #  endif
30 # endif
31
32 /*
33  * Format specifier for printing size_t. Original conundrum was to
34  * get it working with -Wformat [-Werror], which can be considered
35  * overzelaous, especially in multi-platform context, but it's
36  * conscious choice...
37  */
38 # if defined(_WIN64)
39 #  define OSSLzu  "I64u"    /* One would expect _WIN{64|32} cases after
40                              * __STDC_VERSION__, but there are corner
41                              * cases of MinGW compilers that link with
42                              * non-compliant MSVCRT.DLL... */
43 # elif defined(_WIN32)
44 #  define OSSLzu  "u"
45 # elif defined(__VMS)
46 #  define OSSLzu  "u"       /* VMS suffers from similar problem as MinGW,
47                              * i.e. C RTL falling behind compiler. Recall
48                              * that sizeof(size_t)==4 even in LP64 case. */
49 # elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
50 #  define OSSLzu  "zu"
51 # elif defined(__SIZEOF_SIZE_T__) && __SIZEOF_SIZE_T__==4
52 #  define OSSLzu  "u"       /* 'lu' should have worked, but when generating
53                              * 32-bit code gcc still complains :-( */
54 # else
55 #  define OSSLzu  "lu"      /* To see that is works recall what does L
56                              * stand for in ILP32 and LP64 */
57 # endif
58
59 # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
60 #  define REF_ASSERT_ISNT(test) \
61     (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
62 # else
63 #  define REF_ASSERT_ISNT(i)
64 # endif
65 # ifdef REF_PRINT
66 #  define REF_PRINT_COUNT(a, b) \
67         fprintf(stderr, "%p:%4d:%s\n", b, b->references, a)
68 # else
69 #  define REF_PRINT_COUNT(a, b)
70 # endif
71
72 # define osslargused(x)      (void)x
73 # define OPENSSL_CONF        "openssl.cnf"
74
75 # ifndef DEVRANDOM
76 /*
77  * set this to a comma-separated list of 'random' device files to try out. My
78  * default, we will try to read at least one of these files
79  */
80 #  define DEVRANDOM "/dev/urandom","/dev/random","/dev/srandom"
81 # endif
82 # if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
83 /*
84  * set this to a comma-separated list of 'egd' sockets to try out. These
85  * sockets will be tried in the order listed in case accessing the device
86  * files listed in DEVRANDOM did not return enough entropy.
87  */
88 #  define DEVRANDOM_EGD "/var/run/egd-pool","/dev/egd-pool","/etc/egd-pool","/etc/entropy"
89 # endif
90
91 # if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
92 #  define NO_SYS_PARAM_H
93 #  define NO_CHMOD
94 #  define NO_SYSLOG
95 # endif
96
97 /********************************************************************
98  The Microsoft section
99  ********************************************************************/
100 # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
101 #  define WIN32
102 # endif
103 # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
104 #  define WINDOWS
105 # endif
106 # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
107 #  define MSDOS
108 # endif
109
110 # if (defined(MSDOS) || defined(OPENSSL_SYS_UEFI)) && !defined(GETPID_IS_MEANINGLESS)
111 #  define GETPID_IS_MEANINGLESS
112 # endif
113
114 # ifdef WIN32
115 #  define NO_SYS_UN_H
116 #  define get_last_sys_error()    GetLastError()
117 #  define clear_sys_error()       SetLastError(0)
118 #  if !defined(WINNT)
119 #   define WIN_CONSOLE_BUG
120 #  endif
121 # else
122 #  define get_last_sys_error()    errno
123 #  define clear_sys_error()       errno=0
124 # endif
125
126 # if defined(WINDOWS)
127 #  define get_last_socket_error() WSAGetLastError()
128 #  define clear_socket_error()    WSASetLastError(0)
129 #  define readsocket(s,b,n)       recv((s),(b),(n),0)
130 #  define writesocket(s,b,n)      send((s),(b),(n),0)
131 # elif defined(__DJGPP__)
132 #  define WATT32
133 #  define WATT32_NO_OLDIES
134 #  define get_last_socket_error() errno
135 #  define clear_socket_error()    errno=0
136 #  define closesocket(s)          close_s(s)
137 #  define readsocket(s,b,n)       read_s(s,b,n)
138 #  define writesocket(s,b,n)      send(s,b,n,0)
139 # elif defined(OPENSSL_SYS_VMS)
140 #  define get_last_socket_error() errno
141 #  define clear_socket_error()    errno=0
142 #  define ioctlsocket(a,b,c)      ioctl(a,b,c)
143 #  define closesocket(s)          close(s)
144 #  define readsocket(s,b,n)       recv((s),(b),(n),0)
145 #  define writesocket(s,b,n)      send((s),(b),(n),0)
146 # elif defined(OPENSSL_SYS_VXWORKS)
147 #  define get_last_socket_error() errno
148 #  define clear_socket_error()    errno=0
149 #  define ioctlsocket(a,b,c)          ioctl((a),(b),(int)(c))
150 #  define closesocket(s)              close(s)
151 #  define readsocket(s,b,n)           read((s),(b),(n))
152 #  define writesocket(s,b,n)          write((s),(char *)(b),(n))
153 # else
154 #  define get_last_socket_error() errno
155 #  define clear_socket_error()    errno=0
156 #  define ioctlsocket(a,b,c)      ioctl(a,b,c)
157 #  define closesocket(s)          close(s)
158 #  define readsocket(s,b,n)       read((s),(b),(n))
159 #  define writesocket(s,b,n)      write((s),(b),(n))
160 # endif
161
162 # if (defined(WINDOWS) || defined(MSDOS))
163
164 #  ifdef __DJGPP__
165 #   include <unistd.h>
166 #   include <sys/stat.h>
167 #   include <sys/socket.h>
168 #   include <sys/un.h>
169 #   include <tcp.h>
170 #   include <netdb.h>
171 #   define _setmode setmode
172 #   define _O_TEXT O_TEXT
173 #   define _O_BINARY O_BINARY
174 #   define HAS_LFN_SUPPORT(name)  (pathconf((name), _PC_NAME_MAX) > 12)
175 #   undef DEVRANDOM_EGD  /*  Neither MS-DOS nor FreeDOS provide 'egd' sockets.  */
176 #   undef DEVRANDOM
177 #   define DEVRANDOM "/dev/urandom\x24"
178 #  endif                        /* __DJGPP__ */
179
180 #  ifndef S_IFDIR
181 #   define S_IFDIR     _S_IFDIR
182 #  endif
183
184 #  ifndef S_IFMT
185 #   define S_IFMT      _S_IFMT
186 #  endif
187
188 #  if !defined(WINNT) && !defined(__DJGPP__)
189 #   define NO_SYSLOG
190 #  endif
191 #  define NO_DIRENT
192
193 #  ifdef WINDOWS
194 #   if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
195        /*
196         * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
197         * Most notably we ought to check for availability of each specific
198         * routine that was introduced after denoted _WIN32_WINNT with
199         * GetProcAddress(). Normally newer functions are masked with higher
200         * _WIN32_WINNT in SDK headers. So that if you wish to use them in
201         * some module, you'd need to override _WIN32_WINNT definition in
202         * the target module in order to "reach for" prototypes, but replace
203         * calls to new functions with indirect calls. Alternatively it
204         * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
205         * and check for current OS version instead.
206         */
207 #    define _WIN32_WINNT 0x0501
208 #   endif
209 #   if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
210        /*
211         * Just like defining _WIN32_WINNT including winsock2.h implies
212         * certain "discipline" for maintaining [broad] binary compatibility.
213         * As long as structures are invariant among Winsock versions,
214         * it's sufficient to check for specific Winsock2 API availability
215         * at run-time [DSO_global_lookup is recommended]...
216         */
217 #    include <winsock2.h>
218 #    include <ws2tcpip.h>
219        /* yes, they have to be #included prior to <windows.h> */
220 #   endif
221 #   include <windows.h>
222 #   include <stdio.h>
223 #   include <stddef.h>
224 #   include <errno.h>
225 #   if defined(_WIN32_WCE) && !defined(EACCES)
226 #    define EACCES   13
227 #   endif
228 #   include <string.h>
229 #   ifdef _WIN64
230 #    define strlen(s) _strlen31(s)
231 /* cut strings to 2GB */
232 static __inline unsigned int _strlen31(const char *str)
233 {
234     unsigned int len = 0;
235     while (*str && len < 0x80000000U)
236         str++, len++;
237     return len & 0x7FFFFFFF;
238 }
239 #   endif
240 #   include <malloc.h>
241 #   if defined(_MSC_VER) && _MSC_VER<=1200 && defined(_MT) && defined(isspace)
242        /* compensate for bug in VC6 ctype.h */
243 #    undef isspace
244 #    undef isdigit
245 #    undef isalnum
246 #    undef isupper
247 #    undef isxdigit
248 #   endif
249 #   if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
250 #    if _MSC_VER>=1300 && _MSC_VER<1600
251 #     undef stdin
252 #     undef stdout
253 #     undef stderr
254 FILE *__iob_func();
255 #     define stdin  (&__iob_func()[0])
256 #     define stdout (&__iob_func()[1])
257 #     define stderr (&__iob_func()[2])
258 #    elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
259 #     undef stdin
260 #     undef stdout
261 #     undef stderr
262          /*
263           * pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
264           * or in other words with /MD. Declaring implicit import, i.e. with
265           * _imp_ prefix, works correctly with all compiler options, but
266           * without /MD results in LINK warning LNK4049: 'locally defined
267           * symbol "__iob" imported'.
268           */
269 extern FILE *_imp___iob;
270 #     define stdin  (&_imp___iob[0])
271 #     define stdout (&_imp___iob[1])
272 #     define stderr (&_imp___iob[2])
273 #    endif
274 #   endif
275 #  endif
276 #  include <io.h>
277 #  include <fcntl.h>
278
279 #  ifdef OPENSSL_SYS_WINCE
280 #   define OPENSSL_NO_POSIX_IO
281 #  endif
282
283 #  define EXIT(n) exit(n)
284 #  define LIST_SEPARATOR_CHAR ';'
285 #  ifndef X_OK
286 #   define X_OK        0
287 #  endif
288 #  ifndef W_OK
289 #   define W_OK        2
290 #  endif
291 #  ifndef R_OK
292 #   define R_OK        4
293 #  endif
294 #  ifdef OPENSSL_SYS_WINCE
295 #   define DEFAULT_HOME  ""
296 #  else
297 #   define DEFAULT_HOME  "C:"
298 #  endif
299
300 /* Avoid Visual Studio 13 GetVersion deprecated problems */
301 #  if defined(_MSC_VER) && _MSC_VER>=1800
302 #   define check_winnt() (1)
303 #   define check_win_minplat(x) (1)
304 #  else
305 #   define check_winnt() (GetVersion() < 0x80000000)
306 #   define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
307 #  endif
308
309 # else                          /* The non-microsoft world */
310
311 #  ifdef OPENSSL_SYS_VMS
312 #   define VMS 1
313   /*
314    * some programs don't include stdlib, so exit() and others give implicit
315    * function warnings
316    */
317 #   include <stdlib.h>
318 #   if defined(__DECC)
319 #    include <unistd.h>
320 #   else
321 #    include <unixlib.h>
322 #   endif
323 #   define LIST_SEPARATOR_CHAR ','
324   /* We don't have any well-defined random devices on VMS, yet... */
325 #   undef DEVRANDOM
326   /*-
327      We need to do this since VMS has the following coding on status codes:
328
329      Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
330                The important thing to know is that odd numbers are considered
331                good, while even ones are considered errors.
332      Bits 3-15: actual status number
333      Bits 16-27: facility number.  0 is considered "unknown"
334      Bits 28-31: control bits.  If bit 28 is set, the shell won't try to
335                  output the message (which, for random codes, just looks ugly)
336
337      So, what we do here is to change 0 to 1 to get the default success status,
338      and everything else is shifted up to fit into the status number field, and
339      the status is tagged as an error, which is what is wanted here.
340
341      Finally, we add the VMS C facility code 0x35a000, because there are some
342      programs, such as Perl, that will reinterpret the code back to something
343      POSIXly.  'man perlvms' explains it further.
344
345      NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
346      codes (status type = 1).  I couldn't disagree more.  Fortunately, the
347      status type doesn't seem to bother Perl.
348      -- Richard Levitte
349   */
350 #   define EXIT(n)  exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
351
352 #   define NO_SYS_PARAM_H
353 #   define NO_SYS_UN_H
354
355 #   define DEFAULT_HOME "SYS$LOGIN:"
356
357 #  else
358      /* !defined VMS */
359 #   ifdef OPENSSL_UNISTD
360 #    include OPENSSL_UNISTD
361 #   else
362 #    include <unistd.h>
363 #   endif
364 #   ifndef NO_SYS_TYPES_H
365 #    include <sys/types.h>
366 #   endif
367 #   ifdef OPENSSL_SYS_WIN32_CYGWIN
368 #    include <io.h>
369 #    include <fcntl.h>
370 #   endif
371
372 #   define LIST_SEPARATOR_CHAR ':'
373 #   define EXIT(n)             exit(n)
374 #  endif
375
376 # endif
377
378 /*************/
379
380 # ifdef USE_SOCKETS
381 #  ifdef OPENSSL_NO_SOCK
382 #  elif defined(WINDOWS) || defined(MSDOS)
383       /* windows world */
384 #   if !defined(__DJGPP__)
385 #    if defined(_WIN32_WCE) && _WIN32_WCE<410
386 #     define getservbyname _masked_declaration_getservbyname
387 #    endif
388 #    if !defined(IPPROTO_IP)
389          /* winsock[2].h was included already? */
390 #     include <winsock.h>
391 #    endif
392 #    ifdef getservbyname
393 #     undef getservbyname
394          /* this is used to be wcecompat/include/winsock_extras.h */
395 struct servent *PASCAL getservbyname(const char *, const char *);
396 #    endif
397
398 #    ifdef _WIN64
399 /*
400  * Even though sizeof(SOCKET) is 8, it's safe to cast it to int, because
401  * the value constitutes an index in per-process table of limited size
402  * and not a real pointer. And we also depend on fact that all processors
403  * Windows run on happen to be two's-complement, which allows to
404  * interchange INVALID_SOCKET and -1.
405  */
406 #     define socket(d,t,p)   ((int)socket(d,t,p))
407 #     define accept(s,f,l)   ((int)accept(s,f,l))
408 #    endif
409 #   else
410 #   endif
411
412 #  else
413
414 #   ifndef NO_SYS_PARAM_H
415 #    include <sys/param.h>
416 #   endif
417 #   ifdef OPENSSL_SYS_VXWORKS
418 #    include <time.h>
419 #   endif
420
421 #   include <netdb.h>
422 #   if defined(OPENSSL_SYS_VMS_NODECC)
423 #    include <socket.h>
424 #    include <in.h>
425 #    include <inet.h>
426 #   else
427 #    include <sys/socket.h>
428 #    ifndef NO_SYS_UN_H
429 #     ifdef OPENSSL_SYS_VXWORKS
430 #      include <streams/un.h>
431 #     else
432 #      include <sys/un.h>
433 #     endif
434 #     ifndef UNIX_PATH_MAX
435 #      define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)NULL)->sun_path)
436 #     endif
437 #    endif
438 #    ifdef FILIO_H
439 #     include <sys/filio.h> /* FIONBIO in some SVR4, e.g. unixware, solaris */
440 #    endif
441 #    include <netinet/in.h>
442 #    include <arpa/inet.h>
443 #    include <netinet/tcp.h>
444 #   endif
445
446 #   ifdef OPENSSL_SYS_AIX
447 #    include <sys/select.h>
448 #   endif
449
450 #   ifdef __QNX__
451 #    include <sys/select.h>
452 #   endif
453
454 #   ifndef VMS
455 #    include <sys/ioctl.h>
456 #   else
457         /* ioctl is only in VMS > 7.0 and when socketshr is not used */
458 #    if !defined(TCPIP_TYPE_SOCKETSHR) && defined(__VMS_VER) && (__VMS_VER > 70000000)
459 #     include <sys/ioctl.h>
460 #    endif
461 #   endif
462
463 #   ifdef VMS
464 #    include <unixio.h>
465 #    if defined(TCPIP_TYPE_SOCKETSHR)
466 #     include <socketshr.h>
467 #    endif
468 #   endif
469
470 #   ifndef INVALID_SOCKET
471 #    define INVALID_SOCKET      (-1)
472 #   endif                       /* INVALID_SOCKET */
473 #  endif
474
475 /*
476  * Some IPv6 implementations are broken, disable them in known bad versions.
477  */
478 #  if !defined(OPENSSL_USE_IPV6)
479 #   if defined(AF_INET6) && !defined(NETWARE_CLIB)
480 #    define OPENSSL_USE_IPV6 1
481 #   else
482 #    define OPENSSL_USE_IPV6 0
483 #   endif
484 #  endif
485
486 # endif
487
488 # ifndef OPENSSL_EXIT
489 #  if defined(MONOLITH) && !defined(OPENSSL_C)
490 #   define OPENSSL_EXIT(n) return(n)
491 #  else
492 #   define OPENSSL_EXIT(n) do { EXIT(n); return(n); } while(0)
493 #  endif
494 # endif
495
496 /***********************************************/
497
498 # if defined(OPENSSL_SYS_WINDOWS)
499 #  define strcasecmp _stricmp
500 #  define strncasecmp _strnicmp
501 #  if (_MSC_VER >= 1310)
502 #   define open _open
503 #   define fdopen _fdopen
504 #   define close _close
505 #   ifndef strdup
506 #    define strdup _strdup
507 #   endif
508 #   define unlink _unlink
509 #  endif
510 # else
511 #  include <strings.h>
512 # endif
513
514 /* vxworks */
515 # if defined(OPENSSL_SYS_VXWORKS)
516 #  include <ioLib.h>
517 #  include <tickLib.h>
518 #  include <sysLib.h>
519
520 #  define TTY_STRUCT int
521
522 #  define sleep(a) taskDelay((a) * sysClkRateGet())
523
524 #  include <vxWorks.h>
525 #  include <sockLib.h>
526 #  include <taskLib.h>
527
528 #  define getpid taskIdSelf
529
530 /*
531  * NOTE: these are implemented by helpers in database app! if the database is
532  * not linked, we need to implement them elswhere
533  */
534 struct hostent *gethostbyname(const char *name);
535 struct hostent *gethostbyaddr(const char *addr, int length, int type);
536 struct servent *getservbyname(const char *name, const char *proto);
537
538 # endif
539 /* end vxworks */
540
541 #define OSSL_NELEM(x)    (sizeof(x)/sizeof(x[0]))
542
543 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
544 # define CRYPTO_memcmp memcmp
545 #endif
546
547 #ifdef  __cplusplus
548 }
549 #endif
550
551 #endif