1 /* 2 The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-) 3 Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20 #ifndef _OSIP_PORT_H_ 21 #define _OSIP_PORT_H_ 22 23 #include <stdio.h> 24 25 /* on android, va_list is only defined if stdarg.h is included before */ 26 /* on other platform, it doesn't harm to have it */ 27 #if defined(HAVE_STDARG_H) 28 #include <stdarg.h> 29 #else 30 #if defined(HAVE_VARARGS_H) 31 #include <varargs.h> 32 #else 33 #include <stdarg.h> 34 #endif 35 #endif 36 37 #if defined(__VXWORKS_OS__) 38 /* VxWorks lacks support for snprintf */ 39 int osip_vsnprintf(char *buf, int max, const char *fmt, va_list ap); 40 int osip_snprintf(char *buf, int max, const char *fmt, ...); 41 42 #define snprintf osip_snprintf 43 #define vsnprintf osip_vsnprintf 44 #endif 45 46 #include <osipparser2/osip_const.h> 47 48 #include <osipparser2/osip_list.h> 49 50 #define SIP_SYNTAX_ERROR (-1) 51 #define SIP_NETWORK_ERROR (-2) 52 #define SIP_ECONNREFUSED (-3) 53 #define SIP_RESSOURCE_ERROR (-4) 54 #define SIP_GLOBAL_ERROR (-5) 55 56 #ifdef __cplusplus 57 extern "C" { 58 #endif 59 60 /**************************/ 61 /* MALLOC redirections */ 62 /**************************/ 63 64 #if !defined(WIN32) && !defined(_WIN32_WCE) 65 66 #ifndef MINISIZE 67 typedef void *osip_malloc_func_t(size_t size); 68 typedef void osip_free_func_t(void *ptr); 69 typedef void *osip_realloc_func_t(void *ptr, size_t size); 70 71 extern osip_malloc_func_t *osip_malloc_func; 72 extern osip_realloc_func_t *osip_realloc_func; 73 extern osip_free_func_t *osip_free_func; 74 75 void osip_set_allocators(osip_malloc_func_t *malloc_func, osip_realloc_func_t *realloc_func, osip_free_func_t *free_func); 76 #endif 77 78 #ifdef DEBUG_MEM 79 80 void *_osip_malloc(size_t size, char *file, unsigned short line); 81 void _osip_free(void *ptr); 82 void *_osip_realloc(void *ptr, size_t size, char *file, unsigned short line); 83 84 #ifndef osip_malloc 85 #define osip_malloc(S) _osip_malloc(S, __FILE__, __LINE__) 86 #endif 87 #ifndef osip_realloc 88 #define osip_realloc(P, S) _osip_realloc(P, S, __FILE__, __LINE__) 89 #endif 90 #ifndef osip_free 91 #define osip_free(P) \ 92 { \ 93 if (P != NULL) { \ 94 _osip_free(P); \ 95 } \ 96 } 97 #endif 98 99 #else 100 101 #ifndef MINISIZE 102 #ifndef osip_malloc 103 #define osip_malloc(S) (osip_malloc_func ? osip_malloc_func(S) : malloc(S)) 104 #endif 105 #ifndef osip_realloc 106 #define osip_realloc(P, S) (osip_realloc_func ? osip_realloc_func(P, S) : realloc(P, S)) 107 #endif 108 #ifndef osip_free 109 #define osip_free(P) \ 110 { \ 111 if (P != NULL) { \ 112 if (osip_free_func) \ 113 osip_free_func(P); \ 114 else \ 115 free(P); \ 116 } \ 117 } 118 #endif 119 120 #else 121 /* MINISIZE code */ 122 #ifndef osip_malloc 123 #define osip_malloc(S) malloc(S) 124 #endif 125 #ifndef osip_realloc 126 #define osip_realloc(P, S) realloc(P, S) 127 #endif 128 #ifndef osip_free 129 #define osip_free(P) \ 130 { \ 131 if (P != NULL) { \ 132 free(P); \ 133 } \ 134 } 135 #endif 136 137 #endif 138 139 #endif 140 141 #else 142 143 /* Check for memory leaks */ 144 #if defined(_DEBUG) && defined(WIN32) 145 #define OSIP_MEMORY_DEBUG 146 #endif 147 148 #ifdef OSIP_MEMORY_DEBUG 149 /* for windows test purpose */ 150 #ifndef osip_malloc 151 #define osip_malloc(S) _osip_malloc(S, __FILE__, __LINE__) 152 #endif 153 #ifndef osip_realloc 154 #define osip_realloc(P, S) _osip_realloc(P, S, __FILE__, __LINE__) 155 #endif 156 #ifndef osip_free 157 #define osip_free(P) \ 158 { \ 159 if (P != NULL) { \ 160 _osip_free(P); \ 161 } \ 162 } 163 #endif 164 165 void *_osip_malloc(size_t size, char *file, unsigned short line); 166 void _osip_free(void *ptr); 167 void *_osip_realloc(void *ptr, size_t size, char *file, unsigned short line); 168 #else 169 void *osip_malloc(size_t size); 170 void *osip_realloc(void *, size_t size); 171 void osip_free(void *); 172 #endif 173 174 #endif 175 176 #if defined(WIN32) && !defined(__GNUC__) 177 #define alloca _alloca 178 #endif 179 180 /**************************/ 181 /* RANDOM number support */ 182 /**************************/ 183 184 unsigned int osip_build_random_number(void); 185 186 /**************************/ 187 /* TIMER support */ 188 /**************************/ 189 190 #define SP " \0" 191 192 void osip_usleep(int useconds); 193 194 #ifndef MINISIZE 195 int osip_atoi(const char *number); 196 int osip_strcasecmp(const char *s1, const char *s2); 197 int osip_strncasecmp(const char *s1, const char *s2, size_t len); 198 199 #else 200 #define osip_atoi atoi 201 #define osip_strcasecmp strcasecmp 202 #define osip_strncasecmp strncasecmp 203 #endif 204 205 char *osip_strcasestr(const char *haystack, const char *needle); 206 207 /**************************/ 208 /* STRING support */ 209 /**************************/ 210 211 char *osip_strncpy(char *dest, const char *src, size_t length); 212 char *osip_strdup(const char *ch); 213 char *osip_strdup_without_quote(const char *ch); 214 int osip_tolower(char *word); 215 int osip_clrspace(char *word); 216 int __osip_set_next_token(char **dest, char *buf, int end_separator, char **next); 217 /* find the next unescaped quote and return its index. */ 218 const char *__osip_quote_find(const char *qstring); 219 char *osip_enquote(const char *s); 220 void osip_dequote(char *s); 221 222 unsigned long osip_hash(const char *str); 223 char *osip_str_append(char *dst, const char *src); 224 char *osip_strn_append(char *dst, const char *src, size_t len); 225 char *osip_clrncpy(char *dst, const char *src, size_t len); 226 227 /**************************/ 228 /* LOG&DEBUG support */ 229 /**************************/ 230 231 #define LOG_TRUE 1 232 #define LOG_FALSE 0 233 /* levels */ 234 typedef enum _trace_level { 235 TRACE_LEVEL0 = 0, 236 #define OSIP_FATAL TRACE_LEVEL0 237 TRACE_LEVEL1 = 1, 238 #define OSIP_BUG TRACE_LEVEL1 239 TRACE_LEVEL2 = 2, 240 #define OSIP_ERROR TRACE_LEVEL2 241 TRACE_LEVEL3 = 3, 242 #define OSIP_WARNING TRACE_LEVEL3 243 TRACE_LEVEL4 = 4, 244 #define OSIP_INFO1 TRACE_LEVEL4 245 TRACE_LEVEL5 = 5, 246 #define OSIP_INFO2 TRACE_LEVEL5 247 TRACE_LEVEL6 = 6, 248 #define OSIP_INFO3 TRACE_LEVEL6 249 TRACE_LEVEL7 = 7, 250 #define OSIP_INFO4 TRACE_LEVEL7 251 END_TRACE_LEVEL = 8 252 } osip_trace_level_t; 253 254 typedef void osip_trace_func_t(const char *fi, int li, osip_trace_level_t level, const char *chfr, va_list ap); 255 256 /* these are defined in all cases, but are empty when oSIP is compiled 257 without trace */ 258 void osip_trace_initialize_func(osip_trace_level_t level, osip_trace_func_t *func); 259 void osip_trace_initialize_syslog(osip_trace_level_t level, char *ident); 260 int osip_trace_initialize(osip_trace_level_t level, FILE *file); 261 void osip_trace_enable_until_level(osip_trace_level_t level); 262 void osip_trace_enable_level(osip_trace_level_t level); 263 void osip_trace_disable_level(osip_trace_level_t level); 264 int osip_is_trace_level_activate(osip_trace_level_t level); 265 266 #ifndef ENABLE_TRACE 267 268 #define TRACE_INITIALIZE(level, file) \ 269 do { \ 270 } while (0) 271 #define TRACE_ENABLE_LEVEL(level) \ 272 do { \ 273 } while (0) 274 #define TRACE_DISABLE_LEVEL(level) \ 275 do { \ 276 } while (0) 277 #define IS_TRACE_LEVEL_ACTIVATE(level) (-1) 278 279 #else 280 281 #define TRACE_INITIALIZE(level, file) osip_trace_initialize(level, file) 282 #define TRACE_ENABLE_LEVEL(level) osip_trace_enable_level(level) 283 #define TRACE_DISABLE_LEVEL(level) osip_trace_disable_level(level) 284 #define IS_TRACE_LEVEL_ACTIVATE(level) osip_is_trace_level_activate(level) 285 286 #endif 287 288 /* log facility. */ 289 /* if f is NULL, current default log file is used. */ 290 /* INPUT: level | level of the trace */ 291 /* INPUT: f | use f instead of default log file */ 292 /* INPUT: chfr | format string for next args */ 293 int osip_trace(const char *fi, int li, osip_trace_level_t level, FILE *f, const char *chfr, ...); 294 295 #ifdef ENABLE_TRACE 296 #define OSIP_TRACE(P) P 297 #else 298 #define OSIP_TRACE(P) \ 299 do { \ 300 } while (0) 301 #endif 302 303 #define REMOVE_ELEMENT(first_element, element) \ 304 if (element->parent == NULL) { \ 305 first_element = element->next; \ 306 if (first_element != NULL) \ 307 first_element->parent = NULL; \ 308 } else { \ 309 element->parent->next = element->next; \ 310 if (element->next != NULL) \ 311 element->next->parent = element->parent; \ 312 element->next = NULL; \ 313 element->parent = NULL; \ 314 } 315 316 #define ADD_ELEMENT(first_element, element) \ 317 if (first_element == NULL) { \ 318 first_element = element; \ 319 element->next = NULL; \ 320 element->parent = NULL; \ 321 } else { \ 322 element->next = first_element; \ 323 element->parent = NULL; \ 324 element->next->parent = element; \ 325 first_element = element; \ 326 } 327 328 #define APPEND_ELEMENT(type_of_element_t, first_element, element) \ 329 if (first_element == NULL) { \ 330 first_element = element; \ 331 element->next = NULL; /* useless */ \ 332 element->parent = NULL; /* useless */ \ 333 } else { \ 334 type_of_element_t *f; \ 335 for (f = first_element; f->next != NULL; f = f->next) { \ 336 } \ 337 f->next = element; \ 338 element->parent = f; \ 339 element->next = NULL; \ 340 } 341 342 const char *osip_strerror(int err); 343 344 #ifdef __cplusplus 345 } 346 #endif 347 #define OSIP_SUCCESS 0 348 #define OSIP_UNDEFINED_ERROR -1 349 #define OSIP_BADPARAMETER -2 350 #define OSIP_WRONG_STATE -3 351 #define OSIP_NOMEM -4 352 #define OSIP_SYNTAXERROR -5 353 #define OSIP_NOTFOUND -6 354 #define OSIP_API_NOT_INITIALIZED -7 355 #define OSIP_NO_NETWORK -10 356 #define OSIP_PORT_BUSY -11 357 #define OSIP_UNKNOWN_HOST -12 358 #define OSIP_DISK_FULL -30 359 #define OSIP_NO_RIGHTS -31 360 #define OSIP_FILE_NOT_EXIST -32 361 #define OSIP_TIMEOUT -50 362 #define OSIP_TOOMUCHCALL -51 363 #define OSIP_WRONG_FORMAT -52 364 #define OSIP_NOCOMMONCODEC -53 365 #define OSIP_RETRY_LIMIT -60 366 #endif /* _PORT_H_ */ 367