2 * Copyright (c) 2007, Cameron Rich
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
34 * Some stuff to minimise the differences between windows and linux/unix
37 #ifndef HEADER_OS_PORT_H
38 #define HEADER_OS_PORT_H
47 #define STDCALL __stdcall
48 #define EXP_FUNC __declspec(dllexport)
54 #if defined(_WIN32_WCE)
61 /* Windows CE stuff */
62 #if defined(_WIN32_WCE)
64 #define abort() exit(1)
68 #include <sys/timeb.h>
70 #endif /* _WIN32_WCE */
82 #define SOCKET_READ(A,B,C) recv(A,B,C,0)
83 #define SOCKET_WRITE(A,B,C) send(A,B,C,0)
84 #define SOCKET_CLOSE(A) closesocket(A)
85 #define SOCKET_BLOCK(A) u_long argp = 0; \
86 ioctlsocket(A, FIONBIO, &argp)
87 #define srandom(A) srand(A)
88 #define random() rand()
89 #define getpid() _getpid()
90 #define snprintf _snprintf
91 #define open(A,B) _open(A,B)
92 #define dup2(A,B) _dup2(A,B)
93 #define unlink(A) _unlink(A)
94 #define close(A) _close(A)
95 #define read(A,B,C) _read(A,B,C)
96 #define write(A,B,C) _write(A,B,C)
97 #define sleep(A) Sleep(A*1000)
98 #define usleep(A) Sleep(A/1000)
99 #define strdup(A) _strdup(A)
100 #define chroot(A) _chdir(A)
101 #define chdir(A) _chdir(A)
102 #define alloca(A) _alloca(A)
104 #define lseek(A,B,C) _lseek(A,B,C)
107 /* This fix gets around a problem where a win32 application on a cygwin xterm
108 doesn't display regular output (until a certain buffer limit) - but it works
109 fine under a normal DOS window. This is a hack to get around the issue -
110 see http://www.khngai.com/emacs/tty.php */
111 #define TTY_FLUSH() if (!_isatty(_fileno(stdout))) fflush(stdout);
114 * automatically build some library dependencies.
116 #pragma comment(lib, "WS2_32.lib")
117 #pragma comment(lib, "AdvAPI32.lib")
119 typedef UINT8 uint8_t;
121 typedef UINT16 uint16_t;
122 typedef INT16 int16_t;
123 typedef UINT32 uint32_t;
124 typedef INT32 int32_t;
125 typedef UINT64 uint64_t;
126 typedef INT64 int64_t;
127 typedef int socklen_t;
129 EXP_FUNC void STDCALL gettimeofday(struct timeval* t,void* timezone);
130 EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2);
131 EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size);
133 #else /* Not Win32 */
135 #ifdef CONFIG_PLATFORM_SOLARIS
136 #include <inttypes.h>
139 #endif /* Not Solaris */
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>
153 #define SOCKET_READ(A,B,C) read(A,B,C)
154 #define SOCKET_WRITE(A,B,C) write(A,B,C)
155 #define SOCKET_CLOSE(A) close(A)
156 #define SOCKET_BLOCK(A) int fd = fcntl(A, F_GETFL, NULL); \
157 fcntl(A, F_SETFL, fd & ~O_NONBLOCK)
160 #endif /* Not Win32 */
162 /* some functions to mutate the way these work */
163 #define malloc(A) ax_malloc(A)
165 #define realloc(A,B) ax_realloc(A,B)
167 #define calloc(A,B) ax_calloc(A,B)
169 EXP_FUNC void * STDCALL ax_malloc(size_t s);
170 EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s);
171 EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s);
172 EXP_FUNC int STDCALL ax_open(const char *pathname, int flags);
174 #ifdef CONFIG_PLATFORM_LINUX
175 void exit_now(const char *format, ...) __attribute((noreturn));
177 void exit_now(const char *format, ...);
180 /* Mutexing definitions */
181 #if defined(CONFIG_SSL_CTX_MUTEXING)
183 #define SSL_CTX_MUTEX_TYPE HANDLE
184 #define SSL_CTX_MUTEX_INIT(A) A=CreateMutex(0, FALSE, 0)
185 #define SSL_CTX_MUTEX_DESTROY(A) CloseHandle(A)
186 #define SSL_CTX_LOCK(A) WaitForSingleObject(A, INFINITE)
187 #define SSL_CTX_UNLOCK(A) ReleaseMutex(A)
190 #define SSL_CTX_MUTEX_TYPE pthread_mutex_t
191 #define SSL_CTX_MUTEX_INIT(A) pthread_mutex_init(&A, NULL)
192 #define SSL_CTX_MUTEX_DESTROY(A) pthread_mutex_destroy(&A)
193 #define SSL_CTX_LOCK(A) pthread_mutex_lock(&A)
194 #define SSL_CTX_UNLOCK(A) pthread_mutex_unlock(&A)
196 #else /* no mutexing */
197 #define SSL_CTX_MUTEX_INIT(A)
198 #define SSL_CTX_MUTEX_DESTROY(A)
199 #define SSL_CTX_LOCK(A)
200 #define SSL_CTX_UNLOCK(A)