utils.h File Reference

#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>

Include dependency graph for utils.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_hostent

Defines

#define inet_ntoa   __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__
#define PTHREAD_ATTR_STACKSIZE   2097152

Functions

struct hostent * ast_gethostbyname (const char *host, struct ast_hostent *hp)
int ast_base64encode (char *dst, unsigned char *src, int srclen, int max)
int ast_base64decode (unsigned char *dst, char *src, int max)
int test_for_thread_safety (void)
const char * ast_inet_ntoa (char *buf, int bufsiz, struct in_addr ia)
int ast_utils_init (void)
int ast_pthread_create (pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data)
char * ast_strcasestr (const char *, const char *)


Define Documentation

#define inet_ntoa   __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__

Definition at line 40 of file utils.h.

#define PTHREAD_ATTR_STACKSIZE   2097152

Definition at line 48 of file utils.h.


Function Documentation

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

Definition at line 227 of file utils.c.

00228 {
00229    int cnt = 0;
00230    unsigned int byte = 0;
00231    unsigned int bits = 0;
00232    int incnt = 0;
00233 #if 0
00234    unsigned char *odst = dst;
00235 #endif
00236    while(*src && (cnt < max)) {
00237       /* Shift in 6 bits of input */
00238       byte <<= 6;
00239       byte |= (b2a[(int)(*src)]) & 0x3f;
00240       bits += 6;
00241 #if 0
00242       printf("Add: %c %s\n", *src, binary(b2a[(int)(*src)] & 0x3f, 6));
00243 #endif
00244       src++;
00245       incnt++;
00246       /* If we have at least 8 bits left over, take that character 
00247          off the top */
00248       if (bits >= 8)  {
00249          bits -= 8;
00250          *dst = (byte >> bits) & 0xff;
00251 #if 0
00252          printf("Remove: %02x %s\n", *dst, binary(*dst, 8));
00253 #endif
00254          dst++;
00255          cnt++;
00256       }
00257    }
00258 #if 0
00259    dump(odst, cnt);
00260 #endif
00261    /* Dont worry about left over bits, they're extra anyway */
00262    return cnt;
00263 }

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

Definition at line 265 of file utils.c.

00266 {
00267    int cnt = 0;
00268    unsigned int byte = 0;
00269    int bits = 0;
00270    int index;
00271    int cntin = 0;
00272 #if 0
00273    char *odst = dst;
00274    dump(src, srclen);
00275 #endif
00276    /* Reserve one bit for end */
00277    max--;
00278    while((cntin < srclen) && (cnt < max)) {
00279       byte <<= 8;
00280 #if 0
00281       printf("Add: %02x %s\n", *src, binary(*src, 8));
00282 #endif
00283       byte |= *(src++);
00284       bits += 8;
00285       cntin++;
00286       while((bits >= 6) && (cnt < max)) {
00287          bits -= 6;
00288          /* We want only the top */
00289          index = (byte >> bits) & 0x3f;
00290          *dst = base64[index];
00291 #if 0
00292          printf("Remove: %c %s\n", *dst, binary(index, 6));
00293 #endif
00294          dst++;
00295          cnt++;
00296       }
00297    }
00298    if (bits && (cnt < max)) {
00299       /* Add one last character for the remaining bits, 
00300          padding the rest with 0 */
00301       byte <<= (6 - bits);
00302       index = (byte) & 0x3f;
00303       *(dst++) = base64[index];
00304       cnt++;
00305    }
00306    *dst = '\0';
00307    return cnt;
00308 }

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

Definition at line 140 of file utils.c.

00141 {
00142    int res;
00143    int herrno;
00144    const char *s;
00145    struct hostent *result = NULL;
00146    /* Although it is perfectly legitimate to lookup a pure integer, for
00147       the sake of the sanity of people who like to name their peers as
00148       integers, we break with tradition and refuse to look up a
00149       pure integer */
00150    s = host;
00151    while(s && *s) {
00152       if (!isdigit(*s))
00153          break;
00154       s++;
00155    }
00156    if (!s || !*s)
00157       return NULL;
00158    res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
00159 
00160    if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
00161       return NULL;
00162    return &hp->hp;
00163 }

const char* ast_inet_ntoa ( char *  buf,
int  bufsiz,
struct in_addr  ia 
)

Definition at line 343 of file utils.c.

00344 {
00345    return inet_ntop(AF_INET, &ia, buf, bufsiz);
00346 }

int ast_pthread_create ( pthread_t *  thread,
pthread_attr_t *  attr,
void *(*)(void *)  start_routine,
void *  data 
)

Definition at line 357 of file utils.c.

00358 {
00359    pthread_attr_t lattr;
00360    if (!attr) {
00361       pthread_attr_init(&lattr);
00362       attr = &lattr;
00363    }
00364    errno = pthread_attr_setstacksize(attr, PTHREAD_ATTR_STACKSIZE);
00365    if (errno)
00366       ast_log(LOG_WARNING, "pthread_attr_setstacksize returned non-zero: %s\n", strerror(errno));
00367    return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */
00368 }

char* ast_strcasestr ( const char *  ,
const char *   
)

Definition at line 386 of file utils.c.

00387 {
00388    char *u1, *u2;
00389    int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
00390 
00391    u1 = alloca(u1len);
00392    u2 = alloca(u2len);
00393    if (u1 && u2) {
00394       char *offset;
00395       if (u2len > u1len) {
00396          /* Needle bigger than haystack */
00397          return NULL;
00398       }
00399       offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
00400       if (offset) {
00401          /* Return the offset into the original string */
00402          return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
00403       } else {
00404          return NULL;
00405       }
00406    } else {
00407       ast_log(LOG_ERROR, "Out of memory\n");
00408       return NULL;
00409    }
00410 }

int ast_utils_init ( void   ) 

Definition at line 348 of file utils.c.

00349 {
00350    base64_init();
00351    return 0;
00352 }

int test_for_thread_safety ( void   ) 

Definition at line 199 of file utils.c.

00200 { 
00201    ast_mutex_lock(&test_lock2);
00202    ast_mutex_lock(&test_lock);
00203    lock_count += 1;
00204    ast_mutex_lock(&test_lock);
00205    lock_count += 1;
00206    ast_pthread_create(&test_thread, NULL, test_thread_body, NULL); 
00207    usleep(100);
00208    if (lock_count != 2) 
00209       test_errors++;
00210    ast_mutex_unlock(&test_lock);
00211    lock_count -= 1;
00212    usleep(100); 
00213    if (lock_count != 1) 
00214       test_errors++;
00215    ast_mutex_unlock(&test_lock);
00216    lock_count -= 1;
00217    if (lock_count != 0) 
00218       test_errors++;
00219    ast_mutex_unlock(&test_lock2);
00220    usleep(100);
00221    if (lock_count != 0) 
00222       test_errors++;
00223    pthread_join(test_thread, NULL);
00224    return(test_errors);          /* return 0 on success. */
00225 }


Generated on Wed Oct 28 17:01:04 2009 for Asterisk by  doxygen 1.5.6