oRTP  0.22.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 #ifdef ORTP_INET6
00057 #include <netdb.h>
00058 #endif
00059 
00060 typedef int ortp_socket_t;
00061 typedef pthread_t ortp_thread_t;
00062 typedef pthread_mutex_t ortp_mutex_t;
00063 typedef pthread_cond_t ortp_cond_t;
00064 
00065 #ifdef __INTEL_COMPILER
00066 #pragma warning(disable : 111)          // statement is unreachable
00067 #pragma warning(disable : 181)          // argument is incompatible with corresponding format string conversion
00068 #pragma warning(disable : 188)          // enumerated type mixed with another type
00069 #pragma warning(disable : 593)          // variable "xxx" was set but never used
00070 #pragma warning(disable : 810)          // conversion from "int" to "unsigned short" may lose significant bits
00071 #pragma warning(disable : 869)          // parameter "xxx" was never referenced
00072 #pragma warning(disable : 981)          // operands are evaluated in unspecified order
00073 #pragma warning(disable : 1418)         // external function definition with no prior declaration
00074 #pragma warning(disable : 1419)         // external declaration in primary source file
00075 #pragma warning(disable : 1469)         // "cc" clobber ignored
00076 #endif
00077 
00078 #define ORTP_PUBLIC
00079 
00080 #ifdef __cplusplus
00081 extern "C"
00082 {
00083 #endif
00084 
00085 int __ortp_thread_join(ortp_thread_t thread, void **ptr);
00086 int __ortp_thread_create(pthread_t *thread, pthread_attr_t *attr, void * (*routine)(void*), void *arg);
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_exit        pthread_exit
00095 #define ortp_mutex_init         pthread_mutex_init
00096 #define ortp_mutex_lock         pthread_mutex_lock
00097 #define ortp_mutex_unlock       pthread_mutex_unlock
00098 #define ortp_mutex_destroy      pthread_mutex_destroy
00099 #define ortp_cond_init          pthread_cond_init
00100 #define ortp_cond_signal        pthread_cond_signal
00101 #define ortp_cond_broadcast     pthread_cond_broadcast
00102 #define ortp_cond_wait          pthread_cond_wait
00103 #define ortp_cond_destroy       pthread_cond_destroy
00104 
00105 #define SOCKET_OPTION_VALUE     void *
00106 #define SOCKET_BUFFER           void *
00107 
00108 #define getSocketError() strerror(errno)
00109 #define getSocketErrorCode() (errno)
00110 
00111 #define ortp_log10f(x)  log10f(x)
00112 
00113 
00114 #else
00115 /*********************************/
00116 /* definitions for WIN32 flavour */
00117 /*********************************/
00118 
00119 #include <stdio.h>
00120 #include <stdlib.h>
00121 #include <stdarg.h>
00122 #include <winsock2.h>
00123 #include <ws2tcpip.h>
00124 
00125 #ifdef _MSC_VER
00126 #ifdef ORTP_EXPORTS
00127 #define ORTP_PUBLIC     __declspec(dllexport)
00128 #else 
00129 #define ORTP_PUBLIC     __declspec(dllimport)
00130 #endif
00131 #pragma push_macro("_WINSOCKAPI_")
00132 #ifndef _WINSOCKAPI_
00133 #define _WINSOCKAPI_
00134 #endif
00135 
00136 #define strtok_r strtok_s
00137 
00138 typedef  unsigned __int64 uint64_t;
00139 typedef  __int64 int64_t;
00140 typedef  unsigned short uint16_t;
00141 typedef  unsigned int uint32_t;
00142 typedef  int int32_t;
00143 typedef  unsigned char uint8_t;
00144 typedef __int16 int16_t;
00145 #else
00146 #include <stdint.h> /*provided by mingw32*/
00147 #include <io.h>
00148 #define ORTP_PUBLIC
00149 #endif
00150 
00151 #define vsnprintf       _vsnprintf
00152 #define srandom         srand
00153 #define random          rand
00154 
00155 ORTP_PUBLIC char* strtok_r(char *str, const char *delim, char **nextp);
00156 
00157 
00158 typedef SOCKET ortp_socket_t;
00159 #ifdef WINAPI_FAMILY_PHONE_APP
00160 typedef CONDITION_VARIABLE ortp_cond_t;
00161 typedef SRWLOCK ortp_mutex_t;
00162 #else
00163 typedef HANDLE ortp_cond_t;
00164 typedef HANDLE ortp_mutex_t;
00165 #endif
00166 typedef HANDLE ortp_thread_t;
00167 
00168 #define ortp_thread_create      WIN_thread_create
00169 #define ortp_thread_join        WIN_thread_join
00170 #define ortp_thread_exit(arg)
00171 #define ortp_mutex_init         WIN_mutex_init
00172 #define ortp_mutex_lock         WIN_mutex_lock
00173 #define ortp_mutex_unlock       WIN_mutex_unlock
00174 #define ortp_mutex_destroy      WIN_mutex_destroy
00175 #define ortp_cond_init          WIN_cond_init
00176 #define ortp_cond_signal        WIN_cond_signal
00177 #define ortp_cond_broadcast     WIN_cond_broadcast
00178 #define ortp_cond_wait          WIN_cond_wait
00179 #define ortp_cond_destroy       WIN_cond_destroy
00180 
00181 
00182 #ifdef __cplusplus
00183 extern "C"
00184 {
00185 #endif
00186 
00187 ORTP_PUBLIC int WIN_mutex_init(ortp_mutex_t *m, void *attr_unused);
00188 ORTP_PUBLIC int WIN_mutex_lock(ortp_mutex_t *mutex);
00189 ORTP_PUBLIC int WIN_mutex_unlock(ortp_mutex_t *mutex);
00190 ORTP_PUBLIC int WIN_mutex_destroy(ortp_mutex_t *mutex);
00191 ORTP_PUBLIC int WIN_thread_create(ortp_thread_t *t, void *attr_unused, void *(*func)(void*), void *arg);
00192 ORTP_PUBLIC int WIN_thread_join(ortp_thread_t thread, void **unused);
00193 ORTP_PUBLIC int WIN_cond_init(ortp_cond_t *cond, void *attr_unused);
00194 ORTP_PUBLIC int WIN_cond_wait(ortp_cond_t * cond, ortp_mutex_t * mutex);
00195 ORTP_PUBLIC int WIN_cond_signal(ortp_cond_t * cond);
00196 ORTP_PUBLIC int WIN_cond_broadcast(ortp_cond_t * cond);
00197 ORTP_PUBLIC int WIN_cond_destroy(ortp_cond_t * cond);
00198 
00199 #ifdef __cplusplus
00200 }
00201 #endif
00202 
00203 #define SOCKET_OPTION_VALUE     char *
00204 #define inline                  __inline
00205 
00206 #if defined(_WIN32_WCE)
00207 
00208 #define ortp_log10f(x)          (float)log10 ((double)x)
00209 
00210 #ifdef assert
00211         #undef assert
00212 #endif /*assert*/
00213 #define assert(exp)     ((void)0)
00214 
00215 #ifdef errno
00216         #undef errno
00217 #endif /*errno*/
00218 #define  errno GetLastError()
00219 #ifdef strerror
00220                 #undef strerror
00221 #endif /*strerror*/
00222 const char * ortp_strerror(DWORD value);
00223 #define strerror ortp_strerror
00224 
00225 
00226 #else /*_WIN32_WCE*/
00227 
00228 #define ortp_log10f(x)  log10f(x)
00229 
00230 #endif
00231 
00232 ORTP_PUBLIC const char *getWinSocketError(int error);
00233 #define getSocketErrorCode() WSAGetLastError()
00234 #define getSocketError() getWinSocketError(WSAGetLastError())
00235 
00236 #define snprintf _snprintf
00237 #define strcasecmp _stricmp
00238 #define strncasecmp _strnicmp
00239 
00240 
00241 #ifdef __cplusplus
00242 extern "C"{
00243 #endif
00244 ORTP_PUBLIC int gettimeofday (struct timeval *tv, void* tz);
00245 #ifdef _WORKAROUND_MINGW32_BUGS
00246 char * WSAAPI gai_strerror(int errnum);
00247 #endif
00248 #ifdef __cplusplus
00249 }
00250 #endif
00251 
00252 #endif
00253 
00254 typedef unsigned char bool_t;
00255 #undef TRUE
00256 #undef FALSE
00257 #define TRUE 1
00258 #define FALSE 0
00259 
00260 typedef struct ortpTimeSpec{
00261         int64_t tv_sec;
00262         int64_t tv_nsec;
00263 }ortpTimeSpec;
00264 
00265 #ifdef __cplusplus
00266 extern "C"{
00267 #endif
00268 
00269 ORTP_PUBLIC void* ortp_malloc(size_t sz);
00270 ORTP_PUBLIC void ortp_free(void *ptr);
00271 ORTP_PUBLIC void* ortp_realloc(void *ptr, size_t sz);
00272 ORTP_PUBLIC void* ortp_malloc0(size_t sz);
00273 ORTP_PUBLIC char * ortp_strdup(const char *tmp);
00274 
00275 /*override the allocator with this method, to be called BEFORE ortp_init()*/
00276 typedef struct _OrtpMemoryFunctions{
00277         void *(*malloc_fun)(size_t sz);
00278         void *(*realloc_fun)(void *ptr, size_t sz);
00279         void (*free_fun)(void *ptr);
00280 }OrtpMemoryFunctions;
00281 
00282 void ortp_set_memory_functions(OrtpMemoryFunctions *functions);
00283 
00284 #define ortp_new(type,count)    (type*)ortp_malloc(sizeof(type)*(count))
00285 #define ortp_new0(type,count)   (type*)ortp_malloc0(sizeof(type)*(count))
00286 
00287 ORTP_PUBLIC int close_socket(ortp_socket_t sock);
00288 ORTP_PUBLIC int set_non_blocking_socket(ortp_socket_t sock);
00289 
00290 ORTP_PUBLIC char *ortp_strndup(const char *str,int n);
00291 ORTP_PUBLIC char *ortp_strdup_printf(const char *fmt,...);
00292 ORTP_PUBLIC char *ortp_strdup_vprintf(const char *fmt, va_list ap);
00293 
00294 ORTP_PUBLIC int ortp_file_exist(const char *pathname);
00295 
00296 ORTP_PUBLIC void ortp_get_cur_time(ortpTimeSpec *ret);
00297 
00298 /* portable named pipes  and shared memory*/
00299 #if !defined(_WIN32_WCE)
00300 #ifdef WIN32
00301 typedef HANDLE ortp_pipe_t;
00302 #define ORTP_PIPE_INVALID INVALID_HANDLE_VALUE
00303 #else
00304 typedef int ortp_pipe_t;
00305 #define ORTP_PIPE_INVALID (-1)
00306 #endif
00307 
00308 ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_create(const char *name);
00309 /*
00310  * warning: on win32 ortp_server_pipe_accept_client() might return INVALID_HANDLE_VALUE without
00311  * any specific error, this happens when ortp_server_pipe_close() is called on another pipe.
00312  * This pipe api is not thread-safe.
00313 */
00314 ORTP_PUBLIC ortp_pipe_t ortp_server_pipe_accept_client(ortp_pipe_t server);
00315 ORTP_PUBLIC int ortp_server_pipe_close(ortp_pipe_t spipe);
00316 ORTP_PUBLIC int ortp_server_pipe_close_client(ortp_pipe_t client);
00317 
00318 ORTP_PUBLIC ortp_pipe_t ortp_client_pipe_connect(const char *name);
00319 ORTP_PUBLIC int ortp_client_pipe_close(ortp_pipe_t sock);
00320 
00321 ORTP_PUBLIC int ortp_pipe_read(ortp_pipe_t p, uint8_t *buf, int len);
00322 ORTP_PUBLIC int ortp_pipe_write(ortp_pipe_t p, const uint8_t *buf, int len);
00323 
00324 ORTP_PUBLIC void *ortp_shm_open(unsigned int keyid, int size, int create);
00325 ORTP_PUBLIC void ortp_shm_close(void *memory);
00326 
00327 #endif
00328 
00329 #ifdef __cplusplus
00330 }
00331 
00332 #endif
00333 
00334 
00335 #if (defined(WIN32) || defined(_WIN32_WCE)) && !defined(ORTP_STATIC)
00336 #ifdef ORTP_EXPORTS
00337    #define ORTP_VAR_PUBLIC    extern __declspec(dllexport)
00338 #else
00339    #define ORTP_VAR_PUBLIC    __declspec(dllimport)
00340 #endif
00341 #else
00342    #define ORTP_VAR_PUBLIC    extern
00343 #endif
00344 
00345 
00346 /*define __ios when we are compiling for ios.
00347  The TARGET_OS_IPHONE macro is stupid, it is defined to 0 when compiling on mac os x.
00348 */
00349 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE==1
00350 #define __ios 1
00351 #endif
00352 
00353 #endif
00354 
00355