e_os.h 16 KB

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