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