Rough and ready port of axTLS

Committer:
ashleymills
Date:
Mon May 13 18:15:18 2013 +0000
Revision:
0:5a29fd060ac8
initial commit

Who changed what in which revision?

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