Thu Feb 9 06:37:32 2012

Asterisk developer's documentation


utils.c File Reference

Utility functions. More...

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

Include dependency graph for utils.c:

Go to the source code of this file.

Data Structures

struct  thr_arg

Defines

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

Functions

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

Variables

ast_string_field __ast_string_field_empty = __ast_string_field_empty_buffer.string
struct {
   ast_string_field_allocation   allocation
   char   string [1]
__ast_string_field_empty_buffer
static ast_mutex_t __mutex = { PTHREAD_MUTEX_INITIALIZER , NULL, 1 }
struct ast_flags ast_uri_http = {AST_URI_UNRESERVED}
struct ast_flags ast_uri_http_legacy = {AST_URI_LEGACY_SPACE | AST_URI_UNRESERVED}
struct ast_flags ast_uri_sip_user = {AST_URI_UNRESERVED | AST_URI_SIP_USER_UNRESERVED}
static char b2a [256]
static char base64 [64]
static ast_mutex_t fetchadd_m = { PTHREAD_MUTEX_INITIALIZER , 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_MUTEX_INITIALIZER , 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 1530 of file utils.c.

Referenced by optimal_alloc_size().

#define AST_API_MODULE

Definition at line 70 of file utils.c.

#define AST_API_MODULE

Definition at line 70 of file utils.c.

#define AST_API_MODULE

Definition at line 70 of file utils.c.

#define AST_API_MODULE

Definition at line 70 of file utils.c.

#define AST_API_MODULE

Definition at line 70 of file utils.c.

#define AST_API_MODULE

Definition at line 70 of file utils.c.

#define AST_API_MODULE

Definition at line 70 of file utils.c.

#define ERANGE   34

duh? ERANGE value copied from web...

Definition at line 80 of file utils.c.

Referenced by gethostbyname_r(), and reload_queue_members().

#define ONE_MILLION   1000000

Definition at line 1387 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 1809 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.

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

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

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

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

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

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

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

References __ast_string_field_ptr_build_va().

01801 {
01802    va_list ap;
01803 
01804    va_start(ap, format);
01805    __ast_string_field_ptr_build_va(mgr, pool_head, ptr, format, ap);
01806    va_end(ap);
01807 }

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  ap 
)

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

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

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

References AST_STRING_FIELD_ALLOCATION, and ast_string_field_mgr::last_alloc.

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

void __ast_string_field_release_active ( struct ast_string_field_pool pool_head,
const ast_string_field  ptr 
)

Definition at line 1713 of file utils.c.

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

Referenced by __ast_string_field_ptr_build_va().

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

static void __init_inet_ntoa_buf ( void   )  [static]

Definition at line 76 of file utils.c.

00093 {

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

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

Referenced by __ast_string_field_alloc_space(), and __ast_string_field_init().

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

int ast_atomic_fetchadd_int_slow ( volatile int *  p,
int  v 
)

Definition at line 1861 of file utils.c.

References ast_mutex_lock, ast_mutex_unlock, and fetchadd_m.

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

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

decode BASE64 encoded text

Decode data from base64.

Definition at line 274 of file utils.c.

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

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

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

00353 {
00354    return ast_base64encode_full(dst, src, srclen, max, 0);
00355 }

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

encode text to BASE64 coding

Definition at line 301 of file utils.c.

Referenced by ast_base64encode().

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

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

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

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

Referenced by ast_build_string().

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

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

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

Referenced by send_string().

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

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 1155 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(), ast_cli(), and cleanup_module().

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

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

References ast_log(), and LOG_WARNING.

Referenced by ast_netsock_bindaddr(), and reload_config().

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

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

Referenced by initreqprep().

00444 {
00445    const char *ptr  = string;
00446    char *out = outbuf;
00447    char *allow = "\t\v !"; /* allow LWS (minus \r and \n) and "!" */
00448 
00449    while (*ptr && out - outbuf < buflen - 1) {
00450       if (!(strchr(allow, *ptr))
00451          && !(*ptr >= '#' && *ptr <= '[') /* %x23 - %x5b */
00452          && !(*ptr >= ']' && *ptr <= '~') /* %x5d - %x7e */
00453          && !((unsigned char) *ptr > 0x7f)) {             /* UTF8-nonascii */
00454 
00455          if (out - outbuf >= buflen - 2) {
00456             break;
00457          }
00458          out += sprintf(out, "\\%c", (unsigned char) *ptr);
00459       } else {
00460          *out = *ptr;
00461          out++;
00462       }
00463       ptr++;
00464    }
00465 
00466    if (buflen) {
00467       *out = '\0';
00468    }
00469 
00470    return outbuf;
00471 }

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 1370 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(), reload_config(), rtp_reload(), run_agi(), set_config(), set_insecure_flags(), sla_build_trunk(), and strings_to_mask().

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

int ast_get_tid ( void   ) 

Get current thread ID.

Parameters:
None 
Returns:
the ID if platform is supported, else -1

Definition at line 2119 of file utils.c.

Referenced by ast_log(), and ast_register_thread().

02120 {
02121    int ret = -1;
02122 #if defined (__linux) && defined(SYS_gettid)
02123    ret = syscall(SYS_gettid); /* available since Linux 1.4.11 */
02124 #elif defined(__sun)
02125    ret = pthread_self();
02126 #elif defined(__APPLE__)
02127    ret = mach_thread_self();
02128    mach_port_deallocate(mach_task_self(), ret);
02129 #elif defined(__FreeBSD__) && defined(HAVE_SYS_THR_H)
02130    long lwpid;
02131    thr_self(&lwpid); /* available since sys/thr.h creation 2003 */
02132    ret = lwpid;
02133 #endif
02134    return ret;
02135 }

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

get values from config variables.

Definition at line 1901 of file utils.c.

References ast_strlen_zero().

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

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

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

get values from config variables.

Definition at line 1874 of file utils.c.

References ast_strlen_zero().

Referenced by acf_strftime().

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

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

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

Thread-safe gethostbyname function to use in Asterisk.

Definition at line 190 of file utils.c.

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

Referenced by app_exec(), ast_parse_arg(), build_peer(), config_load(), config_parse_variables(), create_addr(), festival_exec(), gtalk_load_config(), 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().

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

const char* ast_inet_ntoa ( struct in_addr  ia  ) 

ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa

thread-safe replacement for inet_ntoa().

Definition at line 473 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(), auth_http_callback(), authenticate(), calltoken_required(), check_access(), config_load(), create_client(), 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(), generic_http_callback(), get_input(), gtalk_show_settings(), 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_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(), reload_config(), resend_response(), rpt_exec(), sched_delay_remove(), send_packet(), send_raw_client(), send_request(), send_response(), send_trunk(), session_do(), 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().

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

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

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

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

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

Produce 32 char MD5 hash of value.

Produces MD5 hash based on input string.

Definition at line 240 of file utils.c.

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

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

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

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

References ast_strdupa, errno, and len().

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

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

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

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

Referenced by auth_http_callback().

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

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

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

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

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

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

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

Referenced by ast_pthread_create_detached_stack().

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

long int ast_random ( void   ) 

Definition at line 1443 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(), auth_http_callback(), authenticate_request(), build_gateway(), 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(), generic_http_callback(), 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(), mktemp_internal(), moh_files_alloc(), multicast_rtp_new(), ogg_vorbis_rewrite(), osp_create_uuid(), page_exec(), park_space_reserve(), process_weights(), rec_request(), reg_source_db(), registry_authrequest(), reqprep(), say_periodic_announcement(), sendmail(), set_nonce_randdata(), sip_alloc(), socket_read(), start_rtp(), stun_req_id(), transmit_invite(), transmit_register(), transmit_response_using_temp(), try_calling(), and try_firmware().

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

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

Produce 40 char SHA1 hash of value.

Produces SHA1 hash based on input string.

Definition at line 256 of file utils.c.

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

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

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

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

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

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 1353 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_readconfig(), ast_tls_read_conf(), autopause2int(), build_device(), build_gateway(), build_peer(), build_user(), config_parse_variables(), connect_link(), 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_format_config(), load_module(), load_modules(), load_moh_classes(), load_odbc_config(), load_rpt_vars(), local_ast_moh_start(), login_exec(), manager_add_queue_member(), manager_mutestream(), manager_pause_queue_member(), message_template_build(), misdn_answer(), new_realtime_sqlite3_db(), odbc_load_module(), osp_load(), osplookup_exec(), parkinglot_config_read(), parse_config(), parse_empty_options(), pbx_load_config(), pbx_load_users(), process_config(), process_dahdi(), process_echocancel(), queue_set_global_params(), queue_set_param(), read_agent_config(), realtime_directory(), reload_config(), rt_handle_member_record(), rtp_reload(), run_startup_commands(), search_directory(), search_directory_sub(), set_active(), set_config(), sla_load_config(), smdi_load(), speex_write(), start_monitor_action(), strings_to_mask(), tds_load_module(), and update_common_options().

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

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

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

Returns the difference of two timevals a - b.

Definition at line 1421 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(), and handle_showuptime().

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

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

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

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

Referenced by sip_cli_notify().

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

void ast_uri_decode ( char *  s,
struct ast_flags  spec 
)

Decode URI, URN, URL (overwrite string).

Note:
The ast_uri_http_legacy decode spec flag will cause this function to decode '+' as ' '.
Parameters:
s string to be decoded
spec flags describing how the decoding should be performed

Definition at line 424 of file utils.c.

References ast_test_flag, and AST_URI_LEGACY_SPACE.

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

00425 {
00426    char *o;
00427    unsigned int tmp;
00428 
00429    for (o = s; *s; s++, o++) {
00430       if (ast_test_flag(&spec, AST_URI_LEGACY_SPACE) && *s == '+') {
00431          /* legacy mode, decode '+' as space */
00432          *o = ' ';
00433       } else if (*s == '%' && s[1] != '\0' && s[2] != '\0' && sscanf(s + 1, "%2x", &tmp) == 1) {
00434          /* have '%', two chars and correct parsing */
00435          *o = tmp;
00436          s += 2;  /* Will be incremented once more when we break out */
00437       } else /* all other cases, just copy */
00438          *o = *s;
00439    }
00440    *o = '\0';
00441 }

char* ast_uri_encode ( const char *  string,
char *  outbuf,
int  buflen,
struct ast_flags  spec 
)

Turn text string to URI-encoded XX version.

This function encodes characters according to the rules presented in RFC 2396 and/or RFC 3261 section 19.1.2 and section 25.1.

Outbuf needs to have more memory allocated than the instring to have room for the expansion. Every byte that is converted is replaced by three ASCII characters.

Parameters:
string string to be converted
outbuf resulting encoded string
buflen size of output buffer
spec flags describing how the encoding should be performed
Returns:
a pointer to the uri encoded string

Definition at line 385 of file utils.c.

References ast_test_flag, AST_URI_ALPHANUM, AST_URI_LEGACY_SPACE, AST_URI_MARK, and AST_URI_SIP_USER_UNRESERVED.

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

00386 {
00387    const char *ptr  = string; /* Start with the string */
00388    char *out = outbuf;
00389    const char *mark = "-_.!~*'()"; /* no encode set, RFC 2396 section 2.3, RFC 3261 sec 25 */
00390    const char *user_unreserved = "&=+$,;?/"; /* user-unreserved set, RFC 3261 sec 25 */
00391 
00392    while (*ptr && out - outbuf < buflen - 1) {
00393       if (ast_test_flag(&spec, AST_URI_LEGACY_SPACE) && *ptr == ' ') {
00394          /* for legacy encoding, encode spaces as '+' */
00395          *out = '+';
00396          out++;
00397       } else if (!(ast_test_flag(&spec, AST_URI_MARK)
00398             && strchr(mark, *ptr))
00399          && !(ast_test_flag(&spec, AST_URI_ALPHANUM)
00400             && ((*ptr >= '0' && *ptr <= '9')
00401             || (*ptr >= 'A' && *ptr <= 'Z')
00402             || (*ptr >= 'a' && *ptr <= 'z')))
00403          && !(ast_test_flag(&spec, AST_URI_SIP_USER_UNRESERVED)
00404             && strchr(user_unreserved, *ptr))) {
00405 
00406          if (out - outbuf >= buflen - 3) {
00407             break;
00408          }
00409          out += sprintf(out, "%%%02X", (unsigned char) *ptr);
00410       } else {
00411          *out = *ptr;   /* Continue copying the string */
00412          out++;
00413       }
00414       ptr++;
00415    }
00416 
00417    if (buflen) {
00418       *out = '\0';
00419    }
00420 
00421    return outbuf;
00422 }

int ast_utils_init ( void   ) 

Definition at line 1969 of file utils.c.

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

Referenced by main().

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

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

References ast_strdupa, and strsep().

02138 {
02139    const char *envPATH = getenv("PATH");
02140    char *tpath, *path;
02141    struct stat unused;
02142    if (!envPATH) {
02143       return NULL;
02144    }
02145    tpath = ast_strdupa(envPATH);
02146    while ((path = strsep(&tpath, ":"))) {
02147       snprintf(fullpath, fullpath_size, "%s/%s", path, binary);
02148       if (!stat(fullpath, &unused)) {
02149          return fullpath;
02150       }
02151    }
02152    return NULL;
02153 }

int ast_wait_for_input ( int  fd,
int  ms 
)

Definition at line 1093 of file utils.c.

References ast_poll.

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

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

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

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

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

static void base64_init ( void   )  [static]

Definition at line 357 of file utils.c.

Referenced by ast_utils_init().

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

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

Definition at line 975 of file utils.c.

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

Referenced by ast_pthread_create_stack().

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

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

Reentrant replacement for gethostbyname for BSD-based systems.

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

Definition at line 90 of file utils.c.

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

Referenced by ast_gethostbyname().

00093 {
00094    int hsave;
00095    struct hostent *ph;
00096    ast_mutex_lock(&__mutex); /* begin critical area */
00097    hsave = h_errno;
00098 
00099    ph = gethostbyname(name);
00100    *h_errnop = h_errno; /* copy h_errno to *h_herrnop */
00101    if (ph == NULL) {
00102       *result = NULL;
00103    } else {
00104       char **p, **q;
00105       char *pbuf;
00106       int nbytes = 0;
00107       int naddr = 0, naliases = 0;
00108       /* determine if we have enough space in buf */
00109 
00110       /* count how many addresses */
00111       for (p = ph->h_addr_list; *p != 0; p++) {
00112          nbytes += ph->h_length; /* addresses */
00113          nbytes += sizeof(*p); /* pointers */
00114          naddr++;
00115       }
00116       nbytes += sizeof(*p); /* one more for the terminating NULL */
00117 
00118       /* count how many aliases, and total length of strings */
00119       for (p = ph->h_aliases; *p != 0; p++) {
00120          nbytes += (strlen(*p)+1); /* aliases */
00121          nbytes += sizeof(*p);  /* pointers */
00122          naliases++;
00123       }
00124       nbytes += sizeof(*p); /* one more for the terminating NULL */
00125 
00126       /* here nbytes is the number of bytes required in buffer */
00127       /* as a terminator must be there, the minimum value is ph->h_length */
00128       if (nbytes > buflen) {
00129          *result = NULL;
00130          ast_mutex_unlock(&__mutex); /* end critical area */
00131          return ERANGE; /* not enough space in buf!! */
00132       }
00133 
00134       /* There is enough space. Now we need to do a deep copy! */
00135       /* Allocation in buffer:
00136          from [0] to [(naddr-1) * sizeof(*p)]:
00137          pointers to addresses
00138          at [naddr * sizeof(*p)]:
00139          NULL
00140          from [(naddr+1) * sizeof(*p)] to [(naddr+naliases) * sizeof(*p)] :
00141          pointers to aliases
00142          at [(naddr+naliases+1) * sizeof(*p)]:
00143          NULL
00144          then naddr addresses (fixed length), and naliases aliases (asciiz).
00145       */
00146 
00147       *ret = *ph;   /* copy whole structure (not its address!) */
00148 
00149       /* copy addresses */
00150       q = (char **)buf; /* pointer to pointers area (type: char **) */
00151       ret->h_addr_list = q; /* update pointer to address list */
00152       pbuf = buf + ((naddr + naliases + 2) * sizeof(*p)); /* skip that area */
00153       for (p = ph->h_addr_list; *p != 0; p++) {
00154          memcpy(pbuf, *p, ph->h_length); /* copy address bytes */
00155          *q++ = pbuf; /* the pointer is the one inside buf... */
00156          pbuf += ph->h_length; /* advance pbuf */
00157       }
00158       *q++ = NULL; /* address list terminator */
00159 
00160       /* copy aliases */
00161       ret->h_aliases = q; /* update pointer to aliases list */
00162       for (p = ph->h_aliases; *p != 0; p++) {
00163          strcpy(pbuf, *p); /* copy alias strings */
00164          *q++ = pbuf; /* the pointer is the one inside buf... */
00165          pbuf += strlen(*p); /* advance pbuf */
00166          *pbuf++ = 0; /* string terminator */
00167       }
00168       *q++ = NULL; /* terminator */
00169 
00170       strcpy(pbuf, ph->h_name); /* copy alias strings */
00171       ret->h_name = pbuf;
00172       pbuf += strlen(ph->h_name); /* advance pbuf */
00173       *pbuf++ = 0; /* string terminator */
00174 
00175       *result = ret;  /* and let *result point to structure */
00176 
00177    }
00178    h_errno = hsave;  /* restore h_errno */
00179    ast_mutex_unlock(&__mutex); /* end critical area */
00180 
00181    return (*result == NULL); /* return 0 on success, non-zero on error */
00182 }

static size_t optimal_alloc_size ( size_t  size  )  [static]

Definition at line 1532 of file utils.c.

References ALLOCATOR_OVERHEAD.

Referenced by __ast_calloc_with_stringfields(), and add_string_pool().

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

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

Definition at line 1392 of file utils.c.

References ast_log(), LOG_WARNING, and ONE_MILLION.

Referenced by ast_tvadd(), and ast_tvsub().

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


Variable Documentation

struct { ... } __ast_string_field_empty_buffer [static]

ast_mutex_t __mutex = { PTHREAD_MUTEX_INITIALIZER , NULL, 1 } [static]

Definition at line 83 of file utils.c.

Referenced by gethostbyname_r().

Definition at line 1524 of file utils.c.

Referenced by __ast_calloc_with_stringfields().

struct ast_flags ast_uri_http = {AST_URI_UNRESERVED}

struct ast_flags ast_uri_http_legacy = {AST_URI_LEGACY_SPACE | AST_URI_UNRESERVED}

Definition at line 382 of file utils.c.

Referenced by acf_curl_helper(), ast_http_get_post_vars(), and handle_uri().

struct ast_flags ast_uri_sip_user = {AST_URI_UNRESERVED | AST_URI_SIP_USER_UNRESERVED}

char b2a[256] [static]

Definition at line 74 of file utils.c.

char base64[64] [static]

Definition at line 73 of file utils.c.

Referenced by aji_start_sasl().

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

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

Referenced by ast_inet_ntoa().

ast_mutex_t randomlock = { PTHREAD_MUTEX_INITIALIZER , 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 1440 of file utils.c.

Referenced by ast_random().

char string[1]

Definition at line 1525 of file utils.c.

Referenced by private_enum_init().


Generated on Thu Feb 9 06:37:32 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6