[Asterisk-code-review] res_pjsip: Add functions PJSIP_REPLY_HEADER and PJSIP_REPLY_HEADERS (asterisk[master])

José Lopes asteriskteam at digium.com
Fri Apr 8 06:52:39 CDT 2022


José Lopes has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/18367 )


Change subject: res_pjsip: Add functions PJSIP_REPLY_HEADER and PJSIP_REPLY_HEADERS
......................................................................

res_pjsip: Add functions PJSIP_REPLY_HEADER and PJSIP_REPLY_HEADERS

These new functions allows to retrieve information from Headers on 200 OK
INVITE reply.
This code is based on res_pjsip_header_funcs.c .

ASTERISK-29999

Change-Id: I264a610a9333359297a0825feb29a1bb4f4ad144
---
A res/res_pjsip_reply_header_funcs.c
1 file changed, 493 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/67/18367/1

diff --git a/res/res_pjsip_reply_header_funcs.c b/res/res_pjsip_reply_header_funcs.c
new file mode 100644
index 0000000..92b9169
--- /dev/null
+++ b/res/res_pjsip_reply_header_funcs.c
@@ -0,0 +1,493 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Fairview 5 Engineering, LLC
+ *
+ * George Joseph <george.joseph at fairview5.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*** MODULEINFO
+	<depend>pjproject</depend>
+	<depend>res_pjsip</depend>
+	<depend>res_pjsip_session</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include <pjsip.h>
+#include <pjsip_ua.h>
+
+#include "asterisk/res_pjsip.h"
+#include "asterisk/res_pjsip_session.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/app.h"
+#include "asterisk/module.h"
+#include "asterisk/utils.h"
+
+/*** DOCUMENTATION
+	<function name="PJSIP_REPLY_HEADER" language="en_US">
+		<synopsis>
+			Gets reply headers from an outbound PJSIP channel.
+		</synopsis>
+		<syntax>
+			<parameter name="action" required="true">
+				<enumlist>
+					<enum name="read"><para>Returns instance <replaceable>number</replaceable>
+					of reply header <replaceable>name</replaceable>. A <literal>*</literal>
+					may be appended to <replaceable>name</replaceable> to iterate over all 
+					headers <emphasis>beginning with</emphasis> <replaceable>name</replaceable>.
+					</para></enum>
+				</enumlist>
+			</parameter>
+
+			<parameter name="name" required="true"><para>The name of the header.</para></parameter>
+
+			<parameter name="number" required="false">
+				<para>If there's more than 1 header with the same name, this specifies which header
+				to read.  If not specified, defaults to <literal>1</literal> meaning
+				the first matching header.
+				</para>
+			</parameter>
+
+		</syntax>
+		<description>
+			<para>PJSIP_REPLY_HEADER allows you to read specific SIP headers from the outbound
+			PJSIP channel.</para>
+			<para>Examples:</para>
+			<para>;</para>
+			<para>; Set 'somevar' to the value of the 'From' header.</para>
+			<para>exten => 1,1,Set(somevar=${PJSIP_REPLY_HEADER(read,From)})</para>
+			<para>;</para>
+			<para>; Set 'via2' to the value of the 2nd 'Via' header.</para>
+			<para>exten => 1,1,Set(via2=${PJSIP_REPLY_HEADER(read,Via,2)})</para>
+			<para>;</para>
+			<para>; Set 'xhdr' to the value of the 1sx X-header.</para>
+			<para>exten => 1,1,Set(xhdr=${PJSIP_REPLY_HEADER(read,X-*,1)})</para>
+			<para>;</para>
+
+			<note><para>If you call PJSIP_REPLY_HEADER in a normal dialplan context you'll be
+			operating on the <emphasis>caller's (incoming)</emphasis> channel which
+			may not be what you want. To operate on the <emphasis>callee's (outgoing)</emphasis>
+			channel call PJSIP_REPLY_HEADER in a pre-connect handler.</para>
+			<para>Example:</para>
+			<para>;</para>
+			<para>[handler]</para>
+			<para>exten => readheader,1,NoOp(PJSIP_REPLY_HEADER(read,X-MyHeader))</para>
+			<para>;</para>
+			<para>[somecontext]</para>
+			<para>exten => 1,1,Dial(PJSIP/${EXTEN},,U(handler^readheader^1))</para>
+			<para>;</para>
+			</note>
+		</description>
+	</function>
+	<function name="PJSIP_REPLY_HEADERS" language="en_US">
+		<synopsis>
+			Gets the list of SIP reply header names from an INVITE message.
+		</synopsis>
+		<syntax>
+			<parameter name="prefix">
+				<para>If specified, only the headers matching the given prefix are returned.</para>
+			</parameter>
+		</syntax>
+		<description>
+			<para>Returns a comma-separated list of header names (without values) from the
+			INVITE reply message. Multiple headers with the same name are included in the list only once.
+			</para>
+			<para>For example, <literal>${PJSIP_REPLY_HEADERS(Co)}</literal> might return
+			<literal>Contact,Content-Length,Content-Type</literal>. As a practical example,
+			you may use <literal>${PJSIP_REPLY_HEADERS(X-)}</literal> to enumerate optional extended
+			headers.</para>
+		</description>
+		<see-also>
+			<ref type="function">PJSIP_REPLY_HEADER</ref>
+		</see-also>
+	</function>
+
+ ***/
+
+/*! \brief Linked list for accumulating headers */
+struct hdr_list_entry {
+	pjsip_hdr *hdr;
+	AST_LIST_ENTRY(hdr_list_entry) nextptr;
+};
+AST_LIST_HEAD_NOLOCK(hdr_list, hdr_list_entry);
+
+/*! \brief Datastore for saving headers */
+static const struct ast_datastore_info header_datastore = {
+	.type = "reply_header_datastore",
+};
+
+/*! \brief Data structure used for ast_sip_push_task_wait_serializer  */
+struct header_data {
+	struct ast_sip_channel_pvt *channel;
+	char *header_name;
+	const char *header_value;
+	char *buf;
+	int header_number;
+	size_t len;
+};
+
+/*!
+ * \internal
+ * \brief Insert the header pointers into the linked list.
+ *
+ * For each header in the message, allocate a list entry,
+ * clone the header, then insert the entry.
+ */
+static int insert_headers(pj_pool_t * pool, struct hdr_list *list, pjsip_msg * msg)
+{
+	pjsip_hdr *hdr = msg->hdr.next;
+	struct hdr_list_entry *le;
+
+	while (hdr && hdr != &msg->hdr) {
+		le = pj_pool_zalloc(pool, sizeof(struct hdr_list_entry));
+		le->hdr = pjsip_hdr_clone(pool, hdr);
+		AST_LIST_INSERT_TAIL(list, le, nextptr);
+		hdr = hdr->next;
+	}
+
+	return 0;
+}
+
+/*!
+ * \internal
+ * \brief Session supplement callback on an incoming INVITE response
+ *
+ * Retrieve the header_datastore from the session or create one if it doesn't exist.
+ * Create and initialize the list if needed.
+ * Insert the headers.
+ */
+static void incoming_response(struct ast_sip_session *session, pjsip_rx_data * rdata)
+{
+	pj_pool_t *pool = session->inv_session->dlg->pool;
+	RAII_VAR(struct ast_datastore *, datastore,
+			 ast_sip_session_get_datastore(session, header_datastore.type), ao2_cleanup);
+
+	if (session->inv_session->state <= PJSIP_INV_STATE_EARLY) {
+		return;
+	}
+
+	if (!datastore) {
+		if (!(datastore =
+			  ast_sip_session_alloc_datastore(&header_datastore, header_datastore.type))
+			||
+			!(datastore->data = pj_pool_alloc(pool, sizeof(struct hdr_list))) ||
+			ast_sip_session_add_datastore(session, datastore)) {
+			ast_log(AST_LOG_ERROR, "Unable to create datastore for header functions.\n");
+			return;
+		}
+		AST_LIST_HEAD_INIT_NOLOCK((struct hdr_list *) datastore->data);
+	}
+	insert_headers(pool, (struct hdr_list *) datastore->data, rdata->msg_info.msg);
+
+	return;
+}
+
+/*!
+ * \internal
+ * \brief Implements PJSIP_REPLY_HEADERS by searching for the requested header prefix.
+ *
+ * Retrieve the header_datastore.
+ * Search for the all matching headers.
+ * Validate the pjsip_hdr found.
+ * Parse pjsip_hdr into a name and copy to the buffer.
+ * Return the value.
+ */
+static int read_headers(void *obj)
+{
+	struct header_data *data = obj;
+	size_t len = strlen(data->header_name);
+	pjsip_hdr *hdr = NULL;
+	char *pj_hdr_string;
+	int pj_hdr_string_len;
+	char *p;
+	char *pos;
+	size_t plen, wlen = 0;
+	struct hdr_list_entry *le;
+	struct hdr_list *list;
+
+	RAII_VAR(struct ast_datastore *, datastore,
+			 ast_sip_session_get_datastore(data->channel->session, header_datastore.type),
+			 ao2_cleanup);
+
+	if (!datastore || !datastore->data) {
+		ast_debug(1, "There was no datastore from which to read headers.\n");
+		return -1;
+	}
+
+	list = datastore->data;
+	pj_hdr_string = ast_alloca(data->len);
+	AST_LIST_TRAVERSE(list, le, nextptr) {
+		if (pj_strnicmp2(&le->hdr->name, data->header_name, len) == 0) {
+			/* Found matched header, append to buf */
+			hdr = le->hdr;
+
+			pj_hdr_string_len = pjsip_hdr_print_on(hdr, pj_hdr_string, data->len - 1);
+			if (pj_hdr_string_len == -1) {
+				ast_log(AST_LOG_ERROR,
+					"Not enough buffer space in pjsip_hdr_print_on\n");
+				return -1;
+			}
+			pj_hdr_string[pj_hdr_string_len] = '\0';
+			p = strchr(pj_hdr_string, ':');
+			if (!p) {
+				ast_log(AST_LOG_WARNING,
+					"A malformed header was returned from pjsip_hdr_print_on\n");
+				continue;
+			}
+
+			pj_hdr_string[p - pj_hdr_string] = '\0';
+			p = ast_strip(pj_hdr_string);
+			plen = strlen(p);
+			if (wlen + plen + 1 > data->len) {
+				ast_log(AST_LOG_ERROR,
+						"Buffer isn't big enough to hold header value.  %zu > %zu\n", plen + 1,
+						data->len);
+				return -1;
+			}
+			pos = strstr(data->buf, p);
+			if (pos && pos[1] == ',') {
+				if (pos == data->buf) {
+					continue;
+				} else if (pos[-1] == ',') {
+					continue;
+				}
+			}
+			ast_copy_string(data->buf + wlen, p, data->len - wlen);
+			wlen += plen;
+			ast_copy_string(data->buf + wlen, ",", data->len - wlen);
+			wlen++;
+		}
+	}
+
+	if (wlen == 0) {
+		ast_debug(1, "There was no header named %s.\n", data->header_name);
+		return -1;
+	} else {
+		data->buf[wlen-1] = '\0';
+	}
+	return 0;
+}
+
+
+/*!
+ * \internal
+ * \brief Implements PJSIP_REPLY_HEADER 'read' by searching the for the requested header.
+ *
+ * Retrieve the header_datastore.
+ * Search for the nth matching header.
+ * Validate the pjsip_hdr found.
+ * Parse pjsip_hdr into a name and value.
+ * Return the value.
+ */
+static int read_header(void *obj)
+{
+	struct header_data *data = obj;
+	size_t len = strlen(data->header_name);
+	pjsip_hdr *hdr = NULL;
+	char *pj_hdr_string;
+	int pj_hdr_string_len;
+	char *p;
+	size_t plen;
+	struct hdr_list_entry *le;
+	struct hdr_list *list;
+	int i = 1;
+	RAII_VAR(struct ast_datastore *, datastore,
+			 ast_sip_session_get_datastore(data->channel->session, header_datastore.type),
+			 ao2_cleanup);
+
+	if (!datastore || !datastore->data) {
+		ast_debug(1, "There was no datastore from which to read headers.\n");
+		return -1;
+	}
+
+	list = datastore->data;
+	AST_LIST_TRAVERSE(list, le, nextptr) {
+		if (data->header_name[len - 1] == '*') {
+			if (pj_strnicmp2(&le->hdr->name, data->header_name, len - 1) == 0 && i++ == data->header_number) {
+				hdr = le->hdr;
+				break;
+			}
+		} else {
+			if (pj_stricmp2(&le->hdr->name, data->header_name) == 0 && i++ == data->header_number) {
+				hdr = le->hdr;
+				break;
+			}
+		}
+	}
+
+	if (!hdr) {
+		ast_debug(1, "There was no header named %s.\n", data->header_name);
+		return -1;
+	}
+
+	pj_hdr_string = ast_alloca(data->len);
+	pj_hdr_string_len = pjsip_hdr_print_on(hdr, pj_hdr_string, data->len - 1);
+	if (pj_hdr_string_len == -1) {
+		ast_log(AST_LOG_ERROR,
+			"Not enough buffer space in pjsip_hdr_print_on\n");
+		return -1;
+	}
+
+	pj_hdr_string[pj_hdr_string_len] = '\0';
+
+	p = strchr(pj_hdr_string, ':');
+	if (!p) {
+		ast_log(AST_LOG_ERROR,
+				"A malformed header was returned from pjsip_hdr_print_on.\n");
+		return -1;
+	}
+
+	++p;
+	p = ast_strip(p);
+	plen = strlen(p);
+	if (plen + 1 > data->len) {
+		ast_log(AST_LOG_ERROR,
+				"Buffer isn't big enough to hold header value.  %zu > %zu\n", plen + 1,
+				data->len);
+		return -1;
+	}
+
+	ast_copy_string(data->buf, p, data->len);
+
+	return 0;
+}
+
+/*!
+ * \brief Read list of unique SIP headers
+ */
+static int func_read_headers(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
+{
+	struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
+	struct header_data header_data;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(header_pattern);
+	);
+	AST_STANDARD_APP_ARGS(args, data);
+
+	if (!chan || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
+		ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
+		return -1;
+	}
+
+	if (ast_strlen_zero(args.header_pattern)) {
+		ast_log(AST_LOG_ERROR, "This function requires a pattern.\n");
+		return -1;
+	}
+
+	header_data.channel = channel;
+	header_data.header_name = args.header_pattern;
+	header_data.header_value = NULL;
+	header_data.buf = buf;
+	header_data.len = len;
+
+	return ast_sip_push_task_wait_serializer(channel->session->serializer, read_headers, &header_data);
+
+}
+
+/*!
+ * \brief Implements function 'read' callback.
+ *
+ * Valid actions are 'read'.
+ */
+static int func_read_header(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
+{
+	struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
+	struct header_data header_data;
+	int number;
+	AST_DECLARE_APP_ARGS(args,
+						 AST_APP_ARG(action);
+						 AST_APP_ARG(header_name); AST_APP_ARG(header_number););
+	AST_STANDARD_APP_ARGS(args, data);
+
+	if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
+		ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
+		return -1;
+	}
+
+	if (ast_strlen_zero(args.action)) {
+		ast_log(AST_LOG_ERROR, "This function requires an action.\n");
+		return -1;
+	}
+	if (ast_strlen_zero(args.header_name)) {
+		ast_log(AST_LOG_ERROR, "This function requires a header name.\n");
+		return -1;
+	}
+	if (!args.header_number) {
+		number = 1;
+	} else {
+		sscanf(args.header_number, "%30d", &number);
+		if (number < 1) {
+			number = 1;
+		}
+	}
+
+	header_data.channel = channel;
+	header_data.header_name = args.header_name;
+	header_data.header_number = number;
+	header_data.header_value = NULL;
+	header_data.buf = buf;
+	header_data.len = len;
+
+	if (!strcasecmp(args.action, "read")) {
+		return ast_sip_push_task_wait_serializer(channel->session->serializer, read_header, &header_data);
+	} else {
+		ast_log(AST_LOG_ERROR,
+				"Unknown action '%s' is not valid, must be 'read'.\n",
+				args.action);
+		return -1;
+	}
+}
+
+static struct ast_custom_function pjsip_reply_header_function = {
+	.name = "PJSIP_REPLY_HEADER",
+	.read = func_read_header
+};
+
+static struct ast_custom_function pjsip_reply_headers_function = {
+	.name = "PJSIP_REPLY_HEADERS",
+	.read = func_read_headers
+};
+
+static struct ast_sip_session_supplement header_funcs_supplement = {
+	.method = "INVITE",
+	.priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL - 1000,
+	.incoming_response = incoming_response
+};
+
+static int load_module(void)
+{
+	ast_sip_session_register_supplement(&header_funcs_supplement);
+	ast_custom_function_register(&pjsip_reply_header_function);
+	ast_custom_function_register(&pjsip_reply_headers_function);
+
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+	ast_custom_function_unregister(&pjsip_reply_header_function);
+	ast_custom_function_unregister(&pjsip_reply_headers_function);
+	ast_sip_session_unregister_supplement(&header_funcs_supplement);
+	return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Reply Header Functions",
+	.support_level = AST_MODULE_SUPPORT_CORE,
+	.load = load_module,
+	.unload = unload_module,
+	.load_pri = AST_MODPRI_APP_DEPEND,
+	.requires = "res_pjsip,res_pjsip_session",
+);

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/18367
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I264a610a9333359297a0825feb29a1bb4f4ad144
Gerrit-Change-Number: 18367
Gerrit-PatchSet: 1
Gerrit-Owner: José Lopes <jose.lopes at nfon.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220408/def60bf3/attachment-0001.html>


More information about the asterisk-code-review mailing list