Asterisk - The Open Source Telephony Project GIT-master-a358458
res_pjsip_send_to_voicemail.c
Go to the documentation of this file.
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"
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#define SEND_TO_VM_REDIRECT_QUOTED_VALUE "\"" SEND_TO_VM_REDIRECT_VALUE "\""
52
53static void send_response(struct ast_sip_session *session, int code, struct pjsip_rx_data *rdata)
54{
55 pjsip_tx_data *tdata;
56
57 if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, code, NULL, &tdata) == PJ_SUCCESS) {
58 struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
59
60 pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
61 }
62}
63
64static void channel_cleanup_wrapper(void *data)
65{
66 struct ast_channel *chan = data;
68}
69
71 .type = "REFER call feature info",
72 .destroy = channel_cleanup_wrapper,
73};
74
75static pjsip_param *get_diversion_reason(pjsip_fromto_hdr *hdr)
76{
77 static const pj_str_t reason_str = { "reason", 6 };
78 return pjsip_param_find(&hdr->other_param, &reason_str);
79}
80
81static pjsip_fromto_hdr *get_diversion_header(pjsip_rx_data *rdata)
82{
83 static const pj_str_t from_str = { "From", 4 };
84 static const pj_str_t diversion_str = { "Diversion", 9 };
85
86 pjsip_generic_string_hdr *hdr;
87 pj_str_t value;
88
89 if (!(hdr = pjsip_msg_find_hdr_by_name(
90 rdata->msg_info.msg, &diversion_str, NULL))) {
91 return NULL;
92 }
93
94 pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue);
95
96 /* parse as a fromto header */
97 return pjsip_parse_hdr(rdata->tp_info.pool, &from_str, value.ptr,
98 pj_strlen(&value), NULL);
99}
100
101static int has_diversion_reason(pjsip_rx_data *rdata)
102{
103 pjsip_param *reason;
104 pjsip_fromto_hdr *hdr = get_diversion_header(rdata);
105
106 if (!hdr) {
107 return 0;
108 }
109 reason = get_diversion_reason(hdr);
110 return reason
111 && (!pj_stricmp2(&reason->value, SEND_TO_VM_REDIRECT_QUOTED_VALUE)
112 || !pj_stricmp2(&reason->value, SEND_TO_VM_REDIRECT_VALUE));
113}
114
115static int has_call_feature(pjsip_rx_data *rdata)
116{
117 static const pj_str_t call_feature_str = { "X-Digium-Call-Feature", 21 };
118
119 pjsip_generic_string_hdr *hdr = pjsip_msg_find_hdr_by_name(
120 rdata->msg_info.msg, &call_feature_str, NULL);
121
122 return hdr && !pj_stricmp2(&hdr->hvalue, SEND_TO_VM_HEADER_VALUE);
123}
124
125static int handle_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
126{
127 struct ast_datastore *sip_session_datastore;
128 struct ast_channel *other_party;
129 int has_feature;
130 int has_reason;
131
132 if (!session->channel) {
133 return 0;
134 }
135
136 has_feature = has_call_feature(rdata);
137 has_reason = has_diversion_reason(rdata);
138 if (!has_feature && !has_reason) {
139 /* If we don't have a call feature or diversion reason or if
140 it's not a feature this module is related to then there
141 is nothing to do. */
142 return 0;
143 }
144
145 /* Check bridge status... */
146 other_party = ast_channel_bridge_peer(session->channel);
147 if (!other_party) {
148 /* The channel wasn't in a two party bridge */
149 ast_log(LOG_WARNING, "%s (%s) attempted to transfer to voicemail, "
150 "but was not in a two party bridge.\n",
152 ast_channel_name(session->channel));
153 send_response(session, 400, rdata);
154 return -1;
155 }
156
157 sip_session_datastore = ast_sip_session_alloc_datastore(
159 if (!sip_session_datastore) {
160 ast_channel_unref(other_party);
161 send_response(session, 500, rdata);
162 return -1;
163 }
164
165 sip_session_datastore->data = other_party;
166
167 if (ast_sip_session_add_datastore(session, sip_session_datastore)) {
168 ao2_ref(sip_session_datastore, -1);
169 send_response(session, 500, rdata);
170 return -1;
171 }
172
173 if (has_feature) {
176 }
177
178 if (has_reason) {
181 }
182
183 ao2_ref(sip_session_datastore, -1);
184 return 0;
185}
186
187static void handle_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
188{
189 pjsip_status_line status = tdata->msg->line.status;
190 struct ast_datastore *feature_datastore =
192 struct ast_channel *target_chan;
193
194 if (!feature_datastore) {
195 return;
196 }
197
198 /* Since we are handling the response, there is no need to keep the datastore in the session anymore. */
200
201 /* If the response >= 300, the refer failed and we need to clear the feature. */
202 if (status.code >= 300) {
203 target_chan = feature_datastore->data;
206 }
207 ao2_ref(feature_datastore, -1);
208}
209
211 .method = "REFER",
212 .incoming_request = handle_incoming_request,
213 .outgoing_response = handle_outgoing_response,
214};
215
216static int load_module(void)
217{
219
221}
222
223static int unload_module(void)
224{
226 return 0;
227}
228
229AST_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 .requires = "res_pjsip,res_pjsip_session",
jack_status_t status
Definition: app_jack.c:146
Asterisk main include file. File version handling, generic pbx functions.
static struct ast_mansession session
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
const char * ast_channel_name(const struct ast_channel *chan)
struct ast_channel * ast_channel_bridge_peer(struct ast_channel *chan)
Get the channel's bridge peer only if the bridge is two-party.
Definition: channel.c:10564
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
#define ast_channel_cleanup(c)
Cleanup a channel reference.
Definition: channel.h:2969
#define LOG_WARNING
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:317
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODPRI_APP_DEPEND
Definition: module.h:328
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
Core PBX routines and definitions.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
static void send_response(struct ast_sip_session *session, int code, struct pjsip_rx_data *rdata)
#define SEND_TO_VM_HEADER_VALUE
static struct ast_sip_session_supplement refer_supplement
#define SEND_TO_VM_REDIRECT_VALUE
static int has_diversion_reason(pjsip_rx_data *rdata)
static int handle_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
static void channel_cleanup_wrapper(void *data)
static struct ast_datastore_info call_feature_info
#define SEND_TO_VM_REDIRECT_QUOTED_VALUE
static pjsip_fromto_hdr * get_diversion_header(pjsip_rx_data *rdata)
#define SEND_TO_VM_REDIRECT
#define DATASTORE_NAME
static int load_module(void)
static void handle_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
static int unload_module(void)
static pjsip_param * get_diversion_reason(pjsip_fromto_hdr *hdr)
static int has_call_feature(pjsip_rx_data *rdata)
#define SEND_TO_VM_HEADER
struct ast_datastore * ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name)
Retrieve a session datastore.
int ast_sip_session_add_datastore(struct ast_sip_session *session, struct ast_datastore *datastore)
Add a datastore to a SIP session.
void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
Remove a session datastore from the session.
#define ast_sip_session_register_supplement(supplement)
struct ast_datastore * ast_sip_session_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
Alternative for ast_datastore_alloc()
void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement)
Unregister a an supplement to SIP session processing.
Definition: pjsip_session.c:63
#define NULL
Definition: resample.c:96
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
Main Channel structure associated with a channel.
const char * data
Structure for a data store type.
Definition: datastore.h:31
const char * type
Definition: datastore.h:32
Structure for a data store object.
Definition: datastore.h:64
void * data
Definition: datastore.h:66
A supplement to SIP message processing.
A structure describing a SIP session.
int value
Definition: syslog.c:37