This is a fork of the mbed port of axTLS

Dependents:   TLS_axTLS-Example HTTPSClientExample

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers os_port.c Source File

os_port.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007, Cameron Rich
00003  * 
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without 
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * * Redistributions of source code must retain the above copyright notice, 
00010  *   this list of conditions and the following disclaimer.
00011  * * Redistributions in binary form must reproduce the above copyright notice, 
00012  *   this list of conditions and the following disclaimer in the documentation 
00013  *   and/or other materials provided with the distribution.
00014  * * Neither the name of the axTLS project nor the names of its contributors 
00015  *   may be used to endorse or promote products derived from this software 
00016  *   without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 /**
00032  * @file os_port.c
00033  *
00034  * OS specific functions.
00035  */
00036 #include <time.h>
00037 #include <stdlib.h>
00038 #include <errno.h>
00039 #include <stdarg.h>
00040 #include "os_port.h"
00041 #include <stdio.h>
00042 #include "sockets.h"
00043 
00044 static int memory_buf[400];
00045 static char enable = 0;
00046 static int nb_entries = 0;
00047 
00048 void disable_memory_buf(void)
00049 {
00050     enable = 0;
00051 }
00052 void enable_memory_buf(void)
00053 {
00054     enable = 1;
00055 }
00056 void init_memory_buf(void)
00057 {
00058     for(int i = 0; i < 400; i += 2)
00059     {
00060         memory_buf[i] = -1;
00061         memory_buf[i+1] = 0;
00062     }
00063 }   
00064 void print_buf_stats(void)
00065 {
00066     if(enable)
00067     {
00068         int used = 0;
00069         for(int i = 1; i < 400; i += 2)
00070             used += memory_buf[i];
00071         printf("%d\n", used);
00072     }
00073 }
00074 
00075 void print_all_buf_stats(void)
00076 {       
00077     int used = 0;
00078     for(int i = 1; i < 400; i += 2)
00079         used += memory_buf[i];
00080     printf("used: %d bytes\n", used);
00081     
00082     for(int i = 0; i < 400; i += 2)
00083         if(memory_buf[i] != -1)
00084             printf("ptr:%X, size:%d\n", memory_buf[i], memory_buf[i+1]);
00085 }
00086 
00087 static void add_entry(void *x, size_t s, const char* f, const int l)
00088 {
00089     nb_entries++;
00090     for(int i = 0; i < 400; i += 2)
00091     {
00092         if(memory_buf[i] == -1)
00093         {
00094             if(enable)
00095                 printf("new ptr:%X, size:%d at %s:%d\n", x, s, f, l); 
00096             memory_buf[i] = (int)(x);
00097             memory_buf[i+1] = s;
00098             return;
00099         }
00100     }
00101     if(enable)
00102         printf("No space left in buffer\n"); 
00103 }
00104 
00105 static void remove_entry(void *x, const char* f, const int l)
00106 {
00107     nb_entries--;
00108     for(int i = 0; i < 400; i += 2)
00109     {
00110         if(memory_buf[i] == (int)(x))
00111         {
00112             if(enable)
00113                 printf("free ptr:%X, size:%d at %s:%d\n", memory_buf[i], memory_buf[i+1], f, l); 
00114             memory_buf[i] = -1;
00115             memory_buf[i+1] = 0;
00116             return;
00117         }
00118     }
00119     if(enable)
00120         printf("not found\n");
00121 }
00122 
00123 #ifdef MBED
00124 /**
00125  * gettimeofday() not in mbed 
00126  */
00127 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
00128 {       
00129     t->tv_sec = time(NULL);
00130     t->tv_usec = 0;                         /* 1sec precision only */ 
00131 }
00132 
00133 #endif
00134 
00135 #ifdef WIN32
00136 /**
00137  * gettimeofday() not in Win32 
00138  */
00139 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
00140 {       
00141 #if defined(_WIN32_WCE)
00142     t->tv_sec = time(NULL);
00143     t->tv_usec = 0;                         /* 1sec precision only */ 
00144 #else
00145     struct _timeb timebuffer;
00146     _ftime(&timebuffer);
00147     t->tv_sec = (long)timebuffer.time;
00148     t->tv_usec = 1000 * timebuffer.millitm; /* 1ms precision */
00149 #endif
00150 }
00151 
00152 
00153 /**
00154  * strcasecmp() not in Win32
00155  */
00156 EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2)
00157 {
00158     while (tolower(*s1) == tolower(*s2++))
00159     {
00160         if (*s1++ == '\0')
00161         {
00162             return 0;
00163         }
00164     }
00165 
00166     return *(unsigned char *)s1 - *(unsigned char *)(s2 - 1);
00167 }
00168 
00169 
00170 EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
00171 {
00172     HKEY hKey;
00173     unsigned long datatype;
00174     unsigned long bufferlength = buf_size;
00175 
00176     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
00177             TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
00178                         0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
00179         return -1;
00180 
00181     RegQueryValueEx(hKey, "Domain", NULL, &datatype, buf, &bufferlength);
00182     RegCloseKey(hKey);
00183     return 0; 
00184 }
00185 #endif
00186 
00187 #undef malloc
00188 #undef realloc
00189 #undef calloc
00190 #undef free
00191 
00192 static const char * out_of_mem_str = "out of memory";
00193 //static const char * file_open_str = "Could not open file \"%s\"";
00194 
00195 /* 
00196  * Some functions that call display some error trace and then call abort().
00197  * This just makes life much easier on embedded systems, since we're 
00198  * suffering major trauma...
00199  */
00200 EXP_FUNC void * STDCALL ax_malloc(size_t s, const char* f, const int l)
00201 {
00202     if(enable)  
00203         printf("malloc\t");
00204 
00205     void *x;
00206 
00207     if ((x = malloc(s)) == NULL)
00208         exit_now(out_of_mem_str);
00209     add_entry(x,s, f, l);
00210     print_buf_stats();
00211 
00212     return x;
00213 }
00214 
00215 EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s, const char* f, const int l)
00216 {
00217     if(enable)  
00218         printf("realloc\t");
00219 
00220     void *x;
00221 
00222     if ((x = realloc(y, s)) == NULL)
00223         exit_now(out_of_mem_str);
00224     remove_entry(y, f, l);
00225     add_entry(x,s, f, l);
00226     print_buf_stats();
00227     return x;
00228 }
00229 
00230 EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s, const char* f, const int l)
00231 {
00232     if(enable)  
00233         printf("calloc\t");
00234     void *x;
00235   
00236     if ((x = calloc(n, s)) == NULL) {
00237         exit_now(out_of_mem_str);
00238     }
00239 
00240     add_entry(x,n*s, f, l);
00241     print_buf_stats();
00242     return x;
00243 }
00244 
00245 EXP_FUNC void STDCALL ax_free(void *y, const char* f, const int l)
00246 {
00247     if(enable)  
00248         printf("free\t");
00249         
00250     remove_entry(y, f, l);
00251     print_buf_stats();
00252     free(y);
00253 }
00254 /*
00255 EXP_FUNC int STDCALL ax_open(const char *pathname, int flags)
00256 {
00257     int x;
00258 
00259     if ((x = open(pathname, flags)) < 0)
00260         exit_now(file_open_str, pathname);
00261 
00262     return x;
00263 }
00264 */
00265 
00266 /**
00267  * This is a call which will deliberately exit an application, but will
00268  * display some information before dying.
00269  */
00270 void exit_now(const char *format, ...)
00271 {
00272     va_list argp;
00273 
00274     va_start(argp, format);
00275     vfprintf(stderr, format, argp);
00276     va_end(argp);
00277     abort();
00278 }
00279 
00280 
00281