From 060d7cae917d9df4fe97c1339c68df3b00ef5482 Mon Sep 17 00:00:00 2001 From: Pietro Bertera Date: Sat, 17 Sep 2016 19:19:50 +0200 Subject: [PATCH] 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;