a-oRTP  5.2.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 #if !defined(WIN32) && !defined(_WIN32_WCE)
25 /********************************/
26 /* definitions for UNIX flavour */
27 /********************************/
28 
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <pthread.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <string.h>
38 
39 #ifdef __linux
40 #include <stdint.h>
41 #endif
42 
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #if defined(_XOPEN_SOURCE_EXTENDED) || !defined(__hpux)
47 #include <arpa/inet.h>
48 #endif
49 
50 #include <sys/time.h>
51 
52 #include <netdb.h>
53 
54 typedef int ortp_socket_t;
55 typedef pthread_t ortp_thread_t;
56 typedef pthread_mutex_t ortp_mutex_t;
57 typedef pthread_cond_t ortp_cond_t;
58 
59 #ifdef __INTEL_COMPILER
60 #pragma warning(disable : 111) // statement is unreachable
61 #pragma warning(disable : 181) // argument is incompatible with corresponding format string conversion
62 #pragma warning(disable : 188) // enumerated type mixed with another type
63 #pragma warning(disable : 593) // variable "xxx" was set but never used
64 #pragma warning(disable : 810) // conversion from "int" to "unsigned short" may lose significant bits
65 #pragma warning(disable : 869) // parameter "xxx" was never referenced
66 #pragma warning(disable : 981) // operands are evaluated in unspecified order
67 #pragma warning(disable : 1418) // external function definition with no prior declaration
68 #pragma warning(disable : 1419) // external declaration in primary source file
69 #pragma warning(disable : 1469) // "cc" clobber ignored
70 #endif
71 
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
76 int __ortp_thread_join(ortp_thread_t thread, void **ptr);
77 int __ortp_thread_create(pthread_t *thread, pthread_attr_t *attr, void *(*routine)(void *), void *arg);
78 int __ortp_thread_set_name(pthread_t thread, const char *name);
79 
80 #ifdef __cplusplus
81 }
82 #endif
83 
84 #define ortp_thread_create __ortp_thread_create
85 #define ortp_thread_join __ortp_thread_join
86 #define ortp_thread_exit pthread_exit
87 #define ortp_thread_set_name __ortp_thread_set_name
88 #define ortp_mutex_init pthread_mutex_init
89 #define ortp_mutex_lock pthread_mutex_lock
90 #define ortp_mutex_unlock pthread_mutex_unlock
91 #define ortp_mutex_destroy pthread_mutex_destroy
92 #define ortp_cond_init pthread_cond_init
93 #define ortp_cond_signal pthread_cond_signal
94 #define ortp_cond_broadcast pthread_cond_broadcast
95 #define ortp_cond_wait pthread_cond_wait
96 #define ortp_cond_destroy pthread_cond_destroy
97 
98 #define SOCKET_OPTION_VALUE void *
99 #define SOCKET_BUFFER void *
100 
101 #define getSocketErrorCode() (errno)
102 
103 #define ortp_log10f(x) log10f(x)
104 
105 #else
106 /*********************************/
107 /* definitions for WIN32 flavour */
108 /*********************************/
109 
110 #ifdef _DEBUG
111 #define ORTP_MEMORY_DEBUG
112 #endif
113 
114 #include <stdio.h>
115 #include <stdlib.h>
116 #include <stdarg.h>
117 #if (_MSC_VER >= 1700) && !defined(_USING_V110_SDK71_)
118 #include <winapifamily.h>
119 #endif
120 
121 #include <winsock2.h>
122 #include <ws2tcpip.h>
123 #ifndef _INC_WINDOWS
124 #include <windows.h>
125 #endif
126 
127 #ifdef _MSC_VER
128 #pragma push_macro("_WINSOCKAPI_")
129 #ifndef _WINSOCKAPI_
130 #define _WINSOCKAPI_
131 #endif
132 
133 #include <stdint.h>
134 
135 #else
136 #include <stdint.h> /*provided by mingw32*/
137 #endif
138 
139 #define vsnprintf _vsnprintf
140 #define srandom srand
141 #define random rand
142 
143 #if (_MSC_VER >= 1700) && !defined(_USING_V110_SDK71_)
144 #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
145 typedef SOCKET ortp_socket_t;
146 #elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)
147 typedef void *ortp_socket_t;
148 #else
149 typedef SOCKET ortp_socket_t;
150 #endif
151 #else
152 typedef SOCKET ortp_socket_t;
153 #endif
154 
155 typedef HANDLE ortp_cond_t;
156 typedef HANDLE ortp_mutex_t;
157 typedef HANDLE ortp_thread_t;
158 
159 #define ortp_thread_create WIN_thread_create
160 #define ortp_thread_join WIN_thread_join
161 #define ortp_thread_exit(arg)
162 #define ortp_thread_set_name WIN_thread_set_name
163 #define ortp_mutex_init WIN_mutex_init
164 #define ortp_mutex_lock WIN_mutex_lock
165 #define ortp_mutex_unlock WIN_mutex_unlock
166 #define ortp_mutex_destroy WIN_mutex_destroy
167 #define ortp_cond_init WIN_cond_init
168 #define ortp_cond_signal WIN_cond_signal
169 #define ortp_cond_broadcast WIN_cond_broadcast
170 #define ortp_cond_wait WIN_cond_wait
171 #define ortp_cond_destroy WIN_cond_destroy
172 
173 #ifdef __cplusplus
174 extern "C" {
175 #endif
176 
177 int WIN_mutex_init(ortp_mutex_t *m, void *attr_unused);
178 int WIN_mutex_lock(ortp_mutex_t *mutex);
179 int WIN_mutex_unlock(ortp_mutex_t *mutex);
180 int WIN_mutex_destroy(ortp_mutex_t *mutex);
181 int WIN_thread_create(ortp_thread_t *t, void *attr_unused, void *(*func)(void *), void *arg);
182 int WIN_thread_join(ortp_thread_t thread, void **unused);
183 int WIN_thread_set_name(ortp_thread_t thread, const char *name);
184 int WIN_cond_init(ortp_cond_t *cond, void *attr_unused);
185 int WIN_cond_wait(ortp_cond_t *cond, ortp_mutex_t *mutex);
186 int WIN_cond_signal(ortp_cond_t *cond);
187 int WIN_cond_broadcast(ortp_cond_t *cond);
188 int WIN_cond_destroy(ortp_cond_t *cond);
189 
190 #ifdef __cplusplus
191 }
192 #endif
193 
194 #define SOCKET_OPTION_VALUE char *
195 /* required for visual studio 2012 */
196 #define _ALLOW_KEYWORD_MACROS
197 #define inline __inline
198 
199 #if defined(_WIN32_WCE)
200 
201 #define ortp_log10f(x) (float) log10((double) x)
202 
203 #ifdef assert
204 #undef assert
205 #endif /*assert*/
206 #define assert(exp) ((void) 0)
207 
208 #ifdef errno
209 #undef errno
210 #endif /*errno*/
211 #define errno GetLastError()
212 #ifdef strerror
213 #undef strerror
214 #endif /*strerror*/
215 
216 #else /*_WIN32_WCE*/
217 
218 #define ortp_log10f(x) log10f(x)
219 
220 #endif
221 
222 #define getSocketErrorCode() WSAGetLastError()
223 
224 #if (_MSC_VER < 1900)
225 #define snprintf _snprintf
226 #endif
227 #define strcasecmp _stricmp
228 
229 #if 0
230 struct timeval {
231  long tv_sec; /* seconds */
232  long tv_usec; /* and microseconds */
233 };
234 #endif
235 
236 #ifdef __cplusplus
237 extern "C" {
238 #endif
239 int gettimeofday(struct timeval *tv, void *tz);
240 #ifdef __cplusplus
241 }
242 #endif
243 
244 #endif
245 
246 typedef unsigned char bool_t;
247 #undef TRUE
248 #undef FALSE
249 #define TRUE 1
250 #define FALSE 0
251 
252 #ifdef __cplusplus
253 extern "C" {
254 #endif
255 
256 #ifdef ORTP_MEMORY_DEBUG
257 /* for windows test purpose */
258 void *ortp_malloc(size_t size, char *file, unsigned short line);
259 void *ortp_malloc0(size_t size, char *file, unsigned short line);
260 void ortp_free(void *ptr);
261 void *ortp_realloc(void *ptr, size_t size, char *file, unsigned short line);
262 
263 #ifndef ortp_malloc
264 #define ortp_malloc(S) ortp_malloc(S, __FILE__, __LINE__)
265 #endif
266 #ifndef ortp_malloc0
267 #define ortp_malloc0(S) ortp_malloc0(S, __FILE__, __LINE__)
268 #endif
269 #ifndef ortp_realloc
270 #define ortp_realloc(P, S) ortp_realloc(P, S, __FILE__, __LINE__)
271 #endif
272 #ifndef ortp_free
273 #define ortp_free(P) \
274  { \
275  if (P != NULL) { \
276  ortp_free(P); \
277  } \
278  }
279 #endif
280 #else
281 void *ortp_malloc(size_t sz);
282 void *ortp_malloc0(size_t sz);
283 void ortp_free(void *ptr);
284 void *ortp_realloc(void *ptr, size_t sz);
285 #endif
286 
287 char *ortp_strdup(const char *tmp);
288 
289 /*override the allocator with this method, to be called BEFORE ortp_init()*/
290 typedef struct _OrtpMemoryFunctions {
291  void *(*malloc_fun)(size_t sz);
292  void *(*realloc_fun)(void *ptr, size_t sz);
293  void (*free_fun)(void *ptr);
295 
296 void ortp_set_memory_functions(OrtpMemoryFunctions *functions);
297 
298 #define ortp_new(type, count) (type *) ortp_malloc(sizeof(type) * (count))
299 #define ortp_new0(type, count) (type *) ortp_malloc0(sizeof(type) * (count))
300 
301 int set_non_blocking_socket(ortp_socket_t sock);
302 
303 char *ortp_strndup(const char *str, int n);
304 char *ortp_strdup_printf(const char *fmt, ...);
305 char *ortp_strdup_vprintf(const char *fmt, va_list ap);
306 
307 int ortp_file_exist(const char *pathname);
308 
309 /* portable named pipes */
310 #if !defined(_WIN32_WCE)
311 #ifdef WIN32
312 typedef HANDLE ortp_pipe_t;
313 #define ORTP_PIPE_INVALID INVALID_HANDLE_VALUE
314 #else
315 typedef int ortp_pipe_t;
316 #define ORTP_PIPE_INVALID (-1)
317 #endif
318 
319 ortp_pipe_t ortp_server_pipe_create(const char *name);
320 /*
321  * warning: on win32 ortp_server_pipe_accept_client() might return INVALID_HANDLE_VALUE without
322  * any specific error, this happens when ortp_server_pipe_close() is called on another pipe.
323  * This pipe api is not thread-safe.
324  */
325 ortp_pipe_t ortp_server_pipe_accept_client(ortp_pipe_t server);
326 int ortp_server_pipe_close(ortp_pipe_t spipe);
327 int ortp_server_pipe_close_client(ortp_pipe_t client);
328 
329 ortp_pipe_t ortp_client_pipe_connect(const char *name);
330 int ortp_client_pipe_close(ortp_pipe_t sock);
331 
332 int ortp_pipe_read(ortp_pipe_t p, uint8_t *buf, int len);
333 int ortp_pipe_write(ortp_pipe_t p, const uint8_t *buf, int len);
334 #endif
335 
336 /* I advise to use ERRBSIZ as a size */
337 #define ERRBSIZ 64
338 char *ortp_strerror(int errnum, char *buf, size_t buflen);
339 char *ortp_gai_strerror(int errnum, char *buf, size_t buflen);
340 
341 #if defined(WIN32) || defined(_WIN32_WCE)
342 const char *ortp_inet_ntop(int af, const void *src, char *dst, size_t size);
343 #else
344 #define ortp_inet_ntop inet_ntop
345 #endif
346 
347 #ifdef __cplusplus
348 }
349 #endif
350 
351 #if (defined(WIN32) || defined(_WIN32_WCE)) && !defined(ORTP_STATIC)
352 #ifdef ORTP_EXPORTS
353 #ifdef __cplusplus
354 #define VAR_DECLSPEC extern "C" __declspec(dllexport)
355 #else
356 #define VAR_DECLSPEC __declspec(dllexport)
357 #endif
358 #else
359 #define VAR_DECLSPEC __declspec(dllimport)
360 #endif
361 #else
362 #define VAR_DECLSPEC extern
363 #endif
364 
365 #endif
Definition: port.h:290