From 060d7cae917d9df4fe97c1339c68df3b00ef5482 Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Sat, 17 Sep 2016 19:19:50 +0200 Subject: [PATCH 01/10] Support for last_XX:value, last_XX:uri, last_XX:param.tag --- include/call.hpp | 3 +- regress/github-#0260/run | 35 +++++++++++++ regress/github-#0260/uac.xml | 54 ++++++++++++++++++++ regress/github-#0260/uas.xml | 51 +++++++++++++++++++ src/call.cpp | 99 ++++++++++++++++++++++++++++++++---- 5 files changed, 232 insertions(+), 10 deletions(-) create mode 100755 regress/github-#0260/run create mode 100644 regress/github-#0260/uac.xml create mode 100644 regress/github-#0260/uas.xml diff --git a/include/call.hpp b/include/call.hpp index 2861dbda..179714ea 100644 --- a/include/call.hpp +++ b/include/call.hpp @@ -291,7 +291,8 @@ class call : virtual public task, virtual public listener, public virtual socket char * get_header_field_code(const char * msg, const char * code); char * get_last_header(const char * name); - char * get_last_request_uri(); + char * get_last_tag (const char *name); + char * get_last_request_uri(const char *name); unsigned long hash(const char * msg); typedef std::map file_line_map; diff --git a/regress/github-#0260/run b/regress/github-#0260/run new file mode 100755 index 00000000..c21a0f62 --- /dev/null +++ b/regress/github-#0260/run @@ -0,0 +1,35 @@ +#!/bin/sh +# This regression test is a part of SIPp. +# Author: Pietro Bertera, Snom Technology AG, 2016 + +. "`dirname "$0"`/../functions"; init + +sippbg -m 1 -sf uas.xml -p 5070 -i 127.0.0.1 \ + -timeout 4 -timeout_error -trace_logs \ + -log_file log.log >/dev/null 2>&1 + +job=$! + +sippfg -m 1 -sf uac.xml 127.0.0.1:5070 \ + -timeout 4 -timeout_error >/dev/null 2>&1 + +status=$? +wait $job || status=1 + +if test $status -eq 0; then + if grep -e ^LAST\ From:\ \"Tom\ Jones\"\ \\;tag=SIPpTag001$ log.log > /dev/null; then + if grep -e ^LAST\ To:uri:\ sip:cheese@paris.fr$ log.log > /dev/null; then + if grep -e ^LAST\ From-tag:\ SIPpTag001$ log.log > /dev/null; then + ok + else + fail "From:param.tag not found" + fi + else + fail "To:uri not found" + fi + else + fail "From header not found" + fi +else + fail "process failure" +fi diff --git a/regress/github-#0260/uac.xml b/regress/github-#0260/uac.xml new file mode 100644 index 00000000..f06d2430 --- /dev/null +++ b/regress/github-#0260/uac.xml @@ -0,0 +1,54 @@ + + + + + ;tag=SIPpTag00[call_number] + To: "Fromage" + Call-ID: [call_id] + CSeq: 1 INVITE + Contact: + Content-Length: 0 + + ]]> + + + + + + ;tag=SIPpTag00[call_number] + To: "Fromage" [peer_tag_param] + Call-ID: [call_id] + CSeq: 1 ACK + Contact: sip:sipp@[local_ip]:[local_port] + Content-Length: 0 + + ]]> + + + + + + + Content-Length: 0 + + ]]> + + + + diff --git a/regress/github-#0260/uas.xml b/regress/github-#0260/uas.xml new file mode 100644 index 00000000..f4d32bda --- /dev/null +++ b/regress/github-#0260/uas.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/call.cpp b/src/call.cpp index 457d3559..750d2bfb 100644 --- a/src/call.cpp +++ b/src/call.cpp @@ -923,33 +923,72 @@ char * call::get_last_header(const char * name) if (name[len - 1] == ':') { return get_header(last_recv_msg, name, false); } else { + char with_colon[MAX_HEADER_LEN]; - sprintf(with_colon, "%s:", name); - return get_header(last_recv_msg, with_colon, false); + const char *sep = strrchr(name, ':'); + + if (!sep){ + sprintf(with_colon, "%s:", name); + return get_header(last_recv_msg, with_colon, false); + } else { + if (!strcmp(sep, ":value")) { + snprintf(with_colon, len - 4, "%s", name); + return get_header(last_recv_msg, with_colon, true); + } + if (!strcmp(sep, ":uri")) { + snprintf(with_colon, len - 2, "%s", name); + char * last_header_uri = get_last_request_uri(with_colon); + char * ret; + if (!(ret = (char *)malloc(strlen(last_header_uri + 1)))){ + ERROR("call::get_last_header: Cannot allocate uri !\n"); + } + sprintf(ret, "%s", last_header_uri); + free(last_header_uri); + return ret; + } + if (!strcmp(sep, ":param.tag")) { + snprintf(with_colon, len - 8, "%s", name); + char * last_tag = get_last_tag(with_colon); + char * ret; + if (!(ret = (char *)malloc(strlen(last_tag + 1)))){ + ERROR("call::get_last_header: Cannot allocate param.tag !\n"); + } + sprintf(ret, "%s", last_tag); + free(last_tag); + return ret; + } + ERROR("Token %s not valid in %s", sep, name); + } } } -/* Return the last request URI from the To header. On any error returns the +/* Return the last request URI from the header. On any error returns the * empty string. The caller must free the result. */ -char * call::get_last_request_uri() +char * call::get_last_request_uri(const char *name) { char * tmp; char * tmp2; char * last_request_uri; + char * last_header; int tmp_len; - char * last_To = get_last_header("To:"); - if (!last_To) { + if (!name || !strlen(name)) { return strdup(""); } - tmp = strchr(last_To, '<'); + last_header = get_last_header(name); + + if (!last_header) { + return strdup(""); + } + + tmp = strchr(last_header, '<'); if (!tmp) { return strdup(""); } tmp++; - tmp2 = strchr(last_To, '>'); + tmp2 = strchr(last_header, '>'); if (!tmp2) { return strdup(""); } @@ -972,6 +1011,48 @@ char * call::get_last_request_uri() return last_request_uri; } +/* Return the last tag from the header line. On any error returns the + * empty string. The caller must free the result. */ +char * call::get_last_tag(const char *name) +{ + char * tmp; + char * tmp2; + char * result; + char * last_header; + int tmp_len; + + if (!name || !strlen(name)) { + return strdup(""); + } + + last_header = get_last_header(name); + if (!last_header) { + return strdup(""); + } + + tmp = strcasestr(last_header, "tag="); + if (!tmp) { + return strdup(""); + } + + tmp += strlen("tag="); + tmp2 = tmp; + + while (*tmp2 && *tmp2 != ';' && *tmp2!='\r' && *tmp2!='\n') tmp2++; + + tmp_len = strlen(tmp) - strlen(tmp2); + if (tmp_len < 0) { + return strdup(""); + } + if(!(result = (char *) malloc(tmp_len+1))) ERROR("call::get_last_tag: Cannot allocate !\n"); + memset(result, 0, sizeof(&result)); + if(tmp && (tmp_len > 0)){ + strncpy(result, tmp, tmp_len); + } + result[tmp_len] = '\0'; + return result; +} + char * call::send_scene(int index, int *send_status, int *len) { #define MAX_MSG_NAME_SIZE 30 @@ -2201,7 +2282,7 @@ char* call::createSendingMessage(SendingMessage *src, int P_index, char *msg_buf } break; case E_Message_Last_Request_URI: { - char * last_request_uri = get_last_request_uri(); + char * last_request_uri = get_last_request_uri("To:"); dest += sprintf(dest, "%s", last_request_uri); free(last_request_uri); break; From 909cae75d3fe3d6efa5ea2b6eef25cde4e2e291e Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Sat, 17 Sep 2016 19:19:50 +0200 Subject: [PATCH 02/10] Support for last_XX:value, last_XX:uri, last_XX:param.tag --- include/call.hpp | 3 +- regress/github-#0260/run | 35 +++++++++++++ regress/github-#0260/uac.xml | 54 ++++++++++++++++++++ regress/github-#0260/uas.xml | 51 +++++++++++++++++++ src/call.cpp | 99 ++++++++++++++++++++++++++++++++---- 5 files changed, 232 insertions(+), 10 deletions(-) create mode 100755 regress/github-#0260/run create mode 100644 regress/github-#0260/uac.xml create mode 100644 regress/github-#0260/uas.xml diff --git a/include/call.hpp b/include/call.hpp index 2861dbda..179714ea 100644 --- a/include/call.hpp +++ b/include/call.hpp @@ -291,7 +291,8 @@ class call : virtual public task, virtual public listener, public virtual socket char * get_header_field_code(const char * msg, const char * code); char * get_last_header(const char * name); - char * get_last_request_uri(); + char * get_last_tag (const char *name); + char * get_last_request_uri(const char *name); unsigned long hash(const char * msg); typedef std::map file_line_map; diff --git a/regress/github-#0260/run b/regress/github-#0260/run new file mode 100755 index 00000000..c21a0f62 --- /dev/null +++ b/regress/github-#0260/run @@ -0,0 +1,35 @@ +#!/bin/sh +# This regression test is a part of SIPp. +# Author: Pietro Bertera, Snom Technology AG, 2016 + +. "`dirname "$0"`/../functions"; init + +sippbg -m 1 -sf uas.xml -p 5070 -i 127.0.0.1 \ + -timeout 4 -timeout_error -trace_logs \ + -log_file log.log >/dev/null 2>&1 + +job=$! + +sippfg -m 1 -sf uac.xml 127.0.0.1:5070 \ + -timeout 4 -timeout_error >/dev/null 2>&1 + +status=$? +wait $job || status=1 + +if test $status -eq 0; then + if grep -e ^LAST\ From:\ \"Tom\ Jones\"\ \\;tag=SIPpTag001$ log.log > /dev/null; then + if grep -e ^LAST\ To:uri:\ sip:cheese@paris.fr$ log.log > /dev/null; then + if grep -e ^LAST\ From-tag:\ SIPpTag001$ log.log > /dev/null; then + ok + else + fail "From:param.tag not found" + fi + else + fail "To:uri not found" + fi + else + fail "From header not found" + fi +else + fail "process failure" +fi diff --git a/regress/github-#0260/uac.xml b/regress/github-#0260/uac.xml new file mode 100644 index 00000000..f06d2430 --- /dev/null +++ b/regress/github-#0260/uac.xml @@ -0,0 +1,54 @@ + + + + + ;tag=SIPpTag00[call_number] + To: "Fromage" + Call-ID: [call_id] + CSeq: 1 INVITE + Contact: + Content-Length: 0 + + ]]> + + + + + + ;tag=SIPpTag00[call_number] + To: "Fromage" [peer_tag_param] + Call-ID: [call_id] + CSeq: 1 ACK + Contact: sip:sipp@[local_ip]:[local_port] + Content-Length: 0 + + ]]> + + + + + + + Content-Length: 0 + + ]]> + + + + diff --git a/regress/github-#0260/uas.xml b/regress/github-#0260/uas.xml new file mode 100644 index 00000000..f4d32bda --- /dev/null +++ b/regress/github-#0260/uas.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/call.cpp b/src/call.cpp index 457d3559..750d2bfb 100644 --- a/src/call.cpp +++ b/src/call.cpp @@ -923,33 +923,72 @@ char * call::get_last_header(const char * name) if (name[len - 1] == ':') { return get_header(last_recv_msg, name, false); } else { + char with_colon[MAX_HEADER_LEN]; - sprintf(with_colon, "%s:", name); - return get_header(last_recv_msg, with_colon, false); + const char *sep = strrchr(name, ':'); + + if (!sep){ + sprintf(with_colon, "%s:", name); + return get_header(last_recv_msg, with_colon, false); + } else { + if (!strcmp(sep, ":value")) { + snprintf(with_colon, len - 4, "%s", name); + return get_header(last_recv_msg, with_colon, true); + } + if (!strcmp(sep, ":uri")) { + snprintf(with_colon, len - 2, "%s", name); + char * last_header_uri = get_last_request_uri(with_colon); + char * ret; + if (!(ret = (char *)malloc(strlen(last_header_uri + 1)))){ + ERROR("call::get_last_header: Cannot allocate uri !\n"); + } + sprintf(ret, "%s", last_header_uri); + free(last_header_uri); + return ret; + } + if (!strcmp(sep, ":param.tag")) { + snprintf(with_colon, len - 8, "%s", name); + char * last_tag = get_last_tag(with_colon); + char * ret; + if (!(ret = (char *)malloc(strlen(last_tag + 1)))){ + ERROR("call::get_last_header: Cannot allocate param.tag !\n"); + } + sprintf(ret, "%s", last_tag); + free(last_tag); + return ret; + } + ERROR("Token %s not valid in %s", sep, name); + } } } -/* Return the last request URI from the To header. On any error returns the +/* Return the last request URI from the header. On any error returns the * empty string. The caller must free the result. */ -char * call::get_last_request_uri() +char * call::get_last_request_uri(const char *name) { char * tmp; char * tmp2; char * last_request_uri; + char * last_header; int tmp_len; - char * last_To = get_last_header("To:"); - if (!last_To) { + if (!name || !strlen(name)) { return strdup(""); } - tmp = strchr(last_To, '<'); + last_header = get_last_header(name); + + if (!last_header) { + return strdup(""); + } + + tmp = strchr(last_header, '<'); if (!tmp) { return strdup(""); } tmp++; - tmp2 = strchr(last_To, '>'); + tmp2 = strchr(last_header, '>'); if (!tmp2) { return strdup(""); } @@ -972,6 +1011,48 @@ char * call::get_last_request_uri() return last_request_uri; } +/* Return the last tag from the header line. On any error returns the + * empty string. The caller must free the result. */ +char * call::get_last_tag(const char *name) +{ + char * tmp; + char * tmp2; + char * result; + char * last_header; + int tmp_len; + + if (!name || !strlen(name)) { + return strdup(""); + } + + last_header = get_last_header(name); + if (!last_header) { + return strdup(""); + } + + tmp = strcasestr(last_header, "tag="); + if (!tmp) { + return strdup(""); + } + + tmp += strlen("tag="); + tmp2 = tmp; + + while (*tmp2 && *tmp2 != ';' && *tmp2!='\r' && *tmp2!='\n') tmp2++; + + tmp_len = strlen(tmp) - strlen(tmp2); + if (tmp_len < 0) { + return strdup(""); + } + if(!(result = (char *) malloc(tmp_len+1))) ERROR("call::get_last_tag: Cannot allocate !\n"); + memset(result, 0, sizeof(&result)); + if(tmp && (tmp_len > 0)){ + strncpy(result, tmp, tmp_len); + } + result[tmp_len] = '\0'; + return result; +} + char * call::send_scene(int index, int *send_status, int *len) { #define MAX_MSG_NAME_SIZE 30 @@ -2201,7 +2282,7 @@ char* call::createSendingMessage(SendingMessage *src, int P_index, char *msg_buf } break; case E_Message_Last_Request_URI: { - char * last_request_uri = get_last_request_uri(); + char * last_request_uri = get_last_request_uri("To:"); dest += sprintf(dest, "%s", last_request_uri); free(last_request_uri); break; From 6a3ec98a4d75d426b5aa46cab495e01952382dd1 Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Fri, 7 Oct 2016 02:22:21 +0200 Subject: [PATCH 03/10] Code review --- include/call.hpp | 3 +- include/sip_parser.hpp | 1 + regress/github-#0260/run | 30 +++++----- regress/github-#0260/uas.xml | 18 +++--- src/call.cpp | 112 ++++++++++------------------------- src/sip_parser.cpp | 11 +++- 6 files changed, 66 insertions(+), 109 deletions(-) diff --git a/include/call.hpp b/include/call.hpp index 179714ea..86f6e666 100644 --- a/include/call.hpp +++ b/include/call.hpp @@ -291,8 +291,7 @@ class call : virtual public task, virtual public listener, public virtual socket char * get_header_field_code(const char * msg, const char * code); char * get_last_header(const char * name); - char * get_last_tag (const char *name); - char * get_last_request_uri(const char *name); + char * get_last_header_uri(const char *name); unsigned long hash(const char * msg); typedef std::map file_line_map; diff --git a/include/sip_parser.hpp b/include/sip_parser.hpp index c059815e..f5a02420 100644 --- a/include/sip_parser.hpp +++ b/include/sip_parser.hpp @@ -26,6 +26,7 @@ #define MAX_HEADER_LEN 2049 char *get_call_id(const char *msg); +char *get_param_tag(const char *msg, const char *name, const char *shortname); char *get_peer_tag(const char *msg); int get_method(char *msg); diff --git a/regress/github-#0260/run b/regress/github-#0260/run index c21a0f62..28a843f8 100755 --- a/regress/github-#0260/run +++ b/regress/github-#0260/run @@ -10,26 +10,24 @@ sippbg -m 1 -sf uas.xml -p 5070 -i 127.0.0.1 \ job=$! -sippfg -m 1 -sf uac.xml 127.0.0.1:5070 \ +sippfg -m 1 -sf uac.xml 127.0.0.1:5070 -trace_msg \ -timeout 4 -timeout_error >/dev/null 2>&1 status=$? wait $job || status=1 -if test $status -eq 0; then - if grep -e ^LAST\ From:\ \"Tom\ Jones\"\ \\;tag=SIPpTag001$ log.log > /dev/null; then - if grep -e ^LAST\ To:uri:\ sip:cheese@paris.fr$ log.log > /dev/null; then - if grep -e ^LAST\ From-tag:\ SIPpTag001$ log.log > /dev/null; then - ok - else - fail "From:param.tag not found" - fi - else - fail "To:uri not found" - fi - else - fail "From header not found" - fi -else + +if test $status -ne 0; then fail "process failure" +elif ! grep -e ^LAST\ Contact\|Contact:\ \ log.log > /dev/null; then + fail "[last_Contact] header not found" +elif ! grep -e ^LAST\ Contact:\|Contact:\ \ log.log > /dev/null; then + fail "[last_Contact:] header not found" +elif ! grep -e ^LAST\ From:value\|\"Tom\ Jones\"\ \\;tag=SIPpTag001$ log.log > /dev/null; then + fail "[last_From:value] not found" +elif ! grep -e ^LAST\ To:uri\|sip:cheese@paris.fr$ log.log > /dev/null; then + fail "[last_To:uri] not found" +elif ! grep -e ^LAST\ From:param.tag\|SIPpTag001$ log.log > /dev/null; then + fail "[last_From:param.tag] not found" fi +ok diff --git a/regress/github-#0260/uas.xml b/regress/github-#0260/uas.xml index f4d32bda..ffcc1331 100644 --- a/regress/github-#0260/uas.xml +++ b/regress/github-#0260/uas.xml @@ -7,7 +7,8 @@ - - - - - - - + + + + + + + + diff --git a/src/call.cpp b/src/call.cpp index 750d2bfb..65dcf38a 100644 --- a/src/call.cpp +++ b/src/call.cpp @@ -920,51 +920,45 @@ char * call::get_last_header(const char * name) ERROR("call::get_last_header: Header to parse bigger than %d (%zu)", MAX_HEADER_LEN, strlen(name)); } + char header_name[MAX_HEADER_LEN]; + const char *sep = strrchr(name, ':'); + + if (!sep){ /* [last_Header] */ + sprintf(header_name, "%s:", name); + return get_header(last_recv_msg, header_name, false); + } + + snprintf(header_name, len - strlen(sep) + 2, name); + + /* [last_Header:] */ if (name[len - 1] == ':') { return get_header(last_recv_msg, name, false); + } + char *value = get_header(last_recv_msg, header_name, true); + + if (!strcmp(sep, ":value")) { /* [last_Header:value] */ + ; /* done */ + } else if (!strcmp(sep, ":uri")) { /* [last_Header:uri] */ + char * uri = get_last_header_uri(header_name); + int uri_len = strlen(uri); + memmove(value, uri, uri_len); + value[uri_len] = '\0'; + free(uri); + } else if (!strcmp(sep, ":param.tag")) { /* [last_Header:param.tag] */ + snprintf(header_name, len - 9, "%s", name); + char * last_tag = get_param_tag(last_recv_msg, header_name, ""); + int last_tag_len = strlen(last_tag); + memmove(value, last_tag, last_tag_len); + value[last_tag_len] = '\0'; } else { - - char with_colon[MAX_HEADER_LEN]; - const char *sep = strrchr(name, ':'); - - if (!sep){ - sprintf(with_colon, "%s:", name); - return get_header(last_recv_msg, with_colon, false); - } else { - if (!strcmp(sep, ":value")) { - snprintf(with_colon, len - 4, "%s", name); - return get_header(last_recv_msg, with_colon, true); - } - if (!strcmp(sep, ":uri")) { - snprintf(with_colon, len - 2, "%s", name); - char * last_header_uri = get_last_request_uri(with_colon); - char * ret; - if (!(ret = (char *)malloc(strlen(last_header_uri + 1)))){ - ERROR("call::get_last_header: Cannot allocate uri !\n"); - } - sprintf(ret, "%s", last_header_uri); - free(last_header_uri); - return ret; - } - if (!strcmp(sep, ":param.tag")) { - snprintf(with_colon, len - 8, "%s", name); - char * last_tag = get_last_tag(with_colon); - char * ret; - if (!(ret = (char *)malloc(strlen(last_tag + 1)))){ - ERROR("call::get_last_header: Cannot allocate param.tag !\n"); - } - sprintf(ret, "%s", last_tag); - free(last_tag); - return ret; - } - ERROR("Token %s not valid in %s", sep, name); - } + ERROR("Token %s not valid in %s", sep, name); } + return value; } /* Return the last request URI from the header. On any error returns the * empty string. The caller must free the result. */ -char * call::get_last_request_uri(const char *name) +char * call::get_last_header_uri(const char *name) { char * tmp; char * tmp2; @@ -1011,48 +1005,6 @@ char * call::get_last_request_uri(const char *name) return last_request_uri; } -/* Return the last tag from the header line. On any error returns the - * empty string. The caller must free the result. */ -char * call::get_last_tag(const char *name) -{ - char * tmp; - char * tmp2; - char * result; - char * last_header; - int tmp_len; - - if (!name || !strlen(name)) { - return strdup(""); - } - - last_header = get_last_header(name); - if (!last_header) { - return strdup(""); - } - - tmp = strcasestr(last_header, "tag="); - if (!tmp) { - return strdup(""); - } - - tmp += strlen("tag="); - tmp2 = tmp; - - while (*tmp2 && *tmp2 != ';' && *tmp2!='\r' && *tmp2!='\n') tmp2++; - - tmp_len = strlen(tmp) - strlen(tmp2); - if (tmp_len < 0) { - return strdup(""); - } - if(!(result = (char *) malloc(tmp_len+1))) ERROR("call::get_last_tag: Cannot allocate !\n"); - memset(result, 0, sizeof(&result)); - if(tmp && (tmp_len > 0)){ - strncpy(result, tmp, tmp_len); - } - result[tmp_len] = '\0'; - return result; -} - char * call::send_scene(int index, int *send_status, int *len) { #define MAX_MSG_NAME_SIZE 30 @@ -2282,7 +2234,7 @@ char* call::createSendingMessage(SendingMessage *src, int P_index, char *msg_buf } break; case E_Message_Last_Request_URI: { - char * last_request_uri = get_last_request_uri("To:"); + char * last_request_uri = get_last_header_uri("To:"); dest += sprintf(dest, "%s", last_request_uri); free(last_request_uri); break; diff --git a/src/sip_parser.cpp b/src/sip_parser.cpp index ebc00241..09dab5a4 100644 --- a/src/sip_parser.cpp +++ b/src/sip_parser.cpp @@ -70,7 +70,7 @@ static const char *internal_hdrend(const char *ptr); /*************************** Mini SIP parser (externals) ***************/ -char * get_peer_tag(const char *msg) +char * get_param_tag(const char *msg, const char *name, const char *shortname) { static char tag[MAX_HEADER_LEN]; const char * to_hdr; @@ -78,9 +78,9 @@ char * get_peer_tag(const char *msg) int tag_i = 0; /* Find start of header */ - to_hdr = internal_find_header(msg, "To", "t", true); + to_hdr = internal_find_header(msg, name, shortname, true); if (!to_hdr) { - WARNING("No valid To: header in reply"); + WARNING("No valid %s: header in reply", name); return NULL; } @@ -109,6 +109,11 @@ char * get_peer_tag(const char *msg) return tag; } +char * get_peer_tag(const char *msg) +{ + return get_param_tag(msg, "To", "t"); +} + char * get_header_content(const char* message, const char * name) { return get_header(message, name, true); From 33449407e2357422a4ea5ba06b16423c5ba6f2cf Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Fri, 7 Oct 2016 02:28:18 +0200 Subject: [PATCH 04/10] Fixed regtest --- regress/github-#0260/run | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/regress/github-#0260/run b/regress/github-#0260/run index 28a843f8..4ea4bf58 100755 --- a/regress/github-#0260/run +++ b/regress/github-#0260/run @@ -10,7 +10,7 @@ sippbg -m 1 -sf uas.xml -p 5070 -i 127.0.0.1 \ job=$! -sippfg -m 1 -sf uac.xml 127.0.0.1:5070 -trace_msg \ +sippfg -m 1 -sf uac.xml 127.0.0.1:5070 \ -timeout 4 -timeout_error >/dev/null 2>&1 status=$? From 9442f8d061739d9ae5b2cbbb7ff052a739d24266 Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Fri, 7 Oct 2016 19:38:09 +0200 Subject: [PATCH 05/10] testcase debugging for travis falure --- regress/github-#0260/run | 3 +++ 1 file changed, 3 insertions(+) diff --git a/regress/github-#0260/run b/regress/github-#0260/run index 4ea4bf58..f5952963 100755 --- a/regress/github-#0260/run +++ b/regress/github-#0260/run @@ -16,6 +16,9 @@ sippfg -m 1 -sf uac.xml 127.0.0.1:5070 \ status=$? wait $job || status=1 +cat log.log + +set -x if test $status -ne 0; then fail "process failure" From a6575cde23d8f6bc4a746800438be039e06ed520 Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Fri, 7 Oct 2016 19:48:22 +0200 Subject: [PATCH 06/10] travis testcase fix --- regress/github-#0260/run | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/regress/github-#0260/run b/regress/github-#0260/run index f5952963..572834ce 100755 --- a/regress/github-#0260/run +++ b/regress/github-#0260/run @@ -10,7 +10,7 @@ sippbg -m 1 -sf uas.xml -p 5070 -i 127.0.0.1 \ job=$! -sippfg -m 1 -sf uac.xml 127.0.0.1:5070 \ +sippfg -m 1 -sf -i 127.0.0.1 -p 5060 uac.xml 127.0.0.1:5070 \ -timeout 4 -timeout_error >/dev/null 2>&1 status=$? From b82bfff36bcd2e7a089ec519d608ee0d30df7c2b Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Fri, 7 Oct 2016 19:56:56 +0200 Subject: [PATCH 07/10] fixed regtest --- regress/github-#0260/run | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/regress/github-#0260/run b/regress/github-#0260/run index 572834ce..cbd3796f 100755 --- a/regress/github-#0260/run +++ b/regress/github-#0260/run @@ -10,21 +10,18 @@ sippbg -m 1 -sf uas.xml -p 5070 -i 127.0.0.1 \ job=$! -sippfg -m 1 -sf -i 127.0.0.1 -p 5060 uac.xml 127.0.0.1:5070 \ +sippfg -m 1 -sf uac.xml -i 127.0.0.1 -p 5060 127.0.0.1:5070 \ -timeout 4 -timeout_error >/dev/null 2>&1 status=$? wait $job || status=1 -cat log.log - -set -x if test $status -ne 0; then fail "process failure" -elif ! grep -e ^LAST\ Contact\|Contact:\ \ log.log > /dev/null; then +elif ! grep -e ^LAST\ Contact\|Contact:\ \ log.log > /dev/null; then fail "[last_Contact] header not found" -elif ! grep -e ^LAST\ Contact:\|Contact:\ \ log.log > /dev/null; then +elif ! grep -e ^LAST\ Contact:\|Contact:\ \ log.log > /dev/null; then fail "[last_Contact:] header not found" elif ! grep -e ^LAST\ From:value\|\"Tom\ Jones\"\ \\;tag=SIPpTag001$ log.log > /dev/null; then fail "[last_From:value] not found" @@ -33,4 +30,5 @@ elif ! grep -e ^LAST\ To:uri\|sip:cheese@paris.fr$ log.log > /dev/null; then elif ! grep -e ^LAST\ From:param.tag\|SIPpTag001$ log.log > /dev/null; then fail "[last_From:param.tag] not found" fi + ok From 8a4ad217f983d61eef3f3d7457d0caa8a431686c Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Thu, 20 Oct 2016 22:17:30 +0200 Subject: [PATCH 08/10] Fixed github-#0260/run --- regress/github-#0260/run | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/regress/github-#0260/run b/regress/github-#0260/run index cbd3796f..3fd8d2c1 100755 --- a/regress/github-#0260/run +++ b/regress/github-#0260/run @@ -19,15 +19,15 @@ wait $job || status=1 if test $status -ne 0; then fail "process failure" -elif ! grep -e ^LAST\ Contact\|Contact:\ \ log.log > /dev/null; then +elif ! grep -qF 'LAST Contact|Contact: ' log.log; then fail "[last_Contact] header not found" -elif ! grep -e ^LAST\ Contact:\|Contact:\ \ log.log > /dev/null; then +elif ! grep -qF 'LAST Contact:|Contact: ' log.log; then fail "[last_Contact:] header not found" -elif ! grep -e ^LAST\ From:value\|\"Tom\ Jones\"\ \\;tag=SIPpTag001$ log.log > /dev/null; then +elif ! grep -qF 'LAST From:value|"Tom Jones" ;tag=SIPpTag001' log.log; then fail "[last_From:value] not found" -elif ! grep -e ^LAST\ To:uri\|sip:cheese@paris.fr$ log.log > /dev/null; then +elif ! grep -qF 'LAST To:uri|sip:cheese@paris.fr' log.log; then fail "[last_To:uri] not found" -elif ! grep -e ^LAST\ From:param.tag\|SIPpTag001$ log.log > /dev/null; then +elif ! grep -qF 'LAST From:param.tag|SIPpTag001' log.log; then fail "[last_From:param.tag] not found" fi From 9b99ed2aa57c24f0edd6581b1a54dd18852ff650 Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Thu, 20 Oct 2016 23:41:50 +0200 Subject: [PATCH 09/10] Code review --- include/call.hpp | 1 - include/sip_parser.hpp | 3 +- src/call.cpp | 69 +++--------------------------------------- src/sip_parser.cpp | 56 +++++++++++++++++++++++++++++----- 4 files changed, 55 insertions(+), 74 deletions(-) diff --git a/include/call.hpp b/include/call.hpp index 86f6e666..6963f578 100644 --- a/include/call.hpp +++ b/include/call.hpp @@ -291,7 +291,6 @@ class call : virtual public task, virtual public listener, public virtual socket char * get_header_field_code(const char * msg, const char * code); char * get_last_header(const char * name); - char * get_last_header_uri(const char *name); unsigned long hash(const char * msg); typedef std::map file_line_map; diff --git a/include/sip_parser.hpp b/include/sip_parser.hpp index f5a02420..402372d4 100644 --- a/include/sip_parser.hpp +++ b/include/sip_parser.hpp @@ -26,7 +26,8 @@ #define MAX_HEADER_LEN 2049 char *get_call_id(const char *msg); -char *get_param_tag(const char *msg, const char *name, const char *shortname); +char *get_header_uri(const char *msg, const char *name, const char *shortname); +char *get_param(const char *msg, const char *param, const char *name, const char *shortname); char *get_peer_tag(const char *msg); int get_method(char *msg); diff --git a/src/call.cpp b/src/call.cpp index 65dcf38a..beda2495 100644 --- a/src/call.cpp +++ b/src/call.cpp @@ -934,75 +934,18 @@ char * call::get_last_header(const char * name) if (name[len - 1] == ':') { return get_header(last_recv_msg, name, false); } - char *value = get_header(last_recv_msg, header_name, true); if (!strcmp(sep, ":value")) { /* [last_Header:value] */ - ; /* done */ + return get_header(last_recv_msg, header_name, true); } else if (!strcmp(sep, ":uri")) { /* [last_Header:uri] */ - char * uri = get_last_header_uri(header_name); - int uri_len = strlen(uri); - memmove(value, uri, uri_len); - value[uri_len] = '\0'; - free(uri); + snprintf(header_name, len - 3, "%s", name); + return get_header_uri(last_recv_msg, header_name, ""); } else if (!strcmp(sep, ":param.tag")) { /* [last_Header:param.tag] */ snprintf(header_name, len - 9, "%s", name); - char * last_tag = get_param_tag(last_recv_msg, header_name, ""); - int last_tag_len = strlen(last_tag); - memmove(value, last_tag, last_tag_len); - value[last_tag_len] = '\0'; + return get_param(last_recv_msg, "tag", header_name, ""); } else { ERROR("Token %s not valid in %s", sep, name); } - return value; -} - -/* Return the last request URI from the header. On any error returns the - * empty string. The caller must free the result. */ -char * call::get_last_header_uri(const char *name) -{ - char * tmp; - char * tmp2; - char * last_request_uri; - char * last_header; - int tmp_len; - - if (!name || !strlen(name)) { - return strdup(""); - } - - last_header = get_last_header(name); - - if (!last_header) { - return strdup(""); - } - - tmp = strchr(last_header, '<'); - if (!tmp) { - return strdup(""); - } - tmp++; - - tmp2 = strchr(last_header, '>'); - if (!tmp2) { - return strdup(""); - } - - tmp_len = strlen(tmp) - strlen(tmp2); - if (tmp_len < 0) { - return strdup(""); - } - - if (!(last_request_uri = (char *)malloc(tmp_len + 1))) { - ERROR("Cannot allocate !\n"); - } - - last_request_uri[0] = '\0'; - if (tmp && (tmp_len > 0)) { - strncpy(last_request_uri, tmp, tmp_len); - } - last_request_uri[tmp_len] = '\0'; - - return last_request_uri; } char * call::send_scene(int index, int *send_status, int *len) @@ -2234,9 +2177,7 @@ char* call::createSendingMessage(SendingMessage *src, int P_index, char *msg_buf } break; case E_Message_Last_Request_URI: { - char * last_request_uri = get_last_header_uri("To:"); - dest += sprintf(dest, "%s", last_request_uri); - free(last_request_uri); + dest += sprintf(dest, "%s", get_last_header("To:uri")); break; } case E_Message_Last_CSeq_Number: { diff --git a/src/sip_parser.cpp b/src/sip_parser.cpp index 09dab5a4..5b2fcdeb 100644 --- a/src/sip_parser.cpp +++ b/src/sip_parser.cpp @@ -70,16 +70,56 @@ static const char *internal_hdrend(const char *ptr); /*************************** Mini SIP parser (externals) ***************/ -char * get_param_tag(const char *msg, const char *name, const char *shortname) +char * get_header_uri(const char *msg, const char *name, const char *shortname) +{ + char * start; + char * end; + const char * hdr; + static char last_request_uri[MAX_HEADER_LEN]; + int uri_len; + + /* Find start of header */ + hdr = internal_find_header(msg, name, shortname, true); + if (!hdr) { + WARNING("No valid %s: header found", name); + return NULL; + } + + start = strchr((char*)hdr, '<'); + if (!start) { + return NULL; + } + start++; + + end = strchr((char*)hdr, '>'); + if (!end) { + return NULL; + } + + uri_len = strlen(start) - strlen(end); + if (uri_len < 0) { + return NULL; + } + + last_request_uri[0] = '\0'; + if (start && (uri_len > 0)) { + strncpy(last_request_uri, start, uri_len); + } + last_request_uri[uri_len] = '\0'; + + return last_request_uri; +} + +char * get_param(const char *msg, const char * param, const char *name, const char *shortname) { static char tag[MAX_HEADER_LEN]; - const char * to_hdr; + const char * hdr; const char * ptr; int tag_i = 0; /* Find start of header */ - to_hdr = internal_find_header(msg, name, shortname, true); - if (!to_hdr) { + hdr = internal_find_header(msg, name, shortname, true); + if (!hdr) { WARNING("No valid %s: header in reply", name); return NULL; } @@ -88,14 +128,14 @@ char * get_param_tag(const char *msg, const char *name, const char *shortname) /* FIXME */ /* Skip past LA/RA-quoted addr-spec if any */ - ptr = internal_hdrchr(to_hdr, '>'); + ptr = internal_hdrchr(hdr, '>'); if (!ptr) { /* Maybe an addr-spec without quotes */ - ptr = to_hdr; + ptr = hdr; } /* Find tag in this header */ - ptr = internal_find_param(ptr, "tag"); + ptr = internal_find_param(ptr, param); if (!ptr) { return NULL; } @@ -111,7 +151,7 @@ char * get_param_tag(const char *msg, const char *name, const char *shortname) char * get_peer_tag(const char *msg) { - return get_param_tag(msg, "To", "t"); + return get_param(msg, "tag", "To", "t"); } char * get_header_content(const char* message, const char * name) From e52e597b3f715f25f689cd50f2eb76757481f28f Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Fri, 21 Oct 2016 00:17:06 +0200 Subject: [PATCH 10/10] Unit tests --- src/sip_parser.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/sip_parser.cpp b/src/sip_parser.cpp index 5b2fcdeb..719a55fa 100644 --- a/src/sip_parser.cpp +++ b/src/sip_parser.cpp @@ -620,4 +620,19 @@ TEST(Parser, get_call_id_github_0101) { // github-#0101 EXPECT_STREQ("1-18220@127.0.0.1", get_call_id(input)); } +TEST(Parser, get_header_uri) { + EXPECT_STREQ("abc@domain.com", get_header_uri("...\r\nContact: ;test=123\r\n\r\n", "Contact", "")); +} + +TEST(Parser, get_param) { + EXPECT_STREQ("123", get_param("...\r\nContact: ;test=123\r\n\r\n", "test", "Contact", "")); +} + +TEST(Parser, get_header) { + EXPECT_STREQ("Contact: ;test=123", get_header("...\r\nContact: ;test=123\r\n\r\n", "Contact:", false)); +} + +TEST(Parser, get_header_content) { + EXPECT_STREQ(";test=123", get_header("...\r\nContact: ;test=123\r\n\r\n", "Contact:", true)); +} #endif //GTEST