2004-05-03 04:31:11 +00:00
|
|
|
/*
|
2005-08-30 18:32:10 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2004-05-03 04:31:11 +00:00
|
|
|
*
|
2006-01-04 21:54:09 +00:00
|
|
|
* Copyright (C) 1999 - 2006, Digium, Inc.
|
2005-08-30 18:32:10 +00:00
|
|
|
*
|
|
|
|
|
* Mark Spencer <markster@digium.com>
|
2004-05-03 04:31:11 +00:00
|
|
|
*
|
2005-08-30 18:32:10 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
|
* channels for your use.
|
2004-05-03 04:31:11 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software, distributed under the terms of
|
2005-08-30 18:32:10 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
|
* at the top of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
|
|
|
|
* \brief Utility functions
|
2004-05-03 04:31:11 +00:00
|
|
|
*/
|
|
|
|
|
|
2005-05-02 00:27:54 +00:00
|
|
|
#ifndef _ASTERISK_UTILS_H
|
|
|
|
|
#define _ASTERISK_UTILS_H
|
2004-05-03 04:31:11 +00:00
|
|
|
|
2007-11-17 14:11:53 +00:00
|
|
|
#include "asterisk/network.h"
|
|
|
|
|
|
2007-06-14 22:09:20 +00:00
|
|
|
#include <time.h> /* we want to override localtime_r */
|
2008-05-19 16:07:09 +00:00
|
|
|
#include <unistd.h>
|
2009-06-26 15:28:53 +00:00
|
|
|
#include <string.h>
|
2004-05-09 08:22:15 +00:00
|
|
|
|
2005-05-02 00:27:54 +00:00
|
|
|
#include "asterisk/lock.h"
|
2005-06-24 22:45:15 +00:00
|
|
|
#include "asterisk/time.h"
|
2006-01-10 23:51:42 +00:00
|
|
|
#include "asterisk/logger.h"
|
2007-06-14 22:09:20 +00:00
|
|
|
#include "asterisk/localtime.h"
|
2009-04-23 20:36:35 +00:00
|
|
|
#include "asterisk/stringfields.h"
|
2005-05-02 00:27:54 +00:00
|
|
|
|
2009-04-23 20:36:35 +00:00
|
|
|
/*!
|
2007-02-24 20:29:41 +00:00
|
|
|
\note \verbatim
|
2005-10-24 20:12:06 +00:00
|
|
|
Note:
|
2005-01-08 19:00:46 +00:00
|
|
|
It is very important to use only unsigned variables to hold
|
|
|
|
|
bit flags, as otherwise you can fall prey to the compiler's
|
|
|
|
|
sign-extension antics if you try to use the top two bits in
|
|
|
|
|
your variable.
|
2004-12-16 03:15:20 +00:00
|
|
|
|
2005-01-08 19:00:46 +00:00
|
|
|
The flag macros below use a set of compiler tricks to verify
|
After some study, thought, comparing, etc. I've backed out the previous universal mod to make ast_flags a 64 bit thing. Instead, I added a 64-bit version of ast_flags (ast_flags64), and 64-bit versions of the test-flag, set-flag, etc. macros, and an app_parse_options64 routine, and I use these in app_dial alone, to eliminate the 30-option limit it had grown to meet. There is room now for 32 more options and flags. I was heavily tempted to implement some of the other ideas that were presented, but this solution does not intro any new versions of dial, doesn't have a different API, has a minimal/zero impact on code outside of dial, and doesn't seriously (I hope) affect the code structure of dial. It's the best I can think of right now. My goal was NOT to rewrite dial. I leave that to a future, coordinated effort.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@75983 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-07-19 23:24:27 +00:00
|
|
|
that the caller is using an "unsigned int" variable to hold
|
2005-01-08 19:00:46 +00:00
|
|
|
the flags, and nothing else. If the caller uses any other
|
|
|
|
|
type of variable, a warning message similar to this:
|
2004-12-16 03:15:20 +00:00
|
|
|
|
2005-01-08 19:00:46 +00:00
|
|
|
warning: comparison of distinct pointer types lacks cast
|
|
|
|
|
will be generated.
|
2004-12-16 03:15:20 +00:00
|
|
|
|
2005-01-08 19:00:46 +00:00
|
|
|
The "dummy" variable below is used to make these comparisons.
|
|
|
|
|
|
|
|
|
|
Also note that at -O2 or above, this type-safety checking
|
|
|
|
|
does _not_ produce any additional object code at all.
|
2005-10-24 20:12:06 +00:00
|
|
|
\endverbatim
|
2005-01-08 19:00:46 +00:00
|
|
|
*/
|
|
|
|
|
|
After some study, thought, comparing, etc. I've backed out the previous universal mod to make ast_flags a 64 bit thing. Instead, I added a 64-bit version of ast_flags (ast_flags64), and 64-bit versions of the test-flag, set-flag, etc. macros, and an app_parse_options64 routine, and I use these in app_dial alone, to eliminate the 30-option limit it had grown to meet. There is room now for 32 more options and flags. I was heavily tempted to implement some of the other ideas that were presented, but this solution does not intro any new versions of dial, doesn't have a different API, has a minimal/zero impact on code outside of dial, and doesn't seriously (I hope) affect the code structure of dial. It's the best I can think of right now. My goal was NOT to rewrite dial. I leave that to a future, coordinated effort.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@75983 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-07-19 23:24:27 +00:00
|
|
|
extern unsigned int __unsigned_int_flags_dummy;
|
2005-01-08 19:00:46 +00:00
|
|
|
|
|
|
|
|
#define ast_test_flag(p,flag) ({ \
|
|
|
|
|
typeof ((p)->flags) __p = (p)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy) __x = 0; \
|
|
|
|
|
(void) (&__p == &__x); \
|
|
|
|
|
((p)->flags & (flag)); \
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
#define ast_set_flag(p,flag) do { \
|
|
|
|
|
typeof ((p)->flags) __p = (p)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy) __x = 0; \
|
|
|
|
|
(void) (&__p == &__x); \
|
|
|
|
|
((p)->flags |= (flag)); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define ast_clear_flag(p,flag) do { \
|
|
|
|
|
typeof ((p)->flags) __p = (p)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy) __x = 0; \
|
|
|
|
|
(void) (&__p == &__x); \
|
|
|
|
|
((p)->flags &= ~(flag)); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define ast_copy_flags(dest,src,flagz) do { \
|
|
|
|
|
typeof ((dest)->flags) __d = (dest)->flags; \
|
|
|
|
|
typeof ((src)->flags) __s = (src)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy) __x = 0; \
|
|
|
|
|
(void) (&__d == &__x); \
|
|
|
|
|
(void) (&__s == &__x); \
|
|
|
|
|
(dest)->flags &= ~(flagz); \
|
|
|
|
|
(dest)->flags |= ((src)->flags & (flagz)); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
#define ast_set2_flag(p,value,flag) do { \
|
|
|
|
|
typeof ((p)->flags) __p = (p)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy) __x = 0; \
|
|
|
|
|
(void) (&__p == &__x); \
|
|
|
|
|
if (value) \
|
|
|
|
|
(p)->flags |= (flag); \
|
|
|
|
|
else \
|
|
|
|
|
(p)->flags &= ~(flag); \
|
|
|
|
|
} while (0)
|
2004-12-16 03:15:20 +00:00
|
|
|
|
2006-05-10 09:09:16 +00:00
|
|
|
#define ast_set_flags_to(p,flag,value) do { \
|
|
|
|
|
typeof ((p)->flags) __p = (p)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy) __x = 0; \
|
|
|
|
|
(void) (&__p == &__x); \
|
|
|
|
|
(p)->flags &= ~(flag); \
|
|
|
|
|
(p)->flags |= (value); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
After some study, thought, comparing, etc. I've backed out the previous universal mod to make ast_flags a 64 bit thing. Instead, I added a 64-bit version of ast_flags (ast_flags64), and 64-bit versions of the test-flag, set-flag, etc. macros, and an app_parse_options64 routine, and I use these in app_dial alone, to eliminate the 30-option limit it had grown to meet. There is room now for 32 more options and flags. I was heavily tempted to implement some of the other ideas that were presented, but this solution does not intro any new versions of dial, doesn't have a different API, has a minimal/zero impact on code outside of dial, and doesn't seriously (I hope) affect the code structure of dial. It's the best I can think of right now. My goal was NOT to rewrite dial. I leave that to a future, coordinated effort.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@75983 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-07-19 23:24:27 +00:00
|
|
|
|
|
|
|
|
/* The following 64-bit flag code can most likely be erased after app_dial
|
|
|
|
|
is reorganized to either reduce the large number of options, or handle
|
|
|
|
|
them in some other way. At the time of this writing, app_dial would be
|
|
|
|
|
the only user of 64-bit option flags */
|
|
|
|
|
|
|
|
|
|
extern uint64_t __unsigned_int_flags_dummy64;
|
|
|
|
|
|
|
|
|
|
#define ast_test_flag64(p,flag) ({ \
|
|
|
|
|
typeof ((p)->flags) __p = (p)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy64) __x = 0; \
|
|
|
|
|
(void) (&__p == &__x); \
|
|
|
|
|
((p)->flags & (flag)); \
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
#define ast_set_flag64(p,flag) do { \
|
|
|
|
|
typeof ((p)->flags) __p = (p)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy64) __x = 0; \
|
|
|
|
|
(void) (&__p == &__x); \
|
|
|
|
|
((p)->flags |= (flag)); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define ast_clear_flag64(p,flag) do { \
|
|
|
|
|
typeof ((p)->flags) __p = (p)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy64) __x = 0; \
|
|
|
|
|
(void) (&__p == &__x); \
|
|
|
|
|
((p)->flags &= ~(flag)); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define ast_copy_flags64(dest,src,flagz) do { \
|
|
|
|
|
typeof ((dest)->flags) __d = (dest)->flags; \
|
|
|
|
|
typeof ((src)->flags) __s = (src)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy64) __x = 0; \
|
|
|
|
|
(void) (&__d == &__x); \
|
|
|
|
|
(void) (&__s == &__x); \
|
|
|
|
|
(dest)->flags &= ~(flagz); \
|
|
|
|
|
(dest)->flags |= ((src)->flags & (flagz)); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
#define ast_set2_flag64(p,value,flag) do { \
|
|
|
|
|
typeof ((p)->flags) __p = (p)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy64) __x = 0; \
|
|
|
|
|
(void) (&__p == &__x); \
|
|
|
|
|
if (value) \
|
|
|
|
|
(p)->flags |= (flag); \
|
|
|
|
|
else \
|
|
|
|
|
(p)->flags &= ~(flag); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
#define ast_set_flags_to64(p,flag,value) do { \
|
|
|
|
|
typeof ((p)->flags) __p = (p)->flags; \
|
|
|
|
|
typeof (__unsigned_int_flags_dummy64) __x = 0; \
|
|
|
|
|
(void) (&__p == &__x); \
|
|
|
|
|
(p)->flags &= ~(flag); \
|
|
|
|
|
(p)->flags |= (value); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
2005-01-10 14:46:59 +00:00
|
|
|
/* Non-type checking variations for non-unsigned int flags. You
|
2012-07-05 19:22:03 +00:00
|
|
|
should only use non-unsigned int flags where required by
|
2005-01-10 14:46:59 +00:00
|
|
|
protocol etc and if you know what you're doing :) */
|
2006-08-21 17:22:24 +00:00
|
|
|
#define ast_test_flag_nonstd(p,flag) \
|
|
|
|
|
((p)->flags & (flag))
|
2005-01-10 14:46:59 +00:00
|
|
|
|
|
|
|
|
#define ast_set_flag_nonstd(p,flag) do { \
|
|
|
|
|
((p)->flags |= (flag)); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define ast_clear_flag_nonstd(p,flag) do { \
|
|
|
|
|
((p)->flags &= ~(flag)); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define ast_copy_flags_nonstd(dest,src,flagz) do { \
|
|
|
|
|
(dest)->flags &= ~(flagz); \
|
|
|
|
|
(dest)->flags |= ((src)->flags & (flagz)); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
#define ast_set2_flag_nonstd(p,value,flag) do { \
|
|
|
|
|
if (value) \
|
|
|
|
|
(p)->flags |= (flag); \
|
|
|
|
|
else \
|
|
|
|
|
(p)->flags &= ~(flag); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
2004-12-23 11:32:22 +00:00
|
|
|
#define AST_FLAGS_ALL UINT_MAX
|
|
|
|
|
|
2012-07-05 19:22:03 +00:00
|
|
|
/*! \brief Structure used to handle boolean flags */
|
2005-01-08 19:00:46 +00:00
|
|
|
struct ast_flags {
|
After some study, thought, comparing, etc. I've backed out the previous universal mod to make ast_flags a 64 bit thing. Instead, I added a 64-bit version of ast_flags (ast_flags64), and 64-bit versions of the test-flag, set-flag, etc. macros, and an app_parse_options64 routine, and I use these in app_dial alone, to eliminate the 30-option limit it had grown to meet. There is room now for 32 more options and flags. I was heavily tempted to implement some of the other ideas that were presented, but this solution does not intro any new versions of dial, doesn't have a different API, has a minimal/zero impact on code outside of dial, and doesn't seriously (I hope) affect the code structure of dial. It's the best I can think of right now. My goal was NOT to rewrite dial. I leave that to a future, coordinated effort.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@75983 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-07-19 23:24:27 +00:00
|
|
|
unsigned int flags;
|
|
|
|
|
};
|
|
|
|
|
|
2012-07-05 19:22:03 +00:00
|
|
|
/*! \brief Structure used to handle a large number of boolean flags == used only in app_dial? */
|
After some study, thought, comparing, etc. I've backed out the previous universal mod to make ast_flags a 64 bit thing. Instead, I added a 64-bit version of ast_flags (ast_flags64), and 64-bit versions of the test-flag, set-flag, etc. macros, and an app_parse_options64 routine, and I use these in app_dial alone, to eliminate the 30-option limit it had grown to meet. There is room now for 32 more options and flags. I was heavily tempted to implement some of the other ideas that were presented, but this solution does not intro any new versions of dial, doesn't have a different API, has a minimal/zero impact on code outside of dial, and doesn't seriously (I hope) affect the code structure of dial. It's the best I can think of right now. My goal was NOT to rewrite dial. I leave that to a future, coordinated effort.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@75983 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-07-19 23:24:27 +00:00
|
|
|
struct ast_flags64 {
|
2007-07-17 19:40:29 +00:00
|
|
|
uint64_t flags;
|
2005-01-08 19:00:46 +00:00
|
|
|
};
|
|
|
|
|
|
2004-05-09 08:22:15 +00:00
|
|
|
struct ast_hostent {
|
|
|
|
|
struct hostent hp;
|
|
|
|
|
char buf[1024];
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-11 15:05:49 -04:00
|
|
|
/*!
|
|
|
|
|
* \brief Thread-safe gethostbyname function to use in Asterisk
|
|
|
|
|
*
|
|
|
|
|
* \deprecated Replaced by \c ast_sockaddr_resolve() and \c ast_sockaddr_resolve_first_af()
|
|
|
|
|
* \note To be removed in Asterisk 23.
|
|
|
|
|
*/
|
2005-11-01 20:09:09 +00:00
|
|
|
struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp);
|
2005-08-29 22:39:39 +00:00
|
|
|
|
2012-07-05 19:22:03 +00:00
|
|
|
/*! \brief Produces MD5 hash based on input string */
|
2009-05-21 21:13:09 +00:00
|
|
|
void ast_md5_hash(char *output, const char *input);
|
2007-02-24 20:29:41 +00:00
|
|
|
/*! \brief Produces SHA1 hash based on input string */
|
2009-05-21 21:13:09 +00:00
|
|
|
void ast_sha1_hash(char *output, const char *input);
|
2012-06-02 21:13:36 +00:00
|
|
|
/*! \brief Produces SHA1 hash based on input string, stored in uint8_t array */
|
|
|
|
|
void ast_sha1_hash_uint(uint8_t *digest, const char *input);
|
2005-08-29 22:39:39 +00:00
|
|
|
|
2006-06-01 19:05:45 +00:00
|
|
|
int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks);
|
2007-03-06 23:58:38 +00:00
|
|
|
|
2009-07-28 13:49:46 +00:00
|
|
|
#undef MIN
|
|
|
|
|
#define MIN(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a > __b) ? __b : __a);})
|
|
|
|
|
#undef MAX
|
|
|
|
|
#define MAX(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a < __b) ? __b : __a);})
|
|
|
|
|
|
2012-02-29 16:52:47 +00:00
|
|
|
#define SWAP(a,b) do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
|
|
|
|
|
|
2007-03-06 23:58:38 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Encode data in base64
|
|
|
|
|
* \param dst the destination buffer
|
|
|
|
|
* \param src the source data to be encoded
|
|
|
|
|
* \param srclen the number of bytes present in the source buffer
|
|
|
|
|
* \param max the maximum number of bytes to write into the destination
|
|
|
|
|
* buffer, *including* the terminating NULL character.
|
|
|
|
|
*/
|
2005-11-01 20:09:09 +00:00
|
|
|
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max);
|
2007-03-06 23:58:38 +00:00
|
|
|
|
res_stir_shaken: Add outbound INVITE support.
Integrated STIR/SHAKEN support with outgoing INVITEs. When an INVITE is
sent, the caller ID will be checked to see if there is a certificate
that corresponds to it. If so, that information will be retrieved and an
Identity header will be added to the SIP message. The format is:
header.payload.signature;info=<public_key_url>alg=ES256;ppt=shaken
Header, payload, and signature are all BASE64 encoded. The public key
URL is retrieved from the certificate. Currently the algorithm and ppt
are ES256 and shaken, respectively. This message is signed and can be
used for verification on the receiving end.
Two new configuration options have been added to the certificate object:
attestation and origid. The attestation is required and must be A, B, or
C. origid is the origination identifier.
A new utility function has been added as well that takes a string,
allocates space, BASE64 encodes it, then returns it, eliminating the
need to calculate the size yourself.
Change-Id: I1f84d6a5839cb2ed152ef4255b380cfc2de662b4
2020-06-02 09:04:23 -05:00
|
|
|
/*!
|
|
|
|
|
* \brief Same as ast_base64encode, but does hte math for you and returns
|
|
|
|
|
* an encoded string
|
|
|
|
|
*
|
|
|
|
|
* \note The returned string will need to be freed later
|
|
|
|
|
*
|
|
|
|
|
* \param src The source buffer
|
|
|
|
|
*
|
|
|
|
|
* \retval NULL on failure
|
2021-11-19 16:47:25 +01:00
|
|
|
* \return Encoded string on success
|
res_stir_shaken: Add outbound INVITE support.
Integrated STIR/SHAKEN support with outgoing INVITEs. When an INVITE is
sent, the caller ID will be checked to see if there is a certificate
that corresponds to it. If so, that information will be retrieved and an
Identity header will be added to the SIP message. The format is:
header.payload.signature;info=<public_key_url>alg=ES256;ppt=shaken
Header, payload, and signature are all BASE64 encoded. The public key
URL is retrieved from the certificate. Currently the algorithm and ppt
are ES256 and shaken, respectively. This message is signed and can be
used for verification on the receiving end.
Two new configuration options have been added to the certificate object:
attestation and origid. The attestation is required and must be A, B, or
C. origid is the origination identifier.
A new utility function has been added as well that takes a string,
allocates space, BASE64 encodes it, then returns it, eliminating the
need to calculate the size yourself.
Change-Id: I1f84d6a5839cb2ed152ef4255b380cfc2de662b4
2020-06-02 09:04:23 -05:00
|
|
|
*/
|
|
|
|
|
char *ast_base64encode_string(const char *src);
|
|
|
|
|
|
2007-03-06 23:58:38 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Decode data from base64
|
|
|
|
|
* \param dst the destination buffer
|
|
|
|
|
* \param src the source buffer
|
|
|
|
|
* \param max The maximum number of bytes to write into the destination
|
|
|
|
|
* buffer. Note that this function will not ensure that the
|
|
|
|
|
* destination buffer is NULL terminated. So, in general,
|
|
|
|
|
* this parameter should be sizeof(dst) - 1.
|
|
|
|
|
*/
|
2005-11-01 20:09:09 +00:00
|
|
|
int ast_base64decode(unsigned char *dst, const char *src, int max);
|
res_stir_shaken: Add inbound INVITE support.
Integrated STIR/SHAKEN support with incoming INVITES. Upon receiving an
INVITE, the Identity header is retrieved, parsing the message to verify
the signature. If any of the parsing fails,
AST_STIR_SHAKEN_VERIFY_NOT_PRESENT will be added to the channel for this
caller ID. If verification itself fails,
AST_STIR_SHAKEN_VERIFY_SIGNATURE_FAILED will be added. If anything in
the payload does not line up with the SIP signaling,
AST_STIR_SHAKEN_VERIFY_MISMATCH will be added. If all of the above steps
pass, then AST_STIR_SHAKEN_VERIFY_PASSED will be added, completing the
verification process.
A new config option has been added to the general section for
stir_shaken.conf. "signature_timeout" is the amount of time a signature
will be considered valid. If an INVITE is received and the amount of
time between when it was received and when it was signed is greater than
signature_timeout, verification will fail.
Some changes were also made to signing and verification. There was an
error where the whole JSON string was being signed rather than the
header combined with the payload. This has been changed to sign the
correct thing. Verification has been changed to do this as well, and the
unit tests have been updated to reflect these changes.
A couple of utility functions have also been added. One decodes a BASE64
string and returns the decoded string, doing all the length calculations
for you. The other retrieves a string value from a header in a rdata
object.
Change-Id: I855f857be3d1c63b64812ac35d9ce0534085b913
2020-05-19 14:46:45 -05:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Same as ast_base64decode, but does the math for you and returns
|
|
|
|
|
* a decoded string
|
|
|
|
|
*
|
2020-08-04 14:36:22 -05:00
|
|
|
* \note The returned string will need to be freed later and IS NULL terminated
|
res_stir_shaken: Add inbound INVITE support.
Integrated STIR/SHAKEN support with incoming INVITES. Upon receiving an
INVITE, the Identity header is retrieved, parsing the message to verify
the signature. If any of the parsing fails,
AST_STIR_SHAKEN_VERIFY_NOT_PRESENT will be added to the channel for this
caller ID. If verification itself fails,
AST_STIR_SHAKEN_VERIFY_SIGNATURE_FAILED will be added. If anything in
the payload does not line up with the SIP signaling,
AST_STIR_SHAKEN_VERIFY_MISMATCH will be added. If all of the above steps
pass, then AST_STIR_SHAKEN_VERIFY_PASSED will be added, completing the
verification process.
A new config option has been added to the general section for
stir_shaken.conf. "signature_timeout" is the amount of time a signature
will be considered valid. If an INVITE is received and the amount of
time between when it was received and when it was signed is greater than
signature_timeout, verification will fail.
Some changes were also made to signing and verification. There was an
error where the whole JSON string was being signed rather than the
header combined with the payload. This has been changed to sign the
correct thing. Verification has been changed to do this as well, and the
unit tests have been updated to reflect these changes.
A couple of utility functions have also been added. One decodes a BASE64
string and returns the decoded string, doing all the length calculations
for you. The other retrieves a string value from a header in a rdata
object.
Change-Id: I855f857be3d1c63b64812ac35d9ce0534085b913
2020-05-19 14:46:45 -05:00
|
|
|
*
|
|
|
|
|
* \param src The source buffer
|
|
|
|
|
*
|
|
|
|
|
* \retval NULL on failure
|
2021-11-19 16:47:25 +01:00
|
|
|
* \return Decoded string on success
|
res_stir_shaken: Add inbound INVITE support.
Integrated STIR/SHAKEN support with incoming INVITES. Upon receiving an
INVITE, the Identity header is retrieved, parsing the message to verify
the signature. If any of the parsing fails,
AST_STIR_SHAKEN_VERIFY_NOT_PRESENT will be added to the channel for this
caller ID. If verification itself fails,
AST_STIR_SHAKEN_VERIFY_SIGNATURE_FAILED will be added. If anything in
the payload does not line up with the SIP signaling,
AST_STIR_SHAKEN_VERIFY_MISMATCH will be added. If all of the above steps
pass, then AST_STIR_SHAKEN_VERIFY_PASSED will be added, completing the
verification process.
A new config option has been added to the general section for
stir_shaken.conf. "signature_timeout" is the amount of time a signature
will be considered valid. If an INVITE is received and the amount of
time between when it was received and when it was signed is greater than
signature_timeout, verification will fail.
Some changes were also made to signing and verification. There was an
error where the whole JSON string was being signed rather than the
header combined with the payload. This has been changed to sign the
correct thing. Verification has been changed to do this as well, and the
unit tests have been updated to reflect these changes.
A couple of utility functions have also been added. One decodes a BASE64
string and returns the decoded string, doing all the length calculations
for you. The other retrieves a string value from a header in a rdata
object.
Change-Id: I855f857be3d1c63b64812ac35d9ce0534085b913
2020-05-19 14:46:45 -05:00
|
|
|
*/
|
|
|
|
|
char *ast_base64decode_string(const char *src);
|
2021-04-26 17:00:11 -05:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Decode data from base64 URL
|
|
|
|
|
*
|
|
|
|
|
* \param dst The destination buffer
|
|
|
|
|
* \param src The source buffer
|
|
|
|
|
* \param max The maximum number of bytes to write into the destination
|
|
|
|
|
* buffer. Note that this function will not ensure that the
|
|
|
|
|
* destination buffer is NULL terminated. So, in general,
|
|
|
|
|
* this parameter should be sizeof(dst) - 1
|
|
|
|
|
*/
|
|
|
|
|
int ast_base64url_decode(unsigned char *dst, const char *src, int max);
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Same as ast_base64encode_full but for base64 URL
|
|
|
|
|
*
|
|
|
|
|
* \param dst The destination buffer
|
|
|
|
|
* \param src The source buffer
|
|
|
|
|
* \param srclen The number of bytes present in the source buffer
|
|
|
|
|
* \param max The maximum number of bytes to write into the destination
|
|
|
|
|
* buffer, *including* the terminating NULL character.
|
|
|
|
|
* \param linebreaks Set to 1 if there should be linebreaks inserted
|
|
|
|
|
* in the result
|
|
|
|
|
*/
|
|
|
|
|
int ast_base64url_encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks);
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Encode data in base64 URL
|
|
|
|
|
*
|
|
|
|
|
* \param dst The destination buffer
|
|
|
|
|
* \param src The source data to be encoded
|
|
|
|
|
* \param srclen The number of bytes present in the source buffer
|
|
|
|
|
* \param max The maximum number of bytes to write into the destination
|
|
|
|
|
* buffer, including the terminating NULL character
|
|
|
|
|
*/
|
|
|
|
|
int ast_base64url_encode(char *dst, const unsigned char *src, int srclen, int max);
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Decode string from base64 URL
|
|
|
|
|
*
|
|
|
|
|
* \note The returned string will need to be freed later
|
|
|
|
|
*
|
|
|
|
|
* \param src The source buffer
|
|
|
|
|
*
|
|
|
|
|
* \retval NULL on failure
|
2021-11-19 16:47:25 +01:00
|
|
|
* \return Decoded string on success
|
2021-04-26 17:00:11 -05:00
|
|
|
*/
|
|
|
|
|
char *ast_base64url_decode_string(const char *src);
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Encode string in base64 URL
|
|
|
|
|
*
|
|
|
|
|
* \note The returned string will need to be freed later
|
|
|
|
|
*
|
|
|
|
|
* \param src The source data to be encoded
|
|
|
|
|
*
|
|
|
|
|
* \retval NULL on failure
|
2021-11-19 16:47:25 +01:00
|
|
|
* \return Encoded string on success
|
2021-04-26 17:00:11 -05:00
|
|
|
*/
|
|
|
|
|
char *ast_base64url_encode_string(const char *src);
|
2021-11-01 15:40:42 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Performs a base 64 encode algorithm on the contents of a File
|
|
|
|
|
* \param inputfile A FILE handle to the input file to be encoded. Must be readable. This handle is not automatically closed.
|
|
|
|
|
* \param outputfile A FILE handle to the output file to receive the base 64 encoded contents of the input file, identified by filename.
|
|
|
|
|
* \param endl The line ending to use (e.g. either "\n" or "\r\n")
|
|
|
|
|
*
|
|
|
|
|
* \return zero on success, -1 on error.
|
|
|
|
|
*/
|
|
|
|
|
int ast_base64_encode_file(FILE *inputfile, FILE *outputfile, const char *endl);
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Performs a base 64 encode algorithm on the contents of a File
|
|
|
|
|
* \param filename The path to the file to be encoded. Must be readable, file is opened in read mode.
|
|
|
|
|
* \param outputfile A FILE handle to the output file to receive the base 64 encoded contents of the input file, identified by filename.
|
|
|
|
|
* \param endl The line ending to use (e.g. either "\n" or "\r\n")
|
|
|
|
|
*
|
|
|
|
|
* \return zero on success, -1 on error.
|
|
|
|
|
*/
|
|
|
|
|
int ast_base64_encode_file_path(const char *filename, FILE *outputfile, const char *endl);
|
2004-05-09 08:22:15 +00:00
|
|
|
|
2011-01-24 18:59:22 +00:00
|
|
|
#define AST_URI_ALPHANUM (1 << 0)
|
|
|
|
|
#define AST_URI_MARK (1 << 1)
|
|
|
|
|
#define AST_URI_UNRESERVED (AST_URI_ALPHANUM | AST_URI_MARK)
|
|
|
|
|
#define AST_URI_LEGACY_SPACE (1 << 2)
|
|
|
|
|
|
|
|
|
|
#define AST_URI_SIP_USER_UNRESERVED (1 << 20)
|
|
|
|
|
|
|
|
|
|
extern const struct ast_flags ast_uri_http;
|
|
|
|
|
extern const struct ast_flags ast_uri_http_legacy;
|
|
|
|
|
extern const struct ast_flags ast_uri_sip_user;
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Turn text string to URI-encoded %XX version
|
RFC compliant uri and display-name encode/decode
1. URI Encoding
This patch changes ast_uri_encode()'s behavior when doreserved is enabled.
Previously when doreserved was enabled only a small set of reserved
characters were encoded. This set was comprised primarily of the reserved
characters defined in RFC3261 section 25.1, but contained other characters as
well. Rather than only escaping the reserved set, doreserved now escapes
all characters not within the unreserved set as defined by RFC 3261 and
RFC 2396. Also, the 'doreserved' variable has been renamed to 'do_special_char'
in attempts to avoid confusion.
When doreserve is not enabled, the previous logic of only encoding the
characters <= 0X1F and > 0X7f remains, except for the '%' character, which
must always be encoded as it signifies a HEX escaped character during the decode
process.
2. URI Decoding: Break up URI before decode.
In chan_sip.c ast_uri_decode is called on the entire URI instead of it's
individual parts after it is parsed. This is not good as ast_uri_decode
can introduce special characters back into the URI which can mess up parsing.
This patch resolves this by not decoding a URI until parsing is completely
done. There are many instances where we check to see if pedantic checking
is enabled before we decode a URI. In these cases a new macro,
SIP_PEDANTIC_DECODE, is used on the individual parsed segments of the URI
rather than constantly putting if (pedantic) { decode() } checks everywhere
in the code. In the areas where ast_uri_decode is not dependent upon
pedantic checking this macro is not used, but decoding is still moved to
each individual part of the URI. The only behavior that should change from
this patch is the time at which decoding occurs.
Since I had to look over every place URI parsing occurs to create this
patch, I found several places where we use duplicate code for parsing.
To consolidate the code, those areas have updated to use the parse_uri()
function where possible.
3. SIP display-name decoding according to RFC3261 section 25.
To properly decode the display-name portion of a FROM header, chan_sip's
get_calleridname() function required a complete re-write. More information
about this change can be found in the comments at the beginning of this function.
4. Unit Tests.
Unit tests for ast_uri_encode, ast_uri_decode, and get_calleridname() have been
written. This involved the addition of the test_utils.c file for testing the
utils api.
(closes issue #16299)
Reported by: wdoekes
Patches:
astsvn-16299-get_calleridname.diff uploaded by wdoekes (license 717)
get_calleridname_rewrite.diff uploaded by dvossel (license 671)
Tested by: wdoekes, dvossel, Nick_Lewis
Review: https://reviewboard.asterisk.org/r/469/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@243200 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-01-26 16:30:08 +00:00
|
|
|
*
|
2011-01-24 18:59:22 +00:00
|
|
|
* This function encodes characters according to the rules presented in RFC
|
|
|
|
|
* 2396 and/or RFC 3261 section 19.1.2 and section 25.1.
|
RFC compliant uri and display-name encode/decode
1. URI Encoding
This patch changes ast_uri_encode()'s behavior when doreserved is enabled.
Previously when doreserved was enabled only a small set of reserved
characters were encoded. This set was comprised primarily of the reserved
characters defined in RFC3261 section 25.1, but contained other characters as
well. Rather than only escaping the reserved set, doreserved now escapes
all characters not within the unreserved set as defined by RFC 3261 and
RFC 2396. Also, the 'doreserved' variable has been renamed to 'do_special_char'
in attempts to avoid confusion.
When doreserve is not enabled, the previous logic of only encoding the
characters <= 0X1F and > 0X7f remains, except for the '%' character, which
must always be encoded as it signifies a HEX escaped character during the decode
process.
2. URI Decoding: Break up URI before decode.
In chan_sip.c ast_uri_decode is called on the entire URI instead of it's
individual parts after it is parsed. This is not good as ast_uri_decode
can introduce special characters back into the URI which can mess up parsing.
This patch resolves this by not decoding a URI until parsing is completely
done. There are many instances where we check to see if pedantic checking
is enabled before we decode a URI. In these cases a new macro,
SIP_PEDANTIC_DECODE, is used on the individual parsed segments of the URI
rather than constantly putting if (pedantic) { decode() } checks everywhere
in the code. In the areas where ast_uri_decode is not dependent upon
pedantic checking this macro is not used, but decoding is still moved to
each individual part of the URI. The only behavior that should change from
this patch is the time at which decoding occurs.
Since I had to look over every place URI parsing occurs to create this
patch, I found several places where we use duplicate code for parsing.
To consolidate the code, those areas have updated to use the parse_uri()
function where possible.
3. SIP display-name decoding according to RFC3261 section 25.
To properly decode the display-name portion of a FROM header, chan_sip's
get_calleridname() function required a complete re-write. More information
about this change can be found in the comments at the beginning of this function.
4. Unit Tests.
Unit tests for ast_uri_encode, ast_uri_decode, and get_calleridname() have been
written. This involved the addition of the test_utils.c file for testing the
utils api.
(closes issue #16299)
Reported by: wdoekes
Patches:
astsvn-16299-get_calleridname.diff uploaded by wdoekes (license 717)
get_calleridname_rewrite.diff uploaded by dvossel (license 671)
Tested by: wdoekes, dvossel, Nick_Lewis
Review: https://reviewboard.asterisk.org/r/469/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@243200 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-01-26 16:30:08 +00:00
|
|
|
*
|
2011-01-24 18:59:22 +00:00
|
|
|
* Outbuf needs to have more memory allocated than the instring to have room
|
|
|
|
|
* for the expansion. Every byte that is converted is replaced by three ASCII
|
|
|
|
|
* characters.
|
|
|
|
|
*
|
|
|
|
|
* \param string string to be converted
|
|
|
|
|
* \param outbuf resulting encoded string
|
|
|
|
|
* \param buflen size of output buffer
|
|
|
|
|
* \param spec flags describing how the encoding should be performed
|
|
|
|
|
* \return a pointer to the uri encoded string
|
RFC compliant uri and display-name encode/decode
1. URI Encoding
This patch changes ast_uri_encode()'s behavior when doreserved is enabled.
Previously when doreserved was enabled only a small set of reserved
characters were encoded. This set was comprised primarily of the reserved
characters defined in RFC3261 section 25.1, but contained other characters as
well. Rather than only escaping the reserved set, doreserved now escapes
all characters not within the unreserved set as defined by RFC 3261 and
RFC 2396. Also, the 'doreserved' variable has been renamed to 'do_special_char'
in attempts to avoid confusion.
When doreserve is not enabled, the previous logic of only encoding the
characters <= 0X1F and > 0X7f remains, except for the '%' character, which
must always be encoded as it signifies a HEX escaped character during the decode
process.
2. URI Decoding: Break up URI before decode.
In chan_sip.c ast_uri_decode is called on the entire URI instead of it's
individual parts after it is parsed. This is not good as ast_uri_decode
can introduce special characters back into the URI which can mess up parsing.
This patch resolves this by not decoding a URI until parsing is completely
done. There are many instances where we check to see if pedantic checking
is enabled before we decode a URI. In these cases a new macro,
SIP_PEDANTIC_DECODE, is used on the individual parsed segments of the URI
rather than constantly putting if (pedantic) { decode() } checks everywhere
in the code. In the areas where ast_uri_decode is not dependent upon
pedantic checking this macro is not used, but decoding is still moved to
each individual part of the URI. The only behavior that should change from
this patch is the time at which decoding occurs.
Since I had to look over every place URI parsing occurs to create this
patch, I found several places where we use duplicate code for parsing.
To consolidate the code, those areas have updated to use the parse_uri()
function where possible.
3. SIP display-name decoding according to RFC3261 section 25.
To properly decode the display-name portion of a FROM header, chan_sip's
get_calleridname() function required a complete re-write. More information
about this change can be found in the comments at the beginning of this function.
4. Unit Tests.
Unit tests for ast_uri_encode, ast_uri_decode, and get_calleridname() have been
written. This involved the addition of the test_utils.c file for testing the
utils api.
(closes issue #16299)
Reported by: wdoekes
Patches:
astsvn-16299-get_calleridname.diff uploaded by wdoekes (license 717)
get_calleridname_rewrite.diff uploaded by dvossel (license 671)
Tested by: wdoekes, dvossel, Nick_Lewis
Review: https://reviewboard.asterisk.org/r/469/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@243200 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2010-01-26 16:30:08 +00:00
|
|
|
*/
|
2011-01-24 18:59:22 +00:00
|
|
|
char *ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec);
|
2005-08-29 22:39:39 +00:00
|
|
|
|
2011-01-24 18:59:22 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Decode URI, URN, URL (overwrite string)
|
|
|
|
|
*
|
|
|
|
|
* \note The ast_uri_http_legacy decode spec flag will cause this function to
|
|
|
|
|
* decode '+' as ' '.
|
|
|
|
|
*
|
|
|
|
|
* \param s string to be decoded
|
|
|
|
|
* \param spec flags describing how the decoding should be performed
|
|
|
|
|
*/
|
|
|
|
|
void ast_uri_decode(char *s, struct ast_flags spec);
|
|
|
|
|
|
2013-01-12 06:43:37 +00:00
|
|
|
/*! ast_xml_escape
|
|
|
|
|
\brief Escape reserved characters for use in XML.
|
|
|
|
|
|
|
|
|
|
If \a outbuf is too short, the output string will be truncated.
|
|
|
|
|
Regardless, the output will always be null terminated.
|
|
|
|
|
|
|
|
|
|
\param string String to be converted
|
|
|
|
|
\param outbuf Resulting encoded string
|
|
|
|
|
\param buflen Size of output buffer
|
2021-11-19 16:47:25 +01:00
|
|
|
\retval 0 for success
|
|
|
|
|
\retval -1 if buflen is too short.
|
2013-01-12 06:43:37 +00:00
|
|
|
*/
|
|
|
|
|
int ast_xml_escape(const char *string, char *outbuf, size_t buflen);
|
|
|
|
|
|
2011-01-24 18:59:22 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Escape characters found in a quoted string.
|
|
|
|
|
*
|
|
|
|
|
* \note This function escapes quoted characters based on the 'qdtext' set of
|
|
|
|
|
* allowed characters from RFC 3261 section 25.1.
|
|
|
|
|
*
|
|
|
|
|
* \param string string to be escaped
|
|
|
|
|
* \param outbuf resulting escaped string
|
|
|
|
|
* \param buflen size of output buffer
|
|
|
|
|
* \return a pointer to the escaped string
|
2005-08-29 22:39:39 +00:00
|
|
|
*/
|
2011-01-24 18:59:22 +00:00
|
|
|
char *ast_escape_quoted(const char *string, char *outbuf, int buflen);
|
2005-10-28 21:35:55 +00:00
|
|
|
|
2014-11-05 00:17:10 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Escape semicolons found in a string.
|
|
|
|
|
*
|
|
|
|
|
* \param string string to be escaped
|
|
|
|
|
* \param outbuf resulting escaped string
|
|
|
|
|
* \param buflen size of output buffer
|
|
|
|
|
* \return a pointer to the escaped string
|
|
|
|
|
*/
|
|
|
|
|
char *ast_escape_semicolons(const char *string, char *outbuf, int buflen);
|
|
|
|
|
|
2014-08-27 15:39:35 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Unescape quotes in a string
|
|
|
|
|
*
|
|
|
|
|
* \param quote_str The string with quotes to be unescaped
|
|
|
|
|
*
|
|
|
|
|
* \note This function mutates the passed-in string.
|
|
|
|
|
*/
|
|
|
|
|
void ast_unescape_quoted(char *quote_str);
|
|
|
|
|
|
2005-11-01 20:09:09 +00:00
|
|
|
static force_inline void ast_slinear_saturated_add(short *input, short *value)
|
2005-10-28 21:35:55 +00:00
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
|
2005-10-31 18:15:02 +00:00
|
|
|
res = (int) *input + *value;
|
2005-10-28 21:35:55 +00:00
|
|
|
if (res > 32767)
|
|
|
|
|
*input = 32767;
|
2013-04-30 13:48:12 +00:00
|
|
|
else if (res < -32768)
|
|
|
|
|
*input = -32768;
|
2005-10-28 21:35:55 +00:00
|
|
|
else
|
|
|
|
|
*input = (short) res;
|
|
|
|
|
}
|
2007-08-29 02:21:08 +00:00
|
|
|
|
|
|
|
|
static force_inline void ast_slinear_saturated_subtract(short *input, short *value)
|
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
res = (int) *input - *value;
|
|
|
|
|
if (res > 32767)
|
|
|
|
|
*input = 32767;
|
2013-04-30 13:48:12 +00:00
|
|
|
else if (res < -32768)
|
|
|
|
|
*input = -32768;
|
2007-08-29 02:21:08 +00:00
|
|
|
else
|
|
|
|
|
*input = (short) res;
|
|
|
|
|
}
|
2012-07-05 19:22:03 +00:00
|
|
|
|
2005-11-01 20:09:09 +00:00
|
|
|
static force_inline void ast_slinear_saturated_multiply(short *input, short *value)
|
2005-10-28 21:35:55 +00:00
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
|
2005-10-31 18:15:02 +00:00
|
|
|
res = (int) *input * *value;
|
2005-10-28 21:35:55 +00:00
|
|
|
if (res > 32767)
|
|
|
|
|
*input = 32767;
|
2013-04-30 13:48:12 +00:00
|
|
|
else if (res < -32768)
|
|
|
|
|
*input = -32768;
|
2005-10-28 21:35:55 +00:00
|
|
|
else
|
|
|
|
|
*input = (short) res;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 14:05:22 +02:00
|
|
|
static force_inline void ast_slinear_saturated_multiply_float(short *input, float *value)
|
|
|
|
|
{
|
|
|
|
|
float res;
|
|
|
|
|
|
|
|
|
|
res = (float) *input * *value;
|
|
|
|
|
if (res > 32767)
|
|
|
|
|
*input = 32767;
|
|
|
|
|
else if (res < -32768)
|
|
|
|
|
*input = -32768;
|
|
|
|
|
else
|
|
|
|
|
*input = (short) res;
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-01 20:09:09 +00:00
|
|
|
static force_inline void ast_slinear_saturated_divide(short *input, short *value)
|
2005-10-28 21:35:55 +00:00
|
|
|
{
|
2005-10-31 18:15:02 +00:00
|
|
|
*input /= *value;
|
2005-10-28 21:35:55 +00:00
|
|
|
}
|
2005-08-29 22:39:39 +00:00
|
|
|
|
2020-04-07 14:05:22 +02:00
|
|
|
static force_inline void ast_slinear_saturated_divide_float(short *input, float *value)
|
|
|
|
|
{
|
|
|
|
|
float res = (float) *input / *value;
|
|
|
|
|
if (res > 32767)
|
|
|
|
|
*input = 32767;
|
|
|
|
|
else if (res < -32768)
|
|
|
|
|
*input = -32768;
|
|
|
|
|
else
|
|
|
|
|
*input = (short) res;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-14 22:09:20 +00:00
|
|
|
#ifdef localtime_r
|
|
|
|
|
#undef localtime_r
|
|
|
|
|
#endif
|
|
|
|
|
#define localtime_r __dont_use_localtime_r_use_ast_localtime_instead__
|
|
|
|
|
|
2005-11-01 20:09:09 +00:00
|
|
|
int ast_utils_init(void);
|
|
|
|
|
int ast_wait_for_input(int fd, int ms);
|
2014-06-12 17:00:08 +00:00
|
|
|
int ast_wait_for_output(int fd, int ms);
|
2005-07-19 23:17:02 +00:00
|
|
|
|
2007-02-24 20:29:41 +00:00
|
|
|
/*!
|
2012-07-05 19:22:03 +00:00
|
|
|
* \brief Try to write string, but wait no more than ms milliseconds
|
|
|
|
|
* before timing out.
|
|
|
|
|
*
|
|
|
|
|
* \note If you are calling ast_carefulwrite, it is assumed that you are calling
|
|
|
|
|
* it on a file descriptor that _DOES_ have NONBLOCK set. This way,
|
|
|
|
|
* there is only one system call made to do a write, unless we actually
|
|
|
|
|
* have a need to wait. This way, we get better performance.
|
|
|
|
|
*/
|
2006-05-05 21:01:39 +00:00
|
|
|
int ast_carefulwrite(int fd, char *s, int len, int timeoutms);
|
|
|
|
|
|
2008-12-22 17:09:36 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Write data to a file stream with a timeout
|
|
|
|
|
*
|
|
|
|
|
* \param f the file stream to write to
|
|
|
|
|
* \param fd the file description to poll on to know when the file stream can
|
|
|
|
|
* be written to without blocking.
|
|
|
|
|
* \param s the buffer to write from
|
|
|
|
|
* \param len the number of bytes to write
|
|
|
|
|
* \param timeoutms The maximum amount of time to block in this function trying
|
|
|
|
|
* to write, specified in milliseconds.
|
|
|
|
|
*
|
|
|
|
|
* \note This function assumes that the associated file stream has been set up
|
|
|
|
|
* as non-blocking.
|
|
|
|
|
*
|
|
|
|
|
* \retval 0 success
|
|
|
|
|
* \retval -1 error
|
|
|
|
|
*/
|
|
|
|
|
int ast_careful_fwrite(FILE *f, int fd, const char *s, size_t len, int timeoutms);
|
|
|
|
|
|
2007-11-16 22:37:17 +00:00
|
|
|
/*
|
|
|
|
|
* Thread management support (should be moved to lock.h or a different header)
|
|
|
|
|
*/
|
2009-06-08 19:33:09 +00:00
|
|
|
|
2019-10-18 14:47:20 -04:00
|
|
|
#if defined(PTHREAD_STACK_MIN)
|
|
|
|
|
# define AST_STACKSIZE MAX((((sizeof(void *) * 8 * 8) - 16) * 1024), PTHREAD_STACK_MIN)
|
|
|
|
|
# define AST_STACKSIZE_LOW MAX((((sizeof(void *) * 8 * 2) - 16) * 1024), PTHREAD_STACK_MIN)
|
|
|
|
|
#else
|
|
|
|
|
# define AST_STACKSIZE (((sizeof(void *) * 8 * 8) - 16) * 1024)
|
|
|
|
|
# define AST_STACKSIZE_LOW (((sizeof(void *) * 8 * 2) - 16) * 1024)
|
|
|
|
|
#endif
|
2006-10-04 19:51:38 +00:00
|
|
|
|
2016-09-19 05:46:27 -04:00
|
|
|
int ast_background_stacksize(void);
|
|
|
|
|
|
|
|
|
|
#define AST_BACKGROUND_STACKSIZE ast_background_stacksize()
|
2006-04-12 20:40:46 +00:00
|
|
|
|
|
|
|
|
void ast_register_thread(char *name);
|
|
|
|
|
void ast_unregister_thread(void *id);
|
|
|
|
|
|
2006-10-04 19:51:38 +00:00
|
|
|
int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *),
|
|
|
|
|
void *data, size_t stacksize, const char *file, const char *caller,
|
|
|
|
|
int line, const char *start_fn);
|
|
|
|
|
|
2007-05-24 18:30:19 +00:00
|
|
|
int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr, void*(*start_routine)(void *),
|
|
|
|
|
void *data, size_t stacksize, const char *file, const char *caller,
|
|
|
|
|
int line, const char *start_fn);
|
|
|
|
|
|
2007-11-16 22:37:17 +00:00
|
|
|
#define ast_pthread_create(a, b, c, d) \
|
|
|
|
|
ast_pthread_create_stack(a, b, c, d, \
|
|
|
|
|
0, __FILE__, __FUNCTION__, __LINE__, #c)
|
|
|
|
|
|
|
|
|
|
#define ast_pthread_create_detached(a, b, c, d) \
|
|
|
|
|
ast_pthread_create_detached_stack(a, b, c, d, \
|
|
|
|
|
0, __FILE__, __FUNCTION__, __LINE__, #c)
|
|
|
|
|
|
|
|
|
|
#define ast_pthread_create_background(a, b, c, d) \
|
|
|
|
|
ast_pthread_create_stack(a, b, c, d, \
|
|
|
|
|
AST_BACKGROUND_STACKSIZE, \
|
|
|
|
|
__FILE__, __FUNCTION__, __LINE__, #c)
|
|
|
|
|
|
|
|
|
|
#define ast_pthread_create_detached_background(a, b, c, d) \
|
|
|
|
|
ast_pthread_create_detached_stack(a, b, c, d, \
|
|
|
|
|
AST_BACKGROUND_STACKSIZE, \
|
|
|
|
|
__FILE__, __FUNCTION__, __LINE__, #c)
|
|
|
|
|
|
|
|
|
|
/* End of thread management support */
|
2007-05-24 18:30:19 +00:00
|
|
|
|
2012-03-22 21:25:22 +00:00
|
|
|
/*!
|
2012-07-05 19:22:03 +00:00
|
|
|
* \brief Replace '^' in a string with ','
|
|
|
|
|
* \param s String within which to replace characters
|
|
|
|
|
*/
|
2012-03-22 21:25:22 +00:00
|
|
|
void ast_replace_subargument_delimiter(char *s);
|
|
|
|
|
|
2005-09-23 02:57:14 +00:00
|
|
|
/*!
|
2012-07-05 19:22:03 +00:00
|
|
|
* \brief Process a string to find and replace characters
|
|
|
|
|
* \param start The string to analyze
|
|
|
|
|
* \param find The character to find
|
|
|
|
|
* \param replace_with The character that will replace the one we are looking for
|
|
|
|
|
*/
|
2005-09-23 02:57:14 +00:00
|
|
|
char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
|
|
|
|
|
|
2006-01-10 00:55:45 +00:00
|
|
|
long int ast_random(void);
|
|
|
|
|
|
2013-04-26 20:05:15 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Returns a random number between 0.0 and 1.0, inclusive.
|
|
|
|
|
* \since 12
|
|
|
|
|
*/
|
|
|
|
|
#define ast_random_double() (((double)ast_random()) / RAND_MAX)
|
2007-09-17 18:57:56 +00:00
|
|
|
|
2006-10-12 18:43:52 +00:00
|
|
|
/*!
|
2012-07-05 19:22:03 +00:00
|
|
|
* \brief Disable PMTU discovery on a socket
|
|
|
|
|
* \param sock The socket to manipulate
|
|
|
|
|
*
|
|
|
|
|
* On Linux, UDP sockets default to sending packets with the Dont Fragment (DF)
|
|
|
|
|
* bit set. This is supposedly done to allow the application to do PMTU
|
|
|
|
|
* discovery, but Asterisk does not do this.
|
|
|
|
|
*
|
|
|
|
|
* Because of this, UDP packets sent by Asterisk that are larger than the MTU
|
|
|
|
|
* of any hop in the path will be lost. This function can be called on a socket
|
|
|
|
|
* to ensure that the DF bit will not be set.
|
2006-10-12 18:43:52 +00:00
|
|
|
*/
|
|
|
|
|
void ast_enable_packet_fragmentation(int sock);
|
|
|
|
|
|
2007-06-22 04:35:12 +00:00
|
|
|
/*!
|
2012-07-05 19:22:03 +00:00
|
|
|
* \brief Recursively create directory path
|
|
|
|
|
* \param path The directory path to create
|
|
|
|
|
* \param mode The permissions with which to try to create the directory
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval 0 on success
|
|
|
|
|
* \return error code otherwise
|
2012-07-05 19:22:03 +00:00
|
|
|
*
|
|
|
|
|
* Creates a directory path, creating parent directories as needed.
|
2007-06-22 04:35:12 +00:00
|
|
|
*/
|
|
|
|
|
int ast_mkdir(const char *path, int mode);
|
|
|
|
|
|
2013-07-03 17:58:45 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Recursively create directory path, but only if it resolves within
|
|
|
|
|
* the given \a base_path.
|
|
|
|
|
*
|
|
|
|
|
* If \a base_path does not exist, it will not be created and this function
|
|
|
|
|
* returns \c EPERM.
|
|
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \param base_path
|
2013-07-03 17:58:45 +00:00
|
|
|
* \param path The directory path to create
|
|
|
|
|
* \param mode The permissions with which to try to create the directory
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval 0 on success
|
|
|
|
|
* \return an error code otherwise
|
2013-07-03 17:58:45 +00:00
|
|
|
*/
|
|
|
|
|
int ast_safe_mkdir(const char *base_path, const char *path, int mode);
|
|
|
|
|
|
2011-06-15 20:02:30 +00:00
|
|
|
#define ARRAY_LEN(a) (size_t) (sizeof(a) / sizeof(0[a]))
|
2007-02-10 00:40:57 +00:00
|
|
|
|
2013-11-23 17:26:57 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Checks to see if value is within the given bounds
|
|
|
|
|
*
|
|
|
|
|
* \param v the value to check
|
|
|
|
|
* \param min minimum lower bound (inclusive)
|
|
|
|
|
* \param max maximum upper bound (inclusive)
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval 0 if value out of bounds
|
|
|
|
|
* \retval non-zero otherwise
|
2013-11-23 17:26:57 +00:00
|
|
|
*/
|
|
|
|
|
#define IN_BOUNDS(v, min, max) ((v) >= (min)) && ((v) <= (max))
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Checks to see if value is within the bounds of the given array
|
|
|
|
|
*
|
|
|
|
|
* \param v the value to check
|
|
|
|
|
* \param a the array to bound check
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval 0 if value out of bounds
|
|
|
|
|
* \retval non-zero otherwise
|
2013-11-23 17:26:57 +00:00
|
|
|
*/
|
2015-04-09 12:57:21 +00:00
|
|
|
#define ARRAY_IN_BOUNDS(v, a) IN_BOUNDS((int) (v), 0, ARRAY_LEN(a) - 1)
|
2009-04-23 20:36:35 +00:00
|
|
|
|
|
|
|
|
/* Definition for Digest authorization */
|
|
|
|
|
struct ast_http_digest {
|
|
|
|
|
AST_DECLARE_STRING_FIELDS(
|
|
|
|
|
AST_STRING_FIELD(username);
|
|
|
|
|
AST_STRING_FIELD(nonce);
|
|
|
|
|
AST_STRING_FIELD(uri);
|
|
|
|
|
AST_STRING_FIELD(realm);
|
|
|
|
|
AST_STRING_FIELD(domain);
|
|
|
|
|
AST_STRING_FIELD(response);
|
|
|
|
|
AST_STRING_FIELD(cnonce);
|
|
|
|
|
AST_STRING_FIELD(opaque);
|
|
|
|
|
AST_STRING_FIELD(nc);
|
|
|
|
|
);
|
|
|
|
|
int qop; /* Flag set to 1, if we send/recv qop="quth" */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*!
|
2012-07-05 19:22:03 +00:00
|
|
|
* \brief Parse digest authorization header.
|
2021-11-19 16:47:25 +01:00
|
|
|
* \return -1 if we have no auth or something wrong with digest.
|
2012-07-05 19:22:03 +00:00
|
|
|
* \note This function may be used for Digest request and responce header.
|
2009-04-23 20:36:35 +00:00
|
|
|
* request arg is set to nonzero, if we parse Digest Request.
|
|
|
|
|
* pedantic arg can be set to nonzero if we need to do addition Digest check.
|
|
|
|
|
*/
|
|
|
|
|
int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic);
|
|
|
|
|
|
2016-07-18 23:46:19 -04:00
|
|
|
#ifdef DO_CRASH
|
|
|
|
|
#define DO_CRASH_NORETURN attribute_noreturn
|
|
|
|
|
#else
|
|
|
|
|
#define DO_CRASH_NORETURN
|
|
|
|
|
#endif
|
2009-04-23 20:36:35 +00:00
|
|
|
|
2016-09-16 19:54:07 -04:00
|
|
|
void DO_CRASH_NORETURN __ast_assert_failed(int condition, const char *condition_str,
|
|
|
|
|
const char *file, int line, const char *function);
|
|
|
|
|
|
2008-05-14 21:40:43 +00:00
|
|
|
#ifdef AST_DEVMODE
|
|
|
|
|
#define ast_assert(a) _ast_assert(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
|
2018-04-13 14:17:36 -06:00
|
|
|
#define ast_assert_return(a, ...) \
|
|
|
|
|
({ \
|
|
|
|
|
if (__builtin_expect(!(a), 1)) { \
|
|
|
|
|
_ast_assert(0, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
|
|
|
|
|
return __VA_ARGS__; \
|
|
|
|
|
}\
|
|
|
|
|
})
|
2012-11-08 17:38:31 +00:00
|
|
|
static void force_inline _ast_assert(int condition, const char *condition_str, const char *file, int line, const char *function)
|
2008-05-14 21:40:43 +00:00
|
|
|
{
|
|
|
|
|
if (__builtin_expect(!condition, 1)) {
|
2012-11-08 17:38:31 +00:00
|
|
|
__ast_assert_failed(condition, condition_str, file, line, function);
|
2008-05-14 21:40:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
#define ast_assert(a)
|
2018-04-13 14:17:36 -06:00
|
|
|
#define ast_assert_return(a, ...) \
|
|
|
|
|
({ \
|
|
|
|
|
if (__builtin_expect(!(a), 1)) { \
|
|
|
|
|
return __VA_ARGS__; \
|
|
|
|
|
}\
|
|
|
|
|
})
|
2008-05-14 21:40:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
2012-11-08 17:38:31 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Force a crash if DO_CRASH is defined.
|
|
|
|
|
*
|
|
|
|
|
* \note If DO_CRASH is not defined then the function returns.
|
|
|
|
|
*/
|
2016-07-18 23:46:19 -04:00
|
|
|
void DO_CRASH_NORETURN ast_do_crash(void);
|
2012-11-08 17:38:31 +00:00
|
|
|
|
2006-12-15 22:08:46 +00:00
|
|
|
#include "asterisk/strings.h"
|
2007-02-10 00:40:57 +00:00
|
|
|
|
2011-11-02 22:02:07 +00:00
|
|
|
/*!
|
2011-11-11 22:00:14 +00:00
|
|
|
* \brief Return the number of bytes used in the alignment of type.
|
|
|
|
|
* \param type
|
|
|
|
|
* \return The number of bytes required for alignment.
|
2011-11-02 22:02:07 +00:00
|
|
|
*
|
2011-11-11 22:00:14 +00:00
|
|
|
* This is really just __alignof__(), but tucked away in this header so we
|
|
|
|
|
* don't have to look at the nasty underscores in the source.
|
|
|
|
|
*/
|
|
|
|
|
#define ast_alignof(type) __alignof__(type)
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Increase offset so it is a multiple of the required alignment of type.
|
|
|
|
|
* \param offset The value that should be increased.
|
|
|
|
|
* \param type The data type that offset should be aligned to.
|
|
|
|
|
* \return The smallest multiple of alignof(type) larger than or equal to offset.
|
|
|
|
|
* \see ast_make_room_for()
|
2011-11-02 22:02:07 +00:00
|
|
|
*
|
2011-11-11 22:00:14 +00:00
|
|
|
* Many systems prefer integers to be stored on aligned on memory locations.
|
|
|
|
|
* This macro will increase an offset so a value of the supplied type can be
|
|
|
|
|
* safely be stored on such a memory location.
|
2011-11-02 22:02:07 +00:00
|
|
|
*
|
|
|
|
|
* Examples:
|
2011-11-11 22:00:14 +00:00
|
|
|
* ast_align_for(0x17, int64_t) ==> 0x18
|
|
|
|
|
* ast_align_for(0x18, int64_t) ==> 0x18
|
|
|
|
|
* ast_align_for(0x19, int64_t) ==> 0x20
|
|
|
|
|
*
|
|
|
|
|
* Don't mind the ugliness, the compiler will optimize it.
|
2011-11-02 22:02:07 +00:00
|
|
|
*/
|
2011-11-11 22:00:14 +00:00
|
|
|
#define ast_align_for(offset, type) (((offset + __alignof__(type) - 1) / __alignof__(type)) * __alignof__(type))
|
2011-11-02 22:02:07 +00:00
|
|
|
|
|
|
|
|
/*!
|
2011-11-11 22:00:14 +00:00
|
|
|
* \brief Increase offset by the required alignment of type and make sure it is
|
|
|
|
|
* a multiple of said alignment.
|
|
|
|
|
* \param offset The value that should be increased.
|
|
|
|
|
* \param type The data type that room should be reserved for.
|
|
|
|
|
* \return The smallest multiple of alignof(type) larger than or equal to offset
|
|
|
|
|
* plus alignof(type).
|
|
|
|
|
* \see ast_align_for()
|
|
|
|
|
*
|
|
|
|
|
* A use case for this is when prepending length fields of type int to a buffer.
|
|
|
|
|
* If you keep the offset a multiple of the alignment of the integer type,
|
|
|
|
|
* a next block of length+buffer will have the length field automatically
|
|
|
|
|
* aligned.
|
|
|
|
|
*
|
|
|
|
|
* Examples:
|
|
|
|
|
* ast_make_room_for(0x17, int64_t) ==> 0x20
|
|
|
|
|
* ast_make_room_for(0x18, int64_t) ==> 0x20
|
|
|
|
|
* ast_make_room_for(0x19, int64_t) ==> 0x28
|
2011-11-02 22:02:07 +00:00
|
|
|
*
|
2011-11-11 22:00:14 +00:00
|
|
|
* Don't mind the ugliness, the compiler will optimize it.
|
2011-11-02 22:02:07 +00:00
|
|
|
*/
|
2011-11-11 22:00:14 +00:00
|
|
|
#define ast_make_room_for(offset, type) (((offset + (2 * __alignof__(type) - 1)) / __alignof__(type)) * __alignof__(type))
|
2011-11-02 22:02:07 +00:00
|
|
|
|
2008-06-10 12:48:50 +00:00
|
|
|
/*!
|
2012-07-05 19:22:03 +00:00
|
|
|
* \brief An Entity ID is essentially a MAC address, brief and unique
|
2008-06-10 12:48:50 +00:00
|
|
|
*/
|
|
|
|
|
struct ast_eid {
|
|
|
|
|
unsigned char eid[6];
|
2008-11-29 17:57:39 +00:00
|
|
|
} __attribute__((__packed__));
|
2008-06-10 12:48:50 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Global EID
|
|
|
|
|
*
|
|
|
|
|
* This is set in asterisk.conf, or determined automatically by taking the mac
|
|
|
|
|
* address of an Ethernet interface on the system.
|
|
|
|
|
*/
|
2009-03-27 14:00:18 +00:00
|
|
|
extern struct ast_eid ast_eid_default;
|
2008-06-10 12:48:50 +00:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Fill in an ast_eid with the default eid of this machine
|
2009-03-09 20:58:17 +00:00
|
|
|
* \since 1.6.1
|
2008-06-10 12:48:50 +00:00
|
|
|
*/
|
|
|
|
|
void ast_set_default_eid(struct ast_eid *eid);
|
|
|
|
|
|
|
|
|
|
/*!
|
2012-07-05 19:22:03 +00:00
|
|
|
* \brief Convert an EID to a string
|
2009-03-09 20:58:17 +00:00
|
|
|
* \since 1.6.1
|
2008-06-10 12:48:50 +00:00
|
|
|
*/
|
|
|
|
|
char *ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid);
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Convert a string into an EID
|
|
|
|
|
*
|
|
|
|
|
* This function expects an EID in the format:
|
|
|
|
|
* 00:11:22:33:44:55
|
|
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval 0 success
|
|
|
|
|
* \retval non-zero failure
|
2009-03-09 20:58:17 +00:00
|
|
|
* \since 1.6.1
|
2008-06-10 12:48:50 +00:00
|
|
|
*/
|
|
|
|
|
int ast_str_to_eid(struct ast_eid *eid, const char *s);
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Compare two EIDs
|
|
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval 0 if the two are the same
|
|
|
|
|
* \retval non-zero otherwise
|
2009-03-09 20:58:17 +00:00
|
|
|
* \since 1.6.1
|
2008-06-10 12:48:50 +00:00
|
|
|
*/
|
|
|
|
|
int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2);
|
|
|
|
|
|
2016-08-10 15:41:38 -04:00
|
|
|
/*!
|
|
|
|
|
* \brief Check if EID is empty
|
|
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval 1 if the EID is empty
|
|
|
|
|
* \retval 0 otherwise
|
2016-08-10 15:41:38 -04:00
|
|
|
* \since 13.12.0
|
|
|
|
|
*/
|
|
|
|
|
int ast_eid_is_empty(const struct ast_eid *eid);
|
|
|
|
|
|
2010-12-12 03:58:33 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Get current thread ID
|
|
|
|
|
* \return the ID if platform is supported, else -1
|
|
|
|
|
*/
|
|
|
|
|
int ast_get_tid(void);
|
|
|
|
|
|
2012-07-05 19:22:03 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Resolve a binary to a full pathname
|
2010-12-18 00:08:13 +00:00
|
|
|
* \param binary Name of the executable to resolve
|
|
|
|
|
* \param fullpath Buffer to hold the complete pathname
|
|
|
|
|
* \param fullpath_size Size of \a fullpath
|
|
|
|
|
* \retval NULL \a binary was not found or the environment variable PATH is not set
|
|
|
|
|
* \return \a fullpath
|
|
|
|
|
*/
|
|
|
|
|
char *ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size);
|
|
|
|
|
|
2012-07-05 19:22:03 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Declare a variable that will call a destructor function when it goes out of scope.
|
|
|
|
|
*
|
|
|
|
|
* Resource Allocation Is Initialization (RAII) variable declaration.
|
|
|
|
|
*
|
|
|
|
|
* \since 11.0
|
2012-06-01 16:33:25 +00:00
|
|
|
* \param vartype The type of the variable
|
|
|
|
|
* \param varname The name of the variable
|
|
|
|
|
* \param initval The initial value of the variable
|
|
|
|
|
* \param dtor The destructor function of type' void func(vartype *)'
|
|
|
|
|
*
|
|
|
|
|
* \code
|
|
|
|
|
* void mything_cleanup(struct mything *t)
|
|
|
|
|
* {
|
|
|
|
|
* if (t) {
|
|
|
|
|
* ast_free(t->stuff);
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* void do_stuff(const char *name)
|
|
|
|
|
* {
|
|
|
|
|
* RAII_VAR(struct mything *, thing, mything_alloc(name), mything_cleanup);
|
|
|
|
|
* ...
|
|
|
|
|
* }
|
2012-08-30 14:23:28 +00:00
|
|
|
* \endcode
|
2012-06-01 16:33:25 +00:00
|
|
|
*
|
|
|
|
|
* \note This macro is especially useful for working with ao2 objects. A common idiom
|
|
|
|
|
* would be a function that needed to look up an ao2 object and might have several error
|
|
|
|
|
* conditions after the allocation that would normally need to unref the ao2 object.
|
|
|
|
|
* With RAII_VAR, it is possible to just return and leave the cleanup to the destructor
|
|
|
|
|
* function. For example:
|
2012-08-30 14:23:28 +00:00
|
|
|
*
|
2012-06-01 16:33:25 +00:00
|
|
|
* \code
|
|
|
|
|
* void do_stuff(const char *name)
|
|
|
|
|
* {
|
|
|
|
|
* RAII_VAR(struct mything *, thing, find_mything(name), ao2_cleanup);
|
|
|
|
|
* if (!thing) {
|
|
|
|
|
* return;
|
|
|
|
|
* }
|
|
|
|
|
* if (error) {
|
|
|
|
|
* return;
|
|
|
|
|
* }
|
|
|
|
|
* do_stuff_with_thing(thing);
|
|
|
|
|
* }
|
2012-08-30 14:23:28 +00:00
|
|
|
* \endcode
|
2012-06-01 16:33:25 +00:00
|
|
|
*/
|
2015-03-12 12:40:23 +00:00
|
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
|
typedef void (^_raii_cleanup_block_t)(void);
|
|
|
|
|
static inline void _raii_cleanup_block(_raii_cleanup_block_t *b) { (*b)(); }
|
|
|
|
|
|
2019-01-05 18:14:26 +01:00
|
|
|
#define RAII_VAR(vartype, varname, initval, dtor) \
|
|
|
|
|
__block vartype varname = initval; \
|
|
|
|
|
_raii_cleanup_block_t _raii_cleanup_ ## varname __attribute__((cleanup(_raii_cleanup_block),unused)) = \
|
|
|
|
|
^{ {(void)dtor(varname);} };
|
2015-03-12 12:40:23 +00:00
|
|
|
|
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
|
|
|
|
|
|
#define RAII_VAR(vartype, varname, initval, dtor) \
|
|
|
|
|
auto void _dtor_ ## varname (vartype * v); \
|
|
|
|
|
void _dtor_ ## varname (vartype * v) { dtor(*v); } \
|
2012-06-01 16:33:25 +00:00
|
|
|
vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
|
|
|
|
|
|
2015-03-12 12:40:23 +00:00
|
|
|
#else
|
|
|
|
|
#error "Cannot compile Asterisk: unknown and unsupported compiler."
|
|
|
|
|
#endif /* #if __GNUC__ */
|
|
|
|
|
|
2013-07-03 16:32:00 +00:00
|
|
|
/*!
|
|
|
|
|
* \brief Asterisk wrapper around crypt(3).
|
|
|
|
|
*
|
|
|
|
|
* The interpretation of the salt (which determines the password hashing
|
|
|
|
|
* algorithm) is system specific. Application code should prefer to use
|
|
|
|
|
* ast_crypt_encrypt() or ast_crypt_validate().
|
|
|
|
|
*
|
|
|
|
|
* The returned string is heap allocated, and should be freed with ast_free().
|
|
|
|
|
*
|
|
|
|
|
* \param key User's password to crypt.
|
|
|
|
|
* \param salt Salt to crypt with.
|
|
|
|
|
* \return Crypted password.
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval NULL on error.
|
2013-07-03 16:32:00 +00:00
|
|
|
*/
|
|
|
|
|
char *ast_crypt(const char *key, const char *salt);
|
|
|
|
|
|
2018-06-19 10:43:17 -05:00
|
|
|
/*!
|
2013-07-03 16:32:00 +00:00
|
|
|
* \brief Asterisk wrapper around crypt(3) for encrypting passwords.
|
|
|
|
|
*
|
|
|
|
|
* This function will generate a random salt and encrypt the given password.
|
|
|
|
|
*
|
|
|
|
|
* The returned string is heap allocated, and should be freed with ast_free().
|
|
|
|
|
*
|
|
|
|
|
* \param key User's password to crypt.
|
|
|
|
|
* \return Crypted password.
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval NULL on error.
|
2013-07-03 16:32:00 +00:00
|
|
|
*/
|
|
|
|
|
char *ast_crypt_encrypt(const char *key);
|
|
|
|
|
|
2018-06-19 10:43:17 -05:00
|
|
|
/*!
|
2013-07-03 16:32:00 +00:00
|
|
|
* \brief Asterisk wrapper around crypt(3) for validating passwords.
|
|
|
|
|
*
|
|
|
|
|
* \param key User's password to validate.
|
|
|
|
|
* \param expected Expected result from crypt.
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval True (non-zero) if \a key matches \a expected.
|
|
|
|
|
* \retval False (zero) if \a key doesn't match.
|
2013-07-03 16:32:00 +00:00
|
|
|
*/
|
|
|
|
|
int ast_crypt_validate(const char *key, const char *expected);
|
|
|
|
|
|
2018-06-19 10:43:17 -05:00
|
|
|
/*!
|
2015-12-08 16:49:20 -07:00
|
|
|
* \brief Test that a file exists and is readable by the effective user.
|
|
|
|
|
* \since 13.7.0
|
|
|
|
|
*
|
|
|
|
|
* \param filename File to test.
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval True (non-zero) if the file exists and is readable.
|
|
|
|
|
* \retval False (zero) if the file either doesn't exists or is not readable.
|
2015-12-08 16:49:20 -07:00
|
|
|
*/
|
|
|
|
|
int ast_file_is_readable(const char *filename);
|
|
|
|
|
|
2018-06-19 10:43:17 -05:00
|
|
|
/*!
|
2015-12-12 10:08:50 -07:00
|
|
|
* \brief Compare 2 major.minor.patch.extra version strings.
|
|
|
|
|
* \since 13.7.0
|
|
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \param version1
|
|
|
|
|
* \param version2
|
2015-12-14 15:25:02 -06:00
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval negative if version 1 < version 2.
|
|
|
|
|
* \retval 0 if version 1 = version 2.
|
|
|
|
|
* \retval positive if version 1 > version 2.
|
2015-12-12 10:08:50 -07:00
|
|
|
*/
|
|
|
|
|
int ast_compare_versions(const char *version1, const char *version2);
|
|
|
|
|
|
2018-06-19 10:43:17 -05:00
|
|
|
/*!
|
2016-11-22 18:20:06 +01:00
|
|
|
* \brief Test that an OS supports IPv6 Networking.
|
|
|
|
|
* \since 13.14.0
|
|
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval True (non-zero) if the IPv6 supported.
|
|
|
|
|
* \retval False (zero) if the OS doesn't support IPv6.
|
2016-11-22 18:20:06 +01:00
|
|
|
*/
|
|
|
|
|
int ast_check_ipv6(void);
|
|
|
|
|
|
2017-12-07 10:52:39 -05:00
|
|
|
enum ast_fd_flag_operation {
|
|
|
|
|
AST_FD_FLAG_SET,
|
|
|
|
|
AST_FD_FLAG_CLEAR,
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-19 10:43:17 -05:00
|
|
|
/*!
|
2017-12-07 10:52:39 -05:00
|
|
|
* \brief Set flags on the given file descriptor
|
|
|
|
|
* \since 13.19
|
|
|
|
|
*
|
|
|
|
|
* If getting or setting flags of the given file descriptor fails, logs an
|
|
|
|
|
* error message.
|
|
|
|
|
*
|
|
|
|
|
* \param fd File descriptor to set flags on
|
|
|
|
|
* \param flags The flag(s) to set
|
|
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval -1 on error
|
|
|
|
|
* \retval 0 if successful
|
2017-12-07 10:52:39 -05:00
|
|
|
*/
|
|
|
|
|
#define ast_fd_set_flags(fd, flags) \
|
|
|
|
|
__ast_fd_set_flags((fd), (flags), AST_FD_FLAG_SET, __FILE__, __LINE__, __PRETTY_FUNCTION__)
|
|
|
|
|
|
2018-06-19 10:43:17 -05:00
|
|
|
/*!
|
2017-12-07 10:52:39 -05:00
|
|
|
* \brief Clear flags on the given file descriptor
|
|
|
|
|
* \since 13.19
|
|
|
|
|
*
|
|
|
|
|
* If getting or setting flags of the given file descriptor fails, logs an
|
|
|
|
|
* error message.
|
|
|
|
|
*
|
|
|
|
|
* \param fd File descriptor to clear flags on
|
|
|
|
|
* \param flags The flag(s) to clear
|
|
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval -1 on error
|
|
|
|
|
* \retval 0 if successful
|
2017-12-07 10:52:39 -05:00
|
|
|
*/
|
|
|
|
|
#define ast_fd_clear_flags(fd, flags) \
|
|
|
|
|
__ast_fd_set_flags((fd), (flags), AST_FD_FLAG_CLEAR, __FILE__, __LINE__, __PRETTY_FUNCTION__)
|
|
|
|
|
|
|
|
|
|
int __ast_fd_set_flags(int fd, int flags, enum ast_fd_flag_operation op,
|
|
|
|
|
const char *file, int lineno, const char *function);
|
|
|
|
|
|
2018-12-07 07:57:48 -05:00
|
|
|
/*!
|
|
|
|
|
* \brief Create a non-blocking socket
|
|
|
|
|
* \since 13.25
|
|
|
|
|
*
|
|
|
|
|
* Wrapper around socket(2) that sets the O_NONBLOCK flag on the resulting
|
|
|
|
|
* socket.
|
|
|
|
|
*
|
|
|
|
|
* \details
|
|
|
|
|
* For parameter and return information, see the man page for
|
|
|
|
|
* socket(2).
|
|
|
|
|
*/
|
|
|
|
|
#ifdef HAVE_SOCK_NONBLOCK
|
|
|
|
|
# define ast_socket_nonblock(domain, type, protocol) socket((domain), (type) | SOCK_NONBLOCK, (protocol))
|
|
|
|
|
#else
|
|
|
|
|
int ast_socket_nonblock(int domain, int type, int protocol);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Create a non-blocking pipe
|
|
|
|
|
* \since 13.25
|
|
|
|
|
*
|
|
|
|
|
* Wrapper around pipe(2) that sets the O_NONBLOCK flag on the resulting
|
|
|
|
|
* file descriptors.
|
|
|
|
|
*
|
|
|
|
|
* \details
|
|
|
|
|
* For parameter and return information, see the man page for
|
|
|
|
|
* pipe(2).
|
|
|
|
|
*/
|
|
|
|
|
#ifdef HAVE_PIPE2
|
|
|
|
|
# define ast_pipe_nonblock(filedes) pipe2((filedes), O_NONBLOCK)
|
|
|
|
|
#else
|
|
|
|
|
int ast_pipe_nonblock(int filedes[2]);
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-06-18 18:04:54 -05:00
|
|
|
/*!
|
|
|
|
|
* \brief Set the current thread's user interface status.
|
|
|
|
|
*
|
|
|
|
|
* \param is_user_interface Non-zero to mark the thread as a user interface.
|
|
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval True (non-zero) if marking current thread failed.
|
|
|
|
|
* \retval False (zero) if successfuly marked current thread.
|
2018-06-18 18:04:54 -05:00
|
|
|
*/
|
|
|
|
|
int ast_thread_user_interface_set(int is_user_interface);
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Indicates whether the current thread is a user interface
|
|
|
|
|
*
|
2021-11-19 16:47:25 +01:00
|
|
|
* \retval True (non-zero) if thread is a user interface.
|
|
|
|
|
* \retval False (zero) if thread is not a user interface.
|
2018-06-18 18:04:54 -05:00
|
|
|
*/
|
|
|
|
|
int ast_thread_is_user_interface(void);
|
|
|
|
|
|
2022-07-26 13:38:10 -06:00
|
|
|
/*!
|
|
|
|
|
* \brief Test for the presence of an executable command in $PATH
|
|
|
|
|
*
|
|
|
|
|
* \param cmd Name of command to locate.
|
|
|
|
|
*
|
|
|
|
|
* \retval True (non-zero) if command is in $PATH.
|
|
|
|
|
* \retval False (zero) command not found.
|
|
|
|
|
*/
|
|
|
|
|
int ast_check_command_in_path(const char *cmd);
|
|
|
|
|
|
2005-05-02 00:27:54 +00:00
|
|
|
#endif /* _ASTERISK_UTILS_H */
|