summaryrefslogtreecommitdiff
path: root/release/src/router/matrixssl/src/os/osLayer.h
blob: 00872198b208095cc66de0c148a9a7341f08863d (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
/*
 *	osLayer.h
 *	Release $Name: MATRIXSSL_1_8_8_OPEN $
 *	
 *	Layered header for OS specific functions
 *	Contributors adding new OS support must implement all functions 
 *	externed below.
 */
/*
 *	Copyright (c) PeerSec Networks, 2002-2009. All Rights Reserved.
 *	The latest version of this code is available at http://www.matrixssl.org
 *
 *	This software is open source; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This General Public License does NOT permit incorporating this software 
 *	into proprietary programs.  If you are unable to comply with the GPL, a 
 *	commercial license for this software may be purchased from PeerSec Networks
 *	at http://www.peersec.com
 *	
 *	This program is distributed in WITHOUT ANY WARRANTY; without even the 
 *	implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *	See the GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *	http://www.gnu.org/copyleft/gpl.html
 */
/******************************************************************************/

#ifndef _h_OS_LAYER
#define _h_OS_LAYER
#define _h_EXPORT_SYMBOLS

#include <stdio.h>
#include <stdlib.h>

#ifndef WINCE
#include <time.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************/

#include "../../matrixCommon.h"
#include "psMalloc.h"

/*
	Functions defined at OS level
*/
extern int32	sslOpenOsdep(void);
extern int32	sslCloseOsdep(void);
extern int32	sslGetEntropy(unsigned char *bytes, int32 size);

/*
	Defines to make library multithreading safe
*/
#ifdef USE_MULTITHREADING

#if WIN32 || WINCE
#include <windows.h>

typedef CRITICAL_SECTION sslMutex_t;
#define sslCreateMutex(M)	InitializeCriticalSection((CRITICAL_SECTION *) M);
#define sslLockMutex(M)		EnterCriticalSection((CRITICAL_SECTION *) M);
#define sslUnlockMutex(M)	LeaveCriticalSection((CRITICAL_SECTION *) M);
#define sslDestroyMutex(M)	DeleteCriticalSection((CRITICAL_SECTION *) M);

#elif LINUX
#include <pthread.h>
#include <string.h>

typedef pthread_mutex_t sslMutex_t;
extern int32	sslCreateMutex(sslMutex_t *mutex);
extern int32	sslLockMutex(sslMutex_t *mutex);
extern int32	sslUnlockMutex(sslMutex_t *mutex);
extern void	sslDestroyMutex(sslMutex_t *mutex);
#elif VXWORKS
#include "semLib.h"

typedef SEM_ID		sslMutex_t; 
extern int32	sslCreateMutex(sslMutex_t *mutex);
extern int32	sslLockMutex(sslMutex_t *mutex);
extern int32	sslUnlockMutex(sslMutex_t *mutex);
extern void	sslDestroyMutex(sslMutex_t *mutex);
#endif /* WIN32 || CE */

#else /* USE_MULTITHREADING */
typedef int32	sslMutex_t;
#define sslCreateMutex(M)
#define sslLockMutex(M)
#define sslUnlockMutex(M)
#define sslDestroyMutex(M)

#endif /* USE_MULTITHREADING */

/*
	Make sslTime_t an opaque time value.
	FUTURE - use high res time instead of time_t
*/
#ifdef LINUX
/*
	On some *NIX versions such as MAC OS X 10.4, CLK_TCK has been deprecated
*/
#ifndef CLK_TCK
#define CLK_TCK		CLOCKS_PER_SEC
#endif /* CLK_TCK */
#endif /* LINUX */

#if defined(WIN32)
#include <windows.h>
typedef LARGE_INTEGER sslTime_t;
#elif VXWORKS
typedef struct {
		long sec;
		long usec;
	} sslTime_t;
#elif (defined(USE_RDTSCLL_TIME) || defined(RDTSC))
typedef unsigned long long LARGE_INTEGER;
typedef LARGE_INTEGER sslTime_t;
#elif WINCE
#include <windows.h>

typedef LARGE_INTEGER sslTime_t;
#else
typedef struct {
		long sec;
		long usec;
	} sslTime_t;
#endif

/******************************************************************************/
/*
	We define our own stat for CE.
*/
#if WINCE

extern int32 stat(char *filename, struct stat *sbuf);

struct stat {
	unsigned long st_size;	/* file size in bytes				*/
	unsigned long st_mode;
	time_t st_atime;		/* time of last access				*/
	time_t st_mtime;		/* time of last data modification	*/
	time_t st_ctime;		/* time of last file status change	*/
};

#define			S_IFREG 0100000
#define			S_IFDIR 0040000

extern time_t time();

#endif /* WINCE */

extern int32		sslInitMsecs(sslTime_t *t);
extern int32		sslCompareTime(sslTime_t a, sslTime_t b);
extern int32		sslDiffSecs(sslTime_t then, sslTime_t now);
extern long		sslDiffMsecs(sslTime_t then, sslTime_t now);


/******************************************************************************/
/*
	Debugging functionality.  
	
	If DEBUG is defined matrixStrDebugMsg and matrixIntDebugMsg messages are
	output to stdout, sslAsserts go to stderror and call psBreak.

	In non-DEBUG builds matrixStrDebugMsg and matrixIntDebugMsg are 
	compiled out.  sslAsserts still go to stderr, but psBreak is not called.

*/

#if DEBUG
extern void psBreak(void);
extern void matrixStrDebugMsg(char *message, char *arg);
extern void matrixIntDebugMsg(char *message, int32 arg);
extern void matrixPtrDebugMsg(char *message, void *arg);
#define sslAssert(C)  if (C) ; else \
	{fprintf(stderr, "%s:%d sslAssert(%s)\n",__FILE__, __LINE__, #C); psBreak(); }
#else
#define matrixStrDebugMsg(x, y)
#define matrixIntDebugMsg(x, y)
#define matrixPtrDebugMsg(x, y)
#define sslAssert(C)  if (C) ; else \
	{fprintf(stderr, "%s:%d sslAssert(%s)\n",__FILE__, __LINE__, #C); }
#endif /* DEBUG */

#ifdef __cplusplus
}
#endif

#endif /* _h_OS_LAYER */

/******************************************************************************/