2 The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3 Copyright (C) 2001,2002,2003 Aymeric MOIZARD jack@atosc.org
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.
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.
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
21 #define _INC_TIME /* for wce.h */
29 #include <osipparser2/osip_port.h>
37 #if defined(__VXWORKS_OS__)
38 #include <selectLib.h>
39 #elif (!defined(WIN32) && !defined(_WIN32_WCE))
43 #ifdef WIN32_USE_CRYPTO
48 #if defined (HAVE_SYS_UNISTD_H)
49 # include <sys/unistd.h>
52 #if defined (HAVE_UNISTD_H)
56 #if defined (HAVE_SYSLOG_H)
60 #if defined (HAVE_SYS_SELECT_H)
61 # include <sys/select.h>
64 #ifdef HAVE_PTH_PTHREAD_H
69 int tracing_table[END_TRACE_LEVEL];
70 static int use_syslog = 0;
72 static unsigned int random_seed_set = 0;
74 #ifndef WIN32_USE_CRYPTO
76 osip_build_random_number ()
79 osip_fallback_random_number ()
87 gettimeofday (&tv, NULL);
98 #ifdef WIN32_USE_CRYPTO
101 osip_build_random_number ()
108 CryptAcquireContext (&crypto, NULL, NULL, PROV_RSA_FULL,
109 CRYPT_VERIFYCONTEXT);
112 err = CryptGenRandom (crypto, sizeof (num), (BYTE *) & num);
113 CryptReleaseContext (crypto, 0);
117 num = osip_fallback_random_number ();
129 osip_atoi (const char *number)
131 #if defined(__linux) || defined(HAVE_STRTOL)
135 i = strtol (number, (char **) NULL, 10);
136 if (i == LONG_MIN || i == LONG_MAX)
141 return atoi (number);
145 osip_strncpy (char *dest, const char *src, size_t length)
148 strncpy (dest, src, length);
150 strncpy (dest, src, length);
156 /* append string_osip_to_append to string at position cur
157 size is the current allocated size of the element
160 __osip_sdp_append_string (char *string, size_t size, char *cur,
161 char *string_osip_to_append)
163 size_t length = strlen (string_osip_to_append);
165 if (cur - string + length > size)
169 length2 = cur - string;
170 string = realloc (string, size + length + 10);
171 cur = string + length2; /* the initial allocation may have changed! */
173 osip_strncpy (cur, string_osip_to_append, length);
174 return cur + strlen (cur);
178 osip_usleep (int useconds)
181 Sleep (useconds / 1000);
183 struct timeval delay;
186 sec = (int) useconds / 1000000;
195 delay.tv_usec = useconds;
197 select (0, 0, 0, 0, &delay);
203 osip_strdup (const char *ch)
209 length = strlen (ch);
210 copy = (char *) osip_malloc (length + 1);
211 osip_strncpy (copy, ch, length);
216 osip_strdup_without_quote (const char *ch)
218 char *copy = (char *) osip_malloc (strlen (ch) + 1);
220 /* remove leading and trailing " */
223 osip_strncpy (copy, ch + 1, strlen (ch + 1));
224 osip_strncpy (copy + strlen (copy) - 1, "\0", 1);
227 osip_strncpy (copy, ch, strlen (ch));
232 osip_tolower (char *word)
235 for (; *word; word++)
236 *word = (char) tolower (*word);
239 size_t len = strlen (word);
241 for (i = 0; i <= len - 1; i++)
243 if ('A' <= word[i] && word[i] <= 'Z')
244 word[i] = word[i] + 32;
251 osip_strcasecmp (const char *s1, const char *s2)
253 #if (!defined WIN32 && !defined _WIN32_WCE)
254 return strcasecmp (s1, s2);
256 return _stricmp (s1, s2);
261 osip_strncasecmp (const char *s1, const char *s2, size_t len)
263 #if (!defined WIN32 && !defined _WIN32_WCE)
264 return strncasecmp (s1, s2, len);
266 return _strnicmp (s1, s2, len);
270 /* remove SPACE before and after the content */
272 osip_clrspace (char *word)
285 while ((' ' == *pbeg) || ('\r' == *pbeg) || ('\n' == *pbeg)
289 pend = word + len - 1;
290 while ((' ' == *pend) || ('\r' == *pend) || ('\n' == *pend)
301 /* Add terminating NULL only if we've cleared room for it */
302 if (pend + 1 <= word + (len - 1))
306 memmove (word, pbeg, pend - pbeg + 2);
311 /* __osip_set_next_token:
312 dest is the place where the value will be allocated
313 buf is the string where the value is searched
314 end_separator is the character that MUST be found at the end of the value
315 next is the final location of the separator + 1
317 the element MUST be found before any "\r" "\n" "\0" and
324 __osip_set_next_token (char **dest, char *buf, int end_separator, char **next)
326 char *sep; /* separator */
331 while ((*sep != end_separator) && (*sep != '\0') && (*sep != '\r')
334 if ((*sep == '\r') || (*sep == '\n'))
335 { /* we should continue normally only if this is the separator asked! */
336 if (*sep != end_separator)
340 return -1; /* value must not end with this separator! */
342 return -1; /* empty value (or several space!) */
344 *dest = osip_malloc (sep - (buf) + 1);
345 osip_strncpy (*dest, buf, sep - buf);
347 *next = sep + 1; /* return the position right after the separator */
352 /* not yet done!!! :-)
355 __osip_set_next_token_better (char **dest, char *buf, int end_separator,
356 int *forbidden_tab[], int size_tab, char **next)
358 char *sep; /* separator */
363 while ((*sep != end_separator) && (*sep != '\0') && (*sep != '\r')
366 if ((*sep == '\r') && (*sep == '\n'))
367 { /* we should continue normally only if this is the separator asked! */
368 if (*sep != end_separator)
372 return -1; /* value must not end with this separator! */
374 return -1; /* empty value (or several space!) */
376 *dest = osip_malloc (sep - (buf) + 1);
377 osip_strncpy (*dest, buf, sep - buf);
379 *next = sep + 1; /* return the position right after the separator */
384 /* in quoted-string, many characters can be escaped... */
385 /* __osip_quote_find returns the next quote that is not escaped */
387 __osip_quote_find (const char *qstring)
391 quote = strchr (qstring, '"');
392 if (quote == qstring) /* the first char matches and is not escaped... */
396 return NULL; /* no quote at all... */
398 /* this is now the nasty cases where '"' is escaped
399 '" jonathan ros \\\""'
405 we must count the number of preceeding '\' */
411 if (0 == strncmp (quote - i, "\\", 1))
415 if (i % 2 == 1) /* the '"' was not escaped */
418 /* else continue with the next '"' */
419 quote = strchr (quote + 1, '"');
424 if (quote - i == qstring - 1)
425 /* example: "\"john" */
426 /* example: "\\"jack" */
428 /* special case where the string start with '\' */
429 if (*qstring == '\\')
430 i++; /* an escape char was not counted */
431 if (i % 2 == 0) /* the '"' was not escaped */
434 { /* else continue with the next '"' */
435 qstring = quote + 1; /* reset qstring because
436 (*quote+1) may be also == to '\\' */
437 quote = strchr (quote + 1, '"');
450 osip_enquote (const char *s)
455 t = rtn = osip_malloc (strlen (s) * 2 + 3);
457 for (; *s != '\0'; s++)
482 osip_dequote (char *s)
491 memmove (s, s + 1, len--);
492 if (len > 0 && s[len - 1] == '"')
494 for (; *s != '\0'; s++, len--)
497 memmove (s, s + 1, len--);
501 /**********************************************************/
502 /* only MACROS from osip/trace.h should be used by others */
503 /* TRACE_INITIALIZE(level,file)) */
504 /* TRACE_ENABLE_LEVEL(level) */
505 /* TRACE_DISABLE_LEVEL(level) */
506 /* IS_TRACE_LEVEL_ACTIVATE(level) */
507 /**********************************************************/
510 osip_trace_initialize_syslog (osip_trace_level_t level, char *ident)
514 osip_trace_initialize (osip_trace_level_t level, FILE * file)
518 osip_trace_enable_level (osip_trace_level_t level)
522 osip_trace_disable_level (osip_trace_level_t level)
527 osip_is_trace_level_activate (osip_trace_level_t level)
535 /* all lower levels of level are logged in file. */
537 osip_trace_initialize (osip_trace_level_t level, FILE * file)
541 /* enable trace in log file by default */
545 #ifndef SYSTEM_LOGGER_ENABLED
550 /* enable all lower levels */
551 while (i < END_TRACE_LEVEL)
554 tracing_table[i] = LOG_TRUE;
556 tracing_table[i] = LOG_FALSE;
562 osip_trace_initialize_syslog (osip_trace_level_t level, char *ident)
565 #if defined (HAVE_SYSLOG_H)
566 openlog (ident, LOG_CONS | LOG_PID, LOG_DAEMON);
569 /* enable all lower levels */
570 while (i < END_TRACE_LEVEL)
573 tracing_table[i] = LOG_TRUE;
575 tracing_table[i] = LOG_FALSE;
580 /* enable a special debugging level! */
582 osip_trace_enable_level (osip_trace_level_t level)
584 tracing_table[level] = LOG_TRUE;
587 /* disable a special debugging level! */
589 osip_trace_disable_level (osip_trace_level_t level)
591 tracing_table[level] = LOG_FALSE;
594 /* not so usefull? */
596 osip_is_trace_level_activate (osip_trace_level_t level)
598 return tracing_table[level];
603 osip_trace (char *fi, int li, osip_trace_level_t level, FILE * f, char *chfr,
609 #if !defined(WIN32) && !defined(SYSTEM_LOGGER_ENABLED)
610 if (logfile == NULL && use_syslog == 0)
611 { /* user did not initialize logger.. */
619 if (tracing_table[level] == LOG_FALSE)
624 #ifdef __VXWORKS_OS__
625 /* vxworks can't have a local file */
629 if (f && use_syslog == 0)
631 if (level == OSIP_FATAL)
632 fprintf (f, "| FATAL | <%s: %i> ", fi, li);
633 else if (level == OSIP_BUG)
634 fprintf (f, "| BUG | <%s: %i> ", fi, li);
635 else if (level == OSIP_ERROR)
636 fprintf (f, "| ERROR | <%s: %i> ", fi, li);
637 else if (level == OSIP_WARNING)
638 fprintf (f, "|WARNING| <%s: %i> ", fi, li);
639 else if (level == OSIP_INFO1)
640 fprintf (f, "| INFO1 | <%s: %i> ", fi, li);
641 else if (level == OSIP_INFO2)
642 fprintf (f, "| INFO2 | <%s: %i> ", fi, li);
643 else if (level == OSIP_INFO3)
644 fprintf (f, "| INFO3 | <%s: %i> ", fi, li);
645 else if (level == OSIP_INFO4)
646 fprintf (f, "| INFO4 | <%s: %i> ", fi, li);
648 vfprintf (f, chfr, ap);
652 #if defined (HAVE_SYSLOG_H)
653 else if (use_syslog == 1)
657 memset (buffer, 0, sizeof (buffer));
658 if (level == OSIP_FATAL)
659 in = snprintf (buffer, 511, "| FATAL | <%s: %i> ", fi, li);
660 else if (level == OSIP_BUG)
661 in = snprintf (buffer, 511, "| BUG | <%s: %i> ", fi, li);
662 else if (level == OSIP_ERROR)
663 in = snprintf (buffer, 511, "| ERROR | <%s: %i> ", fi, li);
664 else if (level == OSIP_WARNING)
665 in = snprintf (buffer, 511, "|WARNING| <%s: %i> ", fi, li);
666 else if (level == OSIP_INFO1)
667 in = snprintf (buffer, 511, "| INFO1 | <%s: %i> ", fi, li);
668 else if (level == OSIP_INFO2)
669 in = snprintf (buffer, 511, "| INFO2 | <%s: %i> ", fi, li);
670 else if (level == OSIP_INFO3)
671 in = snprintf (buffer, 511, "| INFO3 | <%s: %i> ", fi, li);
672 else if (level == OSIP_INFO4)
673 in = snprintf (buffer, 511, "| INFO4 | <%s: %i> ", fi, li);
675 vsnprintf (buffer + in, 511 - in, chfr, ap);
676 if (level == OSIP_FATAL)
677 syslog (LOG_ERR, "%s", buffer);
678 else if (level == OSIP_BUG)
679 syslog (LOG_ERR, "%s", buffer);
680 else if (level == OSIP_ERROR)
681 syslog (LOG_ERR, "%s", buffer);
682 else if (level == OSIP_WARNING)
683 syslog (LOG_WARNING, "%s", buffer);
684 else if (level == OSIP_INFO1)
685 syslog (LOG_INFO, "%s", buffer);
686 else if (level == OSIP_INFO2)
687 syslog (LOG_INFO, "%s", buffer);
688 else if (level == OSIP_INFO3)
689 syslog (LOG_DEBUG, "%s", buffer);
690 else if (level == OSIP_INFO4)
691 syslog (LOG_DEBUG, "%s", buffer);
694 #ifdef SYSTEM_LOGGER_ENABLED
699 memset (buffer, 0, sizeof (buffer));
700 if (level == OSIP_FATAL)
701 in = _snprintf (buffer, 511, "| FATAL | <%s: %i> ", fi, li);
702 else if (level == OSIP_BUG)
703 in = _snprintf (buffer, 511, "| BUG | <%s: %i> ", fi, li);
704 else if (level == OSIP_ERROR)
705 in = _snprintf (buffer, 511, "| ERROR | <%s: %i> ", fi, li);
706 else if (level == OSIP_WARNING)
707 in = _snprintf (buffer, 511, "|WARNING| <%s: %i> ", fi, li);
708 else if (level == OSIP_INFO1)
709 in = _snprintf (buffer, 511, "| INFO1 | <%s: %i> ", fi, li);
710 else if (level == OSIP_INFO2)
711 in = _snprintf (buffer, 511, "| INFO2 | <%s: %i> ", fi, li);
712 else if (level == OSIP_INFO3)
713 in = _snprintf (buffer, 511, "| INFO3 | <%s: %i> ", fi, li);
714 else if (level == OSIP_INFO4)
715 in = _snprintf (buffer, 511, "| INFO4 | <%s: %i> ", fi, li);
717 _vsnprintf (buffer + in, 511 - in, chfr, ap);
718 OutputDebugString (buffer);
729 void *osip_malloc(size_t size)
731 void *ptr = malloc(size);
737 void osip_free(void *ptr)
739 if (ptr==NULL) return;