Add Asterisk Version to core show locks
[asterisk/asterisk.git] / main / utils.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*! \file
18  *
19  * \brief Utility functions
20  *
21  * \note These are important for portability and security,
22  * so please use them in favour of other routines.
23  * Please consult the CODING GUIDELINES for more information.
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <ctype.h>
35 #include <sys/stat.h>
36 #include <sys/stat.h>
37
38 #include <fcntl.h>
39
40 #include <sys/syscall.h>
41 #if defined(__APPLE__)
42 #include <mach/mach.h>
43 #elif defined(HAVE_SYS_THR_H)
44 #include <sys/thr.h>
45 #endif
46
47 #include "asterisk/network.h"
48 #include "asterisk/ast_version.h"
49
50 #define AST_API_MODULE          /* ensure that inlinable API functions will be built in lock.h if required */
51 #include "asterisk/lock.h"
52 #include "asterisk/io.h"
53 #include "asterisk/md5.h"
54 #include "asterisk/sha1.h"
55 #include "asterisk/cli.h"
56 #include "asterisk/linkedlists.h"
57
58 #define AST_API_MODULE          /* ensure that inlinable API functions will be built in this module if required */
59 #include "asterisk/strings.h"
60
61 #define AST_API_MODULE          /* ensure that inlinable API functions will be built in this module if required */
62 #include "asterisk/time.h"
63
64 #define AST_API_MODULE          /* ensure that inlinable API functions will be built in this module if required */
65 #include "asterisk/stringfields.h"
66
67 #define AST_API_MODULE          /* ensure that inlinable API functions will be built in this module if required */
68 #include "asterisk/utils.h"
69
70 #define AST_API_MODULE
71 #include "asterisk/threadstorage.h"
72
73 #define AST_API_MODULE
74 #include "asterisk/config.h"
75
76 static char base64[64];
77 static char b2a[256];
78
79 AST_THREADSTORAGE(inet_ntoa_buf);
80
81 #if !defined(HAVE_GETHOSTBYNAME_R_5) && !defined(HAVE_GETHOSTBYNAME_R_6)
82
83 #define ERANGE 34       /*!< duh? ERANGE value copied from web... */
84 #undef gethostbyname
85
86 AST_MUTEX_DEFINE_STATIC(__mutex);
87
88 /*! \brief Reentrant replacement for gethostbyname for BSD-based systems.
89 \note This
90 routine is derived from code originally written and placed in the public
91 domain by Enzo Michelangeli <em@em.no-ip.com> */
92
93 static int gethostbyname_r (const char *name, struct hostent *ret, char *buf,
94                                 size_t buflen, struct hostent **result,
95                                 int *h_errnop)
96 {
97         int hsave;
98         struct hostent *ph;
99         ast_mutex_lock(&__mutex); /* begin critical area */
100         hsave = h_errno;
101
102         ph = gethostbyname(name);
103         *h_errnop = h_errno; /* copy h_errno to *h_herrnop */
104         if (ph == NULL) {
105                 *result = NULL;
106         } else {
107                 char **p, **q;
108                 char *pbuf;
109                 int nbytes = 0;
110                 int naddr = 0, naliases = 0;
111                 /* determine if we have enough space in buf */
112
113                 /* count how many addresses */
114                 for (p = ph->h_addr_list; *p != 0; p++) {
115                         nbytes += ph->h_length; /* addresses */
116                         nbytes += sizeof(*p); /* pointers */
117                         naddr++;
118                 }
119                 nbytes += sizeof(*p); /* one more for the terminating NULL */
120
121                 /* count how many aliases, and total length of strings */
122                 for (p = ph->h_aliases; *p != 0; p++) {
123                         nbytes += (strlen(*p)+1); /* aliases */
124                         nbytes += sizeof(*p);  /* pointers */
125                         naliases++;
126                 }
127                 nbytes += sizeof(*p); /* one more for the terminating NULL */
128
129                 /* here nbytes is the number of bytes required in buffer */
130                 /* as a terminator must be there, the minimum value is ph->h_length */
131                 if (nbytes > buflen) {
132                         *result = NULL;
133                         ast_mutex_unlock(&__mutex); /* end critical area */
134                         return ERANGE; /* not enough space in buf!! */
135                 }
136
137                 /* There is enough space. Now we need to do a deep copy! */
138                 /* Allocation in buffer:
139                         from [0] to [(naddr-1) * sizeof(*p)]:
140                         pointers to addresses
141                         at [naddr * sizeof(*p)]:
142                         NULL
143                         from [(naddr+1) * sizeof(*p)] to [(naddr+naliases) * sizeof(*p)] :
144                         pointers to aliases
145                         at [(naddr+naliases+1) * sizeof(*p)]:
146                         NULL
147                         then naddr addresses (fixed length), and naliases aliases (asciiz).
148                 */
149
150                 *ret = *ph;   /* copy whole structure (not its address!) */
151
152                 /* copy addresses */
153                 q = (char **)buf; /* pointer to pointers area (type: char **) */
154                 ret->h_addr_list = q; /* update pointer to address list */
155                 pbuf = buf + ((naddr + naliases + 2) * sizeof(*p)); /* skip that area */
156                 for (p = ph->h_addr_list; *p != 0; p++) {
157                         memcpy(pbuf, *p, ph->h_length); /* copy address bytes */
158                         *q++ = pbuf; /* the pointer is the one inside buf... */
159                         pbuf += ph->h_length; /* advance pbuf */
160                 }
161                 *q++ = NULL; /* address list terminator */
162
163                 /* copy aliases */
164                 ret->h_aliases = q; /* update pointer to aliases list */
165                 for (p = ph->h_aliases; *p != 0; p++) {
166                         strcpy(pbuf, *p); /* copy alias strings */
167                         *q++ = pbuf; /* the pointer is the one inside buf... */
168                         pbuf += strlen(*p); /* advance pbuf */
169                         *pbuf++ = 0; /* string terminator */
170                 }
171                 *q++ = NULL; /* terminator */
172
173                 strcpy(pbuf, ph->h_name); /* copy alias strings */
174                 ret->h_name = pbuf;
175                 pbuf += strlen(ph->h_name); /* advance pbuf */
176                 *pbuf++ = 0; /* string terminator */
177
178                 *result = ret;  /* and let *result point to structure */
179
180         }
181         h_errno = hsave;  /* restore h_errno */
182         ast_mutex_unlock(&__mutex); /* end critical area */
183
184         return (*result == NULL); /* return 0 on success, non-zero on error */
185 }
186
187
188 #endif
189
190 /*! \brief Re-entrant (thread safe) version of gethostbyname that replaces the
191    standard gethostbyname (which is not thread safe)
192 */
193 struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp)
194 {
195         int res;
196         int herrno;
197         int dots = 0;
198         const char *s;
199         struct hostent *result = NULL;
200         /* Although it is perfectly legitimate to lookup a pure integer, for
201            the sake of the sanity of people who like to name their peers as
202            integers, we break with tradition and refuse to look up a
203            pure integer */
204         s = host;
205         res = 0;
206         while (s && *s) {
207                 if (*s == '.')
208                         dots++;
209                 else if (!isdigit(*s))
210                         break;
211                 s++;
212         }
213         if (!s || !*s) {
214                 /* Forge a reply for IP's to avoid octal IP's being interpreted as octal */
215                 if (dots != 3)
216                         return NULL;
217                 memset(hp, 0, sizeof(struct ast_hostent));
218                 hp->hp.h_addrtype = AF_INET;
219                 hp->hp.h_addr_list = (void *) hp->buf;
220                 hp->hp.h_addr = hp->buf + sizeof(void *);
221                 /* For AF_INET, this will always be 4 */
222                 hp->hp.h_length = 4;
223                 if (inet_pton(AF_INET, host, hp->hp.h_addr) > 0)
224                         return &hp->hp;
225                 return NULL;
226
227         }
228 #ifdef HAVE_GETHOSTBYNAME_R_5
229         result = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &herrno);
230
231         if (!result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
232                 return NULL;
233 #else
234         res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
235
236         if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
237                 return NULL;
238 #endif
239         return &hp->hp;
240 }
241
242 /*! \brief Produce 32 char MD5 hash of value. */
243 void ast_md5_hash(char *output, const char *input)
244 {
245         struct MD5Context md5;
246         unsigned char digest[16];
247         char *ptr;
248         int x;
249
250         MD5Init(&md5);
251         MD5Update(&md5, (const unsigned char *) input, strlen(input));
252         MD5Final(digest, &md5);
253         ptr = output;
254         for (x = 0; x < 16; x++)
255                 ptr += sprintf(ptr, "%2.2x", digest[x]);
256 }
257
258 /*! \brief Produce 40 char SHA1 hash of value. */
259 void ast_sha1_hash(char *output, const char *input)
260 {
261         struct SHA1Context sha;
262         char *ptr;
263         int x;
264         uint8_t Message_Digest[20];
265
266         SHA1Reset(&sha);
267
268         SHA1Input(&sha, (const unsigned char *) input, strlen(input));
269
270         SHA1Result(&sha, Message_Digest);
271         ptr = output;
272         for (x = 0; x < 20; x++)
273                 ptr += sprintf(ptr, "%2.2x", Message_Digest[x]);
274 }
275
276 /*! \brief Produce a 20 byte SHA1 hash of value. */
277 void ast_sha1_hash_uint(uint8_t *digest, const char *input)
278 {
279         struct SHA1Context sha;
280
281         SHA1Reset(&sha);
282
283         SHA1Input(&sha, (const unsigned char *) input, strlen(input));
284
285         SHA1Result(&sha, digest);
286 }
287
288 /*! \brief decode BASE64 encoded text */
289 int ast_base64decode(unsigned char *dst, const char *src, int max)
290 {
291         int cnt = 0;
292         unsigned int byte = 0;
293         unsigned int bits = 0;
294         int incnt = 0;
295         while(*src && *src != '=' && (cnt < max)) {
296                 /* Shift in 6 bits of input */
297                 byte <<= 6;
298                 byte |= (b2a[(int)(*src)]) & 0x3f;
299                 bits += 6;
300                 src++;
301                 incnt++;
302                 /* If we have at least 8 bits left over, take that character
303                    off the top */
304                 if (bits >= 8)  {
305                         bits -= 8;
306                         *dst = (byte >> bits) & 0xff;
307                         dst++;
308                         cnt++;
309                 }
310         }
311         /* Don't worry about left over bits, they're extra anyway */
312         return cnt;
313 }
314
315 /*! \brief encode text to BASE64 coding */
316 int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks)
317 {
318         int cnt = 0;
319         int col = 0;
320         unsigned int byte = 0;
321         int bits = 0;
322         int cntin = 0;
323         /* Reserve space for null byte at end of string */
324         max--;
325         while ((cntin < srclen) && (cnt < max)) {
326                 byte <<= 8;
327                 byte |= *(src++);
328                 bits += 8;
329                 cntin++;
330                 if ((bits == 24) && (cnt + 4 <= max)) {
331                         *dst++ = base64[(byte >> 18) & 0x3f];
332                         *dst++ = base64[(byte >> 12) & 0x3f];
333                         *dst++ = base64[(byte >> 6) & 0x3f];
334                         *dst++ = base64[byte & 0x3f];
335                         cnt += 4;
336                         col += 4;
337                         bits = 0;
338                         byte = 0;
339                 }
340                 if (linebreaks && (cnt < max) && (col == 64)) {
341                         *dst++ = '\n';
342                         cnt++;
343                         col = 0;
344                 }
345         }
346         if (bits && (cnt + 4 <= max)) {
347                 /* Add one last character for the remaining bits,
348                    padding the rest with 0 */
349                 byte <<= 24 - bits;
350                 *dst++ = base64[(byte >> 18) & 0x3f];
351                 *dst++ = base64[(byte >> 12) & 0x3f];
352                 if (bits == 16)
353                         *dst++ = base64[(byte >> 6) & 0x3f];
354                 else
355                         *dst++ = '=';
356                 *dst++ = '=';
357                 cnt += 4;
358         }
359         if (linebreaks && (cnt < max)) {
360                 *dst++ = '\n';
361                 cnt++;
362         }
363         *dst = '\0';
364         return cnt;
365 }
366
367 int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
368 {
369         return ast_base64encode_full(dst, src, srclen, max, 0);
370 }
371
372 static void base64_init(void)
373 {
374         int x;
375         memset(b2a, -1, sizeof(b2a));
376         /* Initialize base-64 Conversion table */
377         for (x = 0; x < 26; x++) {
378                 /* A-Z */
379                 base64[x] = 'A' + x;
380                 b2a['A' + x] = x;
381                 /* a-z */
382                 base64[x + 26] = 'a' + x;
383                 b2a['a' + x] = x + 26;
384                 /* 0-9 */
385                 if (x < 10) {
386                         base64[x + 52] = '0' + x;
387                         b2a['0' + x] = x + 52;
388                 }
389         }
390         base64[62] = '+';
391         base64[63] = '/';
392         b2a[(int)'+'] = 62;
393         b2a[(int)'/'] = 63;
394 }
395
396 const struct ast_flags ast_uri_http = {AST_URI_UNRESERVED};
397 const struct ast_flags ast_uri_http_legacy = {AST_URI_LEGACY_SPACE | AST_URI_UNRESERVED};
398 const struct ast_flags ast_uri_sip_user = {AST_URI_UNRESERVED | AST_URI_SIP_USER_UNRESERVED};
399
400 char *ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec)
401 {
402         const char *ptr  = string;      /* Start with the string */
403         char *out = outbuf;
404         const char *mark = "-_.!~*'()"; /* no encode set, RFC 2396 section 2.3, RFC 3261 sec 25 */
405         const char *user_unreserved = "&=+$,;?/"; /* user-unreserved set, RFC 3261 sec 25 */
406
407         while (*ptr && out - outbuf < buflen - 1) {
408                 if (ast_test_flag(&spec, AST_URI_LEGACY_SPACE) && *ptr == ' ') {
409                         /* for legacy encoding, encode spaces as '+' */
410                         *out = '+';
411                         out++;
412                 } else if (!(ast_test_flag(&spec, AST_URI_MARK)
413                                 && strchr(mark, *ptr))
414                         && !(ast_test_flag(&spec, AST_URI_ALPHANUM)
415                                 && ((*ptr >= '0' && *ptr <= '9')
416                                 || (*ptr >= 'A' && *ptr <= 'Z')
417                                 || (*ptr >= 'a' && *ptr <= 'z')))
418                         && !(ast_test_flag(&spec, AST_URI_SIP_USER_UNRESERVED)
419                                 && strchr(user_unreserved, *ptr))) {
420
421                         if (out - outbuf >= buflen - 3) {
422                                 break;
423                         }
424                         out += sprintf(out, "%%%02X", (unsigned char) *ptr);
425                 } else {
426                         *out = *ptr;    /* Continue copying the string */
427                         out++;
428                 }
429                 ptr++;
430         }
431
432         if (buflen) {
433                 *out = '\0';
434         }
435
436         return outbuf;
437 }
438
439 void ast_uri_decode(char *s, struct ast_flags spec)
440 {
441         char *o;
442         unsigned int tmp;
443
444         for (o = s; *s; s++, o++) {
445                 if (ast_test_flag(&spec, AST_URI_LEGACY_SPACE) && *s == '+') {
446                         /* legacy mode, decode '+' as space */
447                         *o = ' ';
448                 } else if (*s == '%' && s[1] != '\0' && s[2] != '\0' && sscanf(s + 1, "%2x", &tmp) == 1) {
449                         /* have '%', two chars and correct parsing */
450                         *o = tmp;
451                         s += 2; /* Will be incremented once more when we break out */
452                 } else /* all other cases, just copy */
453                         *o = *s;
454         }
455         *o = '\0';
456 }
457
458 char *ast_escape_quoted(const char *string, char *outbuf, int buflen)
459 {
460         const char *ptr  = string;
461         char *out = outbuf;
462         char *allow = "\t\v !"; /* allow LWS (minus \r and \n) and "!" */
463
464         while (*ptr && out - outbuf < buflen - 1) {
465                 if (!(strchr(allow, *ptr))
466                         && !(*ptr >= '#' && *ptr <= '[') /* %x23 - %x5b */
467                         && !(*ptr >= ']' && *ptr <= '~') /* %x5d - %x7e */
468                         && !((unsigned char) *ptr > 0x7f)) {             /* UTF8-nonascii */
469
470                         if (out - outbuf >= buflen - 2) {
471                                 break;
472                         }
473                         out += sprintf(out, "\\%c", (unsigned char) *ptr);
474                 } else {
475                         *out = *ptr;
476                         out++;
477                 }
478                 ptr++;
479         }
480
481         if (buflen) {
482                 *out = '\0';
483         }
484
485         return outbuf;
486 }
487 int ast_xml_escape(const char *string, char * const outbuf, const size_t buflen)
488 {
489         char *dst = outbuf;
490         char *end = outbuf + buflen - 1; /* save one for the null terminator */
491
492         /* Handle the case for the empty output buffer */
493         if (buflen == 0) {
494                 return -1;
495         }
496
497         /* Escaping rules from http://www.w3.org/TR/REC-xml/#syntax */
498         /* This also prevents partial entities at the end of a string */
499         while (*string && dst < end) {
500                 const char *entity = NULL;
501                 int len = 0;
502
503                 switch (*string) {
504                 case '<':
505                         entity = "&lt;";
506                         len = 4;
507                         break;
508                 case '&':
509                         entity = "&amp;";
510                         len = 5;
511                         break;
512                 case '>':
513                         /* necessary if ]]> is in the string; easier to escape them all */
514                         entity = "&gt;";
515                         len = 4;
516                         break;
517                 case '\'':
518                         /* necessary in single-quoted strings; easier to escape them all */
519                         entity = "&apos;";
520                         len = 6;
521                         break;
522                 case '"':
523                         /* necessary in double-quoted strings; easier to escape them all */
524                         entity = "&quot;";
525                         len = 6;
526                         break;
527                 default:
528                         *dst++ = *string++;
529                         break;
530                 }
531
532                 if (entity) {
533                         ast_assert(len == strlen(entity));
534                         if (end - dst < len) {
535                                 /* no room for the entity; stop */
536                                 break;
537                         }
538                         /* just checked for length; strcpy is fine */
539                         strcpy(dst, entity);
540                         dst += len;
541                         ++string;
542                 }
543         }
544         /* Write null terminator */
545         *dst = '\0';
546         /* If any chars are left in string, return failure */
547         return *string == '\0' ? 0 : -1;
548 }
549
550 /*! \brief  ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa */
551 const char *ast_inet_ntoa(struct in_addr ia)
552 {
553         char *buf;
554
555         if (!(buf = ast_threadstorage_get(&inet_ntoa_buf, INET_ADDRSTRLEN)))
556                 return "";
557
558         return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
559 }
560
561 static int dev_urandom_fd;
562
563 #ifndef __linux__
564 #undef pthread_create /* For ast_pthread_create function only */
565 #endif /* !__linux__ */
566
567 #if !defined(LOW_MEMORY)
568
569 #ifdef DEBUG_THREADS
570
571 /*! \brief A reasonable maximum number of locks a thread would be holding ... */
572 #define AST_MAX_LOCKS 64
573
574 /* Allow direct use of pthread_mutex_t and friends */
575 #undef pthread_mutex_t
576 #undef pthread_mutex_lock
577 #undef pthread_mutex_unlock
578 #undef pthread_mutex_init
579 #undef pthread_mutex_destroy
580
581 /*!
582  * \brief Keep track of which locks a thread holds
583  *
584  * There is an instance of this struct for every active thread
585  */
586 struct thr_lock_info {
587         /*! The thread's ID */
588         pthread_t thread_id;
589         /*! The thread name which includes where the thread was started */
590         const char *thread_name;
591         /*! This is the actual container of info for what locks this thread holds */
592         struct {
593                 const char *file;
594                 int line_num;
595                 const char *func;
596                 const char *lock_name;
597                 void *lock_addr;
598                 int times_locked;
599                 enum ast_lock_type type;
600                 /*! This thread is waiting on this lock */
601                 int pending:2;
602 #ifdef HAVE_BKTR
603                 struct ast_bt *backtrace;
604 #endif
605         } locks[AST_MAX_LOCKS];
606         /*! This is the number of locks currently held by this thread.
607          *  The index (num_locks - 1) has the info on the last one in the
608          *  locks member */
609         unsigned int num_locks;
610         /*! Protects the contents of the locks member
611          * Intentionally not ast_mutex_t */
612         pthread_mutex_t lock;
613         AST_LIST_ENTRY(thr_lock_info) entry;
614 };
615
616 /*!
617  * \brief Locked when accessing the lock_infos list
618  */
619 AST_MUTEX_DEFINE_STATIC(lock_infos_lock);
620 /*!
621  * \brief A list of each thread's lock info
622  */
623 static AST_LIST_HEAD_NOLOCK_STATIC(lock_infos, thr_lock_info);
624
625 /*!
626  * \brief Destroy a thread's lock info
627  *
628  * This gets called automatically when the thread stops
629  */
630 static void lock_info_destroy(void *data)
631 {
632         struct thr_lock_info *lock_info = data;
633         int i;
634
635         pthread_mutex_lock(&lock_infos_lock.mutex);
636         AST_LIST_REMOVE(&lock_infos, lock_info, entry);
637         pthread_mutex_unlock(&lock_infos_lock.mutex);
638
639
640         for (i = 0; i < lock_info->num_locks; i++) {
641                 if (lock_info->locks[i].pending == -1) {
642                         /* This just means that the last lock this thread went for was by
643                          * using trylock, and it failed.  This is fine. */
644                         break;
645                 }
646
647                 ast_log(LOG_ERROR,
648                         "Thread '%s' still has a lock! - '%s' (%p) from '%s' in %s:%d!\n",
649                         lock_info->thread_name,
650                         lock_info->locks[i].lock_name,
651                         lock_info->locks[i].lock_addr,
652                         lock_info->locks[i].func,
653                         lock_info->locks[i].file,
654                         lock_info->locks[i].line_num
655                 );
656         }
657
658         pthread_mutex_destroy(&lock_info->lock);
659         if (lock_info->thread_name)
660                 free((void *) lock_info->thread_name);
661         free(lock_info);
662 }
663
664 /*!
665  * \brief The thread storage key for per-thread lock info
666  */
667 AST_THREADSTORAGE_CUSTOM(thread_lock_info, NULL, lock_info_destroy);
668 #ifdef HAVE_BKTR
669 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
670         int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt)
671 #else
672 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
673         int line_num, const char *func, const char *lock_name, void *lock_addr)
674 #endif
675 {
676         struct thr_lock_info *lock_info;
677         int i;
678
679         if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
680                 return;
681
682         pthread_mutex_lock(&lock_info->lock);
683
684         for (i = 0; i < lock_info->num_locks; i++) {
685                 if (lock_info->locks[i].lock_addr == lock_addr) {
686                         lock_info->locks[i].times_locked++;
687 #ifdef HAVE_BKTR
688                         lock_info->locks[i].backtrace = bt;
689 #endif
690                         pthread_mutex_unlock(&lock_info->lock);
691                         return;
692                 }
693         }
694
695         if (lock_info->num_locks == AST_MAX_LOCKS) {
696                 /* Can't use ast_log here, because it will cause infinite recursion */
697                 fprintf(stderr, "XXX ERROR XXX A thread holds more locks than '%d'."
698                         "  Increase AST_MAX_LOCKS!\n", AST_MAX_LOCKS);
699                 pthread_mutex_unlock(&lock_info->lock);
700                 return;
701         }
702
703         if (i && lock_info->locks[i - 1].pending == -1) {
704                 /* The last lock on the list was one that this thread tried to lock but
705                  * failed at doing so.  It has now moved on to something else, so remove
706                  * the old lock from the list. */
707                 i--;
708                 lock_info->num_locks--;
709                 memset(&lock_info->locks[i], 0, sizeof(lock_info->locks[0]));
710         }
711
712         lock_info->locks[i].file = filename;
713         lock_info->locks[i].line_num = line_num;
714         lock_info->locks[i].func = func;
715         lock_info->locks[i].lock_name = lock_name;
716         lock_info->locks[i].lock_addr = lock_addr;
717         lock_info->locks[i].times_locked = 1;
718         lock_info->locks[i].type = type;
719         lock_info->locks[i].pending = 1;
720 #ifdef HAVE_BKTR
721         lock_info->locks[i].backtrace = bt;
722 #endif
723         lock_info->num_locks++;
724
725         pthread_mutex_unlock(&lock_info->lock);
726 }
727
728 void ast_mark_lock_acquired(void *lock_addr)
729 {
730         struct thr_lock_info *lock_info;
731
732         if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
733                 return;
734
735         pthread_mutex_lock(&lock_info->lock);
736         if (lock_info->locks[lock_info->num_locks - 1].lock_addr == lock_addr) {
737                 lock_info->locks[lock_info->num_locks - 1].pending = 0;
738         }
739         pthread_mutex_unlock(&lock_info->lock);
740 }
741
742 void ast_mark_lock_failed(void *lock_addr)
743 {
744         struct thr_lock_info *lock_info;
745
746         if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
747                 return;
748
749         pthread_mutex_lock(&lock_info->lock);
750         if (lock_info->locks[lock_info->num_locks - 1].lock_addr == lock_addr) {
751                 lock_info->locks[lock_info->num_locks - 1].pending = -1;
752                 lock_info->locks[lock_info->num_locks - 1].times_locked--;
753         }
754         pthread_mutex_unlock(&lock_info->lock);
755 }
756
757 int ast_find_lock_info(void *lock_addr, char *filename, size_t filename_size, int *lineno, char *func, size_t func_size, char *mutex_name, size_t mutex_name_size)
758 {
759         struct thr_lock_info *lock_info;
760         int i = 0;
761
762         if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
763                 return -1;
764
765         pthread_mutex_lock(&lock_info->lock);
766
767         for (i = lock_info->num_locks - 1; i >= 0; i--) {
768                 if (lock_info->locks[i].lock_addr == lock_addr)
769                         break;
770         }
771
772         if (i == -1) {
773                 /* Lock not found :( */
774                 pthread_mutex_unlock(&lock_info->lock);
775                 return -1;
776         }
777
778         ast_copy_string(filename, lock_info->locks[i].file, filename_size);
779         *lineno = lock_info->locks[i].line_num;
780         ast_copy_string(func, lock_info->locks[i].func, func_size);
781         ast_copy_string(mutex_name, lock_info->locks[i].lock_name, mutex_name_size);
782
783         pthread_mutex_unlock(&lock_info->lock);
784
785         return 0;
786 }
787
788 #ifdef HAVE_BKTR
789 void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt)
790 #else
791 void ast_remove_lock_info(void *lock_addr)
792 #endif
793 {
794         struct thr_lock_info *lock_info;
795         int i = 0;
796
797         if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
798                 return;
799
800         pthread_mutex_lock(&lock_info->lock);
801
802         for (i = lock_info->num_locks - 1; i >= 0; i--) {
803                 if (lock_info->locks[i].lock_addr == lock_addr)
804                         break;
805         }
806
807         if (i == -1) {
808                 /* Lock not found :( */
809                 pthread_mutex_unlock(&lock_info->lock);
810                 return;
811         }
812
813         if (lock_info->locks[i].times_locked > 1) {
814                 lock_info->locks[i].times_locked--;
815 #ifdef HAVE_BKTR
816                 lock_info->locks[i].backtrace = bt;
817 #endif
818                 pthread_mutex_unlock(&lock_info->lock);
819                 return;
820         }
821
822         if (i < lock_info->num_locks - 1) {
823                 /* Not the last one ... *should* be rare! */
824                 memmove(&lock_info->locks[i], &lock_info->locks[i + 1],
825                         (lock_info->num_locks - (i + 1)) * sizeof(lock_info->locks[0]));
826         }
827
828         lock_info->num_locks--;
829
830         pthread_mutex_unlock(&lock_info->lock);
831 }
832
833 static const char *locktype2str(enum ast_lock_type type)
834 {
835         switch (type) {
836         case AST_MUTEX:
837                 return "MUTEX";
838         case AST_RDLOCK:
839                 return "RDLOCK";
840         case AST_WRLOCK:
841                 return "WRLOCK";
842         }
843
844         return "UNKNOWN";
845 }
846
847 #ifdef HAVE_BKTR
848 static void append_backtrace_information(struct ast_str **str, struct ast_bt *bt)
849 {
850         char **symbols;
851         int num_frames;
852
853         if (!bt) {
854                 ast_str_append(str, 0, "\tNo backtrace to print\n");
855                 return;
856         }
857
858         /* store frame count locally to avoid the memory corruption that
859          * sometimes happens on virtualized CentOS 6.x systems */
860         num_frames = bt->num_frames;
861         if ((symbols = ast_bt_get_symbols(bt->addresses, num_frames))) {
862                 int frame_iterator;
863
864                 for (frame_iterator = 0; frame_iterator < num_frames; ++frame_iterator) {
865                         ast_str_append(str, 0, "\t%s\n", symbols[frame_iterator]);
866                 }
867
868                 free(symbols);
869         } else {
870                 ast_str_append(str, 0, "\tCouldn't retrieve backtrace symbols\n");
871         }
872 }
873 #endif
874
875 static void append_lock_information(struct ast_str **str, struct thr_lock_info *lock_info, int i)
876 {
877         int j;
878         ast_mutex_t *lock;
879         struct ast_lock_track *lt;
880
881         ast_str_append(str, 0, "=== ---> %sLock #%d (%s): %s %d %s %s %p (%d)\n",
882                                    lock_info->locks[i].pending > 0 ? "Waiting for " :
883                                    lock_info->locks[i].pending < 0 ? "Tried and failed to get " : "", i,
884                                    lock_info->locks[i].file,
885                                    locktype2str(lock_info->locks[i].type),
886                                    lock_info->locks[i].line_num,
887                                    lock_info->locks[i].func, lock_info->locks[i].lock_name,
888                                    lock_info->locks[i].lock_addr,
889                                    lock_info->locks[i].times_locked);
890 #ifdef HAVE_BKTR
891         append_backtrace_information(str, lock_info->locks[i].backtrace);
892 #endif
893
894         if (!lock_info->locks[i].pending || lock_info->locks[i].pending == -1)
895                 return;
896
897         /* We only have further details for mutexes right now */
898         if (lock_info->locks[i].type != AST_MUTEX)
899                 return;
900
901         lock = lock_info->locks[i].lock_addr;
902         lt = lock->track;
903         ast_reentrancy_lock(lt);
904         for (j = 0; *str && j < lt->reentrancy; j++) {
905                 ast_str_append(str, 0, "=== --- ---> Locked Here: %s line %d (%s)\n",
906                                            lt->file[j], lt->lineno[j], lt->func[j]);
907         }
908         ast_reentrancy_unlock(lt);
909 }
910
911
912 /*! This function can help you find highly temporal locks; locks that happen for a
913     short time, but at unexpected times, usually at times that create a deadlock,
914         Why is this thing locked right then? Who is locking it? Who am I fighting
915     with for this lock?
916
917         To answer such questions, just call this routine before you would normally try
918         to aquire a lock. It doesn't do anything if the lock is not acquired. If the
919         lock is taken, it will publish a line or two to the console via ast_log().
920
921         Sometimes, the lock message is pretty uninformative. For instance, you might
922         find that the lock is being aquired deep within the astobj2 code; this tells
923         you little about higher level routines that call the astobj2 routines.
924         But, using gdb, you can set a break at the ast_log below, and for that
925         breakpoint, you can set the commands:
926           where
927           cont
928         which will give a stack trace and continue. -- that aught to do the job!
929
930 */
931 void log_show_lock(void *this_lock_addr)
932 {
933         struct thr_lock_info *lock_info;
934         struct ast_str *str;
935
936         if (!(str = ast_str_create(4096))) {
937                 ast_log(LOG_NOTICE,"Could not create str\n");
938                 return;
939         }
940
941
942         pthread_mutex_lock(&lock_infos_lock.mutex);
943         AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) {
944                 int i;
945                 pthread_mutex_lock(&lock_info->lock);
946                 for (i = 0; str && i < lock_info->num_locks; i++) {
947                         /* ONLY show info about this particular lock, if
948                            it's acquired... */
949                         if (lock_info->locks[i].lock_addr == this_lock_addr) {
950                                 append_lock_information(&str, lock_info, i);
951                                 ast_log(LOG_NOTICE, "%s", ast_str_buffer(str));
952                                 break;
953                         }
954                 }
955                 pthread_mutex_unlock(&lock_info->lock);
956         }
957         pthread_mutex_unlock(&lock_infos_lock.mutex);
958         ast_free(str);
959 }
960
961
962 static char *handle_show_locks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
963 {
964         struct thr_lock_info *lock_info;
965         struct ast_str *str;
966
967         if (!(str = ast_str_create(4096)))
968                 return CLI_FAILURE;
969
970         switch (cmd) {
971         case CLI_INIT:
972                 e->command = "core show locks";
973                 e->usage =
974                         "Usage: core show locks\n"
975                         "       This command is for lock debugging.  It prints out which locks\n"
976                         "are owned by each active thread.\n";
977                 return NULL;
978
979         case CLI_GENERATE:
980                 return NULL;
981         }
982
983         ast_str_append(&str, 0, "\n"
984                        "=======================================================================\n"
985                        "=== %s\n"
986                        "=== Currently Held Locks\n"
987                        "=======================================================================\n"
988                        "===\n"
989                        "=== <pending> <lock#> (<file>): <lock type> <line num> <function> <lock name> <lock addr> (times locked)\n"
990                        "===\n", ast_get_version());
991
992         if (!str)
993                 return CLI_FAILURE;
994
995         pthread_mutex_lock(&lock_infos_lock.mutex);
996         AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) {
997                 int i;
998                 if (lock_info->num_locks) {
999                         ast_str_append(&str, 0, "=== Thread ID: 0x%lx (%s)\n", (long) lock_info->thread_id,
1000                                 lock_info->thread_name);
1001                         pthread_mutex_lock(&lock_info->lock);
1002                         for (i = 0; str && i < lock_info->num_locks; i++) {
1003                                 append_lock_information(&str, lock_info, i);
1004                         }
1005                         pthread_mutex_unlock(&lock_info->lock);
1006                         if (!str)
1007                                 break;
1008                         ast_str_append(&str, 0, "=== -------------------------------------------------------------------\n"
1009                                        "===\n");
1010                         if (!str)
1011                                 break;
1012                 }
1013         }
1014         pthread_mutex_unlock(&lock_infos_lock.mutex);
1015
1016         if (!str)
1017                 return CLI_FAILURE;
1018
1019         ast_str_append(&str, 0, "=======================================================================\n"
1020                        "\n");
1021
1022         if (!str)
1023                 return CLI_FAILURE;
1024
1025         ast_cli(a->fd, "%s", ast_str_buffer(str));
1026
1027         ast_free(str);
1028
1029         return CLI_SUCCESS;
1030 }
1031
1032 static struct ast_cli_entry utils_cli[] = {
1033         AST_CLI_DEFINE(handle_show_locks, "Show which locks are held by which thread"),
1034 };
1035
1036 #endif /* DEBUG_THREADS */
1037
1038 /*
1039  * support for 'show threads'. The start routine is wrapped by
1040  * dummy_start(), so that ast_register_thread() and
1041  * ast_unregister_thread() know the thread identifier.
1042  */
1043 struct thr_arg {
1044         void *(*start_routine)(void *);
1045         void *data;
1046         char *name;
1047 };
1048
1049 /*
1050  * on OS/X, pthread_cleanup_push() and pthread_cleanup_pop()
1051  * are odd macros which start and end a block, so they _must_ be
1052  * used in pairs (the latter with a '1' argument to call the
1053  * handler on exit.
1054  * On BSD we don't need this, but we keep it for compatibility.
1055  */
1056 static void *dummy_start(void *data)
1057 {
1058         void *ret;
1059         struct thr_arg a = *((struct thr_arg *) data);  /* make a local copy */
1060 #ifdef DEBUG_THREADS
1061         struct thr_lock_info *lock_info;
1062         pthread_mutexattr_t mutex_attr;
1063
1064         if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
1065                 return NULL;
1066
1067         lock_info->thread_id = pthread_self();
1068         lock_info->thread_name = strdup(a.name);
1069
1070         pthread_mutexattr_init(&mutex_attr);
1071         pthread_mutexattr_settype(&mutex_attr, AST_MUTEX_KIND);
1072         pthread_mutex_init(&lock_info->lock, &mutex_attr);
1073         pthread_mutexattr_destroy(&mutex_attr);
1074
1075         pthread_mutex_lock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */
1076         AST_LIST_INSERT_TAIL(&lock_infos, lock_info, entry);
1077         pthread_mutex_unlock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */
1078 #endif /* DEBUG_THREADS */
1079
1080         /* note that even though data->name is a pointer to allocated memory,
1081            we are not freeing it here because ast_register_thread is going to
1082            keep a copy of the pointer and then ast_unregister_thread will
1083            free the memory
1084         */
1085         ast_free(data);
1086         ast_register_thread(a.name);
1087         pthread_cleanup_push(ast_unregister_thread, (void *) pthread_self());
1088
1089         ret = a.start_routine(a.data);
1090
1091         pthread_cleanup_pop(1);
1092
1093         return ret;
1094 }
1095
1096 #endif /* !LOW_MEMORY */
1097
1098 int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
1099                              void *data, size_t stacksize, const char *file, const char *caller,
1100                              int line, const char *start_fn)
1101 {
1102 #if !defined(LOW_MEMORY)
1103         struct thr_arg *a;
1104 #endif
1105
1106         if (!attr) {
1107                 attr = ast_alloca(sizeof(*attr));
1108                 pthread_attr_init(attr);
1109         }
1110
1111 #ifdef __linux__
1112         /* On Linux, pthread_attr_init() defaults to PTHREAD_EXPLICIT_SCHED,
1113            which is kind of useless. Change this here to
1114            PTHREAD_INHERIT_SCHED; that way the -p option to set realtime
1115            priority will propagate down to new threads by default.
1116            This does mean that callers cannot set a different priority using
1117            PTHREAD_EXPLICIT_SCHED in the attr argument; instead they must set
1118            the priority afterwards with pthread_setschedparam(). */
1119         if ((errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED)))
1120                 ast_log(LOG_WARNING, "pthread_attr_setinheritsched: %s\n", strerror(errno));
1121 #endif
1122
1123         if (!stacksize)
1124                 stacksize = AST_STACKSIZE;
1125
1126         if ((errno = pthread_attr_setstacksize(attr, stacksize ? stacksize : AST_STACKSIZE)))
1127                 ast_log(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(errno));
1128
1129 #if !defined(LOW_MEMORY)
1130         if ((a = ast_malloc(sizeof(*a)))) {
1131                 a->start_routine = start_routine;
1132                 a->data = data;
1133                 start_routine = dummy_start;
1134                 if (ast_asprintf(&a->name, "%-20s started at [%5d] %s %s()",
1135                              start_fn, line, file, caller) < 0) {
1136                         a->name = NULL;
1137                 }
1138                 data = a;
1139         }
1140 #endif /* !LOW_MEMORY */
1141
1142         return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */
1143 }
1144
1145
1146 int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
1147                              void *data, size_t stacksize, const char *file, const char *caller,
1148                              int line, const char *start_fn)
1149 {
1150         unsigned char attr_destroy = 0;
1151         int res;
1152
1153         if (!attr) {
1154                 attr = ast_alloca(sizeof(*attr));
1155                 pthread_attr_init(attr);
1156                 attr_destroy = 1;
1157         }
1158
1159         if ((errno = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED)))
1160                 ast_log(LOG_WARNING, "pthread_attr_setdetachstate: %s\n", strerror(errno));
1161
1162         res = ast_pthread_create_stack(thread, attr, start_routine, data,
1163                                        stacksize, file, caller, line, start_fn);
1164
1165         if (attr_destroy)
1166                 pthread_attr_destroy(attr);
1167
1168         return res;
1169 }
1170
1171 int ast_wait_for_input(int fd, int ms)
1172 {
1173         struct pollfd pfd[1];
1174         memset(pfd, 0, sizeof(pfd));
1175         pfd[0].fd = fd;
1176         pfd[0].events = POLLIN|POLLPRI;
1177         return ast_poll(pfd, 1, ms);
1178 }
1179
1180 static int ast_wait_for_output(int fd, int timeoutms)
1181 {
1182         struct pollfd pfd = {
1183                 .fd = fd,
1184                 .events = POLLOUT,
1185         };
1186         int res;
1187         struct timeval start = ast_tvnow();
1188         int elapsed = 0;
1189
1190         /* poll() until the fd is writable without blocking */
1191         while ((res = ast_poll(&pfd, 1, timeoutms - elapsed)) <= 0) {
1192                 if (res == 0) {
1193                         /* timed out. */
1194 #ifndef STANDALONE
1195                         ast_debug(1, "Timed out trying to write\n");
1196 #endif
1197                         return -1;
1198                 } else if (res == -1) {
1199                         /* poll() returned an error, check to see if it was fatal */
1200
1201                         if (errno == EINTR || errno == EAGAIN) {
1202                                 elapsed = ast_tvdiff_ms(ast_tvnow(), start);
1203                                 if (elapsed >= timeoutms) {
1204                                         return -1;
1205                                 }
1206                                 /* This was an acceptable error, go back into poll() */
1207                                 continue;
1208                         }
1209
1210                         /* Fatal error, bail. */
1211                         ast_log(LOG_ERROR, "poll returned error: %s\n", strerror(errno));
1212
1213                         return -1;
1214                 }
1215                 elapsed = ast_tvdiff_ms(ast_tvnow(), start);
1216                 if (elapsed >= timeoutms) {
1217                         return -1;
1218                 }
1219         }
1220
1221         return 0;
1222 }
1223
1224 /*!
1225  * Try to write string, but wait no more than ms milliseconds before timing out.
1226  *
1227  * \note The code assumes that the file descriptor has NONBLOCK set,
1228  * so there is only one system call made to do a write, unless we actually
1229  * have a need to wait.  This way, we get better performance.
1230  * If the descriptor is blocking, all assumptions on the guaranteed
1231  * detail do not apply anymore.
1232  */
1233 int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
1234 {
1235         struct timeval start = ast_tvnow();
1236         int res = 0;
1237         int elapsed = 0;
1238
1239         while (len) {
1240                 if (ast_wait_for_output(fd, timeoutms - elapsed)) {
1241                         return -1;
1242                 }
1243
1244                 res = write(fd, s, len);
1245
1246                 if (res < 0 && errno != EAGAIN && errno != EINTR) {
1247                         /* fatal error from write() */
1248                         ast_log(LOG_ERROR, "write() returned error: %s\n", strerror(errno));
1249                         return -1;
1250                 }
1251
1252                 if (res < 0) {
1253                         /* It was an acceptable error */
1254                         res = 0;
1255                 }
1256
1257                 /* Update how much data we have left to write */
1258                 len -= res;
1259                 s += res;
1260                 res = 0;
1261
1262                 elapsed = ast_tvdiff_ms(ast_tvnow(), start);
1263                 if (elapsed >= timeoutms) {
1264                         /* We've taken too long to write
1265                          * This is only an error condition if we haven't finished writing. */
1266                         res = len ? -1 : 0;
1267                         break;
1268                 }
1269         }
1270
1271         return res;
1272 }
1273
1274 int ast_careful_fwrite(FILE *f, int fd, const char *src, size_t len, int timeoutms)
1275 {
1276         struct timeval start = ast_tvnow();
1277         int n = 0;
1278         int elapsed = 0;
1279
1280         while (len) {
1281                 if (ast_wait_for_output(fd, timeoutms - elapsed)) {
1282                         /* poll returned a fatal error, so bail out immediately. */
1283                         return -1;
1284                 }
1285
1286                 /* Clear any errors from a previous write */
1287                 clearerr(f);
1288
1289                 n = fwrite(src, 1, len, f);
1290
1291                 if (ferror(f) && errno != EINTR && errno != EAGAIN) {
1292                         /* fatal error from fwrite() */
1293                         if (!feof(f)) {
1294                                 /* Don't spam the logs if it was just that the connection is closed. */
1295                                 ast_log(LOG_ERROR, "fwrite() returned error: %s\n", strerror(errno));
1296                         }
1297                         n = -1;
1298                         break;
1299                 }
1300
1301                 /* Update for data already written to the socket */
1302                 len -= n;
1303                 src += n;
1304
1305                 elapsed = ast_tvdiff_ms(ast_tvnow(), start);
1306                 if (elapsed >= timeoutms) {
1307                         /* We've taken too long to write
1308                          * This is only an error condition if we haven't finished writing. */
1309                         n = len ? -1 : 0;
1310                         break;
1311                 }
1312         }
1313
1314         while (fflush(f)) {
1315                 if (errno == EAGAIN || errno == EINTR) {
1316                         continue;
1317                 }
1318                 if (!feof(f)) {
1319                         /* Don't spam the logs if it was just that the connection is closed. */
1320                         ast_log(LOG_ERROR, "fflush() returned error: %s\n", strerror(errno));
1321                 }
1322                 n = -1;
1323                 break;
1324         }
1325
1326         return n < 0 ? -1 : 0;
1327 }
1328
1329 char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
1330 {
1331         char *e;
1332         char *q;
1333
1334         s = ast_strip(s);
1335         if ((q = strchr(beg_quotes, *s)) && *q != '\0') {
1336                 e = s + strlen(s) - 1;
1337                 if (*e == *(end_quotes + (q - beg_quotes))) {
1338                         s++;
1339                         *e = '\0';
1340                 }
1341         }
1342
1343         return s;
1344 }
1345
1346 char *ast_unescape_semicolon(char *s)
1347 {
1348         char *e;
1349         char *work = s;
1350
1351         while ((e = strchr(work, ';'))) {
1352                 if ((e > work) && (*(e-1) == '\\')) {
1353                         memmove(e - 1, e, strlen(e) + 1);
1354                         work = e;
1355                 } else {
1356                         work = e + 1;
1357                 }
1358         }
1359
1360         return s;
1361 }
1362
1363 /* !\brief unescape some C sequences in place, return pointer to the original string.
1364  */
1365 char *ast_unescape_c(char *src)
1366 {
1367         char c, *ret, *dst;
1368
1369         if (src == NULL)
1370                 return NULL;
1371         for (ret = dst = src; (c = *src++); *dst++ = c ) {
1372                 if (c != '\\')
1373                         continue;       /* copy char at the end of the loop */
1374                 switch ((c = *src++)) {
1375                 case '\0':      /* special, trailing '\' */
1376                         c = '\\';
1377                         break;
1378                 case 'b':       /* backspace */
1379                         c = '\b';
1380                         break;
1381                 case 'f':       /* form feed */
1382                         c = '\f';
1383                         break;
1384                 case 'n':
1385                         c = '\n';
1386                         break;
1387                 case 'r':
1388                         c = '\r';
1389                         break;
1390                 case 't':
1391                         c = '\t';
1392                         break;
1393                 }
1394                 /* default, use the char literally */
1395         }
1396         *dst = '\0';
1397         return ret;
1398 }
1399
1400 int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap)
1401 {
1402         int result;
1403
1404         if (!buffer || !*buffer || !space || !*space)
1405                 return -1;
1406
1407         result = vsnprintf(*buffer, *space, fmt, ap);
1408
1409         if (result < 0)
1410                 return -1;
1411         else if (result > *space)
1412                 result = *space;
1413
1414         *buffer += result;
1415         *space -= result;
1416         return 0;
1417 }
1418
1419 int ast_build_string(char **buffer, size_t *space, const char *fmt, ...)
1420 {
1421         va_list ap;
1422         int result;
1423
1424         va_start(ap, fmt);
1425         result = ast_build_string_va(buffer, space, fmt, ap);
1426         va_end(ap);
1427
1428         return result;
1429 }
1430
1431 int ast_regex_string_to_regex_pattern(const char *regex_string, struct ast_str **regex_pattern)
1432 {
1433         int regex_len = strlen(regex_string);
1434         int ret = 3;
1435
1436         /* Chop off the leading / if there is one */
1437         if ((regex_len >= 1) && (regex_string[0] == '/')) {
1438                 ast_str_set(regex_pattern, 0, "%s", regex_string + 1);
1439                 ret -= 2;
1440         }
1441
1442         /* Chop off the ending / if there is one */
1443         if ((regex_len > 1) && (regex_string[regex_len - 1] == '/')) {
1444                 ast_str_truncate(*regex_pattern, -1);
1445                 ret -= 1;
1446         }
1447
1448         return ret;
1449 }
1450
1451 int ast_true(const char *s)
1452 {
1453         if (ast_strlen_zero(s))
1454                 return 0;
1455
1456         /* Determine if this is a true value */
1457         if (!strcasecmp(s, "yes") ||
1458             !strcasecmp(s, "true") ||
1459             !strcasecmp(s, "y") ||
1460             !strcasecmp(s, "t") ||
1461             !strcasecmp(s, "1") ||
1462             !strcasecmp(s, "on"))
1463                 return -1;
1464
1465         return 0;
1466 }
1467
1468 int ast_false(const char *s)
1469 {
1470         if (ast_strlen_zero(s))
1471                 return 0;
1472
1473         /* Determine if this is a false value */
1474         if (!strcasecmp(s, "no") ||
1475             !strcasecmp(s, "false") ||
1476             !strcasecmp(s, "n") ||
1477             !strcasecmp(s, "f") ||
1478             !strcasecmp(s, "0") ||
1479             !strcasecmp(s, "off"))
1480                 return -1;
1481
1482         return 0;
1483 }
1484
1485 #define ONE_MILLION     1000000
1486 /*
1487  * put timeval in a valid range. usec is 0..999999
1488  * negative values are not allowed and truncated.
1489  */
1490 static struct timeval tvfix(struct timeval a)
1491 {
1492         if (a.tv_usec >= ONE_MILLION) {
1493                 ast_log(LOG_WARNING, "warning too large timestamp %ld.%ld\n",
1494                         (long)a.tv_sec, (long int) a.tv_usec);
1495                 a.tv_sec += a.tv_usec / ONE_MILLION;
1496                 a.tv_usec %= ONE_MILLION;
1497         } else if (a.tv_usec < 0) {
1498                 ast_log(LOG_WARNING, "warning negative timestamp %ld.%ld\n",
1499                         (long)a.tv_sec, (long int) a.tv_usec);
1500                 a.tv_usec = 0;
1501         }
1502         return a;
1503 }
1504
1505 struct timeval ast_tvadd(struct timeval a, struct timeval b)
1506 {
1507         /* consistency checks to guarantee usec in 0..999999 */
1508         a = tvfix(a);
1509         b = tvfix(b);
1510         a.tv_sec += b.tv_sec;
1511         a.tv_usec += b.tv_usec;
1512         if (a.tv_usec >= ONE_MILLION) {
1513                 a.tv_sec++;
1514                 a.tv_usec -= ONE_MILLION;
1515         }
1516         return a;
1517 }
1518
1519 struct timeval ast_tvsub(struct timeval a, struct timeval b)
1520 {
1521         /* consistency checks to guarantee usec in 0..999999 */
1522         a = tvfix(a);
1523         b = tvfix(b);
1524         a.tv_sec -= b.tv_sec;
1525         a.tv_usec -= b.tv_usec;
1526         if (a.tv_usec < 0) {
1527                 a.tv_sec-- ;
1528                 a.tv_usec += ONE_MILLION;
1529         }
1530         return a;
1531 }
1532
1533 int ast_remaining_ms(struct timeval start, int max_ms)
1534 {
1535         int ms;
1536
1537         if (max_ms < 0) {
1538                 ms = max_ms;
1539         } else {
1540                 ms = max_ms - ast_tvdiff_ms(ast_tvnow(), start);
1541                 if (ms < 0) {
1542                         ms = 0;
1543                 }
1544         }
1545
1546         return ms;
1547 }
1548
1549 #undef ONE_MILLION
1550
1551 #ifndef linux
1552 AST_MUTEX_DEFINE_STATIC(randomlock);
1553 #endif
1554
1555 long int ast_random(void)
1556 {
1557         long int res;
1558
1559         if (dev_urandom_fd >= 0) {
1560                 int read_res = read(dev_urandom_fd, &res, sizeof(res));
1561                 if (read_res > 0) {
1562                         long int rm = RAND_MAX;
1563                         res = res < 0 ? ~res : res;
1564                         rm++;
1565                         return res % rm;
1566                 }
1567         }
1568
1569         /* XXX - Thread safety really depends on the libc, not the OS.
1570          *
1571          * But... popular Linux libc's (uClibc, glibc, eglibc), all have a
1572          * somewhat thread safe random(3) (results are random, but not
1573          * reproducible). The libc's for other systems (BSD, et al.), not so
1574          * much.
1575          */
1576 #ifdef linux
1577         res = random();
1578 #else
1579         ast_mutex_lock(&randomlock);
1580         res = random();
1581         ast_mutex_unlock(&randomlock);
1582 #endif
1583         return res;
1584 }
1585
1586 void ast_replace_subargument_delimiter(char *s)
1587 {
1588         for (; *s; s++) {
1589                 if (*s == '^') {
1590                         *s = ',';
1591                 }
1592         }
1593 }
1594
1595 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
1596 {
1597         char *dataPut = start;
1598         int inEscape = 0;
1599         int inQuotes = 0;
1600
1601         for (; *start; start++) {
1602                 if (inEscape) {
1603                         *dataPut++ = *start;       /* Always goes verbatim */
1604                         inEscape = 0;
1605                 } else {
1606                         if (*start == '\\') {
1607                                 inEscape = 1;      /* Do not copy \ into the data */
1608                         } else if (*start == '\'') {
1609                                 inQuotes = 1 - inQuotes;   /* Do not copy ' into the data */
1610                         } else {
1611                                 /* Replace , with |, unless in quotes */
1612                                 *dataPut++ = inQuotes ? *start : ((*start == find) ? replace_with : *start);
1613                         }
1614                 }
1615         }
1616         if (start != dataPut)
1617                 *dataPut = 0;
1618         return dataPut;
1619 }
1620
1621 void ast_join(char *s, size_t len, const char * const w[])
1622 {
1623         int x, ofs = 0;
1624         const char *src;
1625
1626         /* Join words into a string */
1627         if (!s)
1628                 return;
1629         for (x = 0; ofs < len && w[x]; x++) {
1630                 if (x > 0)
1631                         s[ofs++] = ' ';
1632                 for (src = w[x]; *src && ofs < len; src++)
1633                         s[ofs++] = *src;
1634         }
1635         if (ofs == len)
1636                 ofs--;
1637         s[ofs] = '\0';
1638 }
1639
1640 /*
1641  * stringfields support routines.
1642  */
1643
1644 /* this is a little complex... string fields are stored with their
1645    allocated size in the bytes preceding the string; even the
1646    constant 'empty' string has to be this way, so the code that
1647    checks to see if there is enough room for a new string doesn't
1648    have to have any special case checks
1649 */
1650
1651 static const struct {
1652         ast_string_field_allocation allocation;
1653         char string[1];
1654 } __ast_string_field_empty_buffer;
1655
1656 ast_string_field __ast_string_field_empty = __ast_string_field_empty_buffer.string;
1657
1658 #define ALLOCATOR_OVERHEAD 48
1659
1660 static size_t optimal_alloc_size(size_t size)
1661 {
1662         unsigned int count;
1663
1664         size += ALLOCATOR_OVERHEAD;
1665
1666         for (count = 1; size; size >>= 1, count++);
1667
1668         return (1 << count) - ALLOCATOR_OVERHEAD;
1669 }
1670
1671 /*! \brief add a new block to the pool.
1672  * We can only allocate from the topmost pool, so the
1673  * fields in *mgr reflect the size of that only.
1674  */
1675 static int add_string_pool(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
1676                            size_t size, const char *file, int lineno, const char *func)
1677 {
1678         struct ast_string_field_pool *pool;
1679         size_t alloc_size = optimal_alloc_size(sizeof(*pool) + size);
1680
1681 #if defined(__AST_DEBUG_MALLOC)
1682         if (!(pool = __ast_calloc(1, alloc_size, file, lineno, func))) {
1683                 return -1;
1684         }
1685 #else
1686         if (!(pool = ast_calloc(1, alloc_size))) {
1687                 return -1;
1688         }
1689 #endif
1690
1691         pool->prev = *pool_head;
1692         pool->size = alloc_size - sizeof(*pool);
1693         *pool_head = pool;
1694         mgr->last_alloc = NULL;
1695
1696         return 0;
1697 }
1698
1699 /*
1700  * This is an internal API, code should not use it directly.
1701  * It initializes all fields as empty, then uses 'size' for 3 functions:
1702  * size > 0 means initialize the pool list with a pool of given size.
1703  *      This must be called right after allocating the object.
1704  * size = 0 means release all pools except the most recent one.
1705  *      If the first pool was allocated via embedding in another
1706  *      object, that pool will be preserved instead.
1707  *      This is useful to e.g. reset an object to the initial value.
1708  * size < 0 means release all pools.
1709  *      This must be done before destroying the object.
1710  */
1711 int __ast_string_field_init(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
1712                             int needed, const char *file, int lineno, const char *func)
1713 {
1714         const char **p = (const char **) pool_head + 1;
1715         struct ast_string_field_pool *cur = NULL;
1716         struct ast_string_field_pool *preserve = NULL;
1717
1718         /* clear fields - this is always necessary */
1719         while ((struct ast_string_field_mgr *) p != mgr) {
1720                 *p++ = __ast_string_field_empty;
1721         }
1722
1723         mgr->last_alloc = NULL;
1724 #if defined(__AST_DEBUG_MALLOC)
1725         mgr->owner_file = file;
1726         mgr->owner_func = func;
1727         mgr->owner_line = lineno;
1728 #endif
1729         if (needed > 0) {               /* allocate the initial pool */
1730                 *pool_head = NULL;
1731                 mgr->embedded_pool = NULL;
1732                 return add_string_pool(mgr, pool_head, needed, file, lineno, func);
1733         }
1734
1735         /* if there is an embedded pool, we can't actually release *all*
1736          * pools, we must keep the embedded one. if the caller is about
1737          * to free the structure that contains the stringfield manager
1738          * and embedded pool anyway, it will be freed as part of that
1739          * operation.
1740          */
1741         if ((needed < 0) && mgr->embedded_pool) {
1742                 needed = 0;
1743         }
1744
1745         if (needed < 0) {               /* reset all pools */
1746                 cur = *pool_head;
1747         } else if (mgr->embedded_pool) { /* preserve the embedded pool */
1748                 preserve = mgr->embedded_pool;
1749                 cur = *pool_head;
1750         } else {                        /* preserve the last pool */
1751                 if (*pool_head == NULL) {
1752                         ast_log(LOG_WARNING, "trying to reset empty pool\n");
1753                         return -1;
1754                 }
1755                 preserve = *pool_head;
1756                 cur = preserve->prev;
1757         }
1758
1759         if (preserve) {
1760                 preserve->prev = NULL;
1761                 preserve->used = preserve->active = 0;
1762         }
1763
1764         while (cur) {
1765                 struct ast_string_field_pool *prev = cur->prev;
1766
1767                 if (cur != preserve) {
1768                         ast_free(cur);
1769                 }
1770                 cur = prev;
1771         }
1772
1773         *pool_head = preserve;
1774
1775         return 0;
1776 }
1777
1778 ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr,
1779                                                 struct ast_string_field_pool **pool_head, size_t needed)
1780 {
1781         char *result = NULL;
1782         size_t space = (*pool_head)->size - (*pool_head)->used;
1783         size_t to_alloc;
1784
1785         /* Make room for ast_string_field_allocation and make it a multiple of that. */
1786         to_alloc = ast_make_room_for(needed, ast_string_field_allocation);
1787         ast_assert(to_alloc % ast_alignof(ast_string_field_allocation) == 0);
1788
1789         if (__builtin_expect(to_alloc > space, 0)) {
1790                 size_t new_size = (*pool_head)->size;
1791
1792                 while (new_size < to_alloc) {
1793                         new_size *= 2;
1794                 }
1795
1796 #if defined(__AST_DEBUG_MALLOC)
1797                 if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func))
1798                         return NULL;
1799 #else
1800                 if (add_string_pool(mgr, pool_head, new_size, __FILE__, __LINE__, __FUNCTION__))
1801                         return NULL;
1802 #endif
1803         }
1804
1805         /* pool->base is always aligned (gcc aligned attribute). We ensure that
1806          * to_alloc is also a multiple of ast_alignof(ast_string_field_allocation)
1807          * causing result to always be aligned as well; which in turn fixes that
1808          * AST_STRING_FIELD_ALLOCATION(result) is aligned. */
1809         result = (*pool_head)->base + (*pool_head)->used;
1810         (*pool_head)->used += to_alloc;
1811         (*pool_head)->active += needed;
1812         result += ast_alignof(ast_string_field_allocation);
1813         AST_STRING_FIELD_ALLOCATION(result) = needed;
1814         mgr->last_alloc = result;
1815
1816         return result;
1817 }
1818
1819 int __ast_string_field_ptr_grow(struct ast_string_field_mgr *mgr,
1820                                 struct ast_string_field_pool **pool_head, size_t needed,
1821                                 const ast_string_field *ptr)
1822 {
1823         ssize_t grow = needed - AST_STRING_FIELD_ALLOCATION(*ptr);
1824         size_t space = (*pool_head)->size - (*pool_head)->used;
1825
1826         if (*ptr != mgr->last_alloc) {
1827                 return 1;
1828         }
1829
1830         if (space < grow) {
1831                 return 1;
1832         }
1833
1834         (*pool_head)->used += grow;
1835         (*pool_head)->active += grow;
1836         AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
1837
1838         return 0;
1839 }
1840
1841 void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
1842                                        const ast_string_field ptr)
1843 {
1844         struct ast_string_field_pool *pool, *prev;
1845
1846         if (ptr == __ast_string_field_empty) {
1847                 return;
1848         }
1849
1850         for (pool = pool_head, prev = NULL; pool; prev = pool, pool = pool->prev) {
1851                 if ((ptr >= pool->base) && (ptr <= (pool->base + pool->size))) {
1852                         pool->active -= AST_STRING_FIELD_ALLOCATION(ptr);
1853                         if ((pool->active == 0) && prev) {
1854                                 prev->prev = pool->prev;
1855                                 ast_free(pool);
1856                         }
1857                         break;
1858                 }
1859         }
1860 }
1861
1862 void __ast_string_field_ptr_build_va(struct ast_string_field_mgr *mgr,
1863                                      struct ast_string_field_pool **pool_head,
1864                                      ast_string_field *ptr, const char *format, va_list ap)
1865 {
1866         size_t needed;
1867         size_t available;
1868         size_t space = (*pool_head)->size - (*pool_head)->used;
1869         ssize_t grow;
1870         char *target;
1871         va_list ap2;
1872
1873         /* if the field already has space allocated, try to reuse it;
1874            otherwise, try to use the empty space at the end of the current
1875            pool
1876         */
1877         if (*ptr != __ast_string_field_empty) {
1878                 target = (char *) *ptr;
1879                 available = AST_STRING_FIELD_ALLOCATION(*ptr);
1880                 if (*ptr == mgr->last_alloc) {
1881                         available += space;
1882                 }
1883         } else {
1884                 /* pool->used is always a multiple of ast_alignof(ast_string_field_allocation)
1885                  * so we don't need to re-align anything here.
1886                  */
1887                 target = (*pool_head)->base + (*pool_head)->used + ast_alignof(ast_string_field_allocation);
1888                 available = space - ast_alignof(ast_string_field_allocation);
1889         }
1890
1891         va_copy(ap2, ap);
1892         needed = vsnprintf(target, available, format, ap2) + 1;
1893         va_end(ap2);
1894
1895         if (needed > available) {
1896                 /* the allocation could not be satisfied using the field's current allocation
1897                    (if it has one), or the space available in the pool (if it does not). allocate
1898                    space for it, adding a new string pool if necessary.
1899                 */
1900                 if (!(target = (char *) __ast_string_field_alloc_space(mgr, pool_head, needed))) {
1901                         return;
1902                 }
1903                 vsprintf(target, format, ap);
1904                 va_end(ap); /* XXX va_end without va_start? */
1905                 __ast_string_field_release_active(*pool_head, *ptr);
1906                 *ptr = target;
1907         } else if (*ptr != target) {
1908                 /* the allocation was satisfied using available space in the pool, but not
1909                    using the space already allocated to the field
1910                 */
1911                 __ast_string_field_release_active(*pool_head, *ptr);
1912                 mgr->last_alloc = *ptr = target;
1913                 AST_STRING_FIELD_ALLOCATION(target) = needed;
1914                 (*pool_head)->used += ast_make_room_for(needed, ast_string_field_allocation);
1915                 (*pool_head)->active += needed;
1916         } else if ((grow = (needed - AST_STRING_FIELD_ALLOCATION(*ptr))) > 0) {
1917                 /* the allocation was satisfied by using available space in the pool *and*
1918                    the field was the last allocated field from the pool, so it grew
1919                 */
1920                 AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
1921                 (*pool_head)->used += ast_align_for(grow, ast_string_field_allocation);
1922                 (*pool_head)->active += grow;
1923         }
1924 }
1925
1926 void __ast_string_field_ptr_build(struct ast_string_field_mgr *mgr,
1927                                   struct ast_string_field_pool **pool_head,
1928                                   ast_string_field *ptr, const char *format, ...)
1929 {
1930         va_list ap;
1931
1932         va_start(ap, format);
1933         __ast_string_field_ptr_build_va(mgr, pool_head, ptr, format, ap);
1934         va_end(ap);
1935 }
1936
1937 void *__ast_calloc_with_stringfields(unsigned int num_structs, size_t struct_size, size_t field_mgr_offset,
1938                                      size_t field_mgr_pool_offset, size_t pool_size, const char *file,
1939                                      int lineno, const char *func)
1940 {
1941         struct ast_string_field_mgr *mgr;
1942         struct ast_string_field_pool *pool;
1943         struct ast_string_field_pool **pool_head;
1944         size_t pool_size_needed = sizeof(*pool) + pool_size;
1945         size_t size_to_alloc = optimal_alloc_size(struct_size + pool_size_needed);
1946         void *allocation;
1947         unsigned int x;
1948
1949 #if defined(__AST_DEBUG_MALLOC)
1950         if (!(allocation = __ast_calloc(num_structs, size_to_alloc, file, lineno, func))) {
1951                 return NULL;
1952         }
1953 #else
1954         if (!(allocation = ast_calloc(num_structs, size_to_alloc))) {
1955                 return NULL;
1956         }
1957 #endif
1958
1959         for (x = 0; x < num_structs; x++) {
1960                 void *base = allocation + (size_to_alloc * x);
1961                 const char **p;
1962
1963                 mgr = base + field_mgr_offset;
1964                 pool_head = base + field_mgr_pool_offset;
1965                 pool = base + struct_size;
1966
1967                 p = (const char **) pool_head + 1;
1968                 while ((struct ast_string_field_mgr *) p != mgr) {
1969                         *p++ = __ast_string_field_empty;
1970                 }
1971
1972                 mgr->embedded_pool = pool;
1973                 *pool_head = pool;
1974                 pool->size = size_to_alloc - struct_size - sizeof(*pool);
1975 #if defined(__AST_DEBUG_MALLOC)
1976                 mgr->owner_file = file;
1977                 mgr->owner_func = func;
1978                 mgr->owner_line = lineno;
1979 #endif
1980         }
1981
1982         return allocation;
1983 }
1984
1985 /* end of stringfields support */
1986
1987 AST_MUTEX_DEFINE_STATIC(fetchadd_m); /* used for all fetc&add ops */
1988
1989 int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
1990 {
1991         int ret;
1992         ast_mutex_lock(&fetchadd_m);
1993         ret = *p;
1994         *p += v;
1995         ast_mutex_unlock(&fetchadd_m);
1996         return ret;
1997 }
1998
1999 /*! \brief
2000  * get values from config variables.
2001  */
2002 int ast_get_timeval(const char *src, struct timeval *dst, struct timeval _default, int *consumed)
2003 {
2004         long double dtv = 0.0;
2005         int scanned;
2006
2007         if (dst == NULL)
2008                 return -1;
2009
2010         *dst = _default;
2011
2012         if (ast_strlen_zero(src))
2013                 return -1;
2014
2015         /* only integer at the moment, but one day we could accept more formats */
2016         if (sscanf(src, "%30Lf%n", &dtv, &scanned) > 0) {
2017                 dst->tv_sec = dtv;
2018                 dst->tv_usec = (dtv - dst->tv_sec) * 1000000.0;
2019                 if (consumed)
2020                         *consumed = scanned;
2021                 return 0;
2022         } else
2023                 return -1;
2024 }
2025
2026 /*! \brief
2027  * get values from config variables.
2028  */
2029 int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
2030 {
2031         long t;
2032         int scanned;
2033
2034         if (dst == NULL)
2035                 return -1;
2036
2037         *dst = _default;
2038
2039         if (ast_strlen_zero(src))
2040                 return -1;
2041
2042         /* only integer at the moment, but one day we could accept more formats */
2043         if (sscanf(src, "%30ld%n", &t, &scanned) == 1) {
2044                 *dst = t;
2045                 if (consumed)
2046                         *consumed = scanned;
2047                 return 0;
2048         } else
2049                 return -1;
2050 }
2051
2052 void ast_enable_packet_fragmentation(int sock)
2053 {
2054 #if defined(HAVE_IP_MTU_DISCOVER)
2055         int val = IP_PMTUDISC_DONT;
2056
2057         if (setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)))
2058                 ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n");
2059 #endif /* HAVE_IP_MTU_DISCOVER */
2060 }
2061
2062 int ast_mkdir(const char *path, int mode)
2063 {
2064         char *ptr;
2065         int len = strlen(path), count = 0, x, piececount = 0;
2066         char *tmp = ast_strdupa(path);
2067         char **pieces;
2068         char *fullpath = ast_alloca(len + 1);
2069         int res = 0;
2070
2071         for (ptr = tmp; *ptr; ptr++) {
2072                 if (*ptr == '/')
2073                         count++;
2074         }
2075
2076         /* Count the components to the directory path */
2077         pieces = ast_alloca(count * sizeof(*pieces));
2078         for (ptr = tmp; *ptr; ptr++) {
2079                 if (*ptr == '/') {
2080                         *ptr = '\0';
2081                         pieces[piececount++] = ptr + 1;
2082                 }
2083         }
2084
2085         *fullpath = '\0';
2086         for (x = 0; x < piececount; x++) {
2087                 /* This looks funky, but the buffer is always ideally-sized, so it's fine. */
2088                 strcat(fullpath, "/");
2089                 strcat(fullpath, pieces[x]);
2090                 res = mkdir(fullpath, mode);
2091                 if (res && errno != EEXIST)
2092                         return errno;
2093         }
2094         return 0;
2095 }
2096
2097 int ast_utils_init(void)
2098 {
2099         dev_urandom_fd = open("/dev/urandom", O_RDONLY);
2100         base64_init();
2101 #ifdef DEBUG_THREADS
2102 #if !defined(LOW_MEMORY)
2103         ast_cli_register_multiple(utils_cli, ARRAY_LEN(utils_cli));
2104 #endif
2105 #endif
2106         return 0;
2107 }
2108
2109
2110 /*!
2111  *\brief Parse digest authorization header.
2112  *\return Returns -1 if we have no auth or something wrong with digest.
2113  *\note This function may be used for Digest request and responce header.
2114  * request arg is set to nonzero, if we parse Digest Request.
2115  * pedantic arg can be set to nonzero if we need to do addition Digest check.
2116  */
2117 int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic) {
2118         char *c;
2119         struct ast_str *str = ast_str_create(16);
2120
2121         /* table of recognised keywords, and places where they should be copied */
2122         const struct x {
2123                 const char *key;
2124                 const ast_string_field *field;
2125         } *i, keys[] = {
2126                 { "username=", &d->username },
2127                 { "realm=", &d->realm },
2128                 { "nonce=", &d->nonce },
2129                 { "uri=", &d->uri },
2130                 { "domain=", &d->domain },
2131                 { "response=", &d->response },
2132                 { "cnonce=", &d->cnonce },
2133                 { "opaque=", &d->opaque },
2134                 /* Special cases that cannot be directly copied */
2135                 { "algorithm=", NULL },
2136                 { "qop=", NULL },
2137                 { "nc=", NULL },
2138                 { NULL, 0 },
2139         };
2140
2141         if (ast_strlen_zero(digest) || !d || !str) {
2142                 ast_free(str);
2143                 return -1;
2144         }
2145
2146         ast_str_set(&str, 0, "%s", digest);
2147
2148         c = ast_skip_blanks(ast_str_buffer(str));
2149
2150         if (strncasecmp(c, "Digest ", strlen("Digest "))) {
2151                 ast_log(LOG_WARNING, "Missing Digest.\n");
2152                 ast_free(str);
2153                 return -1;
2154         }
2155         c += strlen("Digest ");
2156
2157         /* lookup for keys/value pair */
2158         while (c && *c && *(c = ast_skip_blanks(c))) {
2159                 /* find key */
2160                 for (i = keys; i->key != NULL; i++) {
2161                         char *src, *separator;
2162                         int unescape = 0;
2163                         if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
2164                                 continue;
2165                         }
2166
2167                         /* Found. Skip keyword, take text in quotes or up to the separator. */
2168                         c += strlen(i->key);
2169                         if (*c == '"') {
2170                                 src = ++c;
2171                                 separator = "\"";
2172                                 unescape = 1;
2173                         } else {
2174                                 src = c;
2175                                 separator = ",";
2176                         }
2177                         strsep(&c, separator); /* clear separator and move ptr */
2178                         if (unescape) {
2179                                 ast_unescape_c(src);
2180                         }
2181                         if (i->field) {
2182                                 ast_string_field_ptr_set(d, i->field, src);
2183                         } else {
2184                                 /* Special cases that require additional procesing */
2185                                 if (!strcasecmp(i->key, "algorithm=")) {
2186                                         if (strcasecmp(src, "MD5")) {
2187                                                 ast_log(LOG_WARNING, "Digest algorithm: \"%s\" not supported.\n", src);
2188                                                 ast_free(str);
2189                                                 return -1;
2190                                         }
2191                                 } else if (!strcasecmp(i->key, "qop=") && !strcasecmp(src, "auth")) {
2192                                         d->qop = 1;
2193                                 } else if (!strcasecmp(i->key, "nc=")) {
2194                                         unsigned long u;
2195                                         if (sscanf(src, "%30lx", &u) != 1) {
2196                                                 ast_log(LOG_WARNING, "Incorrect Digest nc value: \"%s\".\n", src);
2197                                                 ast_free(str);
2198                                                 return -1;
2199                                         }
2200                                         ast_string_field_set(d, nc, src);
2201                                 }
2202                         }
2203                         break;
2204                 }
2205                 if (i->key == NULL) { /* not found, try ',' */
2206                         strsep(&c, ",");
2207                 }
2208         }
2209         ast_free(str);
2210
2211         /* Digest checkout */
2212         if (ast_strlen_zero(d->realm) || ast_strlen_zero(d->nonce)) {
2213                 /* "realm" and "nonce" MUST be always exist */
2214                 return -1;
2215         }
2216
2217         if (!request) {
2218                 /* Additional check for Digest response */
2219                 if (ast_strlen_zero(d->username) || ast_strlen_zero(d->uri) || ast_strlen_zero(d->response)) {
2220                         return -1;
2221                 }
2222
2223                 if (pedantic && d->qop && (ast_strlen_zero(d->cnonce) || ast_strlen_zero(d->nc))) {
2224                         return -1;
2225                 }
2226         }
2227
2228         return 0;
2229 }
2230
2231 #ifndef __AST_DEBUG_MALLOC
2232 int _ast_asprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, ...)
2233 {
2234         int res;
2235         va_list ap;
2236
2237         va_start(ap, fmt);
2238         if ((res = vasprintf(ret, fmt, ap)) == -1) {
2239                 MALLOC_FAILURE_MSG;
2240         }
2241         va_end(ap);
2242
2243         return res;
2244 }
2245 #endif
2246
2247 int ast_get_tid(void)
2248 {
2249         int ret = -1;
2250 #if defined (__linux) && defined(SYS_gettid)
2251         ret = syscall(SYS_gettid); /* available since Linux 1.4.11 */
2252 #elif defined(__sun)
2253         ret = pthread_self();
2254 #elif defined(__APPLE__)
2255         ret = mach_thread_self();
2256         mach_port_deallocate(mach_task_self(), ret);
2257 #elif defined(__FreeBSD__) && defined(HAVE_SYS_THR_H)
2258         long lwpid;
2259         thr_self(&lwpid); /* available since sys/thr.h creation 2003 */
2260         ret = lwpid;
2261 #endif
2262         return ret;
2263 }
2264
2265 char *ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size)
2266 {
2267         const char *envPATH = getenv("PATH");
2268         char *tpath, *path;
2269         struct stat unused;
2270         if (!envPATH) {
2271                 return NULL;
2272         }
2273         tpath = ast_strdupa(envPATH);
2274         while ((path = strsep(&tpath, ":"))) {
2275                 snprintf(fullpath, fullpath_size, "%s/%s", path, binary);
2276                 if (!stat(fullpath, &unused)) {
2277                         return fullpath;
2278                 }
2279         }
2280         return NULL;
2281 }
2282
2283 void ast_do_crash(void)
2284 {
2285 #if defined(DO_CRASH)
2286         abort();
2287         /*
2288          * Just in case abort() doesn't work or something else super
2289          * silly, and for Qwell's amusement.
2290          */
2291         *((int *) 0) = 0;
2292 #endif  /* defined(DO_CRASH) */
2293 }
2294
2295 #if defined(AST_DEVMODE)
2296 void __ast_assert_failed(int condition, const char *condition_str, const char *file, int line, const char *function)
2297 {
2298         /*
2299          * Attempt to put it into the logger, but hope that at least
2300          * someone saw the message on stderr ...
2301          */
2302         ast_log(__LOG_ERROR, file, line, function, "FRACK!, Failed assertion %s (%d)\n",
2303                 condition_str, condition);
2304         fprintf(stderr, "FRACK!, Failed assertion %s (%d) at line %d in %s of %s\n",
2305                 condition_str, condition, line, function, file);
2306         /*
2307          * Give the logger a chance to get the message out, just in case
2308          * we abort(), or Asterisk crashes due to whatever problem just
2309          * happened after we exit ast_assert().
2310          */
2311         usleep(1);
2312         ast_do_crash();
2313 }
2314 #endif  /* defined(AST_DEVMODE) */