e_os.h 15 KB

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