Merged revisions 56740 via svnmerge from
[asterisk/asterisk.git] / channels / iax2-parser.c
old mode 100755 (executable)
new mode 100644 (file)
index 61038c3..5e76dd8
@@ -1,36 +1,67 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
- * Implementation of Inter-Asterisk eXchange
- * 
- * Copyright (C) 2003 - 2005, Digium, Inc.
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
+ * 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.
+ *
  * This program is free software, distributed under the terms of
- * the GNU General Public License
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Implementation of Inter-Asterisk eXchange Protocol, v 2
+ *
+ * \author Mark Spencer <markster@digium.com> 
  */
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <string.h>
 #include <netinet/in.h>
-#include <asterisk/frame.h>
-#include <asterisk/utils.h>
-#include <asterisk/unaligned.h>
 #include <arpa/inet.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
+
+#include "asterisk/frame.h"
+#include "asterisk/utils.h"
+#include "asterisk/unaligned.h"
+#include "asterisk/config.h"
+#include "asterisk/lock.h"
+#include "asterisk/threadstorage.h"
+
 #include "iax2.h"
 #include "iax2-parser.h"
 #include "iax2-provision.h"
 
-
 static int frames = 0;
 static int iframes = 0;
 static int oframes = 0;
 
+#if !defined(LOW_MEMORY)
+static void frame_cache_cleanup(void *data);
+
+/*! \brief A per-thread cache of iax_frame structures */
+AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
+
+/*! \brief This is just so iax_frames, a list head struct for holding a list of
+ *  iax_frame structures, is defined. */
+AST_LIST_HEAD_NOLOCK(iax_frames, iax_frame);
+#endif
+
 static void internaloutput(const char *str)
 {
        fputs(str, stdout);
@@ -47,10 +78,9 @@ static void (*errorf)(const char *str) = internalerror;
 static void dump_addr(char *output, int maxlen, void *value, int len)
 {
        struct sockaddr_in sin;
-       char iabuf[INET_ADDRSTRLEN];
        if (len == (int)sizeof(sin)) {
                memcpy(&sin, value, len);
-               snprintf(output, maxlen, "IPV4 %s:%d", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port));
+               snprintf(output, maxlen, "IPV4 %s:%d", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
        } else {
                snprintf(output, maxlen, "Invalid Address");
        }
@@ -61,7 +91,7 @@ static void dump_string(char *output, int maxlen, void *value, int len)
        maxlen--;
        if (maxlen > len)
                maxlen = len;
-       strncpy(output,value, maxlen);
+       strncpy(output, value, maxlen);
        output[maxlen] = '\0';
 }
 
@@ -76,7 +106,7 @@ static void dump_prefs(char *output, int maxlen, void *value, int len)
        if (maxlen > len)
                maxlen = len;
 
-       strncpy(output,value, maxlen);
+       strncpy(output, value, maxlen);
        output[maxlen] = '\0';
        
        ast_codec_pref_convert(&pref, output, total_len, 0);
@@ -89,7 +119,7 @@ static void dump_int(char *output, int maxlen, void *value, int len)
        if (len == (int)sizeof(unsigned int))
                snprintf(output, maxlen, "%lu", (unsigned long)ntohl(get_unaligned_uint32(value)));
        else
-               snprintf(output, maxlen, "Invalid INT");
+               ast_copy_string(output, "Invalid INT", maxlen); 
 }
 
 static void dump_short(char *output, int maxlen, void *value, int len)
@@ -97,7 +127,7 @@ static void dump_short(char *output, int maxlen, void *value, int len)
        if (len == (int)sizeof(unsigned short))
                snprintf(output, maxlen, "%d", ntohs(get_unaligned_uint16(value)));
        else
-               snprintf(output, maxlen, "Invalid SHORT");
+               ast_copy_string(output, "Invalid SHORT", maxlen);
 }
 
 static void dump_byte(char *output, int maxlen, void *value, int len)
@@ -105,19 +135,33 @@ static void dump_byte(char *output, int maxlen, void *value, int len)
        if (len == (int)sizeof(unsigned char))
                snprintf(output, maxlen, "%d", *((unsigned char *)value));
        else
-               snprintf(output, maxlen, "Invalid BYTE");
+               ast_copy_string(output, "Invalid BYTE", maxlen);
+}
+
+static void dump_datetime(char *output, int maxlen, void *value, int len)
+{
+       struct tm tm;
+       unsigned long val = (unsigned long) ntohl(get_unaligned_uint32(value));
+       if (len == (int)sizeof(unsigned int)) {
+               tm.tm_sec  = (val & 0x1f) << 1;
+               tm.tm_min  = (val >> 5) & 0x3f;
+               tm.tm_hour = (val >> 11) & 0x1f;
+               tm.tm_mday = (val >> 16) & 0x1f;
+               tm.tm_mon  = ((val >> 21) & 0x0f) - 1;
+               tm.tm_year = ((val >> 25) & 0x7f) + 100;
+               strftime(output, maxlen, "%Y-%m-%d  %T", &tm); 
+       } else
+               ast_copy_string(output, "Invalid DATETIME format!", maxlen);
 }
 
 static void dump_ipaddr(char *output, int maxlen, void *value, int len)
 {
        struct sockaddr_in sin;
-       char iabuf[INET_ADDRSTRLEN];
        if (len == (int)sizeof(unsigned int)) {
                memcpy(&sin.sin_addr, value, len);
-               ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr);
-               snprintf(output, maxlen, "%s", iabuf);
+               snprintf(output, maxlen, "%s", ast_inet_ntoa(sin.sin_addr));
        } else
-               snprintf(output, maxlen, "Invalid IPADDR");
+               ast_copy_string(output, "Invalid IPADDR", maxlen);
 }
 
 
@@ -128,7 +172,7 @@ static void dump_prov_flags(char *output, int maxlen, void *value, int len)
                snprintf(output, maxlen, "%lu (%s)", (unsigned long)ntohl(get_unaligned_uint32(value)),
                        iax_provflags2str(buf, sizeof(buf), ntohl(get_unaligned_uint32(value))));
        else
-               snprintf(output, maxlen, "Invalid INT");
+               ast_copy_string(output, "Invalid INT", maxlen);
 }
 
 static void dump_samprate(char *output, int maxlen, void *value, int len)
@@ -150,11 +194,11 @@ static void dump_samprate(char *output, int maxlen, void *value, int len)
                if (sr & IAX_RATE_48KHZ)
                        strcat(tmp, ",48khz");
                if (strlen(tmp))
-                       strncpy(output, &tmp[1], maxlen - 1);
+                       ast_copy_string(output, &tmp[1], maxlen);
                else
-                       strncpy(output, "None specified!\n", maxlen - 1);
+                       ast_copy_string(output, "None Specified!\n", maxlen);
        } else
-               snprintf(output, maxlen, "Invalid SHORT");
+               ast_copy_string(output, "Invalid SHORT", maxlen);
 
 }
 
@@ -198,7 +242,7 @@ static struct iax2_ie {
        { IAX_IE_RDNIS, "REFERRING DNIS", dump_string },
        { IAX_IE_PROVISIONING, "PROVISIONING", dump_prov },
        { IAX_IE_AESPROVISIONING, "AES PROVISIONG" },
-       { IAX_IE_DATETIME, "DATE TIME", dump_int },
+       { IAX_IE_DATETIME, "DATE TIME", dump_datetime },
        { IAX_IE_DEVICETYPE, "DEVICE TYPE", dump_string },
        { IAX_IE_SERVICEIDENT, "SERVICE IDENT", dump_string },
        { IAX_IE_FIRMWAREVER, "FIRMWARE VER", dump_short },
@@ -219,6 +263,7 @@ static struct iax2_ie {
        { IAX_IE_RR_DELAY, "RR_DELAY", dump_short },
        { IAX_IE_RR_DROPPED, "RR_DROPPED", dump_int },
        { IAX_IE_RR_OOO, "RR_OUTOFORDER", dump_int },
+       { IAX_IE_VARIABLE, "VARIABLE", dump_string },
 };
 
 static struct iax2_ie prov_ies[] = {
@@ -269,8 +314,9 @@ static void dump_prov_ies(char *output, int maxlen, unsigned char *iedata, int l
                ielen = iedata[1];
                if (ielen + 2> len) {
                        snprintf(tmp, (int)sizeof(tmp), "Total Prov IE length of %d bytes exceeds remaining prov frame length of %d bytes\n", ielen + 2, len);
-                       strncpy(output, tmp, maxlen - 1);
-                       maxlen -= strlen(output); output += strlen(output);
+                       ast_copy_string(output, tmp, maxlen);
+                       maxlen -= strlen(output);
+                       output += strlen(output);
                        return;
                }
                found = 0;
@@ -279,7 +325,7 @@ static void dump_prov_ies(char *output, int maxlen, unsigned char *iedata, int l
                                if (prov_ies[x].dump) {
                                        prov_ies[x].dump(interp, (int)sizeof(interp), iedata + 2, ielen);
                                        snprintf(tmp, (int)sizeof(tmp), "       %-15.15s : %s\n", prov_ies[x].name, interp);
-                                       strncpy(output, tmp, maxlen - 1);
+                                       ast_copy_string(output, tmp, maxlen);
                                        maxlen -= strlen(output); output += strlen(output);
                                } else {
                                        if (ielen)
@@ -287,7 +333,7 @@ static void dump_prov_ies(char *output, int maxlen, unsigned char *iedata, int l
                                        else
                                                strcpy(interp, "Present");
                                        snprintf(tmp, (int)sizeof(tmp), "       %-15.15s : %s\n", prov_ies[x].name, interp);
-                                       strncpy(output, tmp, maxlen - 1);
+                                       ast_copy_string(output, tmp, maxlen);
                                        maxlen -= strlen(output); output += strlen(output);
                                }
                                found++;
@@ -295,7 +341,7 @@ static void dump_prov_ies(char *output, int maxlen, unsigned char *iedata, int l
                }
                if (!found) {
                        snprintf(tmp, (int)sizeof(tmp), "       Unknown Prov IE %03d  : Present\n", ie);
-                       strncpy(output, tmp, maxlen - 1);
+                       ast_copy_string(output, tmp, maxlen);
                        maxlen -= strlen(output); output += strlen(output);
                }
                iedata += (2 + ielen);
@@ -351,9 +397,9 @@ static void dump_ies(unsigned char *iedata, int len)
 
 void iax_showframe(struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, struct sockaddr_in *sin, int datalen)
 {
-       char *frames[] = {
+       const char *frames[] = {
                "(0?)",
-               "DTMF   ",
+               "DTMF_E ",
                "VOICE  ",
                "VIDEO  ",
                "CONTROL",
@@ -362,8 +408,11 @@ void iax_showframe(struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, s
                "TEXT   ",
                "IMAGE  ",
                "HTML   ",
-               "CNG    " };
-       char *iaxs[] = {
+               "CNG    ",
+               "MODEM  ",
+               "DTMF_B ",
+       };
+       const char *iaxs[] = {
                "(0?)",
                "NEW    ",
                "PING   ",
@@ -394,35 +443,61 @@ void iax_showframe(struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, s
                "TXREJ  ",
                "QUELCH ",
                "UNQULCH",
-               "POKE",
-               "PAGE",
-               "MWI",
-               "UNSUPPORTED",
-               "TRANSFER",
-               "PROVISION",
-               "FWDOWNLD",
-               "FWDATA"
+               "POKE   ",
+               "PAGE   ",
+               "MWI    ",
+               "UNSPRTD",
+               "TRANSFR",
+               "PROVISN",
+               "FWDWNLD",
+               "FWDATA "
        };
-       char *cmds[] = {
+       const char *cmds[] = {
                "(0?)",
                "HANGUP ",
                "RING   ",
                "RINGING",
                "ANSWER ",
                "BUSY   ",
-               "TKOFFHK ",
-               "OFFHOOK" };
+               "TKOFFHK",
+               "OFFHOOK",
+               "CONGSTN",
+               "FLASH  ",
+               "WINK   ",
+               "OPTION ",
+               "RDKEY  ",
+               "RDUNKEY",
+               "PROGRES",
+               "PROCDNG",
+               "HOLD   ",
+               "UNHOLD ",
+               "VIDUPDT", };
        struct ast_iax2_full_hdr *fh;
        char retries[20];
        char class2[20];
        char subclass2[20];
-       char *class;
-       char *subclass;
-       char tmp[256];
-       char iabuf[INET_ADDRSTRLEN];
+       const char *class;
+       const char *subclass;
+       char *dir;
+       char tmp[512];
+
+       switch(rx) {
+       case 0:
+               dir = "Tx";
+               break;
+       case 2:
+               dir = "TE";
+               break;
+       case 3:
+               dir = "RD";
+               break;
+       default:
+               dir = "Rx";
+               break;
+       }
        if (f) {
                fh = f->data;
-               snprintf(retries, (int)sizeof(retries), "%03d", f->retries);
+               snprintf(retries, sizeof(retries), "%03d", f->retries);
        } else {
                fh = fhi;
                if (ntohs(fh->dcallno) & IAX_FLAG_RETRANS)
@@ -435,48 +510,48 @@ void iax_showframe(struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, s
                return;
        }
        if (fh->type >= (int)sizeof(frames)/(int)sizeof(frames[0])) {
-               snprintf(class2, (int)sizeof(class2), "(%d?)", fh->type);
+               snprintf(class2, sizeof(class2), "(%d?)", fh->type);
                class = class2;
        } else {
                class = frames[(int)fh->type];
        }
-       if (fh->type == AST_FRAME_DTMF) {
+       if (fh->type == AST_FRAME_DTMF_BEGIN || fh->type == AST_FRAME_DTMF_END) {
                sprintf(subclass2, "%c", fh->csub);
                subclass = subclass2;
        } else if (fh->type == AST_FRAME_IAX) {
                if (fh->csub >= (int)sizeof(iaxs)/(int)sizeof(iaxs[0])) {
-                       snprintf(subclass2, (int)sizeof(subclass2), "(%d?)", fh->csub);
+                       snprintf(subclass2, sizeof(subclass2), "(%d?)", fh->csub);
                        subclass = subclass2;
                } else {
                        subclass = iaxs[(int)fh->csub];
                }
        } else if (fh->type == AST_FRAME_CONTROL) {
                if (fh->csub >= (int)sizeof(cmds)/(int)sizeof(cmds[0])) {
-                       snprintf(subclass2, (int)sizeof(subclass2), "(%d?)", fh->csub);
+                       snprintf(subclass2, sizeof(subclass2), "(%d?)", fh->csub);
                        subclass = subclass2;
                } else {
                        subclass = cmds[(int)fh->csub];
                }
        } else {
-               snprintf(subclass2, (int)sizeof(subclass2), "%d", fh->csub);
+               snprintf(subclass2, sizeof(subclass2), "%d", fh->csub);
                subclass = subclass2;
        }
-snprintf(tmp, (int)sizeof(tmp), 
-"%s-Frame Retry[%s] -- OSeqno: %3.3d ISeqno: %3.3d Type: %s Subclass: %s\n",
-       (rx ? "Rx" : "Tx"),
-       retries, fh->oseqno, fh->iseqno, class, subclass);
+       snprintf(tmp, sizeof(tmp), 
+                "%s-Frame Retry[%s] -- OSeqno: %3.3d ISeqno: %3.3d Type: %s Subclass: %s\n",
+                dir,
+                retries, fh->oseqno, fh->iseqno, class, subclass);
        outputf(tmp);
-snprintf(tmp, (int)sizeof(tmp), 
-"   Timestamp: %05lums  SCall: %5.5d  DCall: %5.5d [%s:%d]\n",
-       (unsigned long)ntohl(fh->ts),
-       ntohs(fh->scallno) & ~IAX_FLAG_FULL, ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS,
-               ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port));
+       snprintf(tmp, sizeof(tmp), 
+                "   Timestamp: %05lums  SCall: %5.5d  DCall: %5.5d [%s:%d]\n",
+                (unsigned long)ntohl(fh->ts),
+                ntohs(fh->scallno) & ~IAX_FLAG_FULL, ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS,
+                ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
        outputf(tmp);
        if (fh->type == AST_FRAME_IAX)
                dump_ies(fh->iedata, datalen);
 }
 
-int iax_ie_append_raw(struct iax_ie_data *ied, unsigned char ie, void *data, int datalen)
+int iax_ie_append_raw(struct iax_ie_data *ied, unsigned char ie, const void *data, int datalen)
 {
        char tmp[256];
        if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
@@ -491,7 +566,7 @@ int iax_ie_append_raw(struct iax_ie_data *ied, unsigned char ie, void *data, int
        return 0;
 }
 
-int iax_ie_append_addr(struct iax_ie_data *ied, unsigned char ie, struct sockaddr_in *sin)
+int iax_ie_append_addr(struct iax_ie_data *ied, unsigned char ie, const struct sockaddr_in *sin)
 {
        return iax_ie_append_raw(ied, ie, sin, (int)sizeof(struct sockaddr_in));
 }
@@ -510,7 +585,7 @@ int iax_ie_append_short(struct iax_ie_data *ied, unsigned char ie, unsigned shor
        return iax_ie_append_raw(ied, ie, &newval, (int)sizeof(newval));
 }
 
-int iax_ie_append_str(struct iax_ie_data *ied, unsigned char ie, unsigned char *str)
+int iax_ie_append_str(struct iax_ie_data *ied, unsigned char ie, const char *str)
 {
        return iax_ie_append_raw(ied, ie, str, strlen(str));
 }
@@ -540,7 +615,8 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
        /* Parse data into information elements */
        int len;
        int ie;
-       char tmp[256];
+       char tmp[256], *tmp2;
+       struct ast_variable *var, *var2, *prev;
        memset(ies, 0, (int)sizeof(struct iax_ies));
        ies->msgcount = -1;
        ies->firmwarever = -1;
@@ -557,28 +633,28 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
                }
                switch(ie) {
                case IAX_IE_CALLED_NUMBER:
-                       ies->called_number = data + 2;
+                       ies->called_number = (char *)data + 2;
                        break;
                case IAX_IE_CALLING_NUMBER:
-                       ies->calling_number = data + 2;
+                       ies->calling_number = (char *)data + 2;
                        break;
                case IAX_IE_CALLING_ANI:
-                       ies->calling_ani = data + 2;
+                       ies->calling_ani = (char *)data + 2;
                        break;
                case IAX_IE_CALLING_NAME:
-                       ies->calling_name = data + 2;
+                       ies->calling_name = (char *)data + 2;
                        break;
                case IAX_IE_CALLED_CONTEXT:
-                       ies->called_context = data + 2;
+                       ies->called_context = (char *)data + 2;
                        break;
                case IAX_IE_USERNAME:
-                       ies->username = data + 2;
+                       ies->username = (char *)data + 2;
                        break;
                case IAX_IE_PASSWORD:
-                       ies->password = data + 2;
+                       ies->password = (char *)data + 2;
                        break;
                case IAX_IE_CODEC_PREFS:
-                       ies->codec_prefs = data + 2;
+                       ies->codec_prefs = (char *)data + 2;
                        break;
                case IAX_IE_CAPABILITY:
                        if (len != (int)sizeof(unsigned int)) {
@@ -595,7 +671,7 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
                                ies->format = ntohl(get_unaligned_uint32(data + 2));
                        break;
                case IAX_IE_LANGUAGE:
-                       ies->language = data + 2;
+                       ies->language = (char *)data + 2;
                        break;
                case IAX_IE_VERSION:
                        if (len != (int)sizeof(unsigned short)) {
@@ -619,10 +695,10 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
                                ies->samprate = ntohs(get_unaligned_uint16(data + 2));
                        break;
                case IAX_IE_DNID:
-                       ies->dnid = data + 2;
+                       ies->dnid = (char *)data + 2;
                        break;
                case IAX_IE_RDNIS:
-                       ies->rdnis = data + 2;
+                       ies->rdnis = (char *)data + 2;
                        break;
                case IAX_IE_AUTHMETHODS:
                        if (len != (int)sizeof(unsigned short))  {
@@ -639,13 +715,13 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
                                ies->encmethods = ntohs(get_unaligned_uint16(data + 2));
                        break;
                case IAX_IE_CHALLENGE:
-                       ies->challenge = data + 2;
+                       ies->challenge = (char *)data + 2;
                        break;
                case IAX_IE_MD5_RESULT:
-                       ies->md5_result = data + 2;
+                       ies->md5_result = (char *)data + 2;
                        break;
                case IAX_IE_RSA_RESULT:
-                       ies->rsa_result = data + 2;
+                       ies->rsa_result = (char *)data + 2;
                        break;
                case IAX_IE_APPARENT_ADDR:
                        ies->apparent_addr = ((struct sockaddr_in *)(data + 2));
@@ -672,7 +748,7 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
                                ies->callno = ntohs(get_unaligned_uint16(data + 2));
                        break;
                case IAX_IE_CAUSE:
-                       ies->cause = data + 2;
+                       ies->cause = (char *)data + 2;
                        break;
                case IAX_IE_CAUSECODE:
                        if (len != 1) {
@@ -725,10 +801,10 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
                                ies->firmwarever = ntohs(get_unaligned_uint16(data + 2));       
                        break;
                case IAX_IE_DEVICETYPE:
-                       ies->devicetype = data + 2;
+                       ies->devicetype = (char *)data + 2;
                        break;
                case IAX_IE_SERVICEIDENT:
-                       ies->serviceident = data + 2;
+                       ies->serviceident = (char *)data + 2;
                        break;
                case IAX_IE_FWBLOCKDESC:
                        if (len != (int)sizeof(unsigned int)) {
@@ -825,6 +901,35 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
                                ies->rr_ooo = ntohl(get_unaligned_uint32(data + 2));
                        }
                        break;
+               case IAX_IE_VARIABLE:
+                       ast_copy_string(tmp, (char *)data + 2, len + 1);
+                       tmp2 = strchr(tmp, '=');
+                       if (tmp2)
+                               *tmp2++ = '\0';
+                       else
+                               tmp2 = "";
+                       /* Existing variable or new variable? */
+                       for (var2 = ies->vars, prev = NULL; var2; prev = var2, var2 = var2->next) {
+                               if (strcmp(tmp, var2->name) == 0) {
+                                       int len = strlen(var2->value) + strlen(tmp2) + 1;
+                                       char *tmp3 = alloca(len);
+                                       snprintf(tmp3, len, "%s%s", var2->value, tmp2);
+                                       var = ast_variable_new(tmp, tmp3);
+                                       var->next = var2->next;
+                                       if (prev)
+                                               prev->next = var;
+                                       else
+                                               ies->vars = var;
+                                       free(var2);
+                                       break;
+                               }
+                       }
+                       if (!var2) {
+                               var = ast_variable_new(tmp, tmp2);
+                               var->next = ies->vars;
+                               ies->vars = var;
+                       }
+                       break;
                default:
                        snprintf(tmp, (int)sizeof(tmp), "Ignoring unknown information element '%s' (%d) of length %d\n", iax_ie2str(ie), ie, len);
                        outputf(tmp);
@@ -858,7 +963,7 @@ void iax_frame_wrap(struct iax_frame *fr, struct ast_frame *f)
        if (fr->af.datalen) {
 #if __BYTE_ORDER == __LITTLE_ENDIAN
                /* We need to byte-swap slinear samples from network byte order */
-               if (fr->af.subclass == AST_FORMAT_SLINEAR) {
+               if ((fr->af.frametype == AST_FRAME_VOICE) && (fr->af.subclass == AST_FORMAT_SLINEAR)) {
                        ast_swapcopy_samples(fr->af.data, f->data, fr->af.samples);
                } else
 #endif
@@ -866,38 +971,95 @@ void iax_frame_wrap(struct iax_frame *fr, struct ast_frame *f)
        }
 }
 
-struct iax_frame *iax_frame_new(int direction, int datalen)
+struct iax_frame *iax_frame_new(int direction, int datalen, unsigned int cacheable)
 {
-       struct iax_frame *fr;
-       fr = malloc((int)sizeof(struct iax_frame) + datalen);
-       if (fr) {
-               fr->direction = direction;
-               fr->retrans = -1;
-               frames++;
-               if (fr->direction == DIRECTION_INGRESS)
-                       iframes++;
-               else
-                       oframes++;
+       struct iax_frame *fr = NULL;
+
+#if !defined(LOW_MEMORY)
+       struct iax_frames *iax_frames;
+
+       /* Attempt to get a frame from this thread's cache */
+       if ((iax_frames = ast_threadstorage_get(&frame_cache, sizeof(*iax_frames)))) {
+               AST_LIST_TRAVERSE_SAFE_BEGIN(iax_frames, fr, list) {
+                       if (fr->mallocd_datalen >= datalen) {
+                               size_t mallocd_datalen = fr->mallocd_datalen;
+                               AST_LIST_REMOVE_CURRENT(iax_frames, list);
+                               memset(fr, 0, sizeof(*fr));
+                               fr->mallocd_datalen = mallocd_datalen;
+                               break;
+                       }
+               }
+               AST_LIST_TRAVERSE_SAFE_END
+       }
+       if (!fr) {
+               if (!(fr = ast_calloc_cache(1, sizeof(*fr) + datalen)))
+                       return NULL;
+               fr->mallocd_datalen = datalen;
        }
+#else
+       if (!(fr = ast_calloc(1, sizeof(*fr) + datalen)))
+               return NULL;
+       fr->mallocd_datalen = datalen;
+#endif
+
+
+       fr->direction = direction;
+       fr->retrans = -1;
+       fr->cacheable = cacheable;
+       
+       if (fr->direction == DIRECTION_INGRESS)
+               ast_atomic_fetchadd_int(&iframes, 1);
+       else
+               ast_atomic_fetchadd_int(&oframes, 1);
+       
+       ast_atomic_fetchadd_int(&frames, 1);
+
        return fr;
 }
 
 void iax_frame_free(struct iax_frame *fr)
 {
+#if !defined(LOW_MEMORY)
+       struct iax_frames *iax_frames;
+#endif
+
        /* Note: does not remove from scheduler! */
        if (fr->direction == DIRECTION_INGRESS)
-               iframes--;
+               ast_atomic_fetchadd_int(&iframes, -1);
        else if (fr->direction == DIRECTION_OUTGRESS)
-               oframes--;
+               ast_atomic_fetchadd_int(&oframes, -1);
        else {
                errorf("Attempt to double free frame detected\n");
                return;
        }
+       ast_atomic_fetchadd_int(&frames, -1);
+
+#if !defined(LOW_MEMORY)
+       if (!fr->cacheable || !(iax_frames = ast_threadstorage_get(&frame_cache, sizeof(*iax_frames)))) {
+               free(fr);
+               return;
+       }
+
        fr->direction = 0;
+       AST_LIST_INSERT_HEAD(iax_frames, fr, list);
+#else
        free(fr);
-       frames--;
+#endif
 }
 
+#if !defined(LOW_MEMORY)
+static void frame_cache_cleanup(void *data)
+{
+       struct iax_frames *frames = data;
+       struct iax_frame *cur;
+
+       while ((cur = AST_LIST_REMOVE_HEAD(frames, list)))
+               free(cur);
+
+       free(frames);
+}
+#endif
+
 int iax_get_frames(void) { return frames; }
 int iax_get_iframes(void) { return iframes; }
 int iax_get_oframes(void) { return oframes; }