oRTP  0.24.0
include/ortp/port.h
00001 /*
00002   The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack.
00003   Copyright (C) 2001  Simon MORLAT simon.morlat@linphone.org
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the Free Software
00017   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 /* this file is responsible of the portability of the stack */
00020 
00021 #ifndef ORTP_PORT_H
00022 #define ORTP_PORT_H
00023 
00024 
00025 #if !defined(WIN32) && !defined(_WIN32_WCE)
00026 /********************************/
00027 /* definitions for UNIX flavour */
00028 /********************************/
00029 
00030 #include <errno.h>
00031 #include <sys/types.h>
00032 #include <pthread.h>
00033 #include <unistd.h>
00034 #include <fcntl.h>
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <stdarg.h>
00038 #include <string.h>
00039 
00040 #ifdef __linux
00041 #include <stdint.h>
00042 #endif
00043 
00044 
00045 #include <sys/types.h>
00046 #include <sys/socket.h>
00047 #include <netinet/in.h>
00048 #if defined(_XOPEN_SOURCE_EXTENDED) || !defined(__hpux)
00049 #include <arpa/inet.h>
00050 #endif
00051 
00052 
00053 
00054 #include <sys/time.h>
00055 
00056 #include <netdb.h>
00057 
00058 typedef int ortp_socket_t;
00059 typedef pthread_t ortp_thread_t;
00060 typedef pthread_mutex_t ortp_mutex_t;
00061 typedef pthread_cond_t ortp_cond_t;
00062 
00063 #ifdef __INTEL_COMPILER
00064 #pragma warning(disable : 111)          // statement is unreachable
00065 #pragma warning(disable : 181)          // argument is incompatible with corresponding format string conversion
00066 #pragma warning(disable : 188)          // enumerated type mixed with another type
00067 #pragma warning(disable : 593)          // variable "xxx" was set but never used
00068 #pragma warning(disable : 810)          // conversion from "int" to "unsigned short" may lose significant bits
00069 #pragma warning(disable : 869)          // parameter "xxx" was never referenced
00070 #pragma warning(disable : 981)          // operands are evaluated in unspecified order
00071 #pragma warning(disable : 1418)         // external function definition with no prior declaration
00072 #pragma warning(disable : 1419)         // external declaration in primary source file
00073 #pragma warning(disable : 1469)         // "cc" clobber ignored
00074 #endif
00075 
00076 #define ORTP_PUBLIC
00077 #define ORTP_INLINE                     inline
00078 
00079 #define WINAPI_FAMILY_PARTITION(x) 1
00080 
00081 #ifdef __cplusplus
00082 extern "C"
00083 {
00084 #endif
00085 
00086 int __ortp_thread_join(ortp_thread_t thread, void **ptr);
00087 int __ortp_thread_create(ortp_thread_t *thread, pthread_attr_t *attr, void * (*routine)(void*), void *arg);
00088 unsigned long __ortp_thread_self(void);
00089 
00090 #ifdef __cplusplus
00091 }
00092 #endif
00093 
00094 #define ortp_thread_create      __ortp_thread_create
00095 #define ortp_thread_join        __ortp_thread_join
00096 #define ortp_thread_self        __ortp_thread_self
00097 #define ortp_thread_exit        pthread_exit
00098 #define ortp_mutex_init         pthread_mutex_init
00099 #define ortp_mutex_lock         pthread_mutex_lock
00100 #define ortp_mutex_unlock       pthread_mutex_unlock
00101 #define ortp_mutex_destroy      pthread_mutex_destroy
00102 #define ortp_cond_init          pthread_cond_init
00103 #define ortp_cond_signal        pthread_cond_signal
00104 #define ortp_cond_broadcast     pthread_cond_broadcast
00105 #define ortp_cond_wait          pthread_cond_wait
00106 #define ortp_cond_destroy       pthread_cond_destroy
00107 
00108 #define SOCKET_OPTION_VALUE     void *
00109 #define SOCKET_BUFFER           void *
00110 
00111 #define getSocketError() strerror(errno)
00112 #define getSocketErrorCode() (errno)
00113 #define ortp_gettimeofday(tv,tz) gettimeofday(tv,tz)
00114 #define ortp_log10f(x)  log10f(x)
00115 
00116 
00117 #else
00118 /*********************************/
00119 /* definitions for WIN32 flavour */
00120 /*********************************/
00121 
00122 #include <stdio.h>
00123 #define _CRT_RAND_S
00124 #include <stdlib.h>
00125 #include <stdarg.h>
00126 #include <winsock2.h>
00127 #include <ws2tcpip.h>
00128 
00129 #if defined(__MINGW32__) || !defined(WINAPI_FAMILY_PARTITION)
00130 // Only use with x being WINAPI_PARTITION_DESKTOP to test if building on desktop
00131 #define WINAPI_FAMILY_PARTITION(x) 1
00132 #endif
00133 
00134 #ifdef _MSC_VER
00135 #ifdef ORTP_STATIC
00136 #define ORTP_PUBLIC
00137 #else
00138 #ifdef ORTP_EXPORTS
00139 #define ORTP_PUBLIC     __declspec(dllexport)
00140 #else
00141 #define ORTP_PUBLIC     __declspec(dllimport)
00142 #endif
00143 #endif
00144 #pragma push_macro("_WINSOCKAPI_")
00145 #ifndef _WINSOCKAPI_
00146 #define _WINSOCKAPI_
00147 #endif
00148 
00149 #define strtok_r strtok_s
00150 
00151 typedef  unsigned __int64 uint64_t;
00152 typedef  __int64 int64_t;
00153 typedef  unsigned short uint16_t;
00154 typedef  unsigned int uint32_t;
00155 typedef  int int32_t;
00156 typedef  unsigned char uint8_t;
00157 typedef __int16 int16_t;
00158 #else
00159 #include <stdint.h> /*provided by mingw32*/
00160 #include <io.h>
00161 #define ORTP_PUBLIC
00162 ORTP_PUBLIC char* strtok_r(char *str, const char *delim, char **nextp);
00163 #endif
00164 
00165 #define vsnprintf       _vsnprintf
00166 
00167 typedef SOCKET ortp_socket_t;
00168 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
00169 typedef HANDLE ortp_cond_t;
00170 typedef HANDLE ortp_mutex_t;
00171 #else
00172 typedef CONDITION_VARIABLE ortp_cond_t;
00173 typedef SRWLOCK ortp_mutex_t;
00174 #endif
00175 typedef HANDLE ortp_thread_t;
00176 
00177 #define ortp_thread_create      WIN_thread_create
00178 #define ortp_thread_join        WIN_thread_join
00179 #define ortp_thread_self        WIN_thread_self
00180 #define ortp_thread_exit(arg)
00181 #define ortp_mutex_init         WIN_mutex_init
00182 #define ortp_mutex_lock         WIN_mutex_lock
00183 #define ortp_mutex_unlock       WIN_mutex_unlock
00184 #define ortp_mutex_destroy      WIN_mutex_destroy
00185 #define ortp_cond_init          WIN_cond_init
00186 #define ortp_cond_signal        WIN_cond_signal
00187 #define ortp_cond_broadcast     WIN_cond_broadcast
00188 #define ortp_cond_wait          WIN_cond_wait
00189 #define ortp_cond_destroy       WIN_cond_destroy
00190 
00191 
00192 #ifdef __cplusplus
00193 extern "C"
00194 {
00195 #endif
00196 
00197 ORTP_PUBLIC int WIN_mutex_init(ortp_mutex_t *m, void *attr_unused);
00198 ORTP_PUBLIC int WIN_mutex_lock(ortp_mutex_t *mutex);
00199 ORTP_PUBLIC int WIN_mutex_unlock(ortp_mutex_t *mutex);
00200 ORTP_PUBLIC int WIN_mutex_destroy(ortp_mutex_t *mutex);
00201 ORTP_PUBLIC int WIN_thread_create(ortp_thread_t *t, void *attr_unused, void *(*func)(void*), void *arg);
00202 ORTP_PUBLIC int WIN_thread_join(ortp_thread_t thread, void **unused);
00203 ORTP_PUBLIC unsigned long WIN_thread_self(void);
00204 ORTP_PUBLIC int WIN_cond_init(ortp_cond_t *cond, void *attr_unused);
00205 ORTP_PUBLIC int WIN_cond_wait(ortp_cond_t * cond, ortp_mutex_t * mutex);
00206 ORTP_PUBLIC int WIN_cond_signal(ortp_cond_t * cond);
00207 ORTP_PUBLIC int WIN_cond_broadcast(ortp_cond_t * cond);
00208 ORTP_PUBLIC int WIN_cond_destroy(ortp_cond_t * cond);
00209 
00210 #ifdef __cplusplus
00211 }
00212 #endif
00213 
00214 #define SOCKET_OPTION_VALUE     char *
00215 #define ORTP_INLINE                     __inline
00216 
00217 #if defined(_WIN32_WCE)
00218 
00219 #define ortp_log10f(x)          (float)log10 ((double)x)
00220 
00221 #ifdef assert
00222         #undef assert
00223 #endif /*assert*/
00224 #define assert(exp)     ((void)0)
00225 
00226 #ifdef errno
00227         #undef errno
00228 #endif /*errno*/
00229 #define  errno GetLastError()
00230 #ifdef strerror
00231                 #undef strerror
00232 #endif /*strerror*/
00233 const char * ortp_strerror(DWORD value);
00234 #define strerror ortp_strerror
00235 
00236 
00237 #else /*_WIN32_WCE*/
00238 
00239 #define ortp_log10f(x)  log10f(x)
00240 
00241 #endif
00242 
00243 ORTP_PUBLIC const char *getWinSocketError(int error);
00244 #define getSocketErrorCode() WSAGetLastError()
00245 #define getSocketError() getWinSocketError(WSAGetLastError())
00246 
00247 #define snprintf _snprintf
00248 #define strcasecmp _stricmp
00249 #define strncasecmp _strnicmp
00250 
00251 #ifndef F_OK
00252 #define F_OK 00 /* Visual Studio does not define F_OK */
00253 #endif
00254 
00255 
00256 #ifdef __cplusplus
00257 extern "C"{
00258 #endif
00259 ORTP_PUBLIC int ortp_gettimeofday (struct timeval *tv, void* tz);
00260 #ifdef _WORKAROUND_MINGW32_BUGS
00261 char * WSAAPI gai_strerror(int errnum);
00262 #endif
00263 #ifdef __cplusplus
00264 }
00265 #endif
00266 
00267 #endif
00268 
00269 typedef unsigned char bool_t;
00270 #undef TRUE
00271 #undef FALSE
00272 #define TRUE 1
00273 #define FALSE 0
00274 
00275 typedef struct _OList OList;
00276 
00277 typedef struct ortpTimeSpec{
00278         int64_t tv_sec;
00279         int64_t tv_nsec;
00280 }ortpTimeSpec;
00281 
00282 #ifdef __cplusplus
00283 extern "C"{
00284 #endif
00285 
00286 ORTP_PUBLIC void* ortp_malloc(size_t sz);
00287 ORTP_PUBLIC void ortp_free(void *ptr);
00288 ORTP_PUBLIC void* ortp_realloc(void *ptr, size_t sz);
00289 ORTP_PUBLIC void* ortp_malloc0(size_t sz);
00290 ORTP_PUBLIC char * ortp_strdup(const char *tmp);
00291 
00292 /*override the allocator with this method, to be called BEFORE ortp_init()*/
00293 typedef struct _OrtpMemoryFunctions{
00294         void *(*malloc_fun)(size_t sz);
00295         void *(*realloc_fun)(void *ptr, size_t sz);
00296         void (*free_fun)(void *ptr);
00297 }OrtpMemoryFunctions;
00298 
00299 void ortp_set_memory_functions(OrtpMemoryFunctions *functions);
00300 
00301 #define ortp_new(type,count)    (type*)ortp_malloc(sizeof(type)*(count))
00302 #define ortp_new0(type,count)   (type*)ortp_malloc0(sizeof(type)*(count))
00303 
00304 ORTP_PUBLIC int close_socket(ortp_socket_t sock);
00305 ORTP_PUBLIC int set_non_blocking_socket(ortp_socket_t sock);
00306 
00307 ORTP_PUBLIC char *ortp_strndup(const char *str,int n);
00308 ORTP_PUBLIC char *ortp_strdup_printf(const char *fmt,...);
00309 ORTP_PUBLIC char *ortp_strdup_vprintf(const char *fmt, va_list ap);
00310 ORTP_PUBLIC char *ortp_strcat_printf(char *dst, const char *fmt,...);
00311 ORTP_PUBLIC char *ortp_strcat_vprintf(char *dst, const char *fmt, va_list ap);
00312 
00313 ORTP_PUBLIC int ortp_file_exist(const char *pathname);
00314 
00315 ORTP_PUBLIC void ortp_get_cur_time(ortpTimeSpec *ret);
00316 void _ortp_get_cur_time(ortpTimeSpec *ret, bool_t realtime);
00317 ORTP_PUBLIC uint64_t ortp_get_cur_time_ms(void);
00318 ORTP_PUBLIC void ortp_sleep_ms(int ms);
00319 ORTP_PUBLIC void ortp_sleep_until(const ortpTimeSpec *ts);
00320 ORTP_PUBLIC unsigned int ortp_random(void);
00321 
00322 /* portable named pipes  and shared memory*/
00323 #if !defined(_WIN32_WCE)
00324 #ifdef WIN32
00325 typedef HANDLE ortp_pipe_t;
00326 #define ORTP_PIPE_INVALID INVALID_HANDLE_VALUE
00327 #else
00328 typedef int ortp_pipe_t;
00329 #define ORTP_PIPE_INVALID (-1)
00330 #endif
00331 
00332 ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_create(const char *name);
00333 /*
00334  * warning: on win32 ortp_server_pipe_accept_client() might return INVALID_HANDLE_VALUE without
00335  * any specific error, this happens when ortp_server_pipe_close() is called on another pipe.
00336  * This pipe api is not thread-safe.
00337 */
00338 ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_accept_client(ortp_pipe_t server);
00339 ORTP_PUBLIC int ortp_server_pipe_close(ortp_pipe_t spipe);
00340 ORTP_PUBLIC int ortp_server_pipe_close_client(ortp_pipe_t client);
00341 
00342 ORTP_PUBLIC ortp_pipe_t ortp_client_pipe_connect(const char *name);
00343 ORTP_PUBLIC int ortp_client_pipe_close(ortp_pipe_t sock);
00344 
00345 ORTP_PUBLIC int ortp_pipe_read(ortp_pipe_t p, uint8_t *buf, int len);
00346 ORTP_PUBLIC int ortp_pipe_write(ortp_pipe_t p, const uint8_t *buf, int len);
00347 
00348 ORTP_PUBLIC void *ortp_shm_open(unsigned int keyid, int size, int create);
00349 ORTP_PUBLIC void ortp_shm_close(void *memory);
00350 
00351 #endif
00352 
00353 #ifdef __cplusplus
00354 }
00355 
00356 #endif
00357 
00358 
00359 #if (defined(WIN32) || defined(_WIN32_WCE)) && !defined(ORTP_STATIC)
00360 #ifdef ORTP_EXPORTS
00361    #define ORTP_VAR_PUBLIC    extern __declspec(dllexport)
00362 #else
00363    #define ORTP_VAR_PUBLIC    __declspec(dllimport)
00364 #endif
00365 #else
00366    #define ORTP_VAR_PUBLIC    extern
00367 #endif
00368 
00369 #ifndef IN6_IS_ADDR_MULTICAST
00370 #define IN6_IS_ADDR_MULTICAST(i)        (((uint8_t *) (i))[0] == 0xff)
00371 #endif
00372 
00373 /*define __ios when we are compiling for ios.
00374  The TARGET_OS_IPHONE macro is stupid, it is defined to 0 when compiling on mac os x.
00375 */
00376 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE==1
00377 #define __ios 1
00378 #endif
00379 
00380 #endif
00381 
00382