Mon Oct 8 12:39:30 2012

Asterisk developer's documentation


utils.c File Reference

Utility functions. More...

#include "asterisk.h"
#include <ctype.h>
#include <sys/stat.h>
#include <fcntl.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"

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 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 data from base64.
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.
char * ast_escape_quoted (const char *string, char *outbuf, int buflen)
 Escape characters found in a quoted string.
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.
hostent * ast_gethostbyname (const char *host, struct ast_hostent *hp)
 Thread-safe gethostbyname function to use in Asterisk.
const char * ast_inet_ntoa (struct in_addr ia)
 thread-safe replacement for inet_ntoa().
void ast_join (char *s, size_t len, const char *const w[])
void ast_md5_hash (char *output, const char *input)
 Produces MD5 hash based on input string.
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)
 Produces SHA1 hash based on input string.
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".
timeval ast_tvadd (struct timeval a, struct timeval b)
 Returns the sum of two timevals a + b.
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)
 Decode URI, URN, URL (overwrite string).
char * ast_uri_encode (const char *string, char *outbuf, int buflen, int do_special_char)
 Turn text string to URI-encoded XX version.
int ast_utils_init (void)
char * ast_utils_which (const char *binary, char *fullpath, size_t fullpath_size)
 Resolve a binary to a full pathname.
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 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 char b2a [256]
static char base64 [64]
static int dev_urandom_fd
static ast_mutex_t fetchadd_m = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 }
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 = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 }
 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 1528 of file utils.c.

Referenced by optimal_alloc_size().

#define AST_API_MODULE

Definition at line 67 of file utils.c.

#define AST_API_MODULE

Definition at line 67 of file utils.c.

#define AST_API_MODULE

Definition at line 67 of file utils.c.

#define AST_API_MODULE

Definition at line 67 of file utils.c.

#define AST_API_MODULE

Definition at line 67 of file utils.c.

#define AST_API_MODULE

Definition at line 67 of file utils.c.

#define AST_API_MODULE

Definition at line 67 of file utils.c.

#define ONE_MILLION   1000000

Definition at line 1385 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 1807 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.

01810 {
01811    struct ast_string_field_mgr *mgr;
01812    struct ast_string_field_pool *pool;
01813    struct ast_string_field_pool **pool_head;
01814    size_t pool_size_needed = sizeof(*pool) + pool_size;
01815    size_t size_to_alloc = optimal_alloc_size(struct_size + pool_size_needed);
01816    void *allocation;
01817    unsigned int x;
01818 
01819 #if defined(__AST_DEBUG_MALLOC)  
01820    if (!(allocation = __ast_calloc(num_structs, size_to_alloc, file, lineno, func))) {
01821       return NULL;
01822    }
01823 #else
01824    if (!(allocation = ast_calloc(num_structs, size_to_alloc))) {
01825       return NULL;
01826    }
01827 #endif
01828 
01829    for (x = 0; x < num_structs; x++) {
01830       void *base = allocation + (size_to_alloc * x);
01831       const char **p;
01832 
01833       mgr = base + field_mgr_offset;
01834       pool_head = base + field_mgr_pool_offset;
01835       pool = base + struct_size;
01836 
01837       p = (const char **) pool_head + 1;
01838       while ((struct ast_string_field_mgr *) p != mgr) {
01839          *p++ = __ast_string_field_empty;
01840       }
01841 
01842       mgr->embedded_pool = pool;
01843       *pool_head = pool;
01844       pool->size = size_to_alloc - struct_size - sizeof(*pool);
01845 #if defined(__AST_DEBUG_MALLOC)
01846       mgr->owner_file = file;
01847       mgr->owner_func = func;
01848       mgr->owner_line = lineno;
01849 #endif
01850    }
01851 
01852    return allocation;
01853 }

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 1648 of file utils.c.

References add_string_pool(), ast_alignof, ast_assert, ast_make_room_for, AST_STRING_FIELD_ALLOCATION, and ast_string_field_mgr::last_alloc.

Referenced by __ast_string_field_ptr_build_va().

01650 {
01651    char *result = NULL;
01652    size_t space = (*pool_head)->size - (*pool_head)->used;
01653    size_t to_alloc;
01654 
01655    /* Make room for ast_string_field_allocation and make it a multiple of that. */
01656    to_alloc = ast_make_room_for(needed, ast_string_field_allocation);
01657    ast_assert(to_alloc % ast_alignof(ast_string_field_allocation) == 0);
01658 
01659    if (__builtin_expect(to_alloc > space, 0)) {
01660       size_t new_size = (*pool_head)->size;
01661 
01662       while (new_size < to_alloc) {
01663          new_size *= 2;
01664       }
01665 
01666 #if defined(__AST_DEBUG_MALLOC)
01667       if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func))
01668          return NULL;
01669 #else
01670       if (add_string_pool(mgr, pool_head, new_size, __FILE__, __LINE__, __FUNCTION__))
01671          return NULL;
01672 #endif
01673    }
01674 
01675    /* pool->base is always aligned (gcc aligned attribute). We ensure that
01676     * to_alloc is also a multiple of ast_alignof(ast_string_field_allocation)
01677     * causing result to always be aligned as well; which in turn fixes that
01678     * AST_STRING_FIELD_ALLOCATION(result) is aligned. */
01679    result = (*pool_head)->base + (*pool_head)->used;
01680    (*pool_head)->used += to_alloc;
01681    (*pool_head)->active += needed;
01682    result += ast_alignof(ast_string_field_allocation);
01683    AST_STRING_FIELD_ALLOCATION(result) = needed;
01684    mgr->last_alloc = result;
01685 
01686    return result;
01687 }

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 1581 of file utils.c.

References 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.

01583 {
01584    const char **p = (const char **) pool_head + 1;
01585    struct ast_string_field_pool *cur = NULL;
01586    struct ast_string_field_pool *preserve = NULL;
01587 
01588    /* clear fields - this is always necessary */
01589    while ((struct ast_string_field_mgr *) p != mgr) {
01590       *p++ = __ast_string_field_empty;
01591    }
01592 
01593    mgr->last_alloc = NULL;
01594 #if defined(__AST_DEBUG_MALLOC)
01595    mgr->owner_file = file;
01596    mgr->owner_func = func;
01597    mgr->owner_line = lineno;
01598 #endif
01599    if (needed > 0) {    /* allocate the initial pool */
01600       *pool_head = NULL;
01601       mgr->embedded_pool = NULL;
01602       return add_string_pool(mgr, pool_head, needed, file, lineno, func);
01603    }
01604 
01605    /* if there is an embedded pool, we can't actually release *all*
01606     * pools, we must keep the embedded one. if the caller is about
01607     * to free the structure that contains the stringfield manager
01608     * and embedded pool anyway, it will be freed as part of that
01609     * operation.
01610     */
01611    if ((needed < 0) && mgr->embedded_pool) {
01612       needed = 0;
01613    }
01614 
01615    if (needed < 0) {    /* reset all pools */
01616       cur = *pool_head;
01617    } else if (mgr->embedded_pool) { /* preserve the embedded pool */
01618       preserve = mgr->embedded_pool;
01619       cur = *pool_head;
01620    } else {       /* preserve the last pool */
01621       if (*pool_head == NULL) {
01622          ast_log(LOG_WARNING, "trying to reset empty pool\n");
01623          return -1;
01624       }
01625       preserve = *pool_head;
01626       cur = preserve->prev;
01627    }
01628 
01629    if (preserve) {
01630       preserve->prev = NULL;
01631       preserve->used = preserve->active = 0;
01632    }
01633 
01634    while (cur) {
01635       struct ast_string_field_pool *prev = cur->prev;
01636 
01637       if (cur != preserve) {
01638          ast_free(cur);
01639       }
01640       cur = prev;
01641    }
01642 
01643    *pool_head = preserve;
01644 
01645    return 0;
01646 }

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 1792 of file utils.c.

References __ast_string_field_ptr_build_va().

01795 {
01796    va_list ap1, ap2;
01797 
01798    va_start(ap1, format);
01799    va_start(ap2, format);     /* va_copy does not exist on FreeBSD */
01800 
01801    __ast_string_field_ptr_build_va(mgr, pool_head, ptr, format, ap1, ap2);
01802 
01803    va_end(ap1);
01804    va_end(ap2);
01805 }

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 1732 of file utils.c.

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

Referenced by __ast_string_field_ptr_build().

01735 {
01736    size_t needed;
01737    size_t available;
01738    size_t space = (*pool_head)->size - (*pool_head)->used;
01739    ssize_t grow;
01740    char *target;
01741 
01742    /* if the field already has space allocated, try to reuse it;
01743       otherwise, try to use the empty space at the end of the current
01744       pool
01745    */
01746    if (*ptr != __ast_string_field_empty) {
01747       target = (char *) *ptr;
01748       available = AST_STRING_FIELD_ALLOCATION(*ptr);
01749       if (*ptr == mgr->last_alloc) {
01750          available += space;
01751       }
01752    } else {
01753       /* pool->used is always a multiple of ast_alignof(ast_string_field_allocation)
01754        * so we don't need to re-align anything here.
01755        */
01756       target = (*pool_head)->base + (*pool_head)->used + ast_alignof(ast_string_field_allocation);
01757       available = space - ast_alignof(ast_string_field_allocation);
01758    }
01759 
01760    needed = vsnprintf(target, available, format, ap1) + 1;
01761 
01762    if (needed > available) {
01763       /* the allocation could not be satisfied using the field's current allocation
01764          (if it has one), or the space available in the pool (if it does not). allocate
01765          space for it, adding a new string pool if necessary.
01766       */
01767       if (!(target = (char *) __ast_string_field_alloc_space(mgr, pool_head, needed))) {
01768          return;
01769       }
01770       vsprintf(target, format, ap2);
01771       __ast_string_field_release_active(*pool_head, *ptr);
01772       *ptr = target;
01773    } else if (*ptr != target) {
01774       /* the allocation was satisfied using available space in the pool, but not
01775          using the space already allocated to the field
01776       */
01777       __ast_string_field_release_active(*pool_head, *ptr);
01778       mgr->last_alloc = *ptr = target;
01779       AST_STRING_FIELD_ALLOCATION(target) = needed;
01780       (*pool_head)->used += ast_make_room_for(needed, ast_string_field_allocation);
01781       (*pool_head)->active += needed;
01782    } else if ((grow = (needed - AST_STRING_FIELD_ALLOCATION(*ptr))) > 0) {
01783       /* the allocation was satisfied by using available space in the pool *and*
01784          the field was the last allocated field from the pool, so it grew
01785       */
01786       AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
01787       (*pool_head)->used += ast_align_for(grow, ast_string_field_allocation);
01788       (*pool_head)->active += grow;
01789    }
01790 }

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 1689 of file utils.c.

References AST_STRING_FIELD_ALLOCATION, and ast_string_field_mgr::last_alloc.

01692 {
01693    ssize_t grow = needed - AST_STRING_FIELD_ALLOCATION(*ptr);
01694    size_t space = (*pool_head)->size - (*pool_head)->used;
01695 
01696    if (*ptr != mgr->last_alloc) {
01697       return 1;
01698    }
01699 
01700    if (space < grow) {
01701       return 1;
01702    }
01703 
01704    (*pool_head)->used += grow;
01705    (*pool_head)->active += grow;
01706    AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
01707 
01708    return 0;
01709 }

void __ast_string_field_release_active ( struct ast_string_field_pool pool_head,
const ast_string_field  ptr 
)

Definition at line 1711 of file utils.c.

References 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().

01713 {
01714    struct ast_string_field_pool *pool, *prev;
01715 
01716    if (ptr == __ast_string_field_empty) {
01717       return;
01718    }
01719 
01720    for (pool = pool_head, prev = NULL; pool; prev = pool, pool = pool->prev) {
01721       if ((ptr >= pool->base) && (ptr <= (pool->base + pool->size))) {
01722          pool->active -= AST_STRING_FIELD_ALLOCATION(ptr);
01723          if ((pool->active == 0) && prev) {
01724             prev->prev = pool->prev;
01725             ast_free(pool);
01726          }
01727          break;
01728       }
01729    }
01730 }

static void __init_inet_ntoa_buf ( void   )  [static]

Definition at line 73 of file utils.c.

00090 {

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

Definition at line 2104 of file utils.c.

References MALLOC_FAILURE_MSG, and vasprintf.

02105 {
02106    int res;
02107    va_list ap;
02108 
02109    va_start(ap, fmt);
02110    if ((res = vasprintf(ret, fmt, ap)) == -1) {
02111       MALLOC_FAILURE_MSG;
02112    }
02113    va_end(ap);
02114 
02115    return res;
02116 }

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 1545 of file utils.c.

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

Referenced by __ast_string_field_alloc_space(), and __ast_string_field_init().

01547 {
01548    struct ast_string_field_pool *pool;
01549    size_t alloc_size = optimal_alloc_size(sizeof(*pool) + size);
01550 
01551 #if defined(__AST_DEBUG_MALLOC)
01552    if (!(pool = __ast_calloc(1, alloc_size, file, lineno, func))) {
01553       return -1;
01554    }
01555 #else
01556    if (!(pool = ast_calloc(1, alloc_size))) {
01557       return -1;
01558    }
01559 #endif
01560 
01561    pool->prev = *pool_head;
01562    pool->size = alloc_size - sizeof(*pool);
01563    *pool_head = pool;
01564    mgr->last_alloc = NULL;
01565 
01566    return 0;
01567 }

int ast_atomic_fetchadd_int_slow ( volatile int *  p,
int  v 
)

Definition at line 1859 of file utils.c.

References ast_mutex_lock, ast_mutex_unlock, and fetchadd_m.

01860 {
01861    int ret;
01862    ast_mutex_lock(&fetchadd_m);
01863    ret = *p;
01864    *p += v;
01865    ast_mutex_unlock(&fetchadd_m);
01866    return ret;
01867 }

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

Decode data from base64.

Parameters:
dst the destination buffer
src the source buffer
max The maximum number of bytes to write into the destination buffer. Note that this function will not ensure that the destination buffer is NULL terminated. So, in general, this parameter should be sizeof(dst) - 1.

Definition at line 271 of file utils.c.

Referenced by aes_helper(), ast_check_signature(), base64_helper(), osp_validate_token(), sdp_crypto_process(), and sdp_crypto_setup().

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

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 349 of file utils.c.

References ast_base64encode_full().

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

00350 {
00351    return ast_base64encode_full(dst, src, srclen, max, 0);
00352 }

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

encode text to BASE64 coding

Definition at line 298 of file utils.c.

Referenced by ast_base64encode().

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

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 1339 of file utils.c.

References ast_build_string_va().

Referenced by ast_fax_caps_to_str(), config_odbc(), generate_filenames_string(), handle_speechrecognize(), pp_each_extension_helper(), and pp_each_user_helper().

01340 {
01341    va_list ap;
01342    int result;
01343 
01344    va_start(ap, fmt);
01345    result = ast_build_string_va(buffer, space, fmt, ap);
01346    va_end(ap);
01347 
01348    return result;
01349 }

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 1320 of file utils.c.

Referenced by ast_build_string().

01321 {
01322    int result;
01323 
01324    if (!buffer || !*buffer || !space || !*space)
01325       return -1;
01326 
01327    result = vsnprintf(*buffer, *space, fmt, ap);
01328 
01329    if (result < 0)
01330       return -1;
01331    else if (result > *space)
01332       result = *space;
01333 
01334    *buffer += result;
01335    *space -= result;
01336    return 0;
01337 }

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 1194 of file utils.c.

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

Referenced by send_string().

01195 {
01196    struct timeval start = ast_tvnow();
01197    int n = 0;
01198    int elapsed = 0;
01199 
01200    while (len) {
01201       if (ast_wait_for_output(fd, timeoutms - elapsed)) {
01202          /* poll returned a fatal error, so bail out immediately. */
01203          return -1;
01204       }
01205 
01206       /* Clear any errors from a previous write */
01207       clearerr(f);
01208 
01209       n = fwrite(src, 1, len, f);
01210 
01211       if (ferror(f) && errno != EINTR && errno != EAGAIN) {
01212          /* fatal error from fwrite() */
01213          if (!feof(f)) {
01214             /* Don't spam the logs if it was just that the connection is closed. */
01215             ast_log(LOG_ERROR, "fwrite() returned error: %s\n", strerror(errno));
01216          }
01217          n = -1;
01218          break;
01219       }
01220 
01221       /* Update for data already written to the socket */
01222       len -= n;
01223       src += n;
01224 
01225       elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01226       if (elapsed >= timeoutms) {
01227          /* We've taken too long to write 
01228           * This is only an error condition if we haven't finished writing. */
01229          n = len ? -1 : 0;
01230          break;
01231       }
01232    }
01233 
01234    while (fflush(f)) {
01235       if (errno == EAGAIN || errno == EINTR) {
01236          continue;
01237       }
01238       if (!feof(f)) {
01239          /* Don't spam the logs if it was just that the connection is closed. */
01240          ast_log(LOG_ERROR, "fflush() returned error: %s\n", strerror(errno));
01241       }
01242       n = -1;
01243       break;
01244    }
01245 
01246    return n < 0 ? -1 : 0;
01247 }

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 1153 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().

01154 {
01155    struct timeval start = ast_tvnow();
01156    int res = 0;
01157    int elapsed = 0;
01158 
01159    while (len) {
01160       if (ast_wait_for_output(fd, timeoutms - elapsed)) {
01161          return -1;
01162       }
01163 
01164       res = write(fd, s, len);
01165 
01166       if (res < 0 && errno != EAGAIN && errno != EINTR) {
01167          /* fatal error from write() */
01168          ast_log(LOG_ERROR, "write() returned error: %s\n", strerror(errno));
01169          return -1;
01170       }
01171 
01172       if (res < 0) {
01173          /* It was an acceptable error */
01174          res = 0;
01175       }
01176 
01177       /* Update how much data we have left to write */
01178       len -= res;
01179       s += res;
01180       res = 0;
01181 
01182       elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01183       if (elapsed >= timeoutms) {
01184          /* We've taken too long to write 
01185           * This is only an error condition if we haven't finished writing. */
01186          res = len ? -1 : 0;
01187          break;
01188       }
01189    }
01190 
01191    return res;
01192 }

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 1922 of file utils.c.

References ast_log(), and LOG_WARNING.

Referenced by ast_netsock_bindaddr().

01923 {
01924 #if defined(HAVE_IP_MTU_DISCOVER)
01925    int val = IP_PMTUDISC_DONT;
01926    
01927    if (setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)))
01928       ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n");
01929 #endif /* HAVE_IP_MTU_DISCOVER */
01930 }

char* ast_escape_quoted ( const char *  string,
char *  outbuf,
int  buflen 
)

Escape characters found in a quoted string.

Note:
This function escapes quoted characters based on the 'qdtext' set of allowed characters from RFC 3261 section 25.1.
Parameters:
string string to be escaped
outbuf resulting escaped string
buflen size of output buffer
Returns:
a pointer to the escaped string

Definition at line 423 of file utils.c.

Referenced by initreqprep().

00424 {
00425    const char *ptr  = string;
00426    char *out = outbuf;
00427    char *allow = "\t\v !"; /* allow LWS (minus \r and \n) and "!" */
00428 
00429    while (*ptr && out - outbuf < buflen - 1) {
00430       if (!(strchr(allow, *ptr))
00431          && !(*ptr >= '#' && *ptr <= '[') /* %x23 - %x5b */
00432          && !(*ptr >= ']' && *ptr <= '~') /* %x5d - %x7e */
00433          && !((unsigned char) *ptr > 0x7f)) {             /* UTF8-nonascii */
00434 
00435          if (out - outbuf >= buflen - 2) {
00436             break;
00437          }
00438          out += sprintf(out, "\\%c", (unsigned char) *ptr);
00439       } else {
00440          *out = *ptr;
00441          out++;
00442       }
00443       ptr++;
00444    }
00445 
00446    if (buflen) {
00447       *out = '\0';
00448    }
00449 
00450    return outbuf;
00451 }

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 1368 of file utils.c.

References ast_strlen_zero().

Referenced by __ast_udptl_reload(), acf_faxopt_write(), acf_transaction_write(), actual_load_config(), aji_create_client(), aji_load_config(), aoc_cli_debug_enable(), build_peer(), build_user(), dahdi_set_dnd(), find_realtime(), func_channel_write_real(), handle_common_options(), init_acf_query(), load_config(), load_odbc_config(), manager_mute_mixmonitor(), parse_empty_options(), rtp_reload(), run_agi(), set_config(), set_insecure_flags(), and strings_to_mask().

01369 {
01370    if (ast_strlen_zero(s))
01371       return 0;
01372 
01373    /* Determine if this is a false value */
01374    if (!strcasecmp(s, "no") ||
01375        !strcasecmp(s, "false") ||
01376        !strcasecmp(s, "n") ||
01377        !strcasecmp(s, "f") ||
01378        !strcasecmp(s, "0") ||
01379        !strcasecmp(s, "off"))
01380       return -1;
01381 
01382    return 0;
01383 }

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

get values from config variables.

Definition at line 1899 of file utils.c.

References ast_strlen_zero().

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

01900 {
01901    long t;
01902    int scanned;
01903 
01904    if (dst == NULL)
01905       return -1;
01906 
01907    *dst = _default;
01908 
01909    if (ast_strlen_zero(src))
01910       return -1;
01911 
01912    /* only integer at the moment, but one day we could accept more formats */
01913    if (sscanf(src, "%30ld%n", &t, &scanned) == 1) {
01914       *dst = t;
01915       if (consumed)
01916          *consumed = scanned;
01917       return 0;
01918    } else
01919       return -1;
01920 }

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

get values from config variables.

Definition at line 1872 of file utils.c.

References ast_strlen_zero().

Referenced by acf_strftime().

01873 {
01874    long double dtv = 0.0;
01875    int scanned;
01876 
01877    if (dst == NULL)
01878       return -1;
01879 
01880    *dst = _default;
01881 
01882    if (ast_strlen_zero(src))
01883       return -1;
01884 
01885    /* only integer at the moment, but one day we could accept more formats */
01886    if (sscanf(src, "%30Lf%n", &dtv, &scanned) > 0) {
01887       dst->tv_sec = dtv;
01888       dst->tv_usec = (dtv - dst->tv_sec) * 1000000.0;
01889       if (consumed)
01890          *consumed = scanned;
01891       return 0;
01892    } else
01893       return -1;
01894 }

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

Thread-safe gethostbyname function to use in Asterisk.

Definition at line 187 of file utils.c.

References hp.

Referenced by ast_parse_arg(), config_load(), config_parse_variables(), create_addr(), festival_exec(), gtalk_update_stun(), iax_template_parse(), jingle_load_config(), jingle_update_stun(), launch_netscript(), process_sdp(), realtime_peer(), realtime_user(), reload_config(), and set_config().

00188 {
00189    int res;
00190    int herrno;
00191    int dots = 0;
00192    const char *s;
00193    struct hostent *result = NULL;
00194    /* Although it is perfectly legitimate to lookup a pure integer, for
00195       the sake of the sanity of people who like to name their peers as
00196       integers, we break with tradition and refuse to look up a
00197       pure integer */
00198    s = host;
00199    res = 0;
00200    while (s && *s) {
00201       if (*s == '.')
00202          dots++;
00203       else if (!isdigit(*s))
00204          break;
00205       s++;
00206    }
00207    if (!s || !*s) {
00208       /* Forge a reply for IP's to avoid octal IP's being interpreted as octal */
00209       if (dots != 3)
00210          return NULL;
00211       memset(hp, 0, sizeof(struct ast_hostent));
00212       hp->hp.h_addrtype = AF_INET;
00213       hp->hp.h_addr_list = (void *) hp->buf;
00214       hp->hp.h_addr = hp->buf + sizeof(void *);
00215       /* For AF_INET, this will always be 4 */
00216       hp->hp.h_length = 4;
00217       if (inet_pton(AF_INET, host, hp->hp.h_addr) > 0)
00218          return &hp->hp;
00219       return NULL;
00220       
00221    }
00222 #ifdef HAVE_GETHOSTBYNAME_R_5
00223    result = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &herrno);
00224 
00225    if (!result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
00226       return NULL;
00227 #else
00228    res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
00229 
00230    if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
00231       return NULL;
00232 #endif
00233    return &hp->hp;
00234 }

const char* ast_inet_ntoa ( struct in_addr  ia  ) 

thread-safe replacement for inet_ntoa().

Note:
It is very important to note that even though this is a thread-safe replacement for inet_ntoa(), it is *not* reentrant. In a single thread, the result from a previous call to this function is no longer valid once it is called again. If the result from multiple calls to this function need to be kept or used at once, then the result must be copied to a local buffer before calling this function again.

Definition at line 471 of file utils.c.

References ast_threadstorage_get(), and inet_ntoa_buf.

Referenced by __attempt_transmit(), __find_callno(), __iax2_show_peers(), _skinny_show_device(), _skinny_show_devices(), acf_channel_read(), action_login(), add_ipv4_ie(), add_sdp(), ast_apply_ha(), ast_netsock_bindaddr(), ast_parse_arg(), authenticate(), calltoken_required(), check_access(), config_load(), create_client(), data_get_xml_add_child(), data_result_manager_output(), data_result_print_cli_node(), do_message(), 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(), gtalk_update_externip(), 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_command_response(), handle_error(), handle_mgcp_show_endpoints(), handle_open_receive_channel_ack_message(), handle_request(), handle_show_http(), handle_showmanconn(), handle_skinny_show_settings(), iax2_ack_registry(), iax2_prov_app(), iax2_trunk_queue(), iax_server(), iax_showframe(), jingle_create_candidates(), load_module(), manager_iax2_show_peer_list(), manager_iax2_show_registry(), mgcpsock_read(), oh323_addrcmp_str(), oh323_call(), oh323_set_rtp_peer(), parsing(), peercnt_add(), peercnt_modify(), peercnt_remove(), peers_data_provider_get(), phoneprov_callback(), process_request(), process_sdp(), purge_sessions(), raw_hangup(), realtime_peer(), realtime_user(), register_verify(), registry_rerequest(), resend_response(), sched_delay_remove(), score_address(), send_packet(), send_raw_client(), send_request(), send_response(), send_trunk(), set_config(), set_peercnt_limit(), setup_incoming_call(), show_main_page(), skinny_session(), skinny_set_rtp_peer(), socket_process(), socket_process_meta(), start_rtp(), stun_monitor_request(), timing_read(), unistim_info(), unistimsock_read(), and update_registry().

00472 {
00473    char *buf;
00474 
00475    if (!(buf = ast_threadstorage_get(&inet_ntoa_buf, INET_ADDRSTRLEN)))
00476       return "";
00477 
00478    return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
00479 }

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

Definition at line 1491 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().

01492 {
01493    int x, ofs = 0;
01494    const char *src;
01495 
01496    /* Join words into a string */
01497    if (!s)
01498       return;
01499    for (x = 0; ofs < len && w[x]; x++) {
01500       if (x > 0)
01501          s[ofs++] = ' ';
01502       for (src = w[x]; *src && ofs < len; src++)
01503          s[ofs++] = *src;
01504    }
01505    if (ofs == len)
01506       ofs--;
01507    s[ofs] = '\0';
01508 }

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

Produces MD5 hash based on input string.

Definition at line 237 of file utils.c.

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

Referenced by build_reply_digest(), and md5().

00238 {
00239    struct MD5Context md5;
00240    unsigned char digest[16];
00241    char *ptr;
00242    int x;
00243 
00244    MD5Init(&md5);
00245    MD5Update(&md5, (const unsigned char *) input, strlen(input));
00246    MD5Final(digest, &md5);
00247    ptr = output;
00248    for (x = 0; x < 16; x++)
00249       ptr += sprintf(ptr, "%2.2x", digest[x]);
00250 }

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 1932 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(), filename_parse(), init_logger(), load_module(), mixmonitor_exec(), reload_logger(), remove_from_queue(), setup_privacy_args(), sms_nextoutgoing(), sms_writefile(), testclient_exec(), testserver_exec(), and write_history().

01933 {
01934    char *ptr;
01935    int len = strlen(path), count = 0, x, piececount = 0;
01936    char *tmp = ast_strdupa(path);
01937    char **pieces;
01938    char *fullpath = alloca(len + 1);
01939    int res = 0;
01940 
01941    for (ptr = tmp; *ptr; ptr++) {
01942       if (*ptr == '/')
01943          count++;
01944    }
01945 
01946    /* Count the components to the directory path */
01947    pieces = alloca(count * sizeof(*pieces));
01948    for (ptr = tmp; *ptr; ptr++) {
01949       if (*ptr == '/') {
01950          *ptr = '\0';
01951          pieces[piececount++] = ptr + 1;
01952       }
01953    }
01954 
01955    *fullpath = '\0';
01956    for (x = 0; x < piececount; x++) {
01957       /* This looks funky, but the buffer is always ideally-sized, so it's fine. */
01958       strcat(fullpath, "/");
01959       strcat(fullpath, pieces[x]);
01960       res = mkdir(fullpath, mode);
01961       if (res && errno != EEXIST)
01962          return errno;
01963    }
01964    return 0;
01965 }

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 1989 of file utils.c.

References ast_free, ast_log(), ast_skip_blanks(), ast_str_buffer(), ast_str_create(), ast_str_set(), ast_string_field_ptr_set, ast_string_field_set, ast_strlen_zero(), ast_unescape_c(), ast_http_digest::cnonce, ast_http_digest::domain, LOG_WARNING, ast_http_digest::nonce, ast_http_digest::opaque, ast_http_digest::qop, ast_http_digest::realm, ast_http_digest::response, str, strsep(), ast_http_digest::uri, and ast_http_digest::username.

01989                                                                                                {
01990    char *c;
01991    struct ast_str *str = ast_str_create(16);
01992 
01993    /* table of recognised keywords, and places where they should be copied */
01994    const struct x {
01995       const char *key;
01996       const ast_string_field *field;
01997    } *i, keys[] = {
01998       { "username=", &d->username },
01999       { "realm=", &d->realm },
02000       { "nonce=", &d->nonce },
02001       { "uri=", &d->uri },
02002       { "domain=", &d->domain },
02003       { "response=", &d->response },
02004       { "cnonce=", &d->cnonce },
02005       { "opaque=", &d->opaque },
02006       /* Special cases that cannot be directly copied */
02007       { "algorithm=", NULL },
02008       { "qop=", NULL },
02009       { "nc=", NULL },
02010       { NULL, 0 },
02011    };
02012 
02013    if (ast_strlen_zero(digest) || !d || !str) {
02014       ast_free(str);
02015       return -1;
02016    }
02017 
02018    ast_str_set(&str, 0, "%s", digest);
02019 
02020    c = ast_skip_blanks(ast_str_buffer(str));
02021 
02022    if (strncasecmp(c, "Digest ", strlen("Digest "))) {
02023       ast_log(LOG_WARNING, "Missing Digest.\n");
02024       ast_free(str);
02025       return -1;
02026    }
02027    c += strlen("Digest ");
02028 
02029    /* lookup for keys/value pair */
02030    while (c && *c && *(c = ast_skip_blanks(c))) {
02031       /* find key */
02032       for (i = keys; i->key != NULL; i++) {
02033          char *src, *separator;
02034          int unescape = 0;
02035          if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
02036             continue;
02037          }
02038 
02039          /* Found. Skip keyword, take text in quotes or up to the separator. */
02040          c += strlen(i->key);
02041          if (*c == '"') {
02042             src = ++c;
02043             separator = "\"";
02044             unescape = 1;
02045          } else {
02046             src = c;
02047             separator = ",";
02048          }
02049          strsep(&c, separator); /* clear separator and move ptr */
02050          if (unescape) {
02051             ast_unescape_c(src);
02052          }
02053          if (i->field) {
02054             ast_string_field_ptr_set(d, i->field, src);
02055          } else {
02056             /* Special cases that require additional procesing */
02057             if (!strcasecmp(i->key, "algorithm=")) {
02058                if (strcasecmp(src, "MD5")) {
02059                   ast_log(LOG_WARNING, "Digest algorithm: \"%s\" not supported.\n", src);
02060                   ast_free(str);
02061                   return -1;
02062                }
02063             } else if (!strcasecmp(i->key, "qop=") && !strcasecmp(src, "auth")) {
02064                d->qop = 1;
02065             } else if (!strcasecmp(i->key, "nc=")) {
02066                unsigned long u;
02067                if (sscanf(src, "%30lx", &u) != 1) {
02068                   ast_log(LOG_WARNING, "Incorrect Digest nc value: \"%s\".\n", src);
02069                   ast_free(str);
02070                   return -1;
02071                }
02072                ast_string_field_set(d, nc, src);
02073             }
02074          }
02075          break;
02076       }
02077       if (i->key == NULL) { /* not found, try ',' */
02078          strsep(&c, ",");
02079       }
02080    }
02081    ast_free(str);
02082 
02083    /* Digest checkout */
02084    if (ast_strlen_zero(d->realm) || ast_strlen_zero(d->nonce)) {
02085       /* "realm" and "nonce" MUST be always exist */
02086       return -1;
02087    }
02088 
02089    if (!request) {
02090       /* Additional check for Digest response */
02091       if (ast_strlen_zero(d->username) || ast_strlen_zero(d->uri) || ast_strlen_zero(d->response)) {
02092          return -1;
02093       }
02094 
02095       if (pedantic && d->qop && (ast_strlen_zero(d->cnonce) || ast_strlen_zero(d->nc))) {
02096          return -1;
02097       }
02098    }
02099 
02100    return 0;
02101 }

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 1465 of file utils.c.

01466 {
01467    char *dataPut = start;
01468    int inEscape = 0;
01469    int inQuotes = 0;
01470 
01471    for (; *start; start++) {
01472       if (inEscape) {
01473          *dataPut++ = *start;       /* Always goes verbatim */
01474          inEscape = 0;
01475       } else {
01476          if (*start == '\\') {
01477             inEscape = 1;      /* Do not copy \ into the data */
01478          } else if (*start == '\'') {
01479             inQuotes = 1 - inQuotes;   /* Do not copy ' into the data */
01480          } else {
01481             /* Replace , with |, unless in quotes */
01482             *dataPut++ = inQuotes ? *start : ((*start == find) ? replace_with : *start);
01483          }
01484       }
01485    }
01486    if (start != dataPut)
01487       *dataPut = 0;
01488    return dataPut;
01489 }

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 1066 of file utils.c.

References ast_log(), ast_pthread_create_stack(), errno, and LOG_WARNING.

01069 {
01070    unsigned char attr_destroy = 0;
01071    int res;
01072 
01073    if (!attr) {
01074       attr = alloca(sizeof(*attr));
01075       pthread_attr_init(attr);
01076       attr_destroy = 1;
01077    }
01078 
01079    if ((errno = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED)))
01080       ast_log(LOG_WARNING, "pthread_attr_setdetachstate: %s\n", strerror(errno));
01081 
01082    res = ast_pthread_create_stack(thread, attr, start_routine, data, 
01083                                   stacksize, file, caller, line, start_fn);
01084 
01085    if (attr_destroy)
01086       pthread_attr_destroy(attr);
01087 
01088    return res;
01089 }

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 1017 of file utils.c.

References asprintf, ast_log(), ast_malloc, AST_STACKSIZE, dummy_start(), errno, LOG_WARNING, and pthread_create.

Referenced by ast_pthread_create_detached_stack().

01020 {
01021 #if !defined(LOW_MEMORY)
01022    struct thr_arg *a;
01023 #endif
01024 
01025    if (!attr) {
01026       attr = alloca(sizeof(*attr));
01027       pthread_attr_init(attr);
01028    }
01029 
01030 #ifdef __linux__
01031    /* On Linux, pthread_attr_init() defaults to PTHREAD_EXPLICIT_SCHED,
01032       which is kind of useless. Change this here to
01033       PTHREAD_INHERIT_SCHED; that way the -p option to set realtime
01034       priority will propagate down to new threads by default.
01035       This does mean that callers cannot set a different priority using
01036       PTHREAD_EXPLICIT_SCHED in the attr argument; instead they must set
01037       the priority afterwards with pthread_setschedparam(). */
01038    if ((errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED)))
01039       ast_log(LOG_WARNING, "pthread_attr_setinheritsched: %s\n", strerror(errno));
01040 #endif
01041 
01042    if (!stacksize)
01043       stacksize = AST_STACKSIZE;
01044 
01045    if ((errno = pthread_attr_setstacksize(attr, stacksize ? stacksize : AST_STACKSIZE)))
01046       ast_log(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(errno));
01047 
01048 #if !defined(LOW_MEMORY)
01049    if ((a = ast_malloc(sizeof(*a)))) {
01050       a->start_routine = start_routine;
01051       a->data = data;
01052       start_routine = dummy_start;
01053       if (asprintf(&a->name, "%-20s started at [%5d] %s %s()",
01054               start_fn, line, file, caller) < 0) {
01055          ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
01056          a->name = NULL;
01057       }
01058       data = a;
01059    }
01060 #endif /* !LOW_MEMORY */
01061 
01062    return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */
01063 }

long int ast_random ( void   ) 

Definition at line 1441 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_change_source(), ast_rtp_new(), ast_udptl_new_with_bindaddr(), authenticate_request(), build_iv(), build_rand_pad(), build_reply_digest(), calc_metric(), calc_rxstamp(), caldav_write_event(), callno_hash(), create_channel_name(), generate_exchange_uuid(), generate_random_string(), get_trans_id(), gtalk_alloc(), gtalk_create_candidates(), gtalk_new(), handle_incoming(), 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(), and try_firmware().

01442 {
01443    long int res;
01444 #ifdef HAVE_DEV_URANDOM
01445    if (dev_urandom_fd >= 0) {
01446       int read_res = read(dev_urandom_fd, &res, sizeof(res));
01447       if (read_res > 0) {
01448          long int rm = RAND_MAX;
01449          res = res < 0 ? ~res : res;
01450          rm++;
01451          return res % rm;
01452       }
01453    }
01454 #endif
01455 #ifdef linux
01456    res = random();
01457 #else
01458    ast_mutex_lock(&randomlock);
01459    res = random();
01460    ast_mutex_unlock(&randomlock);
01461 #endif
01462    return res;
01463 }

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

Produces SHA1 hash based on input string.

Definition at line 253 of file utils.c.

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

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

00254 {
00255    struct SHA1Context sha;
00256    char *ptr;
00257    int x;
00258    uint8_t Message_Digest[20];
00259 
00260    SHA1Reset(&sha);
00261    
00262    SHA1Input(&sha, (const unsigned char *) input, strlen(input));
00263 
00264    SHA1Result(&sha, Message_Digest);
00265    ptr = output;
00266    for (x = 0; x < 20; x++)
00267       ptr += sprintf(ptr, "%2.2x", Message_Digest[x]);
00268 }

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 1249 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_parse_register_line().

01250 {
01251    char *e;
01252    char *q;
01253 
01254    s = ast_strip(s);
01255    if ((q = strchr(beg_quotes, *s)) && *q != '\0') {
01256       e = s + strlen(s) - 1;
01257       if (*e == *(end_quotes + (q - beg_quotes))) {
01258          s++;
01259          *e = '\0';
01260       }
01261    }
01262 
01263    return s;
01264 }

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 1351 of file utils.c.

References ast_strlen_zero().

Referenced by __ast_http_load(), __ast_udptl_reload(), __init_manager(), _parse(), acf_curlopt_write(), acf_faxopt_write(), acf_transaction_write(), action_agent_logoff(), action_bridge(), action_originate(), action_updateconfig(), actual_load_config(), aji_create_client(), aji_load_config(), aoc_cli_debug_enable(), apply_general_options(), apply_option(), apply_outgoing(), ast_bridge_timelimit(), ast_jb_read_conf(), ast_plc_reload(), ast_tls_read_conf(), autopause2int(), build_device(), build_peer(), build_user(), config_parse_variables(), dahdi_r2_answer(), dahdi_set_dnd(), data_search_cmp_bool(), do_reload(), festival_exec(), func_channel_write_real(), 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_moh_classes(), load_odbc_config(), 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(), osplookup_exec(), parkinglot_config_read(), parse_config(), parse_empty_options(), pbx_load_config(), pbx_load_users(), process_config(), 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(), speex_write(), stackpeek_read(), start_monitor_action(), strings_to_mask(), tds_load_module(), update_common_options(), xmldoc_get_syntax_cmd(), xmldoc_get_syntax_fun(), and xmldoc_get_syntax_manager().

01352 {
01353    if (ast_strlen_zero(s))
01354       return 0;
01355 
01356    /* Determine if this is a true value */
01357    if (!strcasecmp(s, "yes") ||
01358        !strcasecmp(s, "true") ||
01359        !strcasecmp(s, "y") ||
01360        !strcasecmp(s, "t") ||
01361        !strcasecmp(s, "1") ||
01362        !strcasecmp(s, "on"))
01363       return -1;
01364 
01365    return 0;
01366 }

struct timeval ast_tvadd ( struct timeval  a,
struct timeval  b 
)

Returns the sum of two timevals a + b.

Definition at line 1405 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_poll2(), ast_rtp_dtmf_begin(), ast_rtp_dtmf_end_with_duration(), ast_rtp_sendcng(), ast_sched_runq(), ast_smoother_read(), ast_translate(), calc_rxstamp(), 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().

01406 {
01407    /* consistency checks to guarantee usec in 0..999999 */
01408    a = tvfix(a);
01409    b = tvfix(b);
01410    a.tv_sec += b.tv_sec;
01411    a.tv_usec += b.tv_usec;
01412    if (a.tv_usec >= ONE_MILLION) {
01413       a.tv_sec++;
01414       a.tv_usec -= ONE_MILLION;
01415    }
01416    return a;
01417 }

struct timeval ast_tvsub ( struct timeval  a,
struct timeval  b 
)

Returns the difference of two timevals a - b.

Definition at line 1419 of file utils.c.

References ONE_MILLION, and tvfix().

Referenced by __ast_rwlock_timedrdlock(), __ast_rwlock_timedwrlock(), ast_channel_bridge(), ast_poll2(), ast_sched_dump(), ast_translate(), ast_waitfor_nandfds(), calc_rxstamp(), calc_timestamp(), cli_tps_ping(), conf_run(), debug_check_frame_for_silence(), handle_showcalls(), handle_showuptime(), and pri_dchannel().

01420 {
01421    /* consistency checks to guarantee usec in 0..999999 */
01422    a = tvfix(a);
01423    b = tvfix(b);
01424    a.tv_sec -= b.tv_sec;
01425    a.tv_usec -= b.tv_usec;
01426    if (a.tv_usec < 0) {
01427       a.tv_sec-- ;
01428       a.tv_usec += ONE_MILLION;
01429    }
01430    return a;
01431 }

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 1285 of file utils.c.

Referenced by ast_parse_digest().

01286 {
01287    char c, *ret, *dst;
01288 
01289    if (src == NULL)
01290       return NULL;
01291    for (ret = dst = src; (c = *src++); *dst++ = c ) {
01292       if (c != '\\')
01293          continue;   /* copy char at the end of the loop */
01294       switch ((c = *src++)) {
01295       case '\0':  /* special, trailing '\' */
01296          c = '\\';
01297          break;
01298       case 'b':   /* backspace */
01299          c = '\b';
01300          break;
01301       case 'f':   /* form feed */
01302          c = '\f';
01303          break;
01304       case 'n':
01305          c = '\n';
01306          break;
01307       case 'r':
01308          c = '\r';
01309          break;
01310       case 't':
01311          c = '\t';
01312          break;
01313       }
01314       /* default, use the char literally */
01315    }
01316    *dst = '\0';
01317    return ret;
01318 }

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 1266 of file utils.c.

Referenced by sip_cli_notify().

01267 {
01268    char *e;
01269    char *work = s;
01270 
01271    while ((e = strchr(work, ';'))) {
01272       if ((e > work) && (*(e-1) == '\\')) {
01273          memmove(e - 1, e, strlen(e) + 1);
01274          work = e;
01275       } else {
01276          work = e + 1;
01277       }
01278    }
01279 
01280    return s;
01281 }

void ast_uri_decode ( char *  s  ) 

Decode URI, URN, URL (overwrite string).

Parameters:
s String to be decoded

Definition at line 454 of file utils.c.

Referenced by acf_curl_helper(), config_curl(), get_destination(), get_name_and_number(), get_refer_info(), handle_request_invite(), http_decode(), parse_moved_contact(), realtime_curl(), realtime_multi_curl(), sip_new(), sip_uri_cmp(), and uridecode().

00455 {
00456    char *o;
00457    unsigned int tmp;
00458 
00459    for (o = s; *s; s++, o++) {
00460       if (*s == '%' && s[1] != '\0' && s[2] != '\0' && sscanf(s + 1, "%2x", &tmp) == 1) {
00461          /* have '%', two chars and correct parsing */
00462          *o = tmp;
00463          s += 2;  /* Will be incremented once more when we break out */
00464       } else /* all other cases, just copy */
00465          *o = *s;
00466    }
00467    *o = '\0';
00468 }

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

Turn text string to URI-encoded XX version.

Note:
At this point, this function is encoding agnostic; it does not check whether it is fed legal UTF-8. We escape control characters (-), '', and all characters above 0x7F. If do_special_char == 1 we will convert all characters except alnum and mark. 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.

Definition at line 390 of file utils.c.

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

00391 {
00392    const char *ptr  = string; /* Start with the string */
00393    char *out = outbuf;
00394    const char *mark = "-_.!~*'()"; /* no encode set, RFC 2396 section 2.3, RFC 3261 sec 25 */
00395 
00396    while (*ptr && out - outbuf < buflen - 1) {
00397       if ((const signed char) *ptr < 32 || *ptr == 0x7f || *ptr == '%' ||
00398             (do_special_char &&
00399             !(*ptr >= '0' && *ptr <= '9') &&      /* num */
00400             !(*ptr >= 'A' && *ptr <= 'Z') &&      /* ALPHA */
00401             !(*ptr >= 'a' && *ptr <= 'z') &&      /* alpha */
00402             !strchr(mark, *ptr))) {               /* mark set */
00403          if (out - outbuf >= buflen - 3) {
00404             break;
00405          }
00406 
00407          out += sprintf(out, "%%%02X", (unsigned char) *ptr);
00408       } else {
00409          *out = *ptr;   /* Continue copying the string */
00410          out++;
00411       }
00412       ptr++;
00413    }
00414 
00415    if (buflen) {
00416       *out = '\0';
00417    }
00418 
00419    return outbuf;
00420 }

int ast_utils_init ( void   ) 

Definition at line 1967 of file utils.c.

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

Referenced by main().

01968 {
01969 #ifdef HAVE_DEV_URANDOM
01970    dev_urandom_fd = open("/dev/urandom", O_RDONLY);
01971 #endif
01972    base64_init();
01973 #ifdef DEBUG_THREADS
01974 #if !defined(LOW_MEMORY)
01975    ast_cli_register_multiple(utils_cli, ARRAY_LEN(utils_cli));
01976 #endif
01977 #endif
01978    return 0;
01979 }

char* ast_utils_which ( const char *  binary,
char *  fullpath,
size_t  fullpath_size 
)

Resolve a binary to a full pathname.

Parameters:
binary Name of the executable to resolve
fullpath Buffer to hold the complete pathname
fullpath_size Size of fullpath
Return values:
NULL binary was not found or the environment variable PATH is not set
Returns:
fullpath

Definition at line 2119 of file utils.c.

References ast_strdupa, and strsep().

Referenced by ast_bt_get_symbols().

02120 {
02121    const char *envPATH = getenv("PATH");
02122    char *tpath, *path;
02123    struct stat unused;
02124    if (!envPATH) {
02125       return NULL;
02126    }
02127    tpath = ast_strdupa(envPATH);
02128    while ((path = strsep(&tpath, ":"))) {
02129       snprintf(fullpath, fullpath_size, "%s/%s", path, binary);
02130       if (!stat(fullpath, &unused)) {
02131          return fullpath;
02132       }
02133    }
02134    return NULL;
02135 }

int ast_wait_for_input ( int  fd,
int  ms 
)

Definition at line 1091 of file utils.c.

References ast_poll.

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

01092 {
01093    struct pollfd pfd[1];
01094    memset(pfd, 0, sizeof(pfd));
01095    pfd[0].fd = fd;
01096    pfd[0].events = POLLIN|POLLPRI;
01097    return ast_poll(pfd, 1, ms);
01098 }

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

Definition at line 1100 of file utils.c.

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

Referenced by ast_careful_fwrite(), and ast_carefulwrite().

01101 {
01102    struct pollfd pfd = {
01103       .fd = fd,
01104       .events = POLLOUT,
01105    };
01106    int res;
01107    struct timeval start = ast_tvnow();
01108    int elapsed = 0;
01109 
01110    /* poll() until the fd is writable without blocking */
01111    while ((res = ast_poll(&pfd, 1, timeoutms - elapsed)) <= 0) {
01112       if (res == 0) {
01113          /* timed out. */
01114 #ifndef STANDALONE
01115          ast_debug(1, "Timed out trying to write\n");
01116 #endif
01117          return -1;
01118       } else if (res == -1) {
01119          /* poll() returned an error, check to see if it was fatal */
01120 
01121          if (errno == EINTR || errno == EAGAIN) {
01122             elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01123             if (elapsed >= timeoutms) {
01124                return -1;
01125             }
01126             /* This was an acceptable error, go back into poll() */
01127             continue;
01128          }
01129 
01130          /* Fatal error, bail. */
01131          ast_log(LOG_ERROR, "poll returned error: %s\n", strerror(errno));
01132 
01133          return -1;
01134       }
01135       elapsed = ast_tvdiff_ms(ast_tvnow(), start);
01136       if (elapsed >= timeoutms) {
01137          return -1;
01138       }
01139    }
01140 
01141    return 0;
01142 }

static void base64_init ( void   )  [static]

Definition at line 354 of file utils.c.

Referenced by ast_utils_init().

00355 {
00356    int x;
00357    memset(b2a, -1, sizeof(b2a));
00358    /* Initialize base-64 Conversion table */
00359    for (x = 0; x < 26; x++) {
00360       /* A-Z */
00361       base64[x] = 'A' + x;
00362       b2a['A' + x] = x;
00363       /* a-z */
00364       base64[x + 26] = 'a' + x;
00365       b2a['a' + x] = x + 26;
00366       /* 0-9 */
00367       if (x < 10) {
00368          base64[x + 52] = '0' + x;
00369          b2a['0' + x] = x + 52;
00370       }
00371    }
00372    base64[62] = '+';
00373    base64[63] = '/';
00374    b2a[(int)'+'] = 62;
00375    b2a[(int)'/'] = 63;
00376 }

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

Definition at line 973 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, lock_info, thr_arg::name, pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock, thr_arg::start_routine, and strdup.

Referenced by ast_pthread_create_stack().

00974 {
00975    void *ret;
00976    struct thr_arg a = *((struct thr_arg *) data);  /* make a local copy */
00977 #ifdef DEBUG_THREADS
00978    struct thr_lock_info *lock_info;
00979    pthread_mutexattr_t mutex_attr;
00980 #endif
00981 
00982    /* note that even though data->name is a pointer to allocated memory,
00983       we are not freeing it here because ast_register_thread is going to
00984       keep a copy of the pointer and then ast_unregister_thread will
00985       free the memory
00986    */
00987    ast_free(data);
00988    ast_register_thread(a.name);
00989    pthread_cleanup_push(ast_unregister_thread, (void *) pthread_self());
00990 
00991 #ifdef DEBUG_THREADS
00992    if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info))))
00993       return NULL;
00994 
00995    lock_info->thread_id = pthread_self();
00996    lock_info->thread_name = strdup(a.name);
00997 
00998    pthread_mutexattr_init(&mutex_attr);
00999    pthread_mutexattr_settype(&mutex_attr, AST_MUTEX_KIND);
01000    pthread_mutex_init(&lock_info->lock, &mutex_attr);
01001    pthread_mutexattr_destroy(&mutex_attr);
01002 
01003    pthread_mutex_lock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */
01004    AST_LIST_INSERT_TAIL(&lock_infos, lock_info, entry);
01005    pthread_mutex_unlock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */
01006 #endif /* DEBUG_THREADS */
01007 
01008    ret = a.start_routine(a.data);
01009 
01010    pthread_cleanup_pop(1);
01011 
01012    return ret;
01013 }

static size_t optimal_alloc_size ( size_t  size  )  [static]

Definition at line 1530 of file utils.c.

References ALLOCATOR_OVERHEAD.

Referenced by __ast_calloc_with_stringfields(), and add_string_pool().

01531 {
01532    unsigned int count;
01533 
01534    size += ALLOCATOR_OVERHEAD;
01535 
01536    for (count = 1; size; size >>= 1, count++);
01537 
01538    return (1 << count) - ALLOCATOR_OVERHEAD;
01539 }

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

Definition at line 1390 of file utils.c.

References ast_log(), LOG_WARNING, and ONE_MILLION.

Referenced by ast_tvadd(), and ast_tvsub().

01391 {
01392    if (a.tv_usec >= ONE_MILLION) {
01393       ast_log(LOG_WARNING, "warning too large timestamp %ld.%ld\n",
01394          (long)a.tv_sec, (long int) a.tv_usec);
01395       a.tv_sec += a.tv_usec / ONE_MILLION;
01396       a.tv_usec %= ONE_MILLION;
01397    } else if (a.tv_usec < 0) {
01398       ast_log(LOG_WARNING, "warning negative timestamp %ld.%ld\n",
01399          (long)a.tv_sec, (long int) a.tv_usec);
01400       a.tv_usec = 0;
01401    }
01402    return a;
01403 }


Variable Documentation

ast_string_field __ast_string_field_empty = __ast_string_field_empty_buffer.string

Definition at line 1526 of file utils.c.

Referenced by __ast_calloc_with_stringfields(), and __ast_string_field_ptr_build_va().

struct { ... } __ast_string_field_empty_buffer [static]

ast_string_field_allocation allocation

Definition at line 1522 of file utils.c.

Referenced by __ast_calloc_with_stringfields().

char b2a[256] [static]

Definition at line 71 of file utils.c.

char base64[64] [static]

Definition at line 70 of file utils.c.

Referenced by aji_start_sasl().

int dev_urandom_fd [static]

Definition at line 482 of file utils.c.

ast_mutex_t fetchadd_m = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 } [static]

Definition at line 1857 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 73 of file utils.c.

Referenced by ast_inet_ntoa().

ast_mutex_t randomlock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 } [static]

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

Definition at line 1438 of file utils.c.

Referenced by ast_random().

char string[1]

Definition at line 1523 of file utils.c.

Referenced by filter(), pp_each_user_helper(), and private_enum_init().


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