CHAOS: res_pjsip_diversion avoid crash if allocation fails
[asterisk/asterisk.git] / res / res_pjsip_diversion.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Kevin Harwell <kharwell@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 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <depend>res_pjsip_session</depend>
23         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29 #include <pjsip_ua.h>
30
31 #include "asterisk/res_pjsip.h"
32 #include "asterisk/res_pjsip_session.h"
33 #include "asterisk/callerid.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/module.h"
36 #include "asterisk/strings.h"
37
38 static const pj_str_t diversion_name = { "Diversion", 9 };
39
40 /*! \brief Diversion header reasons
41  *
42  * The core defines a bunch of constants used to define
43  * redirecting reasons. This provides a translation table
44  * between those and the strings which may be present in
45  * a SIP Diversion header
46  */
47 static const struct reasons {
48         enum AST_REDIRECTING_REASON code;
49         char *const text;
50 } reason_table[] = {
51         { AST_REDIRECTING_REASON_UNKNOWN, "unknown" },
52         { AST_REDIRECTING_REASON_USER_BUSY, "user-busy" },
53         { AST_REDIRECTING_REASON_NO_ANSWER, "no-answer" },
54         { AST_REDIRECTING_REASON_UNAVAILABLE, "unavailable" },
55         { AST_REDIRECTING_REASON_UNCONDITIONAL, "unconditional" },
56         { AST_REDIRECTING_REASON_TIME_OF_DAY, "time-of-day" },
57         { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "do-not-disturb" },
58         { AST_REDIRECTING_REASON_DEFLECTION, "deflection" },
59         { AST_REDIRECTING_REASON_FOLLOW_ME, "follow-me" },
60         { AST_REDIRECTING_REASON_OUT_OF_ORDER, "out-of-service" },
61         { AST_REDIRECTING_REASON_AWAY, "away" },
62         { AST_REDIRECTING_REASON_CALL_FWD_DTE, "unknown"},
63         { AST_REDIRECTING_REASON_SEND_TO_VM, "send_to_vm"},
64 };
65
66 static const char *reason_code_to_str(const struct ast_party_redirecting_reason *reason)
67 {
68         int code = reason->code;
69
70         /* use specific string if given */
71         if (!ast_strlen_zero(reason->str)) {
72                 return reason->str;
73         }
74
75         if (code >= 0 && code < ARRAY_LEN(reason_table)) {
76                 return reason_table[code].text;
77         }
78
79         return "unknown";
80 }
81
82 static enum AST_REDIRECTING_REASON reason_str_to_code(const char *text)
83 {
84         enum AST_REDIRECTING_REASON code = AST_REDIRECTING_REASON_UNKNOWN;
85         int i;
86
87         for (i = 0; i < ARRAY_LEN(reason_table); ++i) {
88                 if (!strcasecmp(text, reason_table[i].text)) {
89                         code = reason_table[i].code;
90                         break;
91                 }
92         }
93
94         return code;
95 }
96
97 static pjsip_fromto_hdr *get_diversion_header(pjsip_rx_data *rdata)
98 {
99         static const pj_str_t from_name = { "From", 4 };
100
101         pjsip_generic_string_hdr *hdr;
102         pj_str_t value;
103         int size;
104
105         if (!(hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &diversion_name, NULL))) {
106                 return NULL;
107         }
108
109         pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue);
110
111         /* parse as a fromto header */
112         return pjsip_parse_hdr(rdata->tp_info.pool, &from_name, value.ptr,
113                                pj_strlen(&value), &size);
114 }
115
116 static void set_redirecting_value(char **dst, const pj_str_t *src)
117 {
118         ast_free(*dst);
119         *dst = ast_malloc(pj_strlen(src) + 1);
120         if (*dst) {
121                 ast_copy_pj_str(*dst, src, pj_strlen(src) + 1);
122         }
123 }
124
125 static void set_redirecting_id(pjsip_name_addr *name_addr, struct ast_party_id *data,
126                                struct ast_set_party_id *update)
127 {
128         pjsip_sip_uri *uri = pjsip_uri_get_uri(name_addr->uri);
129
130         if (pj_strlen(&uri->user)) {
131                 update->number = 1;
132                 data->number.valid = 1;
133                 set_redirecting_value(&data->number.str, &uri->user);
134         }
135
136         if (pj_strlen(&name_addr->display)) {
137                 update->name = 1;
138                 data->name.valid = 1;
139                 set_redirecting_value(&data->name.str, &name_addr->display);
140         }
141 }
142
143 static void copy_redirecting_id(struct ast_party_id *dst, const struct ast_party_id *src,
144                                 struct ast_set_party_id *update)
145 {
146         ast_party_id_copy(dst, src);
147
148         if (dst->number.valid) {
149                 update->number = 1;
150         }
151
152         if (dst->name.valid) {
153                 update->name = 1;
154         }
155 }
156
157 static void set_redirecting_reason(pjsip_fromto_hdr *hdr,
158                                    struct ast_party_redirecting_reason *data)
159 {
160         static const pj_str_t reason_name = { "reason", 6 };
161         pjsip_param *reason = pjsip_param_find(&hdr->other_param, &reason_name);
162
163         if (!reason) {
164                 return;
165         }
166
167         set_redirecting_value(&data->str, &reason->value);
168         data->code = reason_str_to_code(data->str);
169 }
170
171 static void set_redirecting(struct ast_sip_session *session,
172                             pjsip_fromto_hdr *from_info,
173                             pjsip_name_addr *to_info)
174 {
175         struct ast_party_redirecting data;
176         struct ast_set_party_redirecting update;
177
178         if (!session->channel) {
179                 return;
180         }
181
182         ast_party_redirecting_init(&data);
183         memset(&update, 0, sizeof(update));
184
185         if (from_info) {
186                 set_redirecting_id((pjsip_name_addr*)from_info->uri,
187                         &data.from, &update.from);
188                 set_redirecting_reason(from_info, &data.reason);
189         } else {
190                 copy_redirecting_id(&data.from, &session->id, &update.from);
191         }
192
193         set_redirecting_id(to_info, &data.to, &update.to);
194
195         ast_set_party_id_all(&update.priv_orig);
196         ast_set_party_id_all(&update.priv_from);
197         ast_set_party_id_all(&update.priv_to);
198         ++data.count;
199
200         ast_channel_set_redirecting(session->channel, &data, &update);
201         ast_party_redirecting_free(&data);
202 }
203
204 static int diversion_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
205 {
206         pjsip_fromto_hdr *hdr = get_diversion_header(rdata);
207
208         if (hdr) {
209                 set_redirecting(session, hdr, (pjsip_name_addr*)
210                                 PJSIP_MSG_TO_HDR(rdata->msg_info.msg)->uri);
211         }
212
213         return 0;
214 }
215
216 static void diversion_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
217 {
218         static const pj_str_t contact_name = { "Contact", 7 };
219
220         pjsip_status_line status = rdata->msg_info.msg->line.status;
221         pjsip_fromto_hdr *div_hdr;
222         pjsip_contact_hdr *contact_hdr;
223
224         if ((status.code != 302) && (status.code != 181)) {
225                 return;
226         }
227
228         /* use the diversion header info if there is one. if not one then use the
229            session caller id info. if that doesn't exist use info from the To hdr*/
230         if (!(div_hdr = get_diversion_header(rdata)) && !session->id.number.valid) {
231                 div_hdr = PJSIP_MSG_TO_HDR(rdata->msg_info.msg);
232         }
233
234         contact_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &contact_name, NULL);
235
236         set_redirecting(session, div_hdr, contact_hdr ? (pjsip_name_addr*)contact_hdr->uri :
237                         (pjsip_name_addr*)PJSIP_MSG_FROM_HDR(rdata->msg_info.msg)->uri);
238 }
239
240 /*!
241  * \internal
242  * \brief Adds diversion header information to an outbound SIP message
243  *
244  * \param tdata The outbound message
245  * \param data The redirecting data used to fill parts of the diversion header
246  */
247 static void add_diversion_header(pjsip_tx_data *tdata, struct ast_party_redirecting *data)
248 {
249         pjsip_fromto_hdr *hdr;
250         pjsip_name_addr *name_addr;
251         pjsip_sip_uri *uri;
252         pjsip_param *param;
253         pjsip_fromto_hdr *old_hdr;
254
255         struct ast_party_id *id = &data->from;
256         pjsip_uri *base = PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
257
258         if (!id->number.valid || ast_strlen_zero(id->number.str)) {
259                 return;
260         }
261
262         hdr = pjsip_from_hdr_create(tdata->pool);
263         hdr->type = PJSIP_H_OTHER;
264         pj_strdup(tdata->pool, &hdr->name, &diversion_name);
265         hdr->sname.slen = 0;
266
267         name_addr = pjsip_uri_clone(tdata->pool, base);
268         uri = pjsip_uri_get_uri(name_addr->uri);
269
270         pj_strdup2(tdata->pool, &name_addr->display, id->name.str);
271         pj_strdup2(tdata->pool, &uri->user, id->number.str);
272
273         param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
274         param->name = pj_str("reason");
275         param->value = pj_str((char*)reason_code_to_str(&data->reason));
276         pj_list_insert_before(&hdr->other_param, param);
277
278         hdr->uri = (pjsip_uri *) name_addr;
279         old_hdr = pjsip_msg_find_hdr_by_name(tdata->msg, &diversion_name, NULL);
280         if (old_hdr) {
281                 pj_list_erase(old_hdr);
282         }
283         pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
284 }
285
286 static void get_redirecting_add_diversion(struct ast_sip_session *session, pjsip_tx_data *tdata)
287 {
288         struct ast_party_redirecting *data;
289
290         if (session->channel && session->endpoint->id.send_diversion &&
291             (data = ast_channel_redirecting(session->channel))->count) {
292                 add_diversion_header(tdata, data);
293         }
294 }
295
296 /*!
297  * \internal
298  * \brief Adds a diversion header to an outgoing INVITE request if
299  *  redirecting information is available.
300  *
301  * \param session The session on which the INVITE request is to be sent
302  * \param tdata The outbound INVITE request
303  */
304 static void diversion_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
305 {
306         get_redirecting_add_diversion(session, tdata);
307 }
308
309 /*!
310  * \internal
311  * \brief Adds a diversion header to an outgoing 3XX response
312  *
313  * \param session The session on which the INVITE response is to be sent
314  * \param tdata The outbound INVITE response
315  */
316 static void diversion_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
317 {
318         struct pjsip_status_line status = tdata->msg->line.status;
319
320         /* add to 302 and 181 */
321         if (PJSIP_IS_STATUS_IN_CLASS(status.code, 300) || (status.code == 181)) {
322                 get_redirecting_add_diversion(session, tdata);
323         }
324 }
325
326 static struct ast_sip_session_supplement diversion_supplement = {
327         .method = "INVITE",
328         /* this supplement needs to be called after caller id
329            and after the channel has been created */
330         .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 100,
331         .incoming_request = diversion_incoming_request,
332         .incoming_response = diversion_incoming_response,
333         .outgoing_request = diversion_outgoing_request,
334         .outgoing_response = diversion_outgoing_response,
335         .response_priority = AST_SIP_SESSION_BEFORE_REDIRECTING,
336 };
337
338 static int load_module(void)
339 {
340         CHECK_PJSIP_SESSION_MODULE_LOADED();
341
342         ast_sip_session_register_supplement(&diversion_supplement);
343         return AST_MODULE_LOAD_SUCCESS;
344 }
345
346 static int unload_module(void)
347 {
348         ast_sip_session_unregister_supplement(&diversion_supplement);
349         return 0;
350 }
351
352 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Add Diversion Header Support",
353         .support_level = AST_MODULE_SUPPORT_CORE,
354         .load = load_module,
355         .unload = unload_module,
356         .load_pri = AST_MODPRI_APP_DEPEND,
357 );