Wed Oct 28 13:33:49 2009

Asterisk developer's documentation


utils.c File Reference

Utility functions. More...

#include "asterisk.h"
#include <ctype.h>
#include <sys/stat.h>
#include "asterisk/network.h"
#include "asterisk/lock.h"
#include "asterisk/io.h"
#include "asterisk/md5.h"
#include "asterisk/sha1.h"
#include "asterisk/cli.h"
#include "asterisk/linkedlists.h"
#include "asterisk/strings.h"
#include "asterisk/time.h"
#include "asterisk/stringfields.h"
#include "asterisk/utils.h"
#include "asterisk/threadstorage.h"
#include "asterisk/config.h"

Include dependency graph for utils.c:

Go to the source code of this file.

Data Structures

struct  thr_arg

Defines

#define ALLOCATOR_OVERHEAD   48
#define AST_API_MODULE
#define AST_API_MODULE
#define AST_API_MODULE
#define AST_API_MODULE
#define AST_API_MODULE
#define AST_API_MODULE
#define AST_API_MODULE
#define ERANGE   34
#define ONE_MILLION   1000000

Functions

void * __ast_calloc_with_stringfields (unsigned int num_structs, size_t struct_size, size_t field_mgr_offset, size_t field_mgr_pool_offset, size_t pool_size, const char *file, int lineno, const char *func)
ast_string_field __ast_string_field_alloc_space (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, size_t needed)
int __ast_string_field_init (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, int needed, const char *file, int lineno, const char *func)
void __ast_string_field_ptr_build (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, ast_string_field *ptr, const char *format,...)
void __ast_string_field_ptr_build_va (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, ast_string_field *ptr, const char *format, va_list ap1, va_list ap2)
int __ast_string_field_ptr_grow (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, size_t needed, const ast_string_field *ptr)
void __ast_string_field_release_active (struct ast_string_field_pool *pool_head, const ast_string_field ptr)
static void __init_inet_ntoa_buf (void)
int _ast_asprintf (char **ret, const char *file, int lineno, const char *func, const char *fmt,...)
static int add_string_pool (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, size_t size, const char *file, int lineno, const char *func)
 add a new block to the pool. We can only allocate from the topmost pool, so the fields in *mgr reflect the size of that only.
int ast_atomic_fetchadd_int_slow (volatile int *p, int v)
int ast_base64decode (unsigned char *dst, const char *src, int max)
 decode BASE64 encoded text
int ast_base64encode (char *dst, const unsigned char *src, int srclen, int max)
 Encode data in base64.
int ast_base64encode_full (char *dst, const unsigned char *src, int srclen, int max, int linebreaks)
 encode text to BASE64 coding
int ast_build_string (char **buffer, size_t *space, const char *fmt,...)
 Build a string in a buffer, designed to be called repeatedly.
int ast_build_string_va (char **buffer, size_t *space, const char *fmt, va_list ap)
 Build a string in a buffer, designed to be called repeatedly.
int ast_careful_fwrite (FILE *f, int fd, const char *src, size_t len, int timeoutms)
 Write data to a file stream with a timeout.
int ast_carefulwrite (int fd, char *s, int len, int timeoutms)
 Try to write string, but wait no more than ms milliseconds before timing out.
void ast_enable_packet_fragmentation (int sock)
 Disable PMTU discovery on a socket.
int ast_false (const char *s)
 Make sure something is false. Determine if a string containing a boolean value is "false". This function checks to see whether a string passed to it is an indication of an "false" value. It checks to see if the string is "no", "false", "n", "f", "off" or "0".
int ast_get_time_t (const char *src, time_t *dst, time_t _default, int *consumed)
 get values from config variables.
int ast_get_timeval (const char *src, struct timeval *dst, struct timeval _default, int *consumed)
 get values from config variables.
struct hostent * ast_gethostbyname (const char *host, struct ast_hostent *hp)
 Re-entrant (thread safe) version of gethostbyname that replaces the standard gethostbyname (which is not thread safe).
const char * ast_inet_ntoa (struct in_addr ia)
 ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa
void ast_join (char *s, size_t len, const char *const w[])
void ast_md5_hash (char *output, const char *input)
 Produce 32 char MD5 hash of value.
int ast_mkdir (const char *path, int mode)
 Recursively create directory path.
int ast_parse_digest (const char *digest, struct ast_http_digest *d, int request, int pedantic)
 Parse digest authorization header.
char * ast_process_quotes_and_slashes (char *start, char find, char replace_with)
 Process a string to find and replace characters.
int ast_pthread_create_detached_stack (pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize, const char *file, const char *caller, int line, const char *start_fn)
int ast_pthread_create_stack (pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize, const char *file, const char *caller, int line, const char *start_fn)
long int ast_random (void)
void ast_sha1_hash (char *output, const char *input)
 Produce 40 char SHA1 hash of value.
char * ast_strip_quoted (char *s, const char *beg_quotes, const char *end_quotes)
 Strip leading/trailing whitespace and quotes from a string.
int ast_true (const char *s)
 Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
struct timeval ast_tvadd (struct timeval a, struct timeval b)
 Returns the sum of two timevals a + b.
struct timeval ast_tvsub (struct timeval a, struct timeval b)
 Returns the difference of two timevals a - b.
char * ast_unescape_c (char *src)
 Convert some C escape sequences.
char * ast_unescape_semicolon (char *s)
 Strip backslash for "escaped" semicolons, the string to be stripped (will be modified).
void ast_uri_decode (char *s)
 ast_uri_decode: Decode SIP URI, URN, URL (overwrite the string)
char * ast_uri_encode (const char *string, char *outbuf, int buflen, int doreserved)
 ast_uri_encode: Turn text string to URI-encoded XX version
int ast_utils_init (void)
int ast_wait_for_input (int fd, int ms)
static int ast_wait_for_output (int fd, int timeoutms)
static void base64_init (void)
static void * dummy_start (void *data)
static int gethostbyname_r (const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop)
 Reentrant replacement for gethostbyname for BSD-based systems.
static size_t optimal_alloc_size (size_t size)
static struct timeval tvfix (struct timeval a)

Variables

ast_string_field __ast_string_field_empty = __ast_string_field_empty_buffer.string
struct {
   ast_string_field_allocation   allocation
   char   string [1]
__ast_string_field_empty_buffer
static ast_mutex_t __mutex = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )
static char b2a [256]
static char base64 [64]
static ast_mutex_t fetchadd_m = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )
static struct ast_threadstorage inet_ntoa_buf = { .once = PTHREAD_ONCE_INIT, .key_init = __init_inet_ntoa_buf , .custom_init = NULL , }
static ast_mutex_t randomlock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )
 glibc puts a lock inside random(3), so that the results are thread-safe. BSD libc (and others) do not.


Detailed Description

Utility functions.

Note:
These are important for portability and security, so please use them in favour of other routines. Please consult the CODING GUIDELINES for more information.

Definition in file utils.c.


Define Documentation

#define ALLOCATOR_OVERHEAD   48

Definition at line 1486 of file utils.c.

Referenced by optimal_alloc_size().

#define AST_API_MODULE

Definition at line 62 of file utils.c.

#define AST_API_MODULE

Definition at line 62 of file utils.c.

#define AST_API_MODULE

Definition at line 62 of file utils.c.

#define AST_API_MODULE

Definition at line 62 of file utils.c.

#define AST_API_MODULE

Definition at line 62 of file utils.c.

#define AST_API_MODULE

Definition at line 62 of file utils.c.

#define AST_API_MODULE

Definition at line 62 of file utils.c.

#define ERANGE   34

duh? ERANGE value copied from web...

Definition at line 72 of file utils.c.

Referenced by gethostbyname_r(), and reload_queue_members().

#define ONE_MILLION   1000000

Definition at line 1343 of file utils.c.

Referenced by ast_tvadd(), ast_tvsub(), and tvfix().


Function Documentation

void* __ast_calloc_with_stringfields ( unsigned int  num_structs,
size_t  struct_size,
size_t  field_mgr_offset,
size_t  field_mgr_pool_offset,
size_t  pool_size,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1755 of file utils.c.

References __ast_calloc(), __ast_string_field_empty, allocation, ast_calloc, ast_string_field_pool::base, ast_string_field_mgr::embedded_pool, optimal_alloc_size(), and ast_string_field_pool::size.

01758 {
01759    struct ast_string_field_mgr *mgr;
01760    struct ast_string_field_pool *pool;
01761    struct ast_string_field_pool **pool_head;
01762    size_t pool_size_needed = sizeof(*pool) + pool_size;
01763    size_t size_to_alloc = optimal_alloc_size(struct_size + pool_size_needed);
01764    void *allocation;
01765    unsigned int x;
01766 
01767 #if defined(__AST_DEBUG_MALLOC)  
01768    if (!(allocation = __ast_calloc(num_structs, size_to_alloc, file, lineno, func))) {
01769       return NULL;
01770    }
01771 #else
01772    if (!(allocation = ast_calloc(num_structs, size_to_alloc))) {
01773       return NULL;
01774    }
01775 #endif
01776 
01777    for (x = 0; x < num_structs; x++) {
01778       void *base = allocation + (size_to_alloc * x);
01779       const char **p;
01780 
01781       mgr = base + field_mgr_offset;
01782       pool_head = base + field_mgr_pool_offset;
01783       pool = base + struct_size;
01784 
01785       p = (const char **) pool_head + 1;
01786       while ((struct ast_string_field_mgr *) p != mgr) {
01787          *p++ = __ast_string_field_empty;
01788       }
01789 
01790       mgr->embedded_pool = pool;
01791       *pool_head = pool;
01792       pool->size = size_to_alloc - struct_size - sizeof(*pool);
01793    }
01794 
01795    return allocation;
01796 }

ast_string_field __ast_string_field_alloc_space ( struct ast_string_field_mgr mgr,
struct ast_string_field_pool **  pool_head,
size_t  needed 
)

Definition at line 1605 of file utils.c.

References add_string_pool(), AST_STRING_FIELD_ALLOCATION, and ast_string_field_mgr::last_alloc.

Referenced by __ast_string_field_ptr_build_va().

01607 {
01608    char *result = NULL;
01609    size_t space = (*pool_head)->size - (*pool_head)->used;
01610    size_t to_alloc = needed + sizeof(ast_string_field_allocation);
01611 
01612    if (__builtin_expect(to_alloc > space, 0)) {
01613       size_t new_size = (*pool_head)->size;
01614 
01615       while (new_size < to_alloc) {
01616          new_size *= 2;
01617       }
01618 
01619 #if defined(__AST_DEBUG_MALLOC)
01620       if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func))
01621          return NULL;
01622 #else
01623       if (add_string_pool(mgr, pool_head, new_size, __FILE__, __LINE__, __FUNCTION__))
01624          return NULL;
01625 #endif
01626    }
01627 
01628    result = (*pool_head)->base + (*pool_head)->used;
01629    (*pool_head)->used += to_alloc;
01630    (*pool_head)->active += needed;
01631    result += sizeof(ast_string_field_allocation);
01632    AST_STRING_FIELD_ALLOCATION(result) = needed;
01633    mgr->last_alloc = result;
01634 
01635    return result;
01636 }

int __ast_string_field_init ( struct ast_string_field_mgr mgr,
struct ast_string_field_pool **  pool_head,
int  needed,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1539 of file utils.c.

References __ast_string_field_empty, ast_string_field_pool::active, add_string_pool(), ast_free, ast_log(), ast_string_field_mgr::embedded_pool, ast_string_field_mgr::last_alloc, LOG_WARNING, ast_string_field_pool::prev, and ast_string_field_pool::used.

01541 {
01542    const char **p = (const char **) pool_head + 1;
01543    struct ast_string_field_pool *cur = NULL;
01544    struct ast_string_field_pool *preserve = NULL;
01545 
01546    /* clear fields - this is always necessary */
01547    while ((struct ast_string_field_mgr *) p != mgr) {
01548       *p++ = __ast_string_field_empty;
01549    }
01550 
01551    mgr->last_alloc = NULL;
01552 #if defined(__AST_DEBUG_MALLOC)
01553    mgr->owner_file = file;
01554    mgr->owner_func = func;
01555    mgr->owner_line = lineno;
01556 #endif
01557    if (needed > 0) {    /* allocate the initial pool */
01558       *pool_head = NULL;
01559       return add_string_pool(mgr, pool_head, needed, file, lineno, func);
01560    }
01561 
01562    /* if there is an embedded pool, we can't actually release *all*
01563     * pools, we must keep the embedded one. if the caller is about
01564     * to free the structure that contains the stringfield manager
01565     * and embedded pool anyway, it will be freed as part of that
01566     * operation.
01567     */
01568    if ((needed < 0) && mgr->embedded_pool) {
01569       needed = 0;
01570    }
01571 
01572    if (needed < 0) {    /* reset all pools */
01573       cur = *pool_head;
01574    } else if (mgr->embedded_pool) { /* preserve the embedded pool */
01575       preserve = mgr->embedded_pool;
01576       cur = *pool_head;
01577    } else {       /* preserve the last pool */
01578       if (*pool_head == NULL) {
01579          ast_log(LOG_WARNING, "trying to reset empty pool\n");
01580          return -1;
01581       }
01582       preserve = *pool_head;
01583       cur = preserve->prev;
01584    }
01585 
01586    if (preserve) {
01587       preserve->prev = NULL;
01588       preserve->used = preserve->active = 0;
01589    }
01590 
01591    while (cur) {
01592       struct ast_string_field_pool *prev = cur->prev;
01593 
01594       if (cur != preserve) {
01595          ast_free(cur);
01596       }
01597       cur = prev;
01598    }
01599 
01600    *pool_head = preserve;
01601 
01602    return 0;
01603 }

void __ast_string_field_ptr_build ( struct ast_string_field_mgr mgr,
struct ast_string_field_pool **  pool_head,
ast_string_field ptr,
const char *  format,
  ... 
)

Definition at line 1740 of file utils.c.

References __ast_string_field_ptr_build_va().

01743 {
01744    va_list ap1, ap2;
01745 
01746    va_start(ap1, format);
01747    va_start(ap2, format);     /* va_copy does not exist on FreeBSD */
01748 
01749    __ast_string_field_ptr_build_va(mgr, pool_head, ptr, format, ap1, ap2);
01750 
01751    va_end(ap1);
01752    va_end(ap2);
01753 }

void __ast_string_field_ptr_build_va ( struct ast_string_field_mgr mgr,
struct ast_string_field_pool **  pool_head,
ast_string_field ptr,
const char *  format,
va_list  ap1,
va_list  ap2 
)

Definition at line 1681 of file utils.c.

References __ast_string_field_alloc_space(), __ast_string_field_empty, __ast_string_field_release_active(), AST_STRING_FIELD_ALLOCATION, available(), and ast_string_field_mgr::last_alloc.

Referenced by __ast_string_field_ptr_build().

01684 {
01685    size_t needed;
01686    size_t available;
01687    size_t space = (*pool_head)->size - (*pool_head)->used;
01688    ssize_t grow;
01689    char *target;
01690 
01691    /* if the field already has space allocated, try to reuse it;
01692       otherwise, try to use the empty space at the end of the current
01693       pool
01694    */
01695    if (*ptr != __ast_string_field_empty) {
01696       target = (char *) *ptr;
01697       available = AST_STRING_FIELD_ALLOCATION(*ptr);
01698       if (*ptr == mgr->last_alloc) {
01699          available += space;
01700       }
01701    } else {
01702       target = (*pool_head)->base + (*pool_head)->used + sizeof(ast_string_field_allocation);
01703       available = space - sizeof(ast_string_field_allocation);
01704    }
01705 
01706    needed = vsnprintf(target, available, format, ap1) + 1;
01707 
01708    va_end(ap1);
01709 
01710    if (needed > available) {
01711       /* the allocation could not be satisfied using the field's current allocation
01712          (if it has one), or the space available in the pool (if it does not). allocate
01713          space for it, adding a new string pool if necessary.
01714       */
01715       if (!(target = (char *) __ast_string_field_alloc_space(mgr, pool_head, needed))) {
01716          return;
01717       }
01718       vsprintf(target, format, ap2);
01719       __ast_string_field_release_active(*pool_head, *ptr);
01720       *ptr = target;
01721    } else if (*ptr != target) {
01722       /* the allocation was satisfied using available space in the pool, but not
01723          using the space already allocated to the field
01724       */
01725       __ast_string_field_release_active(*pool_head, *ptr);
01726       mgr->last_alloc = *ptr = target;
01727       AST_STRING_FIELD_ALLOCATION(target) = needed;
01728       (*pool_head)->used += needed + sizeof(ast_string_field_allocation);
01729       (*pool_head)->active += needed;
01730    } else if ((grow = (needed - AST_STRING_FIELD_ALLOCATION(*ptr))) > 0) {
01731       /* the allocation was satisfied by using available space in the pool *and*
01732          the field was the last allocated field from the pool, so it grew
01733       */
01734       (*pool_head)->used += grow;
01735       (*pool_head)->active += grow;
01736       AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
01737    }
01738 }

int __ast_string_field_ptr_grow ( struct ast_string_field_mgr mgr,
struct ast_string_field_pool **  pool_head,
size_t  needed,
const ast_string_field ptr 
)

Definition at line 1638 of file utils.c.

References AST_STRING_FIELD_ALLOCATION, and ast_string_field_mgr::last_alloc.

01641 {
01642    ssize_t grow = needed - AST_STRING_FIELD_ALLOCATION(*ptr);
01643    size_t space = (*pool_head)->size - (*pool_head)->used;
01644 
01645    if (*ptr != mgr->last_alloc) {
01646       return 1;
01647    }
01648 
01649    if (space < grow) {
01650       return 1;
01651    }
01652 
01653    (*pool_head)->used += grow;
01654    (*pool_head)->active += grow;
01655    AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
01656 
01657    return 0;
01658 }

void __ast_string_field_release_active ( struct ast_string_field_pool pool_head,
const ast_string_field  ptr 
)

Definition at line 1660 of file utils.c.

References __ast_string_field_empty, ast_string_field_pool::active, ast_free, AST_STRING_FIELD_ALLOCATION, and ast_string_field_pool::prev.

Referenced by __ast_string_field_ptr_build_va().

01662 {
01663    struct ast_string_field_pool *pool, *prev;
01664 
01665    if (ptr == __ast_string_field_empty) {
01666       return;
01667    }
01668 
01669    for (pool = pool_head, prev = NULL; pool; prev = pool, pool = pool->prev) {
01670       if ((ptr >= pool->base) && (ptr <= (pool->base + pool->size))) {
01671          pool->active -= AST_STRING_FIELD_ALLOCATION(ptr);
01672          if ((pool->active == 0) && prev) {
01673             prev->prev = pool->prev;
01674             ast_free(pool);
01675          }
01676          break;
01677       }
01678    }
01679 }

static void __init_inet_ntoa_buf ( void   )  [static]

Definition at line 68 of file utils.c.

00085 {

int _ast_asprintf ( char **  ret,
const char *  file,
int  lineno,
const char *  func,
const char *  fmt,
  ... 
)

Definition at line 2045 of file utils.c.

References MALLOC_FAILURE_MSG, and vasprintf.

02046 {
02047    int res;
02048    va_list ap;
02049 
02050    va_start(ap, fmt);
02051    if ((res = vasprintf(ret, fmt, ap)) == -1) {
02052       MALLOC_FAILURE_MSG;
02053    }
02054    va_end(ap);
02055 
02056    return res;
02057 }

static int add_string_pool ( struct ast_string_field_mgr mgr,
struct ast_string_field_pool **  pool_head,
size_t  size,
const char *  file,
int  lineno,
const char *  func 
) [static]

add a new block to the pool. We can only allocate from the topmost pool, so the fields in *mgr reflect the size of that only.

Definition at line 1503 of file utils.c.

References __ast_calloc(), ast_calloc, ast_string_field_mgr::last_alloc, optimal_alloc_size(), ast_string_field_pool::prev, and ast_string_field_pool::size.

Referenced by __ast_string_field_alloc_space(), and __ast_string_field_init().

01505 {
01506    struct ast_string_field_pool *pool;
01507    size_t alloc_size = optimal_alloc_size(sizeof(*pool) + size);
01508 
01509 #if defined(__AST_DEBUG_MALLOC)
01510    if (!(pool = __ast_calloc(1, alloc_size, file, lineno, func))) {
01511       return -1;
01512    }
01513 #else
01514    if (!(pool = ast_calloc(1, alloc_size))) {
01515       return -1;
01516    }
01517 #endif
01518 
01519    pool->prev = *pool_head;
01520    pool->size = alloc_size - sizeof(*pool);
01521    *pool_head = pool;
01522    mgr->last_alloc = NULL;
01523 
01524    return 0;
01525 }

int ast_atomic_fetchadd_int_slow ( volatile int *  p,
int  v 
)

Definition at line 1802 of file utils.c.

References ast_mutex_lock(), ast_mutex_unlock(), and fetchadd_m.

01803 {
01804    int ret;
01805    ast_mutex_lock(&fetchadd_m);
01806    ret = *p;
01807    *p += v;
01808    ast_mutex_unlock(&fetchadd_m);
01809    return ret;
01810 }

int ast_base64decode ( unsigned char *  dst,
const char *  src,
int  max 
)

decode BASE64 encoded text

Decode data from base64.

Definition at line 264 of file utils.c.

Referenced by __ast_check_signature(), aes_helper(), base64_helper(), and osp_validate_token().

00265 {
00266    int cnt = 0;
00267    unsigned int byte = 0;
00268    unsigned int bits = 0;
00269    int incnt = 0;
00270    while (*src && (cnt < max)) {
00271       /* Shift in 6 bits of input */
00272       byte <<= 6;
00273       byte |= (b2a[(int)(*src)]) & 0x3f;
00274       bits += 6;
00275       src++;
00276       incnt++;
00277       /* If we have at least 8 bits left over, take that character 
00278          off the top */
00279       if (bits >= 8)  {
00280          bits -= 8;
00281          *dst = (byte >> bits) & 0xff;
00282          dst++;
00283          cnt++;
00284       }
00285    }
00286    /* Dont worry about left over bits, they're extra anyway */
00287    return cnt;
00288 }

int ast_base64encode ( char *  dst,
const unsigned char *  src,
int  srclen,
int  max 
)

Encode data in base64.

Parameters:
dst the destination buffer
src the source data to be encoded
srclen the number of bytes present in the source buffer
max the maximum number of bytes to write into the destination buffer, *including* the terminating NULL character.

Definition at line 342 of file utils.c.

References ast_base64encode_full().

Referenced by __ast_sign(), aes_helper(), aji_start_sasl(), base64_helper(), build_secret(), and osp_check_destination().

00343 {
00344    return ast_base64encode_full(dst, src, srclen, max, 0);
00345 }

int ast_base64encode_full ( char *  dst,
const unsigned char *  src,
int  srclen,
int  max,
int  linebreaks 
)

encode text to BASE64 coding

Definition at line 291 of file utils.c.

Referenced by ast_base64encode().

00292 {
00293    int cnt = 0;
00294    int col = 0;
00295    unsigned int byte = 0;
00296    int bits = 0;
00297    int cntin = 0;
00298    /* Reserve space for null byte at end of string */
00299    max--;
00300    while ((cntin < srclen) && (cnt < max)) {
00301       byte <<= 8;
00302       byte |= *(src++);
00303       bits += 8;
00304       cntin++;
00305       if ((bits == 24) && (cnt + 4 <= max)) {
00306          *dst++ = base64[(byte >> 18) & 0x3f];
00307          *dst++ = base64[(byte >> 12) & 0x3f];
00308          *dst++ = base64[(byte >> 6) & 0x3f];
00309          *dst++ = base64[byte & 0x3f];
00310          cnt += 4;
00311          col += 4;
00312          bits = 0;
00313          byte = 0;
00314       }
00315       if (linebreaks && (cnt < max) && (col == 64)) {
00316          *dst++ = '\n';
00317          cnt++;
00318          col = 0;
00319       }
00320    }
00321    if (bits && (cnt + 4 <= max)) {
00322       /* Add one last character for the remaining bits, 
00323          padding the rest with 0 */
00324       byte <<= 24 - bits;
00325       *dst++ = base64[(byte >> 18) & 0x3f];
00326       *dst++ = base64[(byte >> 12) & 0x3f];
00327       if (bits == 16)
00328          *dst++ = base64[(byte >> 6) & 0x3f];
00329       else
00330          *dst++ = '=';
00331       *dst++ = '=';
00332       cnt += 4;
00333    }
00334    if (linebreaks && (cnt < max)) {
00335       *dst++ = '\n';
00336       cnt++;
00337    }
00338    *dst = '\0';
00339    return cnt;
00340 }

int ast_build_string ( char **  buffer,
size_t *  space,
const char *  fmt,
  ... 
)

Build a string in a buffer, designed to be called repeatedly.

Note:
This method is not recommended. New code should use ast_str_*() instead.
This is a wrapper for snprintf, that properly handles the buffer pointer and buffer space available.

Parameters:
buffer current position in buffer to place string into (will be updated on return)
space remaining space in buffer (will be updated on return)
fmt printf-style format string
Return values:
0 on success
non-zero on failure.

Definition at line 1297 of file utils.c.

References ast_build_string_va().

Referenced by config_odbc(), handle_speechrecognize(), lua_func_read(), lua_pbx_exec(), pp_each_extension_helper(), and pp_each_user_helper().

01298 {
01299    va_list ap;
01300    int result;
01301 
01302    va_start(ap, fmt);
01303    result = ast_build_string_va(buffer, space, fmt, ap);
01304    va_end(ap);
01305 
01306    return result;
01307 }

int ast_build_string_va ( char **  buffer,
size_t *  space,
const char *  fmt,
va_list  ap 
)

Build a string in a buffer, designed to be called repeatedly.

This is a wrapper for snprintf, that properly handles the buffer pointer and buffer space available.

Returns:
0 on success, non-zero on failure.
Parameters:
buffer current position in buffer to place string into (will be updated on return)
space remaining space in buffer (will be updated on return)
fmt printf-style format string
ap varargs list of arguments for format

Definition at line 1278 of file utils.c.

Referenced by ast_build_string().

01279 {
01280    int result;
01281 
01282    if (!buffer || !*buffer || !space || !*space)
01283       return -1;
01284 
01285    result = vsnprintf(*buffer, *space, fmt, ap);
01286 
01287    if (result < 0)
01288       return -1;
01289    else if (result > *space)
01290       result = *space;
01291 
01292    *buffer += result;
01293    *space -= result;
01294    return 0;
01295 }

int ast_careful_fwrite ( FILE *  f,
int  fd,
const char *  s,
size_t  len,
int  timeoutms 
)

Write data to a file stream with a timeout.

Parameters:
f the file stream to write to
fd the file description to poll on to know when the file stream can be written to without blocking.
s the buffer to write from
len the number of bytes to write
timeoutms The maximum amount of time to block in this function trying to write, specified in milliseconds.
Note:
This function assumes that the associated file stream has been set up as non-blocking.
Return values:
0 success
-1 error

Definition at line 1152 of file utils.c.

References ast_log(), ast_tvdiff_ms(), ast_tvnow(), ast_wait_for_output(), errno, and LOG_ERROR.

Referenced by send_string().

01153 {
01154    struct timeval start = ast_tvnow();
01155    int n = 0;
01156    int elapsed = 0;
01157 
01158    while (len) {
01159       if (ast_wait_for_output(fd, timeoutms - elapsed)) {
01160          /* poll returned a fatal error, so bail out immediately. */
01161          return -1;
01162       }
01163 
01164       /* Clear any errors from a previous write */
01165       clearerr(f);
01166 
01167       n = fwrite(src, 1, len, f);
01168 
01169       if (ferror(f) && errno != EINTR && errno != EAGAIN) {
01170          /* fatal error from fwrite() */
01171          if (!feof(f)) {
01172             /* Don't spam the logs if it was just that the connection is closed. */
01173             ast_log(LOG_ERROR, "fwrite() returned error: %s\n", strerror(errno));
01174          }
01175          n = -1;
01176          break;
01177       }
01178 
01179       /* Update for data already written to the socket */
01180       len -= n;
01181       src += n;
01182 
01183       elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01184       if (elapsed >= timeoutms) {
01185          /* We've taken too long to write 
01186           * This is only an error condition if we haven't finished writing. */
01187          n = len ? -1 : 0;
01188          break;
01189       }
01190    }
01191 
01192    while (fflush(f)) {
01193       if (errno == EAGAIN || errno == EINTR) {
01194          continue;
01195       }
01196       if (!feof(f)) {
01197          /* Don't spam the logs if it was just that the connection is closed. */
01198          ast_log(LOG_ERROR, "fflush() returned error: %s\n", strerror(errno));
01199       }
01200       n = -1;
01201       break;
01202    }
01203 
01204    return n < 0 ? -1 : 0;
01205 }

int ast_carefulwrite ( int  fd,
char *  s,
int  len,
int  timeoutms 
)

Try to write string, but wait no more than ms milliseconds before timing out.

Try to write string, but wait no more than ms milliseconds before timing out.

Note:
The code assumes that the file descriptor has NONBLOCK set, so there is only one system call made to do a write, unless we actually have a need to wait. This way, we get better performance. If the descriptor is blocking, all assumptions on the guaranteed detail do not apply anymore.

Definition at line 1111 of file utils.c.

References ast_log(), ast_tvdiff_ms(), ast_tvnow(), ast_wait_for_output(), errno, and LOG_ERROR.

Referenced by ast_agi_send(), and ast_cli().

01112 {
01113    struct timeval start = ast_tvnow();
01114    int res = 0;
01115    int elapsed = 0;
01116 
01117    while (len) {
01118       if (ast_wait_for_output(fd, timeoutms - elapsed)) {
01119          return -1;
01120       }
01121 
01122       res = write(fd, s, len);
01123 
01124       if (res < 0 && errno != EAGAIN && errno != EINTR) {
01125          /* fatal error from write() */
01126          ast_log(LOG_ERROR, "write() returned error: %s\n", strerror(errno));
01127          return -1;
01128       }
01129 
01130       if (res < 0) {
01131          /* It was an acceptable error */
01132          res = 0;
01133       }
01134 
01135       /* Update how much data we have left to write */
01136       len -= res;
01137       s += res;
01138       res = 0;
01139 
01140       elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01141       if (elapsed >= timeoutms) {
01142          /* We've taken too long to write 
01143           * This is only an error condition if we haven't finished writing. */
01144          res = len ? -1 : 0;
01145          break;
01146       }
01147    }
01148 
01149    return res;
01150 }

void ast_enable_packet_fragmentation ( int  sock  ) 

Disable PMTU discovery on a socket.

Parameters:
sock The socket to manipulate
Returns:
Nothing
On Linux, UDP sockets default to sending packets with the Dont Fragment (DF) bit set. This is supposedly done to allow the application to do PMTU discovery, but Asterisk does not do this.

Because of this, UDP packets sent by Asterisk that are larger than the MTU of any hop in the path will be lost. This function can be called on a socket to ensure that the DF bit will not be set.

Definition at line 1865 of file utils.c.

References ast_log(), and LOG_WARNING.

Referenced by ast_netsock_bindaddr(), and reload_config().

01866 {
01867 #if defined(HAVE_IP_MTU_DISCOVER)
01868    int val = IP_PMTUDISC_DONT;
01869    
01870    if (setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)))
01871       ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n");
01872 #endif /* HAVE_IP_MTU_DISCOVER */
01873 }

int ast_false ( const char *  val  ) 

Make sure something is false. Determine if a string containing a boolean value is "false". This function checks to see whether a string passed to it is an indication of an "false" value. It checks to see if the string is "no", "false", "n", "f", "off" or "0".

Return values:
0 if val is a NULL pointer.
-1 if "true".
0 otherwise.

Definition at line 1326 of file utils.c.

References ast_strlen_zero().

Referenced by __ast_udptl_reload(), acf_transaction_write(), aji_create_client(), aji_load_config(), build_peer(), build_user(), dahdi_set_dnd(), find_realtime(), func_channel_write(), handle_common_options(), init_acf_query(), load_config(), load_odbc_config(), parse_empty_options(), reload_config(), rtp_reload(), run_agi(), set_config(), set_insecure_flags(), sla_build_trunk(), and strings_to_mask().

01327 {
01328    if (ast_strlen_zero(s))
01329       return 0;
01330 
01331    /* Determine if this is a false value */
01332    if (!strcasecmp(s, "no") ||
01333        !strcasecmp(s, "false") ||
01334        !strcasecmp(s, "n") ||
01335        !strcasecmp(s, "f") ||
01336        !strcasecmp(s, "0") ||
01337        !strcasecmp(s, "off"))
01338       return -1;
01339 
01340    return 0;
01341 }

int ast_get_time_t ( const char *  src,
time_t *  dst,
time_t  _default,
int *  consumed 
)

get values from config variables.

Definition at line 1842 of file utils.c.

References ast_strlen_zero().

Referenced by build_peer(), cache_lookup_internal(), handle_saydatetime(), load_password(), play_message_datetime(), process_clearcache(), realtime_peer(), and sayunixtime_exec().

01843 {
01844    long t;
01845    int scanned;
01846 
01847    if (dst == NULL)
01848       return -1;
01849 
01850    *dst = _default;
01851 
01852    if (ast_strlen_zero(src))
01853       return -1;
01854 
01855    /* only integer at the moment, but one day we could accept more formats */
01856    if (sscanf(src, "%30ld%n", &t, &scanned) == 1) {
01857       *dst = t;
01858       if (consumed)
01859          *consumed = scanned;
01860       return 0;
01861    } else
01862       return -1;
01863 }

int ast_get_timeval ( const char *  src,
struct timeval *  dst,
struct timeval  _default,
int *  consumed 
)

get values from config variables.

Definition at line 1815 of file utils.c.

References ast_strlen_zero().

Referenced by acf_strftime().

01816 {
01817    long double dtv = 0.0;
01818    int scanned;
01819 
01820    if (dst == NULL)
01821       return -1;
01822 
01823    *dst = _default;
01824 
01825    if (ast_strlen_zero(src))
01826       return -1;
01827 
01828    /* only integer at the moment, but one day we could accept more formats */
01829    if (sscanf(src, "%30Lf%n", &dtv, &scanned) > 0) {
01830       dst->tv_sec = dtv;
01831       dst->tv_usec = (dtv - dst->tv_sec) * 1000000.0;
01832       if (consumed)
01833          *consumed = scanned;
01834       return 0;
01835    } else
01836       return -1;
01837 }

struct hostent* ast_gethostbyname ( const char *  host,
struct ast_hostent hp 
) [read]

Re-entrant (thread safe) version of gethostbyname that replaces the standard gethostbyname (which is not thread safe).

Thread-safe gethostbyname function to use in Asterisk.

Definition at line 182 of file utils.c.

References ast_hostent::buf, gethostbyname_r(), ast_hostent::hp, and s.

Referenced by __ast_http_load(), __set_address_from_contact(), app_exec(), ast_find_ourip(), ast_get_ip_or_srv(), ast_parse_arg(), build_peer(), config_load(), config_parse_variables(), create_addr(), festival_exec(), get_ip_and_port_from_sdp(), gtalk_load_config(), gtalk_update_stun(), handle_cli_udptl_set_debug(), iax_template_parse(), jingle_load_config(), jingle_update_stun(), launch_netscript(), parse_register_contact(), process_sdp(), realtime_peer(), realtime_user(), reload_config(), rpt_exec(), rtcp_do_debug_ip(), rtp_do_debug_ip(), set_config(), set_destination(), and sip_do_debug_ip().

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 HAVE_GETHOSTBYNAME_R_5
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 }

const char* ast_inet_ntoa ( struct in_addr  ia  ) 

ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa

thread-safe replacement for inet_ntoa().

Definition at line 431 of file utils.c.

References ast_threadstorage_get(), buf, and inet_ntoa_buf.

Referenced by __attempt_transmit(), __find_callno(), __iax2_show_peers(), __sip_xmit(), _sip_show_peer(), _sip_show_peers(), _skinny_show_device(), _skinny_show_devices(), acf_channel_read(), action_login(), add_diversion_header(), add_ipv4_ie(), add_rpid(), add_sdp(), ast_append_ha(), ast_apply_ha(), ast_netsock_bindaddr(), ast_parse_arg(), ast_rtcp_read(), ast_rtcp_write_rr(), ast_rtcp_write_sr(), ast_rtp_dtmf_begin(), ast_rtp_dtmf_continuation(), ast_rtp_dtmf_end(), ast_rtp_raw_write(), ast_rtp_read(), ast_sip_ouraddrfor(), ast_tcptls_client_create(), ast_tcptls_client_start(), ast_tcptls_server_start(), ast_udptl_bridge(), ast_udptl_read(), ast_udptl_write(), auth_http_callback(), authenticate(), bridge_p2p_rtp_write(), build_callid_pvt(), build_callid_registry(), build_contact(), build_peer(), build_reply_digest(), build_via(), calltoken_required(), check_access(), check_peer_ok(), check_via(), config_load(), copy_via_headers(), create_addr_from_peer(), create_client(), dnsmgr_refresh(), dump_addr(), dump_ipaddr(), dundi_rexmit(), dundi_show_peer(), dundi_show_peers(), dundi_show_trans(), dundi_showframe(), dundi_xmit(), external_rtp_create(), find_command(), find_peer(), find_subchannel_and_lock(), find_tpeer(), function_iaxpeer(), function_sipchaninfo_read(), function_sippeer(), generic_http_callback(), get_input(), gtalk_create_candidates(), gtalk_update_stun(), handle_call_token(), handle_cli_iax2_set_debug(), handle_cli_iax2_show_callno_limits(), handle_cli_iax2_show_channels(), handle_cli_iax2_show_peer(), handle_cli_iax2_show_registry(), handle_cli_udptl_set_debug(), handle_command_response(), handle_error(), handle_incoming(), handle_mgcp_show_endpoints(), handle_open_receive_channel_ack_message(), handle_request(), handle_request_bye(), handle_request_do(), handle_request_register(), handle_request_subscribe(), handle_response(), handle_response_notify(), handle_response_refer(), handle_show_http(), handle_showmanconn(), handle_skinny_show_settings(), httpstatus_callback(), iax2_ack_registry(), iax2_prov_app(), iax2_trunk_queue(), iax_server(), iax_showframe(), initreqprep(), jingle_create_candidates(), load_module(), manager_iax2_show_peer_list(), manager_iax2_show_registry(), mgcpsock_read(), multicast_rtp_write(), oh323_addrcmp_str(), oh323_call(), oh323_set_rtp_peer(), parse_register_contact(), parsing(), peercnt_add(), peercnt_modify(), peercnt_remove(), phoneprov_callback(), process_cn_rfc3389(), process_dtmf_rfc2833(), process_request(), process_sdp(), purge_sessions(), raw_hangup(), realtime_peer(), realtime_update_peer(), realtime_user(), reg_source_db(), register_verify(), registry_rerequest(), reload_config(), remote_bridge_loop(), resend_response(), retrans_pkt(), rpt_exec(), rtcp_do_debug_ip(), rtp_do_debug_ip(), sched_delay_remove(), send_dtmf(), send_packet(), send_raw_client(), send_request(), send_response(), send_trunk(), session_do(), set_config(), set_destination(), set_peercnt_limit(), setup_incoming_call(), show_channels_cb(), show_chanstats_cb(), show_main_page(), sip_do_debug_ip(), sip_do_debug_peer(), sip_poke_peer(), sip_set_rtp_peer(), sip_set_udptl_peer(), sip_show_channel(), sip_show_settings(), sip_show_tcp(), skinny_session(), skinny_set_rtp_peer(), socket_process(), socket_process_meta(), start_rtp(), timing_read(), transmit_notify_with_mwi(), unistim_info(), unistimsock_read(), and update_registry().

00432 {
00433    char *buf;
00434 
00435    if (!(buf = ast_threadstorage_get(&inet_ntoa_buf, INET_ADDRSTRLEN)))
00436       return "";
00437 
00438    return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
00439 }

void ast_join ( char *  s,
size_t  len,
const char *const   w[] 
)

Definition at line 1449 of file utils.c.

Referenced by __ast_cli_generator(), ast_agi_register(), ast_agi_unregister(), ast_cli_command_full(), cli_console_sendtext(), console_sendtext(), find_best(), handle_cli_agi_show(), handle_cli_check_permissions(), handle_help(), help1(), help_workhorse(), set_full_cmd(), and write_htmldump().

01450 {
01451    int x, ofs = 0;
01452    const char *src;
01453 
01454    /* Join words into a string */
01455    if (!s)
01456       return;
01457    for (x = 0; ofs < len && w[x]; x++) {
01458       if (x > 0)
01459          s[ofs++] = ' ';
01460       for (src = w[x]; *src && ofs < len; src++)
01461          s[ofs++] = *src;
01462    }
01463    if (ofs == len)
01464       ofs--;
01465    s[ofs] = '\0';
01466 }

void ast_md5_hash ( char *  output,
const char *  input 
)

Produce 32 char MD5 hash of value.

Produces MD5 hash based on input string.

Definition at line 230 of file utils.c.

References MD5Final(), MD5Init(), and MD5Update().

Referenced by __init_manager(), auth_exec(), auth_http_callback(), build_reply_digest(), check_auth(), and md5().

00231 {
00232    struct MD5Context md5;
00233    unsigned char digest[16];
00234    char *ptr;
00235    int x;
00236 
00237    MD5Init(&md5);
00238    MD5Update(&md5, (const unsigned char *) input, strlen(input));
00239    MD5Final(digest, &md5);
00240    ptr = output;
00241    for (x = 0; x < 16; x++)
00242       ptr += sprintf(ptr, "%2.2x", digest[x]);
00243 }

int ast_mkdir ( const char *  path,
int  mode 
)

Recursively create directory path.

Parameters:
path The directory path to create
mode The permissions with which to try to create the directory
Returns:
0 on success or an error code otherwise
Creates a directory path, creating parent directories as needed.

Definition at line 1875 of file utils.c.

References ast_strdupa, errno, and len().

Referenced by ast_monitor_change_fname(), ast_monitor_start(), conf_run(), create_dirpath(), dictate_exec(), init_logger(), load_module(), mixmonitor_exec(), record_exec(), reload_logger(), remove_from_queue(), setup_privacy_args(), sms_nextoutgoing(), sms_writefile(), testclient_exec(), testserver_exec(), and write_history().

01876 {
01877    char *ptr;
01878    int len = strlen(path), count = 0, x, piececount = 0;
01879    char *tmp = ast_strdupa(path);
01880    char **pieces;
01881    char *fullpath = alloca(len + 1);
01882    int res = 0;
01883 
01884    for (ptr = tmp; *ptr; ptr++) {
01885       if (*ptr == '/')
01886          count++;
01887    }
01888 
01889    /* Count the components to the directory path */
01890    pieces = alloca(count * sizeof(*pieces));
01891    for (ptr = tmp; *ptr; ptr++) {
01892       if (*ptr == '/') {
01893          *ptr = '\0';
01894          pieces[piececount++] = ptr + 1;
01895       }
01896    }
01897 
01898    *fullpath = '\0';
01899    for (x = 0; x < piececount; x++) {
01900       /* This looks funky, but the buffer is always ideally-sized, so it's fine. */
01901       strcat(fullpath, "/");
01902       strcat(fullpath, pieces[x]);
01903       res = mkdir(fullpath, mode);
01904       if (res && errno != EEXIST)
01905          return errno;
01906    }
01907    return 0;
01908 }

int ast_parse_digest ( const char *  digest,
struct ast_http_digest d,
int  request,
int  pedantic 
)

Parse digest authorization header.

Returns:
Returns -1 if we have no auth or something wrong with digest.
Note:
This function may be used for Digest request and responce header. request arg is set to nonzero, if we parse Digest Request. pedantic arg can be set to nonzero if we need to do addition Digest check.

Definition at line 1932 of file utils.c.

References ast_free, ast_log(), ast_skip_blanks(), ast_str_buffer(), ast_str_create(), ast_str_set(), ast_string_field_set, ast_strlen_zero(), ast_http_digest::cnonce, LOG_WARNING, ast_http_digest::nc, ast_http_digest::nonce, ast_http_digest::qop, ast_http_digest::realm, ast_http_digest::response, ast_http_digest::uri, and ast_http_digest::username.

Referenced by auth_http_callback().

01932                                                                                                {
01933    int i;
01934    char *c, key[512], val[512], tmp[512];
01935    struct ast_str *str = ast_str_create(16);
01936 
01937    if (ast_strlen_zero(digest) || !d || !str) {
01938       ast_free(str);
01939       return -1;
01940    }
01941 
01942    ast_str_set(&str, 0, "%s", digest);
01943 
01944    c = ast_skip_blanks(ast_str_buffer(str));
01945 
01946    if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
01947       ast_log(LOG_WARNING, "Missing Digest.\n");
01948       ast_free(str);
01949       return -1;
01950    }
01951    c += strlen("Digest ");
01952 
01953    /* lookup for keys/value pair */
01954    while (*c && *(c = ast_skip_blanks(c))) {
01955       /* find key */
01956       i = 0;
01957       while (*c && *c != '=' && *c != ',' && !isspace(*c)) {
01958          key[i++] = *c++;
01959       }
01960       key[i] = '\0';
01961       c = ast_skip_blanks(c);
01962       if (*c == '=') {
01963          c = ast_skip_blanks(++c);
01964          i = 0;
01965          if (*c == '\"') {
01966             /* in quotes. Skip first and look for last */
01967             c++;
01968             while (*c && *c != '\"') {
01969                if (*c == '\\' && c[1] != '\0') { /* unescape chars */
01970                   c++;
01971                }
01972                val[i++] = *c++;
01973             }
01974          } else {
01975             /* token */
01976             while (*c && *c != ',' && !isspace(*c)) {
01977                val[i++] = *c++;
01978             }
01979          }
01980          val[i] = '\0';
01981       }
01982 
01983       while (*c && *c != ',') {
01984          c++;
01985       }
01986       if (*c) {
01987          c++;
01988       }
01989 
01990       if (!strcasecmp(key, "username")) {
01991          ast_string_field_set(d, username, val);
01992       } else if (!strcasecmp(key, "realm")) {
01993          ast_string_field_set(d, realm, val);
01994       } else if (!strcasecmp(key, "nonce")) {
01995          ast_string_field_set(d, nonce, val);
01996       } else if (!strcasecmp(key, "uri")) {
01997          ast_string_field_set(d, uri, val);
01998       } else if (!strcasecmp(key, "domain")) {
01999          ast_string_field_set(d, domain, val);
02000       } else if (!strcasecmp(key, "response")) {
02001          ast_string_field_set(d, response, val);
02002       } else if (!strcasecmp(key, "algorithm")) {
02003          if (strcasecmp(val, "MD5")) {
02004             ast_log(LOG_WARNING, "Digest algorithm: \"%s\" not supported.\n", val);
02005             return -1;
02006          }
02007       } else if (!strcasecmp(key, "cnonce")) {
02008          ast_string_field_set(d, cnonce, val);
02009       } else if (!strcasecmp(key, "opaque")) {
02010          ast_string_field_set(d, opaque, val);
02011       } else if (!strcasecmp(key, "qop") && !strcasecmp(val, "auth")) {
02012          d->qop = 1;
02013       } else if (!strcasecmp(key, "nc")) {
02014          unsigned long u;
02015          if (sscanf(val, "%30lx", &u) != 1) {
02016             ast_log(LOG_WARNING, "Incorrect Digest nc value: \"%s\".\n", val);
02017             return -1;
02018          }
02019          ast_string_field_set(d, nc, val);
02020       }
02021    }
02022    ast_free(str);
02023 
02024    /* Digest checkout */
02025    if (ast_strlen_zero(d->realm) || ast_strlen_zero(d->nonce)) {
02026       /* "realm" and "nonce" MUST be always exist */
02027       return -1;
02028    }
02029 
02030    if (!request) {
02031       /* Additional check for Digest response */
02032       if (ast_strlen_zero(d->username) || ast_strlen_zero(d->uri) || ast_strlen_zero(d->response)) {
02033          return -1;
02034       }
02035 
02036       if (pedantic && d->qop && (ast_strlen_zero(d->cnonce) || ast_strlen_zero(d->nc))) {
02037          return -1;
02038       }
02039    }
02040 
02041    return 0;
02042 }

char* ast_process_quotes_and_slashes ( char *  start,
char  find,
char  replace_with 
)

Process a string to find and replace characters.

Parameters:
start The string to analyze
find The character to find
replace_with The character that will replace the one we are looking for

Definition at line 1423 of file utils.c.

01424 {
01425    char *dataPut = start;
01426    int inEscape = 0;
01427    int inQuotes = 0;
01428 
01429    for (; *start; start++) {
01430       if (inEscape) {
01431          *dataPut++ = *start;       /* Always goes verbatim */
01432          inEscape = 0;
01433       } else {
01434          if (*start == '\\') {
01435             inEscape = 1;      /* Do not copy \ into the data */
01436          } else if (*start == '\'') {
01437             inQuotes = 1 - inQuotes;   /* Do not copy ' into the data */
01438          } else {
01439             /* Replace , with |, unless in quotes */
01440             *dataPut++ = inQuotes ? *start : ((*start == find) ? replace_with : *start);
01441          }
01442       }
01443    }
01444    if (start != dataPut)
01445       *dataPut = 0;
01446    return dataPut;
01447 }

int ast_pthread_create_detached_stack ( pthread_t *  thread,
pthread_attr_t *  attr,
void *(*)(void *)  start_routine,
void *  data,
size_t  stacksize,
const char *  file,
const char *  caller,
int  line,
const char *  start_fn 
)

Definition at line 1026 of file utils.c.

References ast_log(), ast_pthread_create_stack(), errno, LOG_WARNING, and thr_arg::start_routine.

01029 {
01030    unsigned char attr_destroy = 0;
01031    int res;
01032 
01033    if (!attr) {
01034       attr = alloca(sizeof(*attr));
01035       pthread_attr_init(attr);
01036       attr_destroy = 1;
01037    }
01038 
01039    if ((errno = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED)))
01040       ast_log(LOG_WARNING, "pthread_attr_setdetachstate: %s\n", strerror(errno));
01041 
01042    res = ast_pthread_create_stack(thread, attr, start_routine, data, 
01043                                   stacksize, file, caller, line, start_fn);
01044 
01045    if (attr_destroy)
01046       pthread_attr_destroy(attr);
01047 
01048    return res;
01049 }

int ast_pthread_create_stack ( pthread_t *  thread,
pthread_attr_t *  attr,
void *(*)(void *)  start_routine,
void *  data,
size_t  stacksize,
const char *  file,
const char *  caller,
int  line,
const char *  start_fn 
)

Definition at line 977 of file utils.c.

References asprintf, ast_log(), ast_malloc, AST_STACKSIZE, thr_arg::data, dummy_start(), errno, LOG_WARNING, thr_arg::name, pthread_create, and thr_arg::start_routine.

Referenced by ast_pthread_create_detached_stack().

00980 {
00981 #if !defined(LOW_MEMORY)
00982    struct thr_arg *a;
00983 #endif
00984 
00985    if (!attr) {
00986       attr = alloca(sizeof(*attr));
00987       pthread_attr_init(attr);
00988    }
00989 
00990 #ifdef __linux__
00991    /* On Linux, pthread_attr_init() defaults to PTHREAD_EXPLICIT_SCHED,
00992       which is kind of useless. Change this here to
00993       PTHREAD_INHERIT_SCHED; that way the -p option to set realtime
00994       priority will propagate down to new threads by default.
00995       This does mean that callers cannot set a different priority using
00996       PTHREAD_EXPLICIT_SCHED in the attr argument; instead they must set
00997       the priority afterwards with pthread_setschedparam(). */
00998    if ((errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED)))
00999       ast_log(LOG_WARNING, "pthread_attr_setinheritsched: %s\n", strerror(errno));
01000 #endif
01001 
01002    if (!stacksize)
01003       stacksize = AST_STACKSIZE;
01004 
01005    if ((errno = pthread_attr_setstacksize(attr, stacksize ? stacksize : AST_STACKSIZE)))
01006       ast_log(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(errno));
01007 
01008 #if !defined(LOW_MEMORY)
01009    if ((a = ast_malloc(sizeof(*a)))) {
01010       a->start_routine = start_routine;
01011       a->data = data;
01012       start_routine = dummy_start;
01013       if (asprintf(&a->name, "%-20s started at [%5d] %s %s()",
01014               start_fn, line, file, caller) < 0) {
01015          ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
01016          a->name = NULL;
01017       }
01018       data = a;
01019    }
01020 #endif /* !LOW_MEMORY */
01021 
01022    return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */
01023 }

long int ast_random ( void   ) 

Definition at line 1399 of file utils.c.

References ast_mutex_lock(), ast_mutex_unlock(), and randomlock.

Referenced by acf_rand_exec(), action_challenge(), add_sdp(), agent_new(), agi_handle_command(), ast_lock_path_lockfile(), ast_moh_files_next(), ast_rtp_new(), ast_rtp_new_source(), ast_udptl_new_with_bindaddr(), auth_http_callback(), authenticate_request(), build_gateway(), build_iv(), build_rand_pad(), build_reply_digest(), calc_metric(), calc_rxstamp(), caldav_write_event(), callno_hash(), dahdi_new(), generate_exchange_uuid(), generate_random_string(), generic_http_callback(), get_trans_id(), gtalk_alloc(), gtalk_create_candidates(), gtalk_new(), handle_response_invite(), iax2_key_rotate(), iax2_start_transfer(), jingle_alloc(), jingle_create_candidates(), jingle_new(), load_module(), local_new(), make_email_file(), make_our_tag(), moh_files_alloc(), multicast_rtp_new(), ogg_vorbis_rewrite(), osp_create_uuid(), page_exec(), park_space_reserve(), process_weights(), reg_source_db(), registry_authrequest(), reqprep(), say_periodic_announcement(), sendmail(), set_nonce_randdata(), sip_alloc(), socket_read(), start_rtp(), stun_req_id(), transmit_invite(), transmit_register(), transmit_response_using_temp(), try_calling(), and try_firmware().

01400 {
01401    long int res;
01402 #ifdef HAVE_DEV_URANDOM
01403    if (dev_urandom_fd >= 0) {
01404       int read_res = read(dev_urandom_fd, &res, sizeof(res));
01405       if (read_res > 0) {
01406          long int rm = RAND_MAX;
01407          res = res < 0 ? ~res : res;
01408          rm++;
01409          return res % rm;
01410       }
01411    }
01412 #endif
01413 #ifdef linux
01414    res = random();
01415 #else
01416    ast_mutex_lock(&randomlock);
01417    res = random();
01418    ast_mutex_unlock(&randomlock);
01419 #endif
01420    return res;
01421 }

void ast_sha1_hash ( char *  output,
const char *  input 
)

Produce 40 char SHA1 hash of value.

Produces SHA1 hash based on input string.

Definition at line 246 of file utils.c.

References SHA1Input(), SHA1Reset(), and SHA1Result().

Referenced by aji_act_hook(), handle_call_token(), jabber_make_auth(), and sha1().

00247 {
00248    struct SHA1Context sha;
00249    char *ptr;
00250    int x;
00251    uint8_t Message_Digest[20];
00252 
00253    SHA1Reset(&sha);
00254    
00255    SHA1Input(&sha, (const unsigned char *) input, strlen(input));
00256 
00257    SHA1Result(&sha, Message_Digest);
00258    ptr = output;
00259    for (x = 0; x < 20; x++)
00260       ptr += sprintf(ptr, "%2.2x", Message_Digest[x]);
00261 }

char* ast_strip_quoted ( char *  s,
const char *  beg_quotes,
const char *  end_quotes 
)

Strip leading/trailing whitespace and quotes from a string.

Parameters:
s The string to be stripped (will be modified).
beg_quotes The list of possible beginning quote characters.
end_quotes The list of matching ending quote characters.
Returns:
The stripped string.
This functions strips all leading and trailing whitespace characters from the input string, and returns a pointer to the resulting string. The string is modified in place.

It can also remove beginning and ending quote (or quote-like) characters, in matching pairs. If the first character of the string matches any character in beg_quotes, and the last character of the string is the matching character in end_quotes, then they are removed from the string.

Examples:

  ast_strip_quoted(buf, "\"", "\"");
  ast_strip_quoted(buf, "'", "'");
  ast_strip_quoted(buf, "[{(", "]})");

Definition at line 1207 of file utils.c.

References ast_strip().

Referenced by ast_register_file_version(), get_rdnis(), iftime(), load_values_config(), parse_allowed_methods(), parse_cookies(), parse_dial_string(), and sip_register().

01208 {
01209    char *e;
01210    char *q;
01211 
01212    s = ast_strip(s);
01213    if ((q = strchr(beg_quotes, *s)) && *q != '\0') {
01214       e = s + strlen(s) - 1;
01215       if (*e == *(end_quotes + (q - beg_quotes))) {
01216          s++;
01217          *e = '\0';
01218       }
01219    }
01220 
01221    return s;
01222 }

int ast_true ( const char *  val  ) 

Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".

Return values:
0 if val is a NULL pointer.
-1 if "true".
0 otherwise.

Definition at line 1309 of file utils.c.

References ast_strlen_zero().

Referenced by __ast_http_load(), __ast_udptl_reload(), __init_manager(), _parse(), acf_curlopt_write(), acf_transaction_write(), action_agent_logoff(), action_bridge(), action_originate(), action_updateconfig(), aji_create_client(), aji_load_config(), apply_general_options(), apply_option(), apply_outgoing(), ast_bridge_timelimit(), ast_jb_read_conf(), ast_readconfig(), ast_tls_read_conf(), build_device(), build_gateway(), build_peer(), build_user(), config_parse_variables(), connect_link(), dahdi_set_dnd(), do_reload(), festival_exec(), func_channel_write(), func_inheritance_write(), func_mute_write(), get_encrypt_methods(), gtalk_load_config(), handle_common_options(), handle_logger_set_level(), handle_t38_options(), init_logger_chain(), jingle_load_config(), load_config(), load_config_meetme(), load_module(), load_modules(), load_moh_classes(), load_odbc_config(), load_rpt_vars(), local_ast_moh_start(), login_exec(), manager_add_queue_member(), manager_mutestream(), manager_pause_queue_member(), message_template_build(), misdn_answer(), odbc_load_module(), osp_load(), parse_config(), parse_empty_options(), pbx_load_config(), pbx_load_users(), process_dahdi(), process_echocancel(), queue_set_global_params(), queue_set_param(), read_agent_config(), realtime_directory(), reload_config(), rtp_reload(), run_startup_commands(), search_directory(), search_directory_sub(), set_active(), set_config(), sla_load_config(), smdi_load(), speex_write(), start_monitor_action(), strings_to_mask(), and update_common_options().

01310 {
01311    if (ast_strlen_zero(s))
01312       return 0;
01313 
01314    /* Determine if this is a true value */
01315    if (!strcasecmp(s, "yes") ||
01316        !strcasecmp(s, "true") ||
01317        !strcasecmp(s, "y") ||
01318        !strcasecmp(s, "t") ||
01319        !strcasecmp(s, "1") ||
01320        !strcasecmp(s, "on"))
01321       return -1;
01322 
01323    return 0;
01324 }

struct timeval ast_tvadd ( struct timeval  a,
struct timeval  b 
) [read]

Returns the sum of two timevals a + b.

Definition at line 1363 of file utils.c.

References ONE_MILLION, and tvfix().

Referenced by __get_from_jb(), acf_jabberreceive_read(), agent_hangup(), ast_audiohook_trigger_wait(), ast_channel_bridge(), ast_channel_cmpwhentohangup_tv(), ast_channel_setwhentohangup_tv(), ast_generic_bridge(), ast_rtp_dtmf_begin(), ast_rtp_dtmf_end(), ast_sched_runq(), ast_smoother_read(), ast_translate(), calc_timestamp(), cli_tps_ping(), conf_run(), dial_exec_full(), do_cdr(), do_timing(), iax2_process_thread(), jb_get_and_deliver(), mb_poll_thread(), monmp3thread(), mp3_exec(), mwi_monitor_handler(), NBScat_exec(), sched_run(), sched_settime(), schedule_delivery(), sla_process_timers(), smdi_message_wait(), and timeout_write().

01364 {
01365    /* consistency checks to guarantee usec in 0..999999 */
01366    a = tvfix(a);
01367    b = tvfix(b);
01368    a.tv_sec += b.tv_sec;
01369    a.tv_usec += b.tv_usec;
01370    if (a.tv_usec >= ONE_MILLION) {
01371       a.tv_sec++;
01372       a.tv_usec -= ONE_MILLION;
01373    }
01374    return a;
01375 }

struct timeval ast_tvsub ( struct timeval  a,
struct timeval  b 
) [read]

Returns the difference of two timevals a - b.

Definition at line 1377 of file utils.c.

References ONE_MILLION, and tvfix().

Referenced by ast_channel_bridge(), ast_rwlock_timedrdlock(), ast_rwlock_timedwrlock(), ast_sched_dump(), ast_translate(), ast_waitfor_nandfds(), calc_rxstamp(), calc_timestamp(), cli_tps_ping(), conf_run(), handle_showcalls(), and handle_showuptime().

01378 {
01379    /* consistency checks to guarantee usec in 0..999999 */
01380    a = tvfix(a);
01381    b = tvfix(b);
01382    a.tv_sec -= b.tv_sec;
01383    a.tv_usec -= b.tv_usec;
01384    if (a.tv_usec < 0) {
01385       a.tv_sec-- ;
01386       a.tv_usec += ONE_MILLION;
01387    }
01388    return a;
01389 }

char* ast_unescape_c ( char *  s  ) 

Convert some C escape sequences.

(\b\f\n\r\t) 
into the equivalent characters. The string to be converted (will be modified).
Returns:
The converted string.

Definition at line 1243 of file utils.c.

01244 {
01245    char c, *ret, *dst;
01246 
01247    if (src == NULL)
01248       return NULL;
01249    for (ret = dst = src; (c = *src++); *dst++ = c ) {
01250       if (c != '\\')
01251          continue;   /* copy char at the end of the loop */
01252       switch ((c = *src++)) {
01253       case '\0':  /* special, trailing '\' */
01254          c = '\\';
01255          break;
01256       case 'b':   /* backspace */
01257          c = '\b';
01258          break;
01259       case 'f':   /* form feed */
01260          c = '\f';
01261          break;
01262       case 'n':
01263          c = '\n';
01264          break;
01265       case 'r':
01266          c = '\r';
01267          break;
01268       case 't':
01269          c = '\t';
01270          break;
01271       }
01272       /* default, use the char literally */
01273    }
01274    *dst = '\0';
01275    return ret;
01276 }

char* ast_unescape_semicolon ( char *  s  ) 

Strip backslash for "escaped" semicolons, the string to be stripped (will be modified).

Returns:
The stripped string.

Definition at line 1224 of file utils.c.

Referenced by sip_cli_notify().

01225 {
01226    char *e;
01227    char *work = s;
01228 
01229    while ((e = strchr(work, ';'))) {
01230       if ((e > work) && (*(e-1) == '\\')) {
01231          memmove(e - 1, e, strlen(e) + 1);
01232          work = e;
01233       } else {
01234          work = e + 1;
01235       }
01236    }
01237 
01238    return s;
01239 }

void ast_uri_decode ( char *  s  ) 

ast_uri_decode: Decode SIP URI, URN, URL (overwrite the string)

Decode URI, URN, URL (overwrite string).

Definition at line 414 of file utils.c.

Referenced by acf_curl_helper(), check_user_full(), config_curl(), get_also_info(), get_destination(), get_refer_info(), handle_request_invite(), http_decode(), realtime_curl(), realtime_multi_curl(), register_verify(), sip_new(), and uridecode().

00415 {
00416    char *o;
00417    unsigned int tmp;
00418 
00419    for (o = s; *s; s++, o++) {
00420       if (*s == '%' && strlen(s) > 2 && sscanf(s + 1, "%2x", &tmp) == 1) {
00421          /* have '%', two chars and correct parsing */
00422          *o = tmp;
00423          s += 2;  /* Will be incremented once more when we break out */
00424       } else /* all other cases, just copy */
00425          *o = *s;
00426    }
00427    *o = '\0';
00428 }

char* ast_uri_encode ( const char *  string,
char *  outbuf,
int  buflen,
int  doreserved 
)

ast_uri_encode: Turn text string to URI-encoded XX version

Turn text string to URI-encoded XX version.

Note:
At this point, we're converting from ISO-8859-x (8-bit), not UTF8 as in the SIP protocol spec If doreserved == 1 we will convert reserved characters also. RFC 2396, section 2.4 outbuf needs to have more memory allocated than the instring to have room for the expansion. Every char that is converted is replaced by three ASCII characters.
Note: The doreserved option is needed for replaces header in SIP transfers.

Definition at line 383 of file utils.c.

References ast_copy_string(), and buf.

Referenced by add_rpid(), config_curl(), destroy_curl(), initreqprep(), launch_asyncagi(), realtime_curl(), realtime_multi_curl(), require_curl(), store_curl(), update2_curl(), update_curl(), and uriencode().

00384 {
00385    char *reserved = ";/?:@&=+$,# "; /* Reserved chars */
00386 
00387    const char *ptr  = string; /* Start with the string */
00388    char *out = NULL;
00389    char *buf = NULL;
00390 
00391    ast_copy_string(outbuf, string, buflen);
00392 
00393    /* If there's no characters to convert, just go through and don't do anything */
00394    while (*ptr) {
00395       if ((*ptr < 32 || (unsigned char) *ptr) > 127 || (doreserved && strchr(reserved, *ptr)) ) {
00396          /* Oops, we need to start working here */
00397          if (!buf) {
00398             buf = outbuf;
00399             out = buf + (ptr - string) ;  /* Set output ptr */
00400          }
00401          out += sprintf(out, "%%%02x", (unsigned char) *ptr);
00402       } else if (buf) {
00403          *out = *ptr;   /* Continue copying the string */
00404          out++;
00405       } 
00406       ptr++;
00407    }
00408    if (buf)
00409       *out = '\0';
00410    return outbuf;
00411 }

int ast_utils_init ( void   ) 

Definition at line 1910 of file utils.c.

References ARRAY_LEN, ast_cli_register_multiple(), and base64_init().

Referenced by main().

01911 {
01912 #ifdef HAVE_DEV_URANDOM
01913    dev_urandom_fd = open("/dev/urandom", O_RDONLY);
01914 #endif
01915    base64_init();
01916 #ifdef DEBUG_THREADS
01917 #if !defined(LOW_MEMORY)
01918    ast_cli_register_multiple(utils_cli, ARRAY_LEN(utils_cli));
01919 #endif
01920 #endif
01921    return 0;
01922 }

int ast_wait_for_input ( int  fd,
int  ms 
)

Definition at line 1051 of file utils.c.

References ast_poll.

Referenced by action_waitevent(), ast_tcptls_server_root(), dahdi_test_timer(), get_input(), and moh_class_destructor().

01052 {
01053    struct pollfd pfd[1];
01054    memset(pfd, 0, sizeof(pfd));
01055    pfd[0].fd = fd;
01056    pfd[0].events = POLLIN|POLLPRI;
01057    return ast_poll(pfd, 1, ms);
01058 }

static int ast_wait_for_output ( int  fd,
int  timeoutms 
) [static]

Definition at line 1060 of file utils.c.

References ast_log(), ast_poll, ast_tvdiff_ms(), ast_tvnow(), errno, LOG_ERROR, and LOG_NOTICE.

Referenced by ast_careful_fwrite(), and ast_carefulwrite().

01061 {
01062    struct pollfd pfd = {
01063       .fd = fd,
01064       .events = POLLOUT,
01065    };
01066    int res;
01067    struct timeval start = ast_tvnow();
01068    int elapsed = 0;
01069 
01070    /* poll() until the fd is writable without blocking */
01071    while ((res = ast_poll(&pfd, 1, timeoutms - elapsed)) <= 0) {
01072       if (res == 0) {
01073          /* timed out. */
01074          ast_log(LOG_NOTICE, "Timed out trying to write\n");
01075          return -1;
01076       } else if (res == -1) {
01077          /* poll() returned an error, check to see if it was fatal */
01078 
01079          if (errno == EINTR || errno == EAGAIN) {
01080             elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01081             if (elapsed >= timeoutms) {
01082                return -1;
01083             }
01084             /* This was an acceptable error, go back into poll() */
01085             continue;
01086          }
01087 
01088          /* Fatal error, bail. */
01089          ast_log(LOG_ERROR, "poll returned error: %s\n", strerror(errno));
01090 
01091          return -1;
01092       }
01093       elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01094       if (elapsed >= timeoutms) {
01095          return -1;
01096       }
01097    }
01098 
01099    return 0;
01100 }

static void base64_init ( void   )  [static]

Definition at line 347 of file utils.c.

Referenced by ast_utils_init().

00348 {
00349    int x;
00350    memset(b2a, -1, sizeof(b2a));
00351    /* Initialize base-64 Conversion table */
00352    for (x = 0; x < 26; x++) {
00353       /* A-Z */
00354       base64[x] = 'A' + x;
00355       b2a['A' + x] = x;
00356       /* a-z */
00357       base64[x + 26] = 'a' + x;
00358       b2a['a' + x] = x + 26;
00359       /* 0-9 */
00360       if (x < 10) {
00361          base64[x + 52] = '0' + x;
00362          b2a['0' + x] = x + 52;
00363       }
00364    }
00365    base64[62] = '+';
00366    base64[63] = '/';
00367    b2a[(int)'+'] = 62;
00368    b2a[(int)'/'] = 63;
00369 }

static void* dummy_start ( void *  data  )  [static]

Definition at line 933 of file utils.c.

References ast_free, AST_LIST_INSERT_TAIL, AST_MUTEX_KIND, ast_register_thread(), ast_threadstorage_get(), ast_unregister_thread(), thr_arg::data, thr_arg::name, pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock, thr_arg::start_routine, and strdup.

Referenced by ast_pthread_create_stack().

00934 {
00935    void *ret;
00936    struct thr_arg a = *((struct thr_arg *) data);  /* make a local copy */
00937 #ifdef DEBUG_THREADS
00938    struct thr_lock_info *lock_info;
00939    pthread_mutexattr_t mutex_attr;
00940 #endif
00941 
00942    /* note that even though data->name is a pointer to allocated memory,
00943       we are not freeing it here because ast_register_thread is going to
00944       keep a copy of the pointer and then ast_unregister_thread will
00945       free the memory
00946    */
00947    ast_free(data);
00948    ast_register_thread(a.name);
00949    pthread_cleanup_push(ast_unregister_thread, (void *) pthread_self());
00950 
00951 #ifdef DEBUG_THREADS
00952    if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
00953       return NULL;
00954 
00955    lock_info->thread_id = pthread_self();
00956    lock_info->thread_name = strdup(a.name);
00957 
00958    pthread_mutexattr_init(&mutex_attr);
00959    pthread_mutexattr_settype(&mutex_attr, AST_MUTEX_KIND);
00960    pthread_mutex_init(&lock_info->lock, &mutex_attr);
00961    pthread_mutexattr_destroy(&mutex_attr);
00962 
00963    pthread_mutex_lock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */
00964    AST_LIST_INSERT_TAIL(&lock_infos, lock_info, entry);
00965    pthread_mutex_unlock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */
00966 #endif /* DEBUG_THREADS */
00967 
00968    ret = a.start_routine(a.data);
00969 
00970    pthread_cleanup_pop(1);
00971 
00972    return ret;
00973 }

static int gethostbyname_r ( const char *  name,
struct hostent *  ret,
char *  buf,
size_t  buflen,
struct hostent **  result,
int *  h_errnop 
) [static]

Reentrant replacement for gethostbyname for BSD-based systems.

Note:
This routine is derived from code originally written and placed in the public domain by Enzo Michelangeli <em@em.no-ip.com>

Definition at line 82 of file utils.c.

References __mutex, ast_mutex_lock(), ast_mutex_unlock(), ERANGE, and gethostbyname.

Referenced by ast_gethostbyname().

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 }

static size_t optimal_alloc_size ( size_t  size  )  [static]

Definition at line 1488 of file utils.c.

References ALLOCATOR_OVERHEAD.

Referenced by __ast_calloc_with_stringfields(), and add_string_pool().

01489 {
01490    unsigned int count;
01491 
01492    size += ALLOCATOR_OVERHEAD;
01493 
01494    for (count = 1; size; size >>= 1, count++);
01495 
01496    return (1 << count) - ALLOCATOR_OVERHEAD;
01497 }

static struct timeval tvfix ( struct timeval  a  )  [static, read]

Definition at line 1348 of file utils.c.

References ast_log(), LOG_WARNING, and ONE_MILLION.

Referenced by ast_tvadd(), and ast_tvsub().

01349 {
01350    if (a.tv_usec >= ONE_MILLION) {
01351       ast_log(LOG_WARNING, "warning too large timestamp %ld.%ld\n",
01352          (long)a.tv_sec, (long int) a.tv_usec);
01353       a.tv_sec += a.tv_usec / ONE_MILLION;
01354       a.tv_usec %= ONE_MILLION;
01355    } else if (a.tv_usec < 0) {
01356       ast_log(LOG_WARNING, "warning negative timestamp %ld.%ld\n",
01357          (long)a.tv_sec, (long int) a.tv_usec);
01358       a.tv_usec = 0;
01359    }
01360    return a;
01361 }


Variable Documentation

struct { ... } __ast_string_field_empty_buffer [static]

ast_mutex_t __mutex = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

Definition at line 75 of file utils.c.

Referenced by gethostbyname_r().

Definition at line 1480 of file utils.c.

Referenced by __ast_calloc_with_stringfields().

char b2a[256] [static]

Definition at line 66 of file utils.c.

char base64[64] [static]

Definition at line 65 of file utils.c.

Referenced by aji_start_sasl().

ast_mutex_t fetchadd_m = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

Definition at line 1800 of file utils.c.

Referenced by ast_atomic_fetchadd_int_slow().

struct ast_threadstorage inet_ntoa_buf = { .once = PTHREAD_ONCE_INIT, .key_init = __init_inet_ntoa_buf , .custom_init = NULL , } [static]

Definition at line 68 of file utils.c.

Referenced by ast_inet_ntoa().

ast_mutex_t randomlock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

glibc puts a lock inside random(3), so that the results are thread-safe. BSD libc (and others) do not.

Definition at line 1396 of file utils.c.

Referenced by ast_random().

char string[1]

Definition at line 1481 of file utils.c.

Referenced by private_enum_init().


Generated on Wed Oct 28 13:33:49 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6