Mon Oct 8 12:39:06 2012

Asterisk developer's documentation


utils.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief Utility functions
00020  *
00021  * \note These are important for portability and security,
00022  * so please use them in favour of other routines.
00023  * Please consult the CODING GUIDELINES for more information.
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
00033 
00034 #include <ctype.h>
00035 #include <sys/stat.h>
00036 #include <sys/stat.h>
00037 
00038 #ifdef HAVE_DEV_URANDOM
00039 #include <fcntl.h>
00040 #endif
00041 
00042 #include "asterisk/network.h"
00043 
00044 #define AST_API_MODULE     /* ensure that inlinable API functions will be built in lock.h if required */
00045 #include "asterisk/lock.h"
00046 #include "asterisk/io.h"
00047 #include "asterisk/md5.h"
00048 #include "asterisk/sha1.h"
00049 #include "asterisk/cli.h"
00050 #include "asterisk/linkedlists.h"
00051 
00052 #define AST_API_MODULE     /* ensure that inlinable API functions will be built in this module if required */
00053 #include "asterisk/strings.h"
00054 
00055 #define AST_API_MODULE     /* ensure that inlinable API functions will be built in this module if required */
00056 #include "asterisk/time.h"
00057 
00058 #define AST_API_MODULE     /* ensure that inlinable API functions will be built in this module if required */
00059 #include "asterisk/stringfields.h"
00060 
00061 #define AST_API_MODULE     /* ensure that inlinable API functions will be built in this module if required */
00062 #include "asterisk/utils.h"
00063 
00064 #define AST_API_MODULE
00065 #include "asterisk/threadstorage.h"
00066 
00067 #define AST_API_MODULE
00068 #include "asterisk/config.h"
00069 
00070 static char base64[64];
00071 static char b2a[256];
00072 
00073 AST_THREADSTORAGE(inet_ntoa_buf);
00074 
00075 #if !defined(HAVE_GETHOSTBYNAME_R_5) && !defined(HAVE_GETHOSTBYNAME_R_6)
00076 
00077 #define ERANGE 34 /*!< duh? ERANGE value copied from web... */
00078 #undef gethostbyname
00079 
00080 AST_MUTEX_DEFINE_STATIC(__mutex);
00081 
00082 /*! \brief Reentrant replacement for gethostbyname for BSD-based systems.
00083 \note This
00084 routine is derived from code originally written and placed in the public 
00085 domain by Enzo Michelangeli <em@em.no-ip.com> */
00086 
00087 static int gethostbyname_r (const char *name, struct hostent *ret, char *buf,
00088             size_t buflen, struct hostent **result, 
00089             int *h_errnop) 
00090 {
00091    int hsave;
00092    struct hostent *ph;
00093    ast_mutex_lock(&__mutex); /* begin critical area */
00094    hsave = h_errno;
00095 
00096    ph = gethostbyname(name);
00097    *h_errnop = h_errno; /* copy h_errno to *h_herrnop */
00098    if (ph == NULL) {
00099       *result = NULL;
00100    } else {
00101       char **p, **q;
00102       char *pbuf;
00103       int nbytes = 0;
00104       int naddr = 0, naliases = 0;
00105       /* determine if we have enough space in buf */
00106 
00107       /* count how many addresses */
00108       for (p = ph->h_addr_list; *p != 0; p++) {
00109          nbytes += ph->h_length; /* addresses */
00110          nbytes += sizeof(*p); /* pointers */
00111          naddr++;
00112       }
00113       nbytes += sizeof(*p); /* one more for the terminating NULL */
00114 
00115       /* count how many aliases, and total length of strings */
00116       for (p = ph->h_aliases; *p != 0; p++) {
00117          nbytes += (strlen(*p)+1); /* aliases */
00118          nbytes += sizeof(*p);  /* pointers */
00119          naliases++;
00120       }
00121       nbytes += sizeof(*p); /* one more for the terminating NULL */
00122 
00123       /* here nbytes is the number of bytes required in buffer */
00124       /* as a terminator must be there, the minimum value is ph->h_length */
00125       if (nbytes > buflen) {
00126          *result = NULL;
00127          ast_mutex_unlock(&__mutex); /* end critical area */
00128          return ERANGE; /* not enough space in buf!! */
00129       }
00130 
00131       /* There is enough space. Now we need to do a deep copy! */
00132       /* Allocation in buffer:
00133          from [0] to [(naddr-1) * sizeof(*p)]:
00134          pointers to addresses
00135          at [naddr * sizeof(*p)]:
00136          NULL
00137          from [(naddr+1) * sizeof(*p)] to [(naddr+naliases) * sizeof(*p)] :
00138          pointers to aliases
00139          at [(naddr+naliases+1) * sizeof(*p)]:
00140          NULL
00141          then naddr addresses (fixed length), and naliases aliases (asciiz).
00142       */
00143 
00144       *ret = *ph;   /* copy whole structure (not its address!) */
00145 
00146       /* copy addresses */
00147       q = (char **)buf; /* pointer to pointers area (type: char **) */
00148       ret->h_addr_list = q; /* update pointer to address list */
00149       pbuf = buf + ((naddr + naliases + 2) * sizeof(*p)); /* skip that area */
00150       for (p = ph->h_addr_list; *p != 0; p++) {
00151          memcpy(pbuf, *p, ph->h_length); /* copy address bytes */
00152          *q++ = pbuf; /* the pointer is the one inside buf... */
00153          pbuf += ph->h_length; /* advance pbuf */
00154       }
00155       *q++ = NULL; /* address list terminator */
00156 
00157       /* copy aliases */
00158       ret->h_aliases = q; /* update pointer to aliases list */
00159       for (p = ph->h_aliases; *p != 0; p++) {
00160          strcpy(pbuf, *p); /* copy alias strings */
00161          *q++ = pbuf; /* the pointer is the one inside buf... */
00162          pbuf += strlen(*p); /* advance pbuf */
00163          *pbuf++ = 0; /* string terminator */
00164       }
00165       *q++ = NULL; /* terminator */
00166 
00167       strcpy(pbuf, ph->h_name); /* copy alias strings */
00168       ret->h_name = pbuf;
00169       pbuf += strlen(ph->h_name); /* advance pbuf */
00170       *pbuf++ = 0; /* string terminator */
00171 
00172       *result = ret;  /* and let *result point to structure */
00173 
00174    }
00175    h_errno = hsave;  /* restore h_errno */
00176    ast_mutex_unlock(&__mutex); /* end critical area */
00177 
00178    return (*result == NULL); /* return 0 on success, non-zero on error */
00179 }
00180 
00181 
00182 #endif
00183 
00184 /*! \brief Re-entrant (thread safe) version of gethostbyname that replaces the 
00185    standard gethostbyname (which is not thread safe)
00186 */
00187 struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp)
00188 {
00189    int res;
00190    int herrno;
00191    int dots = 0;
00192    const char *s;
00193    struct hostent *result = NULL;
00194    /* Although it is perfectly legitimate to lookup a pure integer, for
00195       the sake of the sanity of people who like to name their peers as
00196       integers, we break with tradition and refuse to look up a
00197       pure integer */
00198    s = host;
00199    res = 0;
00200    while (s && *s) {
00201       if (*s == '.')
00202          dots++;
00203       else if (!isdigit(*s))
00204          break;
00205       s++;
00206    }
00207    if (!s || !*s) {
00208       /* Forge a reply for IP's to avoid octal IP's being interpreted as octal */
00209       if (dots != 3)
00210          return NULL;
00211       memset(hp, 0, sizeof(struct ast_hostent));
00212       hp->hp.h_addrtype = AF_INET;
00213       hp->hp.h_addr_list = (void *) hp->buf;
00214       hp->hp.h_addr = hp->buf + sizeof(void *);
00215       /* For AF_INET, this will always be 4 */
00216       hp->hp.h_length = 4;
00217       if (inet_pton(AF_INET, host, hp->hp.h_addr) > 0)
00218          return &hp->hp;
00219       return NULL;
00220       
00221    }
00222 #ifdef HAVE_GETHOSTBYNAME_R_5
00223    result = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &herrno);
00224 
00225    if (!result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
00226       return NULL;
00227 #else
00228    res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
00229 
00230    if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
00231       return NULL;
00232 #endif
00233    return &hp->hp;
00234 }
00235 
00236 /*! \brief Produce 32 char MD5 hash of value. */
00237 void ast_md5_hash(char *output, const char *input)
00238 {
00239    struct MD5Context md5;
00240    unsigned char digest[16];
00241    char *ptr;
00242    int x;
00243 
00244    MD5Init(&md5);
00245    MD5Update(&md5, (const unsigned char *) input, strlen(input));
00246    MD5Final(digest, &md5);
00247    ptr = output;
00248    for (x = 0; x < 16; x++)
00249       ptr += sprintf(ptr, "%2.2x", digest[x]);
00250 }
00251 
00252 /*! \brief Produce 40 char SHA1 hash of value. */
00253 void ast_sha1_hash(char *output, const char *input)
00254 {
00255    struct SHA1Context sha;
00256    char *ptr;
00257    int x;
00258    uint8_t Message_Digest[20];
00259 
00260    SHA1Reset(&sha);
00261    
00262    SHA1Input(&sha, (const unsigned char *) input, strlen(input));
00263 
00264    SHA1Result(&sha, Message_Digest);
00265    ptr = output;
00266    for (x = 0; x < 20; x++)
00267       ptr += sprintf(ptr, "%2.2x", Message_Digest[x]);
00268 }
00269 
00270 /*! \brief decode BASE64 encoded text */
00271 int ast_base64decode(unsigned char *dst, const char *src, int max)
00272 {
00273    int cnt = 0;
00274    unsigned int byte = 0;
00275    unsigned int bits = 0;
00276    int incnt = 0;
00277    while(*src && *src != '=' && (cnt < max)) {
00278       /* Shift in 6 bits of input */
00279       byte <<= 6;
00280       byte |= (b2a[(int)(*src)]) & 0x3f;
00281       bits += 6;
00282       src++;
00283       incnt++;
00284       /* If we have at least 8 bits left over, take that character 
00285          off the top */
00286       if (bits >= 8)  {
00287          bits -= 8;
00288          *dst = (byte >> bits) & 0xff;
00289          dst++;
00290          cnt++;
00291       }
00292    }
00293    /* Don't worry about left over bits, they're extra anyway */
00294    return cnt;
00295 }
00296 
00297 /*! \brief encode text to BASE64 coding */
00298 int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks)
00299 {
00300    int cnt = 0;
00301    int col = 0;
00302    unsigned int byte = 0;
00303    int bits = 0;
00304    int cntin = 0;
00305    /* Reserve space for null byte at end of string */
00306    max--;
00307    while ((cntin < srclen) && (cnt < max)) {
00308       byte <<= 8;
00309       byte |= *(src++);
00310       bits += 8;
00311       cntin++;
00312       if ((bits == 24) && (cnt + 4 <= max)) {
00313          *dst++ = base64[(byte >> 18) & 0x3f];
00314          *dst++ = base64[(byte >> 12) & 0x3f];
00315          *dst++ = base64[(byte >> 6) & 0x3f];
00316          *dst++ = base64[byte & 0x3f];
00317          cnt += 4;
00318          col += 4;
00319          bits = 0;
00320          byte = 0;
00321       }
00322       if (linebreaks && (cnt < max) && (col == 64)) {
00323          *dst++ = '\n';
00324          cnt++;
00325          col = 0;
00326       }
00327    }
00328    if (bits && (cnt + 4 <= max)) {
00329       /* Add one last character for the remaining bits, 
00330          padding the rest with 0 */
00331       byte <<= 24 - bits;
00332       *dst++ = base64[(byte >> 18) & 0x3f];
00333       *dst++ = base64[(byte >> 12) & 0x3f];
00334       if (bits == 16)
00335          *dst++ = base64[(byte >> 6) & 0x3f];
00336       else
00337          *dst++ = '=';
00338       *dst++ = '=';
00339       cnt += 4;
00340    }
00341    if (linebreaks && (cnt < max)) {
00342       *dst++ = '\n';
00343       cnt++;
00344    }
00345    *dst = '\0';
00346    return cnt;
00347 }
00348 
00349 int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
00350 {
00351    return ast_base64encode_full(dst, src, srclen, max, 0);
00352 }
00353 
00354 static void base64_init(void)
00355 {
00356    int x;
00357    memset(b2a, -1, sizeof(b2a));
00358    /* Initialize base-64 Conversion table */
00359    for (x = 0; x < 26; x++) {
00360       /* A-Z */
00361       base64[x] = 'A' + x;
00362       b2a['A' + x] = x;
00363       /* a-z */
00364       base64[x + 26] = 'a' + x;
00365       b2a['a' + x] = x + 26;
00366       /* 0-9 */
00367       if (x < 10) {
00368          base64[x + 52] = '0' + x;
00369          b2a['0' + x] = x + 52;
00370       }
00371    }
00372    base64[62] = '+';
00373    base64[63] = '/';
00374    b2a[(int)'+'] = 62;
00375    b2a[(int)'/'] = 63;
00376 }
00377 
00378 /*! \brief Turn text string to URI-encoded %XX version 
00379  *
00380  * \note 
00381  *  At this point, this function is encoding agnostic; it does not
00382  *  check whether it is fed legal UTF-8. We escape control
00383  *  characters (\x00-\x1F\x7F), '%', and all characters above 0x7F.
00384  *  If do_special_char == 1 we will convert all characters except alnum
00385  *  and mark.
00386  *  Outbuf needs to have more memory allocated than the instring
00387  *  to have room for the expansion. Every char that is converted
00388  *  is replaced by three ASCII characters.
00389  */
00390 char *ast_uri_encode(const char *string, char *outbuf, int buflen, int do_special_char)
00391 {
00392    const char *ptr  = string; /* Start with the string */
00393    char *out = outbuf;
00394    const char *mark = "-_.!~*'()"; /* no encode set, RFC 2396 section 2.3, RFC 3261 sec 25 */
00395 
00396    while (*ptr && out - outbuf < buflen - 1) {
00397       if ((const signed char) *ptr < 32 || *ptr == 0x7f || *ptr == '%' ||
00398             (do_special_char &&
00399             !(*ptr >= '0' && *ptr <= '9') &&      /* num */
00400             !(*ptr >= 'A' && *ptr <= 'Z') &&      /* ALPHA */
00401             !(*ptr >= 'a' && *ptr <= 'z') &&      /* alpha */
00402             !strchr(mark, *ptr))) {               /* mark set */
00403          if (out - outbuf >= buflen - 3) {
00404             break;
00405          }
00406 
00407          out += sprintf(out, "%%%02X", (unsigned char) *ptr);
00408       } else {
00409          *out = *ptr;   /* Continue copying the string */
00410          out++;
00411       }
00412       ptr++;
00413    }
00414 
00415    if (buflen) {
00416       *out = '\0';
00417    }
00418 
00419    return outbuf;
00420 }
00421 
00422 /*! \brief escapes characters specified for quoted portions of sip messages */
00423 char *ast_escape_quoted(const char *string, char *outbuf, int buflen)
00424 {
00425    const char *ptr  = string;
00426    char *out = outbuf;
00427    char *allow = "\t\v !"; /* allow LWS (minus \r and \n) and "!" */
00428 
00429    while (*ptr && out - outbuf < buflen - 1) {
00430       if (!(strchr(allow, *ptr))
00431          && !(*ptr >= '#' && *ptr <= '[') /* %x23 - %x5b */
00432          && !(*ptr >= ']' && *ptr <= '~') /* %x5d - %x7e */
00433          && !((unsigned char) *ptr > 0x7f)) {             /* UTF8-nonascii */
00434 
00435          if (out - outbuf >= buflen - 2) {
00436             break;
00437          }
00438          out += sprintf(out, "\\%c", (unsigned char) *ptr);
00439       } else {
00440          *out = *ptr;
00441          out++;
00442       }
00443       ptr++;
00444    }
00445 
00446    if (buflen) {
00447       *out = '\0';
00448    }
00449 
00450    return outbuf;
00451 }
00452 
00453 /*! \brief  ast_uri_decode: Decode SIP URI, URN, URL (overwrite the string)  */
00454 void ast_uri_decode(char *s) 
00455 {
00456    char *o;
00457    unsigned int tmp;
00458 
00459    for (o = s; *s; s++, o++) {
00460       if (*s == '%' && s[1] != '\0' && s[2] != '\0' && sscanf(s + 1, "%2x", &tmp) == 1) {
00461          /* have '%', two chars and correct parsing */
00462          *o = tmp;
00463          s += 2;  /* Will be incremented once more when we break out */
00464       } else /* all other cases, just copy */
00465          *o = *s;
00466    }
00467    *o = '\0';
00468 }
00469 
00470 /*! \brief  ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa */
00471 const char *ast_inet_ntoa(struct in_addr ia)
00472 {
00473    char *buf;
00474 
00475    if (!(buf = ast_threadstorage_get(&inet_ntoa_buf, INET_ADDRSTRLEN)))
00476       return "";
00477 
00478    return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
00479 }
00480 
00481 #ifdef HAVE_DEV_URANDOM
00482 static int dev_urandom_fd;
00483 #endif
00484 
00485 #ifndef __linux__
00486 #undef pthread_create /* For ast_pthread_create function only */
00487 #endif /* !__linux__ */
00488 
00489 #if !defined(LOW_MEMORY)
00490 
00491 #ifdef DEBUG_THREADS
00492 
00493 /*! \brief A reasonable maximum number of locks a thread would be holding ... */
00494 #define AST_MAX_LOCKS 64
00495 
00496 /* Allow direct use of pthread_mutex_t and friends */
00497 #undef pthread_mutex_t
00498 #undef pthread_mutex_lock
00499 #undef pthread_mutex_unlock
00500 #undef pthread_mutex_init
00501 #undef pthread_mutex_destroy
00502 
00503 /*! 
00504  * \brief Keep track of which locks a thread holds 
00505  *
00506  * There is an instance of this struct for every active thread
00507  */
00508 struct thr_lock_info {
00509    /*! The thread's ID */
00510    pthread_t thread_id;
00511    /*! The thread name which includes where the thread was started */
00512    const char *thread_name;
00513    /*! This is the actual container of info for what locks this thread holds */
00514    struct {
00515       const char *file;
00516       int line_num;
00517       const char *func;
00518       const char *lock_name;
00519       void *lock_addr;
00520       int times_locked;
00521       enum ast_lock_type type;
00522       /*! This thread is waiting on this lock */
00523       int pending:2;
00524 #ifdef HAVE_BKTR
00525       struct ast_bt *backtrace;
00526 #endif
00527    } locks[AST_MAX_LOCKS];
00528    /*! This is the number of locks currently held by this thread.
00529     *  The index (num_locks - 1) has the info on the last one in the
00530     *  locks member */
00531    unsigned int num_locks;
00532    /*! Protects the contents of the locks member 
00533     * Intentionally not ast_mutex_t */
00534    pthread_mutex_t lock;
00535    AST_LIST_ENTRY(thr_lock_info) entry;
00536 };
00537 
00538 /*! 
00539  * \brief Locked when accessing the lock_infos list 
00540  */
00541 AST_MUTEX_DEFINE_STATIC(lock_infos_lock);
00542 /*!
00543  * \brief A list of each thread's lock info 
00544  */
00545 static AST_LIST_HEAD_NOLOCK_STATIC(lock_infos, thr_lock_info);
00546 
00547 /*!
00548  * \brief Destroy a thread's lock info
00549  *
00550  * This gets called automatically when the thread stops
00551  */
00552 static void lock_info_destroy(void *data)
00553 {
00554    struct thr_lock_info *lock_info = data;
00555    int i;
00556 
00557    pthread_mutex_lock(&lock_infos_lock.mutex);
00558    AST_LIST_REMOVE(&lock_infos, lock_info, entry);
00559    pthread_mutex_unlock(&lock_infos_lock.mutex);
00560 
00561 
00562    for (i = 0; i < lock_info->num_locks; i++) {
00563       if (lock_info->locks[i].pending == -1) {
00564          /* This just means that the last lock this thread went for was by
00565           * using trylock, and it failed.  This is fine. */
00566          break;
00567       }
00568 
00569       ast_log(LOG_ERROR, 
00570          "Thread '%s' still has a lock! - '%s' (%p) from '%s' in %s:%d!\n", 
00571          lock_info->thread_name,
00572          lock_info->locks[i].lock_name,
00573          lock_info->locks[i].lock_addr,
00574          lock_info->locks[i].func,
00575          lock_info->locks[i].file,
00576          lock_info->locks[i].line_num
00577       );
00578    }
00579 
00580    pthread_mutex_destroy(&lock_info->lock);
00581    if (lock_info->thread_name)
00582       free((void *) lock_info->thread_name);
00583    free(lock_info);
00584 }
00585 
00586 /*!
00587  * \brief The thread storage key for per-thread lock info
00588  */
00589 AST_THREADSTORAGE_CUSTOM(thread_lock_info, NULL, lock_info_destroy);
00590 #ifdef HAVE_BKTR
00591 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
00592    int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt)
00593 #else
00594 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
00595    int line_num, const char *func, const char *lock_name, void *lock_addr)
00596 #endif
00597 {
00598    struct thr_lock_info *lock_info;
00599    int i;
00600 
00601    if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
00602       return;
00603 
00604    pthread_mutex_lock(&lock_info->lock);
00605 
00606    for (i = 0; i < lock_info->num_locks; i++) {
00607       if (lock_info->locks[i].lock_addr == lock_addr) {
00608          lock_info->locks[i].times_locked++;
00609 #ifdef HAVE_BKTR
00610          lock_info->locks[i].backtrace = bt;
00611 #endif
00612          pthread_mutex_unlock(&lock_info->lock);
00613          return;
00614       }
00615    }
00616 
00617    if (lock_info->num_locks == AST_MAX_LOCKS) {
00618       /* Can't use ast_log here, because it will cause infinite recursion */
00619       fprintf(stderr, "XXX ERROR XXX A thread holds more locks than '%d'."
00620          "  Increase AST_MAX_LOCKS!\n", AST_MAX_LOCKS);
00621       pthread_mutex_unlock(&lock_info->lock);
00622       return;
00623    }
00624 
00625    if (i && lock_info->locks[i - 1].pending == -1) {
00626       /* The last lock on the list was one that this thread tried to lock but
00627        * failed at doing so.  It has now moved on to something else, so remove
00628        * the old lock from the list. */
00629       i--;
00630       lock_info->num_locks--;
00631       memset(&lock_info->locks[i], 0, sizeof(lock_info->locks[0]));
00632    }
00633 
00634    lock_info->locks[i].file = filename;
00635    lock_info->locks[i].line_num = line_num;
00636    lock_info->locks[i].func = func;
00637    lock_info->locks[i].lock_name = lock_name;
00638    lock_info->locks[i].lock_addr = lock_addr;
00639    lock_info->locks[i].times_locked = 1;
00640    lock_info->locks[i].type = type;
00641    lock_info->locks[i].pending = 1;
00642 #ifdef HAVE_BKTR
00643    lock_info->locks[i].backtrace = bt;
00644 #endif
00645    lock_info->num_locks++;
00646 
00647    pthread_mutex_unlock(&lock_info->lock);
00648 }
00649 
00650 void ast_mark_lock_acquired(void *lock_addr)
00651 {
00652    struct thr_lock_info *lock_info;
00653 
00654    if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
00655       return;
00656 
00657    pthread_mutex_lock(&lock_info->lock);
00658    if (lock_info->locks[lock_info->num_locks - 1].lock_addr == lock_addr) {
00659       lock_info->locks[lock_info->num_locks - 1].pending = 0;
00660    }
00661    pthread_mutex_unlock(&lock_info->lock);
00662 }
00663 
00664 void ast_mark_lock_failed(void *lock_addr)
00665 {
00666    struct thr_lock_info *lock_info;
00667 
00668    if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
00669       return;
00670 
00671    pthread_mutex_lock(&lock_info->lock);
00672    if (lock_info->locks[lock_info->num_locks - 1].lock_addr == lock_addr) {
00673       lock_info->locks[lock_info->num_locks - 1].pending = -1;
00674       lock_info->locks[lock_info->num_locks - 1].times_locked--;
00675    }
00676    pthread_mutex_unlock(&lock_info->lock);
00677 }
00678 
00679 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)
00680 {
00681    struct thr_lock_info *lock_info;
00682    int i = 0;
00683 
00684    if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
00685       return -1;
00686 
00687    pthread_mutex_lock(&lock_info->lock);
00688 
00689    for (i = lock_info->num_locks - 1; i >= 0; i--) {
00690       if (lock_info->locks[i].lock_addr == lock_addr)
00691          break;
00692    }
00693 
00694    if (i == -1) {
00695       /* Lock not found :( */
00696       pthread_mutex_unlock(&lock_info->lock);
00697       return -1;
00698    }
00699 
00700    ast_copy_string(filename, lock_info->locks[i].file, filename_size);
00701    *lineno = lock_info->locks[i].line_num;
00702    ast_copy_string(func, lock_info->locks[i].func, func_size);
00703    ast_copy_string(mutex_name, lock_info->locks[i].lock_name, mutex_name_size);
00704 
00705    pthread_mutex_unlock(&lock_info->lock);
00706 
00707    return 0;
00708 }
00709 
00710 #ifdef HAVE_BKTR
00711 void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt)
00712 #else
00713 void ast_remove_lock_info(void *lock_addr)
00714 #endif
00715 {
00716    struct thr_lock_info *lock_info;
00717    int i = 0;
00718 
00719    if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
00720       return;
00721 
00722    pthread_mutex_lock(&lock_info->lock);
00723 
00724    for (i = lock_info->num_locks - 1; i >= 0; i--) {
00725       if (lock_info->locks[i].lock_addr == lock_addr)
00726          break;
00727    }
00728 
00729    if (i == -1) {
00730       /* Lock not found :( */
00731       pthread_mutex_unlock(&lock_info->lock);
00732       return;
00733    }
00734 
00735    if (lock_info->locks[i].times_locked > 1) {
00736       lock_info->locks[i].times_locked--;
00737 #ifdef HAVE_BKTR
00738       lock_info->locks[i].backtrace = bt;
00739 #endif
00740       pthread_mutex_unlock(&lock_info->lock);
00741       return;
00742    }
00743 
00744    if (i < lock_info->num_locks - 1) {
00745       /* Not the last one ... *should* be rare! */
00746       memmove(&lock_info->locks[i], &lock_info->locks[i + 1], 
00747          (lock_info->num_locks - (i + 1)) * sizeof(lock_info->locks[0]));
00748    }
00749 
00750    lock_info->num_locks--;
00751 
00752    pthread_mutex_unlock(&lock_info->lock);
00753 }
00754 
00755 static const char *locktype2str(enum ast_lock_type type)
00756 {
00757    switch (type) {
00758    case AST_MUTEX:
00759       return "MUTEX";
00760    case AST_RDLOCK:
00761       return "RDLOCK";
00762    case AST_WRLOCK:
00763       return "WRLOCK";
00764    }
00765 
00766    return "UNKNOWN";
00767 }
00768 
00769 #ifdef HAVE_BKTR
00770 static void append_backtrace_information(struct ast_str **str, struct ast_bt *bt)
00771 {
00772    char **symbols;
00773 
00774    if (!bt) {
00775       ast_str_append(str, 0, "\tNo backtrace to print\n");
00776       return;
00777    }
00778 
00779    if ((symbols = ast_bt_get_symbols(bt->addresses, bt->num_frames))) {
00780       int frame_iterator;
00781       
00782       for (frame_iterator = 0; frame_iterator < bt->num_frames; ++frame_iterator) {
00783          ast_str_append(str, 0, "\t%s\n", symbols[frame_iterator]);
00784       }
00785 
00786       free(symbols);
00787    } else {
00788       ast_str_append(str, 0, "\tCouldn't retrieve backtrace symbols\n");
00789    }
00790 }
00791 #endif
00792 
00793 static void append_lock_information(struct ast_str **str, struct thr_lock_info *lock_info, int i)
00794 {
00795    int j;
00796    ast_mutex_t *lock;
00797    struct ast_lock_track *lt;
00798    
00799    ast_str_append(str, 0, "=== ---> %sLock #%d (%s): %s %d %s %s %p (%d)\n", 
00800                lock_info->locks[i].pending > 0 ? "Waiting for " : 
00801                lock_info->locks[i].pending < 0 ? "Tried and failed to get " : "", i,
00802                lock_info->locks[i].file, 
00803                locktype2str(lock_info->locks[i].type),
00804                lock_info->locks[i].line_num,
00805                lock_info->locks[i].func, lock_info->locks[i].lock_name,
00806                lock_info->locks[i].lock_addr, 
00807                lock_info->locks[i].times_locked);
00808 #ifdef HAVE_BKTR
00809    append_backtrace_information(str, lock_info->locks[i].backtrace);
00810 #endif
00811    
00812    if (!lock_info->locks[i].pending || lock_info->locks[i].pending == -1)
00813       return;
00814    
00815    /* We only have further details for mutexes right now */
00816    if (lock_info->locks[i].type != AST_MUTEX)
00817       return;
00818    
00819    lock = lock_info->locks[i].lock_addr;
00820    lt = lock->track;
00821    ast_reentrancy_lock(lt);
00822    for (j = 0; *str && j < lt->reentrancy; j++) {
00823       ast_str_append(str, 0, "=== --- ---> Locked Here: %s line %d (%s)\n",
00824                   lt->file[j], lt->lineno[j], lt->func[j]);
00825    }
00826    ast_reentrancy_unlock(lt); 
00827 }
00828 
00829 
00830 /*! This function can help you find highly temporal locks; locks that happen for a 
00831     short time, but at unexpected times, usually at times that create a deadlock,
00832    Why is this thing locked right then? Who is locking it? Who am I fighting
00833     with for this lock? 
00834 
00835    To answer such questions, just call this routine before you would normally try
00836    to aquire a lock. It doesn't do anything if the lock is not acquired. If the
00837    lock is taken, it will publish a line or two to the console via ast_log().
00838 
00839    Sometimes, the lock message is pretty uninformative. For instance, you might
00840    find that the lock is being aquired deep within the astobj2 code; this tells
00841    you little about higher level routines that call the astobj2 routines.
00842    But, using gdb, you can set a break at the ast_log below, and for that
00843    breakpoint, you can set the commands:
00844      where
00845      cont
00846    which will give a stack trace and continue. -- that aught to do the job!
00847 
00848 */
00849 void log_show_lock(void *this_lock_addr)
00850 {
00851    struct thr_lock_info *lock_info;
00852    struct ast_str *str;
00853 
00854    if (!(str = ast_str_create(4096))) {
00855       ast_log(LOG_NOTICE,"Could not create str\n");
00856       return;
00857    }
00858    
00859 
00860    pthread_mutex_lock(&lock_infos_lock.mutex);
00861    AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) {
00862       int i;
00863       pthread_mutex_lock(&lock_info->lock);
00864       for (i = 0; str && i < lock_info->num_locks; i++) {
00865          /* ONLY show info about this particular lock, if
00866             it's acquired... */
00867          if (lock_info->locks[i].lock_addr == this_lock_addr) {
00868             append_lock_information(&str, lock_info, i);
00869             ast_log(LOG_NOTICE, "%s", ast_str_buffer(str));
00870             break;
00871          }
00872       }
00873       pthread_mutex_unlock(&lock_info->lock);
00874    }
00875    pthread_mutex_unlock(&lock_infos_lock.mutex);
00876    ast_free(str);
00877 }
00878 
00879 
00880 static char *handle_show_locks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00881 {
00882    struct thr_lock_info *lock_info;
00883    struct ast_str *str;
00884 
00885    if (!(str = ast_str_create(4096)))
00886       return CLI_FAILURE;
00887 
00888    switch (cmd) {
00889    case CLI_INIT:
00890       e->command = "core show locks";
00891       e->usage =
00892          "Usage: core show locks\n"
00893          "       This command is for lock debugging.  It prints out which locks\n"
00894          "are owned by each active thread.\n";
00895       return NULL;
00896 
00897    case CLI_GENERATE:
00898       return NULL;
00899    }
00900 
00901    ast_str_append(&str, 0, "\n" 
00902                   "=======================================================================\n"
00903                   "=== Currently Held Locks ==============================================\n"
00904                   "=======================================================================\n"
00905                   "===\n"
00906                   "=== <pending> <lock#> (<file>): <lock type> <line num> <function> <lock name> <lock addr> (times locked)\n"
00907                   "===\n");
00908 
00909    if (!str)
00910       return CLI_FAILURE;
00911 
00912    pthread_mutex_lock(&lock_infos_lock.mutex);
00913    AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) {
00914       int i;
00915       if (lock_info->num_locks) {
00916          ast_str_append(&str, 0, "=== Thread ID: 0x%lx (%s)\n", (long) lock_info->thread_id,
00917             lock_info->thread_name);
00918          pthread_mutex_lock(&lock_info->lock);
00919          for (i = 0; str && i < lock_info->num_locks; i++) {
00920             append_lock_information(&str, lock_info, i);
00921          }
00922          pthread_mutex_unlock(&lock_info->lock);
00923          if (!str)
00924             break;
00925          ast_str_append(&str, 0, "=== -------------------------------------------------------------------\n"
00926                         "===\n");
00927          if (!str)
00928             break;
00929       }
00930    }
00931    pthread_mutex_unlock(&lock_infos_lock.mutex);
00932 
00933    if (!str)
00934       return CLI_FAILURE;
00935 
00936    ast_str_append(&str, 0, "=======================================================================\n"
00937                   "\n");
00938 
00939    if (!str)
00940       return CLI_FAILURE;
00941 
00942    ast_cli(a->fd, "%s", ast_str_buffer(str));
00943 
00944    ast_free(str);
00945 
00946    return CLI_SUCCESS;
00947 }
00948 
00949 static struct ast_cli_entry utils_cli[] = {
00950    AST_CLI_DEFINE(handle_show_locks, "Show which locks are held by which thread"),
00951 };
00952 
00953 #endif /* DEBUG_THREADS */
00954 
00955 /*
00956  * support for 'show threads'. The start routine is wrapped by
00957  * dummy_start(), so that ast_register_thread() and
00958  * ast_unregister_thread() know the thread identifier.
00959  */
00960 struct thr_arg {
00961    void *(*start_routine)(void *);
00962    void *data;
00963    char *name;
00964 };
00965 
00966 /*
00967  * on OS/X, pthread_cleanup_push() and pthread_cleanup_pop()
00968  * are odd macros which start and end a block, so they _must_ be
00969  * used in pairs (the latter with a '1' argument to call the
00970  * handler on exit.
00971  * On BSD we don't need this, but we keep it for compatibility.
00972  */
00973 static void *dummy_start(void *data)
00974 {
00975    void *ret;
00976    struct thr_arg a = *((struct thr_arg *) data);  /* make a local copy */
00977 #ifdef DEBUG_THREADS
00978    struct thr_lock_info *lock_info;
00979    pthread_mutexattr_t mutex_attr;
00980 #endif
00981 
00982    /* note that even though data->name is a pointer to allocated memory,
00983       we are not freeing it here because ast_register_thread is going to
00984       keep a copy of the pointer and then ast_unregister_thread will
00985       free the memory
00986    */
00987    ast_free(data);
00988    ast_register_thread(a.name);
00989    pthread_cleanup_push(ast_unregister_thread, (void *) pthread_self());
00990 
00991 #ifdef DEBUG_THREADS
00992    if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
00993       return NULL;
00994 
00995    lock_info->thread_id = pthread_self();
00996    lock_info->thread_name = strdup(a.name);
00997 
00998    pthread_mutexattr_init(&mutex_attr);
00999    pthread_mutexattr_settype(&mutex_attr, AST_MUTEX_KIND);
01000    pthread_mutex_init(&lock_info->lock, &mutex_attr);
01001    pthread_mutexattr_destroy(&mutex_attr);
01002 
01003    pthread_mutex_lock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */
01004    AST_LIST_INSERT_TAIL(&lock_infos, lock_info, entry);
01005    pthread_mutex_unlock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */
01006 #endif /* DEBUG_THREADS */
01007 
01008    ret = a.start_routine(a.data);
01009 
01010    pthread_cleanup_pop(1);
01011 
01012    return ret;
01013 }
01014 
01015 #endif /* !LOW_MEMORY */
01016 
01017 int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
01018               void *data, size_t stacksize, const char *file, const char *caller,
01019               int line, const char *start_fn)
01020 {
01021 #if !defined(LOW_MEMORY)
01022    struct thr_arg *a;
01023 #endif
01024 
01025    if (!attr) {
01026       attr = alloca(sizeof(*attr));
01027       pthread_attr_init(attr);
01028    }
01029 
01030 #ifdef __linux__
01031    /* On Linux, pthread_attr_init() defaults to PTHREAD_EXPLICIT_SCHED,
01032       which is kind of useless. Change this here to
01033       PTHREAD_INHERIT_SCHED; that way the -p option to set realtime
01034       priority will propagate down to new threads by default.
01035       This does mean that callers cannot set a different priority using
01036       PTHREAD_EXPLICIT_SCHED in the attr argument; instead they must set
01037       the priority afterwards with pthread_setschedparam(). */
01038    if ((errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED)))
01039       ast_log(LOG_WARNING, "pthread_attr_setinheritsched: %s\n", strerror(errno));
01040 #endif
01041 
01042    if (!stacksize)
01043       stacksize = AST_STACKSIZE;
01044 
01045    if ((errno = pthread_attr_setstacksize(attr, stacksize ? stacksize : AST_STACKSIZE)))
01046       ast_log(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(errno));
01047 
01048 #if !defined(LOW_MEMORY)
01049    if ((a = ast_malloc(sizeof(*a)))) {
01050       a->start_routine = start_routine;
01051       a->data = data;
01052       start_routine = dummy_start;
01053       if (asprintf(&a->name, "%-20s started at [%5d] %s %s()",
01054               start_fn, line, file, caller) < 0) {
01055          ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
01056          a->name = NULL;
01057       }
01058       data = a;
01059    }
01060 #endif /* !LOW_MEMORY */
01061 
01062    return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */
01063 }
01064 
01065 
01066 int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
01067               void *data, size_t stacksize, const char *file, const char *caller,
01068               int line, const char *start_fn)
01069 {
01070    unsigned char attr_destroy = 0;
01071    int res;
01072 
01073    if (!attr) {
01074       attr = alloca(sizeof(*attr));
01075       pthread_attr_init(attr);
01076       attr_destroy = 1;
01077    }
01078 
01079    if ((errno = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED)))
01080       ast_log(LOG_WARNING, "pthread_attr_setdetachstate: %s\n", strerror(errno));
01081 
01082    res = ast_pthread_create_stack(thread, attr, start_routine, data, 
01083                                   stacksize, file, caller, line, start_fn);
01084 
01085    if (attr_destroy)
01086       pthread_attr_destroy(attr);
01087 
01088    return res;
01089 }
01090 
01091 int ast_wait_for_input(int fd, int ms)
01092 {
01093    struct pollfd pfd[1];
01094    memset(pfd, 0, sizeof(pfd));
01095    pfd[0].fd = fd;
01096    pfd[0].events = POLLIN|POLLPRI;
01097    return ast_poll(pfd, 1, ms);
01098 }
01099 
01100 static int ast_wait_for_output(int fd, int timeoutms)
01101 {
01102    struct pollfd pfd = {
01103       .fd = fd,
01104       .events = POLLOUT,
01105    };
01106    int res;
01107    struct timeval start = ast_tvnow();
01108    int elapsed = 0;
01109 
01110    /* poll() until the fd is writable without blocking */
01111    while ((res = ast_poll(&pfd, 1, timeoutms - elapsed)) <= 0) {
01112       if (res == 0) {
01113          /* timed out. */
01114 #ifndef STANDALONE
01115          ast_debug(1, "Timed out trying to write\n");
01116 #endif
01117          return -1;
01118       } else if (res == -1) {
01119          /* poll() returned an error, check to see if it was fatal */
01120 
01121          if (errno == EINTR || errno == EAGAIN) {
01122             elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01123             if (elapsed >= timeoutms) {
01124                return -1;
01125             }
01126             /* This was an acceptable error, go back into poll() */
01127             continue;
01128          }
01129 
01130          /* Fatal error, bail. */
01131          ast_log(LOG_ERROR, "poll returned error: %s\n", strerror(errno));
01132 
01133          return -1;
01134       }
01135       elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01136       if (elapsed >= timeoutms) {
01137          return -1;
01138       }
01139    }
01140 
01141    return 0;
01142 }
01143 
01144 /*!
01145  * Try to write string, but wait no more than ms milliseconds before timing out.
01146  *
01147  * \note The code assumes that the file descriptor has NONBLOCK set,
01148  * so there is only one system call made to do a write, unless we actually
01149  * have a need to wait.  This way, we get better performance.
01150  * If the descriptor is blocking, all assumptions on the guaranteed
01151  * detail do not apply anymore.
01152  */
01153 int ast_carefulwrite(int fd, char *s, int len, int timeoutms) 
01154 {
01155    struct timeval start = ast_tvnow();
01156    int res = 0;
01157    int elapsed = 0;
01158 
01159    while (len) {
01160       if (ast_wait_for_output(fd, timeoutms - elapsed)) {
01161          return -1;
01162       }
01163 
01164       res = write(fd, s, len);
01165 
01166       if (res < 0 && errno != EAGAIN && errno != EINTR) {
01167          /* fatal error from write() */
01168          ast_log(LOG_ERROR, "write() returned error: %s\n", strerror(errno));
01169          return -1;
01170       }
01171 
01172       if (res < 0) {
01173          /* It was an acceptable error */
01174          res = 0;
01175       }
01176 
01177       /* Update how much data we have left to write */
01178       len -= res;
01179       s += res;
01180       res = 0;
01181 
01182       elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01183       if (elapsed >= timeoutms) {
01184          /* We've taken too long to write 
01185           * This is only an error condition if we haven't finished writing. */
01186          res = len ? -1 : 0;
01187          break;
01188       }
01189    }
01190 
01191    return res;
01192 }
01193 
01194 int ast_careful_fwrite(FILE *f, int fd, const char *src, size_t len, int timeoutms)
01195 {
01196    struct timeval start = ast_tvnow();
01197    int n = 0;
01198    int elapsed = 0;
01199 
01200    while (len) {
01201       if (ast_wait_for_output(fd, timeoutms - elapsed)) {
01202          /* poll returned a fatal error, so bail out immediately. */
01203          return -1;
01204       }
01205 
01206       /* Clear any errors from a previous write */
01207       clearerr(f);
01208 
01209       n = fwrite(src, 1, len, f);
01210 
01211       if (ferror(f) && errno != EINTR && errno != EAGAIN) {
01212          /* fatal error from fwrite() */
01213          if (!feof(f)) {
01214             /* Don't spam the logs if it was just that the connection is closed. */
01215             ast_log(LOG_ERROR, "fwrite() returned error: %s\n", strerror(errno));
01216          }
01217          n = -1;
01218          break;
01219       }
01220 
01221       /* Update for data already written to the socket */
01222       len -= n;
01223       src += n;
01224 
01225       elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01226       if (elapsed >= timeoutms) {
01227          /* We've taken too long to write 
01228           * This is only an error condition if we haven't finished writing. */
01229          n = len ? -1 : 0;
01230          break;
01231       }
01232    }
01233 
01234    while (fflush(f)) {
01235       if (errno == EAGAIN || errno == EINTR) {
01236          continue;
01237       }
01238       if (!feof(f)) {
01239          /* Don't spam the logs if it was just that the connection is closed. */
01240          ast_log(LOG_ERROR, "fflush() returned error: %s\n", strerror(errno));
01241       }
01242       n = -1;
01243       break;
01244    }
01245 
01246    return n < 0 ? -1 : 0;
01247 }
01248 
01249 char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
01250 {
01251    char *e;
01252    char *q;
01253 
01254    s = ast_strip(s);
01255    if ((q = strchr(beg_quotes, *s)) && *q != '\0') {
01256       e = s + strlen(s) - 1;
01257       if (*e == *(end_quotes + (q - beg_quotes))) {
01258          s++;
01259          *e = '\0';
01260       }
01261    }
01262 
01263    return s;
01264 }
01265 
01266 char *ast_unescape_semicolon(char *s)
01267 {
01268    char *e;
01269    char *work = s;
01270 
01271    while ((e = strchr(work, ';'))) {
01272       if ((e > work) && (*(e-1) == '\\')) {
01273          memmove(e - 1, e, strlen(e) + 1);
01274          work = e;
01275       } else {
01276          work = e + 1;
01277       }
01278    }
01279 
01280    return s;
01281 }
01282 
01283 /* !\brief unescape some C sequences in place, return pointer to the original string.
01284  */
01285 char *ast_unescape_c(char *src)
01286 {
01287    char c, *ret, *dst;
01288 
01289    if (src == NULL)
01290       return NULL;
01291    for (ret = dst = src; (c = *src++); *dst++ = c ) {
01292       if (c != '\\')
01293          continue;   /* copy char at the end of the loop */
01294       switch ((c = *src++)) {
01295       case '\0':  /* special, trailing '\' */
01296          c = '\\';
01297          break;
01298       case 'b':   /* backspace */
01299          c = '\b';
01300          break;
01301       case 'f':   /* form feed */
01302          c = '\f';
01303          break;
01304       case 'n':
01305          c = '\n';
01306          break;
01307       case 'r':
01308          c = '\r';
01309          break;
01310       case 't':
01311          c = '\t';
01312          break;
01313       }
01314       /* default, use the char literally */
01315    }
01316    *dst = '\0';
01317    return ret;
01318 }
01319 
01320 int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap)
01321 {
01322    int result;
01323 
01324    if (!buffer || !*buffer || !space || !*space)
01325       return -1;
01326 
01327    result = vsnprintf(*buffer, *space, fmt, ap);
01328 
01329    if (result < 0)
01330       return -1;
01331    else if (result > *space)
01332       result = *space;
01333 
01334    *buffer += result;
01335    *space -= result;
01336    return 0;
01337 }
01338 
01339 int ast_build_string(char **buffer, size_t *space, const char *fmt, ...)
01340 {
01341    va_list ap;
01342    int result;
01343 
01344    va_start(ap, fmt);
01345    result = ast_build_string_va(buffer, space, fmt, ap);
01346    va_end(ap);
01347 
01348    return result;
01349 }
01350 
01351 int ast_true(const char *s)
01352 {
01353    if (ast_strlen_zero(s))
01354       return 0;
01355 
01356    /* Determine if this is a true value */
01357    if (!strcasecmp(s, "yes") ||
01358        !strcasecmp(s, "true") ||
01359        !strcasecmp(s, "y") ||
01360        !strcasecmp(s, "t") ||
01361        !strcasecmp(s, "1") ||
01362        !strcasecmp(s, "on"))
01363       return -1;
01364 
01365    return 0;
01366 }
01367 
01368 int ast_false(const char *s)
01369 {
01370    if (ast_strlen_zero(s))
01371       return 0;
01372 
01373    /* Determine if this is a false value */
01374    if (!strcasecmp(s, "no") ||
01375        !strcasecmp(s, "false") ||
01376        !strcasecmp(s, "n") ||
01377        !strcasecmp(s, "f") ||
01378        !strcasecmp(s, "0") ||
01379        !strcasecmp(s, "off"))
01380       return -1;
01381 
01382    return 0;
01383 }
01384 
01385 #define ONE_MILLION  1000000
01386 /*
01387  * put timeval in a valid range. usec is 0..999999
01388  * negative values are not allowed and truncated.
01389  */
01390 static struct timeval tvfix(struct timeval a)
01391 {
01392    if (a.tv_usec >= ONE_MILLION) {
01393       ast_log(LOG_WARNING, "warning too large timestamp %ld.%ld\n",
01394          (long)a.tv_sec, (long int) a.tv_usec);
01395       a.tv_sec += a.tv_usec / ONE_MILLION;
01396       a.tv_usec %= ONE_MILLION;
01397    } else if (a.tv_usec < 0) {
01398       ast_log(LOG_WARNING, "warning negative timestamp %ld.%ld\n",
01399          (long)a.tv_sec, (long int) a.tv_usec);
01400       a.tv_usec = 0;
01401    }
01402    return a;
01403 }
01404 
01405 struct timeval ast_tvadd(struct timeval a, struct timeval b)
01406 {
01407    /* consistency checks to guarantee usec in 0..999999 */
01408    a = tvfix(a);
01409    b = tvfix(b);
01410    a.tv_sec += b.tv_sec;
01411    a.tv_usec += b.tv_usec;
01412    if (a.tv_usec >= ONE_MILLION) {
01413       a.tv_sec++;
01414       a.tv_usec -= ONE_MILLION;
01415    }
01416    return a;
01417 }
01418 
01419 struct timeval ast_tvsub(struct timeval a, struct timeval b)
01420 {
01421    /* consistency checks to guarantee usec in 0..999999 */
01422    a = tvfix(a);
01423    b = tvfix(b);
01424    a.tv_sec -= b.tv_sec;
01425    a.tv_usec -= b.tv_usec;
01426    if (a.tv_usec < 0) {
01427       a.tv_sec-- ;
01428       a.tv_usec += ONE_MILLION;
01429    }
01430    return a;
01431 }
01432 #undef ONE_MILLION
01433 
01434 /*! \brief glibc puts a lock inside random(3), so that the results are thread-safe.
01435  * BSD libc (and others) do not. */
01436 
01437 #ifndef linux
01438 AST_MUTEX_DEFINE_STATIC(randomlock);
01439 #endif
01440 
01441 long int ast_random(void)
01442 {
01443    long int res;
01444 #ifdef HAVE_DEV_URANDOM
01445    if (dev_urandom_fd >= 0) {
01446       int read_res = read(dev_urandom_fd, &res, sizeof(res));
01447       if (read_res > 0) {
01448          long int rm = RAND_MAX;
01449          res = res < 0 ? ~res : res;
01450          rm++;
01451          return res % rm;
01452       }
01453    }
01454 #endif
01455 #ifdef linux
01456    res = random();
01457 #else
01458    ast_mutex_lock(&randomlock);
01459    res = random();
01460    ast_mutex_unlock(&randomlock);
01461 #endif
01462    return res;
01463 }
01464 
01465 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
01466 {
01467    char *dataPut = start;
01468    int inEscape = 0;
01469    int inQuotes = 0;
01470 
01471    for (; *start; start++) {
01472       if (inEscape) {
01473          *dataPut++ = *start;       /* Always goes verbatim */
01474          inEscape = 0;
01475       } else {
01476          if (*start == '\\') {
01477             inEscape = 1;      /* Do not copy \ into the data */
01478          } else if (*start == '\'') {
01479             inQuotes = 1 - inQuotes;   /* Do not copy ' into the data */
01480          } else {
01481             /* Replace , with |, unless in quotes */
01482             *dataPut++ = inQuotes ? *start : ((*start == find) ? replace_with : *start);
01483          }
01484       }
01485    }
01486    if (start != dataPut)
01487       *dataPut = 0;
01488    return dataPut;
01489 }
01490 
01491 void ast_join(char *s, size_t len, const char * const w[])
01492 {
01493    int x, ofs = 0;
01494    const char *src;
01495 
01496    /* Join words into a string */
01497    if (!s)
01498       return;
01499    for (x = 0; ofs < len && w[x]; x++) {
01500       if (x > 0)
01501          s[ofs++] = ' ';
01502       for (src = w[x]; *src && ofs < len; src++)
01503          s[ofs++] = *src;
01504    }
01505    if (ofs == len)
01506       ofs--;
01507    s[ofs] = '\0';
01508 }
01509 
01510 /*
01511  * stringfields support routines.
01512  */
01513 
01514 /* this is a little complex... string fields are stored with their
01515    allocated size in the bytes preceding the string; even the
01516    constant 'empty' string has to be this way, so the code that
01517    checks to see if there is enough room for a new string doesn't
01518    have to have any special case checks
01519 */
01520 
01521 static const struct {
01522    ast_string_field_allocation allocation;
01523    char string[1];
01524 } __ast_string_field_empty_buffer;
01525 
01526 ast_string_field __ast_string_field_empty = __ast_string_field_empty_buffer.string;
01527 
01528 #define ALLOCATOR_OVERHEAD 48
01529 
01530 static size_t optimal_alloc_size(size_t size)
01531 {
01532    unsigned int count;
01533 
01534    size += ALLOCATOR_OVERHEAD;
01535 
01536    for (count = 1; size; size >>= 1, count++);
01537 
01538    return (1 << count) - ALLOCATOR_OVERHEAD;
01539 }
01540 
01541 /*! \brief add a new block to the pool.
01542  * We can only allocate from the topmost pool, so the
01543  * fields in *mgr reflect the size of that only.
01544  */
01545 static int add_string_pool(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
01546             size_t size, const char *file, int lineno, const char *func)
01547 {
01548    struct ast_string_field_pool *pool;
01549    size_t alloc_size = optimal_alloc_size(sizeof(*pool) + size);
01550 
01551 #if defined(__AST_DEBUG_MALLOC)
01552    if (!(pool = __ast_calloc(1, alloc_size, file, lineno, func))) {
01553       return -1;
01554    }
01555 #else
01556    if (!(pool = ast_calloc(1, alloc_size))) {
01557       return -1;
01558    }
01559 #endif
01560 
01561    pool->prev = *pool_head;
01562    pool->size = alloc_size - sizeof(*pool);
01563    *pool_head = pool;
01564    mgr->last_alloc = NULL;
01565 
01566    return 0;
01567 }
01568 
01569 /*
01570  * This is an internal API, code should not use it directly.
01571  * It initializes all fields as empty, then uses 'size' for 3 functions:
01572  * size > 0 means initialize the pool list with a pool of given size.
01573  * This must be called right after allocating the object.
01574  * size = 0 means release all pools except the most recent one.
01575  *      If the first pool was allocated via embedding in another
01576  *      object, that pool will be preserved instead.
01577  * This is useful to e.g. reset an object to the initial value.
01578  * size < 0 means release all pools.
01579  * This must be done before destroying the object.
01580  */
01581 int __ast_string_field_init(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
01582              int needed, const char *file, int lineno, const char *func)
01583 {
01584    const char **p = (const char **) pool_head + 1;
01585    struct ast_string_field_pool *cur = NULL;
01586    struct ast_string_field_pool *preserve = NULL;
01587 
01588    /* clear fields - this is always necessary */
01589    while ((struct ast_string_field_mgr *) p != mgr) {
01590       *p++ = __ast_string_field_empty;
01591    }
01592 
01593    mgr->last_alloc = NULL;
01594 #if defined(__AST_DEBUG_MALLOC)
01595    mgr->owner_file = file;
01596    mgr->owner_func = func;
01597    mgr->owner_line = lineno;
01598 #endif
01599    if (needed > 0) {    /* allocate the initial pool */
01600       *pool_head = NULL;
01601       mgr->embedded_pool = NULL;
01602       return add_string_pool(mgr, pool_head, needed, file, lineno, func);
01603    }
01604 
01605    /* if there is an embedded pool, we can't actually release *all*
01606     * pools, we must keep the embedded one. if the caller is about
01607     * to free the structure that contains the stringfield manager
01608     * and embedded pool anyway, it will be freed as part of that
01609     * operation.
01610     */
01611    if ((needed < 0) && mgr->embedded_pool) {
01612       needed = 0;
01613    }
01614 
01615    if (needed < 0) {    /* reset all pools */
01616       cur = *pool_head;
01617    } else if (mgr->embedded_pool) { /* preserve the embedded pool */
01618       preserve = mgr->embedded_pool;
01619       cur = *pool_head;
01620    } else {       /* preserve the last pool */
01621       if (*pool_head == NULL) {
01622          ast_log(LOG_WARNING, "trying to reset empty pool\n");
01623          return -1;
01624       }
01625       preserve = *pool_head;
01626       cur = preserve->prev;
01627    }
01628 
01629    if (preserve) {
01630       preserve->prev = NULL;
01631       preserve->used = preserve->active = 0;
01632    }
01633 
01634    while (cur) {
01635       struct ast_string_field_pool *prev = cur->prev;
01636 
01637       if (cur != preserve) {
01638          ast_free(cur);
01639       }
01640       cur = prev;
01641    }
01642 
01643    *pool_head = preserve;
01644 
01645    return 0;
01646 }
01647 
01648 ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr,
01649                   struct ast_string_field_pool **pool_head, size_t needed)
01650 {
01651    char *result = NULL;
01652    size_t space = (*pool_head)->size - (*pool_head)->used;
01653    size_t to_alloc;
01654 
01655    /* Make room for ast_string_field_allocation and make it a multiple of that. */
01656    to_alloc = ast_make_room_for(needed, ast_string_field_allocation);
01657    ast_assert(to_alloc % ast_alignof(ast_string_field_allocation) == 0);
01658 
01659    if (__builtin_expect(to_alloc > space, 0)) {
01660       size_t new_size = (*pool_head)->size;
01661 
01662       while (new_size < to_alloc) {
01663          new_size *= 2;
01664       }
01665 
01666 #if defined(__AST_DEBUG_MALLOC)
01667       if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func))
01668          return NULL;
01669 #else
01670       if (add_string_pool(mgr, pool_head, new_size, __FILE__, __LINE__, __FUNCTION__))
01671          return NULL;
01672 #endif
01673    }
01674 
01675    /* pool->base is always aligned (gcc aligned attribute). We ensure that
01676     * to_alloc is also a multiple of ast_alignof(ast_string_field_allocation)
01677     * causing result to always be aligned as well; which in turn fixes that
01678     * AST_STRING_FIELD_ALLOCATION(result) is aligned. */
01679    result = (*pool_head)->base + (*pool_head)->used;
01680    (*pool_head)->used += to_alloc;
01681    (*pool_head)->active += needed;
01682    result += ast_alignof(ast_string_field_allocation);
01683    AST_STRING_FIELD_ALLOCATION(result) = needed;
01684    mgr->last_alloc = result;
01685 
01686    return result;
01687 }
01688 
01689 int __ast_string_field_ptr_grow(struct ast_string_field_mgr *mgr,
01690             struct ast_string_field_pool **pool_head, size_t needed,
01691             const ast_string_field *ptr)
01692 {
01693    ssize_t grow = needed - AST_STRING_FIELD_ALLOCATION(*ptr);
01694    size_t space = (*pool_head)->size - (*pool_head)->used;
01695 
01696    if (*ptr != mgr->last_alloc) {
01697       return 1;
01698    }
01699 
01700    if (space < grow) {
01701       return 1;
01702    }
01703 
01704    (*pool_head)->used += grow;
01705    (*pool_head)->active += grow;
01706    AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
01707 
01708    return 0;
01709 }
01710 
01711 void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
01712                    const ast_string_field ptr)
01713 {
01714    struct ast_string_field_pool *pool, *prev;
01715 
01716    if (ptr == __ast_string_field_empty) {
01717       return;
01718    }
01719 
01720    for (pool = pool_head, prev = NULL; pool; prev = pool, pool = pool->prev) {
01721       if ((ptr >= pool->base) && (ptr <= (pool->base + pool->size))) {
01722          pool->active -= AST_STRING_FIELD_ALLOCATION(ptr);
01723          if ((pool->active == 0) && prev) {
01724             prev->prev = pool->prev;
01725             ast_free(pool);
01726          }
01727          break;
01728       }
01729    }
01730 }
01731 
01732 void __ast_string_field_ptr_build_va(struct ast_string_field_mgr *mgr,
01733                  struct ast_string_field_pool **pool_head,
01734                  ast_string_field *ptr, const char *format, va_list ap1, va_list ap2)
01735 {
01736    size_t needed;
01737    size_t available;
01738    size_t space = (*pool_head)->size - (*pool_head)->used;
01739    ssize_t grow;
01740    char *target;
01741 
01742    /* if the field already has space allocated, try to reuse it;
01743       otherwise, try to use the empty space at the end of the current
01744       pool
01745    */
01746    if (*ptr != __ast_string_field_empty) {
01747       target = (char *) *ptr;
01748       available = AST_STRING_FIELD_ALLOCATION(*ptr);
01749       if (*ptr == mgr->last_alloc) {
01750          available += space;
01751       }
01752    } else {
01753       /* pool->used is always a multiple of ast_alignof(ast_string_field_allocation)
01754        * so we don't need to re-align anything here.
01755        */
01756       target = (*pool_head)->base + (*pool_head)->used + ast_alignof(ast_string_field_allocation);
01757       available = space - ast_alignof(ast_string_field_allocation);
01758    }
01759 
01760    needed = vsnprintf(target, available, format, ap1) + 1;
01761 
01762    if (needed > available) {
01763       /* the allocation could not be satisfied using the field's current allocation
01764          (if it has one), or the space available in the pool (if it does not). allocate
01765          space for it, adding a new string pool if necessary.
01766       */
01767       if (!(target = (char *) __ast_string_field_alloc_space(mgr, pool_head, needed))) {
01768          return;
01769       }
01770       vsprintf(target, format, ap2);
01771       __ast_string_field_release_active(*pool_head, *ptr);
01772       *ptr = target;
01773    } else if (*ptr != target) {
01774       /* the allocation was satisfied using available space in the pool, but not
01775          using the space already allocated to the field
01776       */
01777       __ast_string_field_release_active(*pool_head, *ptr);
01778       mgr->last_alloc = *ptr = target;
01779       AST_STRING_FIELD_ALLOCATION(target) = needed;
01780       (*pool_head)->used += ast_make_room_for(needed, ast_string_field_allocation);
01781       (*pool_head)->active += needed;
01782    } else if ((grow = (needed - AST_STRING_FIELD_ALLOCATION(*ptr))) > 0) {
01783       /* the allocation was satisfied by using available space in the pool *and*
01784          the field was the last allocated field from the pool, so it grew
01785       */
01786       AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
01787       (*pool_head)->used += ast_align_for(grow, ast_string_field_allocation);
01788       (*pool_head)->active += grow;
01789    }
01790 }
01791 
01792 void __ast_string_field_ptr_build(struct ast_string_field_mgr *mgr,
01793               struct ast_string_field_pool **pool_head,
01794               ast_string_field *ptr, const char *format, ...)
01795 {
01796    va_list ap1, ap2;
01797 
01798    va_start(ap1, format);
01799    va_start(ap2, format);     /* va_copy does not exist on FreeBSD */
01800 
01801    __ast_string_field_ptr_build_va(mgr, pool_head, ptr, format, ap1, ap2);
01802 
01803    va_end(ap1);
01804    va_end(ap2);
01805 }
01806 
01807 void *__ast_calloc_with_stringfields(unsigned int num_structs, size_t struct_size, size_t field_mgr_offset,
01808                  size_t field_mgr_pool_offset, size_t pool_size, const char *file,
01809                  int lineno, const char *func)
01810 {
01811    struct ast_string_field_mgr *mgr;
01812    struct ast_string_field_pool *pool;
01813    struct ast_string_field_pool **pool_head;
01814    size_t pool_size_needed = sizeof(*pool) + pool_size;
01815    size_t size_to_alloc = optimal_alloc_size(struct_size + pool_size_needed);
01816    void *allocation;
01817    unsigned int x;
01818 
01819 #if defined(__AST_DEBUG_MALLOC)  
01820    if (!(allocation = __ast_calloc(num_structs, size_to_alloc, file, lineno, func))) {
01821       return NULL;
01822    }
01823 #else
01824    if (!(allocation = ast_calloc(num_structs, size_to_alloc))) {
01825       return NULL;
01826    }
01827 #endif
01828 
01829    for (x = 0; x < num_structs; x++) {
01830       void *base = allocation + (size_to_alloc * x);
01831       const char **p;
01832 
01833       mgr = base + field_mgr_offset;
01834       pool_head = base + field_mgr_pool_offset;
01835       pool = base + struct_size;
01836 
01837       p = (const char **) pool_head + 1;
01838       while ((struct ast_string_field_mgr *) p != mgr) {
01839          *p++ = __ast_string_field_empty;
01840       }
01841 
01842       mgr->embedded_pool = pool;
01843       *pool_head = pool;
01844       pool->size = size_to_alloc - struct_size - sizeof(*pool);
01845 #if defined(__AST_DEBUG_MALLOC)
01846       mgr->owner_file = file;
01847       mgr->owner_func = func;
01848       mgr->owner_line = lineno;
01849 #endif
01850    }
01851 
01852    return allocation;
01853 }
01854 
01855 /* end of stringfields support */
01856 
01857 AST_MUTEX_DEFINE_STATIC(fetchadd_m); /* used for all fetc&add ops */
01858 
01859 int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
01860 {
01861    int ret;
01862    ast_mutex_lock(&fetchadd_m);
01863    ret = *p;
01864    *p += v;
01865    ast_mutex_unlock(&fetchadd_m);
01866    return ret;
01867 }
01868 
01869 /*! \brief
01870  * get values from config variables.
01871  */
01872 int ast_get_timeval(const char *src, struct timeval *dst, struct timeval _default, int *consumed)
01873 {
01874    long double dtv = 0.0;
01875    int scanned;
01876 
01877    if (dst == NULL)
01878       return -1;
01879 
01880    *dst = _default;
01881 
01882    if (ast_strlen_zero(src))
01883       return -1;
01884 
01885    /* only integer at the moment, but one day we could accept more formats */
01886    if (sscanf(src, "%30Lf%n", &dtv, &scanned) > 0) {
01887       dst->tv_sec = dtv;
01888       dst->tv_usec = (dtv - dst->tv_sec) * 1000000.0;
01889       if (consumed)
01890          *consumed = scanned;
01891       return 0;
01892    } else
01893       return -1;
01894 }
01895 
01896 /*! \brief
01897  * get values from config variables.
01898  */
01899 int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
01900 {
01901    long t;
01902    int scanned;
01903 
01904    if (dst == NULL)
01905       return -1;
01906 
01907    *dst = _default;
01908 
01909    if (ast_strlen_zero(src))
01910       return -1;
01911 
01912    /* only integer at the moment, but one day we could accept more formats */
01913    if (sscanf(src, "%30ld%n", &t, &scanned) == 1) {
01914       *dst = t;
01915       if (consumed)
01916          *consumed = scanned;
01917       return 0;
01918    } else
01919       return -1;
01920 }
01921 
01922 void ast_enable_packet_fragmentation(int sock)
01923 {
01924 #if defined(HAVE_IP_MTU_DISCOVER)
01925    int val = IP_PMTUDISC_DONT;
01926    
01927    if (setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)))
01928       ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n");
01929 #endif /* HAVE_IP_MTU_DISCOVER */
01930 }
01931 
01932 int ast_mkdir(const char *path, int mode)
01933 {
01934    char *ptr;
01935    int len = strlen(path), count = 0, x, piececount = 0;
01936    char *tmp = ast_strdupa(path);
01937    char **pieces;
01938    char *fullpath = alloca(len + 1);
01939    int res = 0;
01940 
01941    for (ptr = tmp; *ptr; ptr++) {
01942       if (*ptr == '/')
01943          count++;
01944    }
01945 
01946    /* Count the components to the directory path */
01947    pieces = alloca(count * sizeof(*pieces));
01948    for (ptr = tmp; *ptr; ptr++) {
01949       if (*ptr == '/') {
01950          *ptr = '\0';
01951          pieces[piececount++] = ptr + 1;
01952       }
01953    }
01954 
01955    *fullpath = '\0';
01956    for (x = 0; x < piececount; x++) {
01957       /* This looks funky, but the buffer is always ideally-sized, so it's fine. */
01958       strcat(fullpath, "/");
01959       strcat(fullpath, pieces[x]);
01960       res = mkdir(fullpath, mode);
01961       if (res && errno != EEXIST)
01962          return errno;
01963    }
01964    return 0;
01965 }
01966 
01967 int ast_utils_init(void)
01968 {
01969 #ifdef HAVE_DEV_URANDOM
01970    dev_urandom_fd = open("/dev/urandom", O_RDONLY);
01971 #endif
01972    base64_init();
01973 #ifdef DEBUG_THREADS
01974 #if !defined(LOW_MEMORY)
01975    ast_cli_register_multiple(utils_cli, ARRAY_LEN(utils_cli));
01976 #endif
01977 #endif
01978    return 0;
01979 }
01980 
01981 
01982 /*!
01983  *\brief Parse digest authorization header.
01984  *\return Returns -1 if we have no auth or something wrong with digest.
01985  *\note  This function may be used for Digest request and responce header.
01986  * request arg is set to nonzero, if we parse Digest Request.
01987  * pedantic arg can be set to nonzero if we need to do addition Digest check.
01988  */
01989 int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic) {
01990    char *c;
01991    struct ast_str *str = ast_str_create(16);
01992 
01993    /* table of recognised keywords, and places where they should be copied */
01994    const struct x {
01995       const char *key;
01996       const ast_string_field *field;
01997    } *i, keys[] = {
01998       { "username=", &d->username },
01999       { "realm=", &d->realm },
02000       { "nonce=", &d->nonce },
02001       { "uri=", &d->uri },
02002       { "domain=", &d->domain },
02003       { "response=", &d->response },
02004       { "cnonce=", &d->cnonce },
02005       { "opaque=", &d->opaque },
02006       /* Special cases that cannot be directly copied */
02007       { "algorithm=", NULL },
02008       { "qop=", NULL },
02009       { "nc=", NULL },
02010       { NULL, 0 },
02011    };
02012 
02013    if (ast_strlen_zero(digest) || !d || !str) {
02014       ast_free(str);
02015       return -1;
02016    }
02017 
02018    ast_str_set(&str, 0, "%s", digest);
02019 
02020    c = ast_skip_blanks(ast_str_buffer(str));
02021 
02022    if (strncasecmp(c, "Digest ", strlen("Digest "))) {
02023       ast_log(LOG_WARNING, "Missing Digest.\n");
02024       ast_free(str);
02025       return -1;
02026    }
02027    c += strlen("Digest ");
02028 
02029    /* lookup for keys/value pair */
02030    while (c && *c && *(c = ast_skip_blanks(c))) {
02031       /* find key */
02032       for (i = keys; i->key != NULL; i++) {
02033          char *src, *separator;
02034          int unescape = 0;
02035          if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
02036             continue;
02037          }
02038 
02039          /* Found. Skip keyword, take text in quotes or up to the separator. */
02040          c += strlen(i->key);
02041          if (*c == '"') {
02042             src = ++c;
02043             separator = "\"";
02044             unescape = 1;
02045          } else {
02046             src = c;
02047             separator = ",";
02048          }
02049          strsep(&c, separator); /* clear separator and move ptr */
02050          if (unescape) {
02051             ast_unescape_c(src);
02052          }
02053          if (i->field) {
02054             ast_string_field_ptr_set(d, i->field, src);
02055          } else {
02056             /* Special cases that require additional procesing */
02057             if (!strcasecmp(i->key, "algorithm=")) {
02058                if (strcasecmp(src, "MD5")) {
02059                   ast_log(LOG_WARNING, "Digest algorithm: \"%s\" not supported.\n", src);
02060                   ast_free(str);
02061                   return -1;
02062                }
02063             } else if (!strcasecmp(i->key, "qop=") && !strcasecmp(src, "auth")) {
02064                d->qop = 1;
02065             } else if (!strcasecmp(i->key, "nc=")) {
02066                unsigned long u;
02067                if (sscanf(src, "%30lx", &u) != 1) {
02068                   ast_log(LOG_WARNING, "Incorrect Digest nc value: \"%s\".\n", src);
02069                   ast_free(str);
02070                   return -1;
02071                }
02072                ast_string_field_set(d, nc, src);
02073             }
02074          }
02075          break;
02076       }
02077       if (i->key == NULL) { /* not found, try ',' */
02078          strsep(&c, ",");
02079       }
02080    }
02081    ast_free(str);
02082 
02083    /* Digest checkout */
02084    if (ast_strlen_zero(d->realm) || ast_strlen_zero(d->nonce)) {
02085       /* "realm" and "nonce" MUST be always exist */
02086       return -1;
02087    }
02088 
02089    if (!request) {
02090       /* Additional check for Digest response */
02091       if (ast_strlen_zero(d->username) || ast_strlen_zero(d->uri) || ast_strlen_zero(d->response)) {
02092          return -1;
02093       }
02094 
02095       if (pedantic && d->qop && (ast_strlen_zero(d->cnonce) || ast_strlen_zero(d->nc))) {
02096          return -1;
02097       }
02098    }
02099 
02100    return 0;
02101 }
02102 
02103 #ifndef __AST_DEBUG_MALLOC
02104 int _ast_asprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, ...)
02105 {
02106    int res;
02107    va_list ap;
02108 
02109    va_start(ap, fmt);
02110    if ((res = vasprintf(ret, fmt, ap)) == -1) {
02111       MALLOC_FAILURE_MSG;
02112    }
02113    va_end(ap);
02114 
02115    return res;
02116 }
02117 #endif
02118 
02119 char *ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size)
02120 {
02121    const char *envPATH = getenv("PATH");
02122    char *tpath, *path;
02123    struct stat unused;
02124    if (!envPATH) {
02125       return NULL;
02126    }
02127    tpath = ast_strdupa(envPATH);
02128    while ((path = strsep(&tpath, ":"))) {
02129       snprintf(fullpath, fullpath_size, "%s/%s", path, binary);
02130       if (!stat(fullpath, &unused)) {
02131          return fullpath;
02132       }
02133    }
02134    return NULL;
02135 }
02136 

Generated on Mon Oct 8 12:39:06 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7