res_pjsip_rfc3326.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2013, Digium, Inc.
00005  *
00006  * Joshua Colp <jcolp@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*** MODULEINFO
00020    <depend>pjproject</depend>
00021    <depend>res_pjsip</depend>
00022    <depend>res_pjsip_session</depend>
00023    <support_level>core</support_level>
00024  ***/
00025 
00026 #include "asterisk.h"
00027 
00028 #include <pjsip.h>
00029 #include <pjsip_ua.h>
00030 
00031 #include "asterisk/res_pjsip.h"
00032 #include "asterisk/res_pjsip_session.h"
00033 #include "asterisk/module.h"
00034 #include "asterisk/causes.h"
00035 
00036 static void rfc3326_use_reason_header(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
00037 {
00038    const pj_str_t str_reason = { "Reason", 6 };
00039    pjsip_generic_string_hdr *header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_reason, NULL);
00040    char buf[20], *cause, *text;
00041    int code;
00042 
00043    if (!header) {
00044       return;
00045    }
00046 
00047    ast_copy_pj_str(buf, &header->hvalue, sizeof(buf));
00048    cause = ast_skip_blanks(buf);
00049 
00050    if (strncasecmp(cause, "Q.850", 5) || !(cause = strstr(cause, "cause="))) {
00051       return;
00052    }
00053 
00054    /* If text is present get rid of it */
00055    if ((text = strstr(cause, ";"))) {
00056       *text = '\0';
00057    }
00058 
00059    if (sscanf(cause, "cause=%30d", &code) != 1) {
00060       return;
00061    }
00062 
00063    ast_channel_hangupcause_set(session->channel, code & 0x7f);
00064 }
00065 
00066 static int rfc3326_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
00067 {
00068    if ((pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method) &&
00069         pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method)) ||
00070        !session->channel) {
00071       return 0;
00072    }
00073 
00074    rfc3326_use_reason_header(session, rdata);
00075 
00076    return 0;
00077 }
00078 
00079 static void rfc3326_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
00080 {
00081    struct pjsip_status_line status = rdata->msg_info.msg->line.status;
00082 
00083    if ((status.code < 300) || !session->channel) {
00084       return;
00085    }
00086 
00087    rfc3326_use_reason_header(session, rdata);
00088 }
00089 
00090 static void rfc3326_add_reason_header(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
00091 {
00092    char buf[20];
00093 
00094    snprintf(buf, sizeof(buf), "Q.850;cause=%i", ast_channel_hangupcause(session->channel) & 0x7f);
00095    ast_sip_add_header(tdata, "Reason", buf);
00096 
00097    if (ast_channel_hangupcause(session->channel) == AST_CAUSE_ANSWERED_ELSEWHERE) {
00098       ast_sip_add_header(tdata, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
00099    }
00100 }
00101 
00102 static void rfc3326_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
00103 {
00104    if ((pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_bye_method) &&
00105         pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method)) ||
00106        !session->channel) {
00107       return;
00108    }
00109 
00110    rfc3326_add_reason_header(session, tdata);
00111 }
00112 
00113 static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
00114 {
00115    struct pjsip_status_line status = tdata->msg->line.status;
00116 
00117    if ((status.code < 300) || !session->channel) {
00118       return;
00119    }
00120 
00121    rfc3326_add_reason_header(session, tdata);
00122 }
00123 
00124 static struct ast_sip_session_supplement rfc3326_supplement = {
00125    .incoming_request = rfc3326_incoming_request,
00126    .incoming_response = rfc3326_incoming_response,
00127    .outgoing_request = rfc3326_outgoing_request,
00128    .outgoing_response = rfc3326_outgoing_response,
00129 };
00130 
00131 static int load_module(void)
00132 {
00133    CHECK_PJSIP_SESSION_MODULE_LOADED();
00134 
00135    ast_sip_session_register_supplement(&rfc3326_supplement);
00136    return AST_MODULE_LOAD_SUCCESS;
00137 }
00138 
00139 static int unload_module(void)
00140 {
00141    ast_sip_session_unregister_supplement(&rfc3326_supplement);
00142    return 0;
00143 }
00144 
00145 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP RFC3326 Support",
00146       .support_level = AST_MODULE_SUPPORT_CORE,
00147       .load = load_module,
00148       .unload = unload_module,
00149       .load_pri = AST_MODPRI_APP_DEPEND,
00150           );

Generated on Thu Apr 16 06:27:50 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6