port.h 11.4 KB
Newer Older
aymeric's avatar
aymeric committed
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) implementation with additional features.
 * Copyright (C) 2017 Belledonne Communications SARL
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
aymeric's avatar
aymeric committed
19 20 21 22 23
/* this file is responsible of the portability of the stack */

#ifndef ORTP_PORT_H
#define ORTP_PORT_H

Benjamin REIS's avatar
Benjamin REIS committed
24 25 26
#if __APPLE__
#include "TargetConditionals.h"
#endif
aymeric's avatar
aymeric committed
27

28
#if !defined(_WIN32) && !defined(_WIN32_WCE)
aymeric's avatar
aymeric committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/********************************/
/* definitions for UNIX flavour */
/********************************/

#include <errno.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#ifdef __linux
#include <stdint.h>
#endif


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#if defined(_XOPEN_SOURCE_EXTENDED) || !defined(__hpux)
#include <arpa/inet.h>
#endif



#include <sys/time.h>

#include <netdb.h>

typedef int ortp_socket_t;
typedef pthread_t ortp_thread_t;
typedef pthread_mutex_t ortp_mutex_t;
typedef pthread_cond_t ortp_cond_t;

#ifdef __INTEL_COMPILER
#pragma warning(disable : 111)		// statement is unreachable
#pragma warning(disable : 181)		// argument is incompatible with corresponding format string conversion
#pragma warning(disable : 188)		// enumerated type mixed with another type
#pragma warning(disable : 593)		// variable "xxx" was set but never used
#pragma warning(disable : 810)		// conversion from "int" to "unsigned short" may lose significant bits
#pragma warning(disable : 869)		// parameter "xxx" was never referenced
#pragma warning(disable : 981)		// operands are evaluated in unspecified order
#pragma warning(disable : 1418)		// external function definition with no prior declaration
#pragma warning(disable : 1419)		// external declaration in primary source file
#pragma warning(disable : 1469)		// "cc" clobber ignored
#endif

79
#define ORTP_PUBLIC
80
#define ORTP_INLINE			inline
81

82 83 84 85 86
#ifdef __cplusplus
extern "C"
{
#endif

aymeric's avatar
aymeric committed
87
int __ortp_thread_join(ortp_thread_t thread, void **ptr);
88 89
int __ortp_thread_create(ortp_thread_t *thread, pthread_attr_t *attr, void * (*routine)(void*), void *arg);
unsigned long __ortp_thread_self(void);
90 91 92 93 94

#ifdef __cplusplus
}
#endif

aymeric's avatar
aymeric committed
95 96
#define ortp_thread_create	__ortp_thread_create
#define ortp_thread_join	__ortp_thread_join
97
#define ortp_thread_self	__ortp_thread_self
aymeric's avatar
aymeric committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#define ortp_thread_exit	pthread_exit
#define ortp_mutex_init		pthread_mutex_init
#define ortp_mutex_lock		pthread_mutex_lock
#define ortp_mutex_unlock	pthread_mutex_unlock
#define ortp_mutex_destroy	pthread_mutex_destroy
#define ortp_cond_init		pthread_cond_init
#define ortp_cond_signal	pthread_cond_signal
#define ortp_cond_broadcast	pthread_cond_broadcast
#define ortp_cond_wait		pthread_cond_wait
#define ortp_cond_destroy	pthread_cond_destroy

#define SOCKET_OPTION_VALUE	void *
#define SOCKET_BUFFER		void *

#define getSocketError() strerror(errno)
#define getSocketErrorCode() (errno)
114
#define ortp_gettimeofday(tv,tz) gettimeofday(tv,tz)
smorlat's avatar
smorlat committed
115 116 117
#define ortp_log10f(x)	log10f(x)


aymeric's avatar
aymeric committed
118 119 120 121 122 123
#else
/*********************************/
/* definitions for WIN32 flavour */
/*********************************/

#include <stdio.h>
124
#define _CRT_RAND_S
aymeric's avatar
aymeric committed
125 126 127 128
#include <stdlib.h>
#include <stdarg.h>
#include <winsock2.h>
#include <ws2tcpip.h>
129 130 131
#ifdef _MSC_VER
#include <io.h>
#endif
aymeric's avatar
aymeric committed
132

133 134 135 136 137 138 139 140 141 142
#if defined(__MINGW32__) || !defined(WINAPI_FAMILY_PARTITION) || !defined(WINAPI_PARTITION_DESKTOP)
#define ORTP_WINDOWS_DESKTOP 1
#elif defined(WINAPI_FAMILY_PARTITION)
#if defined(WINAPI_PARTITION_DESKTOP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
#define ORTP_WINDOWS_DESKTOP 1
#elif defined(WINAPI_PARTITION_PHONE_APP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)
#define ORTP_WINDOWS_PHONE 1
#elif defined(WINAPI_PARTITION_APP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
#define ORTP_WINDOWS_UNIVERSAL 1
#endif
143 144
#endif

aymeric's avatar
aymeric committed
145
#ifdef _MSC_VER
146 147 148
#ifdef ORTP_STATIC
#define ORTP_PUBLIC
#else
Sylvain Berfini's avatar
Sylvain Berfini committed
149
#ifdef ORTP_EXPORTS
150
#define ORTP_PUBLIC	__declspec(dllexport)
151
#else
Sylvain Berfini's avatar
Sylvain Berfini committed
152 153
#define ORTP_PUBLIC	__declspec(dllimport)
#endif
154
#endif
aymeric's avatar
aymeric committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168
#pragma push_macro("_WINSOCKAPI_")
#ifndef _WINSOCKAPI_
#define _WINSOCKAPI_
#endif

typedef  unsigned __int64 uint64_t;
typedef  __int64 int64_t;
typedef  unsigned short uint16_t;
typedef  unsigned int uint32_t;
typedef  int int32_t;
typedef  unsigned char uint8_t;
typedef __int16 int16_t;
#else
#include <stdint.h> /*provided by mingw32*/
169
#include <io.h>
170
#define ORTP_PUBLIC
171
ORTP_PUBLIC char* strtok_r(char *str, const char *delim, char **nextp);
aymeric's avatar
aymeric committed
172 173 174 175 176
#endif

#define vsnprintf	_vsnprintf

typedef SOCKET ortp_socket_t;
177
#ifdef ORTP_WINDOWS_DESKTOP
aymeric's avatar
aymeric committed
178 179
typedef HANDLE ortp_cond_t;
typedef HANDLE ortp_mutex_t;
180 181 182
#else
typedef CONDITION_VARIABLE ortp_cond_t;
typedef SRWLOCK ortp_mutex_t;
Ghislain MARY's avatar
Ghislain MARY committed
183
#endif
aymeric's avatar
aymeric committed
184 185 186 187
typedef HANDLE ortp_thread_t;

#define ortp_thread_create	WIN_thread_create
#define ortp_thread_join	WIN_thread_join
188
#define ortp_thread_self	WIN_thread_self
Jehan Monnier's avatar
Jehan Monnier committed
189
#define ortp_thread_exit(arg)
aymeric's avatar
aymeric committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
#define ortp_mutex_init		WIN_mutex_init
#define ortp_mutex_lock		WIN_mutex_lock
#define ortp_mutex_unlock	WIN_mutex_unlock
#define ortp_mutex_destroy	WIN_mutex_destroy
#define ortp_cond_init		WIN_cond_init
#define ortp_cond_signal	WIN_cond_signal
#define ortp_cond_broadcast	WIN_cond_broadcast
#define ortp_cond_wait		WIN_cond_wait
#define ortp_cond_destroy	WIN_cond_destroy


#ifdef __cplusplus
extern "C"
{
#endif
Jehan Monnier's avatar
Jehan Monnier committed
205

206 207 208 209 210 211
ORTP_PUBLIC int WIN_mutex_init(ortp_mutex_t *m, void *attr_unused);
ORTP_PUBLIC int WIN_mutex_lock(ortp_mutex_t *mutex);
ORTP_PUBLIC int WIN_mutex_unlock(ortp_mutex_t *mutex);
ORTP_PUBLIC int WIN_mutex_destroy(ortp_mutex_t *mutex);
ORTP_PUBLIC int WIN_thread_create(ortp_thread_t *t, void *attr_unused, void *(*func)(void*), void *arg);
ORTP_PUBLIC int WIN_thread_join(ortp_thread_t thread, void **unused);
212
ORTP_PUBLIC unsigned long WIN_thread_self(void);
213 214 215 216 217
ORTP_PUBLIC int WIN_cond_init(ortp_cond_t *cond, void *attr_unused);
ORTP_PUBLIC int WIN_cond_wait(ortp_cond_t * cond, ortp_mutex_t * mutex);
ORTP_PUBLIC int WIN_cond_signal(ortp_cond_t * cond);
ORTP_PUBLIC int WIN_cond_broadcast(ortp_cond_t * cond);
ORTP_PUBLIC int WIN_cond_destroy(ortp_cond_t * cond);
aymeric's avatar
aymeric committed
218 219 220 221 222 223

#ifdef __cplusplus
}
#endif

#define SOCKET_OPTION_VALUE	char *
224
#define ORTP_INLINE			__inline
aymeric's avatar
aymeric committed
225

Jehan Monnier's avatar
Jehan Monnier committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
#if defined(_WIN32_WCE)

#define ortp_log10f(x)		(float)log10 ((double)x)

#ifdef assert
	#undef assert
#endif /*assert*/
#define assert(exp)	((void)0)

#ifdef errno
	#undef errno
#endif /*errno*/
#define  errno GetLastError()
#ifdef strerror
		#undef strerror
#endif /*strerror*/
const char * ortp_strerror(DWORD value);
#define strerror ortp_strerror


smorlat's avatar
smorlat committed
246 247 248 249 250
#else /*_WIN32_WCE*/

#define ortp_log10f(x)	log10f(x)

#endif
Jehan Monnier's avatar
Jehan Monnier committed
251

252 253 254 255 256
#ifdef __cplusplus
extern "C" {
#endif
	
	ORTP_PUBLIC const char *getWinSocketError(int error);
257
#ifndef getSocketErrorCode
aymeric's avatar
aymeric committed
258
#define getSocketErrorCode() WSAGetLastError()
259 260
#endif
#ifndef getSocketError
aymeric's avatar
aymeric committed
261
#define getSocketError() getWinSocketError(WSAGetLastError())
262
#endif
aymeric's avatar
aymeric committed
263

264 265 266 267
#ifndef F_OK
#define F_OK 00 /* Visual Studio does not define F_OK */
#endif

268

269
ORTP_PUBLIC int ortp_gettimeofday (struct timeval *tv, void* tz);
aymeric's avatar
aymeric committed
270 271 272
#ifdef _WORKAROUND_MINGW32_BUGS
char * WSAAPI gai_strerror(int errnum);
#endif
273

274 275 276
#ifdef __cplusplus
}
#endif
aymeric's avatar
aymeric committed
277 278 279

#endif

280 281
#ifndef _BOOL_T_
#define _BOOL_T_
aymeric's avatar
aymeric committed
282
typedef unsigned char bool_t;
283
#endif /* _BOOL_T_ */
aymeric's avatar
aymeric committed
284 285 286 287 288
#undef TRUE
#undef FALSE
#define TRUE 1
#define FALSE 0

289

290 291 292 293 294
typedef struct ortpTimeSpec{
	int64_t tv_sec;
	int64_t tv_nsec;
}ortpTimeSpec;

aymeric's avatar
aymeric committed
295 296 297 298
#ifdef __cplusplus
extern "C"{
#endif

299 300 301 302 303
ORTP_PUBLIC void* ortp_malloc(size_t sz);
ORTP_PUBLIC void ortp_free(void *ptr);
ORTP_PUBLIC void* ortp_realloc(void *ptr, size_t sz);
ORTP_PUBLIC void* ortp_malloc0(size_t sz);
ORTP_PUBLIC char * ortp_strdup(const char *tmp);
aymeric's avatar
aymeric committed
304 305 306 307 308 309 310 311 312 313

/*override the allocator with this method, to be called BEFORE ortp_init()*/
typedef struct _OrtpMemoryFunctions{
	void *(*malloc_fun)(size_t sz);
	void *(*realloc_fun)(void *ptr, size_t sz);
	void (*free_fun)(void *ptr);
}OrtpMemoryFunctions;

void ortp_set_memory_functions(OrtpMemoryFunctions *functions);

Simon Morlat's avatar
Simon Morlat committed
314 315
#define ortp_new(type,count)	(type*)ortp_malloc(sizeof(type)*(count))
#define ortp_new0(type,count)	(type*)ortp_malloc0(sizeof(type)*(count))
aymeric's avatar
aymeric committed
316

317 318
ORTP_PUBLIC int close_socket(ortp_socket_t sock);
ORTP_PUBLIC int set_non_blocking_socket(ortp_socket_t sock);
aymeric's avatar
aymeric committed
319

320
ORTP_PUBLIC char *ortp_strndup(const char *str,int n);
321 322 323 324 325 326 327
#define ortp_strdup_printf bctbx_strdup_printf
/*ORTP_PUBLIC char *ortp_strdup_printf(const char *fmt,...);*/
#define ortp_strdup_vprintf bctbx_strdup_vprintf
/*ORTP_PUBLIC char *ortp_strdup_vprintf(const char *fmt, va_list ap);*/
#define ortp_strcat_printf bctbx_strcat_printf
/*ORTP_PUBLIC char *ortp_strcat_printf(char *dst, const char *fmt,...);*/

328
ORTP_PUBLIC char *ortp_strcat_vprintf(char *dst, const char *fmt, va_list ap);
aymeric's avatar
aymeric committed
329

330
ORTP_PUBLIC int ortp_file_exist(const char *pathname);
Jehan Monnier's avatar
Jehan Monnier committed
331

332
ORTP_PUBLIC void ortp_get_cur_time(ortpTimeSpec *ret);
333
void _ortp_get_cur_time(ortpTimeSpec *ret, bool_t realtime);
334
ORTP_PUBLIC uint64_t ortp_get_cur_time_ms(void);
335 336
ORTP_PUBLIC void ortp_sleep_ms(int ms);
ORTP_PUBLIC void ortp_sleep_until(const ortpTimeSpec *ts);
337
ORTP_PUBLIC int ortp_timespec_compare(const ortpTimeSpec *s1, const ortpTimeSpec *s2);
338
ORTP_PUBLIC unsigned int ortp_random(void);
339

Simon Morlat's avatar
Simon Morlat committed
340
/* portable named pipes  and shared memory*/
aymeric's avatar
aymeric committed
341
#if !defined(_WIN32_WCE)
342
#ifdef _WIN32
343 344 345 346 347 348 349
typedef HANDLE ortp_pipe_t;
#define ORTP_PIPE_INVALID INVALID_HANDLE_VALUE
#else
typedef int ortp_pipe_t;
#define ORTP_PIPE_INVALID (-1)
#endif

350
ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_create(const char *name);
351 352 353 354 355
/*
 * warning: on win32 ortp_server_pipe_accept_client() might return INVALID_HANDLE_VALUE without
 * any specific error, this happens when ortp_server_pipe_close() is called on another pipe.
 * This pipe api is not thread-safe.
*/
356 357 358
ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_accept_client(ortp_pipe_t server);
ORTP_PUBLIC int ortp_server_pipe_close(ortp_pipe_t spipe);
ORTP_PUBLIC int ortp_server_pipe_close_client(ortp_pipe_t client);
359

360 361
ORTP_PUBLIC ortp_pipe_t ortp_client_pipe_connect(const char *name);
ORTP_PUBLIC int ortp_client_pipe_close(ortp_pipe_t sock);
362

363 364
ORTP_PUBLIC int ortp_pipe_read(ortp_pipe_t p, uint8_t *buf, int len);
ORTP_PUBLIC int ortp_pipe_write(ortp_pipe_t p, const uint8_t *buf, int len);
Simon Morlat's avatar
Simon Morlat committed
365

366 367
ORTP_PUBLIC void *ortp_shm_open(unsigned int keyid, int size, int create);
ORTP_PUBLIC void ortp_shm_close(void *memory);
Simon Morlat's avatar
Simon Morlat committed
368

369 370 371
ORTP_PUBLIC	bool_t ortp_is_multicast_addr(const struct sockaddr *addr);
	
	
aymeric's avatar
aymeric committed
372
#endif
373

aymeric's avatar
aymeric committed
374 375
#ifdef __cplusplus
}
Jehan Monnier's avatar
Jehan Monnier committed
376

aymeric's avatar
aymeric committed
377 378 379
#endif


380
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(ORTP_STATIC)
aymeric's avatar
aymeric committed
381
#ifdef ORTP_EXPORTS
382
   #define ORTP_VAR_PUBLIC    extern __declspec(dllexport)
aymeric's avatar
aymeric committed
383
#else
384
   #define ORTP_VAR_PUBLIC    __declspec(dllimport)
aymeric's avatar
aymeric committed
385 386
#endif
#else
387
   #define ORTP_VAR_PUBLIC    extern
aymeric's avatar
aymeric committed
388 389
#endif

390 391 392
#ifndef IN6_IS_ADDR_MULTICAST
#define IN6_IS_ADDR_MULTICAST(i)	(((uint8_t *) (i))[0] == 0xff)
#endif
aymeric's avatar
aymeric committed
393

Simon Morlat's avatar
Simon Morlat committed
394 395 396
/*define __ios when we are compiling for ios.
 The TARGET_OS_IPHONE macro is stupid, it is defined to 0 when compiling on mac os x.
*/
Benjamin REIS's avatar
Benjamin REIS committed
397
#if TARGET_OS_IPHONE
Simon Morlat's avatar
Simon Morlat committed
398 399 400
#define __ios 1
#endif

aymeric's avatar
aymeric committed
401 402 403
#endif