diff --git a/res/res_pjsip_reply_header_funcs.c b/res/res_pjsip_reply_header_funcs.c new file mode 100644 index 0000000000..c7c6345d30 --- /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 + * + * 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 + pjproject + res_pjsip + res_pjsip_session + core + ***/ + +#include "asterisk.h" + +#include +#include + +#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 + + + Gets reply headers from an outbound PJSIP channel. + + + + + Returns instance number + of reply header name. A * + may be appended to name to iterate over all + headers beginning with name. + + + + + The name of the header. + + + If there's more than 1 header with the same name, this specifies which header + to read. If not specified, defaults to 1 meaning + the first matching header. + + + + + + PJSIP_REPLY_HEADER allows you to read specific SIP headers from the outbound + PJSIP channel. + Examples: + ; + ; Set 'somevar' to the value of the 'From' header. + exten => 1,1,Set(somevar=${PJSIP_REPLY_HEADER(read,From)}) + ; + ; Set 'via2' to the value of the 2nd 'Via' header. + exten => 1,1,Set(via2=${PJSIP_REPLY_HEADER(read,Via,2)}) + ; + ; Set 'xhdr' to the value of the 1sx X-header. + exten => 1,1,Set(xhdr=${PJSIP_REPLY_HEADER(read,X-*,1)}) + ; + + If you call PJSIP_REPLY_HEADER in a normal dialplan context you'll be + operating on the caller's (incoming) channel which + may not be what you want. To operate on the callee's (outgoing) + channel call PJSIP_REPLY_HEADER in a pre-connect handler. + Example: + ; + [handler] + exten => readheader,1,NoOp(PJSIP_REPLY_HEADER(read,X-MyHeader)) + ; + [somecontext] + exten => 1,1,Dial(PJSIP/${EXTEN},,U(handler^readheader^1)) + ; + + + + + + Gets the list of SIP reply header names from an INVITE message. + + + + If specified, only the headers matching the given prefix are returned. + + + + 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. + + For example, ${PJSIP_REPLY_HEADERS(Co)} might return + Contact,Content-Length,Content-Type. As a practical example, + you may use ${PJSIP_REPLY_HEADERS(X-)} to enumerate optional extended + headers. + + + PJSIP_REPLY_HEADER + + + + ***/ + +/*! \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", +);