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