2004-05-09 08:22:15 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2004-05-09 08:22:15 +00:00
|
|
|
*
|
2006-01-04 21:54:09 +00:00
|
|
|
* Copyright (C) 1999 - 2006, Digium, Inc.
|
2004-05-09 08:22:15 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +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-09 08:22:15 +00:00
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +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
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \brief Utility functions
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \note These are important for portability and security,
|
|
|
|
* so please use them in favour of other routines.
|
|
|
|
* Please consult the CODING GUIDELINES for more information.
|
2004-05-09 08:22:15 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2004-05-11 18:41:09 +00:00
|
|
|
#include <ctype.h>
|
2004-05-31 16:51:28 +00:00
|
|
|
#include <string.h>
|
2004-06-22 17:42:14 +00:00
|
|
|
#include <unistd.h>
|
2004-09-06 15:29:41 +00:00
|
|
|
#include <stdlib.h>
|
2004-08-08 17:15:02 +00:00
|
|
|
#include <errno.h>
|
2005-05-15 22:33:02 +00:00
|
|
|
#include <stdarg.h>
|
2005-01-08 18:54:30 +00:00
|
|
|
#include <stdio.h>
|
2007-06-22 04:35:12 +00:00
|
|
|
#include <sys/stat.h>
|
2004-06-29 12:56:46 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2004-06-29 17:54:25 +00:00
|
|
|
#include <netinet/in.h>
|
2004-06-29 12:56:46 +00:00
|
|
|
#include <arpa/inet.h>
|
2005-04-22 13:11:34 +00:00
|
|
|
|
2007-07-21 14:39:52 +00:00
|
|
|
#ifdef HAVE_DEV_URANDOM
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2006-03-30 23:26:22 +00:00
|
|
|
#define AST_API_MODULE /* ensure that inlinable API functions will be built in lock.h if required */
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/io.h"
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/md5.h"
|
2006-02-03 22:37:29 +00:00
|
|
|
#include "asterisk/sha1.h"
|
2005-07-19 14:08:19 +00:00
|
|
|
#include "asterisk/options.h"
|
2004-05-09 08:22:15 +00:00
|
|
|
|
2005-06-24 22:45:15 +00:00
|
|
|
#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */
|
|
|
|
#include "asterisk/strings.h"
|
|
|
|
|
|
|
|
#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */
|
|
|
|
#include "asterisk/time.h"
|
|
|
|
|
2006-01-04 21:54:09 +00:00
|
|
|
#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */
|
|
|
|
#include "asterisk/stringfields.h"
|
|
|
|
|
2005-06-23 23:07:55 +00:00
|
|
|
#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
|
Merge team/russell/ast_verbose_threadstorage
- instead of defining a free() wrapper in a bunch of files, define it as
ast_free() in utils.h and remove the copies from all the files.
- centralize and abstract the code used for doing thread storage. The code
lives in threadstorage.h, with one function being implemented in utils.c.
This new API includes generic thread storage as well as special functions
for handling thread local dynamic length string buffers.
- update ast_inet_ntoa() to use the new threadstorage API
- update ast_state2str() to use the new threadstorage API
- update ast_cli() to use the new threadstorage API
- Modify manager_event() to use thread storage. Instead of using a buffer of
4096 characters as the workspace for building the manager event, use a thread
local dynamic string. Now there is no length limitation on the length of the
body of a manager event.
- Significantly simplify the handling of ast_verbose() ...
- Instead of using a static char buffer and a lock to make sure only one
thread can be using ast_verbose() at a time, use a thread local dynamic
string as the workspace for preparing the verbose message. Instead of
locking around the entire function, the only locking done now is when the
message has been built and is being deliviered to the list of registered
verbose message handlers.
- This function was doing a strdup() on every message passed to it and
keeping a queue of the last 200 messages in memory. This has been
completely removed. The only place this was used was that if there were
any messages in the verbose queue when a verbose handler was registered,
all of the messages in the queue would be fed to it. So, I just made sure
that the console verbose handler and the network verbose handler (for
remote asterisk consoles) were registered before any verbose messages.
pbx_gtkconsole and pbx_kdeconsole will now lose a few verbose messages at
startup, but I didn't feel the performance hit of this message queue was
worth saving the initial verbose output for these very rarely used modules.
- I have removed the last three arguments to the verbose handlers, leaving
only the string itself because they aren't needed anymore. For example,
ast_verbose had some logic for telling the verbose handler to add
a newline if the buffer was completely full. Now that the buffer can grow
as needed, this doesn't matter anymore.
- remove unused function, ast_verbose_dmesg() which was to dispatch the
message queue
- Convert the list of verbose handlers to use the linked list macros.
- add missing newline characters to a few ast_verbose() calls
- convert the list of log channels to use the linked list macros in logger.c
- fix close_logger() to close all of the files it opened for logging
- update ast_log() to use a thread local dynamic string for its workspace
for preparing log messages instead of a buffer of size BUFSIZ (8kB on my
system) allocated on the stack. The dynamic string in this case is limited
to only growing to a maximum size of BUFSIZ.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@39272 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-08-08 06:32:04 +00:00
|
|
|
#define AST_API_MODULE
|
|
|
|
#include "asterisk/threadstorage.h"
|
|
|
|
|
2004-06-25 04:07:52 +00:00
|
|
|
static char base64[64];
|
|
|
|
static char b2a[256];
|
|
|
|
|
2006-10-19 01:00:57 +00:00
|
|
|
AST_THREADSTORAGE(inet_ntoa_buf);
|
2006-07-27 01:49:57 +00:00
|
|
|
|
2006-10-23 16:54:54 +00:00
|
|
|
#if !defined(HAVE_GETHOSTBYNAME_R)
|
2004-05-09 08:22:15 +00:00
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
#define ERANGE 34 /*!< duh? ERANGE value copied from web... */
|
2004-05-09 08:22:15 +00:00
|
|
|
#undef gethostbyname
|
|
|
|
|
2004-06-09 01:45:08 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(__mutex);
|
|
|
|
|
2006-06-01 14:18:12 +00:00
|
|
|
/*! \brief Reentrant replacement for gethostbyname for BSD-based systems.
|
2006-03-07 17:55:33 +00:00
|
|
|
\note This
|
2005-05-09 13:50:38 +00:00
|
|
|
routine is derived from code originally written and placed in the public
|
|
|
|
domain by Enzo Michelangeli <em@em.no-ip.com> */
|
|
|
|
|
2004-06-22 17:42:14 +00:00
|
|
|
static int gethostbyname_r (const char *name, struct hostent *ret, char *buf,
|
2004-06-22 20:11:15 +00:00
|
|
|
size_t buflen, struct hostent **result,
|
|
|
|
int *h_errnop)
|
2004-05-09 08:22:15 +00:00
|
|
|
{
|
|
|
|
int hsave;
|
|
|
|
struct hostent *ph;
|
|
|
|
ast_mutex_lock(&__mutex); /* begin critical area */
|
|
|
|
hsave = h_errno;
|
|
|
|
|
|
|
|
ph = gethostbyname(name);
|
|
|
|
*h_errnop = h_errno; /* copy h_errno to *h_herrnop */
|
|
|
|
if (ph == NULL) {
|
|
|
|
*result = NULL;
|
|
|
|
} else {
|
|
|
|
char **p, **q;
|
|
|
|
char *pbuf;
|
|
|
|
int nbytes=0;
|
|
|
|
int naddr=0, naliases=0;
|
|
|
|
/* determine if we have enough space in buf */
|
|
|
|
|
|
|
|
/* count how many addresses */
|
|
|
|
for (p = ph->h_addr_list; *p != 0; p++) {
|
|
|
|
nbytes += ph->h_length; /* addresses */
|
|
|
|
nbytes += sizeof(*p); /* pointers */
|
|
|
|
naddr++;
|
|
|
|
}
|
|
|
|
nbytes += sizeof(*p); /* one more for the terminating NULL */
|
|
|
|
|
|
|
|
/* count how many aliases, and total length of strings */
|
|
|
|
for (p = ph->h_aliases; *p != 0; p++) {
|
|
|
|
nbytes += (strlen(*p)+1); /* aliases */
|
|
|
|
nbytes += sizeof(*p); /* pointers */
|
|
|
|
naliases++;
|
|
|
|
}
|
|
|
|
nbytes += sizeof(*p); /* one more for the terminating NULL */
|
|
|
|
|
|
|
|
/* here nbytes is the number of bytes required in buffer */
|
|
|
|
/* as a terminator must be there, the minimum value is ph->h_length */
|
2006-07-10 19:05:48 +00:00
|
|
|
if (nbytes > buflen) {
|
2004-05-09 08:22:15 +00:00
|
|
|
*result = NULL;
|
|
|
|
ast_mutex_unlock(&__mutex); /* end critical area */
|
|
|
|
return ERANGE; /* not enough space in buf!! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There is enough space. Now we need to do a deep copy! */
|
|
|
|
/* Allocation in buffer:
|
|
|
|
from [0] to [(naddr-1) * sizeof(*p)]:
|
|
|
|
pointers to addresses
|
|
|
|
at [naddr * sizeof(*p)]:
|
|
|
|
NULL
|
|
|
|
from [(naddr+1) * sizeof(*p)] to [(naddr+naliases) * sizeof(*p)] :
|
|
|
|
pointers to aliases
|
|
|
|
at [(naddr+naliases+1) * sizeof(*p)]:
|
|
|
|
NULL
|
|
|
|
then naddr addresses (fixed length), and naliases aliases (asciiz).
|
|
|
|
*/
|
|
|
|
|
|
|
|
*ret = *ph; /* copy whole structure (not its address!) */
|
|
|
|
|
|
|
|
/* copy addresses */
|
|
|
|
q = (char **)buf; /* pointer to pointers area (type: char **) */
|
|
|
|
ret->h_addr_list = q; /* update pointer to address list */
|
2006-07-10 19:05:48 +00:00
|
|
|
pbuf = buf + ((naddr + naliases + 2) * sizeof(*p)); /* skip that area */
|
2004-05-09 08:22:15 +00:00
|
|
|
for (p = ph->h_addr_list; *p != 0; p++) {
|
|
|
|
memcpy(pbuf, *p, ph->h_length); /* copy address bytes */
|
|
|
|
*q++ = pbuf; /* the pointer is the one inside buf... */
|
|
|
|
pbuf += ph->h_length; /* advance pbuf */
|
|
|
|
}
|
|
|
|
*q++ = NULL; /* address list terminator */
|
|
|
|
|
|
|
|
/* copy aliases */
|
|
|
|
ret->h_aliases = q; /* update pointer to aliases list */
|
|
|
|
for (p = ph->h_aliases; *p != 0; p++) {
|
|
|
|
strcpy(pbuf, *p); /* copy alias strings */
|
|
|
|
*q++ = pbuf; /* the pointer is the one inside buf... */
|
|
|
|
pbuf += strlen(*p); /* advance pbuf */
|
|
|
|
*pbuf++ = 0; /* string terminator */
|
|
|
|
}
|
|
|
|
*q++ = NULL; /* terminator */
|
|
|
|
|
|
|
|
strcpy(pbuf, ph->h_name); /* copy alias strings */
|
|
|
|
ret->h_name = pbuf;
|
|
|
|
pbuf += strlen(ph->h_name); /* advance pbuf */
|
|
|
|
*pbuf++ = 0; /* string terminator */
|
|
|
|
|
|
|
|
*result = ret; /* and let *result point to structure */
|
|
|
|
|
|
|
|
}
|
|
|
|
h_errno = hsave; /* restore h_errno */
|
|
|
|
ast_mutex_unlock(&__mutex); /* end critical area */
|
|
|
|
|
2004-05-15 05:02:42 +00:00
|
|
|
return (*result == NULL); /* return 0 on success, non-zero on error */
|
2004-05-09 08:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief Re-entrant (thread safe) version of gethostbyname that replaces the
|
2005-04-06 21:12:32 +00:00
|
|
|
standard gethostbyname (which is not thread safe)
|
2004-06-29 17:54:25 +00:00
|
|
|
*/
|
2004-05-09 08:22:15 +00:00
|
|
|
struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
int herrno;
|
2005-10-21 04:09:23 +00:00
|
|
|
int dots=0;
|
2004-05-11 18:41:09 +00:00
|
|
|
const char *s;
|
2004-05-09 08:22:15 +00:00
|
|
|
struct hostent *result = NULL;
|
2004-05-11 18:41:09 +00:00
|
|
|
/* Although it is perfectly legitimate to lookup a pure integer, for
|
|
|
|
the sake of the sanity of people who like to name their peers as
|
|
|
|
integers, we break with tradition and refuse to look up a
|
|
|
|
pure integer */
|
|
|
|
s = host;
|
2005-03-17 23:12:15 +00:00
|
|
|
res = 0;
|
2007-01-23 00:11:32 +00:00
|
|
|
while (s && *s) {
|
2005-10-21 04:09:23 +00:00
|
|
|
if (*s == '.')
|
|
|
|
dots++;
|
|
|
|
else if (!isdigit(*s))
|
2004-05-11 18:41:09 +00:00
|
|
|
break;
|
|
|
|
s++;
|
|
|
|
}
|
2005-10-21 04:09:23 +00:00
|
|
|
if (!s || !*s) {
|
|
|
|
/* Forge a reply for IP's to avoid octal IP's being interpreted as octal */
|
|
|
|
if (dots != 3)
|
|
|
|
return NULL;
|
2005-10-21 14:09:03 +00:00
|
|
|
memset(hp, 0, sizeof(struct ast_hostent));
|
2006-10-31 06:21:40 +00:00
|
|
|
hp->hp.h_addrtype = AF_INET;
|
2005-10-31 15:21:12 +00:00
|
|
|
hp->hp.h_addr_list = (void *) hp->buf;
|
2005-10-21 14:09:03 +00:00
|
|
|
hp->hp.h_addr = hp->buf + sizeof(void *);
|
2005-10-21 04:09:23 +00:00
|
|
|
if (inet_pton(AF_INET, host, hp->hp.h_addr) > 0)
|
|
|
|
return &hp->hp;
|
2004-05-11 18:41:09 +00:00
|
|
|
return NULL;
|
2005-10-21 04:09:23 +00:00
|
|
|
|
|
|
|
}
|
2004-12-14 23:36:30 +00:00
|
|
|
#ifdef SOLARIS
|
|
|
|
result = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &herrno);
|
|
|
|
|
|
|
|
if (!result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
|
|
|
|
return NULL;
|
|
|
|
#else
|
2004-05-09 08:22:15 +00:00
|
|
|
res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
|
|
|
|
|
2004-05-13 21:54:30 +00:00
|
|
|
if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
|
2004-05-09 08:22:15 +00:00
|
|
|
return NULL;
|
2004-12-14 23:36:30 +00:00
|
|
|
#endif
|
2004-05-09 08:22:15 +00:00
|
|
|
return &hp->hp;
|
|
|
|
}
|
2004-06-22 17:42:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AST_MUTEX_DEFINE_STATIC(test_lock);
|
|
|
|
AST_MUTEX_DEFINE_STATIC(test_lock2);
|
|
|
|
static pthread_t test_thread;
|
|
|
|
static int lock_count = 0;
|
|
|
|
static int test_errors = 0;
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief This is a regression test for recursive mutexes.
|
|
|
|
test_for_thread_safety() will return 0 if recursive mutex locks are
|
|
|
|
working properly, and non-zero if they are not working properly. */
|
2004-06-22 17:42:14 +00:00
|
|
|
static void *test_thread_body(void *data)
|
|
|
|
{
|
2004-06-22 19:53:36 +00:00
|
|
|
ast_mutex_lock(&test_lock);
|
|
|
|
lock_count += 10;
|
|
|
|
if (lock_count != 10)
|
|
|
|
test_errors++;
|
|
|
|
ast_mutex_lock(&test_lock);
|
|
|
|
lock_count += 10;
|
|
|
|
if (lock_count != 20)
|
|
|
|
test_errors++;
|
|
|
|
ast_mutex_lock(&test_lock2);
|
|
|
|
ast_mutex_unlock(&test_lock);
|
|
|
|
lock_count -= 10;
|
|
|
|
if (lock_count != 10)
|
|
|
|
test_errors++;
|
|
|
|
ast_mutex_unlock(&test_lock);
|
|
|
|
lock_count -= 10;
|
|
|
|
ast_mutex_unlock(&test_lock2);
|
|
|
|
if (lock_count != 0)
|
|
|
|
test_errors++;
|
|
|
|
return NULL;
|
2004-06-22 17:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int test_for_thread_safety(void)
|
|
|
|
{
|
2004-06-22 19:53:36 +00:00
|
|
|
ast_mutex_lock(&test_lock2);
|
|
|
|
ast_mutex_lock(&test_lock);
|
|
|
|
lock_count += 1;
|
|
|
|
ast_mutex_lock(&test_lock);
|
|
|
|
lock_count += 1;
|
2004-08-08 17:15:02 +00:00
|
|
|
ast_pthread_create(&test_thread, NULL, test_thread_body, NULL);
|
2004-06-22 19:53:36 +00:00
|
|
|
usleep(100);
|
|
|
|
if (lock_count != 2)
|
|
|
|
test_errors++;
|
|
|
|
ast_mutex_unlock(&test_lock);
|
|
|
|
lock_count -= 1;
|
|
|
|
usleep(100);
|
|
|
|
if (lock_count != 1)
|
|
|
|
test_errors++;
|
|
|
|
ast_mutex_unlock(&test_lock);
|
|
|
|
lock_count -= 1;
|
|
|
|
if (lock_count != 0)
|
|
|
|
test_errors++;
|
|
|
|
ast_mutex_unlock(&test_lock2);
|
|
|
|
usleep(100);
|
|
|
|
if (lock_count != 0)
|
|
|
|
test_errors++;
|
|
|
|
pthread_join(test_thread, NULL);
|
|
|
|
return(test_errors); /* return 0 on success. */
|
2004-06-22 17:42:14 +00:00
|
|
|
}
|
2004-06-25 04:07:52 +00:00
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief Produce 32 char MD5 hash of value. */
|
2005-02-18 05:29:31 +00:00
|
|
|
void ast_md5_hash(char *output, char *input)
|
|
|
|
{
|
2005-05-04 04:05:34 +00:00
|
|
|
struct MD5Context md5;
|
|
|
|
unsigned char digest[16];
|
|
|
|
char *ptr;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
MD5Init(&md5);
|
2005-08-07 06:47:20 +00:00
|
|
|
MD5Update(&md5, (unsigned char *)input, strlen(input));
|
2005-05-04 04:05:34 +00:00
|
|
|
MD5Final(digest, &md5);
|
|
|
|
ptr = output;
|
2006-07-10 19:05:48 +00:00
|
|
|
for (x = 0; x < 16; x++)
|
2005-05-04 04:05:34 +00:00
|
|
|
ptr += sprintf(ptr, "%2.2x", digest[x]);
|
2005-02-18 05:29:31 +00:00
|
|
|
}
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief Produce 40 char SHA1 hash of value. */
|
2006-02-03 22:37:29 +00:00
|
|
|
void ast_sha1_hash(char *output, char *input)
|
|
|
|
{
|
|
|
|
struct SHA1Context sha;
|
|
|
|
char *ptr;
|
|
|
|
int x;
|
|
|
|
uint8_t Message_Digest[20];
|
|
|
|
|
|
|
|
SHA1Reset(&sha);
|
|
|
|
|
|
|
|
SHA1Input(&sha, (const unsigned char *) input, strlen(input));
|
|
|
|
|
|
|
|
SHA1Result(&sha, Message_Digest);
|
|
|
|
ptr = output;
|
|
|
|
for (x = 0; x < 20; x++)
|
|
|
|
ptr += sprintf(ptr, "%2.2x", Message_Digest[x]);
|
|
|
|
}
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief decode BASE64 encoded text */
|
2005-10-16 03:10:33 +00:00
|
|
|
int ast_base64decode(unsigned char *dst, const char *src, int max)
|
2004-06-25 04:07:52 +00:00
|
|
|
{
|
|
|
|
int cnt = 0;
|
|
|
|
unsigned int byte = 0;
|
|
|
|
unsigned int bits = 0;
|
|
|
|
int incnt = 0;
|
2007-01-23 00:11:32 +00:00
|
|
|
while (*src && (cnt < max)) {
|
2004-06-25 04:07:52 +00:00
|
|
|
/* Shift in 6 bits of input */
|
|
|
|
byte <<= 6;
|
|
|
|
byte |= (b2a[(int)(*src)]) & 0x3f;
|
|
|
|
bits += 6;
|
|
|
|
src++;
|
|
|
|
incnt++;
|
|
|
|
/* If we have at least 8 bits left over, take that character
|
|
|
|
off the top */
|
|
|
|
if (bits >= 8) {
|
|
|
|
bits -= 8;
|
|
|
|
*dst = (byte >> bits) & 0xff;
|
|
|
|
dst++;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Dont worry about left over bits, they're extra anyway */
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief encode text to BASE64 coding */
|
2006-06-01 19:05:45 +00:00
|
|
|
int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks)
|
2004-06-25 04:07:52 +00:00
|
|
|
{
|
|
|
|
int cnt = 0;
|
2006-06-01 19:05:45 +00:00
|
|
|
int col = 0;
|
2004-06-25 04:07:52 +00:00
|
|
|
unsigned int byte = 0;
|
|
|
|
int bits = 0;
|
|
|
|
int cntin = 0;
|
2006-06-01 19:05:45 +00:00
|
|
|
/* Reserve space for null byte at end of string */
|
2004-06-25 04:07:52 +00:00
|
|
|
max--;
|
2006-07-10 19:05:48 +00:00
|
|
|
while ((cntin < srclen) && (cnt < max)) {
|
2004-06-25 04:07:52 +00:00
|
|
|
byte <<= 8;
|
|
|
|
byte |= *(src++);
|
|
|
|
bits += 8;
|
|
|
|
cntin++;
|
2006-08-25 17:48:34 +00:00
|
|
|
if ((bits == 24) && (cnt + 4 <= max)) {
|
2006-06-01 19:05:45 +00:00
|
|
|
*dst++ = base64[(byte >> 18) & 0x3f];
|
|
|
|
*dst++ = base64[(byte >> 12) & 0x3f];
|
|
|
|
*dst++ = base64[(byte >> 6) & 0x3f];
|
|
|
|
*dst++ = base64[byte & 0x3f];
|
|
|
|
cnt += 4;
|
|
|
|
col += 4;
|
|
|
|
bits = 0;
|
|
|
|
byte = 0;
|
|
|
|
}
|
|
|
|
if (linebreaks && (cnt < max) && (col == 64)) {
|
|
|
|
*dst++ = '\n';
|
2004-06-25 04:07:52 +00:00
|
|
|
cnt++;
|
2006-06-01 19:05:45 +00:00
|
|
|
col = 0;
|
2004-06-25 04:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-25 17:48:34 +00:00
|
|
|
if (bits && (cnt + 4 <= max)) {
|
2004-06-25 04:07:52 +00:00
|
|
|
/* Add one last character for the remaining bits,
|
|
|
|
padding the rest with 0 */
|
2006-06-01 19:05:45 +00:00
|
|
|
byte <<= 24 - bits;
|
|
|
|
*dst++ = base64[(byte >> 18) & 0x3f];
|
|
|
|
*dst++ = base64[(byte >> 12) & 0x3f];
|
|
|
|
if (bits == 16)
|
|
|
|
*dst++ = base64[(byte >> 6) & 0x3f];
|
|
|
|
else
|
|
|
|
*dst++ = '=';
|
|
|
|
*dst++ = '=';
|
|
|
|
cnt += 4;
|
|
|
|
}
|
|
|
|
if (linebreaks && (cnt < max)) {
|
|
|
|
*dst++ = '\n';
|
2004-06-25 04:07:52 +00:00
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2006-06-01 19:05:45 +00:00
|
|
|
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
|
|
|
|
{
|
|
|
|
return ast_base64encode_full(dst, src, srclen, max, 0);
|
|
|
|
}
|
|
|
|
|
2004-06-25 04:07:52 +00:00
|
|
|
static void base64_init(void)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
memset(b2a, -1, sizeof(b2a));
|
|
|
|
/* Initialize base-64 Conversion table */
|
2006-07-10 19:05:48 +00:00
|
|
|
for (x = 0; x < 26; x++) {
|
2004-06-25 04:07:52 +00:00
|
|
|
/* A-Z */
|
|
|
|
base64[x] = 'A' + x;
|
|
|
|
b2a['A' + x] = x;
|
|
|
|
/* a-z */
|
|
|
|
base64[x + 26] = 'a' + x;
|
|
|
|
b2a['a' + x] = x + 26;
|
|
|
|
/* 0-9 */
|
|
|
|
if (x < 10) {
|
|
|
|
base64[x + 52] = '0' + x;
|
|
|
|
b2a['0' + x] = x + 52;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
base64[62] = '+';
|
|
|
|
base64[63] = '/';
|
|
|
|
b2a[(int)'+'] = 62;
|
|
|
|
b2a[(int)'/'] = 63;
|
|
|
|
}
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief ast_uri_encode: Turn text string to URI-encoded %XX version
|
|
|
|
\note At this point, we're converting from ISO-8859-x (8-bit), not UTF8
|
2005-08-29 22:39:39 +00:00
|
|
|
as in the SIP protocol spec
|
|
|
|
If doreserved == 1 we will convert reserved characters also.
|
|
|
|
RFC 2396, section 2.4
|
|
|
|
outbuf needs to have more memory allocated than the instring
|
|
|
|
to have room for the expansion. Every char that is converted
|
|
|
|
is replaced by three ASCII characters.
|
|
|
|
|
|
|
|
Note: The doreserved option is needed for replaces header in
|
|
|
|
SIP transfers.
|
|
|
|
*/
|
2006-01-04 21:54:09 +00:00
|
|
|
char *ast_uri_encode(const char *string, char *outbuf, int buflen, int doreserved)
|
2005-08-29 22:39:39 +00:00
|
|
|
{
|
|
|
|
char *reserved = ";/?:@&=+$, "; /* Reserved chars */
|
|
|
|
|
2006-01-04 21:54:09 +00:00
|
|
|
const char *ptr = string; /* Start with the string */
|
2005-08-29 22:39:39 +00:00
|
|
|
char *out = NULL;
|
|
|
|
char *buf = NULL;
|
|
|
|
|
2006-10-25 14:44:50 +00:00
|
|
|
ast_copy_string(outbuf, string, buflen);
|
2005-08-29 22:39:39 +00:00
|
|
|
|
|
|
|
/* If there's no characters to convert, just go through and don't do anything */
|
|
|
|
while (*ptr) {
|
|
|
|
if (((unsigned char) *ptr) > 127 || (doreserved && strchr(reserved, *ptr)) ) {
|
|
|
|
/* Oops, we need to start working here */
|
|
|
|
if (!buf) {
|
|
|
|
buf = outbuf;
|
|
|
|
out = buf + (ptr - string) ; /* Set output ptr */
|
|
|
|
}
|
|
|
|
out += sprintf(out, "%%%02x", (unsigned char) *ptr);
|
|
|
|
} else if (buf) {
|
|
|
|
*out = *ptr; /* Continue copying the string */
|
|
|
|
out++;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
if (buf)
|
|
|
|
*out = '\0';
|
|
|
|
return outbuf;
|
|
|
|
}
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief ast_uri_decode: Decode SIP URI, URN, URL (overwrite the string) */
|
2005-08-29 22:39:39 +00:00
|
|
|
void ast_uri_decode(char *s)
|
|
|
|
{
|
|
|
|
char *o;
|
|
|
|
unsigned int tmp;
|
|
|
|
|
|
|
|
for (o = s; *s; s++, o++) {
|
|
|
|
if (*s == '%' && strlen(s) > 2 && sscanf(s + 1, "%2x", &tmp) == 1) {
|
|
|
|
/* have '%', two chars and correct parsing */
|
|
|
|
*o = tmp;
|
|
|
|
s += 2; /* Will be incremented once more when we break out */
|
|
|
|
} else /* all other cases, just copy */
|
|
|
|
*o = *s;
|
|
|
|
}
|
|
|
|
*o = '\0';
|
|
|
|
}
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa */
|
2006-07-21 17:31:28 +00:00
|
|
|
const char *ast_inet_ntoa(struct in_addr ia)
|
2004-06-29 12:56:46 +00:00
|
|
|
{
|
2006-07-21 17:31:28 +00:00
|
|
|
char *buf;
|
|
|
|
|
Merge team/russell/ast_verbose_threadstorage
- instead of defining a free() wrapper in a bunch of files, define it as
ast_free() in utils.h and remove the copies from all the files.
- centralize and abstract the code used for doing thread storage. The code
lives in threadstorage.h, with one function being implemented in utils.c.
This new API includes generic thread storage as well as special functions
for handling thread local dynamic length string buffers.
- update ast_inet_ntoa() to use the new threadstorage API
- update ast_state2str() to use the new threadstorage API
- update ast_cli() to use the new threadstorage API
- Modify manager_event() to use thread storage. Instead of using a buffer of
4096 characters as the workspace for building the manager event, use a thread
local dynamic string. Now there is no length limitation on the length of the
body of a manager event.
- Significantly simplify the handling of ast_verbose() ...
- Instead of using a static char buffer and a lock to make sure only one
thread can be using ast_verbose() at a time, use a thread local dynamic
string as the workspace for preparing the verbose message. Instead of
locking around the entire function, the only locking done now is when the
message has been built and is being deliviered to the list of registered
verbose message handlers.
- This function was doing a strdup() on every message passed to it and
keeping a queue of the last 200 messages in memory. This has been
completely removed. The only place this was used was that if there were
any messages in the verbose queue when a verbose handler was registered,
all of the messages in the queue would be fed to it. So, I just made sure
that the console verbose handler and the network verbose handler (for
remote asterisk consoles) were registered before any verbose messages.
pbx_gtkconsole and pbx_kdeconsole will now lose a few verbose messages at
startup, but I didn't feel the performance hit of this message queue was
worth saving the initial verbose output for these very rarely used modules.
- I have removed the last three arguments to the verbose handlers, leaving
only the string itself because they aren't needed anymore. For example,
ast_verbose had some logic for telling the verbose handler to add
a newline if the buffer was completely full. Now that the buffer can grow
as needed, this doesn't matter anymore.
- remove unused function, ast_verbose_dmesg() which was to dispatch the
message queue
- Convert the list of verbose handlers to use the linked list macros.
- add missing newline characters to a few ast_verbose() calls
- convert the list of log channels to use the linked list macros in logger.c
- fix close_logger() to close all of the files it opened for logging
- update ast_log() to use a thread local dynamic string for its workspace
for preparing log messages instead of a buffer of size BUFSIZ (8kB on my
system) allocated on the stack. The dynamic string in this case is limited
to only growing to a maximum size of BUFSIZ.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@39272 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-08-08 06:32:04 +00:00
|
|
|
if (!(buf = ast_threadstorage_get(&inet_ntoa_buf, INET_ADDRSTRLEN)))
|
|
|
|
return "";
|
2006-07-21 17:31:28 +00:00
|
|
|
|
|
|
|
return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
|
2004-06-29 12:56:46 +00:00
|
|
|
}
|
2004-06-25 04:07:52 +00:00
|
|
|
|
2007-07-21 14:39:52 +00:00
|
|
|
#ifdef HAVE_DEV_URANDOM
|
|
|
|
static int dev_urandom_fd;
|
|
|
|
#endif
|
|
|
|
|
2004-06-25 04:07:52 +00:00
|
|
|
int ast_utils_init(void)
|
|
|
|
{
|
2007-07-21 14:39:52 +00:00
|
|
|
#ifdef HAVE_DEV_URANDOM
|
|
|
|
dev_urandom_fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
#endif
|
2004-06-25 04:07:52 +00:00
|
|
|
base64_init();
|
|
|
|
return 0;
|
|
|
|
}
|
2004-08-08 17:15:02 +00:00
|
|
|
|
2005-03-08 20:47:30 +00:00
|
|
|
#ifndef __linux__
|
|
|
|
#undef pthread_create /* For ast_pthread_create function only */
|
2005-10-31 21:25:21 +00:00
|
|
|
#endif /* !__linux__ */
|
2005-08-29 22:39:39 +00:00
|
|
|
|
2006-10-04 21:10:36 +00:00
|
|
|
#if !defined(LOW_MEMORY)
|
2006-04-12 20:40:46 +00:00
|
|
|
/*
|
|
|
|
* support for 'show threads'. The start routine is wrapped by
|
|
|
|
* dummy_start(), so that ast_register_thread() and
|
|
|
|
* ast_unregister_thread() know the thread identifier.
|
|
|
|
*/
|
|
|
|
struct thr_arg {
|
|
|
|
void *(*start_routine)(void *);
|
|
|
|
void *data;
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* on OS/X, pthread_cleanup_push() and pthread_cleanup_pop()
|
|
|
|
* are odd macros which start and end a block, so they _must_ be
|
|
|
|
* used in pairs (the latter with a '1' argument to call the
|
|
|
|
* handler on exit.
|
2006-10-04 19:51:38 +00:00
|
|
|
* On BSD we don't need this, but we keep it for compatibility.
|
2006-04-12 20:40:46 +00:00
|
|
|
*/
|
|
|
|
static void *dummy_start(void *data)
|
2004-08-08 17:15:02 +00:00
|
|
|
{
|
2006-04-12 20:40:46 +00:00
|
|
|
void *ret;
|
2006-10-04 19:51:38 +00:00
|
|
|
struct thr_arg a = *((struct thr_arg *) data); /* make a local copy */
|
2006-04-12 20:40:46 +00:00
|
|
|
|
2006-10-04 19:51:38 +00:00
|
|
|
/* note that even though data->name is a pointer to allocated memory,
|
|
|
|
we are not freeing it here because ast_register_thread is going to
|
|
|
|
keep a copy of the pointer and then ast_unregister_thread will
|
|
|
|
free the memory
|
|
|
|
*/
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(data);
|
2006-04-12 20:40:46 +00:00
|
|
|
ast_register_thread(a.name);
|
2006-10-04 19:51:38 +00:00
|
|
|
pthread_cleanup_push(ast_unregister_thread, (void *) pthread_self());
|
2006-04-12 20:40:46 +00:00
|
|
|
ret = a.start_routine(a.data);
|
|
|
|
pthread_cleanup_pop(1);
|
2006-10-04 19:51:38 +00:00
|
|
|
|
2006-04-12 20:40:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-10-04 21:10:36 +00:00
|
|
|
#endif /* !LOW_MEMORY */
|
|
|
|
|
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)
|
2006-04-12 20:40:46 +00:00
|
|
|
{
|
2006-10-04 21:10:36 +00:00
|
|
|
#if !defined(LOW_MEMORY)
|
2006-04-12 20:40:46 +00:00
|
|
|
struct thr_arg *a;
|
2006-10-04 21:10:36 +00:00
|
|
|
#endif
|
2006-04-12 20:40:46 +00:00
|
|
|
|
2004-08-08 17:15:02 +00:00
|
|
|
if (!attr) {
|
2006-10-04 19:51:38 +00:00
|
|
|
attr = alloca(sizeof(*attr));
|
|
|
|
pthread_attr_init(attr);
|
2004-08-08 17:15:02 +00:00
|
|
|
}
|
2006-10-04 19:51:38 +00:00
|
|
|
|
2005-09-29 04:08:18 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
/* On Linux, pthread_attr_init() defaults to PTHREAD_EXPLICIT_SCHED,
|
|
|
|
which is kind of useless. Change this here to
|
|
|
|
PTHREAD_INHERIT_SCHED; that way the -p option to set realtime
|
|
|
|
priority will propagate down to new threads by default.
|
|
|
|
This does mean that callers cannot set a different priority using
|
|
|
|
PTHREAD_EXPLICIT_SCHED in the attr argument; instead they must set
|
|
|
|
the priority afterwards with pthread_setschedparam(). */
|
2006-10-04 19:51:38 +00:00
|
|
|
if ((errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED)))
|
|
|
|
ast_log(LOG_WARNING, "pthread_attr_setinheritsched: %s\n", strerror(errno));
|
2005-09-29 04:08:18 +00:00
|
|
|
#endif
|
|
|
|
|
2005-03-04 00:01:41 +00:00
|
|
|
if (!stacksize)
|
|
|
|
stacksize = AST_STACKSIZE;
|
2006-10-04 19:51:38 +00:00
|
|
|
|
|
|
|
if ((errno = pthread_attr_setstacksize(attr, stacksize ? stacksize : AST_STACKSIZE)))
|
|
|
|
ast_log(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(errno));
|
|
|
|
|
2006-10-04 21:10:36 +00:00
|
|
|
#if !defined(LOW_MEMORY)
|
2006-10-04 19:51:38 +00:00
|
|
|
if ((a = ast_malloc(sizeof(*a)))) {
|
2006-04-12 20:40:46 +00:00
|
|
|
a->start_routine = start_routine;
|
|
|
|
a->data = data;
|
|
|
|
start_routine = dummy_start;
|
|
|
|
asprintf(&a->name, "%-20s started at [%5d] %s %s()",
|
2006-10-04 19:51:38 +00:00
|
|
|
start_fn, line, file, caller);
|
2006-04-12 20:40:46 +00:00
|
|
|
data = a;
|
|
|
|
}
|
2006-10-04 21:10:36 +00:00
|
|
|
#endif /* !LOW_MEMORY */
|
2006-10-04 19:51:38 +00:00
|
|
|
|
2004-08-08 17:15:02 +00:00
|
|
|
return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */
|
|
|
|
}
|
2004-08-27 04:21:09 +00:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
unsigned char attr_destroy = 0;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!attr) {
|
|
|
|
attr = alloca(sizeof(*attr));
|
|
|
|
pthread_attr_init(attr);
|
|
|
|
attr_destroy = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((errno = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED)))
|
|
|
|
ast_log(LOG_WARNING, "pthread_attr_setdetachstate: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
res = ast_pthread_create_stack(thread, attr, start_routine, data,
|
|
|
|
stacksize, file, caller, line, start_fn);
|
|
|
|
|
|
|
|
if (attr_destroy)
|
|
|
|
pthread_attr_destroy(attr);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-03-05 03:20:55 +00:00
|
|
|
int ast_wait_for_input(int fd, int ms)
|
|
|
|
{
|
|
|
|
struct pollfd pfd[1];
|
|
|
|
memset(pfd, 0, sizeof(pfd));
|
|
|
|
pfd[0].fd = fd;
|
|
|
|
pfd[0].events = POLLIN|POLLPRI;
|
|
|
|
return poll(pfd, 1, ms);
|
|
|
|
}
|
|
|
|
|
2006-11-07 08:34:26 +00:00
|
|
|
/*!
|
|
|
|
* Try to write string, but wait no more than ms milliseconds before timing out.
|
|
|
|
*
|
|
|
|
* \note The code assumes that the file descriptor has NONBLOCK set,
|
|
|
|
* so 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.
|
|
|
|
* If the descriptor is blocking, all assumptions on the guaranteed
|
|
|
|
* detail do not apply anymore.
|
|
|
|
* Also note that in the current implementation, the delay is per-write,
|
|
|
|
* so you still have no guarantees, anyways.
|
|
|
|
* Fortunately the routine is only used in a few places (cli.c, manager.c,
|
|
|
|
* res_agi.c) so it is reasonably easy to check how it behaves there.
|
|
|
|
*
|
|
|
|
* XXX We either need to fix the code, or fix the documentation.
|
|
|
|
*/
|
2006-05-05 21:01:39 +00:00
|
|
|
int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
|
|
|
|
{
|
|
|
|
/* Try to write string, but wait no more than ms milliseconds
|
|
|
|
before timing out */
|
|
|
|
int res = 0;
|
|
|
|
struct pollfd fds[1];
|
|
|
|
while (len) {
|
|
|
|
res = write(fd, s, len);
|
|
|
|
if ((res < 0) && (errno != EAGAIN)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (res < 0)
|
|
|
|
res = 0;
|
|
|
|
len -= res;
|
|
|
|
s += res;
|
|
|
|
res = 0;
|
|
|
|
if (len) {
|
|
|
|
fds[0].fd = fd;
|
|
|
|
fds[0].events = POLLOUT;
|
|
|
|
/* Wait until writable again */
|
|
|
|
res = poll(fds, 1, timeoutms);
|
|
|
|
if (res < 1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-06-24 22:45:15 +00:00
|
|
|
char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
|
|
|
|
{
|
|
|
|
char *e;
|
|
|
|
char *q;
|
|
|
|
|
|
|
|
s = ast_strip(s);
|
2007-01-17 20:57:51 +00:00
|
|
|
if ((q = strchr(beg_quotes, *s)) && *q != '\0') {
|
2005-06-24 22:45:15 +00:00
|
|
|
e = s + strlen(s) - 1;
|
|
|
|
if (*e == *(end_quotes + (q - beg_quotes))) {
|
|
|
|
s++;
|
|
|
|
*e = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-10-01 15:41:27 +00:00
|
|
|
int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap)
|
2005-05-15 22:33:02 +00:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (!buffer || !*buffer || !space || !*space)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
result = vsnprintf(*buffer, *space, fmt, ap);
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
return -1;
|
|
|
|
else if (result > *space)
|
|
|
|
result = *space;
|
|
|
|
|
|
|
|
*buffer += result;
|
|
|
|
*space -= result;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-01 15:41:27 +00:00
|
|
|
int ast_build_string(char **buffer, size_t *space, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
result = ast_build_string_va(buffer, space, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-06-24 22:45:15 +00:00
|
|
|
int ast_true(const char *s)
|
|
|
|
{
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(s))
|
2005-06-24 22:45:15 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Determine if this is a true value */
|
|
|
|
if (!strcasecmp(s, "yes") ||
|
|
|
|
!strcasecmp(s, "true") ||
|
|
|
|
!strcasecmp(s, "y") ||
|
|
|
|
!strcasecmp(s, "t") ||
|
|
|
|
!strcasecmp(s, "1") ||
|
|
|
|
!strcasecmp(s, "on"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_false(const char *s)
|
|
|
|
{
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(s))
|
2005-06-24 22:45:15 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Determine if this is a false value */
|
|
|
|
if (!strcasecmp(s, "no") ||
|
|
|
|
!strcasecmp(s, "false") ||
|
|
|
|
!strcasecmp(s, "n") ||
|
|
|
|
!strcasecmp(s, "f") ||
|
|
|
|
!strcasecmp(s, "0") ||
|
|
|
|
!strcasecmp(s, "off"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-15 23:00:47 +00:00
|
|
|
#define ONE_MILLION 1000000
|
|
|
|
/*
|
|
|
|
* put timeval in a valid range. usec is 0..999999
|
|
|
|
* negative values are not allowed and truncated.
|
|
|
|
*/
|
|
|
|
static struct timeval tvfix(struct timeval a)
|
|
|
|
{
|
|
|
|
if (a.tv_usec >= ONE_MILLION) {
|
2005-08-23 17:55:59 +00:00
|
|
|
ast_log(LOG_WARNING, "warning too large timestamp %ld.%ld\n",
|
2005-09-02 11:39:24 +00:00
|
|
|
a.tv_sec, (long int) a.tv_usec);
|
2005-12-13 16:07:19 +00:00
|
|
|
a.tv_sec += a.tv_usec / ONE_MILLION;
|
2005-07-15 23:00:47 +00:00
|
|
|
a.tv_usec %= ONE_MILLION;
|
|
|
|
} else if (a.tv_usec < 0) {
|
2005-07-19 15:32:03 +00:00
|
|
|
ast_log(LOG_WARNING, "warning negative timestamp %ld.%ld\n",
|
2005-12-13 16:07:19 +00:00
|
|
|
a.tv_sec, (long int) a.tv_usec);
|
2005-07-15 23:00:47 +00:00
|
|
|
a.tv_usec = 0;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval ast_tvadd(struct timeval a, struct timeval b)
|
|
|
|
{
|
|
|
|
/* consistency checks to guarantee usec in 0..999999 */
|
|
|
|
a = tvfix(a);
|
|
|
|
b = tvfix(b);
|
|
|
|
a.tv_sec += b.tv_sec;
|
|
|
|
a.tv_usec += b.tv_usec;
|
|
|
|
if (a.tv_usec >= ONE_MILLION) {
|
|
|
|
a.tv_sec++;
|
|
|
|
a.tv_usec -= ONE_MILLION;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval ast_tvsub(struct timeval a, struct timeval b)
|
|
|
|
{
|
|
|
|
/* consistency checks to guarantee usec in 0..999999 */
|
|
|
|
a = tvfix(a);
|
|
|
|
b = tvfix(b);
|
|
|
|
a.tv_sec -= b.tv_sec;
|
|
|
|
a.tv_usec -= b.tv_usec;
|
|
|
|
if (a.tv_usec < 0) {
|
|
|
|
a.tv_sec-- ;
|
|
|
|
a.tv_usec += ONE_MILLION;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
#undef ONE_MILLION
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief glibc puts a lock inside random(3), so that the results are thread-safe.
|
2006-01-10 00:55:45 +00:00
|
|
|
* BSD libc (and others) do not. */
|
2006-01-10 08:33:52 +00:00
|
|
|
|
2007-07-21 14:39:52 +00:00
|
|
|
#ifndef linux
|
2006-01-10 08:33:52 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(randomlock);
|
2007-07-21 14:39:52 +00:00
|
|
|
#endif
|
2006-01-10 08:33:52 +00:00
|
|
|
|
2006-01-10 00:55:45 +00:00
|
|
|
long int ast_random(void)
|
|
|
|
{
|
|
|
|
long int res;
|
2007-07-21 14:39:52 +00:00
|
|
|
#ifdef HAVE_DEV_URANDOM
|
|
|
|
if (dev_urandom_fd >= 0) {
|
|
|
|
int read_res = read(dev_urandom_fd, &res, sizeof(res));
|
|
|
|
if (read_res > 0)
|
2007-07-26 13:20:36 +00:00
|
|
|
return labs(res);
|
2007-07-21 14:39:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef linux
|
|
|
|
res = random();
|
|
|
|
#else
|
2006-01-10 00:55:45 +00:00
|
|
|
ast_mutex_lock(&randomlock);
|
|
|
|
res = random();
|
|
|
|
ast_mutex_unlock(&randomlock);
|
2007-07-21 14:39:52 +00:00
|
|
|
#endif
|
2006-01-10 00:55:45 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-09-23 02:57:14 +00:00
|
|
|
char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
|
|
|
|
{
|
|
|
|
char *dataPut = start;
|
|
|
|
int inEscape = 0;
|
|
|
|
int inQuotes = 0;
|
|
|
|
|
|
|
|
for (; *start; start++) {
|
|
|
|
if (inEscape) {
|
|
|
|
*dataPut++ = *start; /* Always goes verbatim */
|
|
|
|
inEscape = 0;
|
2006-07-10 19:05:48 +00:00
|
|
|
} else {
|
2005-09-23 02:57:14 +00:00
|
|
|
if (*start == '\\') {
|
|
|
|
inEscape = 1; /* Do not copy \ into the data */
|
|
|
|
} else if (*start == '\'') {
|
2006-07-10 19:05:48 +00:00
|
|
|
inQuotes = 1 - inQuotes; /* Do not copy ' into the data */
|
2005-09-23 02:57:14 +00:00
|
|
|
} else {
|
|
|
|
/* Replace , with |, unless in quotes */
|
2006-07-10 19:05:48 +00:00
|
|
|
*dataPut++ = inQuotes ? *start : ((*start == find) ? replace_with : *start);
|
2005-09-23 02:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (start != dataPut)
|
|
|
|
*dataPut = 0;
|
|
|
|
return dataPut;
|
|
|
|
}
|
2005-12-20 20:20:04 +00:00
|
|
|
|
2006-07-10 19:58:33 +00:00
|
|
|
void ast_join(char *s, size_t len, char * const w[])
|
2005-12-20 20:20:04 +00:00
|
|
|
{
|
|
|
|
int x, ofs = 0;
|
|
|
|
const char *src;
|
|
|
|
|
|
|
|
/* Join words into a string */
|
|
|
|
if (!s)
|
|
|
|
return;
|
2006-07-10 19:05:48 +00:00
|
|
|
for (x = 0; ofs < len && w[x]; x++) {
|
2005-12-20 20:20:04 +00:00
|
|
|
if (x > 0)
|
|
|
|
s[ofs++] = ' ';
|
|
|
|
for (src = w[x]; *src && ofs < len; src++)
|
|
|
|
s[ofs++] = *src;
|
|
|
|
}
|
|
|
|
if (ofs == len)
|
|
|
|
ofs--;
|
|
|
|
s[ofs] = '\0';
|
|
|
|
}
|
2006-01-04 21:54:09 +00:00
|
|
|
|
2006-02-11 20:54:21 +00:00
|
|
|
const char __ast_string_field_empty[] = "";
|
2006-01-04 21:54:09 +00:00
|
|
|
|
2006-01-26 04:00:05 +00:00
|
|
|
static int add_string_pool(struct ast_string_field_mgr *mgr, size_t size)
|
|
|
|
{
|
|
|
|
struct ast_string_field_pool *pool;
|
|
|
|
|
|
|
|
if (!(pool = ast_calloc(1, sizeof(*pool) + size)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pool->prev = mgr->pool;
|
|
|
|
mgr->pool = pool;
|
|
|
|
mgr->size = size;
|
|
|
|
mgr->space = size;
|
|
|
|
mgr->used = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __ast_string_field_init(struct ast_string_field_mgr *mgr, size_t size,
|
2006-01-04 21:54:09 +00:00
|
|
|
ast_string_field *fields, int num_fields)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
2006-01-26 04:00:05 +00:00
|
|
|
if (add_string_pool(mgr, size))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (index = 0; index < num_fields; index++)
|
|
|
|
fields[index] = __ast_string_field_empty;
|
|
|
|
|
|
|
|
return 0;
|
2006-01-04 21:54:09 +00:00
|
|
|
}
|
|
|
|
|
2006-01-26 04:00:05 +00:00
|
|
|
ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr, size_t needed,
|
2006-01-06 19:19:17 +00:00
|
|
|
ast_string_field *fields, int num_fields)
|
2006-01-04 21:54:09 +00:00
|
|
|
{
|
|
|
|
char *result = NULL;
|
|
|
|
|
2006-01-26 04:00:05 +00:00
|
|
|
if (__builtin_expect(needed > mgr->space, 0)) {
|
|
|
|
size_t new_size = mgr->size * 2;
|
2006-01-04 21:54:09 +00:00
|
|
|
|
2006-01-26 04:00:05 +00:00
|
|
|
while (new_size < needed)
|
2006-01-04 21:54:09 +00:00
|
|
|
new_size *= 2;
|
|
|
|
|
2006-01-26 04:00:05 +00:00
|
|
|
if (add_string_pool(mgr, new_size))
|
2006-01-04 21:54:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-26 04:00:05 +00:00
|
|
|
result = mgr->pool->base + mgr->used;
|
|
|
|
mgr->used += needed;
|
|
|
|
mgr->space -= needed;
|
2006-01-04 21:54:09 +00:00
|
|
|
return result;
|
|
|
|
}
|
2006-01-06 19:19:17 +00:00
|
|
|
|
2006-11-07 21:47:49 +00:00
|
|
|
void __ast_string_field_index_build_va(struct ast_string_field_mgr *mgr,
|
2006-01-06 19:19:17 +00:00
|
|
|
ast_string_field *fields, int num_fields,
|
2006-11-07 21:47:49 +00:00
|
|
|
int index, const char *format, va_list ap1, va_list ap2)
|
2006-01-06 19:19:17 +00:00
|
|
|
{
|
|
|
|
size_t needed;
|
|
|
|
|
2006-01-26 04:00:05 +00:00
|
|
|
needed = vsnprintf(mgr->pool->base + mgr->used, mgr->space, format, ap1) + 1;
|
2006-01-06 19:19:17 +00:00
|
|
|
|
|
|
|
va_end(ap1);
|
|
|
|
|
2006-01-26 04:00:05 +00:00
|
|
|
if (needed > mgr->space) {
|
|
|
|
size_t new_size = mgr->size * 2;
|
|
|
|
|
|
|
|
while (new_size < needed)
|
|
|
|
new_size *= 2;
|
|
|
|
|
|
|
|
if (add_string_pool(mgr, new_size))
|
|
|
|
return;
|
|
|
|
|
|
|
|
vsprintf(mgr->pool->base + mgr->used, format, ap2);
|
|
|
|
}
|
|
|
|
|
|
|
|
fields[index] = mgr->pool->base + mgr->used;
|
|
|
|
mgr->used += needed;
|
|
|
|
mgr->space -= needed;
|
2006-11-07 21:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void __ast_string_field_index_build(struct ast_string_field_mgr *mgr,
|
|
|
|
ast_string_field *fields, int num_fields,
|
|
|
|
int index, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap1, ap2;
|
2006-01-06 19:19:17 +00:00
|
|
|
|
2006-11-07 21:47:49 +00:00
|
|
|
va_start(ap1, format);
|
|
|
|
va_start(ap2, format); /* va_copy does not exist on FreeBSD */
|
|
|
|
|
|
|
|
__ast_string_field_index_build_va(mgr, fields, num_fields, index, format, ap1, ap2);
|
|
|
|
|
|
|
|
va_end(ap1);
|
2006-01-06 19:19:17 +00:00
|
|
|
va_end(ap2);
|
|
|
|
}
|
2006-02-15 00:24:24 +00:00
|
|
|
|
2006-03-30 23:26:22 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(fetchadd_m); /* used for all fetc&add ops */
|
2006-07-10 19:05:48 +00:00
|
|
|
|
2006-03-30 23:26:22 +00:00
|
|
|
int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ast_mutex_lock(&fetchadd_m);
|
|
|
|
ret = *p;
|
|
|
|
*p += v;
|
|
|
|
ast_mutex_unlock(&fetchadd_m);
|
|
|
|
return ret;
|
|
|
|
}
|
2006-02-15 00:24:24 +00:00
|
|
|
|
2007-07-18 19:47:20 +00:00
|
|
|
/*! \brief
|
|
|
|
* get values from config variables.
|
|
|
|
*/
|
|
|
|
int ast_get_timeval(const char *src, struct timeval *dst, struct timeval _default, int *consumed)
|
|
|
|
{
|
|
|
|
long double dtv = 0.0;
|
|
|
|
int scanned;
|
|
|
|
|
|
|
|
if (dst == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*dst = _default;
|
|
|
|
|
|
|
|
if (ast_strlen_zero(src))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* only integer at the moment, but one day we could accept more formats */
|
|
|
|
if (sscanf(src, "%Lf%n", &dtv, &scanned) > 0) {
|
|
|
|
dst->tv_sec = dtv;
|
|
|
|
dst->tv_usec = (dtv - dst->tv_sec) * 1000000.0;
|
|
|
|
if (consumed)
|
|
|
|
*consumed = scanned;
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-03-07 17:55:33 +00:00
|
|
|
/*! \brief
|
2006-02-15 00:24:24 +00:00
|
|
|
* get values from config variables.
|
|
|
|
*/
|
2006-02-23 17:13:57 +00:00
|
|
|
int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
|
2006-02-15 00:24:24 +00:00
|
|
|
{
|
|
|
|
long t;
|
2006-02-23 17:13:57 +00:00
|
|
|
int scanned;
|
2006-02-15 00:24:24 +00:00
|
|
|
|
|
|
|
if (dst == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*dst = _default;
|
|
|
|
|
|
|
|
if (ast_strlen_zero(src))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* only integer at the moment, but one day we could accept more formats */
|
2006-02-23 17:13:57 +00:00
|
|
|
if (sscanf(src, "%ld%n", &t, &scanned) == 1) {
|
2006-02-15 00:24:24 +00:00
|
|
|
*dst = t;
|
2006-02-23 17:13:57 +00:00
|
|
|
if (consumed)
|
|
|
|
*consumed = scanned;
|
2006-02-15 00:24:24 +00:00
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
}
|
2006-07-10 19:05:48 +00:00
|
|
|
|
2006-12-15 22:08:46 +00:00
|
|
|
/*!
|
|
|
|
* core handler for dynamic strings.
|
|
|
|
* This is not meant to be called directly, but rather through the
|
|
|
|
* various wrapper macros
|
2006-12-15 23:10:42 +00:00
|
|
|
* ast_str_set(...)
|
|
|
|
* ast_str_append(...)
|
|
|
|
* ast_str_set_va(...)
|
|
|
|
* ast_str_append_va(...)
|
2006-12-15 22:08:46 +00:00
|
|
|
*/
|
2006-12-15 23:10:42 +00:00
|
|
|
int __ast_str_helper(struct ast_str **buf, size_t max_len,
|
|
|
|
int append, const char *fmt, va_list ap)
|
Merge team/russell/ast_verbose_threadstorage
- instead of defining a free() wrapper in a bunch of files, define it as
ast_free() in utils.h and remove the copies from all the files.
- centralize and abstract the code used for doing thread storage. The code
lives in threadstorage.h, with one function being implemented in utils.c.
This new API includes generic thread storage as well as special functions
for handling thread local dynamic length string buffers.
- update ast_inet_ntoa() to use the new threadstorage API
- update ast_state2str() to use the new threadstorage API
- update ast_cli() to use the new threadstorage API
- Modify manager_event() to use thread storage. Instead of using a buffer of
4096 characters as the workspace for building the manager event, use a thread
local dynamic string. Now there is no length limitation on the length of the
body of a manager event.
- Significantly simplify the handling of ast_verbose() ...
- Instead of using a static char buffer and a lock to make sure only one
thread can be using ast_verbose() at a time, use a thread local dynamic
string as the workspace for preparing the verbose message. Instead of
locking around the entire function, the only locking done now is when the
message has been built and is being deliviered to the list of registered
verbose message handlers.
- This function was doing a strdup() on every message passed to it and
keeping a queue of the last 200 messages in memory. This has been
completely removed. The only place this was used was that if there were
any messages in the verbose queue when a verbose handler was registered,
all of the messages in the queue would be fed to it. So, I just made sure
that the console verbose handler and the network verbose handler (for
remote asterisk consoles) were registered before any verbose messages.
pbx_gtkconsole and pbx_kdeconsole will now lose a few verbose messages at
startup, but I didn't feel the performance hit of this message queue was
worth saving the initial verbose output for these very rarely used modules.
- I have removed the last three arguments to the verbose handlers, leaving
only the string itself because they aren't needed anymore. For example,
ast_verbose had some logic for telling the verbose handler to add
a newline if the buffer was completely full. Now that the buffer can grow
as needed, this doesn't matter anymore.
- remove unused function, ast_verbose_dmesg() which was to dispatch the
message queue
- Convert the list of verbose handlers to use the linked list macros.
- add missing newline characters to a few ast_verbose() calls
- convert the list of log channels to use the linked list macros in logger.c
- fix close_logger() to close all of the files it opened for logging
- update ast_log() to use a thread local dynamic string for its workspace
for preparing log messages instead of a buffer of size BUFSIZ (8kB on my
system) allocated on the stack. The dynamic string in this case is limited
to only growing to a maximum size of BUFSIZ.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@39272 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-08-08 06:32:04 +00:00
|
|
|
{
|
2006-11-28 14:07:09 +00:00
|
|
|
int res, need;
|
2006-12-15 22:08:46 +00:00
|
|
|
int offset = (append && (*buf)->len) ? (*buf)->used : 0;
|
|
|
|
|
|
|
|
if (max_len < 0)
|
|
|
|
max_len = (*buf)->len; /* don't exceed the allocated space */
|
|
|
|
/*
|
|
|
|
* Ask vsnprintf how much space we need. Remember that vsnprintf
|
|
|
|
* does not count the final '\0' so we must add 1.
|
|
|
|
*/
|
Merge team/russell/ast_verbose_threadstorage
- instead of defining a free() wrapper in a bunch of files, define it as
ast_free() in utils.h and remove the copies from all the files.
- centralize and abstract the code used for doing thread storage. The code
lives in threadstorage.h, with one function being implemented in utils.c.
This new API includes generic thread storage as well as special functions
for handling thread local dynamic length string buffers.
- update ast_inet_ntoa() to use the new threadstorage API
- update ast_state2str() to use the new threadstorage API
- update ast_cli() to use the new threadstorage API
- Modify manager_event() to use thread storage. Instead of using a buffer of
4096 characters as the workspace for building the manager event, use a thread
local dynamic string. Now there is no length limitation on the length of the
body of a manager event.
- Significantly simplify the handling of ast_verbose() ...
- Instead of using a static char buffer and a lock to make sure only one
thread can be using ast_verbose() at a time, use a thread local dynamic
string as the workspace for preparing the verbose message. Instead of
locking around the entire function, the only locking done now is when the
message has been built and is being deliviered to the list of registered
verbose message handlers.
- This function was doing a strdup() on every message passed to it and
keeping a queue of the last 200 messages in memory. This has been
completely removed. The only place this was used was that if there were
any messages in the verbose queue when a verbose handler was registered,
all of the messages in the queue would be fed to it. So, I just made sure
that the console verbose handler and the network verbose handler (for
remote asterisk consoles) were registered before any verbose messages.
pbx_gtkconsole and pbx_kdeconsole will now lose a few verbose messages at
startup, but I didn't feel the performance hit of this message queue was
worth saving the initial verbose output for these very rarely used modules.
- I have removed the last three arguments to the verbose handlers, leaving
only the string itself because they aren't needed anymore. For example,
ast_verbose had some logic for telling the verbose handler to add
a newline if the buffer was completely full. Now that the buffer can grow
as needed, this doesn't matter anymore.
- remove unused function, ast_verbose_dmesg() which was to dispatch the
message queue
- Convert the list of verbose handlers to use the linked list macros.
- add missing newline characters to a few ast_verbose() calls
- convert the list of log channels to use the linked list macros in logger.c
- fix close_logger() to close all of the files it opened for logging
- update ast_log() to use a thread local dynamic string for its workspace
for preparing log messages instead of a buffer of size BUFSIZ (8kB on my
system) allocated on the stack. The dynamic string in this case is limited
to only growing to a maximum size of BUFSIZ.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@39272 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-08-08 06:32:04 +00:00
|
|
|
res = vsnprintf((*buf)->str + offset, (*buf)->len - offset, fmt, ap);
|
|
|
|
|
2006-11-28 14:07:09 +00:00
|
|
|
need = res + offset + 1;
|
2006-12-15 22:08:46 +00:00
|
|
|
/*
|
|
|
|
* If there is not enough space and we are below the max length,
|
|
|
|
* reallocate the buffer and return a message telling to retry.
|
|
|
|
*/
|
2006-11-28 14:07:09 +00:00
|
|
|
if (need > (*buf)->len && (max_len == 0 || (*buf)->len < max_len) ) {
|
2006-12-15 22:08:46 +00:00
|
|
|
if (max_len && max_len < need) /* truncate as needed */
|
2006-11-28 14:07:09 +00:00
|
|
|
need = max_len;
|
2006-12-16 11:23:07 +00:00
|
|
|
else if (max_len == 0) /* if unbounded, give more room for next time */
|
|
|
|
need += 16 + need/4;
|
|
|
|
if (0) /* debugging */
|
|
|
|
ast_verbose("extend from %d to %d\n", (int)(*buf)->len, need);
|
|
|
|
if (ast_str_make_space(buf, need)) {
|
|
|
|
ast_verbose("failed to extend from %d to %d\n", (int)(*buf)->len, need);
|
Merge team/russell/ast_verbose_threadstorage
- instead of defining a free() wrapper in a bunch of files, define it as
ast_free() in utils.h and remove the copies from all the files.
- centralize and abstract the code used for doing thread storage. The code
lives in threadstorage.h, with one function being implemented in utils.c.
This new API includes generic thread storage as well as special functions
for handling thread local dynamic length string buffers.
- update ast_inet_ntoa() to use the new threadstorage API
- update ast_state2str() to use the new threadstorage API
- update ast_cli() to use the new threadstorage API
- Modify manager_event() to use thread storage. Instead of using a buffer of
4096 characters as the workspace for building the manager event, use a thread
local dynamic string. Now there is no length limitation on the length of the
body of a manager event.
- Significantly simplify the handling of ast_verbose() ...
- Instead of using a static char buffer and a lock to make sure only one
thread can be using ast_verbose() at a time, use a thread local dynamic
string as the workspace for preparing the verbose message. Instead of
locking around the entire function, the only locking done now is when the
message has been built and is being deliviered to the list of registered
verbose message handlers.
- This function was doing a strdup() on every message passed to it and
keeping a queue of the last 200 messages in memory. This has been
completely removed. The only place this was used was that if there were
any messages in the verbose queue when a verbose handler was registered,
all of the messages in the queue would be fed to it. So, I just made sure
that the console verbose handler and the network verbose handler (for
remote asterisk consoles) were registered before any verbose messages.
pbx_gtkconsole and pbx_kdeconsole will now lose a few verbose messages at
startup, but I didn't feel the performance hit of this message queue was
worth saving the initial verbose output for these very rarely used modules.
- I have removed the last three arguments to the verbose handlers, leaving
only the string itself because they aren't needed anymore. For example,
ast_verbose had some logic for telling the verbose handler to add
a newline if the buffer was completely full. Now that the buffer can grow
as needed, this doesn't matter anymore.
- remove unused function, ast_verbose_dmesg() which was to dispatch the
message queue
- Convert the list of verbose handlers to use the linked list macros.
- add missing newline characters to a few ast_verbose() calls
- convert the list of log channels to use the linked list macros in logger.c
- fix close_logger() to close all of the files it opened for logging
- update ast_log() to use a thread local dynamic string for its workspace
for preparing log messages instead of a buffer of size BUFSIZ (8kB on my
system) allocated on the stack. The dynamic string in this case is limited
to only growing to a maximum size of BUFSIZ.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@39272 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-08-08 06:32:04 +00:00
|
|
|
return AST_DYNSTR_BUILD_FAILED;
|
2006-12-16 11:23:07 +00:00
|
|
|
}
|
2006-12-15 22:08:46 +00:00
|
|
|
(*buf)->str[offset] = '\0'; /* Truncate the partial write. */
|
2006-09-03 22:56:01 +00:00
|
|
|
|
Merge team/russell/ast_verbose_threadstorage
- instead of defining a free() wrapper in a bunch of files, define it as
ast_free() in utils.h and remove the copies from all the files.
- centralize and abstract the code used for doing thread storage. The code
lives in threadstorage.h, with one function being implemented in utils.c.
This new API includes generic thread storage as well as special functions
for handling thread local dynamic length string buffers.
- update ast_inet_ntoa() to use the new threadstorage API
- update ast_state2str() to use the new threadstorage API
- update ast_cli() to use the new threadstorage API
- Modify manager_event() to use thread storage. Instead of using a buffer of
4096 characters as the workspace for building the manager event, use a thread
local dynamic string. Now there is no length limitation on the length of the
body of a manager event.
- Significantly simplify the handling of ast_verbose() ...
- Instead of using a static char buffer and a lock to make sure only one
thread can be using ast_verbose() at a time, use a thread local dynamic
string as the workspace for preparing the verbose message. Instead of
locking around the entire function, the only locking done now is when the
message has been built and is being deliviered to the list of registered
verbose message handlers.
- This function was doing a strdup() on every message passed to it and
keeping a queue of the last 200 messages in memory. This has been
completely removed. The only place this was used was that if there were
any messages in the verbose queue when a verbose handler was registered,
all of the messages in the queue would be fed to it. So, I just made sure
that the console verbose handler and the network verbose handler (for
remote asterisk consoles) were registered before any verbose messages.
pbx_gtkconsole and pbx_kdeconsole will now lose a few verbose messages at
startup, but I didn't feel the performance hit of this message queue was
worth saving the initial verbose output for these very rarely used modules.
- I have removed the last three arguments to the verbose handlers, leaving
only the string itself because they aren't needed anymore. For example,
ast_verbose had some logic for telling the verbose handler to add
a newline if the buffer was completely full. Now that the buffer can grow
as needed, this doesn't matter anymore.
- remove unused function, ast_verbose_dmesg() which was to dispatch the
message queue
- Convert the list of verbose handlers to use the linked list macros.
- add missing newline characters to a few ast_verbose() calls
- convert the list of log channels to use the linked list macros in logger.c
- fix close_logger() to close all of the files it opened for logging
- update ast_log() to use a thread local dynamic string for its workspace
for preparing log messages instead of a buffer of size BUFSIZ (8kB on my
system) allocated on the stack. The dynamic string in this case is limited
to only growing to a maximum size of BUFSIZ.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@39272 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-08-08 06:32:04 +00:00
|
|
|
/* va_end() and va_start() must be done before calling
|
|
|
|
* vsnprintf() again. */
|
|
|
|
return AST_DYNSTR_BUILD_RETRY;
|
|
|
|
}
|
2006-12-15 22:08:46 +00:00
|
|
|
/* update space used, keep in mind the truncation */
|
|
|
|
(*buf)->used = (res + offset > (*buf)->len) ? (*buf)->len : res + offset;
|
Merge team/russell/ast_verbose_threadstorage
- instead of defining a free() wrapper in a bunch of files, define it as
ast_free() in utils.h and remove the copies from all the files.
- centralize and abstract the code used for doing thread storage. The code
lives in threadstorage.h, with one function being implemented in utils.c.
This new API includes generic thread storage as well as special functions
for handling thread local dynamic length string buffers.
- update ast_inet_ntoa() to use the new threadstorage API
- update ast_state2str() to use the new threadstorage API
- update ast_cli() to use the new threadstorage API
- Modify manager_event() to use thread storage. Instead of using a buffer of
4096 characters as the workspace for building the manager event, use a thread
local dynamic string. Now there is no length limitation on the length of the
body of a manager event.
- Significantly simplify the handling of ast_verbose() ...
- Instead of using a static char buffer and a lock to make sure only one
thread can be using ast_verbose() at a time, use a thread local dynamic
string as the workspace for preparing the verbose message. Instead of
locking around the entire function, the only locking done now is when the
message has been built and is being deliviered to the list of registered
verbose message handlers.
- This function was doing a strdup() on every message passed to it and
keeping a queue of the last 200 messages in memory. This has been
completely removed. The only place this was used was that if there were
any messages in the verbose queue when a verbose handler was registered,
all of the messages in the queue would be fed to it. So, I just made sure
that the console verbose handler and the network verbose handler (for
remote asterisk consoles) were registered before any verbose messages.
pbx_gtkconsole and pbx_kdeconsole will now lose a few verbose messages at
startup, but I didn't feel the performance hit of this message queue was
worth saving the initial verbose output for these very rarely used modules.
- I have removed the last three arguments to the verbose handlers, leaving
only the string itself because they aren't needed anymore. For example,
ast_verbose had some logic for telling the verbose handler to add
a newline if the buffer was completely full. Now that the buffer can grow
as needed, this doesn't matter anymore.
- remove unused function, ast_verbose_dmesg() which was to dispatch the
message queue
- Convert the list of verbose handlers to use the linked list macros.
- add missing newline characters to a few ast_verbose() calls
- convert the list of log channels to use the linked list macros in logger.c
- fix close_logger() to close all of the files it opened for logging
- update ast_log() to use a thread local dynamic string for its workspace
for preparing log messages instead of a buffer of size BUFSIZ (8kB on my
system) allocated on the stack. The dynamic string in this case is limited
to only growing to a maximum size of BUFSIZ.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@39272 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-08-08 06:32:04 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2006-10-12 18:43:52 +00:00
|
|
|
|
|
|
|
void ast_enable_packet_fragmentation(int sock)
|
|
|
|
{
|
2006-10-13 15:47:42 +00:00
|
|
|
#if defined(HAVE_IP_MTU_DISCOVER)
|
2006-10-12 18:43:52 +00:00
|
|
|
int val = IP_PMTUDISC_DONT;
|
|
|
|
|
|
|
|
if (setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)))
|
|
|
|
ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n");
|
2006-10-13 15:47:42 +00:00
|
|
|
#endif /* HAVE_IP_MTU_DISCOVER */
|
2006-10-12 18:43:52 +00:00
|
|
|
}
|
2007-06-22 04:35:12 +00:00
|
|
|
|
|
|
|
int ast_mkdir(const char *path, int mode)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
int len = strlen(path), count = 0, x, piececount = 0;
|
|
|
|
char *tmp = ast_strdupa(path);
|
|
|
|
char **pieces;
|
|
|
|
char *fullpath = alloca(len + 1);
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
for (ptr = tmp; *ptr; ptr++) {
|
|
|
|
if (*ptr == '/')
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Count the components to the directory path */
|
|
|
|
pieces = alloca(count * sizeof(*pieces));
|
|
|
|
for (ptr = tmp; *ptr; ptr++) {
|
|
|
|
if (*ptr == '/') {
|
|
|
|
*ptr = '\0';
|
|
|
|
pieces[piececount++] = ptr + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*fullpath = '\0';
|
|
|
|
for (x = 0; x < piececount; x++) {
|
|
|
|
/* This looks funky, but the buffer is always ideally-sized, so it's fine. */
|
|
|
|
strcat(fullpath, "/");
|
|
|
|
strcat(fullpath, pieces[x]);
|
|
|
|
res = mkdir(fullpath, mode);
|
|
|
|
if (res && errno != EEXIST)
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|