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