Skip to content

Instantly share code, notes, and snippets.

@ipoddubny
Created May 31, 2016 19:54
Show Gist options
  • Save ipoddubny/56b411e9e37ffb09fcd156e80a44316e to your computer and use it in GitHub Desktop.
Save ipoddubny/56b411e9e37ffb09fcd156e80a44316e to your computer and use it in GitHub Desktop.
Asterisk: filling SIP_HEADERS hash in incoming calls so that headers are available from the dialplan
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index f648454..d6be92b 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -19266,6 +19266,33 @@ static int set_message_vars_from_req(struct ast_msg *msg, struct sip_request *re
return res;
}
+static void fill_sip_headers_hash(struct ast_channel *channel, struct sip_request *req)
+{
+ size_t x;
+ char name_buf[1024];
+ char val_buf[1024];
+ char hash_buf[1024 + sizeof("HASH_SIP(HEADERS,)")];
+ const char *name;
+ char *c;
+
+ for (x = 0; x < req->headers; x++) {
+ const char *header = REQ_OFFSET_TO_STR(req, header[x]);
+
+ if ((c = strchr(header, ':'))) {
+ ast_copy_string(name_buf, header, MIN((c - header + 1), sizeof(name_buf)));
+ ast_copy_string(val_buf, ast_skip_blanks(c + 1), sizeof(val_buf));
+ ast_trim_blanks(name_buf);
+
+ /* Convert header name to full name alias. */
+ name = find_full_alias(name_buf, name_buf);
+
+ snprintf(hash_buf, sizeof(hash_buf), "HASH(SIP_HEADERS,%s)", name);
+
+ ast_func_write(channel, hash_buf, val_buf);
+ }
+ }
+}
+
/*! \brief Receive SIP MESSAGE method messages
\note We only handle messages within current calls currently
Reference: RFC 3428 */
@@ -26268,6 +26295,8 @@ static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, str
build_route(p, req, 0, 0);
if (c) {
+ fill_sip_headers_hash(c, req);
+
ast_party_redirecting_init(&redirecting);
memset(&update_redirecting, 0, sizeof(update_redirecting));
change_redirecting_information(p, req, &redirecting, &update_redirecting,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment