oRTP  0.20.0
port.h
1 /*
2  The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack.
3  Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 /* this file is responsible of the portability of the stack */
20 
21 #ifndef ORTP_PORT_H
22 #define ORTP_PORT_H
23 
24 
25 #if !defined(WIN32) && !defined(_WIN32_WCE)
26 /********************************/
27 /* definitions for UNIX flavour */
28 /********************************/
29 
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <pthread.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <string.h>
39 
40 #ifdef __linux
41 #include <stdint.h>
42 #endif
43 
44 
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #if defined(_XOPEN_SOURCE_EXTENDED) || !defined(__hpux)
49 #include <arpa/inet.h>
50 #endif
51 
52 
53 
54 #include <sys/time.h>
55 
56 #ifdef ORTP_INET6
57 #include <netdb.h>
58 #endif
59 
60 typedef int ortp_socket_t;
61 typedef pthread_t ortp_thread_t;
62 typedef pthread_mutex_t ortp_mutex_t;
63 typedef pthread_cond_t ortp_cond_t;
64 
65 #ifdef __INTEL_COMPILER
66 #pragma warning(disable : 111) // statement is unreachable
67 #pragma warning(disable : 181) // argument is incompatible with corresponding format string conversion
68 #pragma warning(disable : 188) // enumerated type mixed with another type
69 #pragma warning(disable : 593) // variable "xxx" was set but never used
70 #pragma warning(disable : 810) // conversion from "int" to "unsigned short" may lose significant bits
71 #pragma warning(disable : 869) // parameter "xxx" was never referenced
72 #pragma warning(disable : 981) // operands are evaluated in unspecified order
73 #pragma warning(disable : 1418) // external function definition with no prior declaration
74 #pragma warning(disable : 1419) // external declaration in primary source file
75 #pragma warning(disable : 1469) // "cc" clobber ignored
76 #endif
77 
78 #ifdef __cplusplus
79 extern "C"
80 {
81 #endif
82 
83 int __ortp_thread_join(ortp_thread_t thread, void **ptr);
84 int __ortp_thread_create(pthread_t *thread, pthread_attr_t *attr, void * (*routine)(void*), void *arg);
85 
86 #ifdef __cplusplus
87 }
88 #endif
89 
90 #define ortp_thread_create __ortp_thread_create
91 #define ortp_thread_join __ortp_thread_join
92 #define ortp_thread_exit pthread_exit
93 #define ortp_mutex_init pthread_mutex_init
94 #define ortp_mutex_lock pthread_mutex_lock
95 #define ortp_mutex_unlock pthread_mutex_unlock
96 #define ortp_mutex_destroy pthread_mutex_destroy
97 #define ortp_cond_init pthread_cond_init
98 #define ortp_cond_signal pthread_cond_signal
99 #define ortp_cond_broadcast pthread_cond_broadcast
100 #define ortp_cond_wait pthread_cond_wait
101 #define ortp_cond_destroy pthread_cond_destroy
102 
103 #define SOCKET_OPTION_VALUE void *
104 #define SOCKET_BUFFER void *
105 
106 #define getSocketError() strerror(errno)
107 #define getSocketErrorCode() (errno)
108 
109 #define ortp_log10f(x) log10f(x)
110 
111 
112 #else
113 /*********************************/
114 /* definitions for WIN32 flavour */
115 /*********************************/
116 
117 #include <stdio.h>
118 #include <stdlib.h>
119 #include <stdarg.h>
120 #include <winsock2.h>
121 #include <ws2tcpip.h>
122 #include <io.h>
123 
124 #ifdef _MSC_VER
125 #pragma push_macro("_WINSOCKAPI_")
126 #ifndef _WINSOCKAPI_
127 #define _WINSOCKAPI_
128 #endif
129 
130 typedef unsigned __int64 uint64_t;
131 typedef __int64 int64_t;
132 typedef unsigned short uint16_t;
133 typedef unsigned int uint32_t;
134 typedef int int32_t;
135 typedef unsigned char uint8_t;
136 typedef __int16 int16_t;
137 #else
138 #include <stdint.h> /*provided by mingw32*/
139 #endif
140 
141 #define vsnprintf _vsnprintf
142 #define srandom srand
143 #define random rand
144 
145 
146 typedef SOCKET ortp_socket_t;
147 typedef HANDLE ortp_cond_t;
148 typedef HANDLE ortp_mutex_t;
149 typedef HANDLE ortp_thread_t;
150 
151 #define ortp_thread_create WIN_thread_create
152 #define ortp_thread_join WIN_thread_join
153 #define ortp_thread_exit(arg)
154 #define ortp_mutex_init WIN_mutex_init
155 #define ortp_mutex_lock WIN_mutex_lock
156 #define ortp_mutex_unlock WIN_mutex_unlock
157 #define ortp_mutex_destroy WIN_mutex_destroy
158 #define ortp_cond_init WIN_cond_init
159 #define ortp_cond_signal WIN_cond_signal
160 #define ortp_cond_broadcast WIN_cond_broadcast
161 #define ortp_cond_wait WIN_cond_wait
162 #define ortp_cond_destroy WIN_cond_destroy
163 
164 
165 #ifdef __cplusplus
166 extern "C"
167 {
168 #endif
169 
170 int WIN_mutex_init(ortp_mutex_t *m, void *attr_unused);
171 int WIN_mutex_lock(ortp_mutex_t *mutex);
172 int WIN_mutex_unlock(ortp_mutex_t *mutex);
173 int WIN_mutex_destroy(ortp_mutex_t *mutex);
174 int WIN_thread_create(ortp_thread_t *t, void *attr_unused, void *(*func)(void*), void *arg);
175 int WIN_thread_join(ortp_thread_t thread, void **unused);
176 int WIN_cond_init(ortp_cond_t *cond, void *attr_unused);
177 int WIN_cond_wait(ortp_cond_t * cond, ortp_mutex_t * mutex);
178 int WIN_cond_signal(ortp_cond_t * cond);
179 int WIN_cond_broadcast(ortp_cond_t * cond);
180 int WIN_cond_destroy(ortp_cond_t * cond);
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #define SOCKET_OPTION_VALUE char *
187 #define inline __inline
188 
189 #if defined(_WIN32_WCE)
190 
191 #define ortp_log10f(x) (float)log10 ((double)x)
192 
193 #ifdef assert
194  #undef assert
195 #endif /*assert*/
196 #define assert(exp) ((void)0)
197 
198 #ifdef errno
199  #undef errno
200 #endif /*errno*/
201 #define errno GetLastError()
202 #ifdef strerror
203  #undef strerror
204 #endif /*strerror*/
205 const char * ortp_strerror(DWORD value);
206 #define strerror ortp_strerror
207 
208 
209 #else /*_WIN32_WCE*/
210 
211 #define ortp_log10f(x) log10f(x)
212 
213 #endif
214 
215 const char *getWinSocketError(int error);
216 #define getSocketErrorCode() WSAGetLastError()
217 #define getSocketError() getWinSocketError(WSAGetLastError())
218 
219 #define snprintf _snprintf
220 #define strcasecmp _stricmp
221 
222 #if 0
223 struct timeval {
224  long tv_sec; /* seconds */
225  long tv_usec; /* and microseconds */
226 };
227 #endif
228 
229 
230 #ifdef __cplusplus
231 extern "C"{
232 #endif
233 int gettimeofday (struct timeval *tv, void* tz);
234 #ifdef _WORKAROUND_MINGW32_BUGS
235 char * WSAAPI gai_strerror(int errnum);
236 #endif
237 #ifdef __cplusplus
238 }
239 #endif
240 
241 #endif
242 
243 typedef unsigned char bool_t;
244 #undef TRUE
245 #undef FALSE
246 #define TRUE 1
247 #define FALSE 0
248 
249 #ifdef __cplusplus
250 extern "C"{
251 #endif
252 
253 void* ortp_malloc(size_t sz);
254 void ortp_free(void *ptr);
255 void* ortp_realloc(void *ptr, size_t sz);
256 void* ortp_malloc0(size_t sz);
257 char * ortp_strdup(const char *tmp);
258 
259 /*override the allocator with this method, to be called BEFORE ortp_init()*/
260 typedef struct _OrtpMemoryFunctions{
261  void *(*malloc_fun)(size_t sz);
262  void *(*realloc_fun)(void *ptr, size_t sz);
263  void (*free_fun)(void *ptr);
265 
266 void ortp_set_memory_functions(OrtpMemoryFunctions *functions);
267 
268 #define ortp_new(type,count) (type*)ortp_malloc(sizeof(type)*(count))
269 #define ortp_new0(type,count) (type*)ortp_malloc0(sizeof(type)*(count))
270 
271 int close_socket(ortp_socket_t sock);
272 int set_non_blocking_socket(ortp_socket_t sock);
273 
274 char *ortp_strndup(const char *str,int n);
275 char *ortp_strdup_printf(const char *fmt,...);
276 char *ortp_strdup_vprintf(const char *fmt, va_list ap);
277 
278 int ortp_file_exist(const char *pathname);
279 
280 /* portable named pipes and shared memory*/
281 #if !defined(_WIN32_WCE)
282 #ifdef WIN32
283 typedef HANDLE ortp_pipe_t;
284 #define ORTP_PIPE_INVALID INVALID_HANDLE_VALUE
285 #else
286 typedef int ortp_pipe_t;
287 #define ORTP_PIPE_INVALID (-1)
288 #endif
289 
290 ortp_pipe_t ortp_server_pipe_create(const char *name);
291 /*
292  * warning: on win32 ortp_server_pipe_accept_client() might return INVALID_HANDLE_VALUE without
293  * any specific error, this happens when ortp_server_pipe_close() is called on another pipe.
294  * This pipe api is not thread-safe.
295 */
296 ortp_pipe_t ortp_server_pipe_accept_client(ortp_pipe_t server);
297 int ortp_server_pipe_close(ortp_pipe_t spipe);
298 int ortp_server_pipe_close_client(ortp_pipe_t client);
299 
300 ortp_pipe_t ortp_client_pipe_connect(const char *name);
301 int ortp_client_pipe_close(ortp_pipe_t sock);
302 
303 int ortp_pipe_read(ortp_pipe_t p, uint8_t *buf, int len);
304 int ortp_pipe_write(ortp_pipe_t p, const uint8_t *buf, int len);
305 
306 void *ortp_shm_open(unsigned int keyid, int size, int create);
307 void ortp_shm_close(void *memory);
308 
309 #endif
310 
311 #ifdef __cplusplus
312 }
313 
314 #endif
315 
316 
317 #if (defined(WIN32) || defined(_WIN32_WCE)) && !defined(ORTP_STATIC)
318 #ifdef ORTP_EXPORTS
319  #define VAR_DECLSPEC __declspec(dllexport)
320 #else
321  #define VAR_DECLSPEC __declspec(dllimport)
322 #endif
323 #else
324  #define VAR_DECLSPEC extern
325 #endif
326 
327 
328 /*define __ios when we are compiling for ios.
329  The TARGET_OS_IPHONE macro is stupid, it is defined to 0 when compiling on mac os x.
330 */
331 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE==1
332 #define __ios 1
333 #endif
334 
335 #endif
336 
337 
Definition: port.h:260