Mon May 14 04:42:58 2007

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 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00029 
00030 #include <ctype.h>
00031 #include <string.h>
00032 #include <unistd.h>
00033 #include <stdlib.h>
00034 #include <errno.h>
00035 #include <stdarg.h>
00036 #include <stdio.h>
00037 #include <sys/types.h>
00038 #include <sys/socket.h>
00039 #include <netinet/in.h>
00040 #include <arpa/inet.h>
00041 
00042 #define AST_API_MODULE     /* ensure that inlinable API functions will be built in lock.h if required */
00043 #include "asterisk/lock.h"
00044 #include "asterisk/io.h"
00045 #include "asterisk/logger.h"
00046 #include "asterisk/md5.h"
00047 #include "asterisk/sha1.h"
00048 #include "asterisk/options.h"
00049 
00050 #define AST_API_MODULE     /* ensure that inlinable API functions will be built in this module if required */
00051 #include "asterisk/strings.h"
00052 
00053 #define AST_API_MODULE     /* ensure that inlinable API functions will be built in this module if required */
00054 #include "asterisk/time.h"
00055 
00056 #define AST_API_MODULE     /* ensure that inlinable API functions will be built in this module if required */
00057 #include "asterisk/stringfields.h"
00058 
00059 #define AST_API_MODULE     /* ensure that inlinable API functions will be built in this module if required */
00060 #include "asterisk/utils.h"
00061 
00062 #define AST_API_MODULE
00063 #include "asterisk/threadstorage.h"
00064 
00065 static char base64[64];
00066 static char b2a[256];
00067 
00068 AST_THREADSTORAGE(inet_ntoa_buf, inet_ntoa_buf_init);
00069 
00070 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) || defined(__CYGWIN__)
00071 
00072 #define ERANGE 34 /*!< duh? ERANGE value copied from web... */
00073 #undef gethostbyname
00074 
00075 AST_MUTEX_DEFINE_STATIC(__mutex);
00076 
00077 /*! \brief Reentrant replacement for gethostbyname for BSD-based systems.
00078 \note This
00079 routine is derived from code originally written and placed in the public 
00080 domain by Enzo Michelangeli <em@em.no-ip.com> */
00081 
00082 static int gethostbyname_r (const char *name, struct hostent *ret, char *buf,
00083             size_t buflen, struct hostent **result, 
00084             int *h_errnop) 
00085 {
00086    int hsave;
00087    struct hostent *ph;
00088    ast_mutex_lock(&__mutex); /* begin critical area */
00089    hsave = h_errno;
00090 
00091    ph = gethostbyname(name);
00092    *h_errnop = h_errno; /* copy h_errno to *h_herrnop */
00093    if (ph == NULL) {
00094       *result = NULL;
00095    } else {
00096       char **p, **q;
00097       char *pbuf;
00098       int nbytes=0;
00099       int naddr=0, naliases=0;
00100       /* determine if we have enough space in buf */
00101 
00102       /* count how many addresses */
00103       for (p = ph->h_addr_list; *p != 0; p++) {
00104          nbytes += ph->h_length; /* addresses */
00105          nbytes += sizeof(*p); /* pointers */
00106          naddr++;
00107       }
00108       nbytes += sizeof(*p); /* one more for the terminating NULL */
00109 
00110       /* count how many aliases, and total length of strings */
00111       for (p = ph->h_aliases; *p != 0; p++) {
00112          nbytes += (strlen(*p)+1); /* aliases */
00113          nbytes += sizeof(*p);  /* pointers */
00114          naliases++;
00115       }
00116       nbytes += sizeof(*p); /* one more for the terminating NULL */
00117 
00118       /* here nbytes is the number of bytes required in buffer */
00119       /* as a terminator must be there, the minimum value is ph->h_length */
00120       if (nbytes > buflen) {
00121          *result = NULL;
00122          ast_mutex_unlock(&__mutex); /* end critical area */
00123          return ERANGE; /* not enough space in buf!! */
00124       }
00125 
00126       /* There is enough space. Now we need to do a deep copy! */
00127       /* Allocation in buffer:
00128          from [0] to [(naddr-1) * sizeof(*p)]:
00129          pointers to addresses
00130          at [naddr * sizeof(*p)]:
00131          NULL
00132          from [(naddr+1) * sizeof(*p)] to [(naddr+naliases) * sizeof(*p)] :
00133          pointers to aliases
00134          at [(naddr+naliases+1) * sizeof(*p)]:
00135          NULL
00136          then naddr addresses (fixed length), and naliases aliases (asciiz).
00137       */
00138 
00139       *ret = *ph;   /* copy whole structure (not its address!) */
00140 
00141       /* copy addresses */
00142       q = (char **)buf; /* pointer to pointers area (type: char **) */
00143       ret->h_addr_list = q; /* update pointer to address list */
00144       pbuf = buf + ((naddr + naliases + 2) * sizeof(*p)); /* skip that area */
00145       for (p = ph->h_addr_list; *p != 0; p++) {
00146          memcpy(pbuf, *p, ph->h_length); /* copy address bytes */
00147          *q++ = pbuf; /* the pointer is the one inside buf... */
00148          pbuf += ph->h_length; /* advance pbuf */
00149       }
00150       *q++ = NULL; /* address list terminator */
00151 
00152       /* copy aliases */
00153       ret->h_aliases = q; /* update pointer to aliases list */
00154       for (p = ph->h_aliases; *p != 0; p++) {
00155          strcpy(pbuf, *p); /* copy alias strings */
00156          *q++ = pbuf; /* the pointer is the one inside buf... */
00157          pbuf += strlen(*p); /* advance pbuf */
00158          *pbuf++ = 0; /* string terminator */
00159       }
00160       *q++ = NULL; /* terminator */
00161 
00162       strcpy(pbuf, ph->h_name); /* copy alias strings */
00163       ret->h_name = pbuf;
00164       pbuf += strlen(ph->h_name); /* advance pbuf */
00165       *pbuf++ = 0; /* string terminator */
00166 
00167       *result = ret;  /* and let *result point to structure */
00168 
00169    }
00170    h_errno = hsave;  /* restore h_errno */
00171    ast_mutex_unlock(&__mutex); /* end critical area */
00172 
00173    return (*result == NULL); /* return 0 on success, non-zero on error */
00174 }
00175 
00176 
00177 #endif
00178 
00179 /*! \brief Re-entrant (thread safe) version of gethostbyname that replaces the 
00180    standard gethostbyname (which is not thread safe)
00181 */
00182 struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp)
00183 {
00184    int res;
00185    int herrno;
00186    int dots=0;
00187    const char *s;
00188    struct hostent *result = NULL;
00189    /* Although it is perfectly legitimate to lookup a pure integer, for
00190       the sake of the sanity of people who like to name their peers as
00191       integers, we break with tradition and refuse to look up a
00192       pure integer */
00193    s = host;
00194    res = 0;
00195    while(s && *s) {
00196       if (*s == '.')
00197          dots++;
00198       else if (!isdigit(*s))
00199          break;
00200       s++;
00201    }
00202    if (!s || !*s) {
00203       /* Forge a reply for IP's to avoid octal IP's being interpreted as octal */
00204       if (dots != 3)
00205          return NULL;
00206       memset(hp, 0, sizeof(struct ast_hostent));
00207       hp->hp.h_addrtype = AF_INET;
00208       hp->hp.h_addr_list = (void *) hp->buf;
00209       hp->hp.h_addr = hp->buf + sizeof(void *);
00210       if (inet_pton(AF_INET, host, hp->hp.h_addr) > 0)
00211          return &hp->hp;
00212       return NULL;
00213       
00214    }
00215 #ifdef SOLARIS
00216    result = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &herrno);
00217 
00218    if (!result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
00219       return NULL;
00220 #else
00221    res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
00222 
00223    if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
00224       return NULL;
00225 #endif
00226    return &hp->hp;
00227 }
00228 
00229 
00230 
00231 AST_MUTEX_DEFINE_STATIC(test_lock);
00232 AST_MUTEX_DEFINE_STATIC(test_lock2);
00233 static pthread_t test_thread; 
00234 static int lock_count = 0;
00235 static int test_errors = 0;
00236 
00237 /*! \brief This is a regression test for recursive mutexes.
00238    test_for_thread_safety() will return 0 if recursive mutex locks are
00239    working properly, and non-zero if they are not working properly. */
00240 static void *test_thread_body(void *data) 
00241 { 
00242    ast_mutex_lock(&test_lock);
00243    lock_count += 10;
00244    if (lock_count != 10) 
00245       test_errors++;
00246    ast_mutex_lock(&test_lock);
00247    lock_count += 10;
00248    if (lock_count != 20) 
00249       test_errors++;
00250    ast_mutex_lock(&test_lock2);
00251    ast_mutex_unlock(&test_lock);
00252    lock_count -= 10;
00253    if (lock_count != 10) 
00254       test_errors++;
00255    ast_mutex_unlock(&test_lock);
00256    lock_count -= 10;
00257    ast_mutex_unlock(&test_lock2);
00258    if (lock_count != 0) 
00259       test_errors++;
00260    return NULL;
00261 } 
00262 
00263 int test_for_thread_safety(void)
00264 { 
00265    ast_mutex_lock(&test_lock2);
00266    ast_mutex_lock(&test_lock);
00267    lock_count += 1;
00268    ast_mutex_lock(&test_lock);
00269    lock_count += 1;
00270    ast_pthread_create(&test_thread, NULL, test_thread_body, NULL); 
00271    usleep(100);
00272    if (lock_count != 2) 
00273       test_errors++;
00274    ast_mutex_unlock(&test_lock);
00275    lock_count -= 1;
00276    usleep(100); 
00277    if (lock_count != 1) 
00278       test_errors++;
00279    ast_mutex_unlock(&test_lock);
00280    lock_count -= 1;
00281    if (lock_count != 0) 
00282       test_errors++;
00283    ast_mutex_unlock(&test_lock2);
00284    usleep(100);
00285    if (lock_count != 0) 
00286       test_errors++;
00287    pthread_join(test_thread, NULL);
00288    return(test_errors);          /* return 0 on success. */
00289 }
00290 
00291 /*! \brief Produce 32 char MD5 hash of value. */
00292 void ast_md5_hash(char *output, char *input)
00293 {
00294    struct MD5Context md5;
00295    unsigned char digest[16];
00296    char *ptr;
00297    int x;
00298 
00299    MD5Init(&md5);
00300    MD5Update(&md5, (unsigned char *)input, strlen(input));
00301    MD5Final(digest, &md5);
00302    ptr = output;
00303    for (x = 0; x < 16; x++)
00304       ptr += sprintf(ptr, "%2.2x", digest[x]);
00305 }
00306 
00307 /*! \brief Produce 40 char SHA1 hash of value. */
00308 void ast_sha1_hash(char *output, char *input)
00309 {
00310    struct SHA1Context sha;
00311    char *ptr;
00312    int x;
00313    uint8_t Message_Digest[20];
00314 
00315    SHA1Reset(&sha);
00316    
00317    SHA1Input(&sha, (const unsigned char *) input, strlen(input));
00318 
00319    SHA1Result(&sha, Message_Digest);
00320    ptr = output;
00321    for (x = 0; x < 20; x++)
00322       ptr += sprintf(ptr, "%2.2x", Message_Digest[x]);
00323 }
00324 
00325 /*! \brief decode BASE64 encoded text */
00326 int ast_base64decode(unsigned char *dst, const char *src, int max)
00327 {
00328    int cnt = 0;
00329    unsigned int byte = 0;
00330    unsigned int bits = 0;
00331    int incnt = 0;
00332    while(*src && (cnt < max)) {
00333       /* Shift in 6 bits of input */
00334       byte <<= 6;
00335       byte |= (b2a[(int)(*src)]) & 0x3f;
00336       bits += 6;
00337       src++;
00338       incnt++;
00339       /* If we have at least 8 bits left over, take that character 
00340          off the top */
00341       if (bits >= 8)  {
00342          bits -= 8;
00343          *dst = (byte >> bits) & 0xff;
00344          dst++;
00345          cnt++;
00346       }
00347    }
00348    /* Dont worry about left over bits, they're extra anyway */
00349    return cnt;
00350 }
00351 
00352 /*! \brief encode text to BASE64 coding */
00353 int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks)
00354 {
00355    int cnt = 0;
00356    int col = 0;
00357    unsigned int byte = 0;
00358    int bits = 0;
00359    int cntin = 0;
00360    /* Reserve space for null byte at end of string */
00361    max--;
00362    while ((cntin < srclen) && (cnt < max)) {
00363       byte <<= 8;
00364       byte |= *(src++);
00365       bits += 8;
00366       cntin++;
00367       if ((bits == 24) && (cnt + 4 <= max)) {
00368          *dst++ = base64[(byte >> 18) & 0x3f];
00369          *dst++ = base64[(byte >> 12) & 0x3f];
00370          *dst++ = base64[(byte >> 6) & 0x3f];
00371          *dst++ = base64[byte & 0x3f];
00372          cnt += 4;
00373          col += 4;
00374          bits = 0;
00375          byte = 0;
00376       }
00377       if (linebreaks && (cnt < max) && (col == 64)) {
00378          *dst++ = '\n';
00379          cnt++;
00380          col = 0;
00381       }
00382    }
00383    if (bits && (cnt + 4 <= max)) {
00384       /* Add one last character for the remaining bits, 
00385          padding the rest with 0 */
00386       byte <<= 24 - bits;
00387       *dst++ = base64[(byte >> 18) & 0x3f];
00388       *dst++ = base64[(byte >> 12) & 0x3f];
00389       if (bits == 16)
00390          *dst++ = base64[(byte >> 6) & 0x3f];
00391       else
00392          *dst++ = '=';
00393       *dst++ = '=';
00394       cnt += 4;
00395    }
00396    if (linebreaks && (cnt < max)) {
00397       *dst++ = '\n';
00398       cnt++;
00399    }
00400    *dst = '\0';
00401    return cnt;
00402 }
00403 
00404 int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
00405 {
00406    return ast_base64encode_full(dst, src, srclen, max, 0);
00407 }
00408 
00409 static void base64_init(void)
00410 {
00411    int x;
00412    memset(b2a, -1, sizeof(b2a));
00413    /* Initialize base-64 Conversion table */
00414    for (x = 0; x < 26; x++) {
00415       /* A-Z */
00416       base64[x] = 'A' + x;
00417       b2a['A' + x] = x;
00418       /* a-z */
00419       base64[x + 26] = 'a' + x;
00420       b2a['a' + x] = x + 26;
00421       /* 0-9 */
00422       if (x < 10) {
00423          base64[x + 52] = '0' + x;
00424          b2a['0' + x] = x + 52;
00425       }
00426    }
00427    base64[62] = '+';
00428    base64[63] = '/';
00429    b2a[(int)'+'] = 62;
00430    b2a[(int)'/'] = 63;
00431 }
00432 
00433 /*! \brief  ast_uri_encode: Turn text string to URI-encoded %XX version
00434 \note    At this point, we're converting from ISO-8859-x (8-bit), not UTF8
00435    as in the SIP protocol spec 
00436    If doreserved == 1 we will convert reserved characters also.
00437    RFC 2396, section 2.4
00438    outbuf needs to have more memory allocated than the instring
00439    to have room for the expansion. Every char that is converted
00440    is replaced by three ASCII characters.
00441 
00442    Note: The doreserved option is needed for replaces header in
00443    SIP transfers.
00444 */
00445 char *ast_uri_encode(const char *string, char *outbuf, int buflen, int doreserved) 
00446 {
00447    char *reserved = ";/?:@&=+$, ";  /* Reserved chars */
00448 
00449    const char *ptr  = string; /* Start with the string */
00450    char *out = NULL;
00451    char *buf = NULL;
00452 
00453    ast_copy_string(outbuf, string, buflen);
00454 
00455    /* If there's no characters to convert, just go through and don't do anything */
00456    while (*ptr) {
00457       if (((unsigned char) *ptr) > 127 || (doreserved && strchr(reserved, *ptr)) ) {
00458          /* Oops, we need to start working here */
00459          if (!buf) {
00460             buf = outbuf;
00461             out = buf + (ptr - string) ;  /* Set output ptr */
00462          }
00463          out += sprintf(out, "%%%02x", (unsigned char) *ptr);
00464       } else if (buf) {
00465          *out = *ptr;   /* Continue copying the string */
00466          out++;
00467       } 
00468       ptr++;
00469    }
00470    if (buf)
00471       *out = '\0';
00472    return outbuf;
00473 }
00474 
00475 /*! \brief  ast_uri_decode: Decode SIP URI, URN, URL (overwrite the string)  */
00476 void ast_uri_decode(char *s) 
00477 {
00478    char *o;
00479    unsigned int tmp;
00480 
00481    for (o = s; *s; s++, o++) {
00482       if (*s == '%' && strlen(s) > 2 && sscanf(s + 1, "%2x", &tmp) == 1) {
00483          /* have '%', two chars and correct parsing */
00484          *o = tmp;
00485          s += 2;  /* Will be incremented once more when we break out */
00486       } else /* all other cases, just copy */
00487          *o = *s;
00488    }
00489    *o = '\0';
00490 }
00491 
00492 /*! \brief  ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa */
00493 const char *ast_inet_ntoa(struct in_addr ia)
00494 {
00495    char *buf;
00496 
00497    if (!(buf = ast_threadstorage_get(&inet_ntoa_buf, INET_ADDRSTRLEN)))
00498       return "";
00499 
00500    return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
00501 }
00502 
00503 int ast_utils_init(void)
00504 {
00505    base64_init();
00506    return 0;
00507 }
00508 
00509 #ifndef __linux__
00510 #undef pthread_create /* For ast_pthread_create function only */
00511 #endif /* !__linux__ */
00512 
00513 #if !defined(LOW_MEMORY)
00514 /*
00515  * support for 'show threads'. The start routine is wrapped by
00516  * dummy_start(), so that ast_register_thread() and
00517  * ast_unregister_thread() know the thread identifier.
00518  */
00519 struct thr_arg {
00520    void *(*start_routine)(void *);
00521    void *data;
00522    char *name;
00523 };
00524 
00525 /*
00526  * on OS/X, pthread_cleanup_push() and pthread_cleanup_pop()
00527  * are odd macros which start and end a block, so they _must_ be
00528  * used in pairs (the latter with a '1' argument to call the
00529  * handler on exit.
00530  * On BSD we don't need this, but we keep it for compatibility.
00531  */
00532 static void *dummy_start(void *data)
00533 {
00534    void *ret;
00535    struct thr_arg a = *((struct thr_arg *) data);  /* make a local copy */
00536 
00537    /* note that even though data->name is a pointer to allocated memory,
00538       we are not freeing it here because ast_register_thread is going to
00539       keep a copy of the pointer and then ast_unregister_thread will
00540       free the memory
00541    */
00542    free(data);
00543    ast_register_thread(a.name);
00544    pthread_cleanup_push(ast_unregister_thread, (void *) pthread_self());
00545    ret = a.start_routine(a.data);
00546    pthread_cleanup_pop(1);
00547 
00548    return ret;
00549 }
00550 
00551 #endif /* !LOW_MEMORY */
00552 
00553 int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
00554               void *data, size_t stacksize, const char *file, const char *caller,
00555               int line, const char *start_fn)
00556 {
00557 #if !defined(LOW_MEMORY)
00558    struct thr_arg *a;
00559 #endif
00560 
00561    if (!attr) {
00562       attr = alloca(sizeof(*attr));
00563       pthread_attr_init(attr);
00564    }
00565 
00566 #ifdef __linux__
00567    /* On Linux, pthread_attr_init() defaults to PTHREAD_EXPLICIT_SCHED,
00568       which is kind of useless. Change this here to
00569       PTHREAD_INHERIT_SCHED; that way the -p option to set realtime
00570       priority will propagate down to new threads by default.
00571       This does mean that callers cannot set a different priority using
00572       PTHREAD_EXPLICIT_SCHED in the attr argument; instead they must set
00573       the priority afterwards with pthread_setschedparam(). */
00574    if ((errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED)))
00575       ast_log(LOG_WARNING, "pthread_attr_setinheritsched: %s\n", strerror(errno));
00576 #endif
00577 
00578    if (!stacksize)
00579       stacksize = AST_STACKSIZE;
00580 
00581    if ((errno = pthread_attr_setstacksize(attr, stacksize ? stacksize : AST_STACKSIZE)))
00582       ast_log(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(errno));
00583 
00584 #if !defined(LOW_MEMORY)
00585    if ((a = ast_malloc(sizeof(*a)))) {
00586       a->start_routine = start_routine;
00587       a->data = data;
00588       start_routine = dummy_start;
00589       asprintf(&a->name, "%-20s started at [%5d] %s %s()",
00590           start_fn, line, file, caller);
00591       data = a;
00592    }
00593 #endif /* !LOW_MEMORY */
00594 
00595    return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */
00596 }
00597 
00598 int ast_wait_for_input(int fd, int ms)
00599 {
00600    struct pollfd pfd[1];
00601    memset(pfd, 0, sizeof(pfd));
00602    pfd[0].fd = fd;
00603    pfd[0].events = POLLIN|POLLPRI;
00604    return poll(pfd, 1, ms);
00605 }
00606 
00607 /*!
00608  * Try to write string, but wait no more than ms milliseconds before timing out.
00609  *
00610  * \note The code assumes that the file descriptor has NONBLOCK set,
00611  * so there is only one system call made to do a write, unless we actually
00612  * have a need to wait.  This way, we get better performance.
00613  * If the descriptor is blocking, all assumptions on the guaranteed
00614  * detail do not apply anymore.
00615  * Also note that in the current implementation, the delay is per-write,
00616  * so you still have no guarantees, anyways.
00617  * Fortunately the routine is only used in a few places (cli.c, manager.c,
00618  * res_agi.c) so it is reasonably easy to check how it behaves there.
00619  *
00620  * XXX We either need to fix the code, or fix the documentation.
00621  */
00622 int ast_carefulwrite(int fd, char *s, int len, int timeoutms) 
00623 {
00624    /* Try to write string, but wait no more than ms milliseconds
00625       before timing out */
00626    int res = 0;
00627    struct pollfd fds[1];
00628    while (len) {
00629       res = write(fd, s, len);
00630       if ((res < 0) && (errno != EAGAIN)) {
00631          return -1;
00632       }
00633       if (res < 0)
00634          res = 0;
00635       len -= res;
00636       s += res;
00637       res = 0;
00638       if (len) {
00639          fds[0].fd = fd;
00640          fds[0].events = POLLOUT;
00641          /* Wait until writable again */
00642          res = poll(fds, 1, timeoutms);
00643          if (res < 1)
00644             return -1;
00645       }
00646    }
00647    return res;
00648 }
00649 
00650 char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
00651 {
00652    char *e;
00653    char *q;
00654 
00655    s = ast_strip(s);
00656    if ((q = strchr(beg_quotes, *s)) && *q != '\0') {
00657       e = s + strlen(s) - 1;
00658       if (*e == *(end_quotes + (q - beg_quotes))) {
00659          s++;
00660          *e = '\0';
00661       }
00662    }
00663 
00664    return s;
00665 }
00666 
00667 int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap)
00668 {
00669    int result;
00670 
00671    if (!buffer || !*buffer || !space || !*space)
00672       return -1;
00673 
00674    result = vsnprintf(*buffer, *space, fmt, ap);
00675 
00676    if (result < 0)
00677       return -1;
00678    else if (result > *space)
00679       result = *space;
00680 
00681    *buffer += result;
00682    *space -= result;
00683    return 0;
00684 }
00685 
00686 int ast_build_string(char **buffer, size_t *space, const char *fmt, ...)
00687 {
00688    va_list ap;
00689    int result;
00690 
00691    va_start(ap, fmt);
00692    result = ast_build_string_va(buffer, space, fmt, ap);
00693    va_end(ap);
00694 
00695    return result;
00696 }
00697 
00698 int ast_true(const char *s)
00699 {
00700    if (ast_strlen_zero(s))
00701       return 0;
00702 
00703    /* Determine if this is a true value */
00704    if (!strcasecmp(s, "yes") ||
00705        !strcasecmp(s, "true") ||
00706        !strcasecmp(s, "y") ||
00707        !strcasecmp(s, "t") ||
00708        !strcasecmp(s, "1") ||
00709        !strcasecmp(s, "on"))
00710       return -1;
00711 
00712    return 0;
00713 }
00714 
00715 int ast_false(const char *s)
00716 {
00717    if (ast_strlen_zero(s))
00718       return 0;
00719 
00720    /* Determine if this is a false value */
00721    if (!strcasecmp(s, "no") ||
00722        !strcasecmp(s, "false") ||
00723        !strcasecmp(s, "n") ||
00724        !strcasecmp(s, "f") ||
00725        !strcasecmp(s, "0") ||
00726        !strcasecmp(s, "off"))
00727       return -1;
00728 
00729    return 0;
00730 }
00731 
00732 #define ONE_MILLION  1000000
00733 /*
00734  * put timeval in a valid range. usec is 0..999999
00735  * negative values are not allowed and truncated.
00736  */
00737 static struct timeval tvfix(struct timeval a)
00738 {
00739    if (a.tv_usec >= ONE_MILLION) {
00740       ast_log(LOG_WARNING, "warning too large timestamp %ld.%ld\n",
00741          a.tv_sec, (long int) a.tv_usec);
00742       a.tv_sec += a.tv_usec / ONE_MILLION;
00743       a.tv_usec %= ONE_MILLION;
00744    } else if (a.tv_usec < 0) {
00745       ast_log(LOG_WARNING, "warning negative timestamp %ld.%ld\n",
00746          a.tv_sec, (long int) a.tv_usec);
00747       a.tv_usec = 0;
00748    }
00749    return a;
00750 }
00751 
00752 struct timeval ast_tvadd(struct timeval a, struct timeval b)
00753 {
00754    /* consistency checks to guarantee usec in 0..999999 */
00755    a = tvfix(a);
00756    b = tvfix(b);
00757    a.tv_sec += b.tv_sec;
00758    a.tv_usec += b.tv_usec;
00759    if (a.tv_usec >= ONE_MILLION) {
00760       a.tv_sec++;
00761       a.tv_usec -= ONE_MILLION;
00762    }
00763    return a;
00764 }
00765 
00766 struct timeval ast_tvsub(struct timeval a, struct timeval b)
00767 {
00768    /* consistency checks to guarantee usec in 0..999999 */
00769    a = tvfix(a);
00770    b = tvfix(b);
00771    a.tv_sec -= b.tv_sec;
00772    a.tv_usec -= b.tv_usec;
00773    if (a.tv_usec < 0) {
00774       a.tv_sec-- ;
00775       a.tv_usec += ONE_MILLION;
00776    }
00777    return a;
00778 }
00779 #undef ONE_MILLION
00780 
00781 /*! \brief glibc puts a lock inside random(3), so that the results are thread-safe.
00782  * BSD libc (and others) do not. */
00783 #ifndef linux
00784 
00785 AST_MUTEX_DEFINE_STATIC(randomlock);
00786 
00787 long int ast_random(void)
00788 {
00789    long int res;
00790    ast_mutex_lock(&randomlock);
00791    res = random();
00792    ast_mutex_unlock(&randomlock);
00793    return res;
00794 }
00795 #endif
00796 
00797 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
00798 {
00799    char *dataPut = start;
00800    int inEscape = 0;
00801    int inQuotes = 0;
00802 
00803    for (; *start; start++) {
00804       if (inEscape) {
00805          *dataPut++ = *start;       /* Always goes verbatim */
00806          inEscape = 0;
00807       } else {
00808          if (*start == '\\') {
00809             inEscape = 1;      /* Do not copy \ into the data */
00810          } else if (*start == '\'') {
00811             inQuotes = 1 - inQuotes;   /* Do not copy ' into the data */
00812          } else {
00813             /* Replace , with |, unless in quotes */
00814             *dataPut++ = inQuotes ? *start : ((*start == find) ? replace_with : *start);
00815          }
00816       }
00817    }
00818    if (start != dataPut)
00819       *dataPut = 0;
00820    return dataPut;
00821 }
00822 
00823 void ast_join(char *s, size_t len, char * const w[])
00824 {
00825    int x, ofs = 0;
00826    const char *src;
00827 
00828    /* Join words into a string */
00829    if (!s)
00830       return;
00831    for (x = 0; ofs < len && w[x]; x++) {
00832       if (x > 0)
00833          s[ofs++] = ' ';
00834       for (src = w[x]; *src && ofs < len; src++)
00835          s[ofs++] = *src;
00836    }
00837    if (ofs == len)
00838       ofs--;
00839    s[ofs] = '\0';
00840 }
00841 
00842 const char __ast_string_field_empty[] = "";
00843 
00844 static int add_string_pool(struct ast_string_field_mgr *mgr, size_t size)
00845 {
00846    struct ast_string_field_pool *pool;
00847 
00848    if (!(pool = ast_calloc(1, sizeof(*pool) + size)))
00849       return -1;
00850    
00851    pool->prev = mgr->pool;
00852    mgr->pool = pool;
00853    mgr->size = size;
00854    mgr->space = size;
00855    mgr->used = 0;
00856 
00857    return 0;
00858 }
00859 
00860 int __ast_string_field_init(struct ast_string_field_mgr *mgr, size_t size,
00861              ast_string_field *fields, int num_fields)
00862 {
00863    int index;
00864 
00865    if (add_string_pool(mgr, size))
00866       return -1;
00867 
00868    for (index = 0; index < num_fields; index++)
00869       fields[index] = __ast_string_field_empty;
00870 
00871    return 0;
00872 }
00873 
00874 ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr, size_t needed,
00875                   ast_string_field *fields, int num_fields)
00876 {
00877    char *result = NULL;
00878 
00879    if (__builtin_expect(needed > mgr->space, 0)) {
00880       size_t new_size = mgr->size * 2;
00881 
00882       while (new_size < needed)
00883          new_size *= 2;
00884 
00885       if (add_string_pool(mgr, new_size))
00886          return NULL;
00887    }
00888 
00889    result = mgr->pool->base + mgr->used;
00890    mgr->used += needed;
00891    mgr->space -= needed;
00892    return result;
00893 }
00894 
00895 void __ast_string_field_index_build_va(struct ast_string_field_mgr *mgr,
00896                 ast_string_field *fields, int num_fields,
00897                 int index, const char *format, va_list ap1, va_list ap2)
00898 {
00899    size_t needed;
00900 
00901    needed = vsnprintf(mgr->pool->base + mgr->used, mgr->space, format, ap1) + 1;
00902 
00903    va_end(ap1);
00904 
00905    if (needed > mgr->space) {
00906       size_t new_size = mgr->size * 2;
00907 
00908       while (new_size < needed)
00909          new_size *= 2;
00910 
00911       if (add_string_pool(mgr, new_size))
00912          return;
00913 
00914       vsprintf(mgr->pool->base + mgr->used, format, ap2);
00915    }
00916 
00917    fields[index] = mgr->pool->base + mgr->used;
00918    mgr->used += needed;
00919    mgr->space -= needed;
00920 }
00921 
00922 void __ast_string_field_index_build(struct ast_string_field_mgr *mgr,
00923                 ast_string_field *fields, int num_fields,
00924                 int index, const char *format, ...)
00925 {
00926    va_list ap1, ap2;
00927 
00928    va_start(ap1, format);
00929    va_start(ap2, format);     /* va_copy does not exist on FreeBSD */
00930 
00931    __ast_string_field_index_build_va(mgr, fields, num_fields, index, format, ap1, ap2);
00932 
00933    va_end(ap1);
00934    va_end(ap2);
00935 }
00936 
00937 AST_MUTEX_DEFINE_STATIC(fetchadd_m); /* used for all fetc&add ops */
00938 
00939 int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
00940 {
00941         int ret;
00942         ast_mutex_lock(&fetchadd_m);
00943         ret = *p;
00944         *p += v;
00945         ast_mutex_unlock(&fetchadd_m);
00946         return ret;
00947 }
00948 
00949 /*! \brief
00950  * get values from config variables.
00951  */
00952 int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
00953 {
00954    long t;
00955    int scanned;
00956 
00957    if (dst == NULL)
00958       return -1;
00959 
00960    *dst = _default;
00961 
00962    if (ast_strlen_zero(src))
00963       return -1;
00964 
00965    /* only integer at the moment, but one day we could accept more formats */
00966    if (sscanf(src, "%ld%n", &t, &scanned) == 1) {
00967       *dst = t;
00968       if (consumed)
00969          *consumed = scanned;
00970       return 0;
00971    } else
00972       return -1;
00973 }
00974 
00975 int ast_dynamic_str_thread_build_va(struct ast_dynamic_str **buf, size_t max_len,
00976    struct ast_threadstorage *ts, int append, const char *fmt, va_list ap)
00977 {
00978    int res;
00979    int offset = (append && (*buf)->len) ? strlen((*buf)->str) : 0;
00980 #if defined(DEBUG_THREADLOCALS)
00981    struct ast_dynamic_str *old_buf = *buf;
00982 #endif /* defined(DEBUG_THREADLOCALS) */
00983 
00984    res = vsnprintf((*buf)->str + offset, (*buf)->len - offset, fmt, ap);
00985 
00986    /* Check to see if there was not enough space in the string buffer to prepare
00987     * the string.  Also, if a maximum length is present, make sure the current
00988     * length is less than the maximum before increasing the size. */
00989    if ((res + offset + 1) > (*buf)->len && (max_len ? ((*buf)->len < max_len) : 1)) {
00990       /* Set the new size of the string buffer to be the size needed
00991        * to hold the resulting string (res) plus one byte for the
00992        * terminating '\0'.  If this size is greater than the max, set
00993        * the new length to be the maximum allowed. */
00994       if (max_len)
00995          (*buf)->len = ((res + offset + 1) < max_len) ? (res + offset + 1) : max_len;
00996       else
00997          (*buf)->len = res + offset + 1;
00998 
00999       if (!(*buf = ast_realloc(*buf, (*buf)->len + sizeof(*(*buf)))))
01000          return AST_DYNSTR_BUILD_FAILED;
01001 
01002       if (append)
01003          (*buf)->str[offset] = '\0';
01004 
01005       if (ts) {
01006          pthread_setspecific(ts->key, *buf);
01007 #if defined(DEBUG_THREADLOCALS)
01008          __ast_threadstorage_object_replace(old_buf, *buf, (*buf)->len + sizeof(*(*buf)));
01009 #endif /* defined(DEBUG_THREADLOCALS) */
01010       }
01011 
01012       /* va_end() and va_start() must be done before calling
01013        * vsnprintf() again. */
01014       return AST_DYNSTR_BUILD_RETRY;
01015    }
01016 
01017    return res;
01018 }
01019 
01020 void ast_enable_packet_fragmentation(int sock)
01021 {
01022 #if defined(HAVE_IP_MTU_DISCOVER)
01023    int val = IP_PMTUDISC_DONT;
01024    
01025    if (setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)))
01026       ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n");
01027 #endif /* HAVE_IP_MTU_DISCOVER */
01028 }

Generated on Mon May 14 04:42:58 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1