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 struct ast_flags {
00200 unsigned int flags;
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, const char *input);
00218
00219 void ast_sha1_hash(char *output, const char *input);
00220
00221 void ast_sha1_hash_uint(uint8_t *digest, 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 #define SWAP(a,b) do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max);
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 int ast_base64decode(unsigned char *dst, const char *src, int max);
00252
00253 #define AST_URI_ALPHANUM (1 << 0)
00254 #define AST_URI_MARK (1 << 1)
00255 #define AST_URI_UNRESERVED (AST_URI_ALPHANUM | AST_URI_MARK)
00256 #define AST_URI_LEGACY_SPACE (1 << 2)
00257
00258 #define AST_URI_SIP_USER_UNRESERVED (1 << 20)
00259
00260 extern const struct ast_flags ast_uri_http;
00261 extern const struct ast_flags ast_uri_http_legacy;
00262 extern const struct ast_flags ast_uri_sip_user;
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 char *ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec);
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 void ast_uri_decode(char *s, struct ast_flags spec);
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 int ast_xml_escape(const char *string, char *outbuf, size_t buflen);
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 char *ast_escape_quoted(const char *string, char *outbuf, int buflen);
00319
00320 static force_inline void ast_slinear_saturated_add(short *input, short *value)
00321 {
00322 int res;
00323
00324 res = (int) *input + *value;
00325 if (res > 32767)
00326 *input = 32767;
00327 else if (res < -32768)
00328 *input = -32768;
00329 else
00330 *input = (short) res;
00331 }
00332
00333 static force_inline void ast_slinear_saturated_subtract(short *input, short *value)
00334 {
00335 int res;
00336
00337 res = (int) *input - *value;
00338 if (res > 32767)
00339 *input = 32767;
00340 else if (res < -32768)
00341 *input = -32768;
00342 else
00343 *input = (short) res;
00344 }
00345
00346 static force_inline void ast_slinear_saturated_multiply(short *input, short *value)
00347 {
00348 int res;
00349
00350 res = (int) *input * *value;
00351 if (res > 32767)
00352 *input = 32767;
00353 else if (res < -32768)
00354 *input = -32768;
00355 else
00356 *input = (short) res;
00357 }
00358
00359 static force_inline void ast_slinear_saturated_divide(short *input, short *value)
00360 {
00361 *input /= *value;
00362 }
00363
00364 #ifdef localtime_r
00365 #undef localtime_r
00366 #endif
00367 #define localtime_r __dont_use_localtime_r_use_ast_localtime_instead__
00368
00369 int ast_utils_init(void);
00370 int ast_wait_for_input(int fd, int ms);
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 int ast_carefulwrite(int fd, char *s, int len, int timeoutms);
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 int ast_careful_fwrite(FILE *f, int fd, const char *s, size_t len, int timeoutms);
00401
00402
00403
00404
00405
00406 #define AST_STACKSIZE (((sizeof(void *) * 8 * 8) - 16) * 1024)
00407
00408 #if defined(LOW_MEMORY)
00409 #define AST_BACKGROUND_STACKSIZE (((sizeof(void *) * 8 * 2) - 16) * 1024)
00410 #else
00411 #define AST_BACKGROUND_STACKSIZE AST_STACKSIZE
00412 #endif
00413
00414 void ast_register_thread(char *name);
00415 void ast_unregister_thread(void *id);
00416
00417 int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
00418 void *data, size_t stacksize, const char *file, const char *caller,
00419 int line, const char *start_fn);
00420
00421 int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr, void*(*start_routine)(void *),
00422 void *data, size_t stacksize, const char *file, const char *caller,
00423 int line, const char *start_fn);
00424
00425 #define ast_pthread_create(a, b, c, d) \
00426 ast_pthread_create_stack(a, b, c, d, \
00427 0, __FILE__, __FUNCTION__, __LINE__, #c)
00428
00429 #define ast_pthread_create_detached(a, b, c, d) \
00430 ast_pthread_create_detached_stack(a, b, c, d, \
00431 0, __FILE__, __FUNCTION__, __LINE__, #c)
00432
00433 #define ast_pthread_create_background(a, b, c, d) \
00434 ast_pthread_create_stack(a, b, c, d, \
00435 AST_BACKGROUND_STACKSIZE, \
00436 __FILE__, __FUNCTION__, __LINE__, #c)
00437
00438 #define ast_pthread_create_detached_background(a, b, c, d) \
00439 ast_pthread_create_detached_stack(a, b, c, d, \
00440 AST_BACKGROUND_STACKSIZE, \
00441 __FILE__, __FUNCTION__, __LINE__, #c)
00442
00443
00444
00445
00446
00447
00448
00449 void ast_replace_subargument_delimiter(char *s);
00450
00451
00452
00453
00454
00455
00456
00457 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
00458
00459 long int ast_random(void);
00460
00461
00462 #ifndef __AST_DEBUG_MALLOC
00463 #define ast_std_malloc malloc
00464 #define ast_std_calloc calloc
00465 #define ast_std_realloc realloc
00466 #define ast_std_free free
00467
00468
00469
00470
00471
00472
00473
00474 #define ast_free free
00475 #define ast_free_ptr ast_free
00476
00477 #define MALLOC_FAILURE_MSG \
00478 ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file);
00479
00480
00481
00482
00483
00484
00485
00486
00487 #define ast_malloc(len) \
00488 _ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00489
00490 AST_INLINE_API(
00491 void * attribute_malloc _ast_malloc(size_t len, const char *file, int lineno, const char *func),
00492 {
00493 void *p;
00494
00495 if (!(p = malloc(len)))
00496 MALLOC_FAILURE_MSG;
00497
00498 return p;
00499 }
00500 )
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510 #define ast_calloc(num, len) \
00511 _ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00512
00513 AST_INLINE_API(
00514 void * attribute_malloc _ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func),
00515 {
00516 void *p;
00517
00518 if (!(p = calloc(num, len)))
00519 MALLOC_FAILURE_MSG;
00520
00521 return p;
00522 }
00523 )
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535 #define ast_calloc_cache(num, len) \
00536 _ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 #define ast_realloc(p, len) \
00547 _ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00548
00549 AST_INLINE_API(
00550 void * attribute_malloc _ast_realloc(void *p, size_t len, const char *file, int lineno, const char *func),
00551 {
00552 void *newp;
00553
00554 if (!(newp = realloc(p, len)))
00555 MALLOC_FAILURE_MSG;
00556
00557 return newp;
00558 }
00559 )
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573 #define ast_strdup(str) \
00574 _ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00575
00576 AST_INLINE_API(
00577 char * attribute_malloc _ast_strdup(const char *str, const char *file, int lineno, const char *func),
00578 {
00579 char *newstr = NULL;
00580
00581 if (str) {
00582 if (!(newstr = strdup(str)))
00583 MALLOC_FAILURE_MSG;
00584 }
00585
00586 return newstr;
00587 }
00588 )
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602 #define ast_strndup(str, len) \
00603 _ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
00604
00605 AST_INLINE_API(
00606 char * attribute_malloc _ast_strndup(const char *str, size_t len, const char *file, int lineno, const char *func),
00607 {
00608 char *newstr = NULL;
00609
00610 if (str) {
00611 if (!(newstr = strndup(str, len)))
00612 MALLOC_FAILURE_MSG;
00613 }
00614
00615 return newstr;
00616 }
00617 )
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627 #define ast_asprintf(ret, fmt, ...) \
00628 _ast_asprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, __VA_ARGS__)
00629
00630 int __attribute__((format(printf, 5, 6)))
00631 _ast_asprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, ...);
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641 #define ast_vasprintf(ret, fmt, ap) \
00642 _ast_vasprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, (fmt), (ap))
00643
00644 AST_INLINE_API(
00645 __attribute__((format(printf, 5, 0)))
00646 int _ast_vasprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, va_list ap),
00647 {
00648 int res;
00649
00650 if ((res = vasprintf(ret, fmt, ap)) == -1)
00651 MALLOC_FAILURE_MSG;
00652
00653 return res;
00654 }
00655 )
00656
00657 #endif
00658
00659
00660
00661
00662
00663
00664
00665
00666 #define ast_alloca(size) __builtin_alloca(size)
00667
00668 #if !defined(ast_strdupa) && defined(__GNUC__)
00669
00670
00671
00672
00673
00674
00675
00676 #define ast_strdupa(s) \
00677 (__extension__ \
00678 ({ \
00679 const char *__old = (s); \
00680 size_t __len = strlen(__old) + 1; \
00681 char *__new = __builtin_alloca(__len); \
00682 memcpy (__new, __old, __len); \
00683 __new; \
00684 }))
00685 #endif
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700 void ast_enable_packet_fragmentation(int sock);
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710 int ast_mkdir(const char *path, int mode);
00711
00712 #define ARRAY_LEN(a) (size_t) (sizeof(a) / sizeof(0[a]))
00713
00714
00715
00716 struct ast_http_digest {
00717 AST_DECLARE_STRING_FIELDS(
00718 AST_STRING_FIELD(username);
00719 AST_STRING_FIELD(nonce);
00720 AST_STRING_FIELD(uri);
00721 AST_STRING_FIELD(realm);
00722 AST_STRING_FIELD(domain);
00723 AST_STRING_FIELD(response);
00724 AST_STRING_FIELD(cnonce);
00725 AST_STRING_FIELD(opaque);
00726 AST_STRING_FIELD(nc);
00727 );
00728 int qop;
00729 };
00730
00731
00732
00733
00734
00735
00736
00737
00738 int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic);
00739
00740
00741 #ifdef AST_DEVMODE
00742 void __ast_assert_failed(int condition, const char *condition_str, const char *file, int line, const char *function);
00743 #define ast_assert(a) _ast_assert(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
00744 static void force_inline _ast_assert(int condition, const char *condition_str, const char *file, int line, const char *function)
00745 {
00746 if (__builtin_expect(!condition, 1)) {
00747 __ast_assert_failed(condition, condition_str, file, line, function);
00748 }
00749 }
00750 #else
00751 #define ast_assert(a)
00752 #endif
00753
00754
00755
00756
00757
00758
00759
00760
00761 void ast_do_crash(void);
00762
00763 #include "asterisk/strings.h"
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773 #define ast_alignof(type) __alignof__(type)
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793 #define ast_align_for(offset, type) (((offset + __alignof__(type) - 1) / __alignof__(type)) * __alignof__(type))
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816 #define ast_make_room_for(offset, type) (((offset + (2 * __alignof__(type) - 1)) / __alignof__(type)) * __alignof__(type))
00817
00818
00819
00820
00821 struct ast_eid {
00822 unsigned char eid[6];
00823 } __attribute__((__packed__));
00824
00825
00826
00827
00828
00829
00830
00831 extern struct ast_eid ast_eid_default;
00832
00833
00834
00835
00836
00837 void ast_set_default_eid(struct ast_eid *eid);
00838
00839
00840
00841
00842
00843 char *ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid);
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854 int ast_str_to_eid(struct ast_eid *eid, const char *s);
00855
00856
00857
00858
00859
00860
00861
00862 int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2);
00863
00864
00865
00866
00867
00868
00869 int ast_get_tid(void);
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879 char *ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size);
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925 #define RAII_VAR(vartype, varname, initval, dtor) \
00926 \
00927 auto void _dtor_ ## varname (vartype * v); \
00928 void _dtor_ ## varname (vartype * v) { dtor(*v); } \
00929 vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
00930
00931 #endif