1
0

os_port.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2007, 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 <stdio.h>
  41. #if defined(WIN32)
  42. #define STDCALL __stdcall
  43. #define EXP_FUNC __declspec(dllexport)
  44. #else
  45. #define STDCALL
  46. #define EXP_FUNC
  47. #endif
  48. #if defined(_WIN32_WCE)
  49. #undef WIN32
  50. #define WIN32
  51. #endif
  52. #ifdef WIN32
  53. /* Windows CE stuff */
  54. #if defined(_WIN32_WCE)
  55. #include <basetsd.h>
  56. #define abort() exit(1)
  57. #else
  58. #include <io.h>
  59. #include <process.h>
  60. #include <sys/timeb.h>
  61. #include <fcntl.h>
  62. #endif /* _WIN32_WCE */
  63. #include <winsock.h>
  64. #include <direct.h>
  65. #undef getpid
  66. #undef open
  67. #undef close
  68. #undef sleep
  69. #undef gettimeofday
  70. #undef dup2
  71. #undef unlink
  72. #define SOCKET_READ(A,B,C) recv(A,B,C,0)
  73. #define SOCKET_WRITE(A,B,C) send(A,B,C,0)
  74. #define SOCKET_CLOSE(A) closesocket(A)
  75. #define SOCKET_BLOCK(A) u_long argp = 0; \
  76. ioctlsocket(A, FIONBIO, &argp)
  77. #define srandom(A) srand(A)
  78. #define random() rand()
  79. #define getpid() _getpid()
  80. #define snprintf _snprintf
  81. #define open(A,B) _open(A,B)
  82. #define dup2(A,B) _dup2(A,B)
  83. #define unlink(A) _unlink(A)
  84. #define close(A) _close(A)
  85. #define read(A,B,C) _read(A,B,C)
  86. #define write(A,B,C) _write(A,B,C)
  87. #define sleep(A) Sleep(A*1000)
  88. #define usleep(A) Sleep(A/1000)
  89. #define strdup(A) _strdup(A)
  90. #define chroot(A) _chdir(A)
  91. #define chdir(A) _chdir(A)
  92. #define alloca(A) _alloca(A)
  93. #ifndef lseek
  94. #define lseek(A,B,C) _lseek(A,B,C)
  95. #endif
  96. /* This fix gets around a problem where a win32 application on a cygwin xterm
  97. doesn't display regular output (until a certain buffer limit) - but it works
  98. fine under a normal DOS window. This is a hack to get around the issue -
  99. see http://www.khngai.com/emacs/tty.php */
  100. #define TTY_FLUSH() if (!_isatty(_fileno(stdout))) fflush(stdout);
  101. /*
  102. * automatically build some library dependencies.
  103. */
  104. #pragma comment(lib, "WS2_32.lib")
  105. #pragma comment(lib, "AdvAPI32.lib")
  106. typedef UINT8 uint8_t;
  107. typedef INT8 int8_t;
  108. typedef UINT16 uint16_t;
  109. typedef INT16 int16_t;
  110. typedef UINT32 uint32_t;
  111. typedef INT32 int32_t;
  112. typedef UINT64 uint64_t;
  113. typedef INT64 int64_t;
  114. typedef int socklen_t;
  115. EXP_FUNC void STDCALL gettimeofday(struct timeval* t,void* timezone);
  116. EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2);
  117. EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size);
  118. #else /* Not Win32 */
  119. #ifdef CONFIG_PLATFORM_SOLARIS
  120. #include <inttypes.h>
  121. #else
  122. #include <stdint.h>
  123. #endif /* Not Solaris */
  124. #include <unistd.h>
  125. #include <pwd.h>
  126. #include <netdb.h>
  127. #include <dirent.h>
  128. #include <fcntl.h>
  129. #include <sys/stat.h>
  130. #include <sys/time.h>
  131. #include <sys/socket.h>
  132. #include <sys/wait.h>
  133. #include <netinet/in.h>
  134. #include <arpa/inet.h>
  135. #define SOCKET_READ(A,B,C) read(A,B,C)
  136. #define SOCKET_WRITE(A,B,C) write(A,B,C)
  137. #define SOCKET_CLOSE(A) close(A)
  138. #define SOCKET_BLOCK(A) int fd = fcntl(A, F_GETFL, NULL); \
  139. fcntl(A, F_SETFL, fd & ~O_NONBLOCK)
  140. #define TTY_FLUSH()
  141. #endif /* Not Win32 */
  142. /* some functions to mutate the way these work */
  143. #define malloc(A) ax_malloc(A)
  144. #ifndef realloc
  145. #define realloc(A,B) ax_realloc(A,B)
  146. #endif
  147. #define calloc(A,B) ax_calloc(A,B)
  148. EXP_FUNC void * STDCALL ax_malloc(size_t s);
  149. EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s);
  150. EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s);
  151. EXP_FUNC int STDCALL ax_open(const char *pathname, int flags);
  152. #ifdef CONFIG_PLATFORM_LINUX
  153. void exit_now(const char *format, ...) __attribute((noreturn));
  154. #else
  155. void exit_now(const char *format, ...);
  156. #endif
  157. /* Mutexing definitions */
  158. #if defined(CONFIG_SSL_CTX_MUTEXING)
  159. #if defined(WIN32)
  160. #define SSL_CTX_MUTEX_TYPE HANDLE
  161. #define SSL_CTX_MUTEX_INIT(A) A=CreateMutex(0, FALSE, 0)
  162. #define SSL_CTX_MUTEX_DESTROY(A) CloseHandle(A)
  163. #define SSL_CTX_LOCK(A) WaitForSingleObject(A, INFINITE)
  164. #define SSL_CTX_UNLOCK(A) ReleaseMutex(A)
  165. #else
  166. #include <pthread.h>
  167. #define SSL_CTX_MUTEX_TYPE pthread_mutex_t
  168. #define SSL_CTX_MUTEX_INIT(A) pthread_mutex_init(&A, NULL)
  169. #define SSL_CTX_MUTEX_DESTROY(A) pthread_mutex_destroy(&A)
  170. #define SSL_CTX_LOCK(A) pthread_mutex_lock(&A)
  171. #define SSL_CTX_UNLOCK(A) pthread_mutex_unlock(&A)
  172. #endif
  173. #else /* no mutexing */
  174. #define SSL_CTX_MUTEX_INIT(A)
  175. #define SSL_CTX_MUTEX_DESTROY(A)
  176. #define SSL_CTX_LOCK(A)
  177. #define SSL_CTX_UNLOCK(A)
  178. #endif
  179. #ifdef __cplusplus
  180. }
  181. #endif
  182. #endif