res_pjsip_diversion.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  * Kevin Harwell <kharwell@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/callerid.h"
00034 #include "asterisk/channel.h"
00035 #include "asterisk/module.h"
00036 #include "asterisk/strings.h"
00037 
00038 static const pj_str_t diversion_name = { "Diversion", 9 };
00039 
00040 /*! \brief Diversion header reasons
00041  *
00042  * The core defines a bunch of constants used to define
00043  * redirecting reasons. This provides a translation table
00044  * between those and the strings which may be present in
00045  * a SIP Diversion header
00046  */
00047 static const struct reasons {
00048    enum AST_REDIRECTING_REASON code;
00049    char *const text;
00050 } reason_table[] = {
00051    { AST_REDIRECTING_REASON_UNKNOWN, "unknown" },
00052    { AST_REDIRECTING_REASON_USER_BUSY, "user-busy" },
00053    { AST_REDIRECTING_REASON_NO_ANSWER, "no-answer" },
00054    { AST_REDIRECTING_REASON_UNAVAILABLE, "unavailable" },
00055    { AST_REDIRECTING_REASON_UNCONDITIONAL, "unconditional" },
00056    { AST_REDIRECTING_REASON_TIME_OF_DAY, "time-of-day" },
00057    { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "do-not-disturb" },
00058    { AST_REDIRECTING_REASON_DEFLECTION, "deflection" },
00059    { AST_REDIRECTING_REASON_FOLLOW_ME, "follow-me" },
00060    { AST_REDIRECTING_REASON_OUT_OF_ORDER, "out-of-service" },
00061    { AST_REDIRECTING_REASON_AWAY, "away" },
00062    { AST_REDIRECTING_REASON_CALL_FWD_DTE, "unknown"},
00063    { AST_REDIRECTING_REASON_SEND_TO_VM, "send_to_vm"},
00064 };
00065 
00066 static const char *reason_code_to_str(const struct ast_party_redirecting_reason *reason)
00067 {
00068    int code = reason->code;
00069 
00070    /* use specific string if given */
00071    if (!ast_strlen_zero(reason->str)) {
00072       return reason->str;
00073    }
00074 
00075    if (code >= 0 && code < ARRAY_LEN(reason_table)) {
00076       return reason_table[code].text;
00077    }
00078 
00079    return "unknown";
00080 }
00081 
00082 static enum AST_REDIRECTING_REASON reason_str_to_code(const char *text)
00083 {
00084    enum AST_REDIRECTING_REASON code = AST_REDIRECTING_REASON_UNKNOWN;
00085    int i;
00086 
00087    for (i = 0; i < ARRAY_LEN(reason_table); ++i) {
00088       if (!strcasecmp(text, reason_table[i].text)) {
00089          code = reason_table[i].code;
00090          break;
00091       }
00092    }
00093 
00094    return code;
00095 }
00096 
00097 static pjsip_fromto_hdr *get_diversion_header(pjsip_rx_data *rdata)
00098 {
00099    static const pj_str_t from_name = { "From", 4 };
00100 
00101    pjsip_generic_string_hdr *hdr;
00102    pj_str_t value;
00103    int size;
00104 
00105    if (!(hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &diversion_name, NULL))) {
00106       return NULL;
00107    }
00108 
00109    pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue);
00110 
00111    /* parse as a fromto header */
00112    return pjsip_parse_hdr(rdata->tp_info.pool, &from_name, value.ptr,
00113                 pj_strlen(&value), &size);
00114 }
00115 
00116 static void set_redirecting_value(char **dst, const pj_str_t *src)
00117 {
00118    ast_free(*dst);
00119    *dst = ast_malloc(pj_strlen(src) + 1);
00120    ast_copy_pj_str(*dst, src, pj_strlen(src) + 1);
00121 }
00122 
00123 static void set_redirecting_id(pjsip_name_addr *name_addr, struct ast_party_id *data,
00124                 struct ast_set_party_id *update)
00125 {
00126    pjsip_sip_uri *uri = pjsip_uri_get_uri(name_addr->uri);
00127 
00128    if (pj_strlen(&uri->user)) {
00129       update->number = 1;
00130       data->number.valid = 1;
00131       set_redirecting_value(&data->number.str, &uri->user);
00132    }
00133 
00134    if (pj_strlen(&name_addr->display)) {
00135       update->name = 1;
00136       data->name.valid = 1;
00137       set_redirecting_value(&data->name.str, &name_addr->display);
00138    }
00139 }
00140 
00141 static void copy_redirecting_id(struct ast_party_id *dst, const struct ast_party_id *src,
00142             struct ast_set_party_id *update)
00143 {
00144    ast_party_id_copy(dst, src);
00145 
00146    if (dst->number.valid) {
00147       update->number = 1;
00148    }
00149 
00150    if (dst->name.valid) {
00151       update->name = 1;
00152    }
00153 }
00154 
00155 static void set_redirecting_reason(pjsip_fromto_hdr *hdr,
00156                struct ast_party_redirecting_reason *data)
00157 {
00158    static const pj_str_t reason_name = { "reason", 6 };
00159    pjsip_param *reason = pjsip_param_find(&hdr->other_param, &reason_name);
00160 
00161    if (!reason) {
00162       return;
00163    }
00164 
00165    set_redirecting_value(&data->str, &reason->value);
00166    data->code = reason_str_to_code(data->str);
00167 }
00168 
00169 static void set_redirecting(struct ast_sip_session *session,
00170              pjsip_fromto_hdr *from_info,
00171              pjsip_name_addr *to_info)
00172 {
00173    struct ast_party_redirecting data;
00174    struct ast_set_party_redirecting update;
00175 
00176    if (!session->channel) {
00177       return;
00178    }
00179 
00180    ast_party_redirecting_init(&data);
00181    memset(&update, 0, sizeof(update));
00182 
00183    if (from_info) {
00184       set_redirecting_id((pjsip_name_addr*)from_info->uri,
00185          &data.from, &update.from);
00186       set_redirecting_reason(from_info, &data.reason);
00187    } else {
00188       copy_redirecting_id(&data.from, &session->id, &update.from);
00189    }
00190 
00191    set_redirecting_id(to_info, &data.to, &update.to);
00192 
00193    ast_set_party_id_all(&update.priv_orig);
00194    ast_set_party_id_all(&update.priv_from);
00195    ast_set_party_id_all(&update.priv_to);
00196    ++data.count;
00197 
00198    ast_channel_set_redirecting(session->channel, &data, &update);
00199    ast_party_redirecting_free(&data);
00200 }
00201 
00202 static int diversion_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
00203 {
00204    pjsip_fromto_hdr *hdr = get_diversion_header(rdata);
00205 
00206    if (hdr) {
00207       set_redirecting(session, hdr, (pjsip_name_addr*)
00208             PJSIP_MSG_TO_HDR(rdata->msg_info.msg)->uri);
00209    }
00210 
00211    return 0;
00212 }
00213 
00214 static void diversion_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
00215 {
00216    static const pj_str_t contact_name = { "Contact", 7 };
00217 
00218    pjsip_status_line status = rdata->msg_info.msg->line.status;
00219    pjsip_fromto_hdr *div_hdr;
00220    pjsip_contact_hdr *contact_hdr;
00221 
00222    if ((status.code != 302) && (status.code != 181)) {
00223       return;
00224    }
00225 
00226    /* use the diversion header info if there is one. if not one then use the
00227            session caller id info. if that doesn't exist use info from the To hdr*/
00228    if (!(div_hdr = get_diversion_header(rdata)) && !session->id.number.valid) {
00229       div_hdr = PJSIP_MSG_TO_HDR(rdata->msg_info.msg);
00230    }
00231 
00232    contact_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &contact_name, NULL);
00233 
00234    set_redirecting(session, div_hdr, contact_hdr ? (pjsip_name_addr*)contact_hdr->uri :
00235          (pjsip_name_addr*)PJSIP_MSG_FROM_HDR(rdata->msg_info.msg)->uri);
00236 }
00237 
00238 /*!
00239  * \internal
00240  * \brief Adds diversion header information to an outbound SIP message
00241  *
00242  * \param tdata The outbound message
00243  * \param data The redirecting data used to fill parts of the diversion header
00244  */
00245 static void add_diversion_header(pjsip_tx_data *tdata, struct ast_party_redirecting *data)
00246 {
00247    pjsip_fromto_hdr *hdr;
00248    pjsip_name_addr *name_addr;
00249    pjsip_sip_uri *uri;
00250    pjsip_param *param;
00251 
00252    struct ast_party_id *id = &data->from;
00253    pjsip_uri *base = PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
00254 
00255    if (!id->number.valid || ast_strlen_zero(id->number.str)) {
00256       return;
00257    }
00258 
00259    hdr = pjsip_from_hdr_create(tdata->pool);
00260    hdr->type = PJSIP_H_OTHER;
00261    pj_strdup(tdata->pool, &hdr->name, &diversion_name);
00262    hdr->sname.slen = 0;
00263 
00264    name_addr = pjsip_uri_clone(tdata->pool, base);
00265    uri = pjsip_uri_get_uri(name_addr->uri);
00266 
00267    pj_strdup2(tdata->pool, &name_addr->display, id->name.str);
00268    pj_strdup2(tdata->pool, &uri->user, id->number.str);
00269 
00270    param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
00271    param->name = pj_str("reason");
00272    param->value = pj_str((char*)reason_code_to_str(&data->reason));
00273    pj_list_insert_before(&hdr->other_param, param);
00274 
00275    hdr->uri = (pjsip_uri *) name_addr;
00276    pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
00277 }
00278 
00279 static void get_redirecting_add_diversion(struct ast_sip_session *session, pjsip_tx_data *tdata)
00280 {
00281    struct ast_party_redirecting *data;
00282 
00283    if (session->channel && session->endpoint->id.send_diversion &&
00284        (data = ast_channel_redirecting(session->channel))->count) {
00285       add_diversion_header(tdata, data);
00286    }
00287 }
00288 
00289 /*!
00290  * \internal
00291  * \brief Adds a diversion header to an outgoing INVITE request if
00292  *  redirecting information is available.
00293  *
00294  * \param session The session on which the INVITE request is to be sent
00295  * \param tdata The outbound INVITE request
00296  */
00297 static void diversion_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
00298 {
00299    get_redirecting_add_diversion(session, tdata);
00300 }
00301 
00302 /*!
00303  * \internal
00304  * \brief Adds a diversion header to an outgoing 3XX response
00305  *
00306  * \param session The session on which the INVITE response is to be sent
00307  * \param tdata The outbound INVITE response
00308  */
00309 static void diversion_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
00310 {
00311    struct pjsip_status_line status = tdata->msg->line.status;
00312 
00313    /* add to 302 and 181 */
00314    if (PJSIP_IS_STATUS_IN_CLASS(status.code, 300) || (status.code == 181)) {
00315       get_redirecting_add_diversion(session, tdata);
00316    }
00317 }
00318 
00319 static struct ast_sip_session_supplement diversion_supplement = {
00320    .method = "INVITE",
00321    /* this supplement needs to be called after caller id
00322            and after the channel has been created */
00323    .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 100,
00324    .incoming_request = diversion_incoming_request,
00325    .incoming_response = diversion_incoming_response,
00326    .outgoing_request = diversion_outgoing_request,
00327    .outgoing_response = diversion_outgoing_response,
00328    .response_priority = AST_SIP_SESSION_BEFORE_REDIRECTING,
00329 };
00330 
00331 static int load_module(void)
00332 {
00333    CHECK_PJSIP_SESSION_MODULE_LOADED();
00334 
00335    ast_sip_session_register_supplement(&diversion_supplement);
00336    return AST_MODULE_LOAD_SUCCESS;
00337 }
00338 
00339 static int unload_module(void)
00340 {
00341    ast_sip_session_unregister_supplement(&diversion_supplement);
00342    return 0;
00343 }
00344 
00345 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Add Diversion Header Support",
00346       .support_level = AST_MODULE_SUPPORT_CORE,
00347       .load = load_module,
00348       .unload = unload_module,
00349       .load_pri = AST_MODPRI_APP_DEPEND,
00350           );

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