osip_port.h 10 KB

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