os_port.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2007-2016, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @file os_port.h
  32. *
  33. * Some stuff to minimise the differences between windows and linux/unix
  34. */
  35. #ifndef HEADER_OS_PORT_H
  36. #define HEADER_OS_PORT_H
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #include "os_int.h"
  41. #include "config.h"
  42. #include <stdio.h>
  43. #if defined(WIN32)
  44. #ifdef SSL_STATIC_LIBRARY
  45. #define EXP_FUNC
  46. #define LIB_CALLTYPE
  47. #define STDCALL
  48. #else
  49. #define STDCALL __stdcall
  50. #define EXP_FUNC __declspec(dllexport)
  51. #endif
  52. #else
  53. #define STDCALL
  54. #define EXP_FUNC
  55. #endif
  56. #if defined(_WIN32_WCE)
  57. #undef WIN32
  58. #define WIN32
  59. #endif
  60. #ifdef WIN32
  61. /* Windows CE stuff */
  62. #if defined(_WIN32_WCE)
  63. #include <basetsd.h>
  64. #define abort() exit(1)
  65. #include <time.h>
  66. #include <io.h>
  67. #else
  68. #include <io.h>
  69. #include <process.h>
  70. #include <sys/timeb.h>
  71. #include <fcntl.h>
  72. #endif /* _WIN32_WCE */
  73. #include <winsock.h>
  74. #if !defined(_WIN32_WCE)
  75. #include <direct.h>
  76. #endif
  77. #ifdef __MINGW32__
  78. #include <stdint.h>
  79. #include <malloc.h>
  80. #endif
  81. #undef getpid
  82. #undef open
  83. #undef close
  84. #undef sleep
  85. #undef gettimeofday
  86. #undef dup2
  87. #undef unlink
  88. #define SOCKET_READ(A,B,C) recv(A,B,C,0)
  89. #define SOCKET_WRITE(A,B,C) send(A,B,C,0)
  90. #define SOCKET_CLOSE(A) closesocket(A)
  91. #define SOCKET_BLOCK(A) u_long argp = 0; \
  92. ioctlsocket(A, FIONBIO, &argp)
  93. #define srandom(A) srand(A)
  94. #define random() rand()
  95. #define getpid() _getpid()
  96. #define snprintf _snprintf
  97. #define open(A,B) _open(A,B)
  98. #define dup2(A,B) _dup2(A,B)
  99. #define unlink(A) _unlink(A)
  100. #define close(A) _close(A)
  101. #define read(A,B,C) _read(A,B,C)
  102. #define write(A,B,C) _write(A,B,C)
  103. #define sleep(A) Sleep(A*1000)
  104. #define usleep(A) Sleep(A/1000)
  105. #define strdup(A) _strdup(A)
  106. #define chroot(A) _chdir(A)
  107. #define chdir(A) _chdir(A)
  108. #ifndef __MINGW32__
  109. #define alloca(A) _alloca(A)
  110. #endif
  111. #ifndef lseek
  112. #define lseek(A,B,C) _lseek(A,B,C)
  113. #endif
  114. /* This fix gets around a problem where a win32 application on a cygwin xterm
  115. doesn't display regular output (until a certain buffer limit) - but it works
  116. fine under a normal DOS window. This is a hack to get around the issue -
  117. see http://www.khngai.com/emacs/tty.php */
  118. #if defined(_WIN32_WCE)
  119. #define TTY_FLUSH()
  120. #else
  121. #define TTY_FLUSH() if (!_isatty(_fileno(stdout))) fflush(stdout);
  122. #endif
  123. /*
  124. * automatically build some library dependencies.
  125. */
  126. #ifndef __MINGW32__
  127. #pragma comment(lib, "WS2_32.lib")
  128. #pragma comment(lib, "AdvAPI32.lib")
  129. #else
  130. #define SHUT_WR 1
  131. typedef int socklen_t;
  132. #endif
  133. EXP_FUNC void STDCALL gettimeofday(struct timeval* t,void* timezone);
  134. #ifndef __MINGW32__
  135. EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2);
  136. #endif
  137. EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size);
  138. #else /* Not Win32 */
  139. #include <alloca.h>
  140. #include <unistd.h>
  141. #include <pwd.h>
  142. #include <netdb.h>
  143. #include <dirent.h>
  144. #include <fcntl.h>
  145. #include <errno.h>
  146. #include <sys/stat.h>
  147. #include <sys/time.h>
  148. #include <sys/socket.h>
  149. #include <sys/wait.h>
  150. #include <netinet/in.h>
  151. #include <arpa/inet.h>
  152. #define SOCKET_READ(A,B,C) read(A,B,C)
  153. #define SOCKET_WRITE(A,B,C) write(A,B,C)
  154. #define SOCKET_CLOSE(A) if (A >= 0) close(A)
  155. #define SOCKET_BLOCK(A) int fd = fcntl(A, F_GETFL, NULL); \
  156. fcntl(A, F_SETFL, fd & ~O_NONBLOCK)
  157. #define TTY_FLUSH()
  158. #ifndef be64toh
  159. #define be64toh(x) __be64_to_cpu(x)
  160. #endif
  161. #endif /* Not Win32 */
  162. /* some functions to mutate the way these work */
  163. #define malloc(A) ax_malloc(A)
  164. #ifndef realloc
  165. #define realloc(A,B) ax_realloc(A,B)
  166. #endif
  167. #define calloc(A,B) ax_calloc(A,B)
  168. EXP_FUNC void * STDCALL ax_malloc(size_t s);
  169. EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s);
  170. EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s);
  171. EXP_FUNC int STDCALL ax_open(const char *pathname, int flags);
  172. #ifdef CONFIG_PLATFORM_LINUX
  173. void exit_now(const char *format, ...) __attribute((noreturn));
  174. #else
  175. void exit_now(const char *format, ...);
  176. #endif
  177. /* Mutexing definitions */
  178. #if defined(CONFIG_SSL_CTX_MUTEXING)
  179. #if defined(WIN32)
  180. #define SSL_CTX_MUTEX_TYPE HANDLE
  181. #define SSL_CTX_MUTEX_INIT(A) A=CreateMutex(0, FALSE, 0)
  182. #define SSL_CTX_MUTEX_DESTROY(A) CloseHandle(A)
  183. #define SSL_CTX_LOCK(A) WaitForSingleObject(A, INFINITE)
  184. #define SSL_CTX_UNLOCK(A) ReleaseMutex(A)
  185. #else
  186. #include <pthread.h>
  187. #define SSL_CTX_MUTEX_TYPE pthread_mutex_t
  188. #define SSL_CTX_MUTEX_INIT(A) pthread_mutex_init(&A, NULL)
  189. #define SSL_CTX_MUTEX_DESTROY(A) pthread_mutex_destroy(&A)
  190. #define SSL_CTX_LOCK(A) pthread_mutex_lock(&A)
  191. #define SSL_CTX_UNLOCK(A) pthread_mutex_unlock(&A)
  192. #endif
  193. #else /* no mutexing */
  194. #define SSL_CTX_MUTEX_INIT(A)
  195. #define SSL_CTX_MUTEX_DESTROY(A)
  196. #define SSL_CTX_LOCK(A)
  197. #define SSL_CTX_UNLOCK(A)
  198. #endif
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif