oRTP  0.24.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 #include <netdb.h>
57 
58 typedef int ortp_socket_t;
59 typedef pthread_t ortp_thread_t;
60 typedef pthread_mutex_t ortp_mutex_t;
61 typedef pthread_cond_t ortp_cond_t;
62 
63 #ifdef __INTEL_COMPILER
64 #pragma warning(disable : 111) // statement is unreachable
65 #pragma warning(disable : 181) // argument is incompatible with corresponding format string conversion
66 #pragma warning(disable : 188) // enumerated type mixed with another type
67 #pragma warning(disable : 593) // variable "xxx" was set but never used
68 #pragma warning(disable : 810) // conversion from "int" to "unsigned short" may lose significant bits
69 #pragma warning(disable : 869) // parameter "xxx" was never referenced
70 #pragma warning(disable : 981) // operands are evaluated in unspecified order
71 #pragma warning(disable : 1418) // external function definition with no prior declaration
72 #pragma warning(disable : 1419) // external declaration in primary source file
73 #pragma warning(disable : 1469) // "cc" clobber ignored
74 #endif
75 
76 #define ORTP_PUBLIC
77 #define ORTP_INLINE inline
78 
79 #define WINAPI_FAMILY_PARTITION(x) 1
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(ortp_thread_t *thread, pthread_attr_t *attr, void * (*routine)(void*), void *arg);
88 unsigned long __ortp_thread_self(void);
89 
90 #ifdef __cplusplus
91 }
92 #endif
93 
94 #define ortp_thread_create __ortp_thread_create
95 #define ortp_thread_join __ortp_thread_join
96 #define ortp_thread_self __ortp_thread_self
97 #define ortp_thread_exit pthread_exit
98 #define ortp_mutex_init pthread_mutex_init
99 #define ortp_mutex_lock pthread_mutex_lock
100 #define ortp_mutex_unlock pthread_mutex_unlock
101 #define ortp_mutex_destroy pthread_mutex_destroy
102 #define ortp_cond_init pthread_cond_init
103 #define ortp_cond_signal pthread_cond_signal
104 #define ortp_cond_broadcast pthread_cond_broadcast
105 #define ortp_cond_wait pthread_cond_wait
106 #define ortp_cond_destroy pthread_cond_destroy
107 
108 #define SOCKET_OPTION_VALUE void *
109 #define SOCKET_BUFFER void *
110 
111 #define getSocketError() strerror(errno)
112 #define getSocketErrorCode() (errno)
113 #define ortp_gettimeofday(tv,tz) gettimeofday(tv,tz)
114 #define ortp_log10f(x) log10f(x)
115 
116 
117 #else
118 /*********************************/
119 /* definitions for WIN32 flavour */
120 /*********************************/
121 
122 #include <stdio.h>
123 #define _CRT_RAND_S
124 #include <stdlib.h>
125 #include <stdarg.h>
126 #include <winsock2.h>
127 #include <ws2tcpip.h>
128 
129 #if defined(__MINGW32__) || !defined(WINAPI_FAMILY_PARTITION)
130 // Only use with x being WINAPI_PARTITION_DESKTOP to test if building on desktop
131 #define WINAPI_FAMILY_PARTITION(x) 1
132 #endif
133 
134 #ifdef _MSC_VER
135 #ifdef ORTP_STATIC
136 #define ORTP_PUBLIC
137 #else
138 #ifdef ORTP_EXPORTS
139 #define ORTP_PUBLIC __declspec(dllexport)
140 #else
141 #define ORTP_PUBLIC __declspec(dllimport)
142 #endif
143 #endif
144 #pragma push_macro("_WINSOCKAPI_")
145 #ifndef _WINSOCKAPI_
146 #define _WINSOCKAPI_
147 #endif
148 
149 #define strtok_r strtok_s
150 
151 typedef unsigned __int64 uint64_t;
152 typedef __int64 int64_t;
153 typedef unsigned short uint16_t;
154 typedef unsigned int uint32_t;
155 typedef int int32_t;
156 typedef unsigned char uint8_t;
157 typedef __int16 int16_t;
158 #else
159 #include <stdint.h> /*provided by mingw32*/
160 #include <io.h>
161 #define ORTP_PUBLIC
162 ORTP_PUBLIC char* strtok_r(char *str, const char *delim, char **nextp);
163 #endif
164 
165 #define vsnprintf _vsnprintf
166 
167 typedef SOCKET ortp_socket_t;
168 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
169 typedef HANDLE ortp_cond_t;
170 typedef HANDLE ortp_mutex_t;
171 #else
172 typedef CONDITION_VARIABLE ortp_cond_t;
173 typedef SRWLOCK ortp_mutex_t;
174 #endif
175 typedef HANDLE ortp_thread_t;
176 
177 #define ortp_thread_create WIN_thread_create
178 #define ortp_thread_join WIN_thread_join
179 #define ortp_thread_self WIN_thread_self
180 #define ortp_thread_exit(arg)
181 #define ortp_mutex_init WIN_mutex_init
182 #define ortp_mutex_lock WIN_mutex_lock
183 #define ortp_mutex_unlock WIN_mutex_unlock
184 #define ortp_mutex_destroy WIN_mutex_destroy
185 #define ortp_cond_init WIN_cond_init
186 #define ortp_cond_signal WIN_cond_signal
187 #define ortp_cond_broadcast WIN_cond_broadcast
188 #define ortp_cond_wait WIN_cond_wait
189 #define ortp_cond_destroy WIN_cond_destroy
190 
191 
192 #ifdef __cplusplus
193 extern "C"
194 {
195 #endif
196 
197 ORTP_PUBLIC int WIN_mutex_init(ortp_mutex_t *m, void *attr_unused);
198 ORTP_PUBLIC int WIN_mutex_lock(ortp_mutex_t *mutex);
199 ORTP_PUBLIC int WIN_mutex_unlock(ortp_mutex_t *mutex);
200 ORTP_PUBLIC int WIN_mutex_destroy(ortp_mutex_t *mutex);
201 ORTP_PUBLIC int WIN_thread_create(ortp_thread_t *t, void *attr_unused, void *(*func)(void*), void *arg);
202 ORTP_PUBLIC int WIN_thread_join(ortp_thread_t thread, void **unused);
203 ORTP_PUBLIC unsigned long WIN_thread_self(void);
204 ORTP_PUBLIC int WIN_cond_init(ortp_cond_t *cond, void *attr_unused);
205 ORTP_PUBLIC int WIN_cond_wait(ortp_cond_t * cond, ortp_mutex_t * mutex);
206 ORTP_PUBLIC int WIN_cond_signal(ortp_cond_t * cond);
207 ORTP_PUBLIC int WIN_cond_broadcast(ortp_cond_t * cond);
208 ORTP_PUBLIC int WIN_cond_destroy(ortp_cond_t * cond);
209 
210 #ifdef __cplusplus
211 }
212 #endif
213 
214 #define SOCKET_OPTION_VALUE char *
215 #define ORTP_INLINE __inline
216 
217 #if defined(_WIN32_WCE)
218 
219 #define ortp_log10f(x) (float)log10 ((double)x)
220 
221 #ifdef assert
222  #undef assert
223 #endif /*assert*/
224 #define assert(exp) ((void)0)
225 
226 #ifdef errno
227  #undef errno
228 #endif /*errno*/
229 #define errno GetLastError()
230 #ifdef strerror
231  #undef strerror
232 #endif /*strerror*/
233 const char * ortp_strerror(DWORD value);
234 #define strerror ortp_strerror
235 
236 
237 #else /*_WIN32_WCE*/
238 
239 #define ortp_log10f(x) log10f(x)
240 
241 #endif
242 
243 ORTP_PUBLIC const char *getWinSocketError(int error);
244 #define getSocketErrorCode() WSAGetLastError()
245 #define getSocketError() getWinSocketError(WSAGetLastError())
246 
247 #define snprintf _snprintf
248 #define strcasecmp _stricmp
249 #define strncasecmp _strnicmp
250 
251 #ifndef F_OK
252 #define F_OK 00 /* Visual Studio does not define F_OK */
253 #endif
254 
255 
256 #ifdef __cplusplus
257 extern "C"{
258 #endif
259 ORTP_PUBLIC int ortp_gettimeofday (struct timeval *tv, void* tz);
260 #ifdef _WORKAROUND_MINGW32_BUGS
261 char * WSAAPI gai_strerror(int errnum);
262 #endif
263 #ifdef __cplusplus
264 }
265 #endif
266 
267 #endif
268 
269 typedef unsigned char bool_t;
270 #undef TRUE
271 #undef FALSE
272 #define TRUE 1
273 #define FALSE 0
274 
275 typedef struct _OList OList;
276 
277 typedef struct ortpTimeSpec{
278  int64_t tv_sec;
279  int64_t tv_nsec;
280 }ortpTimeSpec;
281 
282 #ifdef __cplusplus
283 extern "C"{
284 #endif
285 
286 ORTP_PUBLIC void* ortp_malloc(size_t sz);
287 ORTP_PUBLIC void ortp_free(void *ptr);
288 ORTP_PUBLIC void* ortp_realloc(void *ptr, size_t sz);
289 ORTP_PUBLIC void* ortp_malloc0(size_t sz);
290 ORTP_PUBLIC char * ortp_strdup(const char *tmp);
291 
292 /*override the allocator with this method, to be called BEFORE ortp_init()*/
293 typedef struct _OrtpMemoryFunctions{
294  void *(*malloc_fun)(size_t sz);
295  void *(*realloc_fun)(void *ptr, size_t sz);
296  void (*free_fun)(void *ptr);
298 
299 void ortp_set_memory_functions(OrtpMemoryFunctions *functions);
300 
301 #define ortp_new(type,count) (type*)ortp_malloc(sizeof(type)*(count))
302 #define ortp_new0(type,count) (type*)ortp_malloc0(sizeof(type)*(count))
303 
304 ORTP_PUBLIC int close_socket(ortp_socket_t sock);
305 ORTP_PUBLIC int set_non_blocking_socket(ortp_socket_t sock);
306 
307 ORTP_PUBLIC char *ortp_strndup(const char *str,int n);
308 ORTP_PUBLIC char *ortp_strdup_printf(const char *fmt,...);
309 ORTP_PUBLIC char *ortp_strdup_vprintf(const char *fmt, va_list ap);
310 ORTP_PUBLIC char *ortp_strcat_printf(char *dst, const char *fmt,...);
311 ORTP_PUBLIC char *ortp_strcat_vprintf(char *dst, const char *fmt, va_list ap);
312 
313 ORTP_PUBLIC int ortp_file_exist(const char *pathname);
314 
315 ORTP_PUBLIC void ortp_get_cur_time(ortpTimeSpec *ret);
316 void _ortp_get_cur_time(ortpTimeSpec *ret, bool_t realtime);
317 ORTP_PUBLIC uint64_t ortp_get_cur_time_ms(void);
318 ORTP_PUBLIC void ortp_sleep_ms(int ms);
319 ORTP_PUBLIC void ortp_sleep_until(const ortpTimeSpec *ts);
320 ORTP_PUBLIC unsigned int ortp_random(void);
321 
322 /* portable named pipes and shared memory*/
323 #if !defined(_WIN32_WCE)
324 #ifdef WIN32
325 typedef HANDLE ortp_pipe_t;
326 #define ORTP_PIPE_INVALID INVALID_HANDLE_VALUE
327 #else
328 typedef int ortp_pipe_t;
329 #define ORTP_PIPE_INVALID (-1)
330 #endif
331 
332 ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_create(const char *name);
333 /*
334  * warning: on win32 ortp_server_pipe_accept_client() might return INVALID_HANDLE_VALUE without
335  * any specific error, this happens when ortp_server_pipe_close() is called on another pipe.
336  * This pipe api is not thread-safe.
337 */
338 ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_accept_client(ortp_pipe_t server);
339 ORTP_PUBLIC int ortp_server_pipe_close(ortp_pipe_t spipe);
340 ORTP_PUBLIC int ortp_server_pipe_close_client(ortp_pipe_t client);
341 
342 ORTP_PUBLIC ortp_pipe_t ortp_client_pipe_connect(const char *name);
343 ORTP_PUBLIC int ortp_client_pipe_close(ortp_pipe_t sock);
344 
345 ORTP_PUBLIC int ortp_pipe_read(ortp_pipe_t p, uint8_t *buf, int len);
346 ORTP_PUBLIC int ortp_pipe_write(ortp_pipe_t p, const uint8_t *buf, int len);
347 
348 ORTP_PUBLIC void *ortp_shm_open(unsigned int keyid, int size, int create);
349 ORTP_PUBLIC void ortp_shm_close(void *memory);
350 
351 #endif
352 
353 #ifdef __cplusplus
354 }
355 
356 #endif
357 
358 
359 #if (defined(WIN32) || defined(_WIN32_WCE)) && !defined(ORTP_STATIC)
360 #ifdef ORTP_EXPORTS
361  #define ORTP_VAR_PUBLIC extern __declspec(dllexport)
362 #else
363  #define ORTP_VAR_PUBLIC __declspec(dllimport)
364 #endif
365 #else
366  #define ORTP_VAR_PUBLIC extern
367 #endif
368 
369 #ifndef IN6_IS_ADDR_MULTICAST
370 #define IN6_IS_ADDR_MULTICAST(i) (((uint8_t *) (i))[0] == 0xff)
371 #endif
372 
373 /*define __ios when we are compiling for ios.
374  The TARGET_OS_IPHONE macro is stupid, it is defined to 0 when compiling on mac os x.
375 */
376 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE==1
377 #define __ios 1
378 #endif
379 
380 #endif
381 
382 
Definition: port.h:277
Definition: utils.h:33
Definition: port.h:293