oRTP  0.25.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 #ifdef __cplusplus
00080 extern "C"
00081 {
00082 #endif
00083 
00084 int __ortp_thread_join(ortp_thread_t thread, void **ptr);
00085 int __ortp_thread_create(ortp_thread_t *thread, pthread_attr_t *attr, void * (*routine)(void*), void *arg);
00086 unsigned long __ortp_thread_self(void);
00087 
00088 #ifdef __cplusplus
00089 }
00090 #endif
00091 
00092 #define ortp_thread_create      __ortp_thread_create
00093 #define ortp_thread_join        __ortp_thread_join
00094 #define ortp_thread_self        __ortp_thread_self
00095 #define ortp_thread_exit        pthread_exit
00096 #define ortp_mutex_init         pthread_mutex_init
00097 #define ortp_mutex_lock         pthread_mutex_lock
00098 #define ortp_mutex_unlock       pthread_mutex_unlock
00099 #define ortp_mutex_destroy      pthread_mutex_destroy
00100 #define ortp_cond_init          pthread_cond_init
00101 #define ortp_cond_signal        pthread_cond_signal
00102 #define ortp_cond_broadcast     pthread_cond_broadcast
00103 #define ortp_cond_wait          pthread_cond_wait
00104 #define ortp_cond_destroy       pthread_cond_destroy
00105 
00106 #define SOCKET_OPTION_VALUE     void *
00107 #define SOCKET_BUFFER           void *
00108 
00109 #define getSocketError() strerror(errno)
00110 #define getSocketErrorCode() (errno)
00111 #define ortp_gettimeofday(tv,tz) gettimeofday(tv,tz)
00112 #define ortp_log10f(x)  log10f(x)
00113 
00114 
00115 #else
00116 /*********************************/
00117 /* definitions for WIN32 flavour */
00118 /*********************************/
00119 
00120 #include <stdio.h>
00121 #define _CRT_RAND_S
00122 #include <stdlib.h>
00123 #include <stdarg.h>
00124 #include <winsock2.h>
00125 #include <ws2tcpip.h>
00126 #ifdef _MSC_VER
00127 #include <io.h>
00128 #endif
00129 
00130 #if defined(__MINGW32__) || !defined(WINAPI_FAMILY_PARTITION) || !defined(WINAPI_PARTITION_DESKTOP)
00131 #define ORTP_WINDOWS_DESKTOP 1
00132 #elif defined(WINAPI_FAMILY_PARTITION)
00133 #if defined(WINAPI_PARTITION_DESKTOP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
00134 #define ORTP_WINDOWS_DESKTOP 1
00135 #elif defined(WINAPI_PARTITION_PHONE_APP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)
00136 #define ORTP_WINDOWS_PHONE 1
00137 #elif defined(WINAPI_PARTITION_APP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
00138 #define ORTP_WINDOWS_UNIVERSAL 1
00139 #endif
00140 #endif
00141 
00142 #ifdef _MSC_VER
00143 #ifdef ORTP_STATIC
00144 #define ORTP_PUBLIC
00145 #else
00146 #ifdef ORTP_EXPORTS
00147 #define ORTP_PUBLIC     __declspec(dllexport)
00148 #else
00149 #define ORTP_PUBLIC     __declspec(dllimport)
00150 #endif
00151 #endif
00152 #pragma push_macro("_WINSOCKAPI_")
00153 #ifndef _WINSOCKAPI_
00154 #define _WINSOCKAPI_
00155 #endif
00156 
00157 #define strtok_r strtok_s
00158 
00159 typedef  unsigned __int64 uint64_t;
00160 typedef  __int64 int64_t;
00161 typedef  unsigned short uint16_t;
00162 typedef  unsigned int uint32_t;
00163 typedef  int int32_t;
00164 typedef  unsigned char uint8_t;
00165 typedef __int16 int16_t;
00166 #else
00167 #include <stdint.h> /*provided by mingw32*/
00168 #include <io.h>
00169 #define ORTP_PUBLIC
00170 ORTP_PUBLIC char* strtok_r(char *str, const char *delim, char **nextp);
00171 #endif
00172 
00173 #define vsnprintf       _vsnprintf
00174 
00175 typedef SOCKET ortp_socket_t;
00176 #ifdef ORTP_WINDOWS_DESKTOP
00177 typedef HANDLE ortp_cond_t;
00178 typedef HANDLE ortp_mutex_t;
00179 #else
00180 typedef CONDITION_VARIABLE ortp_cond_t;
00181 typedef SRWLOCK ortp_mutex_t;
00182 #endif
00183 typedef HANDLE ortp_thread_t;
00184 
00185 #define ortp_thread_create      WIN_thread_create
00186 #define ortp_thread_join        WIN_thread_join
00187 #define ortp_thread_self        WIN_thread_self
00188 #define ortp_thread_exit(arg)
00189 #define ortp_mutex_init         WIN_mutex_init
00190 #define ortp_mutex_lock         WIN_mutex_lock
00191 #define ortp_mutex_unlock       WIN_mutex_unlock
00192 #define ortp_mutex_destroy      WIN_mutex_destroy
00193 #define ortp_cond_init          WIN_cond_init
00194 #define ortp_cond_signal        WIN_cond_signal
00195 #define ortp_cond_broadcast     WIN_cond_broadcast
00196 #define ortp_cond_wait          WIN_cond_wait
00197 #define ortp_cond_destroy       WIN_cond_destroy
00198 
00199 
00200 #ifdef __cplusplus
00201 extern "C"
00202 {
00203 #endif
00204 
00205 ORTP_PUBLIC int WIN_mutex_init(ortp_mutex_t *m, void *attr_unused);
00206 ORTP_PUBLIC int WIN_mutex_lock(ortp_mutex_t *mutex);
00207 ORTP_PUBLIC int WIN_mutex_unlock(ortp_mutex_t *mutex);
00208 ORTP_PUBLIC int WIN_mutex_destroy(ortp_mutex_t *mutex);
00209 ORTP_PUBLIC int WIN_thread_create(ortp_thread_t *t, void *attr_unused, void *(*func)(void*), void *arg);
00210 ORTP_PUBLIC int WIN_thread_join(ortp_thread_t thread, void **unused);
00211 ORTP_PUBLIC unsigned long WIN_thread_self(void);
00212 ORTP_PUBLIC int WIN_cond_init(ortp_cond_t *cond, void *attr_unused);
00213 ORTP_PUBLIC int WIN_cond_wait(ortp_cond_t * cond, ortp_mutex_t * mutex);
00214 ORTP_PUBLIC int WIN_cond_signal(ortp_cond_t * cond);
00215 ORTP_PUBLIC int WIN_cond_broadcast(ortp_cond_t * cond);
00216 ORTP_PUBLIC int WIN_cond_destroy(ortp_cond_t * cond);
00217 
00218 #ifdef __cplusplus
00219 }
00220 #endif
00221 
00222 #define SOCKET_OPTION_VALUE     char *
00223 #define ORTP_INLINE                     __inline
00224 
00225 #if defined(_WIN32_WCE)
00226 
00227 #define ortp_log10f(x)          (float)log10 ((double)x)
00228 
00229 #ifdef assert
00230         #undef assert
00231 #endif /*assert*/
00232 #define assert(exp)     ((void)0)
00233 
00234 #ifdef errno
00235         #undef errno
00236 #endif /*errno*/
00237 #define  errno GetLastError()
00238 #ifdef strerror
00239                 #undef strerror
00240 #endif /*strerror*/
00241 const char * ortp_strerror(DWORD value);
00242 #define strerror ortp_strerror
00243 
00244 
00245 #else /*_WIN32_WCE*/
00246 
00247 #define ortp_log10f(x)  log10f(x)
00248 
00249 #endif
00250 
00251 ORTP_PUBLIC const char *getWinSocketError(int error);
00252 #define getSocketErrorCode() WSAGetLastError()
00253 #define getSocketError() getWinSocketError(WSAGetLastError())
00254 
00255 #define snprintf _snprintf
00256 #define strcasecmp _stricmp
00257 #define strncasecmp _strnicmp
00258 
00259 #ifndef F_OK
00260 #define F_OK 00 /* Visual Studio does not define F_OK */
00261 #endif
00262 
00263 
00264 #ifdef __cplusplus
00265 extern "C"{
00266 #endif
00267 ORTP_PUBLIC int ortp_gettimeofday (struct timeval *tv, void* tz);
00268 #ifdef _WORKAROUND_MINGW32_BUGS
00269 char * WSAAPI gai_strerror(int errnum);
00270 #endif
00271 #ifdef __cplusplus
00272 }
00273 #endif
00274 
00275 #endif
00276 
00277 typedef unsigned char bool_t;
00278 #undef TRUE
00279 #undef FALSE
00280 #define TRUE 1
00281 #define FALSE 0
00282 
00283 typedef struct _OList OList;
00284 
00285 typedef struct ortpTimeSpec{
00286         int64_t tv_sec;
00287         int64_t tv_nsec;
00288 }ortpTimeSpec;
00289 
00290 #ifdef __cplusplus
00291 extern "C"{
00292 #endif
00293 
00294 ORTP_PUBLIC void* ortp_malloc(size_t sz);
00295 ORTP_PUBLIC void ortp_free(void *ptr);
00296 ORTP_PUBLIC void* ortp_realloc(void *ptr, size_t sz);
00297 ORTP_PUBLIC void* ortp_malloc0(size_t sz);
00298 ORTP_PUBLIC char * ortp_strdup(const char *tmp);
00299 
00300 /*override the allocator with this method, to be called BEFORE ortp_init()*/
00301 typedef struct _OrtpMemoryFunctions{
00302         void *(*malloc_fun)(size_t sz);
00303         void *(*realloc_fun)(void *ptr, size_t sz);
00304         void (*free_fun)(void *ptr);
00305 }OrtpMemoryFunctions;
00306 
00307 void ortp_set_memory_functions(OrtpMemoryFunctions *functions);
00308 
00309 #define ortp_new(type,count)    (type*)ortp_malloc(sizeof(type)*(count))
00310 #define ortp_new0(type,count)   (type*)ortp_malloc0(sizeof(type)*(count))
00311 
00312 ORTP_PUBLIC int close_socket(ortp_socket_t sock);
00313 ORTP_PUBLIC int set_non_blocking_socket(ortp_socket_t sock);
00314 
00315 ORTP_PUBLIC char *ortp_strndup(const char *str,int n);
00316 ORTP_PUBLIC char *ortp_strdup_printf(const char *fmt,...);
00317 ORTP_PUBLIC char *ortp_strdup_vprintf(const char *fmt, va_list ap);
00318 ORTP_PUBLIC char *ortp_strcat_printf(char *dst, const char *fmt,...);
00319 ORTP_PUBLIC char *ortp_strcat_vprintf(char *dst, const char *fmt, va_list ap);
00320 
00321 ORTP_PUBLIC int ortp_file_exist(const char *pathname);
00322 
00323 ORTP_PUBLIC void ortp_get_cur_time(ortpTimeSpec *ret);
00324 void _ortp_get_cur_time(ortpTimeSpec *ret, bool_t realtime);
00325 ORTP_PUBLIC uint64_t ortp_get_cur_time_ms(void);
00326 ORTP_PUBLIC void ortp_sleep_ms(int ms);
00327 ORTP_PUBLIC void ortp_sleep_until(const ortpTimeSpec *ts);
00328 ORTP_PUBLIC unsigned int ortp_random(void);
00329 
00330 /* portable named pipes  and shared memory*/
00331 #if !defined(_WIN32_WCE)
00332 #ifdef _WIN32
00333 typedef HANDLE ortp_pipe_t;
00334 #define ORTP_PIPE_INVALID INVALID_HANDLE_VALUE
00335 #else
00336 typedef int ortp_pipe_t;
00337 #define ORTP_PIPE_INVALID (-1)
00338 #endif
00339 
00340 ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_create(const char *name);
00341 /*
00342  * warning: on win32 ortp_server_pipe_accept_client() might return INVALID_HANDLE_VALUE without
00343  * any specific error, this happens when ortp_server_pipe_close() is called on another pipe.
00344  * This pipe api is not thread-safe.
00345 */
00346 ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_accept_client(ortp_pipe_t server);
00347 ORTP_PUBLIC int ortp_server_pipe_close(ortp_pipe_t spipe);
00348 ORTP_PUBLIC int ortp_server_pipe_close_client(ortp_pipe_t client);
00349 
00350 ORTP_PUBLIC ortp_pipe_t ortp_client_pipe_connect(const char *name);
00351 ORTP_PUBLIC int ortp_client_pipe_close(ortp_pipe_t sock);
00352 
00353 ORTP_PUBLIC int ortp_pipe_read(ortp_pipe_t p, uint8_t *buf, int len);
00354 ORTP_PUBLIC int ortp_pipe_write(ortp_pipe_t p, const uint8_t *buf, int len);
00355 
00356 ORTP_PUBLIC void *ortp_shm_open(unsigned int keyid, int size, int create);
00357 ORTP_PUBLIC void ortp_shm_close(void *memory);
00358 
00359 #endif
00360 
00361 #ifdef __cplusplus
00362 }
00363 
00364 #endif
00365 
00366 
00367 #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(ORTP_STATIC)
00368 #ifdef ORTP_EXPORTS
00369    #define ORTP_VAR_PUBLIC    extern __declspec(dllexport)
00370 #else
00371    #define ORTP_VAR_PUBLIC    __declspec(dllimport)
00372 #endif
00373 #else
00374    #define ORTP_VAR_PUBLIC    extern
00375 #endif
00376 
00377 #ifndef IN6_IS_ADDR_MULTICAST
00378 #define IN6_IS_ADDR_MULTICAST(i)        (((uint8_t *) (i))[0] == 0xff)
00379 #endif
00380 
00381 /*define __ios when we are compiling for ios.
00382  The TARGET_OS_IPHONE macro is stupid, it is defined to 0 when compiling on mac os x.
00383 */
00384 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE==1
00385 #define __ios 1
00386 #endif
00387 
00388 #endif
00389 
00390