oRTP 0.16.3

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 #ifdef __cplusplus
00079 extern "C"
00080 {
00081 #endif
00082 
00083 int __ortp_thread_join(ortp_thread_t thread, void **ptr);
00084 int __ortp_thread_create(pthread_t *thread, pthread_attr_t *attr, void * (*routine)(void*), void *arg);
00085 
00086 #ifdef __cplusplus
00087 }
00088 #endif
00089 
00090 #define ortp_thread_create      __ortp_thread_create
00091 #define ortp_thread_join        __ortp_thread_join
00092 #define ortp_thread_exit        pthread_exit
00093 #define ortp_mutex_init         pthread_mutex_init
00094 #define ortp_mutex_lock         pthread_mutex_lock
00095 #define ortp_mutex_unlock       pthread_mutex_unlock
00096 #define ortp_mutex_destroy      pthread_mutex_destroy
00097 #define ortp_cond_init          pthread_cond_init
00098 #define ortp_cond_signal        pthread_cond_signal
00099 #define ortp_cond_broadcast     pthread_cond_broadcast
00100 #define ortp_cond_wait          pthread_cond_wait
00101 #define ortp_cond_destroy       pthread_cond_destroy
00102 
00103 #define SOCKET_OPTION_VALUE     void *
00104 #define SOCKET_BUFFER           void *
00105 
00106 #define getSocketError() strerror(errno)
00107 #define getSocketErrorCode() (errno)
00108 
00109 #define ortp_log10f(x)  log10f(x)
00110 
00111 
00112 #else
00113 /*********************************/
00114 /* definitions for WIN32 flavour */
00115 /*********************************/
00116 
00117 #include <stdio.h>
00118 #include <stdlib.h>
00119 #include <stdarg.h>
00120 #include <winsock2.h>
00121 #include <ws2tcpip.h>
00122 #include <io.h>
00123 
00124 #ifdef _MSC_VER
00125 #pragma push_macro("_WINSOCKAPI_")
00126 #ifndef _WINSOCKAPI_
00127 #define _WINSOCKAPI_
00128 #endif
00129 
00130 typedef  unsigned __int64 uint64_t;
00131 typedef  __int64 int64_t;
00132 typedef  unsigned short uint16_t;
00133 typedef  unsigned int uint32_t;
00134 typedef  int int32_t;
00135 typedef  unsigned char uint8_t;
00136 typedef __int16 int16_t;
00137 #else
00138 #include <stdint.h> /*provided by mingw32*/
00139 #endif
00140 
00141 #define vsnprintf       _vsnprintf
00142 #define srandom         srand
00143 #define random          rand
00144 
00145 
00146 typedef SOCKET ortp_socket_t;
00147 typedef HANDLE ortp_cond_t;
00148 typedef HANDLE ortp_mutex_t;
00149 typedef HANDLE ortp_thread_t;
00150 
00151 #define ortp_thread_create      WIN_thread_create
00152 #define ortp_thread_join        WIN_thread_join
00153 #define ortp_thread_exit(arg)
00154 #define ortp_mutex_init         WIN_mutex_init
00155 #define ortp_mutex_lock         WIN_mutex_lock
00156 #define ortp_mutex_unlock       WIN_mutex_unlock
00157 #define ortp_mutex_destroy      WIN_mutex_destroy
00158 #define ortp_cond_init          WIN_cond_init
00159 #define ortp_cond_signal        WIN_cond_signal
00160 #define ortp_cond_broadcast     WIN_cond_broadcast
00161 #define ortp_cond_wait          WIN_cond_wait
00162 #define ortp_cond_destroy       WIN_cond_destroy
00163 
00164 
00165 #ifdef __cplusplus
00166 extern "C"
00167 {
00168 #endif
00169 
00170 int WIN_mutex_init(ortp_mutex_t *m, void *attr_unused);
00171 int WIN_mutex_lock(ortp_mutex_t *mutex);
00172 int WIN_mutex_unlock(ortp_mutex_t *mutex);
00173 int WIN_mutex_destroy(ortp_mutex_t *mutex);
00174 int WIN_thread_create(ortp_thread_t *t, void *attr_unused, void *(*func)(void*), void *arg);
00175 int WIN_thread_join(ortp_thread_t thread, void **unused);
00176 int WIN_cond_init(ortp_cond_t *cond, void *attr_unused);
00177 int WIN_cond_wait(ortp_cond_t * cond, ortp_mutex_t * mutex);
00178 int WIN_cond_signal(ortp_cond_t * cond);
00179 int WIN_cond_broadcast(ortp_cond_t * cond);
00180 int WIN_cond_destroy(ortp_cond_t * cond);
00181 
00182 #ifdef __cplusplus
00183 }
00184 #endif
00185 
00186 #define SOCKET_OPTION_VALUE     char *
00187 #define inline                  __inline
00188 
00189 #if defined(_WIN32_WCE)
00190 
00191 #define ortp_log10f(x)          (float)log10 ((double)x)
00192 
00193 #ifdef assert
00194         #undef assert
00195 #endif /*assert*/
00196 #define assert(exp)     ((void)0)
00197 
00198 #ifdef errno
00199         #undef errno
00200 #endif /*errno*/
00201 #define  errno GetLastError()
00202 #ifdef strerror
00203                 #undef strerror
00204 #endif /*strerror*/
00205 const char * ortp_strerror(DWORD value);
00206 #define strerror ortp_strerror
00207 
00208 
00209 #else /*_WIN32_WCE*/
00210 
00211 #define ortp_log10f(x)  log10f(x)
00212 
00213 #endif
00214 
00215 const char *getWinSocketError(int error);
00216 #define getSocketErrorCode() WSAGetLastError()
00217 #define getSocketError() getWinSocketError(WSAGetLastError())
00218 
00219 #define snprintf _snprintf
00220 #define strcasecmp _stricmp
00221 
00222 #if 0
00223 struct timeval {
00224         long    tv_sec;         /* seconds */
00225         long    tv_usec;        /* and microseconds */
00226 };
00227 #endif
00228 
00229 int gettimeofday (struct timeval *tv, void* tz);
00230 #ifdef _WORKAROUND_MINGW32_BUGS
00231 char * WSAAPI gai_strerror(int errnum);
00232 #endif
00233 
00234 
00235 #endif
00236 
00237 typedef unsigned char bool_t;
00238 #undef TRUE
00239 #undef FALSE
00240 #define TRUE 1
00241 #define FALSE 0
00242 
00243 #ifdef __cplusplus
00244 extern "C"{
00245 #endif
00246 
00247 void* ortp_malloc(size_t sz);
00248 void ortp_free(void *ptr);
00249 void* ortp_realloc(void *ptr, size_t sz);
00250 void* ortp_malloc0(size_t sz);
00251 char * ortp_strdup(const char *tmp);
00252 
00253 /*override the allocator with this method, to be called BEFORE ortp_init()*/
00254 typedef struct _OrtpMemoryFunctions{
00255         void *(*malloc_fun)(size_t sz);
00256         void *(*realloc_fun)(void *ptr, size_t sz);
00257         void (*free_fun)(void *ptr);
00258 }OrtpMemoryFunctions;
00259 
00260 void ortp_set_memory_functions(OrtpMemoryFunctions *functions);
00261 
00262 #define ortp_new(type,count)    (type*)ortp_malloc(sizeof(type)*(count))
00263 #define ortp_new0(type,count)   (type*)ortp_malloc0(sizeof(type)*(count))
00264 
00265 int close_socket(ortp_socket_t sock);
00266 int set_non_blocking_socket(ortp_socket_t sock);
00267 
00268 char *ortp_strndup(const char *str,int n);
00269 char *ortp_strdup_printf(const char *fmt,...);
00270 char *ortp_strdup_vprintf(const char *fmt, va_list ap);
00271 
00272 int ortp_file_exist(const char *pathname);
00273 
00274 /* portable named pipes */
00275 #if !defined(_WIN32_WCE)
00276 #ifdef WIN32
00277 typedef HANDLE ortp_pipe_t;
00278 #define ORTP_PIPE_INVALID INVALID_HANDLE_VALUE
00279 #else
00280 typedef int ortp_pipe_t;
00281 #define ORTP_PIPE_INVALID (-1)
00282 #endif
00283 
00284 ortp_pipe_t ortp_server_pipe_create(const char *name);
00285 /*
00286  * warning: on win32 ortp_server_pipe_accept_client() might return INVALID_HANDLE_VALUE without
00287  * any specific error, this happens when ortp_server_pipe_close() is called on another pipe.
00288  * This pipe api is not thread-safe.
00289 */
00290 ortp_pipe_t ortp_server_pipe_accept_client(ortp_pipe_t server);
00291 int ortp_server_pipe_close(ortp_pipe_t spipe);
00292 int ortp_server_pipe_close_client(ortp_pipe_t client);
00293 
00294 ortp_pipe_t ortp_client_pipe_connect(const char *name);
00295 int ortp_client_pipe_close(ortp_pipe_t sock);
00296 
00297 int ortp_pipe_read(ortp_pipe_t p, uint8_t *buf, int len);
00298 int ortp_pipe_write(ortp_pipe_t p, const uint8_t *buf, int len);
00299 #endif
00300 
00301 #ifdef __cplusplus
00302 }
00303 
00304 #endif
00305 
00306 
00307 #if (defined(WIN32) || defined(_WIN32_WCE)) && !defined(ORTP_STATIC)
00308 #ifdef ORTP_EXPORTS
00309    #define VAR_DECLSPEC    __declspec(dllexport)
00310 #else
00311    #define VAR_DECLSPEC    __declspec(dllimport)
00312 #endif
00313 #else
00314    #define VAR_DECLSPEC    extern
00315 #endif
00316 
00317 
00318 #endif
00319 
00320