libosip 3.6.0

osip_port.h

00001 /*
00002   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
00003   Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Aymeric MOIZARD jack@atosc.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 
00020 
00021 #ifndef _OSIP_PORT_H_
00022 #define _OSIP_PORT_H_
00023 
00024 /* Include necessary headers for osip */
00025 
00026 #include <stdio.h>
00027 
00028 #if defined(__arc__)
00029 #include "includes_api.h"
00030 #include "os_cfg_pub.h"
00031 #include <posix_time_pub.h>
00032 #endif
00033 
00034 #if defined(__PALMOS__) && (__PALMOS__ >= 0x06000000)
00035 #       define STDC_HEADERS 1
00036 #       define HAVE_CTYPE_H 1
00037 #       define HAVE_STRING_H 1
00038 #       define HAVE_SYS_TYPES_H 1
00039 #       define HAVE_TIME_H 1
00040 #       define HAVE_STDARG_H 1
00041 
00042 #elif defined _WIN32_WCE
00043 
00044 #define STDC_HEADERS 1
00045 #define HAVE_CTYPE_H 1
00046 #define HAVE_STRING_H 1
00047 #define HAVE_TIME_H 1
00048 #define HAVE_STDARG_H 1
00049 
00050 #define snprintf  _snprintf
00051 #define EBUSY           16
00052 
00053 #elif defined(WIN32)
00054 
00055 #define STDC_HEADERS 1
00056 #define HAVE_CTYPE_H 1
00057 #define HAVE_STRING_H 1
00058 #define HAVE_SYS_TYPES_H 1
00059 #define HAVE_TIME_H 1
00060 #define HAVE_STDARG_H 1
00061 
00062 #define snprintf _snprintf
00063 
00064 /* use win32 crypto routines for random number generation */
00065 /* only use for vs .net (compiler v. 1300) or greater */
00066 #if _MSC_VER >= 1300
00067 #define WIN32_USE_CRYPTO 1
00068 #endif
00069 
00070 #endif
00071 
00072 #if defined(__VXWORKS_OS__) || defined(__rtems__)
00073 #include <string.h>
00074 #include <time.h>
00075 #include <sys/times.h>
00076 #include <stdarg.h>
00077 #include <sys/types.h>
00078 #include <stdlib.h>
00079 #define VA_START(a, f)  va_start(a, f)
00080 
00081 #if defined(__VXWORKS_OS__)
00082 /* VxWorks lacks support for snprintf */
00083 int osip_vsnprintf(char *buf, int max, const char *fmt, va_list ap);
00084 int osip_snprintf(char *buf, int max, const char *fmt, ...);
00085 
00086 #define snprintf  osip_snprintf
00087 #define vsnprintf osip_vsnprintf
00088 #endif
00089 
00090 #else                                                   /* end of __VXWORKS_OS__ */
00091 
00092 #if defined (HAVE_CONFIG_H)
00093 #include <config.h>
00094 #  if defined (HAVE_STRING_H)
00095 #    include <string.h>
00096 #  else
00097 #    include <strings.h>
00098 #  endif                                                /* HAVE_STRING_H */
00099 #else
00100 #  include <string.h>
00101 #endif                                                  /* !HAVE_CONFIG_H */
00102 
00103 #if defined (HAVE_SYS_TYPES_H)
00104 #  include <sys/types.h>
00105 #endif
00106 
00107 #ifdef STDC_HEADERS
00108 #    include <stdlib.h>
00109 #endif                                                  /* !STDC_HEADERS */
00110 
00111 #if defined(HAVE_STDARG_H) || defined(WIN32)
00112 #  include <stdarg.h>
00113 #  define VA_START(a, f)  va_start(a, f)
00114 #else
00115 #  if defined(HAVE_VARARGS_H)
00116 #    include <varargs.h>
00117 #    define VA_START(a, f) va_start(a)
00118 #  else
00119 #    include <stdarg.h>
00120 #    define VA_START(a, f)  va_start(a, f)
00121 #  endif
00122 #endif
00123 
00124 #ifdef HAVE_TIME_H
00125 #  include <time.h>
00126 #endif
00127 
00128 #if defined (HAVE_SYS_TIME_H)
00129 #  include <sys/time.h>
00130 #endif
00131 
00132 #endif                                                  /* end of !__VXWORKS_OS__ */
00133 
00134 #ifdef _WIN32_WCE
00135 #define VA_START(a, f)  va_start(a, f)
00136 #endif
00137 
00138 #ifdef WIN32
00139 #define VA_START(a, f)  va_start(a, f)
00140 #endif
00141 
00142 #ifdef __PSOS__
00143 #define VA_START(a, f)  va_start(a, f)
00144 #include "pna.h"
00145 #include "stdlib.h"
00146 #include "time.h"
00147 #define timercmp(tvp, uvp, cmp) \
00148         ((tvp)->tv_sec cmp (uvp)->tv_sec || \
00149          (tvp)->tv_sec == (uvp)->tv_sec && (tvp)->tv_usec cmp (uvp)->tv_usec)
00150 #define snprintf  osip_snprintf
00151 #ifndef INT_MAX
00152 #define INT_MAX 0x7FFFFFFF
00153 #endif
00154 
00155 #endif
00156 
00157 
00158 #if __STDC__
00159 #  ifndef NOPROTOS
00160 #    define PARAMS(args)   args
00161 #  endif
00162 #endif
00163 
00164 #ifndef PARAMS
00165 #  define PARAMS(args)     ()
00166 #endif
00167 
00168 #include <osipparser2/osip_const.h>
00169 
00170 #include <osipparser2/osip_list.h>
00171 
00172 #define SIP_SYNTAX_ERROR    (-1)
00173 #define SIP_NETWORK_ERROR   (-2)
00174 #define SIP_ECONNREFUSED    (-3)
00175 #define SIP_RESSOURCE_ERROR (-4)
00176 #define SIP_GLOBAL_ERROR    (-5)
00177 
00178 #ifdef __cplusplus
00179 extern "C" {
00180 #endif
00181 
00182 /**************************/
00183 /* MALLOC redirections    */
00184 /**************************/
00185 
00186 #if !defined(WIN32) && !defined(_WIN32_WCE)
00187 
00188 #ifndef MINISIZE
00189         typedef void *osip_malloc_func_t(size_t size);
00190         typedef void osip_free_func_t(void *ptr);
00191         typedef void *osip_realloc_func_t(void *ptr, size_t size);
00192 
00193         extern osip_malloc_func_t *osip_malloc_func;
00194         extern osip_realloc_func_t *osip_realloc_func;
00195         extern osip_free_func_t *osip_free_func;
00196 
00197         void osip_set_allocators(osip_malloc_func_t * malloc_func,
00198                                                          osip_realloc_func_t * realloc_func,
00199                                                          osip_free_func_t * free_func);
00200 #endif
00201 
00202 #ifdef DEBUG_MEM
00203 
00204         void *_osip_malloc(size_t size, char *file, unsigned short line);
00205         void _osip_free(void *ptr);
00206         void *_osip_realloc(void *ptr, size_t size, char *file, unsigned short line);
00207 
00208 #ifndef osip_malloc
00209 #define osip_malloc(S) _osip_malloc(S,__FILE__,__LINE__)
00210 #endif
00211 #ifndef osip_realloc
00212 #define osip_realloc(P,S) _osip_realloc(P,S,__FILE__,__LINE__)
00213 #endif
00214 #ifndef osip_free
00215 #define osip_free(P) { if (P!=NULL) { _osip_free(P); } }
00216 #endif
00217 
00218 #else
00219 
00220 #ifndef MINISIZE
00221 #ifndef osip_malloc
00222 #define osip_malloc(S) (osip_malloc_func?osip_malloc_func(S):malloc(S))
00223 #endif
00224 #ifndef osip_realloc
00225 #define osip_realloc(P,S) (osip_realloc_func?osip_realloc_func(P,S):realloc(P,S))
00226 #endif
00227 #ifndef osip_free
00228 #define osip_free(P) { if (P!=NULL) { if (osip_free_func) osip_free_func(P); else free(P);} }
00229 #endif
00230 
00231 #else
00232         /* MINISIZE code */
00233 #ifndef osip_malloc
00234 #define osip_malloc(S) malloc(S)
00235 #endif
00236 #ifndef osip_realloc
00237 #define osip_realloc(P,S) realloc(P,S)
00238 #endif
00239 #ifndef osip_free
00240 #define osip_free(P) { if (P!=NULL) { free(P);} }
00241 #endif
00242 
00243 #endif
00244 
00245 #endif
00246 
00247 #else
00248 
00249         void *osip_malloc(size_t size);
00250         void *osip_realloc(void *, size_t size);
00251         void osip_free(void *);
00252 
00253 #endif
00254 
00255 #ifdef WIN32
00256 #define alloca _alloca
00257 #endif
00258 
00259 /**************************/
00260 /* RANDOM number support  */
00261 /**************************/
00262 
00263         unsigned int osip_build_random_number(void);
00264 
00265 /**************************/
00266 /* TIMER support          */
00267 /**************************/
00268 
00269 #define SP   " \0"
00270 
00271         void osip_usleep(int useconds);
00272 
00273 #ifndef MINISIZE
00274         int osip_atoi(const char *number);
00275         int osip_strcasecmp(const char *s1, const char *s2);
00276         int osip_strncasecmp(const char *s1, const char *s2, size_t len);
00277         char *osip_strcasestr(const char *haystack, const char *needle);
00278         
00279 #else
00280 #define osip_atoi  atoi
00281 #define osip_strcasecmp  strcasecmp
00282 #define osip_strncasecmp  strncasecmp
00283 #define osip_strcasestr  strcasestr
00284 #endif
00285 
00286 /**************************/
00287 /* STRING support         */
00288 /**************************/
00289 
00290         char *osip_strncpy(char *dest, const char *src, size_t length);
00291         char *osip_strdup(const char *ch);
00292         char *osip_strdup_without_quote(const char *ch);
00293         int osip_tolower(char *word);
00294         int osip_clrspace(char *word);
00295         char *__osip_sdp_append_string(char *string, size_t size,
00296                                                                    char *cur, char *string_osip_to_append);
00297         int __osip_set_next_token(char **dest, char *buf, int end_separator,
00298                                                           char **next);
00299         /* find the next unescaped quote and return its index. */
00300         char *__osip_quote_find(const char *qstring);
00301         char *osip_enquote(const char *s);
00302         void osip_dequote(char *s);
00303 
00304         unsigned long osip_hash(const char *str);
00305         char *osip_str_append(char *dst, const char *src);
00306         char *osip_strn_append(char *dst, const char *src, size_t len);
00307         char *osip_clrncpy(char *dst, const char *src, size_t len);
00308 
00309 /**************************/
00310 /* LOG&DEBUG support      */
00311 /**************************/
00312 
00313 #define LOG_TRUE  1
00314 #define LOG_FALSE 0
00315 /* levels */
00316         typedef enum _trace_level {
00317                 TRACE_LEVEL0 = 0,
00318 #define OSIP_FATAL    TRACE_LEVEL0
00319                 TRACE_LEVEL1 = 1,
00320 #define OSIP_BUG      TRACE_LEVEL1
00321                 TRACE_LEVEL2 = 2,
00322 #define OSIP_ERROR    TRACE_LEVEL2
00323                 TRACE_LEVEL3 = 3,
00324 #define OSIP_WARNING  TRACE_LEVEL3
00325                 TRACE_LEVEL4 = 4,
00326 #define OSIP_INFO1    TRACE_LEVEL4
00327                 TRACE_LEVEL5 = 5,
00328 #define OSIP_INFO2    TRACE_LEVEL5
00329                 TRACE_LEVEL6 = 6,
00330 #define OSIP_INFO3    TRACE_LEVEL6
00331                 TRACE_LEVEL7 = 7,
00332 #define OSIP_INFO4    TRACE_LEVEL7
00333                 END_TRACE_LEVEL = 8
00334         } osip_trace_level_t;
00335 
00336 
00337         typedef void osip_trace_func_t(char *fi, int li, osip_trace_level_t level,
00338                                                                    char *chfr, va_list ap);
00339 
00340 /* these are defined in all cases, but are empty when oSIP is compiled
00341    without trace */
00342         void osip_trace_initialize_func(osip_trace_level_t level,
00343                                                                         osip_trace_func_t * func);
00344         void osip_trace_initialize_syslog(osip_trace_level_t level, char *ident);
00345         void osip_trace_initialize(osip_trace_level_t level, FILE * file);
00346         void osip_trace_enable_until_level(osip_trace_level_t level);
00347         void osip_trace_enable_level(osip_trace_level_t level);
00348         void osip_trace_disable_level(osip_trace_level_t level);
00349         int osip_is_trace_level_activate(osip_trace_level_t level);
00350 
00351 #ifndef ENABLE_TRACE
00352 
00353 #define TRACE_INITIALIZE(level, file)  do { } while (0)
00354 #define TRACE_ENABLE_LEVEL(level)      do { } while (0)
00355 #define TRACE_DISABLE_LEVEL(level)     do { } while (0)
00356 #define IS_TRACE_LEVEL_ACTIVATE(level) (-1)
00357 
00358 #else
00359 
00360 #define TRACE_INITIALIZE(level, file)  osip_trace_initialize(level, file)
00361 #define TRACE_ENABLE_LEVEL(level)      osip_trace_enable_level ( level )
00362 #define TRACE_DISABLE_LEVEL(level)     osip_trace_disable_level ( level )
00363 #define IS_TRACE_LEVEL_ACTIVATE(level) osip_is_trace_level_activate( level )
00364 
00365 #endif
00366 
00367 /* log facility. */
00368 /* if f is NULL, current default log file is used. */
00369 /* INPUT: level | level of the trace               */
00370 /* INPUT: f | use f instead of default log file    */
00371 /* INPUT: chfr | format string for next args       */
00372         int osip_trace(char *fi, int li, osip_trace_level_t level, FILE * f,
00373                                    char *chfr, ...);
00374 
00375 #ifdef ENABLE_TRACE
00376 #define OSIP_TRACE(P) P
00377 #else
00378 #define OSIP_TRACE(P) do {} while (0)
00379 #endif
00380 
00381 #define REMOVE_ELEMENT(first_element, element)   \
00382        if (element->parent==NULL)                \
00383         { first_element = element->next;         \
00384           if (first_element!=NULL)               \
00385           first_element->parent = NULL; }        \
00386        else \
00387         { element->parent->next = element->next; \
00388           if (element->next!=NULL)               \
00389         element->next->parent = element->parent; \
00390         element->next = NULL;                    \
00391         element->parent = NULL; }
00392 
00393 #define ADD_ELEMENT(first_element, element) \
00394    if (first_element==NULL)                 \
00395     {                                       \
00396       first_element   = element;            \
00397       element->next   = NULL;               \
00398       element->parent = NULL;               \
00399     }                                       \
00400   else                                      \
00401     {                                       \
00402       element->next   = first_element;      \
00403       element->parent = NULL;               \
00404       element->next->parent = element;      \
00405       first_element = element;              \
00406     }
00407 
00408 #define APPEND_ELEMENT(type_of_element_t, first_element, element) \
00409   if (first_element==NULL)                            \
00410     { first_element = element;                        \
00411       element->next   = NULL; /* useless */           \
00412       element->parent = NULL; /* useless */ }         \
00413   else                                                \
00414     { type_of_element_t *f;                           \
00415       for (f=first_element; f->next!=NULL; f=f->next) \
00416          { }                                          \
00417       f->next    = element;                           \
00418       element->parent = f;                            \
00419       element->next   = NULL;                         \
00420     }
00421 
00422         const char *osip_strerror(int err);
00423 
00424 #ifdef __cplusplus
00425 }
00426 #endif
00427 #define OSIP_SUCCESS               0
00428 #define OSIP_UNDEFINED_ERROR      -1
00429 #define OSIP_BADPARAMETER         -2
00430 #define OSIP_WRONG_STATE          -3
00431 #define OSIP_NOMEM                -4
00432 #define OSIP_SYNTAXERROR          -5
00433 #define OSIP_NOTFOUND             -6
00434 #define OSIP_API_NOT_INITIALIZED  -7
00435 #define OSIP_NO_NETWORK           -10
00436 #define OSIP_PORT_BUSY            -11
00437 #define OSIP_UNKNOWN_HOST         -12
00438 #define OSIP_DISK_FULL            -30
00439 #define OSIP_NO_RIGHTS            -31
00440 #define OSIP_FILE_NOT_EXIST       -32
00441 #define OSIP_TIMEOUT              -50
00442 #define OSIP_TOOMUCHCALL          -51
00443 #define OSIP_WRONG_FORMAT         -52
00444 #define OSIP_NOCOMMONCODEC        -53
00445 #endif                                                  /* _PORT_H_ */