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