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.c
ashleymills 0:5a29fd060ac8 33 *
ashleymills 0:5a29fd060ac8 34 * OS specific functions.
ashleymills 0:5a29fd060ac8 35 */
ashleymills 0:5a29fd060ac8 36 #include <time.h>
ashleymills 0:5a29fd060ac8 37 #include <stdlib.h>
ashleymills 0:5a29fd060ac8 38 #include <errno.h>
ashleymills 0:5a29fd060ac8 39 #include <stdarg.h>
ashleymills 0:5a29fd060ac8 40 #include "os_port.h"
ashleymills 0:5a29fd060ac8 41 #include <stdio.h>
ashleymills 0:5a29fd060ac8 42 #include "sockets.h"
ashleymills 0:5a29fd060ac8 43
ashleymills 0:5a29fd060ac8 44 #ifdef MBED
ashleymills 0:5a29fd060ac8 45 /**
ashleymills 0:5a29fd060ac8 46 * gettimeofday() not in mbed
ashleymills 0:5a29fd060ac8 47 */
ashleymills 0:5a29fd060ac8 48 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
ashleymills 0:5a29fd060ac8 49 {
ashleymills 0:5a29fd060ac8 50 t->tv_sec = time(NULL);
ashleymills 0:5a29fd060ac8 51 t->tv_usec = 0; /* 1sec precision only */
ashleymills 0:5a29fd060ac8 52
ashleymills 0:5a29fd060ac8 53 }
ashleymills 0:5a29fd060ac8 54
ashleymills 0:5a29fd060ac8 55 #endif
ashleymills 0:5a29fd060ac8 56
ashleymills 0:5a29fd060ac8 57 #ifdef WIN32
ashleymills 0:5a29fd060ac8 58 /**
ashleymills 0:5a29fd060ac8 59 * gettimeofday() not in Win32
ashleymills 0:5a29fd060ac8 60 */
ashleymills 0:5a29fd060ac8 61 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
ashleymills 0:5a29fd060ac8 62 {
ashleymills 0:5a29fd060ac8 63 #if defined(_WIN32_WCE)
ashleymills 0:5a29fd060ac8 64 t->tv_sec = time(NULL);
ashleymills 0:5a29fd060ac8 65 t->tv_usec = 0; /* 1sec precision only */
ashleymills 0:5a29fd060ac8 66 #else
ashleymills 0:5a29fd060ac8 67 struct _timeb timebuffer;
ashleymills 0:5a29fd060ac8 68 _ftime(&timebuffer);
ashleymills 0:5a29fd060ac8 69 t->tv_sec = (long)timebuffer.time;
ashleymills 0:5a29fd060ac8 70 t->tv_usec = 1000 * timebuffer.millitm; /* 1ms precision */
ashleymills 0:5a29fd060ac8 71 #endif
ashleymills 0:5a29fd060ac8 72 }
ashleymills 0:5a29fd060ac8 73
ashleymills 0:5a29fd060ac8 74
ashleymills 0:5a29fd060ac8 75 /**
ashleymills 0:5a29fd060ac8 76 * strcasecmp() not in Win32
ashleymills 0:5a29fd060ac8 77 */
ashleymills 0:5a29fd060ac8 78 EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2)
ashleymills 0:5a29fd060ac8 79 {
ashleymills 0:5a29fd060ac8 80 while (tolower(*s1) == tolower(*s2++))
ashleymills 0:5a29fd060ac8 81 {
ashleymills 0:5a29fd060ac8 82 if (*s1++ == '\0')
ashleymills 0:5a29fd060ac8 83 {
ashleymills 0:5a29fd060ac8 84 return 0;
ashleymills 0:5a29fd060ac8 85 }
ashleymills 0:5a29fd060ac8 86 }
ashleymills 0:5a29fd060ac8 87
ashleymills 0:5a29fd060ac8 88 return *(unsigned char *)s1 - *(unsigned char *)(s2 - 1);
ashleymills 0:5a29fd060ac8 89 }
ashleymills 0:5a29fd060ac8 90
ashleymills 0:5a29fd060ac8 91
ashleymills 0:5a29fd060ac8 92 EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
ashleymills 0:5a29fd060ac8 93 {
ashleymills 0:5a29fd060ac8 94 HKEY hKey;
ashleymills 0:5a29fd060ac8 95 unsigned long datatype;
ashleymills 0:5a29fd060ac8 96 unsigned long bufferlength = buf_size;
ashleymills 0:5a29fd060ac8 97
ashleymills 0:5a29fd060ac8 98 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
ashleymills 0:5a29fd060ac8 99 TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
ashleymills 0:5a29fd060ac8 100 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
ashleymills 0:5a29fd060ac8 101 return -1;
ashleymills 0:5a29fd060ac8 102
ashleymills 0:5a29fd060ac8 103 RegQueryValueEx(hKey, "Domain", NULL, &datatype, buf, &bufferlength);
ashleymills 0:5a29fd060ac8 104 RegCloseKey(hKey);
ashleymills 0:5a29fd060ac8 105 return 0;
ashleymills 0:5a29fd060ac8 106 }
ashleymills 0:5a29fd060ac8 107 #endif
ashleymills 0:5a29fd060ac8 108
ashleymills 0:5a29fd060ac8 109 #undef malloc
ashleymills 0:5a29fd060ac8 110 #undef realloc
ashleymills 0:5a29fd060ac8 111 #undef calloc
ashleymills 0:5a29fd060ac8 112
ashleymills 0:5a29fd060ac8 113 static const char * out_of_mem_str = "out of memory";
ashleymills 0:5a29fd060ac8 114 static const char * file_open_str = "Could not open file \"%s\"";
ashleymills 0:5a29fd060ac8 115
ashleymills 0:5a29fd060ac8 116 /*
ashleymills 0:5a29fd060ac8 117 * Some functions that call display some error trace and then call abort().
ashleymills 0:5a29fd060ac8 118 * This just makes life much easier on embedded systems, since we're
ashleymills 0:5a29fd060ac8 119 * suffering major trauma...
ashleymills 0:5a29fd060ac8 120 */
ashleymills 0:5a29fd060ac8 121 EXP_FUNC void * STDCALL ax_malloc(size_t s)
ashleymills 0:5a29fd060ac8 122 {
ashleymills 0:5a29fd060ac8 123 void *x;
ashleymills 0:5a29fd060ac8 124
ashleymills 0:5a29fd060ac8 125 if ((x = malloc(s)) == NULL)
ashleymills 0:5a29fd060ac8 126 exit_now(out_of_mem_str);
ashleymills 0:5a29fd060ac8 127
ashleymills 0:5a29fd060ac8 128 return x;
ashleymills 0:5a29fd060ac8 129 }
ashleymills 0:5a29fd060ac8 130
ashleymills 0:5a29fd060ac8 131 EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s)
ashleymills 0:5a29fd060ac8 132 {
ashleymills 0:5a29fd060ac8 133 void *x;
ashleymills 0:5a29fd060ac8 134
ashleymills 0:5a29fd060ac8 135 if ((x = realloc(y, s)) == NULL)
ashleymills 0:5a29fd060ac8 136 exit_now(out_of_mem_str);
ashleymills 0:5a29fd060ac8 137
ashleymills 0:5a29fd060ac8 138 return x;
ashleymills 0:5a29fd060ac8 139 }
ashleymills 0:5a29fd060ac8 140
ashleymills 0:5a29fd060ac8 141 EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s)
ashleymills 0:5a29fd060ac8 142 {
ashleymills 0:5a29fd060ac8 143 void *x;
ashleymills 0:5a29fd060ac8 144
ashleymills 0:5a29fd060ac8 145 if ((x = calloc(n, s)) == NULL) {
ashleymills 0:5a29fd060ac8 146 exit_now(out_of_mem_str);
ashleymills 0:5a29fd060ac8 147 }
ashleymills 0:5a29fd060ac8 148
ashleymills 0:5a29fd060ac8 149 return x;
ashleymills 0:5a29fd060ac8 150 }
ashleymills 0:5a29fd060ac8 151 /*
ashleymills 0:5a29fd060ac8 152 EXP_FUNC int STDCALL ax_open(const char *pathname, int flags)
ashleymills 0:5a29fd060ac8 153 {
ashleymills 0:5a29fd060ac8 154 int x;
ashleymills 0:5a29fd060ac8 155
ashleymills 0:5a29fd060ac8 156 if ((x = open(pathname, flags)) < 0)
ashleymills 0:5a29fd060ac8 157 exit_now(file_open_str, pathname);
ashleymills 0:5a29fd060ac8 158
ashleymills 0:5a29fd060ac8 159 return x;
ashleymills 0:5a29fd060ac8 160 }
ashleymills 0:5a29fd060ac8 161 */
ashleymills 0:5a29fd060ac8 162
ashleymills 0:5a29fd060ac8 163 /**
ashleymills 0:5a29fd060ac8 164 * This is a call which will deliberately exit an application, but will
ashleymills 0:5a29fd060ac8 165 * display some information before dying.
ashleymills 0:5a29fd060ac8 166 */
ashleymills 0:5a29fd060ac8 167 void exit_now(const char *format, ...)
ashleymills 0:5a29fd060ac8 168 {
ashleymills 0:5a29fd060ac8 169 va_list argp;
ashleymills 0:5a29fd060ac8 170
ashleymills 0:5a29fd060ac8 171 va_start(argp, format);
ashleymills 0:5a29fd060ac8 172 vfprintf(stderr, format, argp);
ashleymills 0:5a29fd060ac8 173 va_end(argp);
ashleymills 0:5a29fd060ac8 174 abort();
ashleymills 0:5a29fd060ac8 175 }
ashleymills 0:5a29fd060ac8 176