e_os.h 17 KB

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