#include "asterisk/compat.h"
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <limits.h>
#include "asterisk/lock.h"
#include "asterisk/time.h"
#include "asterisk/strings.h"
Go to the source code of this file.
Data Structures | |
struct | ast_flags |
struct | ast_hostent |
Defines | |
#define | ast_clear_flag(p, flag) |
#define | ast_clear_flag_nonstd(p, flag) |
#define | ast_copy_flags(dest, src, flagz) |
#define | ast_copy_flags_nonstd(dest, src, flagz) |
#define | AST_FLAGS_ALL UINT_MAX |
#define | ast_pthread_create(a, b, c, d) ast_pthread_create_stack(a,b,c,d,0) |
#define | ast_set2_flag(p, value, flag) |
#define | ast_set2_flag_nonstd(p, value, flag) |
#define | ast_set_flag(p, flag) |
#define | ast_set_flag_nonstd(p, flag) |
#define | AST_STACKSIZE 256 * 1024 |
#define | ast_test_flag(p, flag) |
#define | ast_test_flag_nonstd(p, flag) |
#define | inet_ntoa __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__ |
Functions | |
int | ast_base64decode (unsigned char *dst, const char *src, int max) |
int | ast_base64encode (char *dst, const unsigned char *src, int srclen, int max) |
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 (char *buf, int bufsiz, struct in_addr ia) |
ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa | |
void | ast_md5_hash (char *output, char *input) |
ast_md5_hash: Produce 16 char MD5 hash of value. --- | |
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_stack (pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize) |
force_inline void | ast_slinear_saturated_add (short *input, short *value) |
force_inline void | ast_slinear_saturated_divide (short *input, short *value) |
force_inline void | ast_slinear_saturated_multiply (short *input, short *value) |
void | ast_uri_decode (char *s) |
Decode URI, URN, URL (overwrite string). | |
char * | ast_uri_encode (char *string, char *outbuf, int buflen, int doreserved) |
Turn text string to URI-encoded XX version At this point, we're converting from ISO-8859-x (8-bit), not UTF8 as in the SIP protocol spec If doreserved == 1 we will convert reserved characters also. RFC 2396, section 2.4 outbuf needs to have more memory allocated than the instring to have room for the expansion. Every char that is converted is replaced by three ASCII characters. | |
int | ast_utils_init (void) |
int | ast_wait_for_input (int fd, int ms) |
int | getloadavg (double *list, int nelem) |
force_inline int | inaddrcmp (const struct sockaddr_in *sin1, const struct sockaddr_in *sin2) |
int | test_for_thread_safety (void) |
Variables | |
unsigned int | __unsigned_int_flags_dummy |
Definition in file utils.h.
|
|
Value: do { \ ((p)->flags &= ~(flag)); \ } while(0) Definition at line 114 of file utils.h. Referenced by build_transactions(), cache_lookup_internal(), dundi_lookup_local(), dundi_prop_precache(), and handle_command_response(). |
|
Definition at line 83 of file utils.h. Referenced by agent_read(), ast_cdr_reset(), ast_do_masquerade(), ast_feature_interpret(), build_peer(), build_user(), cache_lookup_internal(), check_access(), check_user_full(), create_addr(), create_addr_from_peer(), dial_exec_full(), dundi_lookup_local(), find_callno(), iax2_request(), populate_defaults(), realtime_peer(), register_verify(), sip_alloc(), sip_hangup(), temp_peer(), vm_exec(), and wait_for_answer(). |
|
Value: do { \ (dest)->flags &= ~(flagz); \ (dest)->flags |= ((src)->flags & (flagz)); \ } while (0) |
|
Definition at line 130 of file utils.h. Referenced by ast_app_parse_options(), ast_cdr_reset(), ast_feature_interpret(), builtin_atxfer(), cache_lookup_internal(), check_goto_on_transfer(), dundi_lookup_local(), populate_defaults(), reload_config(), and set_config_flags(). |
|
|
Definition at line 93 of file utils.h. Referenced by __ast_pbx_run(), apply_option(), build_peer(), build_user(), check_access(), dial_exec_full(), find_user(), forkcdr_exec(), handle_common_options(), load_config(), load_moh_classes(), macro_exec(), reload_config(), and set_config(). |
|
|
|
|
Value: do { \ ((p)->flags |= (flag)); \ } while(0) Definition at line 110 of file utils.h. Referenced by dundi_lookup_internal(), dundi_query_eid_internal(), and handle_command_response(). |
|
|
|
|
Value: ({ \ ((p)->flags & (flag)); \ }) Definition at line 106 of file utils.h. Referenced by cache_save_hint(), dundi_lookup_thread(), dundi_precache_thread(), and handle_command_response(). |
|
|
|
Definition at line 297 of file utils.c. References b2a. Referenced by __ast_check_signature(), and ast_osp_validate(). 00298 { 00299 int cnt = 0; 00300 unsigned int byte = 0; 00301 unsigned int bits = 0; 00302 int incnt = 0; 00303 #if 0 00304 unsigned char *odst = dst; 00305 #endif 00306 while(*src && (cnt < max)) { 00307 /* Shift in 6 bits of input */ 00308 byte <<= 6; 00309 byte |= (b2a[(int)(*src)]) & 0x3f; 00310 bits += 6; 00311 #if 0 00312 printf("Add: %c %s\n", *src, binary(b2a[(int)(*src)] & 0x3f, 6)); 00313 #endif 00314 src++; 00315 incnt++; 00316 /* If we have at least 8 bits left over, take that character 00317 off the top */ 00318 if (bits >= 8) { 00319 bits -= 8; 00320 *dst = (byte >> bits) & 0xff; 00321 #if 0 00322 printf("Remove: %02x %s\n", *dst, binary(*dst, 8)); 00323 #endif 00324 dst++; 00325 cnt++; 00326 } 00327 } 00328 #if 0 00329 dump(odst, cnt); 00330 #endif 00331 /* Dont worry about left over bits, they're extra anyway */ 00332 return cnt; 00333 }
|
|
Definition at line 335 of file utils.c. References base64. Referenced by __ast_sign(), ast_osp_lookup(), ast_osp_next(), and build_secret(). 00336 { 00337 int cnt = 0; 00338 unsigned int byte = 0; 00339 int bits = 0; 00340 int index; 00341 int cntin = 0; 00342 #if 0 00343 char *odst = dst; 00344 dump(src, srclen); 00345 #endif 00346 /* Reserve one bit for end */ 00347 max--; 00348 while((cntin < srclen) && (cnt < max)) { 00349 byte <<= 8; 00350 #if 0 00351 printf("Add: %02x %s\n", *src, binary(*src, 8)); 00352 #endif 00353 byte |= *(src++); 00354 bits += 8; 00355 cntin++; 00356 while((bits >= 6) && (cnt < max)) { 00357 bits -= 6; 00358 /* We want only the top */ 00359 index = (byte >> bits) & 0x3f; 00360 *dst = base64[index]; 00361 #if 0 00362 printf("Remove: %c %s\n", *dst, binary(index, 6)); 00363 #endif 00364 dst++; 00365 cnt++; 00366 } 00367 } 00368 if (bits && (cnt < max)) { 00369 /* Add one last character for the remaining bits, 00370 padding the rest with 0 */ 00371 byte <<= (6 - bits); 00372 index = (byte) & 0x3f; 00373 *(dst++) = base64[index]; 00374 cnt++; 00375 } 00376 *dst = '\0'; 00377 return cnt; 00378 }
|
|
Re-entrant (thread safe) version of gethostbyname that replaces the standard gethostbyname (which is not thread safe).
Definition at line 173 of file utils.c. References ast_hostent::buf, ast_hostent::hp, hp, result, and s. Referenced by ast_dnsmgr_lookup(), ast_find_ourip(), ast_get_ip_or_srv(), ast_sip_ouraddrfor(), build_peer(), check_via(), create_addr(), festival_exec(), iax2_register(), iax_template_parse(), launch_netscript(), parse_ok_contact(), parse_register_contact(), process_sdp(), refresh_list(), reload_config(), rpt_exec(), rtp_do_debug_ip(), set_config(), set_destination(), sip_devicestate(), and sip_do_debug_ip(). 00174 { 00175 int res; 00176 int herrno; 00177 int dots=0; 00178 const char *s; 00179 struct hostent *result = NULL; 00180 /* Although it is perfectly legitimate to lookup a pure integer, for 00181 the sake of the sanity of people who like to name their peers as 00182 integers, we break with tradition and refuse to look up a 00183 pure integer */ 00184 s = host; 00185 res = 0; 00186 while(s && *s) { 00187 if (*s == '.') 00188 dots++; 00189 else if (!isdigit(*s)) 00190 break; 00191 s++; 00192 } 00193 if (!s || !*s) { 00194 /* Forge a reply for IP's to avoid octal IP's being interpreted as octal */ 00195 if (dots != 3) 00196 return NULL; 00197 memset(hp, 0, sizeof(struct ast_hostent)); 00198 hp->hp.h_addr_list = (void *) hp->buf; 00199 hp->hp.h_addr = hp->buf + sizeof(void *); 00200 if (inet_pton(AF_INET, host, hp->hp.h_addr) > 0) 00201 return &hp->hp; 00202 return NULL; 00203 00204 } 00205 #ifdef SOLARIS 00206 result = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &herrno); 00207 00208 if (!result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0]) 00209 return NULL; 00210 #else 00211 res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno); 00212 00213 if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0]) 00214 return NULL; 00215 #endif 00216 return &hp->hp; 00217 }
|
|
|
ast_md5_hash: Produce 16 char MD5 hash of value. ---
Definition at line 282 of file utils.c. References MD5Final(), MD5Init(), and MD5Update(). Referenced by auth_exec(), build_reply_digest(), builtin_function_checkmd5(), builtin_function_md5(), check_auth(), md5_exec(), and md5check_exec(). 00283 { 00284 struct MD5Context md5; 00285 unsigned char digest[16]; 00286 char *ptr; 00287 int x; 00288 00289 MD5Init(&md5); 00290 MD5Update(&md5, (unsigned char *)input, strlen(input)); 00291 MD5Final(digest, &md5); 00292 ptr = output; 00293 for (x=0; x<16; x++) 00294 ptr += sprintf(ptr, "%2.2x", digest[x]); 00295 }
|
|
Process a string to find and replace characters.
Definition at line 881 of file utils.c. Referenced by __build_step(), handle_context_add_extension(), and pbx_load_module(). 00882 { 00883 char *dataPut = start; 00884 int inEscape = 0; 00885 int inQuotes = 0; 00886 00887 for (; *start; start++) { 00888 if (inEscape) { 00889 *dataPut++ = *start; /* Always goes verbatim */ 00890 inEscape = 0; 00891 } else { 00892 if (*start == '\\') { 00893 inEscape = 1; /* Do not copy \ into the data */ 00894 } else if (*start == '\'') { 00895 inQuotes = 1-inQuotes; /* Do not copy ' into the data */ 00896 } else { 00897 /* Replace , with |, unless in quotes */ 00898 *dataPut++ = inQuotes ? *start : ((*start==find) ? replace_with : *start); 00899 } 00900 } 00901 } 00902 if (start != dataPut) 00903 *dataPut = 0; 00904 return dataPut; 00905 }
|
|
Definition at line 487 of file utils.c. References ast_log(), LOG_WARNING, and pthread_create. 00488 { 00489 pthread_attr_t lattr; 00490 if (!attr) { 00491 pthread_attr_init(&lattr); 00492 attr = &lattr; 00493 } 00494 #ifdef __linux__ 00495 /* On Linux, pthread_attr_init() defaults to PTHREAD_EXPLICIT_SCHED, 00496 which is kind of useless. Change this here to 00497 PTHREAD_INHERIT_SCHED; that way the -p option to set realtime 00498 priority will propagate down to new threads by default. 00499 This does mean that callers cannot set a different priority using 00500 PTHREAD_EXPLICIT_SCHED in the attr argument; instead they must set 00501 the priority afterwards with pthread_setschedparam(). */ 00502 errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED); 00503 if (errno) 00504 ast_log(LOG_WARNING, "pthread_attr_setinheritsched returned non-zero: %s\n", strerror(errno)); 00505 #endif 00506 00507 if (!stacksize) 00508 stacksize = AST_STACKSIZE; 00509 errno = pthread_attr_setstacksize(attr, stacksize); 00510 if (errno) 00511 ast_log(LOG_WARNING, "pthread_attr_setstacksize returned non-zero: %s\n", strerror(errno)); 00512 return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */ 00513 }
|
|
Definition at line 172 of file utils.h. References force_inline. Referenced by ast_frame_slinear_sum(). 00173 { 00174 int res; 00175 00176 res = (int) *input + *value; 00177 if (res > 32767) 00178 *input = 32767; 00179 else if (res < -32767) 00180 *input = -32767; 00181 else 00182 *input = (short) res; 00183 }
|
|
Definition at line 198 of file utils.h. References force_inline. Referenced by ast_frame_adjust_volume(). 00199 { 00200 *input /= *value; 00201 }
|
|
Definition at line 185 of file utils.h. References force_inline. Referenced by ast_frame_adjust_volume(). 00186 { 00187 int res; 00188 00189 res = (int) *input * *value; 00190 if (res > 32767) 00191 *input = 32767; 00192 else if (res < -32767) 00193 *input = -32767; 00194 else 00195 *input = (short) res; 00196 }
|
|
Decode URI, URN, URL (overwrite string).
Definition at line 455 of file utils.c. References s. Referenced by builtin_function_uridecode(), check_user_full(), get_destination(), get_refer_info(), and register_verify(). 00456 { 00457 char *o; 00458 unsigned int tmp; 00459 00460 for (o = s; *s; s++, o++) { 00461 if (*s == '%' && strlen(s) > 2 && sscanf(s + 1, "%2x", &tmp) == 1) { 00462 /* have '%', two chars and correct parsing */ 00463 *o = tmp; 00464 s += 2; /* Will be incremented once more when we break out */ 00465 } else /* all other cases, just copy */ 00466 *o = *s; 00467 } 00468 *o = '\0'; 00469 }
|
|
Turn text string to URI-encoded XX version At this point, we're converting from ISO-8859-x (8-bit), not UTF8 as in the SIP protocol spec If doreserved == 1 we will convert reserved characters also. RFC 2396, section 2.4 outbuf needs to have more memory allocated than the instring to have room for the expansion. Every char that is converted is replaced by three ASCII characters. ast_uri_encode
Definition at line 424 of file utils.c. Referenced by builtin_function_uriencode(), and initreqprep(). 00425 { 00426 char *reserved = ";/?:@&=+$, "; /* Reserved chars */ 00427 00428 char *ptr = string; /* Start with the string */ 00429 char *out = NULL; 00430 char *buf = NULL; 00431 00432 strncpy(outbuf, string, buflen); 00433 00434 /* If there's no characters to convert, just go through and don't do anything */ 00435 while (*ptr) { 00436 if (((unsigned char) *ptr) > 127 || (doreserved && strchr(reserved, *ptr)) ) { 00437 /* Oops, we need to start working here */ 00438 if (!buf) { 00439 buf = outbuf; 00440 out = buf + (ptr - string) ; /* Set output ptr */ 00441 } 00442 out += sprintf(out, "%%%02x", (unsigned char) *ptr); 00443 } else if (buf) { 00444 *out = *ptr; /* Continue copying the string */ 00445 out++; 00446 } 00447 ptr++; 00448 } 00449 if (buf) 00450 *out = '\0'; 00451 return outbuf; 00452 }
|
|
Definition at line 477 of file utils.c. References base64_init(). Referenced by main(). 00478 { 00479 base64_init(); 00480 return 0; 00481 }
|
|
Definition at line 515 of file utils.c. References pollfd::events, pollfd::fd, poll(), and POLLIN. Referenced by ast_moh_destroy(). 00516 { 00517 struct pollfd pfd[1]; 00518 memset(pfd, 0, sizeof(pfd)); 00519 pfd[0].fd = fd; 00520 pfd[0].events = POLLIN|POLLPRI; 00521 return poll(pfd, 1, ms); 00522 }
|
|
Definition at line 869 of file utils.c. Referenced by ast_readconfig(), and increase_call_count(). 00870 { 00871 int i; 00872 00873 for (i = 0; i < nelem; i++) { 00874 list[i] = 0.1; 00875 } 00876 return -1; 00877 }
|
|
Compares the source address and port of two sockaddr_in Definition at line 216 of file utils.h. References force_inline. Referenced by ast_netsock_find(), ast_rtp_bridge(), find_peer(), find_tpeer(), find_transaction(), handle_command_response(), iax2_ack_registry(), parse_register_contact(), registry_rerequest(), sip_addrcmp(), socket_read(), and update_registry(). 00217 {
00218 return ((sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
00219 || (sin1->sin_port != sin2->sin_port));
00220 }
|
|
Definition at line 253 of file utils.c. References ast_mutex_lock(), ast_mutex_unlock(), ast_pthread_create, lock_count, test_errors, test_thread, and test_thread_body(). Referenced by main(). 00254 { 00255 ast_mutex_lock(&test_lock2); 00256 ast_mutex_lock(&test_lock); 00257 lock_count += 1; 00258 ast_mutex_lock(&test_lock); 00259 lock_count += 1; 00260 ast_pthread_create(&test_thread, NULL, test_thread_body, NULL); 00261 usleep(100); 00262 if (lock_count != 2) 00263 test_errors++; 00264 ast_mutex_unlock(&test_lock); 00265 lock_count -= 1; 00266 usleep(100); 00267 if (lock_count != 1) 00268 test_errors++; 00269 ast_mutex_unlock(&test_lock); 00270 lock_count -= 1; 00271 if (lock_count != 0) 00272 test_errors++; 00273 ast_mutex_unlock(&test_lock2); 00274 usleep(100); 00275 if (lock_count != 0) 00276 test_errors++; 00277 pthread_join(test_thread, NULL); 00278 return(test_errors); /* return 0 on success. */ 00279 }
|
|
|