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