[svn-commits] rizzo: branch rizzo/astobj2 r48518 - in /team/rizzo/astobj2: channels/ funcs/...

svn-commits at lists.digium.com svn-commits at lists.digium.com
Sat Dec 16 03:05:11 MST 2006


Author: rizzo
Date: Sat Dec 16 04:05:11 2006
New Revision: 48518

URL: http://svn.digium.com/view/asterisk?view=rev&rev=48518
Log:
update to trunk.
the most noticeable change is the addition of
ast_str*() support to replace ast_build_string


Modified:
    team/rizzo/astobj2/channels/chan_iax2.c
    team/rizzo/astobj2/funcs/func_curl.c
    team/rizzo/astobj2/include/asterisk/http.h
    team/rizzo/astobj2/include/asterisk/strings.h
    team/rizzo/astobj2/include/asterisk/threadstorage.h
    team/rizzo/astobj2/include/asterisk/utils.h
    team/rizzo/astobj2/main/cli.c
    team/rizzo/astobj2/main/http.c
    team/rizzo/astobj2/main/logger.c
    team/rizzo/astobj2/main/manager.c
    team/rizzo/astobj2/main/rtp.c
    team/rizzo/astobj2/main/translate.c
    team/rizzo/astobj2/main/utils.c
    team/rizzo/astobj2/res/res_limit.c
    team/rizzo/astobj2/res/res_smdi.c

Modified: team/rizzo/astobj2/channels/chan_iax2.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/channels/chan_iax2.c?view=diff&rev=48518&r1=48517&r2=48518
==============================================================================
--- team/rizzo/astobj2/channels/chan_iax2.c (original)
+++ team/rizzo/astobj2/channels/chan_iax2.c Sat Dec 16 04:05:11 2006
@@ -1788,11 +1788,14 @@
 			ast_queue_hangup(owner);
 		}
 
+		AST_LIST_LOCK(&iaxq.queue);
 		AST_LIST_TRAVERSE(&iaxq.queue, cur, list) {
 			/* Cancel any pending transmissions */
 			if (cur->callno == pvt->callno) 
 				cur->retries = -1;
 		}
+		AST_LIST_UNLOCK(&iaxq.queue);
+
 		if (pvt->reg)
 			pvt->reg->callno = 0;
 		if (!owner) {
@@ -2099,8 +2102,11 @@
 {
 	struct iax_frame *cur;
 	int cnt = 0, dead=0, final=0;
+
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
+
+	AST_LIST_LOCK(&iaxq.queue);
 	AST_LIST_TRAVERSE(&iaxq.queue, cur, list) {
 		if (cur->retries < 0)
 			dead++;
@@ -2108,6 +2114,8 @@
 			final++;
 		cnt++;
 	}
+	AST_LIST_UNLOCK(&iaxq.queue);
+
 	ast_cli(fd, "    IAX Statistics\n");
 	ast_cli(fd, "---------------------\n");
 	ast_cli(fd, "Outstanding frames: %d (%d ingress, %d egress)\n", iax_get_frames(), iax_get_iframes(), iax_get_oframes());
@@ -7744,7 +7752,9 @@
 	 * call has the pointer to IP and must be updated to the new one
 	 */
 	if (reg->dnsmgr && ast_dnsmgr_changed(reg->dnsmgr) && (reg->callno > 0)) {
+		ast_mutex_lock(&iaxsl[reg->callno]);
 		iax2_destroy(reg->callno);
+		ast_mutex_unlock(&iaxsl[reg->callno]);
 		reg->callno = 0;
 	}
 	if (!reg->addr.sin_addr.s_addr) {
@@ -7911,8 +7921,11 @@
 		manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: IAX2/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, peer->lastms);
 		ast_device_state_changed("IAX2/%s", peer->name); /* Activate notification */
 	}
-	if (peer->callno > 0)
+	if (peer->callno > 0) {
+		ast_mutex_lock(&iaxsl[peer->callno]);
 		iax2_destroy(peer->callno);
+		ast_mutex_unlock(&iaxsl[peer->callno]);
+	}
 	peer->callno = 0;
 	peer->lastms = -1;
 	/* Try again quickly */
@@ -7943,7 +7956,9 @@
 	}
 	if (peer->callno > 0) {
 		ast_log(LOG_NOTICE, "Still have a callno...\n");
+		ast_mutex_lock(&iaxsl[peer->callno]);
 		iax2_destroy(peer->callno);
+		ast_mutex_unlock(&iaxsl[peer->callno]);
 	}
 	if (heldcall)
 		ast_mutex_unlock(&iaxsl[heldcall]);
@@ -8778,8 +8793,11 @@
 		ast_sched_del(sched, peer->expire);
 	if (peer->pokeexpire > -1)
 		ast_sched_del(sched, peer->pokeexpire);
-	if (peer->callno > 0)
+	if (peer->callno > 0) {
+		ast_mutex_lock(&iaxsl[peer->callno]);
 		iax2_destroy(peer->callno);
+		ast_mutex_unlock(&iaxsl[peer->callno]);
+	}
 
 	register_peer_exten(peer, 0);
 

Modified: team/rizzo/astobj2/funcs/func_curl.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/funcs/func_curl.c?view=diff&rev=48518&r1=48517&r2=48518
==============================================================================
--- team/rizzo/astobj2/funcs/func_curl.c (original)
+++ team/rizzo/astobj2/funcs/func_curl.c Sat Dec 16 04:05:11 2006
@@ -51,12 +51,12 @@
 #include "asterisk/module.h"
 #include "asterisk/app.h"
 #include "asterisk/utils.h"
+#include "asterisk/threadstorage.h"
 
 struct MemoryStruct {
 	char *memory;
 	size_t size;
 };
-
 
 static void *myrealloc(void *ptr, size_t size)
 {
@@ -82,32 +82,46 @@
 	return realsize;
 }
 
+static const char *global_useragent = "asterisk-libcurl-agent/1.0";
+
+static void curl_instance_cleanup(void *data)
+{
+	CURL **curl = data;
+
+	curl_easy_cleanup(*curl);
+}
+
+AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup);
+
 static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
 {
-	CURL *curl;
-
-	curl = curl_easy_init();
-
-	if (!curl) {
+	CURL **curl;
+
+	if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl))))
 		return -1;
-	}
-
-	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);
-	curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
-	curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);
-	curl_easy_setopt(curl, CURLOPT_URL, url);
-	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
-	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
-	curl_easy_setopt(curl, CURLOPT_USERAGENT, "asterisk-libcurl-agent/1.0");
+
+	if (!*curl) {
+		if (!(*curl = curl_easy_init()))
+			return -1;
+		curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1);
+		curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180);
+		curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
+		curl_easy_setopt(*curl, CURLOPT_USERAGENT, global_useragent);
+	}
+
+	curl_easy_setopt(*curl, CURLOPT_URL, url);
+	curl_easy_setopt(*curl, CURLOPT_WRITEDATA, (void *) chunk);
 
 	if (post) {
-		curl_easy_setopt(curl, CURLOPT_POST, 1);
-		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
-	}
-
-	curl_easy_perform(curl);
-	curl_easy_cleanup(curl);
+		curl_easy_setopt(*curl, CURLOPT_POST, 1);
+		curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, post);
+	}
+
+	curl_easy_perform(*curl);
+
+	if (post)
+		curl_easy_setopt(*curl, CURLOPT_POST, 0);
+
 	return 0;
 }
 

Modified: team/rizzo/astobj2/include/asterisk/http.h
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/include/asterisk/http.h?view=diff&rev=48518&r1=48517&r2=48518
==============================================================================
--- team/rizzo/astobj2/include/asterisk/http.h (original)
+++ team/rizzo/astobj2/include/asterisk/http.h Sat Dec 16 04:05:11 2006
@@ -144,7 +144,7 @@
    The return value may include additional headers at the front and MUST include a blank 
    line with \r\n to provide separation between user headers and content (even if no
    content is specified) */
-typedef char *(*ast_http_callback)(struct sockaddr_in *requestor, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength);
+typedef struct ast_str *(*ast_http_callback)(struct sockaddr_in *requestor, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength);
 
 struct ast_http_uri {
 	struct ast_http_uri *next;
@@ -157,13 +157,11 @@
 /*! \brief Link into the Asterisk HTTP server */
 int ast_http_uri_link(struct ast_http_uri *urihandler);
 
-/*! \brief Return a malloc()'d string containing an HTTP error message */
-char *ast_http_error(int status, const char *title, const char *extra_header, const char *text);
+/*! \brief Return an ast_str malloc()'d string containing an HTTP error message */
+struct ast_str *ast_http_error(int status, const char *title, const char *extra_header, const char *text);
 
 /*! \brief Destroy an HTTP server */
 void ast_http_uri_unlink(struct ast_http_uri *urihandler);
-
-char *ast_http_setcookie(const char *var, const char *val, int expires, char *buf, size_t buflen);
 
 int ast_http_init(void);
 int ast_http_reload(void);

Modified: team/rizzo/astobj2/include/asterisk/strings.h
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/include/asterisk/strings.h?view=diff&rev=48518&r1=48517&r2=48518
==============================================================================
--- team/rizzo/astobj2/include/asterisk/strings.h (original)
+++ team/rizzo/astobj2/include/asterisk/strings.h Sat Dec 16 04:05:11 2006
@@ -29,6 +29,8 @@
 #include "asterisk/inline_api.h"
 #include "asterisk/compiler.h"
 #include "asterisk/compat.h"
+#include "asterisk/utils.h"
+#include "asterisk/threadstorage.h"
 
 static force_inline int ast_strlen_zero(const char *s)
 {
@@ -184,6 +186,20 @@
 */
 int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__ ((format (printf, 3, 4)));
 
+/*!
+  \brief Build a string in a buffer, designed to be called repeatedly
+  
+  This is a wrapper for snprintf, that properly handles the buffer pointer
+  and buffer space available.
+
+  \return 0 on success, non-zero on failure.
+  \param buffer current position in buffer to place string into (will be updated on return)
+  \param space remaining space in buffer (will be updated on return)
+  \param fmt printf-style format string
+  \param ap varargs list of arguments for format
+*/
+int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap);
+
 /*! Make sure something is true */
 /*!
  * Determine if a string containing a boolean value is "true".
@@ -242,4 +258,333 @@
 		(ra)->ptr; \
 	})
 
+/*!
+ * Support for dynamic strings.
+ *
+ * A dynamic string is just a C string prefixed by a few control fields
+ * that help setting/appending/extending it using a printf-like syntax.
+ *
+ * One should never declare a variable with this type, but only a pointer
+ * to it, e.g.
+ *
+ *	struct ast_str *ds;
+ *
+ * The pointer can be initialized with the following:
+ *
+ *	ds = ast_str_create(init_len);
+ *		creates a malloc()'ed dynamic string;
+ *
+ *	ds = ast_str_alloca(init_len);
+ *		creates a string on the stack (not very dynamic!).
+ *
+ *	ds = ast_str_thread_get(ts, init_len)
+ *		creates a malloc()'ed dynamic string associated to
+ *		the thread-local storage key ts
+ *
+ * Finally, the string can be manipulated with the following:
+ *
+ *	ast_str_set(&buf, max_len, ts, fmt, ...)
+ *	ast_str_append(&buf, max_len, ts, fmt, ...)
+ *
+ * and their varargs format.
+ *
+ * \arg max_len The maximum allowed length, reallocating if needed.
+ * 	0 means unlimited, -1 means "at most the available space"
+ */
+
+/*! \brief The descriptor of a dynamic string
+ *  XXX storage will be optimized later if needed
+ * We use the ts field to indicate the type of storage.
+ * Three special constants indicate malloc, alloca() or static
+ * variables, all other values indicate a
+ * struct ast_threadstorage pointer.
+ */
+struct ast_str {
+	size_t len;	/*!< The current maximum length of the string */
+	size_t used;	/*!< Amount of space used */
+	struct ast_threadstorage *ts;	/*!< What kind of storage is this ? */
+#define DS_MALLOC	((struct ast_threadstorage *)1)
+#define DS_ALLOCA	((struct ast_threadstorage *)2)
+#define DS_STATIC	((struct ast_threadstorage *)3)	/* not supported yet */
+	char str[0];	/*!< The string buffer */
+};
+
+/*!
+ * \brief Create a malloc'ed dynamic length string
+ *
+ * \arg init_len This is the initial length of the string buffer
+ *
+ * \return This function returns a pointer to the dynamic string length.  The
+ *         result will be NULL in the case of a memory allocation error.
+ *
+ * \note The result of this function is dynamically allocated memory, and must
+ *       be free()'d after it is no longer needed.
+ */
+AST_INLINE_API(
+struct ast_str * attribute_malloc ast_str_create(size_t init_len),
+{
+	struct ast_str *buf;
+
+	if (!(buf = ast_calloc(1, sizeof(*buf) + init_len)))
+		return NULL;
+	
+	buf->len = init_len;
+	buf->used = 0;
+	buf->ts = DS_MALLOC;
+
+	return buf;
+}
+)
+
+/*!
+ * Make space in a new string (e.g. to read in data from a file)
+ */
+AST_INLINE_API(
+int ast_str_make_space(struct ast_str **buf, size_t new_len),
+{
+	if (new_len <= (*buf)->len) 
+		return 0;	/* success */
+	if ((*buf)->ts == DS_ALLOCA || (*buf)->ts == DS_STATIC)
+		return -1;	/* cannot extend */
+	*buf = ast_realloc(*buf, new_len + sizeof(struct ast_str));
+	if (*buf == NULL) /* XXX watch out, we leak memory here */
+		return -1;
+	if ((*buf)->ts != DS_MALLOC)
+		pthread_setspecific((*buf)->ts->key, *buf);
+
+        (*buf)->len = new_len;
+        return 0;
+}
+)
+
+#define ast_str_alloca(init_len)			\
+	({						\
+		struct ast_str *buf;			\
+		buf = alloca(sizeof(*buf) + init_len);	\
+		buf->len = init_len;			\
+		buf->used = 0;				\
+		buf->ts = DS_ALLOCA;			\
+		buf->str[0] = '\0';			\
+		(buf);					\
+	})
+
+
+/*!
+ * \brief Retrieve a thread locally stored dynamic string
+ *
+ * \arg ts This is a pointer to the thread storage structure declared by using
+ *      the AST_THREADSTORAGE macro.  If declared with 
+ *      AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be 
+ *      (&my_buf).
+ * \arg init_len This is the initial length of the thread's dynamic string. The
+ *      current length may be bigger if previous operations in this thread have
+ *      caused it to increase.
+ *
+ * \return This function will return the thread locally stored dynamic string
+ *         associated with the thread storage management variable passed as the
+ *         first argument.
+ *         The result will be NULL in the case of a memory allocation error.
+ *
+ * Example usage:
+ * \code
+ * AST_THREADSTORAGE(my_str, my_str_init);
+ * #define MY_STR_INIT_SIZE   128
+ * ...
+ * void my_func(const char *fmt, ...)
+ * {
+ *      struct ast_str *buf;
+ *
+ *      if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
+ *           return;
+ *      ...
+ * }
+ * \endcode
+ */
+AST_INLINE_API(
+struct ast_str *ast_str_thread_get(struct ast_threadstorage *ts,
+	size_t init_len),
+{
+	struct ast_str *buf;
+
+	if (!(buf = ast_threadstorage_get(ts, sizeof(*buf) + init_len)))
+		return NULL;
+	
+	if (!buf->len) {
+		buf->len = init_len;
+		buf->used = 0;
+		buf->ts = ts;
+	}
+
+	return buf;
+}
+)
+
+/*!
+ * \brief Error codes from __ast_str_helper()
+ * The undelying processing to manipulate dynamic string is done
+ * by __ast_str_helper(), which can return a success, a
+ * permanent failure (e.g. no memory), or a temporary one (when
+ * the string needs to be reallocated, and we must run va_start()
+ * again; XXX this convoluted interface is only here because
+ * FreeBSD 4 lacks va_copy, but this will be fixed and the
+ * interface simplified).
+ */
+enum {
+	/*! An error has occured and the contents of the dynamic string
+	 *  are undefined */
+	AST_DYNSTR_BUILD_FAILED = -1,
+	/*! The buffer size for the dynamic string had to be increased, and
+	 *  __ast_str_helper() needs to be called again after
+	 *  a va_end() and va_start().
+	 */
+	AST_DYNSTR_BUILD_RETRY = -2
+};
+
+/*!
+ * \brief Set a dynamic string from a va_list
+ *
+ * \arg buf This is the address of a pointer to a struct ast_str.
+ *	If it is retrieved using ast_str_thread_get, the
+	struct ast_threadstorage pointer will need to
+ *      be updated in the case that the buffer has to be reallocated to
+ *      accommodate a longer string than what it currently has space for.
+ * \arg max_len This is the maximum length to allow the string buffer to grow
+ *      to.  If this is set to 0, then there is no maximum length.
+ * \arg fmt This is the format string (printf style)
+ * \arg ap This is the va_list
+ *
+ * \return The return value of this function is the same as that of the printf
+ *         family of functions.
+ *
+ * Example usage (the first part is only for thread-local storage)
+ * \code
+ * AST_THREADSTORAGE(my_str, my_str_init);
+ * #define MY_STR_INIT_SIZE   128
+ * ...
+ * void my_func(const char *fmt, ...)
+ * {
+ *      struct ast_str *buf;
+ *      va_list ap;
+ *
+ *      if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
+ *           return;
+ *      ...
+ *      va_start(fmt, ap);
+ *      ast_str_set_va(&buf, 0, fmt, ap);
+ *      va_end(ap);
+ * 
+ *      printf("This is the string we just built: %s\n", buf->str);
+ *      ...
+ * }
+ * \endcode
+ *
+ * \note: the following two functions must be implemented as macros
+ *	because we must do va_end()/va_start() on the original arguments.
+ */
+#define ast_str_set_va(buf, max_len, fmt, ap)			\
+	({								\
+		int __res;						\
+		while ((__res = __ast_str_helper(buf, max_len,		\
+			0, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) {	\
+			va_end(ap);					\
+			va_start(ap, fmt);				\
+		}							\
+		(__res);						\
+	})
+
+/*!
+ * \brief Append to a dynamic string using a va_list
+ *
+ * Same as ast_str_set_va(), but append to the current content.
+ */
+#define ast_str_append_va(buf, max_len, fmt, ap)		\
+	({								\
+		int __res;						\
+		while ((__res = __ast_str_helper(buf, max_len,		\
+			1, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) {	\
+			va_end(ap);					\
+			va_start(ap, fmt);				\
+		}							\
+		(__res);						\
+	})
+
+/*!
+ * \brief Core functionality of ast_str_(set|append)_va
+ *
+ * The arguments to this function are the same as those described for
+ * ast_str_set_va except for an addition argument, append.
+ * If append is non-zero, this will append to the current string instead of
+ * writing over it.
+ *
+ * In the case that this function is called and the buffer was not large enough
+ * to hold the result, the partial write will be truncated, and the result
+ * AST_DYNSTR_BUILD_RETRY will be returned to indicate that the buffer size
+ * was increased, and the function should be called a second time.
+ *
+ * A return of AST_DYNSTR_BUILD_FAILED indicates a memory allocation error.
+ *
+ * A return value greater than or equal to zero indicates the number of
+ * characters that have been written, not including the terminating '\0'.
+ * In the append case, this only includes the number of characters appended.
+ *
+ * \note This function should never need to be called directly.  It should
+ *       through calling one of the other functions or macros defined in this
+ *       file.
+ */
+int __ast_str_helper(struct ast_str **buf, size_t max_len,
+	int append, const char *fmt, va_list ap);
+
+/*!
+ * \brief Set a dynamic string using variable arguments
+ *
+ * \arg buf This is the address of a pointer to a struct ast_str which should
+ *      have been retrieved using ast_str_thread_get.  It will need to
+ *      be updated in the case that the buffer has to be reallocated to
+ *      accomodate a longer string than what it currently has space for.
+ * \arg max_len This is the maximum length to allow the string buffer to grow
+ *      to.  If this is set to 0, then there is no maximum length.
+ *	If set to -1, we are bound to the current maximum length.
+ * \arg fmt This is the format string (printf style)
+ *
+ * \return The return value of this function is the same as that of the printf
+ *         family of functions.
+ *
+ * All the rest is the same as ast_str_set_va()
+ */
+AST_INLINE_API(
+int __attribute__ ((format (printf, 3, 4))) ast_str_set(
+	struct ast_str **buf, size_t max_len, const char *fmt, ...),
+{
+	int res;
+	va_list ap;
+
+	va_start(ap, fmt);
+	res = ast_str_set_va(buf, max_len, fmt, ap);
+	va_end(ap);
+
+	return res;
+}
+)
+
+/*!
+ * \brief Append to a thread local dynamic string
+ *
+ * The arguments, return values, and usage of this function are the same as
+ * ast_str_set(), but the new data is appended to the current value.
+ */
+AST_INLINE_API(
+int __attribute__ ((format (printf, 3, 4))) ast_str_append(
+	struct ast_str **buf, size_t max_len, const char *fmt, ...),
+{
+	int res;
+	va_list ap;
+
+	va_start(ap, fmt);
+	res = ast_str_append_va(buf, max_len, fmt, ap);
+	va_end(ap);
+
+	return res;
+}
+)
+
 #endif /* _ASTERISK_STRINGS_H */

Modified: team/rizzo/astobj2/include/asterisk/threadstorage.h
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/include/asterisk/threadstorage.h?view=diff&rev=48518&r1=48517&r2=48518
==============================================================================
--- team/rizzo/astobj2/include/asterisk/threadstorage.h (original)
+++ team/rizzo/astobj2/include/asterisk/threadstorage.h Sat Dec 16 04:05:11 2006
@@ -56,14 +56,10 @@
  * \brief data for a thread locally stored variable
  */
 struct ast_threadstorage {
-	/*! Ensure that the key is only initialized by one thread */
-	pthread_once_t once;
-	/*! The key used to retrieve this thread's data */
-	pthread_key_t key;
-	/*! The function that initializes the key */
-	void (*key_init)(void);
-	/*! Custom initialization function specific to the object */
-	int (*custom_init)(void *);
+	pthread_once_t once;	/*!< Ensure that the key is only initialized by one thread */
+	pthread_key_t key;	/*!< The key used to retrieve this thread's data */
+	void (*key_init)(void);	/*!< The function that initializes the key */
+	int (*custom_init)(void *); /*!< Custom initialization function specific to the object */
 };
 
 /*!
@@ -161,327 +157,4 @@
 
 void __ast_threadstorage_cleanup(void *);
 
-/*!
- * A dynamic length string. This is just a C string prefixed by a length
- * field. len reflects the actual space allocated, while the string is
- * NUL-terminated as a regular C string.
- * One should never declare a variable with this type, but only a pointer
- * to it, and use ast_dynamic_str_create() to initialize it.
- */
-struct ast_dynamic_str {
-	size_t len;	/*!< The current maximum length of the string */
-	char str[0];	/*!< The string buffer */
-};
-
-/*!
- * \brief Create a dynamic length string
- *
- * \arg init_len This is the initial length of the string buffer
- *
- * \return This function returns a pointer to the dynamic string length.  The
- *         result will be NULL in the case of a memory allocation error.
- *
- * \note The result of this function is dynamically allocated memory, and must
- *       be free()'d after it is no longer needed.
- */
-AST_INLINE_API(
-struct ast_dynamic_str * attribute_malloc ast_dynamic_str_create(size_t init_len),
-{
-	struct ast_dynamic_str *buf;
-
-	if (!(buf = ast_calloc(1, sizeof(*buf) + init_len)))
-		return NULL;
-	
-	buf->len = init_len;
-
-	return buf;
-}
-)
-
-/*!
- * \brief Retrieve a thread locally stored dynamic string
- *
- * \arg ts This is a pointer to the thread storage structure declared by using
- *      the AST_THREADSTORAGE macro.  If declared with 
- *      AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be 
- *      (&my_buf).
- * \arg init_len This is the initial length of the thread's dynamic string. The
- *      current length may be bigger if previous operations in this thread have
- *      caused it to increase.
- *
- * \return This function will return the thread locally stored dynamic string
- *         associated with the thread storage management variable passed as the
- *         first argument.
- *         The result will be NULL in the case of a memory allocation error.
- *
- * Example usage:
- * \code
- * AST_THREADSTORAGE(my_str, my_str_init);
- * #define MY_STR_INIT_SIZE   128
- * ...
- * void my_func(const char *fmt, ...)
- * {
- *      struct ast_dynamic_str *buf;
- *
- *      if (!(buf = ast_dynamic_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
- *           return;
- *      ...
- * }
- * \endcode
- */
-AST_INLINE_API(
-struct ast_dynamic_str *ast_dynamic_str_thread_get(struct ast_threadstorage *ts,
-	size_t init_len),
-{
-	struct ast_dynamic_str *buf;
-
-	if (!(buf = ast_threadstorage_get(ts, sizeof(*buf) + init_len)))
-		return NULL;
-	
-	if (!buf->len)
-		buf->len = init_len;
-
-	return buf;
-}
-)
-
-/*!
- * \brief Error codes from ast_dynamic_str_thread_build_va()
- */
-enum {
-	/*! An error has occured and the contents of the dynamic string
-	 *  are undefined */
-	AST_DYNSTR_BUILD_FAILED = -1,
-	/*! The buffer size for the dynamic string had to be increased, and
-	 *  ast_dynamic_str_thread_build_va() needs to be called again after
-	 *  a va_end() and va_start().
-	 */
-	AST_DYNSTR_BUILD_RETRY = -2
-};
-
-/*!
- * \brief Set a thread locally stored dynamic string from a va_list
- *
- * \arg buf This is the address of a pointer to an ast_dynamic_str which should
- *      have been retrieved using ast_dynamic_str_thread_get.  It will need to
- *      be updated in the case that the buffer has to be reallocated to
- *      accommodate a longer string than what it currently has space for.
- * \arg max_len This is the maximum length to allow the string buffer to grow
- *      to.  If this is set to 0, then there is no maximum length.
- * \arg ts This is a pointer to the thread storage structure declared by using
- *      the AST_THREADSTORAGE macro.  If declared with 
- *      AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be 
- *      (&my_buf).
- * \arg fmt This is the format string (printf style)
- * \arg ap This is the va_list
- *
- * \return The return value of this function is the same as that of the printf
- *         family of functions.
- *
- * Example usage:
- * \code
- * AST_THREADSTORAGE(my_str, my_str_init);
- * #define MY_STR_INIT_SIZE   128
- * ...
- * void my_func(const char *fmt, ...)
- * {
- *      struct ast_dynamic_str *buf;
- *      va_list ap;
- *
- *      if (!(buf = ast_dynamic_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
- *           return;
- *      ...
- *      va_start(fmt, ap);
- *      ast_dynamic_str_thread_set_va(&buf, 0, &my_str, fmt, ap);
- *      va_end(ap);
- * 
- *      printf("This is the string we just built: %s\n", buf->str);
- *      ...
- * }
- * \endcode
- */
-#define ast_dynamic_str_thread_set_va(buf, max_len, ts, fmt, ap)                 \
-	({                                                                       \
-		int __res;                                                       \
-		while ((__res = ast_dynamic_str_thread_build_va(buf, max_len,    \
-			ts, 0, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) {            \
-			va_end(ap);                                              \
-			va_start(ap, fmt);                                       \
-		}                                                                \
-		(__res);                                                         \
-	})
-
-/*!
- * \brief Append to a thread local dynamic string using a va_list
- *
- * The arguments, return values, and usage of this are the same as those for
- * ast_dynamic_str_thread_set_va().  However, instead of setting a new value
- * for the string, this will append to the current value.
- */
-#define ast_dynamic_str_thread_append_va(buf, max_len, ts, fmt, ap)              \
-	({                                                                       \
-		int __res;                                                       \
-		while ((__res = ast_dynamic_str_thread_build_va(buf, max_len,    \
-			ts, 1, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) {            \
-			va_end(ap);                                              \
-			va_start(ap, fmt);                                       \
-		}                                                                \
-		(__res);                                                         \
-	})
-
-/*!
- * \brief Core functionality of ast_dynamic_str_thread_(set|append)_va
- *
- * The arguments to this function are the same as those described for
- * ast_dynamic_str_thread_set_va except for an addition argument, append.
- * If append is non-zero, this will append to the current string instead of
- * writing over it.
- *
- * In the case that this function is called and the buffer was not large enough
- * to hold the result, the partial write will be truncated, and the result
- * AST_DYNSTR_BUILD_RETRY will be returned to indicate that the buffer size
- * was increased, and the function should be called a second time.
- *
- * A return of AST_DYNSTR_BUILD_FAILED indicates a memory allocation error.
- *
- * A return value greater than or equal to zero indicates the number of
- * characters that have been written, not including the terminating '\0'.
- * In the append case, this only includes the number of characters appended.
- *
- * \note This function should never need to be called directly.  It should
- *       through calling one of the other functions or macros defined in this
- *       file.
- */
-int ast_dynamic_str_thread_build_va(struct ast_dynamic_str **buf, size_t max_len,
-	struct ast_threadstorage *ts, int append, const char *fmt, va_list ap);
-
-/*!
- * \brief Set a thread locally stored dynamic string using variable arguments
- *
- * \arg buf This is the address of a pointer to an ast_dynamic_str which should
- *      have been retrieved using ast_dynamic_str_thread_get.  It will need to
- *      be updated in the case that the buffer has to be reallocated to
- *      accomodate a longer string than what it currently has space for.
- * \arg max_len This is the maximum length to allow the string buffer to grow
- *      to.  If this is set to 0, then there is no maximum length.
- * \arg ts This is a pointer to the thread storage structure declared by using
- *      the AST_THREADSTORAGE macro.  If declared with 
- *      AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be 
- *      (&my_buf).
- * \arg fmt This is the format string (printf style)
- *
- * \return The return value of this function is the same as that of the printf
- *         family of functions.
- *
- * Example usage:
- * \code
- * AST_THREADSTORAGE(my_str, my_str_init);
- * #define MY_STR_INIT_SIZE   128
- * ...
- * void my_func(int arg1, int arg2)
- * {
- *      struct ast_dynamic_str *buf;
- *      va_list ap;
- *
- *      if (!(buf = ast_dynamic_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
- *           return;
- *      ...
- *      ast_dynamic_str_thread_set(&buf, 0, &my_str, "arg1: %d  arg2: %d\n",
- *           arg1, arg2);
- * 
- *      printf("This is the string we just built: %s\n", buf->str);
- *      ...
- * }
- * \endcode
- */
-AST_INLINE_API(
-int __attribute__ ((format (printf, 4, 5))) ast_dynamic_str_thread_set(
-	struct ast_dynamic_str **buf, size_t max_len, 
-	struct ast_threadstorage *ts, const char *fmt, ...),
-{
-	int res;
-	va_list ap;
-
-	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_set_va(buf, max_len, ts, fmt, ap);
-	va_end(ap);
-
-	return res;
-}
-)
-
-/*!
- * \brief Append to a thread local dynamic string
- *
- * The arguments, return values, and usage of this function are the same as
- * ast_dynamic_str_thread_set().  However, instead of setting a new value for
- * the string, this function appends to the current value.
- */
-AST_INLINE_API(
-int __attribute__ ((format (printf, 4, 5))) ast_dynamic_str_thread_append(
-	struct ast_dynamic_str **buf, size_t max_len, 
-	struct ast_threadstorage *ts, const char *fmt, ...),
-{
-	int res;
-	va_list ap;
-
-	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_append_va(buf, max_len, ts, fmt, ap);
-	va_end(ap);
-
-	return res;
-}
-)
-
-/*!
- * \brief Set a dynamic string
- *
- * \arg buf This is the address of a pointer to an ast_dynamic_str.  It will
- *      need to be updated in the case that the buffer has to be reallocated to
- *      accommodate a longer string than what it currently has space for.
- * \arg max_len This is the maximum length to allow the string buffer to grow
- *      to.  If this is set to 0, then there is no maximum length.
- *
- * \return The return value of this function is the same as that of the printf
- *         family of functions.
- */
-AST_INLINE_API(
-int __attribute__ ((format (printf, 3, 4))) ast_dynamic_str_set(
-	struct ast_dynamic_str **buf, size_t max_len,
-	const char *fmt, ...),
-{
-	int res;
-	va_list ap;
-	
-	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_set_va(buf, max_len, NULL, fmt, ap);
-	va_end(ap);
-
-	return res;
-}
-)
-
-/*!
- * \brief Append to a dynamic string
- *
- * The arguments, return values, and usage of this function are the same as
- * ast_dynamic_str_set().  However, this function appends to the string instead
- * of setting a new value.
- */
-AST_INLINE_API(
-int __attribute__ ((format (printf, 3, 4))) ast_dynamic_str_append(
-	struct ast_dynamic_str **buf, size_t max_len,
-	const char *fmt, ...),
-{
-	int res;
-	va_list ap;
-	
-	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_append_va(buf, max_len, NULL, fmt, ap);
-	va_end(ap);
-
-	return res;
-}
-)
-
 #endif /* ASTERISK_THREADSTORAGE_H */

Modified: team/rizzo/astobj2/include/asterisk/utils.h
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/include/asterisk/utils.h?view=diff&rev=48518&r1=48517&r2=48518
==============================================================================
--- team/rizzo/astobj2/include/asterisk/utils.h (original)
+++ team/rizzo/astobj2/include/asterisk/utils.h Sat Dec 16 04:05:11 2006
@@ -32,10 +32,10 @@
 #include <arpa/inet.h>	/* we want to override inet_ntoa */
 #include <netdb.h>
 #include <limits.h>
+#include <string.h>
 
 #include "asterisk/lock.h"
 #include "asterisk/time.h"
-#include "asterisk/strings.h"
 #include "asterisk/logger.h"
 #include "asterisk/compiler.h"
 
@@ -540,4 +540,5 @@
  */
 void ast_enable_packet_fragmentation(int sock);
 
+#include "asterisk/strings.h"
 #endif /* _ASTERISK_UTILS_H */

Modified: team/rizzo/astobj2/main/cli.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/main/cli.c?view=diff&rev=48518&r1=48517&r2=48518
==============================================================================
--- team/rizzo/astobj2/main/cli.c (original)
+++ team/rizzo/astobj2/main/cli.c Sat Dec 16 04:05:11 2006
@@ -59,14 +59,14 @@
 void ast_cli(int fd, char *fmt, ...)
 {
 	int res;
-	struct ast_dynamic_str *buf;
+	struct ast_str *buf;
 	va_list ap;
 
-	if (!(buf = ast_dynamic_str_thread_get(&ast_cli_buf, AST_CLI_INITLEN)))
+	if (!(buf = ast_str_thread_get(&ast_cli_buf, AST_CLI_INITLEN)))
 		return;
 
 	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_set_va(&buf, 0, &ast_cli_buf, fmt, ap);
+	res = ast_str_set_va(&buf, 0, fmt, ap);
 	va_end(ap);
 
 	if (res != AST_DYNSTR_BUILD_FAILED)

Modified: team/rizzo/astobj2/main/http.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/main/http.c?view=diff&rev=48518&r1=48517&r2=48518
==============================================================================
--- team/rizzo/astobj2/main/http.c (original)
+++ team/rizzo/astobj2/main/http.c Sat Dec 16 04:05:11 2006
@@ -148,17 +148,15 @@
 	}
 	return buf;
 }
-static char *static_callback(struct sockaddr_in *req, const char *uri, struct ast_variable *vars, int *status, char **title, int *contentlength)
-{
-	char result[4096];
-	char *c=result;
+static struct ast_str *static_callback(struct sockaddr_in *req, const char *uri, struct ast_variable *vars, int *status, char **title, int *contentlength)
+{
+	struct ast_str *result;
 	char *path;
 	char *ftype, *mtype;
 	char wkspace[80];
 	struct stat st;
 	int len;
 	int fd;
-	void *blob;
 
 	/* Yuck.  I'm not really sold on this, but if you don't deliver static content it makes your configuration 
 	   substantially more challenging, but this seems like a rather irritating feature creep on Asterisk. */
@@ -188,23 +186,22 @@
 	fd = open(path, O_RDONLY);
 	if (fd < 0)
 		goto out403;
-	
+
 	len = st.st_size + strlen(mtype) + 40;
-	
-	blob = malloc(len);
-	if (blob) {
-		c = blob;
-		sprintf(c, "Content-type: %s\r\n\r\n", mtype);
-		c += strlen(c);
-		*contentlength = read(fd, c, st.st_size);
-		if (*contentlength < 0) {
-			close(fd);
-			free(blob);
-			goto out403;
-		}
-	}
+	result = ast_str_create(len);
+	if (result == NULL)	/* XXX not really but... */
+		goto out403;
+
+	ast_str_append(&result, 0, "Content-type: %s\r\n\r\n", mtype);
+	*contentlength = read(fd, result->str + result->used, st.st_size);
+	if (*contentlength < 0) {
+		close(fd);
+		free(result);
+		goto out403;
+	}
+	result->used += *contentlength;
 	close(fd);
-	return blob;
+	return result;
 
 out404:
 	*status = 404;
@@ -218,44 +215,41 @@
 }
 
 
-static char *httpstatus_callback(struct sockaddr_in *req, const char *uri, struct ast_variable *vars, int *status, char **title, int *contentlength)
-{
-	char result[4096];
-	size_t reslen = sizeof(result);
-	char *c=result;
+static struct ast_str *httpstatus_callback(struct sockaddr_in *req, const char *uri, struct ast_variable *vars, int *status, char **title, int *contentlength)
+{
+	struct ast_str *out = ast_str_create(512);
 	struct ast_variable *v;
 
-	ast_build_string(&c, &reslen,
+	if (out == NULL)
+		return out;
+
+	ast_str_append(&out, 0,
 		"\r\n"
 		"<title>Asterisk HTTP Status</title>\r\n"
 		"<body bgcolor=\"#ffffff\">\r\n"
 		"<table bgcolor=\"#f1f1f1\" align=\"center\"><tr><td bgcolor=\"#e0e0ff\" colspan=\"2\" width=\"500\">\r\n"
 		"<h2>&nbsp;&nbsp;Asterisk&trade; HTTP Status</h2></td></tr>\r\n");
 
-	ast_build_string(&c, &reslen, "<tr><td><i>Prefix</i></td><td><b>%s</b></td></tr>\r\n", prefix);
-	ast_build_string(&c, &reslen, "<tr><td><i>Bind Address</i></td><td><b>%s</b></td></tr>\r\n",
+	ast_str_append(&out, 0, "<tr><td><i>Prefix</i></td><td><b>%s</b></td></tr>\r\n", prefix);
+	ast_str_append(&out, 0, "<tr><td><i>Bind Address</i></td><td><b>%s</b></td></tr>\r\n",
 			ast_inet_ntoa(http_desc.oldsin.sin_addr));
-	ast_build_string(&c, &reslen, "<tr><td><i>Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
+	ast_str_append(&out, 0, "<tr><td><i>Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
 			ntohs(http_desc.oldsin.sin_port));
 	if (http_tls_cfg.enabled)
-		ast_build_string(&c, &reslen, "<tr><td><i>SSL Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
+		ast_str_append(&out, 0, "<tr><td><i>SSL Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
 			ntohs(https_desc.oldsin.sin_port));
-	ast_build_string(&c, &reslen, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
-	v = vars;
-	while(v) {
+	ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
+	for (v = vars; v; v = v->next) {
 		if (strncasecmp(v->name, "cookie_", 7))
-			ast_build_string(&c, &reslen, "<tr><td><i>Submitted Variable '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
-		v = v->next;
-	}
-	ast_build_string(&c, &reslen, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
-	v = vars;
-	while(v) {
+			ast_str_append(&out, 0, "<tr><td><i>Submitted Variable '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
+	}
+	ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
+	for (v = vars; v; v = v->next) {
 		if (!strncasecmp(v->name, "cookie_", 7))
-			ast_build_string(&c, &reslen, "<tr><td><i>Cookie '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
-		v = v->next;
-	}
-	ast_build_string(&c, &reslen, "</table><center><font size=\"-1\"><i>Asterisk and Digium are registered trademarks of Digium, Inc.</i></font></center></body>\r\n");
-	return strdup(result);
+			ast_str_append(&out, 0, "<tr><td><i>Cookie '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
+	}
+	ast_str_append(&out, 0, "</table><center><font size=\"-1\"><i>Asterisk and Digium are registered trademarks of Digium, Inc.</i></font></center></body>\r\n");
+	return out;
 }
 
 static struct ast_http_uri statusuri = {
@@ -272,10 +266,12 @@
 	.has_subtree = 1,
 };
 	
-char *ast_http_error(int status, const char *title, const char *extra_header, const char *text)
-{
-	char *c = NULL;
-	asprintf(&c,
+struct ast_str *ast_http_error(int status, const char *title, const char *extra_header, const char *text)
+{
+	struct ast_str *out = ast_str_create(512);
+	if (out == NULL)
+		return out;
+	ast_str_set(&out, 0,
 		"Content-type: text/html\r\n"
 		"%s"
 		"\r\n"
@@ -289,7 +285,7 @@
 		"<address>Asterisk Server</address>\r\n"
 		"</body></html>\r\n",
 			(extra_header ? extra_header : ""), status, title, title, text);
-	return c;
+	return out;
 }
 
 /*! \brief 
@@ -334,9 +330,10 @@
 	}
 }
 
-static char *handle_uri(struct sockaddr_in *sin, char *uri, int *status, char **title, int *contentlength, struct ast_variable **cookies)
+static struct ast_str *handle_uri(struct sockaddr_in *sin, char *uri, int *status, char **title, int *contentlength, struct ast_variable **cookies)
 {
 	char *c;
+	struct ast_str *out = NULL;
 	char *params = uri;
 	struct ast_http_uri *urih=NULL;
 	int l;

[... 1102 lines stripped ...]


More information about the svn-commits mailing list