summaryrefslogtreecommitdiff
path: root/tool/mbed/mbed-sdk/libraries/net/https/axTLS/ssl/os_port.c
blob: a0627bf620ec0951a0b79a8c51a58a4da0088c76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (c) 2007, Cameron Rich
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice, 
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 * * Neither the name of the axTLS project nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @file os_port.c
 *
 * OS specific functions.
 */
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include "os_port.h"
#include <stdio.h>
#include "sockets.h"

static int memory_buf[400];
static char enable = 1;
static int nb_entries = 0;
static int nb_alloc = 0;

void disable_memory_buf(void)
{
    enable = 0;
}
void enable_memory_buf(void)
{
    enable = 1;
}
void init_memory_buf(void)
{
    for(int i = 0; i < 400; i += 2)
    {
        memory_buf[i] = -1;
        memory_buf[i+1] = 0;
    }
}   
void print_buf_stats(void)
{
    if(enable)
    {
        int used = 0;
        for(int i = 1; i < 400; i += 2)
            used += memory_buf[i];
        printf("%d\n", used);
    }
}

void print_all_buf_stats(void)
{       
    int used = 0;
    for(int i = 1; i < 400; i += 2)
        used += memory_buf[i];
    printf("used: %d bytes\n", used);
    
    for(int i = 0; i < 400; i += 2)
        if(memory_buf[i] != -1)
            printf("ptr:%X, size:%d\n", memory_buf[i], memory_buf[i+1]);
}

static void add_entry(void *x, size_t s, const char* f, const int l)
{
    nb_entries++;
    for(int i = 0; i < 400; i += 2)
    {
        if(memory_buf[i] == -1)
        {
            if(enable)
                printf("new ptr:%X, size:%d at %s:%d\n", x, s, f, l); 
            memory_buf[i] = (int)(x);
            memory_buf[i+1] = s;
            return;
        }
    }
    if(enable)
        printf("No space left in buffer\n"); 
}

static void remove_entry(void *x, const char* f, const int l)
{
    nb_entries--;
    for(int i = 0; i < 400; i += 2)
    {
        if(memory_buf[i] == (int)(x))
        {
            if(enable)
                printf("free ptr:%X, size:%d at %s:%d\n", memory_buf[i], memory_buf[i+1], f, l); 
            memory_buf[i] = -1;
            memory_buf[i+1] = 0;
            return;
        }
    }
    if(enable)
        printf("not found\n");
}

#ifdef MBED
/**
 * gettimeofday() not in mbed 
 */
EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
{       
    t->tv_sec = time(NULL);
    t->tv_usec = 0;                         /* 1sec precision only */ 
}

#endif

#ifdef WIN32
/**
 * gettimeofday() not in Win32 
 */
EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
{       
#if defined(_WIN32_WCE)
    t->tv_sec = time(NULL);
    t->tv_usec = 0;                         /* 1sec precision only */ 
#else
    struct _timeb timebuffer;
    _ftime(&timebuffer);
    t->tv_sec = (long)timebuffer.time;
    t->tv_usec = 1000 * timebuffer.millitm; /* 1ms precision */
#endif
}


/**
 * strcasecmp() not in Win32
 */
EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2)
{
    while (tolower(*s1) == tolower(*s2++))
    {
        if (*s1++ == '\0')
        {
            return 0;
        }
    }

    return *(unsigned char *)s1 - *(unsigned char *)(s2 - 1);
}


EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
{
    HKEY hKey;
    unsigned long datatype;
    unsigned long bufferlength = buf_size;

    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
            TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
                        0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
        return -1;

    RegQueryValueEx(hKey, "Domain", NULL, &datatype, buf, &bufferlength);
    RegCloseKey(hKey);
    return 0; 
}
#endif

#undef malloc
#undef realloc
#undef calloc
#undef free

static const char * out_of_mem_str = "out of memory";
static const char * file_open_str = "Could not open file \"%s\"";

/* 
 * Some functions that call display some error trace and then call abort().
 * This just makes life much easier on embedded systems, since we're 
 * suffering major trauma...
 */
EXP_FUNC void * STDCALL ax_malloc(size_t s, const char* f, const int l)
{
    if(enable)  
        printf("malloc\t");

    void *x;

    if ((x = malloc(s)) == NULL)
        exit_now(out_of_mem_str);
    add_entry(x,s, f, l);
    print_buf_stats();

    return x;
}

EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s, const char* f, const int l)
{
    if(enable)  
        printf("realloc\t");

    void *x;

    if ((x = realloc(y, s)) == NULL)
        exit_now(out_of_mem_str);
    remove_entry(y, f, l);
    add_entry(x,s, f, l);
    print_buf_stats();
    return x;
}

EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s, const char* f, const int l)
{
    if(enable)  
        printf("calloc\t");
    void *x;
  
    if ((x = calloc(n, s)) == NULL) {
        exit_now(out_of_mem_str);
    }

    add_entry(x,n*s, f, l);
    print_buf_stats();
    return x;
}

EXP_FUNC void STDCALL ax_free(void *y, const char* f, const int l)
{
    if(enable)  
        printf("free\t");
        
    remove_entry(y, f, l);
    print_buf_stats();
    free(y);
}
/*
EXP_FUNC int STDCALL ax_open(const char *pathname, int flags)
{
    int x;

    if ((x = open(pathname, flags)) < 0)
        exit_now(file_open_str, pathname);

    return x;
}
*/

/**
 * This is a call which will deliberately exit an application, but will
 * display some information before dying.
 */
void exit_now(const char *format, ...)
{
    va_list argp;

    va_start(argp, format);
    vfprintf(stderr, format, argp);
    va_end(argp);
    abort();
}