[Asterisk-code-review] chan sip: Add dialplan function SIP HEADERS (asterisk[master])

Kirill Katsnelson asteriskteam at digium.com
Sun Jul 30 01:25:17 CDT 2017


Kirill Katsnelson has uploaded this change for review. ( https://gerrit.asterisk.org/6119


Change subject: chan_sip: Add dialplan function SIP_HEADERS
......................................................................

chan_sip: Add dialplan function SIP_HEADERS

Syntax: SIP_HEADERS([prefix])

If the argument is specified, only the headers matching the given prefix
are returned.

The function returns a comma-separated list of SIP header names from an
incoming INVITE message. Multiple headers with the same name are included
in the list only once. The returned list can be iterated over using the
functions POP() and SIP_HEADER().

For example, '${SIP_HEADER(Co)}' might return the string
'Contact,Content-Length,Content-Type'.

Practical use is rather '${SIP_HEADER(X-)}' to enumerate optional
extended headers sent by a peer.

ASTERISK-27163

Change-Id: I2076d3893d03a2f82429f393b5b46db6cf68a267
---
M channels/chan_sip.c
1 file changed, 109 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/19/6119/1

diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index f2daf2b..4d6d1a3 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -381,6 +381,32 @@
 			SIP request) can't be accessed with this function.</para>
 		</description>
 	</function>
+	<function name="SIP_HEADERS" language="en_US">
+		<synopsis>
+			Gets the list of SIP header names from an incoming 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 message that originated the current channel. Multiple headers with the
+			same name are included in the list only once. The returned list can be iterated
+			over using the functions POP() and SIP_HEADER().</para>
+			<para>For example, <literal>${SIP_HEADER(Co)}</literal> might return
+			<literal>Contact,Content-Length,Content-Type</literal>. As a practical example,
+			you may	use <literal>${SIP_HEADER(X-)}</literal> to enumerate optional extended
+			headers.</para>
+			<para>Please observe that contents of the SDP (an attachment to the
+			SIP request) can't be accessed with this function.</para>
+		</description>
+		<see-also>
+			<ref type="function">SIP_HEADER</ref>
+			<ref type="function">POP</ref>
+		</see-also>
+	</function>
 	<function name="SIPPEER" language="en_US">
 		<synopsis>
 			Gets SIP peer information.
@@ -23054,6 +23080,87 @@
 	.read = func_header_read,
 };
 
+/*! \brief Read unique list of SIP headers (dialplan function) */
+static int func_headers_read2(struct ast_channel *chan, const char *function, char *data, struct ast_str **buf, ssize_t maxlen)
+{
+	int i;
+	struct sip_pvt *pvt;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(pattern);
+	);
+
+	SCOPED_CHANNELLOCK(chanlock, chan);
+
+	if (!chan) {
+		return -1;
+	}
+
+	if (!IS_SIP_TECH(ast_channel_tech(chan))) {
+		ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
+		return -1;
+	}
+
+	pvt = ast_channel_tech_pvt(chan);
+	if (!pvt) {
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, data);
+	if (!args.pattern) {
+		args.pattern = "";
+	}
+
+	for (i = 0; i < pvt->initreq.headers; i++) {
+		const char *header = REQ_OFFSET_TO_STR(&pvt->initreq, header[i]);
+		if (ast_begins_with(header, args.pattern)) {
+			int hdrlen = strcspn(header, " \t:,");  /* Comma will break our logic, and illegal per RFC. */
+			const char *term = ast_skip_blanks(header + hdrlen);
+			if (hdrlen > 0 && *term == ':') {  /* Header is malformed otherwise! */
+				struct ast_str *token = ast_str_alloca(100);
+				const char *s = NULL;
+
+				/* Return short headers in full form always. */
+				if (hdrlen == 1) {
+					char short_hdr[2] = { header[0], '\0' };
+					s = find_full_alias(short_hdr, NULL);
+				}
+				if (s) {
+					/* Short header was found and expanded. */
+					ast_str_set(&token, -1, "%s,", s);
+				} else {
+					/* Return the header as is, whether 1-character or not. */
+					ast_str_set(&token, -1, "%.*s,", hdrlen, header);
+				}
+
+				/* Has the same header been already added? */
+				s = ast_str_buffer(*buf);
+				while ((s = strstr(s, ast_str_buffer(token))) != NULL) {
+					/* Found suffix, but is it the full token? */
+					if (s == ast_str_buffer(*buf) || s[-1] == ',')
+						break;
+					/* Only suffix matched, go on with the search after the comma. */
+					s += hdrlen + 1;
+				}
+
+				/* s is null iff not broken from the loop, hence header not yet added. */
+				if (s == NULL) {
+					ast_str_append(buf, maxlen, "%s", ast_str_buffer(token));
+				}
+			}
+		}
+	}
+
+	ast_str_truncate(*buf, -1);  /* Trim the last comma. Safe if empty. */
+
+	return 0;
+}
+
+static struct ast_custom_function sip_headers_function = {
+	.name = "SIP_HEADERS",
+	.read2 = func_headers_read2,
+};
+
+
 /*! \brief  Dial plan function to check if domain is local */
 static int func_check_sipdomain(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 {
@@ -35201,6 +35308,7 @@
 
 	/* Register dialplan functions */
 	ast_custom_function_register(&sip_header_function);
+	ast_custom_function_register(&sip_headers_function);
 	ast_custom_function_register(&sippeer_function);
 	ast_custom_function_register(&checksipdomain_function);
 
@@ -35301,6 +35409,7 @@
 
 	/* Unregister dial plan functions */
 	ast_custom_function_unregister(&sippeer_function);
+	ast_custom_function_unregister(&sip_headers_function);
 	ast_custom_function_unregister(&sip_header_function);
 	ast_custom_function_unregister(&checksipdomain_function);
 

-- 
To view, visit https://gerrit.asterisk.org/6119
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I2076d3893d03a2f82429f393b5b46db6cf68a267
Gerrit-Change-Number: 6119
Gerrit-PatchSet: 1
Gerrit-Owner: Kirill Katsnelson <kkm at smartaction.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20170730/155b7973/attachment-0001.html>


More information about the asterisk-code-review mailing list