media formats: re-architect handling of media for performance improvements
[asterisk/asterisk.git] / channels / sip / reqresp_parser.c
index 594bcfa..88aea07 100644 (file)
  * \brief sip request parsing functions and unit tests
  */
 
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
@@ -33,7 +37,7 @@ locale_t c_locale;
 
 /*! \brief * parses a URI in its components.*/
 int parse_uri_full(char *uri, const char *scheme, char **user, char **pass,
-                  char **domain, struct uriparams *params, char **headers,
+                  char **hostport, struct uriparams *params, char **headers,
                   char **residue)
 {
        char *userinfo = NULL;
@@ -41,6 +45,28 @@ int parse_uri_full(char *uri, const char *scheme, char **user, char **pass,
        char *endparams = NULL;
        char *c = NULL;
        int error = 0;
+       int teluri_scheme = 0;
+
+       /*
+        * Initialize requested strings - some functions don't care if parse_uri fails
+        * and will attempt to use string pointers passed into parse_uri even after a
+        * parse_uri failure
+        */
+       if (user) {
+               *user = "";
+       }
+       if (pass) {
+               *pass = "";
+       }
+       if (hostport) {
+               *hostport = "";
+       }
+       if (headers) {
+               *headers = "";
+       }
+       if (residue) {
+               *residue = "";
+       }
 
        /* check for valid input */
        if (ast_strlen_zero(uri)) {
@@ -54,6 +80,7 @@ int parse_uri_full(char *uri, const char *scheme, char **user, char **pass,
                for (; !ast_strlen_zero(cur); cur = strsep(&scheme2, ",")) {
                        l = strlen(cur);
                        if (!strncasecmp(uri, cur, l)) {
+                               teluri_scheme = !strncasecmp(uri, "tel:", 4);   /* TEL URI */
                                uri += l;
                                break;
                        }
@@ -64,10 +91,46 @@ int parse_uri_full(char *uri, const char *scheme, char **user, char **pass,
                }
        }
 
-       if (!domain) {
-               /* if we don't want to split around domain, keep everything as a
+       if (!hostport) {
+               /* if we don't want to split around hostport, keep everything as a
                 * userinfo - cos thats how old parse_uri operated*/
                userinfo = uri;
+       } else if (teluri_scheme) {
+               /*
+                * tel: TEL URI INVITE RFC 3966 patch
+                * See http://www.ietf.org/rfc/rfc3966.txt
+                *
+                * Once the full RFC 3966 parsing is implemented,
+                * the ext= or isub= parameters would be extracted from userinfo.
+                * When this kind of subaddressing would be implemented, the userinfo must be further parsed.
+                * Those parameters would be used for ISDN or PSTN local extensions.
+                *
+                * Current restrictions:
+                * We currently consider the ";isub=" or the ";ext=" as part of the userinfo (unparsed).
+                */
+
+               if ((c = strstr(uri, ";phone-context="))) {
+                       /*
+                        * Local number with context or domain.
+                        * ext= or isub= TEL URI parameters should be upfront.
+                        * All other parameters should come after the ";phone-context=" parameter.
+                        * If other parameters would occur before ";phone-context=" they will be ignored.
+                        */
+
+                        *c = '\0';
+                        userinfo = uri;
+                        uri = c + 15;
+                       *hostport = uri;
+                } else if ('+' == uri[0]) {
+                       /* Global number without context or domain; possibly followed by RFC 3966 and optional other parameters. */
+
+                        userinfo = uri;
+                       *hostport = uri;
+               } else {
+                       ast_debug(1, "No RFC 3966 global number or context found in '%s'; returning local number anyway\n", uri);
+                        userinfo = uri;                /* Return local number anyway */
+                       error = -1;
+               }
        } else {
                char *dom = "";
                if ((c = strchr(uri, '@'))) {
@@ -81,7 +144,7 @@ int parse_uri_full(char *uri, const char *scheme, char **user, char **pass,
                        userinfo = "";
                }
 
-               *domain = dom;
+               *hostport = dom;
        }
 
        if (pass && (c = strchr(userinfo, ':'))) {        /* user:password */
@@ -155,23 +218,23 @@ int parse_uri_full(char *uri, const char *scheme, char **user, char **pass,
                        }
 
                        if (!strcmp(label, "transport")) {
-                               if (params) {params->transport=value;}
+                               params->transport = value;
                                rem = parameters;
                        } else if (!strcmp(label, "user")) {
-                               if (params) {params->user=value;}
+                               params->user = value;
                                rem = parameters;
                        } else if (!strcmp(label, "method")) {
-                               if (params) {params->method=value;}
+                               params->method = value;
                                rem = parameters;
                        } else if (!strcmp(label, "ttl")) {
-                               if (params) {params->ttl=value;}
+                               params->ttl = value;
                                rem = parameters;
                        } else if (!strcmp(label, "maddr")) {
-                               if (params) {params->maddr=value;}
+                               params->maddr = value;
                                rem = parameters;
                        /* Treat "lr", "lr=yes", "lr=on", "lr=1", "lr=almostanything" as lr enabled and "", "lr=no", "lr=off", "lr=0", "lr=" and "lranything" as lr disabled */
                        } else if ((!strcmp(label, "lr") && strcmp(value, "no") && strcmp(value, "off") && strcmp(value, "0") && strcmp(value, "")) || ((lr) && strcmp(value, "lr"))) {
-                               if (params) {params->lr=1;}
+                               params->lr = 1;
                                rem = parameters;
                        } else {
                                value--;
@@ -196,25 +259,19 @@ int parse_uri_full(char *uri, const char *scheme, char **user, char **pass,
 }
 
 
-AST_TEST_DEFINE(sip_parse_uri_fully_test)
+AST_TEST_DEFINE(sip_parse_uri_full_test)
 {
        int res = AST_TEST_PASS;
        char uri[1024];
-       char *user, *pass, *domain, *headers, *residue;
+       char *user, *pass, *hostport, *headers, *residue;
        struct uriparams params;
 
        struct testdata {
                char *desc;
                char *uri;
-               char **userptr;
-               char **passptr;
-               char **domainptr;
-               char **headersptr;
-               char **residueptr;
-               struct uriparams *paramsptr;
                char *user;
                char *pass;
-               char *domain;
+               char *hostport;
                char *headers;
                char *residue;
                struct uriparams params;
@@ -229,15 +286,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        struct testdata td1 = {
                .desc = "no headers",
                .uri = "sip:user:secret@host:5060;param=discard;transport=tcp;param2=residue",
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .user = "user",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "",
                .residue = "param2=residue",
                .params.transport = "tcp",
@@ -248,15 +299,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        struct testdata td2 = {
                .desc = "with headers",
                .uri = "sip:user:secret@host:5060;param=discard;transport=tcp;param2=discard2?header=blah&header2=blah2;param3=residue",
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .user = "user",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "header=blah&header2=blah2",
                .residue = "param3=residue",
                .params.transport = "tcp",
@@ -267,15 +312,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        struct testdata td3 = {
                .desc = "difficult user",
                .uri = "sip:-_.!~*'()&=+$,;?/:secret@host:5060;transport=tcp",
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .user = "-_.!~*'()&=+$,;?/",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "",
                .residue = "",
                .params.transport = "tcp",
@@ -286,15 +325,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        struct testdata td4 = {
                .desc = "difficult pass",
                .uri = "sip:user:-_.!~*'()&=+$,@host:5060;transport=tcp",
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .user = "user",
                .pass = "-_.!~*'()&=+$,",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "",
                .residue = "",
                .params.transport = "tcp",
@@ -305,15 +338,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        struct testdata td5 = {
                .desc = "difficult host",
                .uri = "sip:user:secret@1-1.a-1.:5060;transport=tcp",
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .user = "user",
                .pass = "secret",
-               .domain = "1-1.a-1.:5060",
+               .hostport = "1-1.a-1.:5060",
                .headers = "",
                .residue = "",
                .params.transport = "tcp",
@@ -324,15 +351,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        struct testdata td6 = {
                .desc = "difficult params near transport",
                .uri = "sip:user:secret@host:5060;-_.!~*'()[]/:&+$=-_.!~*'()[]/:&+$;transport=tcp",
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .user = "user",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "",
                .residue = "",
                .params.transport = "tcp",
@@ -343,15 +364,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        struct testdata td7 = {
                .desc = "difficult params near headers",
                .uri = "sip:user:secret@host:5060;-_.!~*'()[]/:&+$=-_.!~*'()[]/:&+$?header=blah&header2=blah2;-_.!~*'()[]/:&+$=residue",
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .user = "user",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "header=blah&header2=blah2",
                .residue = "-_.!~*'()[]/:&+$=residue",
                .params.transport = "",
@@ -362,15 +377,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        struct testdata td8 = {
                .desc = "lr parameter",
                .uri = "sip:user:secret@host:5060;param=discard;lr?header=blah",
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .user = "user",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "header=blah",
                .residue = "",
                .params.transport = "",
@@ -381,15 +390,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        struct testdata td9 = {
                .desc = "alternative lr parameter",
                .uri = "sip:user:secret@host:5060;param=discard;lr=yes?header=blah",
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .user = "user",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "header=blah",
                .residue = "",
                .params.transport = "",
@@ -400,15 +403,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        struct testdata td10 = {
                .desc = "no lr parameter",
                .uri = "sip:user:secret@host:5060;paramlr=lr;lr=no;lr=off;lr=0;lr=;=lr;lrextra;lrparam2=lr?header=blah",
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .user = "user",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "header=blah",
                .residue = "",
                .params.transport = "",
@@ -416,6 +413,51 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
                .params.user = ""
        };
 
+       /* RFC 3966 TEL URI INVITE */
+       struct testdata td11 = {
+               .desc = "tel local number",
+               .uri = "tel:0987654321;phone-context=+32987654321",
+               .user = "0987654321",
+               .pass = "",
+               .hostport = "+32987654321",
+               .headers = "",
+               .residue = "",
+               .params.transport = "",
+               .params.lr = 0,
+               .params.user = ""
+       };
+
+       struct testdata td12 = {
+               .desc = "tel global number",
+               .uri = "tel:+32987654321",
+               .user = "+32987654321",
+               .pass = "",
+               .hostport = "+32987654321",
+               .headers = "",
+               .residue = "",
+               .params.transport = "",
+               .params.lr = 0,
+               .params.user = ""
+       };
+
+       /*
+        * Once the full RFC 3966 parsing is implemented,
+        * only the ext= or isub= parameters would be extracted from .user
+        * Then the ;param=discard would be ignored,
+        * and the .user would only contain "0987654321"
+        */
+       struct testdata td13 = {
+               .desc = "tel local number",
+               .uri = "tel:0987654321;ext=1234;param=discard;phone-context=+32987654321;transport=udp;param2=discard2?header=blah&header2=blah2;param3=residue",
+               .user = "0987654321;ext=1234;param=discard",
+               .pass = "",
+               .hostport = "+32987654321",
+               .headers = "header=blah&header2=blah2",
+               .residue = "param3=residue",
+               .params.transport = "udp",
+               .params.lr = 0,
+               .params.user = ""
+       };
 
        AST_LIST_HEAD_SET_NOLOCK(&testdatalist, &td1);
        AST_LIST_INSERT_TAIL(&testdatalist, &td2, list);
@@ -427,7 +469,9 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        AST_LIST_INSERT_TAIL(&testdatalist, &td8, list);
        AST_LIST_INSERT_TAIL(&testdatalist, &td9, list);
        AST_LIST_INSERT_TAIL(&testdatalist, &td10, list);
-
+       AST_LIST_INSERT_TAIL(&testdatalist, &td11, list);
+       AST_LIST_INSERT_TAIL(&testdatalist, &td12, list);
+       AST_LIST_INSERT_TAIL(&testdatalist, &td13, list);
 
        switch (cmd) {
        case TEST_INIT:
@@ -443,24 +487,24 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
        }
 
        AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
-               user = pass = domain = headers = residue = NULL;
+               user = pass = hostport = headers = residue = NULL;
                params.transport = params.user = params.method = params.ttl = params.maddr = NULL;
                params.lr = 0;
 
                ast_copy_string(uri,testdataptr->uri,sizeof(uri));
-               if (parse_uri_full(uri, "sip:,sips:", testdataptr->userptr,
-                                  testdataptr->passptr, testdataptr->domainptr,
-                                  testdataptr->paramsptr,
-                                  testdataptr->headersptr,
-                                  testdataptr->residueptr) ||
-                       ((testdataptr->userptr) && strcmp(testdataptr->user, user)) ||
-                       ((testdataptr->passptr) && strcmp(testdataptr->pass, pass)) ||
-                       ((testdataptr->domainptr) && strcmp(testdataptr->domain, domain)) ||
-                       ((testdataptr->headersptr) && strcmp(testdataptr->headers, headers)) ||
-                       ((testdataptr->residueptr) && strcmp(testdataptr->residue, residue)) ||
-                       ((testdataptr->paramsptr) && strcmp(testdataptr->params.transport,params.transport)) ||
-                       ((testdataptr->paramsptr) && (testdataptr->params.lr != params.lr)) ||
-                       ((testdataptr->paramsptr) && strcmp(testdataptr->params.user,params.user))
+               if (parse_uri_full(uri, "sip:,sips:,tel:", &user,
+                                  &pass, &hostport,
+                                  &params,
+                                  &headers,
+                                  &residue) ||
+                       (user && strcmp(testdataptr->user, user)) ||
+                       (pass && strcmp(testdataptr->pass, pass)) ||
+                       (hostport && strcmp(testdataptr->hostport, hostport)) ||
+                       (headers && strcmp(testdataptr->headers, headers)) ||
+                       (residue && strcmp(testdataptr->residue, residue)) ||
+                       (strcmp(testdataptr->params.transport,params.transport)) ||
+                       (testdataptr->params.lr != params.lr) ||
+                       (strcmp(testdataptr->params.user,params.user))
                ) {
                                ast_test_status_update(test, "Sub-Test: %s, failed.\n", testdataptr->desc);
                                res = AST_TEST_FAIL;
@@ -473,13 +517,13 @@ AST_TEST_DEFINE(sip_parse_uri_fully_test)
 
 
 int parse_uri(char *uri, const char *scheme, char **user, char **pass,
-             char **domain, char **transport) {
+             char **hostport, char **transport) {
        int ret;
        char *headers;
        struct uriparams params;
 
        headers = NULL;
-       ret = parse_uri_full(uri, scheme, user, pass, domain, &params, &headers, NULL);
+       ret = parse_uri_full(uri, scheme, user, pass, hostport, &params, &headers, NULL);
        if (transport) {
                *transport=params.transport;
        }
@@ -489,7 +533,7 @@ int parse_uri(char *uri, const char *scheme, char **user, char **pass,
 AST_TEST_DEFINE(sip_parse_uri_test)
 {
        int res = AST_TEST_PASS;
-       char *name, *pass, *domain, *transport;
+       char *name, *pass, *hostport, *transport;
        char uri1[] = "sip:name@host";
        char uri2[] = "sip:name@host;transport=tcp";
        char uri3[] = "sip:name:secret@host;transport=tcp";
@@ -501,6 +545,7 @@ AST_TEST_DEFINE(sip_parse_uri_test)
        char uri9[] = "sip:host:port;transport=tcp?headers=%40%40testblah&headers2=blah%20blah";
        char uri10[] = "host:port;transport=tcp?headers=%40%40testblah&headers2=blah%20blah";
        char uri11[] = "host";
+       char uri12[] = "tel:911";       /* TEL URI Local number without context or global number */
 
        switch (cmd) {
        case TEST_INIT:
@@ -516,119 +561,130 @@ AST_TEST_DEFINE(sip_parse_uri_test)
        }
 
        /* Test 1, simple URI */
-       name = pass = domain = transport = NULL;
-       if (parse_uri(uri1, "sip:,sips:", &name, &pass, &domain, &transport) ||
+       name = pass = hostport = transport = NULL;
+       if (parse_uri(uri1, "sip:,sips:", &name, &pass, &hostport, &transport) ||
                        strcmp(name, "name")        ||
                        !ast_strlen_zero(pass)      ||
-                       strcmp(domain, "host")      ||
+                       strcmp(hostport, "host")      ||
                        !ast_strlen_zero(transport)) {
                ast_test_status_update(test, "Test 1: simple uri failed. \n");
                res = AST_TEST_FAIL;
        }
 
        /* Test 2, add tcp transport */
-       name = pass = domain = transport = NULL;
-       if (parse_uri(uri2, "sip:,sips:", &name, &pass, &domain, &transport) ||
+       name = pass = hostport = transport = NULL;
+       if (parse_uri(uri2, "sip:,sips:", &name, &pass, &hostport, &transport) ||
                        strcmp(name, "name")        ||
                        !ast_strlen_zero(pass)      ||
-                       strcmp(domain, "host")    ||
+                       strcmp(hostport, "host")    ||
                        strcmp(transport, "tcp")) {
                ast_test_status_update(test, "Test 2: uri with addtion of tcp transport failed. \n");
                res = AST_TEST_FAIL;
        }
 
        /* Test 3, add secret */
-       name = pass = domain = transport = NULL;
-       if (parse_uri(uri3, "sip:,sips:", &name, &pass, &domain, &transport) ||
+       name = pass = hostport = transport = NULL;
+       if (parse_uri(uri3, "sip:,sips:", &name, &pass, &hostport, &transport) ||
                        strcmp(name, "name")        ||
                        strcmp(pass, "secret")      ||
-                       strcmp(domain, "host")    ||
+                       strcmp(hostport, "host")    ||
                        strcmp(transport, "tcp")) {
                ast_test_status_update(test, "Test 3: uri with addition of secret failed.\n");
                res = AST_TEST_FAIL;
        }
 
        /* Test 4, add port and unparsed header field*/
-       name = pass = domain = transport = NULL;
-       if (parse_uri(uri4, "sip:,sips:", &name, &pass, &domain, &transport) ||
+       name = pass = hostport = transport = NULL;
+       if (parse_uri(uri4, "sip:,sips:", &name, &pass, &hostport, &transport) ||
                        strcmp(name, "name")        ||
                        strcmp(pass, "secret")      ||
-                       strcmp(domain, "host:port") ||
+                       strcmp(hostport, "host:port") ||
                        strcmp(transport, "tcp")) {
                ast_test_status_update(test, "Test 4: add port and unparsed header field failed.\n");
                res = AST_TEST_FAIL;
        }
 
        /* Test 5, verify parse_uri does not crash when given a NULL uri */
-       name = pass = domain = transport = NULL;
-       if (!parse_uri(NULL, "sip:,sips:", &name, &pass, &domain, &transport)) {
+       name = pass = hostport = transport = NULL;
+       if (!parse_uri(NULL, "sip:,sips:", &name, &pass, &hostport, &transport)) {
                ast_test_status_update(test, "Test 5: passing a NULL uri failed.\n");
                res = AST_TEST_FAIL;
        }
 
        /* Test 6, verify parse_uri does not crash when given a NULL output parameters */
-       name = pass = domain = transport = NULL;
+       name = pass = hostport = transport = NULL;
        if (parse_uri(uri6, "sip:,sips:", NULL, NULL, NULL, NULL)) {
                ast_test_status_update(test, "Test 6: passing NULL output parameters failed.\n");
                res = AST_TEST_FAIL;
        }
 
-       /* Test 7, verify parse_uri returns user:secret and domain when no port or secret output parameters are supplied. */
-       name = pass = domain = transport = NULL;
-       if (parse_uri(uri7, "sip:,sips:", &name, NULL, &domain, NULL) ||
+       /* Test 7, verify parse_uri returns user:secret and hostport when no port or secret output parameters are supplied. */
+       name = pass = hostport = transport = NULL;
+       if (parse_uri(uri7, "sip:,sips:", &name, NULL, &hostport, NULL) ||
                        strcmp(name, "name:secret")        ||
-                       strcmp(domain, "host:port")) {
+                       strcmp(hostport, "host:port")) {
 
                ast_test_status_update(test, "Test 7: providing no port and secret output parameters failed.\n");
                res = AST_TEST_FAIL;
        }
 
-       /* Test 8, verify parse_uri can handle a domain only uri */
-       name = pass = domain = transport = NULL;
-       if (parse_uri(uri8, "sip:,sips:", &name, &pass, &domain, &transport) ||
-                       strcmp(domain, "host") ||
+       /* Test 8, verify parse_uri can handle a hostport only uri */
+       name = pass = hostport = transport = NULL;
+       if (parse_uri(uri8, "sip:,sips:", &name, &pass, &hostport, &transport) ||
+                       strcmp(hostport, "host") ||
                        !ast_strlen_zero(name)) {
                ast_test_status_update(test, "Test 8: add port and unparsed header field failed.\n");
                res = AST_TEST_FAIL;
        }
 
-       /* Test 9, add port and unparsed header field with domain only uri*/
-       name = pass = domain = transport = NULL;
-       if (parse_uri(uri9, "sip:,sips:", &name, &pass, &domain, &transport) ||
+       /* Test 9, add port and unparsed header field with hostport only uri*/
+       name = pass = hostport = transport = NULL;
+       if (parse_uri(uri9, "sip:,sips:", &name, &pass, &hostport, &transport) ||
                        !ast_strlen_zero(name)        ||
                        !ast_strlen_zero(pass)      ||
-                       strcmp(domain, "host:port")    ||
+                       strcmp(hostport, "host:port")    ||
                        strcmp(transport, "tcp")) {
-               ast_test_status_update(test, "Test 9: domain only uri failed \n");
+               ast_test_status_update(test, "Test 9: hostport only uri failed \n");
                res = AST_TEST_FAIL;
        }
 
        /* Test 10, handle invalid/missing "sip:,sips:" scheme
         * we expect parse_uri to return an error, but still parse
         * the results correctly here */
-       name = pass = domain = transport = NULL;
-       if (!parse_uri(uri10, "sip:,sips:", &name, &pass, &domain, &transport) ||
+       name = pass = hostport = transport = NULL;
+       if (!parse_uri(uri10, "sip:,sips:", &name, &pass, &hostport, &transport) ||
                        !ast_strlen_zero(name)        ||
                        !ast_strlen_zero(pass)      ||
-                       strcmp(domain, "host:port")    ||
+                       strcmp(hostport, "host:port")    ||
                        strcmp(transport, "tcp")) {
                ast_test_status_update(test, "Test 10: missing \"sip:sips:\" scheme failed\n");
                res = AST_TEST_FAIL;
        }
 
-       /* Test 11, simple domain only URI with missing scheme
+       /* Test 11, simple hostport only URI with missing scheme
         * we expect parse_uri to return an error, but still parse
         * the results correctly here */
-       name = pass = domain = transport = NULL;
-       if (!parse_uri(uri11, "sip:,sips:", &name, &pass, &domain, &transport) ||
+       name = pass = hostport = transport = NULL;
+       if (!parse_uri(uri11, "sip:,sips:", &name, &pass, &hostport, &transport) ||
                        !ast_strlen_zero(name)      ||
                        !ast_strlen_zero(pass)      ||
-                       strcmp(domain, "host")      ||
+                       strcmp(hostport, "host")      ||
                        !ast_strlen_zero(transport)) {
                ast_test_status_update(test, "Test 11: simple uri with missing scheme failed. \n");
                res = AST_TEST_FAIL;
        }
 
+       /* Test 12, simple URI */
+       name = pass = hostport = transport = NULL;
+       if (!parse_uri(uri12, "sip:,sips:,tel:", &name, &pass, &hostport, &transport) ||
+                       strcmp(name, "911")      ||     /* We return local number anyway */
+                       !ast_strlen_zero(pass)      ||
+                       !ast_strlen_zero(hostport)      ||      /* No global number nor context */
+                       !ast_strlen_zero(transport)) {
+               ast_test_status_update(test, "Test 12: TEL URI INVITE failed.\n");
+               res = AST_TEST_FAIL;
+       }
+
        return res;
 }
 
@@ -664,63 +720,77 @@ const char *get_calleridname(const char *input, char *output, size_t outputsize)
        char *orig_output = output;
        const char *orig_input = input;
 
+       if (!output || !outputsize) {
+               /* Bad output parameters.  Should never happen. */
+               return input;
+       }
+
        /* clear any empty characters in the beginning */
        input = ast_skip_blanks(input);
 
-       /* no data at all or no storage room? */
-       if (!input || *input == '<' || !outputsize || !output) {
-               return orig_input;
-       }
-
        /* make sure the output buffer is initilized */
        *orig_output = '\0';
 
        /* make room for '\0' at the end of the output buffer */
-       outputsize--;
+       --outputsize;
+
+       /* no data at all or no display name? */
+       if (!input || *input == '<') {
+               return input;
+       }
 
        /* quoted-string rules */
        if (input[0] == '"') {
                input++; /* skip the first " */
 
-               for (;((outputsize > 0) && *input); input++) {
+               for (; *input; ++input) {
                        if (*input == '"') {  /* end of quoted-string */
                                break;
                        } else if (*input == 0x5c) { /* quoted-pair = "\" (%x00-09 / %x0B-0C / %x0E-7F) */
-                               input++;
-                               if (!*input || (unsigned char)*input > 0x7f || *input == 0xa || *input == 0xd) {
+                               ++input;
+                               if (!*input) {
+                                       break;
+                               }
+                               if ((unsigned char) *input > 0x7f || *input == 0xa || *input == 0xd) {
                                        continue;  /* not a valid quoted-pair, so skip it */
                                }
-                       } else if (((*input != 0x9) && ((unsigned char) *input < 0x20)) ||
-                                   (*input == 0x7f)) {
+                       } else if ((*input != 0x9 && (unsigned char) *input < 0x20)
+                               || *input == 0x7f) {
                                continue; /* skip this invalid character. */
                        }
 
-                       *output++ = *input;
-                       outputsize--;
+                       if (0 < outputsize) {
+                               /* We still have room for the output display-name. */
+                               *output++ = *input;
+                               --outputsize;
+                       }
                }
 
                /* if this is successful, input should be at the ending quote */
-               if (!input || *input != '"') {
+               if (*input != '"') {
                        ast_log(LOG_WARNING, "No ending quote for display-name was found\n");
                        *orig_output = '\0';
                        return orig_input;
                }
 
                /* make sure input is past the last quote */
-               input++;
+               ++input;
 
-               /* terminate outbuf */
+               /* terminate output */
                *output = '\0';
        } else {  /* either an addr-spec or tokenLWS-combo */
-               for (;((outputsize > 0) && *input); input++) {
+               for (; *input; ++input) {
                        /* token or WSP (without LWS) */
                        if ((*input >= '0' && *input <= '9') || (*input >= 'A' && *input <= 'Z')
                                || (*input >= 'a' && *input <= 'z') || *input == '-' || *input == '.'
                                || *input == '!' || *input == '%' || *input == '*' || *input == '_'
                                || *input == '+' || *input == '`' || *input == '\'' || *input == '~'
                                || *input == 0x9 || *input == ' ') {
-                               *output++ = *input;
-                               outputsize -= 1;
+                               if (0 < outputsize) {
+                                       /* We still have room for the output display-name. */
+                                       *output++ = *input;
+                                       --outputsize;
+                               }
                        } else if (*input == '<') {   /* end of tokenLWS-combo */
                                /* we could assert that the previous char is LWS, but we don't care */
                                break;
@@ -738,10 +808,10 @@ const char *get_calleridname(const char *input, char *output, size_t outputsize)
                        return orig_input;
                }
 
-               /* set NULL while trimming trailing whitespace */
+               /* terminate output while trimming any trailing whitespace */
                do {
                        *output-- = '\0';
-               } while (*output == 0x9 || *output == ' '); /* we won't go past orig_output as first was a non-space */
+               } while (orig_output <= output && (*output == 0x9 || *output == ' '));
        }
 
        return input;
@@ -750,11 +820,12 @@ const char *get_calleridname(const char *input, char *output, size_t outputsize)
 AST_TEST_DEFINE(get_calleridname_test)
 {
        int res = AST_TEST_PASS;
-       const char *in1 = "\" quoted-text internal \\\" quote \"<stuff>";
+       const char *in1 = " \" quoted-text internal \\\" quote \"<stuff>";
        const char *in2 = " token text with no quotes <stuff>";
        const char *overflow1 = " \"quoted-text overflow 1234567890123456789012345678901234567890\" <stuff>";
+       const char *overflow2 = " non-quoted text overflow 1234567890123456789012345678901234567890 <stuff>";
        const char *noendquote = " \"quoted-text no end <stuff>";
-       const char *addrspec = " \"sip:blah@blah <stuff>";
+       const char *addrspec = " sip:blah@blah";
        const char *no_quotes_no_brackets = "blah@blah";
        const char *after_dname;
        char dname[40];
@@ -789,11 +860,19 @@ AST_TEST_DEFINE(get_calleridname_test)
        /* quoted-text buffer overflow */
        after_dname = get_calleridname(overflow1, dname, sizeof(dname));
        ast_test_status_update(test, "overflow display-name1: %s\nafter: %s\n", dname, after_dname);
-       if (*dname != '\0' && after_dname != overflow1) {
+       if (strcmp(dname, "quoted-text overflow 123456789012345678")) {
                ast_test_status_update(test, "overflow display-name1 test failed\n");
                res = AST_TEST_FAIL;
        }
 
+       /* non-quoted-text buffer overflow */
+       after_dname = get_calleridname(overflow2, dname, sizeof(dname));
+       ast_test_status_update(test, "overflow display-name2: %s\nafter: %s\n", dname, after_dname);
+       if (strcmp(dname, "non-quoted text overflow 12345678901234")) {
+               ast_test_status_update(test, "overflow display-name2 test failed\n");
+               res = AST_TEST_FAIL;
+       }
+
        /* quoted-text buffer with no terminating end quote */
        after_dname = get_calleridname(noendquote, dname, sizeof(dname));
        ast_test_status_update(test, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
@@ -804,7 +883,7 @@ AST_TEST_DEFINE(get_calleridname_test)
 
        /* addr-spec rather than display-name. */
        after_dname = get_calleridname(addrspec, dname, sizeof(dname));
-       ast_test_status_update(test, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
+       ast_test_status_update(test, "addr-spec display-name1: %s\nafter: %s\n", dname, after_dname);
        if (*dname != '\0' && after_dname != addrspec) {
                ast_test_status_update(test, "detection of addr-spec failed\n");
                res = AST_TEST_FAIL;
@@ -818,16 +897,15 @@ AST_TEST_DEFINE(get_calleridname_test)
                res = AST_TEST_FAIL;
        }
 
-
        return res;
 }
 
 int get_name_and_number(const char *hdr, char **name, char **number)
 {
        char header[256];
-       char tmp_name[50] = { 0, };
+       char tmp_name[256];
        char *tmp_number = NULL;
-       char *domain = NULL;
+       char *hostport = NULL;
        char *dummy = NULL;
 
        if (!name || !number || ast_strlen_zero(hdr)) {
@@ -845,7 +923,7 @@ int get_name_and_number(const char *hdr, char **name, char **number)
        tmp_number = get_in_brackets(header);
 
        /* parse out the number here */
-       if (parse_uri(tmp_number, "sip:,sips:", &tmp_number, &dummy, &domain, NULL) || ast_strlen_zero(tmp_number)) {
+       if (parse_uri(tmp_number, "sip:,sips:", &tmp_number, &dummy, &hostport, NULL) || ast_strlen_zero(tmp_number)) {
                ast_log(LOG_ERROR, "can not parse name and number from sip header.\n");
                return -1;
        }
@@ -967,6 +1045,59 @@ AST_TEST_DEFINE(get_name_and_number_test)
        return res;
 }
 
+int get_in_brackets_const(const char *src,const char **start,int *length)
+{
+       const char *parse = src;
+       const char *first_bracket;
+       const char *second_bracket;
+
+       if (start == NULL) {
+               return -1;
+       }
+       if (length == NULL) {
+               return -1;
+       }
+       *start = NULL;
+       *length = -1;
+       if (ast_strlen_zero(src)) {
+               return 1;
+       }
+
+       /*
+        * Skip any quoted text until we find the part in brackets.
+        * On any error give up and return -1
+        */
+       while ( (first_bracket = strchr(parse, '<')) ) {
+               const char *first_quote = strchr(parse, '"');
+               first_bracket++;
+               if (!first_quote || first_quote >= first_bracket) {
+                       break; /* no need to look at quoted part */
+               }
+               /* the bracket is within quotes, so ignore it */
+               parse = find_closing_quote(first_quote + 1, NULL);
+               if (!*parse) {
+                       ast_log(LOG_WARNING, "No closing quote found in '%s'\n", src);
+                       return  -1;
+               }
+               parse++;
+       }
+
+       /* Require a first bracket.  Unlike get_in_brackets_full, this procedure is passed a const,
+        * so it can expect a pointer to an original value */
+       if (!first_bracket) {
+               ast_log(LOG_WARNING, "No opening bracket found in '%s'\n", src);
+               return 1;
+       }
+
+       if ((second_bracket = strchr(first_bracket, '>'))) {
+               *start = first_bracket;
+               *length = second_bracket - first_bracket;
+               return 0;
+       }
+       ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", src);
+       return -1;
+}
+
 int get_in_brackets_full(char *tmp,char **out,char **residue)
 {
        const char *parse = tmp;
@@ -1008,14 +1139,14 @@ int get_in_brackets_full(char *tmp,char **out,char **residue)
        only affects token based display-names there is no danger of brackets being in quotes */
        if (first_bracket) {
                parse = first_bracket;
-               } else {
+       } else {
                parse = tmp;
        }
 
        if ((second_bracket = strchr(parse, '>'))) {
                *second_bracket++ = '\0';
                if (out) {
-                       *out = first_bracket;
+                       *out = (char *) parse;
                }
                if (residue) {
                        *residue = second_bracket;
@@ -1024,9 +1155,9 @@ int get_in_brackets_full(char *tmp,char **out,char **residue)
        }
 
        if ((first_bracket)) {
-                       ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
+               ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
                return -1;
-               }
+       }
 
        if (out) {
                *out = tmp;
@@ -1048,13 +1179,14 @@ char *get_in_brackets(char *tmp)
 AST_TEST_DEFINE(get_in_brackets_test)
 {
        int res = AST_TEST_PASS;
-       char *in_brackets = "<sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
+       char in_brackets[] = "sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah";
        char no_name[] = "<sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
        char quoted_string[] = "\"I'm a quote stri><ng\" <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
        char missing_end_quote[] = "\"I'm a quote string <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
        char name_no_quotes[] = "name not in quotes <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
        char no_end_bracket[] = "name not in quotes <sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah";
        char no_name_no_brackets[] = "sip:name@host";
+       char missing_start_bracket[] = "sip:name:secret@host:port;transport=tcp?headers=testblah&headers2=blahblah>";
        char *uri = NULL;
 
        switch (cmd) {
@@ -1071,101 +1203,115 @@ AST_TEST_DEFINE(get_in_brackets_test)
        }
 
        /* Test 1, simple get in brackets */
-       if (!(uri = get_in_brackets(no_name)) || !(strcmp(uri, in_brackets))) {
-
-               ast_test_status_update(test, "Test 1, simple get in brackets failed.\n");
+       if (!(uri = get_in_brackets(no_name)) || strcmp(uri, in_brackets)) {
+               ast_test_status_update(test, "Test 1, simple get in brackets failed. %s\n", uri);
                res = AST_TEST_FAIL;
        }
 
        /* Test 2, starts with quoted string */
-       if (!(uri = get_in_brackets(quoted_string)) || !(strcmp(uri, in_brackets))) {
-
-               ast_test_status_update(test, "Test 2, get in brackets with quoted string in front failed.\n");
+       if (!(uri = get_in_brackets(quoted_string)) || strcmp(uri, in_brackets)) {
+               ast_test_status_update(test, "Test 2, get in brackets with quoted string in front failed. %s\n", uri);
                res = AST_TEST_FAIL;
        }
 
        /* Test 3, missing end quote */
-       if (!(uri = get_in_brackets(missing_end_quote)) || !(strcmp(uri, in_brackets))) {
-
-               ast_test_status_update(test, "Test 3, missing end quote failed.\n");
+       if (!(uri = get_in_brackets(missing_end_quote)) || !strcmp(uri, in_brackets)) {
+               ast_test_status_update(test, "Test 3, missing end quote failed. %s\n", uri);
                res = AST_TEST_FAIL;
        }
 
        /* Test 4, starts with a name not in quotes */
-       if (!(uri = get_in_brackets(name_no_quotes)) || !(strcmp(uri, in_brackets))) {
-
-               ast_test_status_update(test, "Test 4, passing name not in quotes failed.\n");
+       if (!(uri = get_in_brackets(name_no_quotes)) || strcmp(uri, in_brackets)) {
+               ast_test_status_update(test, "Test 4, passing name not in quotes failed. %s\n", uri);
                res = AST_TEST_FAIL;
        }
 
        /* Test 5, no end bracket, should just return everything after the first '<'  */
-       if (!(uri = get_in_brackets(no_end_bracket)) || !(strcmp(uri, in_brackets))) {
-
-               ast_test_status_update(test, "Test 5, no end bracket failed.\n");
+       if (!(uri = get_in_brackets(no_end_bracket)) || !strcmp(uri, in_brackets)) {
+               ast_test_status_update(test, "Test 5, no end bracket failed. %s\n", uri);
                res = AST_TEST_FAIL;
        }
 
        /* Test 6, NULL input  */
-       if ((uri = get_in_brackets(NULL))) {
-
+       if (get_in_brackets(NULL)) {
                ast_test_status_update(test, "Test 6, NULL input failed.\n");
                res = AST_TEST_FAIL;
        }
 
        /* Test 7, no name, and no brackets. */
-       if (!(uri = get_in_brackets(no_name_no_brackets)) || (strcmp(uri, "sip:name@host"))) {
-
+       if (!(uri = get_in_brackets(no_name_no_brackets)) || strcmp(uri, "sip:name@host")) {
                ast_test_status_update(test, "Test 7 failed. %s\n", uri);
                res = AST_TEST_FAIL;
        }
 
+       /* Test 8, no start bracket, but with ending bracket. */
+       if (!(uri = get_in_brackets(missing_start_bracket)) || strcmp(uri, in_brackets)) {
+               ast_test_status_update(test, "Test 8 failed. %s\n", uri);
+               res = AST_TEST_FAIL;
+       }
+
        return res;
 }
 
 
 int parse_name_andor_addr(char *uri, const char *scheme, char **name,
-                         char **user, char **pass, char **domain,
+                         char **user, char **pass, char **hostport,
                          struct uriparams *params, char **headers,
                          char **residue)
 {
        char buf[1024];
-       char **residue2=residue;
+       char **residue2 = residue;
+       char *orig_uri = uri;
        int ret;
+
+       buf[0] = '\0';
        if (name) {
-               get_calleridname(uri,buf,sizeof(buf));
-               *name = buf;
+               uri = (char *) get_calleridname(uri, buf, sizeof(buf));
        }
-       ret = get_in_brackets_full(uri,&uri,residue);
-       if (ret == 0) { /* uri is in brackets so do not treat unknown trailing uri parameters as potential messageheader parameters */
-               *residue = *residue + 1; /* step over the first semicolon so as per parse uri residue */
+       ret = get_in_brackets_full(uri, &uri, residue);
+       if (ret == 0) {
+               /*
+                * The uri is in brackets so do not treat unknown trailing uri
+                * parameters as potential message header parameters.
+                */
+               if (residue && **residue) {
+                       /* step over the first semicolon as per parse_uri_full residue */
+                       *residue = *residue + 1;
+               }
                residue2 = NULL;
        }
 
-       return parse_uri_full(uri, scheme, user, pass, domain, params, headers,
-                             residue2);
+       if (name) {
+               if (buf[0]) {
+                       /*
+                        * There is always room at orig_uri for the display-name because
+                        * at least one character has always been removed.  A '"' or '<'
+                        * has been removed.
+                        */
+                       strcpy(orig_uri, buf);
+                       *name = orig_uri;
+               } else {
+                       *name = "";
+               }
+       }
+
+       return parse_uri_full(uri, scheme, user, pass, hostport, params, headers, residue2);
 }
 
 AST_TEST_DEFINE(parse_name_andor_addr_test)
 {
        int res = AST_TEST_PASS;
        char uri[1024];
-       char *name, *user, *pass, *domain, *headers, *residue;
+       char *name, *user, *pass, *hostport, *headers, *residue;
        struct uriparams params;
 
        struct testdata {
                char *desc;
                char *uri;
-               char **nameptr;
-               char **userptr;
-               char **passptr;
-               char **domainptr;
-               char **headersptr;
-               char **residueptr;
-               struct uriparams *paramsptr;
                char *name;
                char *user;
                char *pass;
-               char *domain;
+               char *hostport;
                char *headers;
                char *residue;
                struct uriparams params;
@@ -1179,17 +1325,10 @@ AST_TEST_DEFINE(parse_name_andor_addr_test)
        struct testdata td1 = {
                .desc = "quotes and brackets",
                .uri = "\"name :@ \" <sip:user:secret@host:5060;param=discard;transport=tcp>;tag=tag",
-               .nameptr = &name,
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .name =  "name :@ ",
                .user = "user",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "",
                .residue = "tag=tag",
                .params.transport = "tcp",
@@ -1200,17 +1339,10 @@ AST_TEST_DEFINE(parse_name_andor_addr_test)
        struct testdata td2 = {
                .desc = "no quotes",
                .uri = "givenname familyname <sip:user:secret@host:5060;param=discard;transport=tcp>;expires=3600",
-               .nameptr = &name,
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .name = "givenname familyname",
                .user = "user",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "",
                .residue = "expires=3600",
                .params.transport = "tcp",
@@ -1221,17 +1353,10 @@ AST_TEST_DEFINE(parse_name_andor_addr_test)
        struct testdata td3 = {
                .desc = "no brackets",
                .uri = "sip:user:secret@host:5060;param=discard;transport=tcp;q=1",
-               .nameptr = &name,
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .name = "",
                .user = "user",
                .pass = "secret",
-               .domain = "host:5060",
+               .hostport = "host:5060",
                .headers = "",
                .residue = "q=1",
                .params.transport = "tcp",
@@ -1242,17 +1367,10 @@ AST_TEST_DEFINE(parse_name_andor_addr_test)
        struct testdata td4 = {
                .desc = "just host",
                .uri = "sips:host",
-               .nameptr = &name,
-               .userptr = &user,
-               .passptr = &pass,
-               .domainptr = &domain,
-               .headersptr = &headers,
-               .residueptr = &residue,
-               .paramsptr = &params,
                .name = "",
                .user = "",
                .pass = "",
-               .domain = "host",
+               .hostport = "host",
                .headers = "",
                .residue = "",
                .params.transport = "",
@@ -1281,37 +1399,37 @@ AST_TEST_DEFINE(parse_name_andor_addr_test)
        }
 
        AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
-               name = user = pass = domain = headers = residue = NULL;
+               name = user = pass = hostport = headers = residue = NULL;
                params.transport = params.user = params.method = params.ttl = params.maddr = NULL;
                params.lr = 0;
-       ast_copy_string(uri,testdataptr->uri,sizeof(uri));
+               ast_copy_string(uri,testdataptr->uri,sizeof(uri));
                if (parse_name_andor_addr(uri, "sip:,sips:",
-                                         testdataptr->nameptr,
-                                         testdataptr->userptr,
-                                         testdataptr->passptr,
-                                         testdataptr->domainptr,
-                                         testdataptr->paramsptr,
-                                         testdataptr->headersptr,
-                                         testdataptr->residueptr) ||
-                       ((testdataptr->nameptr) && strcmp(testdataptr->name, name)) ||
-                       ((testdataptr->userptr) && strcmp(testdataptr->user, user)) ||
-                       ((testdataptr->passptr) && strcmp(testdataptr->pass, pass)) ||
-                       ((testdataptr->domainptr) && strcmp(testdataptr->domain, domain)) ||
-                       ((testdataptr->headersptr) && strcmp(testdataptr->headers, headers)) ||
-                       ((testdataptr->residueptr) && strcmp(testdataptr->residue, residue)) ||
-                       ((testdataptr->paramsptr) && strcmp(testdataptr->params.transport,params.transport)) ||
-                       ((testdataptr->paramsptr) && strcmp(testdataptr->params.user,params.user))
-               ) {
-                               ast_test_status_update(test, "Sub-Test: %s,failed.\n", testdataptr->desc);
-                               res = AST_TEST_FAIL;
+                                         &name,
+                                         &user,
+                                         &pass,
+                                         &hostport,
+                                         &params,
+                                         &headers,
+                                         &residue) ||
+                       (name && strcmp(testdataptr->name, name)) ||
+                       (user && strcmp(testdataptr->user, user)) ||
+                       (pass && strcmp(testdataptr->pass, pass)) ||
+                       (hostport && strcmp(testdataptr->hostport, hostport)) ||
+                       (headers && strcmp(testdataptr->headers, headers)) ||
+                       (residue && strcmp(testdataptr->residue, residue)) ||
+                       (strcmp(testdataptr->params.transport,params.transport)) ||
+                       (strcmp(testdataptr->params.user,params.user))
+                       ) {
+                       ast_test_status_update(test, "Sub-Test: %s,failed.\n", testdataptr->desc);
+                       res = AST_TEST_FAIL;
                }
        }
 
-
        return res;
 }
 
-int get_comma(char *in, char **out) {
+int get_comma(char *in, char **out)
+{
        char *c;
        char *parse = in;
        if (out) {
@@ -1347,35 +1465,35 @@ int get_comma(char *in, char **out) {
        return 1;
 }
 
-int parse_contact_header(char *contactheader, struct contactliststruct *contactlist) {
+int parse_contact_header(char *contactheader, struct contactliststruct *contactlist)
+{
        int res;
        int last;
        char *comma;
        char *residue;
        char *param;
        char *value;
-       struct contact *contact=NULL;
+       struct contact *split_contact = NULL;
 
        if (*contactheader == '*') {
                return 1;
        }
 
-       contact = malloc(sizeof(*contact));
-
-       AST_LIST_HEAD_SET_NOLOCK(contactlist, contact);
-       while ((last = get_comma(contactheader,&comma)) != -1) {
+       split_contact = ast_calloc(1, sizeof(*split_contact));
 
+       AST_LIST_HEAD_SET_NOLOCK(contactlist, split_contact);
+       while ((last = get_comma(contactheader, &comma)) != -1) {
                res = parse_name_andor_addr(contactheader, "sip:,sips:",
-                                           &contact->name, &contact->user,
-                                           &contact->pass, &contact->domain,
-                                           &contact->params, &contact->headers,
-                                           &residue);
+                       &split_contact->name, &split_contact->user,
+                       &split_contact->pass, &split_contact->hostport,
+                       &split_contact->params, &split_contact->headers,
+                       &residue);
                if (res == -1) {
                        return res;
                }
 
                /* parse contact params */
-               contact->expires = contact->q = "";
+               split_contact->expires = split_contact->q = "";
 
                while ((value = strchr(residue,'='))) {
                        *value++ = '\0';
@@ -1388,20 +1506,19 @@ int parse_contact_header(char *contactheader, struct contactliststruct *contactl
                        }
 
                        if (!strcmp(param,"expires")) {
-                               contact->expires = value;
+                               split_contact->expires = value;
                        } else if (!strcmp(param,"q")) {
-                               contact->q = value;
+                               split_contact->q = value;
                        }
                }
 
-               if(last) {
+               if (last) {
                        return 0;
                }
                contactheader = comma;
 
-               contact = malloc(sizeof(*contact));
-               AST_LIST_INSERT_TAIL(contactlist, contact, list);
-
+               split_contact = ast_calloc(1, sizeof(*split_contact));
+               AST_LIST_INSERT_TAIL(contactlist, split_contact, list);
        }
        return last;
 }
@@ -1440,7 +1557,7 @@ AST_TEST_DEFINE(parse_contact_header_test)
                .name = "name :@;?&,",
                .user = "user",
                .pass = "secret",
-               .domain = "host:5082",
+               .hostport = "host:5082",
                .params.transport = "tcp",
                .params.ttl = "",
                .params.lr = 0,
@@ -1459,7 +1576,7 @@ AST_TEST_DEFINE(parse_contact_header_test)
                .name = "",
                .user = ",user1,",
                .pass = ",secret1,",
-               .domain = "host1",
+               .hostport = "host1",
                .params.transport = "",
                .params.ttl = "7",
                .params.lr = 0,
@@ -1471,7 +1588,7 @@ AST_TEST_DEFINE(parse_contact_header_test)
                .name = "",
                .user = "",
                .pass = "",
-               .domain = "host2",
+               .hostport = "host2",
                .params.transport = "",
                .params.ttl = "",
                .params.lr = 0,
@@ -1527,23 +1644,26 @@ AST_TEST_DEFINE(parse_contact_header_test)
                                        strcmp(tdcontactptr->name, contactptr->name) ||
                                        strcmp(tdcontactptr->user, contactptr->user) ||
                                        strcmp(tdcontactptr->pass, contactptr->pass) ||
-                                       strcmp(tdcontactptr->domain, contactptr->domain) ||
+                                       strcmp(tdcontactptr->hostport, contactptr->hostport) ||
                                        strcmp(tdcontactptr->headers, contactptr->headers) ||
                                        strcmp(tdcontactptr->expires, contactptr->expires) ||
                                        strcmp(tdcontactptr->q, contactptr->q) ||
                                        strcmp(tdcontactptr->params.transport, contactptr->params.transport) ||
                                        strcmp(tdcontactptr->params.ttl, contactptr->params.ttl) ||
                                        (tdcontactptr->params.lr != contactptr->params.lr)
-                               ) {
+                                       ) {
                                        ast_test_status_update(test, "Sub-Test: %s,failed.\n", testdataptr->desc);
                                        res = AST_TEST_FAIL;
                                        break;
                                }
 
-                       contactptr = AST_LIST_NEXT(contactptr,list);
+                               contactptr = AST_LIST_NEXT(contactptr,list);
                        }
-               }
 
+                       while ((contactptr = AST_LIST_REMOVE_HEAD(contactlistptr,list))) {
+                               ast_free(contactptr);
+                       }
+               }
        }
 
        return res;
@@ -1557,9 +1677,9 @@ AST_TEST_DEFINE(parse_contact_header_test)
  * item is found that is not supported, it is copied to the unsupported
  * out buffer.
  *
- * \param option list
+ * \param options list
  * \param unsupported out buffer (optional)
- * \param unsupported out buffer length (optional)
+ * \param unsupported_len out buffer length (optional)
  */
 unsigned int parse_sip_options(const char *options, char *unsupported, size_t unsupported_len)
 {
@@ -2253,10 +2373,7 @@ void free_via(struct sip_via *v)
                return;
        }
 
-       if (v->via) {
-               ast_free(v->via);
-       }
-
+       ast_free(v->via);
        ast_free(v);
 }
 
@@ -2305,8 +2422,9 @@ struct sip_via *parse_via(const char *header)
        }
        v->sent_by = ast_skip_blanks(v->sent_by);
 
-       /* store the port */
-       if ((parm = strchr(v->sent_by, ':'))) {
+       /* store the port, we have to handle ipv6 addresses containing ':'
+        * characters gracefully */
+       if (((parm = strchr(v->sent_by, ']')) && *(++parm) == ':') || (parm = strchr(v->sent_by, ':'))) {
                char *endptr;
 
                v->port = strtol(++parm, &endptr, 10);
@@ -2392,6 +2510,13 @@ AST_TEST_DEFINE(parse_via_test)
                .expected_maddr = "224.0.0.1",
                .expected_ttl = 1,
        };
+       struct testdata t7 = {
+               .in = "SIP/2.0/UDP [::1]:5060",
+               .expected_protocol = "SIP/2.0/UDP",
+               .expected_sent_by = "[::1]:5060",
+               .expected_port = 5060,
+               .expected_branch = "",
+       };
        switch (cmd) {
        case TEST_INIT:
                info->name = "parse_via_test";
@@ -2411,6 +2536,7 @@ AST_TEST_DEFINE(parse_via_test)
        AST_LIST_INSERT_TAIL(&testdatalist, &t4, list);
        AST_LIST_INSERT_TAIL(&testdatalist, &t5, list);
        AST_LIST_INSERT_TAIL(&testdatalist, &t6, list);
+       AST_LIST_INSERT_TAIL(&testdatalist, &t7, list);
 
 
        AST_LIST_TRAVERSE(&testdatalist, testdataptr, list) {
@@ -2460,8 +2586,8 @@ AST_TEST_DEFINE(parse_via_test)
 
                if (testdataptr->expected_port && testdataptr->expected_port != via->port) {
                        ast_test_status_update(test, "TEST#%d FAILED: VIA = \"%s\"\n"
-                               "parsed port = \"%d\"\n"
-                               "expected = \"%d\"\n"
+                               "parsed port = \"%u\"\n"
+                               "expected = \"%u\"\n"
                                "failed to parse port\n",
                        i, testdataptr->in, via->port, testdataptr->expected_port);
                        res = AST_TEST_FAIL;
@@ -2510,7 +2636,7 @@ void sip_request_parser_register_tests(void)
        AST_TEST_REGISTER(sip_parse_uri_test);
        AST_TEST_REGISTER(get_in_brackets_test);
        AST_TEST_REGISTER(get_name_and_number_test);
-       AST_TEST_REGISTER(sip_parse_uri_fully_test);
+       AST_TEST_REGISTER(sip_parse_uri_full_test);
        AST_TEST_REGISTER(parse_name_andor_addr_test);
        AST_TEST_REGISTER(parse_contact_header_test);
        AST_TEST_REGISTER(sip_parse_options_test);
@@ -2523,7 +2649,7 @@ void sip_request_parser_unregister_tests(void)
        AST_TEST_UNREGISTER(get_calleridname_test);
        AST_TEST_UNREGISTER(get_in_brackets_test);
        AST_TEST_UNREGISTER(get_name_and_number_test);
-       AST_TEST_UNREGISTER(sip_parse_uri_fully_test);
+       AST_TEST_UNREGISTER(sip_parse_uri_full_test);
        AST_TEST_UNREGISTER(parse_name_andor_addr_test);
        AST_TEST_UNREGISTER(parse_contact_header_test);
        AST_TEST_UNREGISTER(sip_parse_options_test);