res_pjsip_send_to_voicemail.c: Fix off-nominal double channel unref.
[asterisk/asterisk.git] / res / res_pjsip_send_to_voicemail.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Jonathan Rose <jrose@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Module for managing send to voicemail requests in SIP
22  *        REFER messages against PJSIP channels
23  *
24  * \author Jonathan Rose <jrose@digium.com>
25  */
26
27 /*** MODULEINFO
28          <depend>pjproject</depend>
29          <depend>res_pjsip</depend>
30          <depend>res_pjsip_session</depend>
31          <support_level>core</support_level>
32 ***/
33
34 #include "asterisk.h"
35
36 #include <pjsip.h>
37 #include <pjsip_ua.h>
38
39 #include "asterisk/pbx.h"
40 #include "asterisk/res_pjsip.h"
41 #include "asterisk/res_pjsip_session.h"
42 #include "asterisk/module.h"
43
44 #define DATASTORE_NAME "call_feature_send_to_vm_datastore"
45
46 #define SEND_TO_VM_HEADER "PJSIP_HEADER(add,X-Digium-Call-Feature)"
47 #define SEND_TO_VM_HEADER_VALUE "feature_send_to_vm"
48
49 #define SEND_TO_VM_REDIRECT "REDIRECTING(reason)"
50 #define SEND_TO_VM_REDIRECT_VALUE "\"send_to_vm\""
51
52 static void send_response(struct ast_sip_session *session, int code, struct pjsip_rx_data *rdata)
53 {
54         pjsip_tx_data *tdata;
55
56         if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, code, NULL, &tdata) == PJ_SUCCESS) {
57                 struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
58
59                 pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
60         }
61 }
62
63 static void channel_cleanup_wrapper(void *data)
64 {
65         struct ast_channel *chan = data;
66         ast_channel_cleanup(chan);
67 }
68
69 static struct ast_datastore_info call_feature_info = {
70         .type = "REFER call feature info",
71         .destroy = channel_cleanup_wrapper,
72 };
73
74 static pjsip_param *get_diversion_reason(pjsip_fromto_hdr *hdr)
75 {
76         static const pj_str_t reason_str = { "reason", 6 };
77         return pjsip_param_find(&hdr->other_param, &reason_str);
78 }
79
80 static pjsip_fromto_hdr *get_diversion_header(pjsip_rx_data *rdata)
81 {
82         static const pj_str_t from_str = { "From", 4 };
83         static const pj_str_t diversion_str = { "Diversion", 9 };
84
85         pjsip_generic_string_hdr *hdr;
86         pj_str_t value;
87
88         if (!(hdr = pjsip_msg_find_hdr_by_name(
89                       rdata->msg_info.msg, &diversion_str, NULL))) {
90                 return NULL;
91         }
92
93         pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue);
94
95         /* parse as a fromto header */
96         return pjsip_parse_hdr(rdata->tp_info.pool, &from_str, value.ptr,
97                                pj_strlen(&value), NULL);
98 }
99
100 static int has_diversion_reason(pjsip_rx_data *rdata)
101 {
102         pjsip_param *reason;
103         pjsip_fromto_hdr *hdr = get_diversion_header(rdata);
104
105         return hdr &&
106                 (reason = get_diversion_reason(hdr)) &&
107                 !pj_stricmp2(&reason->value, SEND_TO_VM_REDIRECT_VALUE);
108 }
109
110 static int has_call_feature(pjsip_rx_data *rdata)
111 {
112         static const pj_str_t call_feature_str = { "X-Digium-Call-Feature", 21 };
113
114         pjsip_generic_string_hdr *hdr = pjsip_msg_find_hdr_by_name(
115                 rdata->msg_info.msg, &call_feature_str, NULL);
116
117         return hdr && !pj_stricmp2(&hdr->hvalue, SEND_TO_VM_HEADER_VALUE);
118 }
119
120 static int handle_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
121 {
122         struct ast_datastore *sip_session_datastore;
123         struct ast_channel *other_party;
124         int has_feature;
125         int has_reason;
126
127         if (!session->channel) {
128                 return 0;
129         }
130
131         has_feature = has_call_feature(rdata);
132         has_reason = has_diversion_reason(rdata);
133         if (!has_feature && !has_reason) {
134                 /* If we don't have a call feature or diversion reason or if
135                    it's not a feature this module is related to then there
136                    is nothing to do. */
137                 return 0;
138         }
139
140         /* Check bridge status... */
141         other_party = ast_channel_bridge_peer(session->channel);
142         if (!other_party) {
143                 /* The channel wasn't in a two party bridge */
144                 ast_log(LOG_WARNING, "%s (%s) attempted to transfer to voicemail, "
145                         "but was not in a two party bridge.\n",
146                         ast_sorcery_object_get_id(session->endpoint),
147                         ast_channel_name(session->channel));
148                 send_response(session, 400, rdata);
149                 return -1;
150         }
151
152         sip_session_datastore = ast_sip_session_alloc_datastore(
153                 &call_feature_info, DATASTORE_NAME);
154         if (!sip_session_datastore) {
155                 ast_channel_unref(other_party);
156                 send_response(session, 500, rdata);
157                 return -1;
158         }
159
160         sip_session_datastore->data = other_party;
161
162         if (ast_sip_session_add_datastore(session, sip_session_datastore)) {
163                 ao2_ref(sip_session_datastore, -1);
164                 send_response(session, 500, rdata);
165                 return -1;
166         }
167
168         if (has_feature) {
169                 pbx_builtin_setvar_helper(other_party, SEND_TO_VM_HEADER,
170                                           SEND_TO_VM_HEADER_VALUE);
171         }
172
173         if (has_reason) {
174                 pbx_builtin_setvar_helper(other_party, SEND_TO_VM_REDIRECT,
175                                           SEND_TO_VM_REDIRECT_VALUE);
176         }
177
178         ao2_ref(sip_session_datastore, -1);
179         return 0;
180 }
181
182 static void handle_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
183 {
184         pjsip_status_line status = tdata->msg->line.status;
185         struct ast_datastore *feature_datastore =
186                 ast_sip_session_get_datastore(session, DATASTORE_NAME);
187         struct ast_channel *target_chan;
188
189         if (!feature_datastore) {
190                 return;
191         }
192
193         /* Since we are handling the response, there is no need to keep the datastore in the session anymore. */
194         ast_sip_session_remove_datastore(session, DATASTORE_NAME);
195
196         /* If the response >= 300, the refer failed and we need to clear the feature. */
197         if (status.code >= 300) {
198                 target_chan = feature_datastore->data;
199                 pbx_builtin_setvar_helper(target_chan, SEND_TO_VM_HEADER, NULL);
200                 pbx_builtin_setvar_helper(target_chan, SEND_TO_VM_REDIRECT, NULL);
201         }
202         ao2_ref(feature_datastore, -1);
203 }
204
205 static struct ast_sip_session_supplement refer_supplement = {
206         .method = "REFER",
207         .incoming_request = handle_incoming_request,
208         .outgoing_response = handle_outgoing_response,
209 };
210
211 static int load_module(void)
212 {
213         CHECK_PJSIP_SESSION_MODULE_LOADED();
214
215         if (ast_sip_session_register_supplement(&refer_supplement)) {
216                 ast_log(LOG_ERROR, "Unable to register Send to Voicemail supplement\n");
217                 return AST_MODULE_LOAD_FAILURE;
218         }
219
220         return AST_MODULE_LOAD_SUCCESS;
221 }
222
223 static int unload_module(void)
224 {
225         ast_sip_session_unregister_supplement(&refer_supplement);
226         return 0;
227 }
228
229 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP REFER Send to Voicemail Support",
230         .support_level = AST_MODULE_SUPPORT_CORE,
231         .load = load_module,
232         .unload = unload_module,
233         .load_pri = AST_MODPRI_APP_DEPEND,
234 );