123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- #ifdef Linux
- #define __USE_GNU
- #endif
- #include <ctype.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <asterisk/lock.h>
- #include <asterisk/utils.h>
- #include <asterisk/logger.h>
- static char base64[64];
- static char b2a[256];
- #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__)
- #define ERANGE 34
- #undef gethostbyname
- AST_MUTEX_DEFINE_STATIC(__mutex);
- static int gethostbyname_r (const char *name, struct hostent *ret, char *buf,
- size_t buflen, struct hostent **result,
- int *h_errnop)
- {
- int hsave;
- struct hostent *ph;
- ast_mutex_lock(&__mutex);
- hsave = h_errno;
- ph = gethostbyname(name);
- *h_errnop = h_errno;
- if (ph == NULL) {
- *result = NULL;
- } else {
- char **p, **q;
- char *pbuf;
- int nbytes=0;
- int naddr=0, naliases=0;
-
-
- for (p = ph->h_addr_list; *p != 0; p++) {
- nbytes += ph->h_length;
- nbytes += sizeof(*p);
- naddr++;
- }
- nbytes += sizeof(*p);
-
- for (p = ph->h_aliases; *p != 0; p++) {
- nbytes += (strlen(*p)+1);
- nbytes += sizeof(*p);
- naliases++;
- }
- nbytes += sizeof(*p);
-
-
- if(nbytes > buflen) {
- *result = NULL;
- ast_mutex_unlock(&__mutex);
- return ERANGE;
- }
-
-
- *ret = *ph;
-
- q = (char **)buf;
- ret->h_addr_list = q;
- pbuf = buf + ((naddr+naliases+2)*sizeof(*p));
- for (p = ph->h_addr_list; *p != 0; p++) {
- memcpy(pbuf, *p, ph->h_length);
- *q++ = pbuf;
- pbuf += ph->h_length;
- }
- *q++ = NULL;
-
- ret->h_aliases = q;
- for (p = ph->h_aliases; *p != 0; p++) {
- strcpy(pbuf, *p);
- *q++ = pbuf;
- pbuf += strlen(*p);
- *pbuf++ = 0;
- }
- *q++ = NULL;
- strcpy(pbuf, ph->h_name);
- ret->h_name = pbuf;
- pbuf += strlen(ph->h_name);
- *pbuf++ = 0;
- *result = ret;
- }
- h_errno = hsave;
- ast_mutex_unlock(&__mutex);
- return (*result == NULL);
- }
- #endif
- struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp)
- {
- int res;
- int herrno;
- const char *s;
- struct hostent *result = NULL;
-
- s = host;
- while(s && *s) {
- if (!isdigit(*s))
- break;
- s++;
- }
- if (!s || !*s)
- return NULL;
- res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
- if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
- return NULL;
- return &hp->hp;
- }
- AST_MUTEX_DEFINE_STATIC(test_lock);
- AST_MUTEX_DEFINE_STATIC(test_lock2);
- static pthread_t test_thread;
- static int lock_count = 0;
- static int test_errors = 0;
- static void *test_thread_body(void *data)
- {
- ast_mutex_lock(&test_lock);
- lock_count += 10;
- if (lock_count != 10)
- test_errors++;
- ast_mutex_lock(&test_lock);
- lock_count += 10;
- if (lock_count != 20)
- test_errors++;
- ast_mutex_lock(&test_lock2);
- ast_mutex_unlock(&test_lock);
- lock_count -= 10;
- if (lock_count != 10)
- test_errors++;
- ast_mutex_unlock(&test_lock);
- lock_count -= 10;
- ast_mutex_unlock(&test_lock2);
- if (lock_count != 0)
- test_errors++;
- return NULL;
- }
- int test_for_thread_safety(void)
- {
- ast_mutex_lock(&test_lock2);
- ast_mutex_lock(&test_lock);
- lock_count += 1;
- ast_mutex_lock(&test_lock);
- lock_count += 1;
- ast_pthread_create(&test_thread, NULL, test_thread_body, NULL);
- usleep(100);
- if (lock_count != 2)
- test_errors++;
- ast_mutex_unlock(&test_lock);
- lock_count -= 1;
- usleep(100);
- if (lock_count != 1)
- test_errors++;
- ast_mutex_unlock(&test_lock);
- lock_count -= 1;
- if (lock_count != 0)
- test_errors++;
- ast_mutex_unlock(&test_lock2);
- usleep(100);
- if (lock_count != 0)
- test_errors++;
- pthread_join(test_thread, NULL);
- return(test_errors);
- }
- int ast_base64decode(unsigned char *dst, char *src, int max)
- {
- int cnt = 0;
- unsigned int byte = 0;
- unsigned int bits = 0;
- int incnt = 0;
- #if 0
- unsigned char *odst = dst;
- #endif
- while(*src && (cnt < max)) {
-
- byte <<= 6;
- byte |= (b2a[(int)(*src)]) & 0x3f;
- bits += 6;
- #if 0
- printf("Add: %c %s\n", *src, binary(b2a[(int)(*src)] & 0x3f, 6));
- #endif
- src++;
- incnt++;
-
- if (bits >= 8) {
- bits -= 8;
- *dst = (byte >> bits) & 0xff;
- #if 0
- printf("Remove: %02x %s\n", *dst, binary(*dst, 8));
- #endif
- dst++;
- cnt++;
- }
- }
- #if 0
- dump(odst, cnt);
- #endif
-
- return cnt;
- }
- int ast_base64encode(char *dst, unsigned char *src, int srclen, int max)
- {
- int cnt = 0;
- unsigned int byte = 0;
- int bits = 0;
- int index;
- int cntin = 0;
- #if 0
- char *odst = dst;
- dump(src, srclen);
- #endif
-
- max--;
- while((cntin < srclen) && (cnt < max)) {
- byte <<= 8;
- #if 0
- printf("Add: %02x %s\n", *src, binary(*src, 8));
- #endif
- byte |= *(src++);
- bits += 8;
- cntin++;
- while((bits >= 6) && (cnt < max)) {
- bits -= 6;
-
- index = (byte >> bits) & 0x3f;
- *dst = base64[index];
- #if 0
- printf("Remove: %c %s\n", *dst, binary(index, 6));
- #endif
- dst++;
- cnt++;
- }
- }
- if (bits && (cnt < max)) {
-
- byte <<= (6 - bits);
- index = (byte) & 0x3f;
- *(dst++) = base64[index];
- cnt++;
- }
- *dst = '\0';
- return cnt;
- }
- static void base64_init(void)
- {
- int x;
- memset(b2a, -1, sizeof(b2a));
-
- for (x=0;x<26;x++) {
-
- base64[x] = 'A' + x;
- b2a['A' + x] = x;
-
- base64[x + 26] = 'a' + x;
- b2a['a' + x] = x + 26;
-
- if (x < 10) {
- base64[x + 52] = '0' + x;
- b2a['0' + x] = x + 52;
- }
- }
- base64[62] = '+';
- base64[63] = '/';
- b2a[(int)'+'] = 62;
- b2a[(int)'/'] = 63;
- #if 0
- for (x=0;x<64;x++) {
- if (b2a[(int)base64[x]] != x) {
- fprintf(stderr, "!!! %d failed\n", x);
- } else
- fprintf(stderr, "--- %d passed\n", x);
- }
- #endif
- }
- const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia)
- {
- return inet_ntop(AF_INET, &ia, buf, bufsiz);
- }
- int ast_utils_init(void)
- {
- base64_init();
- return 0;
- }
- #ifndef LINUX
- #undef pthread_create
- int ast_pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data)
- {
- pthread_attr_t lattr;
- if (!attr) {
- pthread_attr_init(&lattr);
- attr = &lattr;
- }
- errno = pthread_attr_setstacksize(attr, PTHREAD_ATTR_STACKSIZE);
- if (errno)
- ast_log(LOG_WARNING, "pthread_attr_setstacksize returned non-zero: %s\n", strerror(errno));
- return pthread_create(thread, attr, start_routine, data);
- }
- #endif
- static char *upper(const char *orig, char *buf, int bufsize)
- {
- int i;
- memset(buf, 0, bufsize);
- for (i=0; i<bufsize - 1; i++) {
- buf[i] = toupper(orig[i]);
- if (orig[i] == '\0') {
- break;
- }
- }
- return buf;
- }
- #ifndef LINUX
- char *ast_strcasestr(const char *haystack, const char *needle)
- {
- char *u1, *u2;
- int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
- u1 = alloca(u1len);
- u2 = alloca(u2len);
- if (u1 && u2) {
- char *offset;
- if (u2len > u1len) {
-
- return NULL;
- }
- offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
- if (offset) {
-
- return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
- } else {
- return NULL;
- }
- } else {
- ast_log(LOG_ERROR, "Out of memory\n");
- return NULL;
- }
- }
- #endif
|