oRTP  0.22.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 #define ORTP_PUBLIC
79 #define ORTP_INLINE inline
80 
81 #ifdef __cplusplus
82 extern "C"
83 {
84 #endif
85 
86 int __ortp_thread_join(ortp_thread_t thread, void **ptr);
87 int __ortp_thread_create(pthread_t *thread, pthread_attr_t *attr, void * (*routine)(void*), void *arg);
88 
89 #ifdef __cplusplus
90 }
91 #endif
92 
93 #define ortp_thread_create __ortp_thread_create
94 #define ortp_thread_join __ortp_thread_join
95 #define ortp_thread_exit pthread_exit
96 #define ortp_mutex_init pthread_mutex_init
97 #define ortp_mutex_lock pthread_mutex_lock
98 #define ortp_mutex_unlock pthread_mutex_unlock
99 #define ortp_mutex_destroy pthread_mutex_destroy
100 #define ortp_cond_init pthread_cond_init
101 #define ortp_cond_signal pthread_cond_signal
102 #define ortp_cond_broadcast pthread_cond_broadcast
103 #define ortp_cond_wait pthread_cond_wait
104 #define ortp_cond_destroy pthread_cond_destroy
105 
106 #define SOCKET_OPTION_VALUE void *
107 #define SOCKET_BUFFER void *
108 
109 #define getSocketError() strerror(errno)
110 #define getSocketErrorCode() (errno)
111 
112 #define ortp_log10f(x) log10f(x)
113 
114 
115 #else
116 /*********************************/
117 /* definitions for WIN32 flavour */
118 /*********************************/
119 
120 #include <stdio.h>
121 #include <stdlib.h>
122 #include <stdarg.h>
123 #include <winsock2.h>
124 #include <ws2tcpip.h>
125 
126 #ifdef _MSC_VER
127 #ifdef ORTP_EXPORTS
128 #define ORTP_PUBLIC __declspec(dllexport)
129 #else
130 #define ORTP_PUBLIC __declspec(dllimport)
131 #endif
132 #pragma push_macro("_WINSOCKAPI_")
133 #ifndef _WINSOCKAPI_
134 #define _WINSOCKAPI_
135 #endif
136 
137 #define strtok_r strtok_s
138 
139 typedef unsigned __int64 uint64_t;
140 typedef __int64 int64_t;
141 typedef unsigned short uint16_t;
142 typedef unsigned int uint32_t;
143 typedef int int32_t;
144 typedef unsigned char uint8_t;
145 typedef __int16 int16_t;
146 #else
147 #include <stdint.h> /*provided by mingw32*/
148 #include <io.h>
149 #define ORTP_PUBLIC
150 ORTP_PUBLIC char* strtok_r(char *str, const char *delim, char **nextp);
151 #endif
152 
153 #define vsnprintf _vsnprintf
154 #define srandom srand
155 #define random rand
156 
157 typedef SOCKET ortp_socket_t;
158 #ifdef WINAPI_FAMILY_PHONE_APP
159 typedef CONDITION_VARIABLE ortp_cond_t;
160 typedef SRWLOCK ortp_mutex_t;
161 #else
162 typedef HANDLE ortp_cond_t;
163 typedef HANDLE ortp_mutex_t;
164 #endif
165 typedef HANDLE ortp_thread_t;
166 
167 #define ortp_thread_create WIN_thread_create
168 #define ortp_thread_join WIN_thread_join
169 #define ortp_thread_exit(arg)
170 #define ortp_mutex_init WIN_mutex_init
171 #define ortp_mutex_lock WIN_mutex_lock
172 #define ortp_mutex_unlock WIN_mutex_unlock
173 #define ortp_mutex_destroy WIN_mutex_destroy
174 #define ortp_cond_init WIN_cond_init
175 #define ortp_cond_signal WIN_cond_signal
176 #define ortp_cond_broadcast WIN_cond_broadcast
177 #define ortp_cond_wait WIN_cond_wait
178 #define ortp_cond_destroy WIN_cond_destroy
179 
180 
181 #ifdef __cplusplus
182 extern "C"
183 {
184 #endif
185 
186 ORTP_PUBLIC int WIN_mutex_init(ortp_mutex_t *m, void *attr_unused);
187 ORTP_PUBLIC int WIN_mutex_lock(ortp_mutex_t *mutex);
188 ORTP_PUBLIC int WIN_mutex_unlock(ortp_mutex_t *mutex);
189 ORTP_PUBLIC int WIN_mutex_destroy(ortp_mutex_t *mutex);
190 ORTP_PUBLIC int WIN_thread_create(ortp_thread_t *t, void *attr_unused, void *(*func)(void*), void *arg);
191 ORTP_PUBLIC int WIN_thread_join(ortp_thread_t thread, void **unused);
192 ORTP_PUBLIC int WIN_cond_init(ortp_cond_t *cond, void *attr_unused);
193 ORTP_PUBLIC int WIN_cond_wait(ortp_cond_t * cond, ortp_mutex_t * mutex);
194 ORTP_PUBLIC int WIN_cond_signal(ortp_cond_t * cond);
195 ORTP_PUBLIC int WIN_cond_broadcast(ortp_cond_t * cond);
196 ORTP_PUBLIC int WIN_cond_destroy(ortp_cond_t * cond);
197 
198 #ifdef __cplusplus
199 }
200 #endif
201 
202 #define SOCKET_OPTION_VALUE char *
203 #define ORTP_INLINE __inline
204 
205 #if defined(_WIN32_WCE)
206 
207 #define ortp_log10f(x) (float)log10 ((double)x)
208 
209 #ifdef assert
210  #undef assert
211 #endif /*assert*/
212 #define assert(exp) ((void)0)
213 
214 #ifdef errno
215  #undef errno
216 #endif /*errno*/
217 #define errno GetLastError()
218 #ifdef strerror
219  #undef strerror
220 #endif /*strerror*/
221 const char * ortp_strerror(DWORD value);
222 #define strerror ortp_strerror
223 
224 
225 #else /*_WIN32_WCE*/
226 
227 #define ortp_log10f(x) log10f(x)
228 
229 #endif
230 
231 ORTP_PUBLIC const char *getWinSocketError(int error);
232 #define getSocketErrorCode() WSAGetLastError()
233 #define getSocketError() getWinSocketError(WSAGetLastError())
234 
235 #define snprintf _snprintf
236 #define strcasecmp _stricmp
237 #define strncasecmp _strnicmp
238 
239 
240 #ifdef __cplusplus
241 extern "C"{
242 #endif
243 ORTP_PUBLIC int gettimeofday (struct timeval *tv, void* tz);
244 #ifdef _WORKAROUND_MINGW32_BUGS
245 char * WSAAPI gai_strerror(int errnum);
246 #endif
247 #ifdef __cplusplus
248 }
249 #endif
250 
251 #endif
252 
253 typedef unsigned char bool_t;
254 #undef TRUE
255 #undef FALSE
256 #define TRUE 1
257 #define FALSE 0
258 
259 typedef struct ortpTimeSpec{
260  int64_t tv_sec;
261  int64_t tv_nsec;
262 }ortpTimeSpec;
263 
264 #ifdef __cplusplus
265 extern "C"{
266 #endif
267 
268 ORTP_PUBLIC void* ortp_malloc(size_t sz);
269 ORTP_PUBLIC void ortp_free(void *ptr);
270 ORTP_PUBLIC void* ortp_realloc(void *ptr, size_t sz);
271 ORTP_PUBLIC void* ortp_malloc0(size_t sz);
272 ORTP_PUBLIC char * ortp_strdup(const char *tmp);
273 
274 /*override the allocator with this method, to be called BEFORE ortp_init()*/
275 typedef struct _OrtpMemoryFunctions{
276  void *(*malloc_fun)(size_t sz);
277  void *(*realloc_fun)(void *ptr, size_t sz);
278  void (*free_fun)(void *ptr);
280 
281 void ortp_set_memory_functions(OrtpMemoryFunctions *functions);
282 
283 #define ortp_new(type,count) (type*)ortp_malloc(sizeof(type)*(count))
284 #define ortp_new0(type,count) (type*)ortp_malloc0(sizeof(type)*(count))
285 
286 ORTP_PUBLIC int close_socket(ortp_socket_t sock);
287 ORTP_PUBLIC int set_non_blocking_socket(ortp_socket_t sock);
288 
289 ORTP_PUBLIC char *ortp_strndup(const char *str,int n);
290 ORTP_PUBLIC char *ortp_strdup_printf(const char *fmt,...);
291 ORTP_PUBLIC char *ortp_strdup_vprintf(const char *fmt, va_list ap);
292 
293 ORTP_PUBLIC int ortp_file_exist(const char *pathname);
294 
295 ORTP_PUBLIC void ortp_get_cur_time(ortpTimeSpec *ret);
296 
297 /* portable named pipes and shared memory*/
298 #if !defined(_WIN32_WCE)
299 #ifdef WIN32
300 typedef HANDLE ortp_pipe_t;
301 #define ORTP_PIPE_INVALID INVALID_HANDLE_VALUE
302 #else
303 typedef int ortp_pipe_t;
304 #define ORTP_PIPE_INVALID (-1)
305 #endif
306 
307 ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_create(const char *name);
308 /*
309  * warning: on win32 ortp_server_pipe_accept_client() might return INVALID_HANDLE_VALUE without
310  * any specific error, this happens when ortp_server_pipe_close() is called on another pipe.
311  * This pipe api is not thread-safe.
312 */
313 ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_accept_client(ortp_pipe_t server);
314 ORTP_PUBLIC int ortp_server_pipe_close(ortp_pipe_t spipe);
315 ORTP_PUBLIC int ortp_server_pipe_close_client(ortp_pipe_t client);
316 
317 ORTP_PUBLIC ortp_pipe_t ortp_client_pipe_connect(const char *name);
318 ORTP_PUBLIC int ortp_client_pipe_close(ortp_pipe_t sock);
319 
320 ORTP_PUBLIC int ortp_pipe_read(ortp_pipe_t p, uint8_t *buf, int len);
321 ORTP_PUBLIC int ortp_pipe_write(ortp_pipe_t p, const uint8_t *buf, int len);
322 
323 ORTP_PUBLIC void *ortp_shm_open(unsigned int keyid, int size, int create);
324 ORTP_PUBLIC void ortp_shm_close(void *memory);
325 
326 #endif
327 
328 #ifdef __cplusplus
329 }
330 
331 #endif
332 
333 
334 #if (defined(WIN32) || defined(_WIN32_WCE)) && !defined(ORTP_STATIC)
335 #ifdef ORTP_EXPORTS
336  #define ORTP_VAR_PUBLIC extern __declspec(dllexport)
337 #else
338  #define ORTP_VAR_PUBLIC __declspec(dllimport)
339 #endif
340 #else
341  #define ORTP_VAR_PUBLIC extern
342 #endif
343 
344 
345 /*define __ios when we are compiling for ios.
346  The TARGET_OS_IPHONE macro is stupid, it is defined to 0 when compiling on mac os x.
347 */
348 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE==1
349 #define __ios 1
350 #endif
351 
352 #endif
353 
354