Sat Mar 10 01:55:49 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 1524 of file utils.c.

Referenced by optimal_alloc_size().

#define AST_API_MODULE

Definition at line 63 of file utils.c.

#define AST_API_MODULE

Definition at line 63 of file utils.c.

#define AST_API_MODULE

Definition at line 63 of file utils.c.

#define AST_API_MODULE

Definition at line 63 of file utils.c.

#define AST_API_MODULE

Definition at line 63 of file utils.c.

#define AST_API_MODULE

Definition at line 63 of file utils.c.

#define AST_API_MODULE

Definition at line 63 of file utils.c.

#define ONE_MILLION   1000000

Definition at line 1381 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 1805 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.

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

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

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

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

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

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

References __ast_string_field_ptr_build_va().

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

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

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

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

References AST_STRING_FIELD_ALLOCATION, and ast_string_field_mgr::last_alloc.

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

void __ast_string_field_release_active ( struct ast_string_field_pool pool_head,
const ast_string_field  ptr 
)

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

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

static void __init_inet_ntoa_buf ( void   )  [static]

Definition at line 69 of file utils.c.

00086 {

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

Definition at line 2100 of file utils.c.

References MALLOC_FAILURE_MSG, and vasprintf.

02101 {
02102    int res;
02103    va_list ap;
02104 
02105    va_start(ap, fmt);
02106    if ((res = vasprintf(ret, fmt, ap)) == -1) {
02107       MALLOC_FAILURE_MSG;
02108    }
02109    va_end(ap);
02110 
02111    return res;
02112 }

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

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

int ast_atomic_fetchadd_int_slow ( volatile int *  p,
int  v 
)

Definition at line 1857 of file utils.c.

References ast_mutex_lock, ast_mutex_unlock, and fetchadd_m.

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

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

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

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

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

00346 {
00347    return ast_base64encode_full(dst, src, srclen, max, 0);
00348 }

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

encode text to BASE64 coding

Definition at line 294 of file utils.c.

Referenced by ast_base64encode().

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

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

01336 {
01337    va_list ap;
01338    int result;
01339 
01340    va_start(ap, fmt);
01341    result = ast_build_string_va(buffer, space, fmt, ap);
01342    va_end(ap);
01343 
01344    return result;
01345 }

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

Referenced by ast_build_string().

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

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

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

Referenced by send_string().

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

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

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

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

References ast_log(), and LOG_WARNING.

Referenced by ast_netsock_bindaddr().

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

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

Referenced by initreqprep().

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

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

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

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

get values from config variables.

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

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

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

get values from config variables.

Definition at line 1870 of file utils.c.

References ast_strlen_zero().

Referenced by acf_strftime().

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

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

Thread-safe gethostbyname function to use in Asterisk.

Definition at line 183 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(), rpt_exec(), and set_config().

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

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 467 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(), rpt_exec(), 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().

00468 {
00469    char *buf;
00470 
00471    if (!(buf = ast_threadstorage_get(&inet_ntoa_buf, INET_ADDRSTRLEN)))
00472       return "";
00473 
00474    return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
00475 }

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

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

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

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

Produces MD5 hash based on input string.

Definition at line 233 of file utils.c.

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

Referenced by build_reply_digest(), and md5().

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

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 1930 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(), reload_logger(), remove_from_queue(), setup_privacy_args(), sms_nextoutgoing(), sms_writefile(), testclient_exec(), testserver_exec(), and write_history().

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

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 1987 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, str, ast_http_digest::uri, and ast_http_digest::username.

01987                                                                                                {
01988    int i;
01989    char *c, key[512], val[512];
01990    struct ast_str *str = ast_str_create(16);
01991 
01992    if (ast_strlen_zero(digest) || !d || !str) {
01993       ast_free(str);
01994       return -1;
01995    }
01996 
01997    ast_str_set(&str, 0, "%s", digest);
01998 
01999    c = ast_skip_blanks(ast_str_buffer(str));
02000 
02001    if (strncasecmp(c, "Digest ", strlen("Digest "))) {
02002       ast_log(LOG_WARNING, "Missing Digest.\n");
02003       ast_free(str);
02004       return -1;
02005    }
02006    c += strlen("Digest ");
02007 
02008    /* lookup for keys/value pair */
02009    while (*c && *(c = ast_skip_blanks(c))) {
02010       /* find key */
02011       i = 0;
02012       while (*c && *c != '=' && *c != ',' && !isspace(*c)) {
02013          key[i++] = *c++;
02014       }
02015       key[i] = '\0';
02016       c = ast_skip_blanks(c);
02017       if (*c == '=') {
02018          c = ast_skip_blanks(++c);
02019          i = 0;
02020          if (*c == '\"') {
02021             /* in quotes. Skip first and look for last */
02022             c++;
02023             while (*c && *c != '\"') {
02024                if (*c == '\\' && c[1] != '\0') { /* unescape chars */
02025                   c++;
02026                }
02027                val[i++] = *c++;
02028             }
02029          } else {
02030             /* token */
02031             while (*c && *c != ',' && !isspace(*c)) {
02032                val[i++] = *c++;
02033             }
02034          }
02035          val[i] = '\0';
02036       }
02037 
02038       while (*c && *c != ',') {
02039          c++;
02040       }
02041       if (*c) {
02042          c++;
02043       }
02044 
02045       if (!strcasecmp(key, "username")) {
02046          ast_string_field_set(d, username, val);
02047       } else if (!strcasecmp(key, "realm")) {
02048          ast_string_field_set(d, realm, val);
02049       } else if (!strcasecmp(key, "nonce")) {
02050          ast_string_field_set(d, nonce, val);
02051       } else if (!strcasecmp(key, "uri")) {
02052          ast_string_field_set(d, uri, val);
02053       } else if (!strcasecmp(key, "domain")) {
02054          ast_string_field_set(d, domain, val);
02055       } else if (!strcasecmp(key, "response")) {
02056          ast_string_field_set(d, response, val);
02057       } else if (!strcasecmp(key, "algorithm")) {
02058          if (strcasecmp(val, "MD5")) {
02059             ast_log(LOG_WARNING, "Digest algorithm: \"%s\" not supported.\n", val);
02060             return -1;
02061          }
02062       } else if (!strcasecmp(key, "cnonce")) {
02063          ast_string_field_set(d, cnonce, val);
02064       } else if (!strcasecmp(key, "opaque")) {
02065          ast_string_field_set(d, opaque, val);
02066       } else if (!strcasecmp(key, "qop") && !strcasecmp(val, "auth")) {
02067          d->qop = 1;
02068       } else if (!strcasecmp(key, "nc")) {
02069          unsigned long u;
02070          if (sscanf(val, "%30lx", &u) != 1) {
02071             ast_log(LOG_WARNING, "Incorrect Digest nc value: \"%s\".\n", val);
02072             return -1;
02073          }
02074          ast_string_field_set(d, nc, val);
02075       }
02076    }
02077    ast_free(str);
02078 
02079    /* Digest checkout */
02080    if (ast_strlen_zero(d->realm) || ast_strlen_zero(d->nonce)) {
02081       /* "realm" and "nonce" MUST be always exist */
02082       return -1;
02083    }
02084 
02085    if (!request) {
02086       /* Additional check for Digest response */
02087       if (ast_strlen_zero(d->username) || ast_strlen_zero(d->uri) || ast_strlen_zero(d->response)) {
02088          return -1;
02089       }
02090 
02091       if (pedantic && d->qop && (ast_strlen_zero(d->cnonce) || ast_strlen_zero(d->nc))) {
02092          return -1;
02093       }
02094    }
02095 
02096    return 0;
02097 }

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

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

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

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

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

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

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

long int ast_random ( void   ) 

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

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

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

Produces SHA1 hash based on input string.

Definition at line 249 of file utils.c.

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

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

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

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

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

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 1347 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(), connect_link(), 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(), 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(), 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(), 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().

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

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

Returns the sum of two timevals a + b.

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

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

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

Returns the difference of two timevals a - b.

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

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

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

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

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

Referenced by sip_cli_notify().

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

void ast_uri_decode ( char *  s  ) 

Decode URI, URN, URL (overwrite string).

Parameters:
s String to be decoded

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

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

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

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

int ast_utils_init ( void   ) 

Definition at line 1965 of file utils.c.

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

Referenced by main().

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

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

References ast_strdupa, and strsep().

Referenced by ast_bt_get_symbols().

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

int ast_wait_for_input ( int  fd,
int  ms 
)

Definition at line 1087 of file utils.c.

References ast_poll.

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

01088 {
01089    struct pollfd pfd[1];
01090    memset(pfd, 0, sizeof(pfd));
01091    pfd[0].fd = fd;
01092    pfd[0].events = POLLIN|POLLPRI;
01093    return ast_poll(pfd, 1, ms);
01094 }

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

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

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

static void base64_init ( void   )  [static]

Definition at line 350 of file utils.c.

Referenced by ast_utils_init().

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

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

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

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

static size_t optimal_alloc_size ( size_t  size  )  [static]

Definition at line 1526 of file utils.c.

References ALLOCATOR_OVERHEAD.

Referenced by __ast_calloc_with_stringfields(), and add_string_pool().

01527 {
01528    unsigned int count;
01529 
01530    size += ALLOCATOR_OVERHEAD;
01531 
01532    for (count = 1; size; size >>= 1, count++);
01533 
01534    return (1 << count) - ALLOCATOR_OVERHEAD;
01535 }

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

Definition at line 1386 of file utils.c.

References ast_log(), LOG_WARNING, and ONE_MILLION.

Referenced by ast_tvadd(), and ast_tvsub().

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


Variable Documentation

ast_string_field __ast_string_field_empty = __ast_string_field_empty_buffer.string

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

Referenced by __ast_calloc_with_stringfields().

char b2a[256] [static]

Definition at line 67 of file utils.c.

char base64[64] [static]

Definition at line 66 of file utils.c.

Referenced by aji_start_sasl().

int dev_urandom_fd [static]

Definition at line 478 of file utils.c.

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

Definition at line 1855 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 69 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 1434 of file utils.c.

Referenced by ast_random().

char string[1]

Definition at line 1519 of file utils.c.

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


Generated on Sat Mar 10 01:55:50 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7