PJSIP: Add Path header support
[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         ast_copy_pj_str(*dst, src, pj_strlen(src) + 1);
121 }
122
123 static void set_redirecting_id(pjsip_name_addr *name_addr, struct ast_party_id *data,
124                                struct ast_set_party_id *update)
125 {
126         pjsip_sip_uri *uri = pjsip_uri_get_uri(name_addr->uri);
127
128         if (pj_strlen(&uri->user)) {
129                 update->number = 1;
130                 data->number.valid = 1;
131                 set_redirecting_value(&data->number.str, &uri->user);
132         }
133
134         if (pj_strlen(&name_addr->display)) {
135                 update->name = 1;
136                 data->name.valid = 1;
137                 set_redirecting_value(&data->name.str, &name_addr->display);
138         }
139 }
140
141 static void copy_redirecting_id(struct ast_party_id *dst, const struct ast_party_id *src,
142                                 struct ast_set_party_id *update)
143 {
144         ast_party_id_copy(dst, src);
145
146         if (dst->number.valid) {
147                 update->number = 1;
148         }
149
150         if (dst->name.valid) {
151                 update->name = 1;
152         }
153 }
154
155 static void set_redirecting_reason(pjsip_fromto_hdr *hdr,
156                                    struct ast_party_redirecting_reason *data)
157 {
158         static const pj_str_t reason_name = { "reason", 6 };
159         pjsip_param *reason = pjsip_param_find(&hdr->other_param, &reason_name);
160
161         if (!reason) {
162                 return;
163         }
164
165         set_redirecting_value(&data->str, &reason->value);
166         data->code = reason_str_to_code(data->str);
167 }
168
169 static void set_redirecting(struct ast_sip_session *session,
170                             pjsip_fromto_hdr *from_info,
171                             pjsip_name_addr *to_info)
172 {
173         struct ast_party_redirecting data;
174         struct ast_set_party_redirecting update;
175
176         if (!session->channel) {
177                 return;
178         }
179
180         ast_party_redirecting_init(&data);
181         memset(&update, 0, sizeof(update));
182
183         if (from_info) {
184                 set_redirecting_id((pjsip_name_addr*)from_info->uri,
185                         &data.from, &update.from);
186                 set_redirecting_reason(from_info, &data.reason);
187         } else {
188                 copy_redirecting_id(&data.from, &session->id, &update.from);
189         }
190
191         set_redirecting_id(to_info, &data.to, &update.to);
192
193         ast_set_party_id_all(&update.priv_orig);
194         ast_set_party_id_all(&update.priv_from);
195         ast_set_party_id_all(&update.priv_to);
196         ++data.count;
197
198         ast_channel_set_redirecting(session->channel, &data, &update);
199         ast_party_redirecting_free(&data);
200 }
201
202 static int diversion_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
203 {
204         pjsip_fromto_hdr *hdr = get_diversion_header(rdata);
205
206         if (hdr) {
207                 set_redirecting(session, hdr, (pjsip_name_addr*)
208                                 PJSIP_MSG_TO_HDR(rdata->msg_info.msg)->uri);
209         }
210
211         return 0;
212 }
213
214 static void diversion_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
215 {
216         static const pj_str_t contact_name = { "Contact", 7 };
217
218         pjsip_status_line status = rdata->msg_info.msg->line.status;
219         pjsip_fromto_hdr *div_hdr;
220         pjsip_contact_hdr *contact_hdr;
221
222         if ((status.code != 302) && (status.code != 181)) {
223                 return;
224         }
225
226         /* use the diversion header info if there is one. if not one then use the
227            session caller id info. if that doesn't exist use info from the To hdr*/
228         if (!(div_hdr = get_diversion_header(rdata)) && !session->id.number.valid) {
229                 div_hdr = PJSIP_MSG_TO_HDR(rdata->msg_info.msg);
230         }
231
232         contact_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &contact_name, NULL);
233
234         set_redirecting(session, div_hdr, contact_hdr ? (pjsip_name_addr*)contact_hdr->uri :
235                         (pjsip_name_addr*)PJSIP_MSG_FROM_HDR(rdata->msg_info.msg)->uri);
236 }
237
238 /*!
239  * \internal
240  * \brief Adds diversion header information to an outbound SIP message
241  *
242  * \param tdata The outbound message
243  * \param data The redirecting data used to fill parts of the diversion header
244  */
245 static void add_diversion_header(pjsip_tx_data *tdata, struct ast_party_redirecting *data)
246 {
247         pjsip_fromto_hdr *hdr;
248         pjsip_name_addr *name_addr;
249         pjsip_sip_uri *uri;
250         pjsip_param *param;
251
252         struct ast_party_id *id = &data->from;
253         pjsip_uri *base = PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
254
255         if (!id->number.valid || ast_strlen_zero(id->number.str)) {
256                 return;
257         }
258
259         hdr = pjsip_from_hdr_create(tdata->pool);
260         hdr->type = PJSIP_H_OTHER;
261         pj_strdup(tdata->pool, &hdr->name, &diversion_name);
262         hdr->sname.slen = 0;
263
264         name_addr = pjsip_uri_clone(tdata->pool, base);
265         uri = pjsip_uri_get_uri(name_addr->uri);
266
267         pj_strdup2(tdata->pool, &name_addr->display, id->name.str);
268         pj_strdup2(tdata->pool, &uri->user, id->number.str);
269
270         param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
271         param->name = pj_str("reason");
272         param->value = pj_str((char*)reason_code_to_str(&data->reason));
273         pj_list_insert_before(&hdr->other_param, param);
274
275         hdr->uri = (pjsip_uri *) name_addr;
276         pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
277 }
278
279 static void get_redirecting_add_diversion(struct ast_sip_session *session, pjsip_tx_data *tdata)
280 {
281         struct ast_party_redirecting *data;
282
283         if (session->channel && session->endpoint->id.send_diversion &&
284             (data = ast_channel_redirecting(session->channel))->count) {
285                 add_diversion_header(tdata, data);
286         }
287 }
288
289 /*!
290  * \internal
291  * \brief Adds a diversion header to an outgoing INVITE request if
292  *  redirecting information is available.
293  *
294  * \param session The session on which the INVITE request is to be sent
295  * \param tdata The outbound INVITE request
296  */
297 static void diversion_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
298 {
299         get_redirecting_add_diversion(session, tdata);
300 }
301
302 /*!
303  * \internal
304  * \brief Adds a diversion header to an outgoing 3XX response
305  *
306  * \param session The session on which the INVITE response is to be sent
307  * \param tdata The outbound INVITE response
308  */
309 static void diversion_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
310 {
311         struct pjsip_status_line status = tdata->msg->line.status;
312
313         /* add to 302 and 181 */
314         if (PJSIP_IS_STATUS_IN_CLASS(status.code, 300) || (status.code == 181)) {
315                 get_redirecting_add_diversion(session, tdata);
316         }
317 }
318
319 static struct ast_sip_session_supplement diversion_supplement = {
320         .method = "INVITE",
321         /* this supplement needs to be called after caller id
322            and after the channel has been created */
323         .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 100,
324         .incoming_request = diversion_incoming_request,
325         .incoming_response = diversion_incoming_response,
326         .outgoing_request = diversion_outgoing_request,
327         .outgoing_response = diversion_outgoing_response,
328 };
329
330 static int load_module(void)
331 {
332         ast_sip_session_register_supplement(&diversion_supplement);
333         return AST_MODULE_LOAD_SUCCESS;
334 }
335
336 static int unload_module(void)
337 {
338         ast_sip_session_unregister_supplement(&diversion_supplement);
339         return 0;
340 }
341
342 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Add Diversion Header Support",
343                 .load = load_module,
344                 .unload = unload_module,
345                 .load_pri = AST_MODPRI_APP_DEPEND,
346                );