00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _ASTERISK_UTILS_H
00024 #define _ASTERISK_UTILS_H
00025
00026 #include "asterisk/network.h"
00027
00028 #include <time.h>
00029 #include <unistd.h>
00030 #include <string.h>
00031
00032 #include "asterisk/lock.h"
00033 #include "asterisk/time.h"
00034 #include "asterisk/logger.h"
00035 #include "asterisk/localtime.h"
00036 #include "asterisk/stringfields.h"
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 extern unsigned int __unsigned_int_flags_dummy;
00062
00063 #define ast_test_flag(p,flag) ({ \
00064 typeof ((p)->flags) __p = (p)->flags; \
00065 typeof (__unsigned_int_flags_dummy) __x = 0; \
00066 (void) (&__p == &__x); \
00067 ((p)->flags & (flag)); \
00068 })
00069
00070 #define ast_set_flag(p,flag) do { \
00071 typeof ((p)->flags) __p = (p)->flags; \
00072 typeof (__unsigned_int_flags_dummy) __x = 0; \
00073 (void) (&__p == &__x); \
00074 ((p)->flags |= (flag)); \
00075 } while(0)
00076
00077 #define ast_clear_flag(p,flag) do { \
00078 typeof ((p)->flags) __p = (p)->flags; \
00079 typeof (__unsigned_int_flags_dummy) __x = 0; \
00080 (void) (&__p == &__x); \
00081 ((p)->flags &= ~(flag)); \
00082 } while(0)
00083
00084 #define ast_copy_flags(dest,src,flagz) do { \
00085 typeof ((dest)->flags) __d = (dest)->flags; \
00086 typeof ((src)->flags) __s = (src)->flags; \
00087 typeof (__unsigned_int_flags_dummy) __x = 0; \
00088 (void) (&__d == &__x); \
00089 (void) (&__s == &__x); \
00090 (dest)->flags &= ~(flagz); \
00091 (dest)->flags |= ((src)->flags & (flagz)); \
00092 } while (0)
00093
00094 #define ast_set2_flag(p,value,flag) do { \
00095 typeof ((p)->flags) __p = (p)->flags; \
00096 typeof (__unsigned_int_flags_dummy) __x = 0; \
00097 (void) (&__p == &__x); \
00098 if (value) \
00099 (p)->flags |= (flag); \
00100 else \
00101 (p)->flags &= ~(flag); \
00102 } while (0)
00103
00104 #define ast_set_flags_to(p,flag,value) do { \
00105 typeof ((p)->flags) __p = (p)->flags; \
00106 typeof (__unsigned_int_flags_dummy) __x = 0; \
00107 (void) (&__p == &__x); \
00108 (p)->flags &= ~(flag); \
00109 (p)->flags |= (value); \
00110 } while (0)
00111
00112
00113
00114
00115
00116
00117
00118 extern uint64_t __unsigned_int_flags_dummy64;
00119
00120 #define ast_test_flag64(p,flag) ({ \
00121 typeof ((p)->flags) __p = (p)->flags; \
00122 typeof (__unsigned_int_flags_dummy64) __x = 0; \
00123 (void) (&__p == &__x); \
00124 ((p)->flags & (flag)); \
00125 })
00126
00127 #define ast_set_flag64(p,flag) do { \
00128 typeof ((p)->flags) __p = (p)->flags; \
00129 typeof (__unsigned_int_flags_dummy64) __x = 0; \
00130 (void) (&__p == &__x); \
00131 ((p)->flags |= (flag)); \
00132 } while(0)
00133
00134 #define ast_clear_flag64(p,flag) do { \
00135 typeof ((p)->flags) __p = (p)->flags; \
00136 typeof (__unsigned_int_flags_dummy64) __x = 0; \
00137 (void) (&__p == &__x); \
00138 ((p)->flags &= ~(flag)); \
00139 } while(0)
00140
00141 #define ast_copy_flags64(dest,src,flagz) do { \
00142 typeof ((dest)->flags) __d = (dest)->flags; \
00143 typeof ((src)->flags) __s = (src)->flags; \
00144 typeof (__unsigned_int_flags_dummy64) __x = 0; \
00145 (void) (&__d == &__x); \
00146 (void) (&__s == &__x); \
00147 (dest)->flags &= ~(flagz); \
00148 (dest)->flags |= ((src)->flags & (flagz)); \
00149 } while (0)
00150
00151 #define ast_set2_flag64(p,value,flag) do { \
00152 typeof ((p)->flags) __p = (p)->flags; \
00153 typeof (__unsigned_int_flags_dummy64) __x = 0; \
00154 (void) (&__p == &__x); \
00155 if (value) \
00156 (p)->flags |= (flag); \
00157 else \
00158 (p)->flags &= ~(flag); \
00159 } while (0)
00160
00161 #define ast_set_flags_to64(p,flag,value) do { \
00162 typeof ((p)->flags) __p = (p)->flags; \
00163 typeof (__unsigned_int_flags_dummy64) __x = 0; \
00164 (void) (&__p == &__x); \
00165 (p)->flags &= ~(flag); \
00166 (p)->flags |= (value); \
00167 } while (0)
00168
00169
00170
00171
00172
00173 #define ast_test_flag_nonstd(p,flag) \
00174 ((p)->flags & (flag))
00175
00176 #define ast_set_flag_nonstd(p,flag) do { \
00177 ((p)->flags |= (flag)); \
00178 } while(0)
00179
00180 #define ast_clear_flag_nonstd(p,flag) do { \
00181 ((p)->flags &= ~(flag)); \
00182 } while(0)
00183
00184 #define ast_copy_flags_nonstd(dest,src,flagz) do { \
00185 (dest)->flags &= ~(flagz); \
00186 (dest)->flags |= ((src)->flags & (flagz)); \
00187 } while (0)
00188
00189 #define ast_set2_flag_nonstd(p,value,flag) do { \
00190 if (value) \
00191 (p)->flags |= (flag); \
00192 else \
00193 (p)->flags &= ~(flag); \
00194 } while (0)
00195
00196 #define AST_FLAGS_ALL UINT_MAX
00197
00198
00199
00200 struct ast_flags {
00201 unsigned int flags;
00202 };
00203
00204
00205
00206 struct ast_flags64 {
00207 uint64_t flags;
00208 };
00209
00210 struct ast_hostent {
00211 struct hostent hp;
00212 char buf[1024];
00213 };
00214
00215
00216 struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp);
00217
00218
00219 void ast_md5_hash(char *output, const char *input);
00220
00221 void ast_sha1_hash(char *output, const char *input);
00222
00223 int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks);
00224
00225 #undef MIN
00226 #define MIN(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a > __b) ? __b : __a);})
00227 #undef MAX
00228 #define MAX(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a < __b) ? __b : __a);})
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max);
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 int ast_base64decode(unsigned char *dst, const char *src, int max);
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 char *ast_uri_encode(const char *string, char *outbuf, int buflen, int do_special_char);
00270
00271
00272
00273
00274 void ast_uri_decode(char *s);
00275
00276 static force_inline void ast_slinear_saturated_add(short *input, short *value)
00277 {
00278 int res;
00279
00280 res = (int) *input + *value;
00281 if (res > 32767)
00282 *input = 32767;
00283 else if (res < -32767)
00284 *input = -32767;
00285 else
00286 *input = (short) res;
00287 }
00288
00289 static force_inline void ast_slinear_saturated_subtract(short *input, short *value)
00290 {
00291 int res;
00292
00293 res = (int) *input - *value;
00294 if (res > 32767)
00295 *input = 32767;
00296 else if (res < -32767)
00297 *input = -32767;
00298 else
00299 *input = (short) res;
00300 }
00301
00302 static force_inline void ast_slinear_saturated_multiply(short *input, short *value)
00303 {
00304 int res;
00305
00306 res = (int) *input * *value;
00307 if (res > 32767)
00308 *input = 32767;
00309 else if (res < -32767)
00310 *input = -32767;
00311 else
00312 *input = (short) res;
00313 }
00314
00315 static force_inline void ast_slinear_saturated_divide(short *input, short *value)
00316 {
00317 *input /= *value;
00318 }
00319
00320 #ifdef localtime_r
00321 #undef localtime_r
00322 #endif
00323 #define localtime_r __dont_use_localtime_r_use_ast_localtime_instead__
00324
00325 int ast_utils_init(void);
00326 int ast_wait_for_input(int fd, int ms);
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337 int ast_carefulwrite(int fd, char *s, int len, int timeoutms);
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 int ast_careful_fwrite(FILE *f, int fd, const char *s, size_t len, int timeoutms);
00357
00358
00359
00360
00361
00362 #define AST_STACKSIZE (((sizeof(void *) * 8 * 8) - 16) * 1024)
00363
00364 #if defined(LOW_MEMORY)
00365 #define AST_BACKGROUND_STACKSIZE (((sizeof(void *) * 8 * 2) - 16) * 1024)
00366 #else
00367 #define AST_BACKGROUND_STACKSIZE AST_STACKSIZE
00368 #endif
00369
00370 void ast_register_thread(char *name);
00371 void ast_unregister_thread(void *id);
00372
00373 int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
00374 void *data, size_t stacksize, const char *file, const char *caller,
00375 int line, const char *start_fn);
00376
00377 int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr, void*(*start_routine)(void *),
00378 void *data, size_t stacksize, const char *file, const char *caller,
00379 int line, const char *start_fn);
00380
00381 #define ast_pthread_create(a, b, c, d) \
00382 ast_pthread_create_stack(a, b, c, d, \
00383 0, __FILE__, __FUNCTION__, __LINE__, #c)
00384
00385 #define ast_pthread_create_detached(a, b, c, d) \
00386 ast_pthread_create_detached_stack(a, b, c, d, \
00387 0, __FILE__, __FUNCTION__, __LINE__, #c)
00388
00389 #define ast_pthread_create_background(a, b, c, d) \
00390 ast_pthread_create_stack(a, b, c, d, \
00391 AST_BACKGROUND_STACKSIZE, \
00392 __FILE__, __FUNCTION__, __LINE__, #c)
00393
00394 #define ast_pthread_create_detached_background(a, b, c, d) \
00395 ast_pthread_create_detached_stack(a, b, c, d, \
00396 AST_BACKGROUND_STACKSIZE, \
00397 __FILE__, __FUNCTION__, __LINE__, #c)
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
00408
00409 long int ast_random(void);
00410
00411
00412
00413
00414
00415
00416
00417
00418 #ifdef __AST_DEBUG_MALLOC
00419 static void ast_free_ptr(void *ptr) attribute_unused;
00420 static void ast_free_ptr(void *ptr)
00421 {
00422 ast_free(ptr);
00423 }
00424 #else
00425 #define ast_free free
00426 #define ast_free_ptr ast_free
00427 #endif
00428
00429 #ifndef __AST_DEBUG_MALLOC
00430
00431 #define MALLOC_FAILURE_MSG \
00432 ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file);
00433
00434
00435
00436
00437
00438
00439
00440
00441 #define ast_malloc(len) \
00442 _ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00443
00444 AST_INLINE_API(
00445 void * attribute_malloc _ast_malloc(size_t len, const char *file, int lineno, const char *func),
00446 {
00447 void *p;
00448
00449 if (!(p = malloc(len)))
00450 MALLOC_FAILURE_MSG;
00451
00452 return p;
00453 }
00454 )
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 #define ast_calloc(num, len) \
00465 _ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00466
00467 AST_INLINE_API(
00468 void * attribute_malloc _ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func),
00469 {
00470 void *p;
00471
00472 if (!(p = calloc(num, len)))
00473 MALLOC_FAILURE_MSG;
00474
00475 return p;
00476 }
00477 )
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 #define ast_calloc_cache(num, len) \
00490 _ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500 #define ast_realloc(p, len) \
00501 _ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00502
00503 AST_INLINE_API(
00504 void * attribute_malloc _ast_realloc(void *p, size_t len, const char *file, int lineno, const char *func),
00505 {
00506 void *newp;
00507
00508 if (!(newp = realloc(p, len)))
00509 MALLOC_FAILURE_MSG;
00510
00511 return newp;
00512 }
00513 )
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 #define ast_strdup(str) \
00528 _ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00529
00530 AST_INLINE_API(
00531 char * attribute_malloc _ast_strdup(const char *str, const char *file, int lineno, const char *func),
00532 {
00533 char *newstr = NULL;
00534
00535 if (str) {
00536 if (!(newstr = strdup(str)))
00537 MALLOC_FAILURE_MSG;
00538 }
00539
00540 return newstr;
00541 }
00542 )
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 #define ast_strndup(str, len) \
00557 _ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00558
00559 AST_INLINE_API(
00560 char * attribute_malloc _ast_strndup(const char *str, size_t len, const char *file, int lineno, const char *func),
00561 {
00562 char *newstr = NULL;
00563
00564 if (str) {
00565 if (!(newstr = strndup(str, len)))
00566 MALLOC_FAILURE_MSG;
00567 }
00568
00569 return newstr;
00570 }
00571 )
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581 #define ast_asprintf(ret, fmt, ...) \
00582 _ast_asprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, __VA_ARGS__)
00583
00584 int __attribute__((format(printf, 5, 6)))
00585 _ast_asprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, ...);
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595 #define ast_vasprintf(ret, fmt, ap) \
00596 _ast_vasprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, (fmt), (ap))
00597
00598 AST_INLINE_API(
00599 __attribute__((format(printf, 5, 0)))
00600 int _ast_vasprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, va_list ap),
00601 {
00602 int res;
00603
00604 if ((res = vasprintf(ret, fmt, ap)) == -1)
00605 MALLOC_FAILURE_MSG;
00606
00607 return res;
00608 }
00609 )
00610
00611 #endif
00612
00613 #if !defined(ast_strdupa) && defined(__GNUC__)
00614
00615
00616
00617
00618
00619
00620
00621 #define ast_strdupa(s) \
00622 (__extension__ \
00623 ({ \
00624 const char *__old = (s); \
00625 size_t __len = strlen(__old) + 1; \
00626 char *__new = __builtin_alloca(__len); \
00627 memcpy (__new, __old, __len); \
00628 __new; \
00629 }))
00630 #endif
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645 void ast_enable_packet_fragmentation(int sock);
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655 int ast_mkdir(const char *path, int mode);
00656
00657 #define ARRAY_LEN(a) (sizeof(a) / sizeof(0[a]))
00658
00659
00660
00661 struct ast_http_digest {
00662 AST_DECLARE_STRING_FIELDS(
00663 AST_STRING_FIELD(username);
00664 AST_STRING_FIELD(nonce);
00665 AST_STRING_FIELD(uri);
00666 AST_STRING_FIELD(realm);
00667 AST_STRING_FIELD(domain);
00668 AST_STRING_FIELD(response);
00669 AST_STRING_FIELD(cnonce);
00670 AST_STRING_FIELD(opaque);
00671 AST_STRING_FIELD(nc);
00672 );
00673 int qop;
00674 };
00675
00676
00677
00678
00679
00680
00681
00682
00683 int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic);
00684
00685
00686 #ifdef AST_DEVMODE
00687 #define ast_assert(a) _ast_assert(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
00688 static void force_inline _ast_assert(int condition, const char *condition_str,
00689 const char *file, int line, const char *function)
00690 {
00691 if (__builtin_expect(!condition, 1)) {
00692
00693
00694 ast_log(__LOG_ERROR, file, line, function, "FRACK!, Failed assertion %s (%d)\n",
00695 condition_str, condition);
00696 fprintf(stderr, "FRACK!, Failed assertion %s (%d) at line %d in %s of %s\n",
00697 condition_str, condition, line, function, file);
00698
00699
00700 usleep(1);
00701 #ifdef DO_CRASH
00702 abort();
00703
00704
00705 *((int*)0)=0;
00706 #endif
00707 }
00708 }
00709 #else
00710 #define ast_assert(a)
00711 #endif
00712
00713 #include "asterisk/strings.h"
00714
00715
00716
00717
00718 struct ast_eid {
00719 unsigned char eid[6];
00720 } __attribute__((__packed__));
00721
00722
00723
00724
00725
00726
00727
00728 extern struct ast_eid ast_eid_default;
00729
00730
00731
00732
00733
00734 void ast_set_default_eid(struct ast_eid *eid);
00735
00736
00737
00738
00739
00740 char *ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid);
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751 int ast_str_to_eid(struct ast_eid *eid, const char *s);
00752
00753
00754
00755
00756
00757
00758
00759 int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2);
00760
00761
00762
00763
00764
00765
00766
00767
00768 char *ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size);
00769
00770 #endif