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