diff options
author | Russell Bryant <russell@russellbryant.com> | 2006-07-21 17:31:28 +0000 |
---|---|---|
committer | Russell Bryant <russell@russellbryant.com> | 2006-07-21 17:31:28 +0000 |
commit | ca9ba719b6f4ef598bc3dc71bdee05d11d0c7687 (patch) | |
tree | 9a03027276c525f44c08eb5dc4d7b9035fa697c9 /utils.c | |
parent | 1861dcdff5666cbac27288f7bdfdf40104d29d4d (diff) |
Merge a new implementation of ast_inet_ntoa, our thread safe replacement for
inet_ntoa, which uses thread specific data (aka thread local storage) instead
of stack allocatted buffers to store the result.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@38042 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'utils.c')
-rw-r--r-- | utils.c | 21 |
1 files changed, 19 insertions, 2 deletions
@@ -62,6 +62,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") static char base64[64]; static char b2a[256]; +static pthread_key_t inet_ntoa_buf_key; +static pthread_once_t inet_ntoa_buf_once = PTHREAD_ONCE_INIT; + #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) || defined(__CYGWIN__) #define ERANGE 34 /*!< duh? ERANGE value copied from web... */ @@ -483,10 +486,24 @@ void ast_uri_decode(char *s) *o = '\0'; } +static void inet_ntoa_buf_key_create(void) +{ + pthread_key_create(&inet_ntoa_buf_key, free); +} + /*! \brief ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa */ -const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia) +const char *ast_inet_ntoa(struct in_addr ia) { - return inet_ntop(AF_INET, &ia, buf, bufsiz); + char *buf; + + pthread_once(&inet_ntoa_buf_once, inet_ntoa_buf_key_create); + if (!(buf = pthread_getspecific(inet_ntoa_buf_key))) { + if (!(buf = ast_calloc(1, INET_ADDRSTRLEN))) + return NULL; + pthread_setspecific(inet_ntoa_buf_key, buf); + } + + return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN); } int ast_utils_init(void) |