res_pjsip_diversion: implement support for History-Info
[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 static const pj_str_t history_info_name = { "History-Info", 12 };
40 static pj_str_t HISTINFO_SUPPORTED_NAME = { "histinfo", 8 };
41
42 /*!
43  * \internal
44  * \brief Determine if the given string is a SIP token.
45  * \since 13.8.0
46  *
47  * \param str String to determine if is a SIP token.
48  *
49  * \note A token is defined by RFC3261 Section 25.1
50  *
51  * \return Non-zero if the string is a SIP token.
52  */
53 static int sip_is_token(const char *str)
54 {
55         int is_token;
56
57         if (ast_strlen_zero(str)) {
58                 /* An empty string is not a token. */
59                 return 0;
60         }
61
62         is_token = 1;
63         do {
64                 if (!isalnum(*str)
65                         && !strchr("-.!%*_+`'~", *str)) {
66                         /* The character is not allowed in a token. */
67                         is_token = 0;
68                         break;
69                 }
70         } while (*++str);
71
72         return is_token;
73 }
74
75 /*! \brief Diversion header reasons
76  *
77  * The core defines a bunch of constants used to define
78  * redirecting reasons. This provides a translation table
79  * between those and the strings which may be present in
80  * a SIP Diversion header
81  */
82 static const struct reasons {
83         enum AST_REDIRECTING_REASON code;
84         const char *text;
85         const unsigned int cause;
86 } reason_table[] = {
87         { AST_REDIRECTING_REASON_UNKNOWN, "unknown", 404 },
88         { AST_REDIRECTING_REASON_USER_BUSY, "user-busy", 486 },
89         { AST_REDIRECTING_REASON_NO_ANSWER, "no-answer", 408 },
90         { AST_REDIRECTING_REASON_UNAVAILABLE, "unavailable", 503 },
91         { AST_REDIRECTING_REASON_UNCONDITIONAL, "unconditional", 302 },
92         { AST_REDIRECTING_REASON_TIME_OF_DAY, "time-of-day", 404 },
93         { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "do-not-disturb", 404 },
94         { AST_REDIRECTING_REASON_DEFLECTION, "deflection", 480 },
95         { AST_REDIRECTING_REASON_FOLLOW_ME, "follow-me", 404 },
96         { AST_REDIRECTING_REASON_OUT_OF_ORDER, "out-of-service", 404 },
97         { AST_REDIRECTING_REASON_AWAY, "away", 404 },
98         { AST_REDIRECTING_REASON_CALL_FWD_DTE, "cf_dte", 404 },         /* Non-standard */
99         { AST_REDIRECTING_REASON_SEND_TO_VM, "send_to_vm", 404 },       /* Non-standard */
100 };
101
102 static enum AST_REDIRECTING_REASON cause_to_reason(const unsigned long cause) {
103         switch(cause) {
104                 case 302:
105                         return AST_REDIRECTING_REASON_UNCONDITIONAL;
106                 case 486:
107                         return AST_REDIRECTING_REASON_USER_BUSY;
108                 case 408:
109                         return AST_REDIRECTING_REASON_NO_ANSWER;
110                 case 480:
111                 case 487:
112                         return AST_REDIRECTING_REASON_DEFLECTION;
113                 case 503:
114                         return AST_REDIRECTING_REASON_UNAVAILABLE;
115                 default:
116                         return AST_REDIRECTING_REASON_UNKNOWN;
117         }
118 }
119
120 static int add_supported(pjsip_tx_data *tdata)
121 {
122         pjsip_supported_hdr *hdr;
123
124         hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_SUPPORTED, NULL);
125         if (!hdr) {
126                 /* insert a new Supported header */
127                 hdr = pjsip_supported_hdr_create(tdata->pool);
128                 if (!hdr) {
129                         return -1;
130                 }
131
132                 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
133         }
134
135         /* add on to the existing Supported header */
136         pj_strassign(&hdr->values[hdr->count++], &HISTINFO_SUPPORTED_NAME);
137
138         return 0;
139 }
140
141 static const char *reason_code_to_str(const struct ast_party_redirecting_reason *reason)
142 {
143         int idx;
144         int code;
145
146         /* use specific string if given */
147         if (!ast_strlen_zero(reason->str)) {
148                 return reason->str;
149         }
150
151         code = reason->code;
152         for (idx = 0; idx < ARRAY_LEN(reason_table); ++idx) {
153                 if (code == reason_table[idx].code) {
154                         return reason_table[idx].text;
155                 }
156         }
157
158         return "unknown";
159 }
160
161 static const unsigned int reason_code_to_cause(const struct ast_party_redirecting_reason *reason)
162 {
163         int idx;
164         int code;
165
166         code = reason->code;
167         for (idx = 0; idx < ARRAY_LEN(reason_table); ++idx) {
168                 if (code == reason_table[idx].code) {
169                         return reason_table[idx].cause;
170                 }
171         }
172
173         return 404;
174 }
175
176 static pjsip_fromto_hdr *get_diversion_header(pjsip_rx_data *rdata)
177 {
178         static const pj_str_t from_name = { "From", 4 };
179
180         pjsip_generic_string_hdr *hdr;
181         pj_str_t value;
182         int size;
183
184         if (!(hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &diversion_name, NULL))) {
185                 return NULL;
186         }
187
188         pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue);
189
190         /* parse as a fromto header */
191         return pjsip_parse_hdr(rdata->tp_info.pool, &from_name, value.ptr,
192                                pj_strlen(&value), &size);
193 }
194
195 /* Asterisk keeps track of 2 things. The redirected from address and
196  * the redirected to address. If first=0 method will get the most recent
197  * redirection target for use as the redirected to address. If first=1
198  * then this method will get the original redirection target (index=1)
199  * for use as the redirected from address.
200  */
201 static pjsip_fromto_hdr *get_history_info_header(pjsip_rx_data *rdata, const unsigned int first)
202 {
203         static const pj_str_t from_name = { "From", 4 };
204         pjsip_fromto_hdr * result_hdr = NULL;
205
206         pjsip_generic_string_hdr *hdr = NULL;
207
208         hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &history_info_name, NULL);
209
210         if (!hdr) {
211                 return NULL;
212         }
213
214         do {
215                 static const pj_str_t index_name = { "index", 5 };
216                 pj_str_t value;
217                 int size;
218                 pjsip_fromto_hdr * fromto_hdr = NULL;
219                 pjsip_param * index = NULL;
220
221                 pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue);
222
223                 /* parse as a fromto header */
224                 fromto_hdr =  pjsip_parse_hdr(rdata->tp_info.pool, &from_name, value.ptr,
225                                        pj_strlen(&value), &size);
226
227                 if (fromto_hdr == NULL) {
228                         continue;
229                 }
230
231                 index = pjsip_param_find(&fromto_hdr->other_param, &index_name);
232
233                 if (index) {
234                         if (!pj_strcmp2(&index->value, "1")) {
235                                 if (!first) {
236                                         continue;
237                                 } else {
238                                         return fromto_hdr;
239                                 }
240                         }
241                 }
242
243                 result_hdr = fromto_hdr;
244
245         } while ((hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &history_info_name, hdr->next)));
246
247         return result_hdr;
248 }
249
250 static void set_redirecting_value(char **dst, const pj_str_t *src)
251 {
252         ast_free(*dst);
253         *dst = ast_malloc(pj_strlen(src) + 1);
254         if (*dst) {
255                 ast_copy_pj_str(*dst, src, pj_strlen(src) + 1);
256         }
257 }
258
259 static void set_redirecting_id(pjsip_name_addr *name_addr, struct ast_party_id *data,
260                                struct ast_set_party_id *update)
261 {
262         pjsip_sip_uri *uri = pjsip_uri_get_uri(name_addr->uri);
263         char *semi;
264         pj_str_t uri_user;
265
266         uri_user = uri->user;
267
268         /* Always truncate redirecting number at a semicolon. */
269         semi = pj_strchr(&uri_user, ';');
270         if (semi) {
271                 /*
272                  * We need to be able to handle URI's looking like
273                  * "sip:1235557890;phone-context=national@x.x.x.x;user=phone"
274                  *
275                  * Where the uri->user field will result in:
276                  * "1235557890;phone-context=national"
277                  *
278                  * People don't care about anything after the semicolon
279                  * showing up on their displays even though the RFC
280                  * allows the semicolon.
281                  */
282                 pj_strset(&uri_user, (char *) pj_strbuf(&uri_user), semi - pj_strbuf(&uri_user));
283         }
284
285         if (pj_strlen(&uri_user)) {
286                 update->number = 1;
287                 data->number.valid = 1;
288                 set_redirecting_value(&data->number.str, &uri_user);
289         }
290
291         if (pj_strlen(&name_addr->display)) {
292                 update->name = 1;
293                 data->name.valid = 1;
294                 set_redirecting_value(&data->name.str, &name_addr->display);
295         }
296 }
297
298 static void copy_redirecting_id(struct ast_party_id *dst, const struct ast_party_id *src,
299                                 struct ast_set_party_id *update)
300 {
301         ast_party_id_copy(dst, src);
302
303         if (dst->number.valid) {
304                 update->number = 1;
305         }
306
307         if (dst->name.valid) {
308                 update->name = 1;
309         }
310 }
311
312 static void set_redirecting_reason_by_cause(pjsip_name_addr *name_addr,
313                                    struct ast_party_redirecting_reason *data)
314 {
315         static const pj_str_t cause_name = { "cause", 5 };
316         pjsip_sip_uri *uri = pjsip_uri_get_uri(name_addr);
317         pjsip_param *cause = pjsip_param_find(&uri->other_param, &cause_name);
318         unsigned long cause_value;
319
320         if (!cause) {
321                 return;
322         }
323
324         cause_value = pj_strtoul(&cause->value);
325
326         data->code = cause_to_reason(cause_value);
327         ast_free(data->str);
328         data->str = ast_strdup("");
329 }
330
331 static void set_redirecting_reason(pjsip_fromto_hdr *from_info, pjsip_name_addr *to_info,
332                                    struct ast_party_redirecting_reason *data)
333 {
334         static const pj_str_t reason_name = { "reason", 6 };
335         pjsip_param *reason = pjsip_param_find(&from_info->other_param, &reason_name);
336         char *reason_str;
337
338         if (!reason) {
339                 if (to_info) {
340                         set_redirecting_reason_by_cause(to_info, data);
341                 }
342                 return;
343         }
344
345         set_redirecting_value(&data->str, &reason->value);
346         if (!data->str) {
347                 /* Oops, allocation failure */
348                 return;
349         }
350         reason_str = ast_strdupa(data->str);
351
352         /* Remove any enclosing double-quotes */
353         if (*reason_str == '"') {
354                 reason_str = ast_strip_quoted(reason_str, "\"", "\"");
355         }
356
357         data->code = ast_redirecting_reason_parse(reason_str);
358         if (data->code < 0) {
359                 data->code = AST_REDIRECTING_REASON_UNKNOWN;
360         } else {
361                 ast_free(data->str);
362                 data->str = ast_strdup("");
363         }
364 }
365
366 static void set_redirecting(struct ast_sip_session *session,
367                             pjsip_fromto_hdr *from_info,
368                             pjsip_name_addr *to_info)
369 {
370         struct ast_party_redirecting data;
371         struct ast_set_party_redirecting update;
372
373         if (!session->channel) {
374                 return;
375         }
376
377         ast_party_redirecting_init(&data);
378         memset(&update, 0, sizeof(update));
379
380         if (from_info) {
381                 set_redirecting_id((pjsip_name_addr*)from_info->uri,
382                         &data.from, &update.from);
383                 set_redirecting_reason(from_info, to_info, &data.reason);
384                 ast_set_party_id_all(&update.priv_to);
385         } else {
386                 copy_redirecting_id(&data.from, &session->id, &update.from);
387         }
388
389         if (to_info) {
390                 set_redirecting_id(to_info, &data.to, &update.to);
391         }
392
393         ast_set_party_id_all(&update.priv_orig);
394         ast_set_party_id_all(&update.priv_from);
395         ast_set_party_id_all(&update.priv_to);
396         ++data.count;
397
398         ast_channel_set_redirecting(session->channel, &data, &update);
399         /* Only queue an indication if it was due to a response */
400         if (session->inv_session->role == PJSIP_ROLE_UAC) {
401                 ast_channel_queue_redirecting_update(session->channel, &data, &update);
402         }
403         ast_party_redirecting_free(&data);
404 }
405
406 static int diversion_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
407 {
408         pjsip_fromto_hdr *hdr = get_diversion_header(rdata);
409
410         if (hdr) {
411                 set_redirecting(session, hdr, (pjsip_name_addr*)
412                                 PJSIP_MSG_TO_HDR(rdata->msg_info.msg)->uri);
413         } else {
414                 pjsip_fromto_hdr *history_info_to;
415                 pjsip_fromto_hdr *history_info_from;
416                 history_info_to  = get_history_info_header(rdata, 0);
417
418                 if (history_info_to) {
419                         /* If History-Info is present, then it will also include the original
420                            redirected-from in addition to the redirected-to */
421                         history_info_from = get_history_info_header(rdata, 1);
422                         set_redirecting(session, history_info_from, (pjsip_name_addr*)history_info_to->uri);
423                 }
424         }
425
426         return 0;
427 }
428
429 static void diversion_incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
430 {
431         static const pj_str_t contact_name = { "Contact", 7 };
432         static const pj_str_t contact_name_s = { "m", 1 };
433
434         pjsip_status_line status = rdata->msg_info.msg->line.status;
435         pjsip_fromto_hdr *div_hdr;
436         pjsip_fromto_hdr *history_info_to;
437         pjsip_fromto_hdr *history_info_from;
438         pjsip_contact_hdr *contact_hdr;
439
440         if ((status.code != 302) && (status.code != 181)) {
441                 return;
442         }
443
444         /* use the diversion header info if there is one. if not one then use the
445            the history-info, if that doesn't exist, use session caller id info. if
446            that doesn't exist use info from the To hdr*/
447         if (!(div_hdr = get_diversion_header(rdata))) {
448                 history_info_to  = get_history_info_header(rdata, 0);
449
450                 if (history_info_to) {
451                         /* If History-Info is present, then it will also include the original
452                            redirected-from in addition to the redirected-to */
453                         history_info_from = get_history_info_header(rdata, 1);
454                         set_redirecting(session, history_info_from, (pjsip_name_addr*)history_info_to->uri);
455                         return;
456                 }
457                 if (!div_hdr && !session->id.number.valid) {
458                         div_hdr = PJSIP_MSG_TO_HDR(rdata->msg_info.msg);
459                 }
460         }
461
462
463         if (status.code == 302) {
464                 /* With 302, Contact indicates the final destination and possibly Diversion indicates the hop before */
465                 contact_hdr = pjsip_msg_find_hdr_by_names(rdata->msg_info.msg, &contact_name, &contact_name_s, NULL);
466
467                 set_redirecting(session, div_hdr, contact_hdr ? (pjsip_name_addr*)contact_hdr->uri :
468                                 (pjsip_name_addr*)PJSIP_MSG_FROM_HDR(rdata->msg_info.msg)->uri);
469         } else {
470                 /* With 181, Diversion is non-standard, but if present indicates the new final destination, and To indicating the original */
471                 set_redirecting(session, PJSIP_MSG_TO_HDR(rdata->msg_info.msg),
472                                 div_hdr ? (pjsip_name_addr*)div_hdr->uri : NULL);
473         }
474 }
475
476 /*!
477  * \internal
478  * \brief Adds diversion header information to an outbound SIP message
479  *
480  * \param tdata The outbound message
481  * \param data The redirecting data used to fill parts of the diversion header
482  */
483 static void add_diversion_header(pjsip_tx_data *tdata, struct ast_party_redirecting *data)
484 {
485         static const pj_str_t reason_name = { "reason", 6 };
486
487         pjsip_fromto_hdr *hdr;
488         pjsip_name_addr *name_addr;
489         pjsip_sip_uri *uri;
490         pjsip_param *param;
491         pjsip_fromto_hdr *old_hdr;
492         const char *reason_str;
493         const char *quote_str;
494         char *reason_buf;
495         pjsip_uri *base;
496
497         struct ast_party_id *id = NULL;
498         if (tdata->msg->type == PJSIP_REQUEST_MSG) {
499                 id = &data->from;
500         } else {
501                 /* In responses indicate the new destination */
502                 id = &data->to;
503         }
504
505         base = PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
506
507         if (!id->number.valid || ast_strlen_zero(id->number.str)) {
508                 return;
509         }
510
511         hdr = pjsip_from_hdr_create(tdata->pool);
512         hdr->type = PJSIP_H_OTHER;
513         hdr->sname = hdr->name = diversion_name;
514
515         name_addr = pjsip_uri_clone(tdata->pool, base);
516         uri = pjsip_uri_get_uri(name_addr->uri);
517
518         pj_strdup2(tdata->pool, &name_addr->display, id->name.str);
519         pj_strdup2(tdata->pool, &uri->user, id->number.str);
520
521         param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
522         param->name = reason_name;
523
524         reason_str = reason_code_to_str(&data->reason);
525
526         /* Reason is either already quoted or it is a token to not need quotes added. */
527         quote_str = *reason_str == '\"' || sip_is_token(reason_str) ? "" : "\"";
528
529         reason_buf = pj_pool_alloc(tdata->pool, strlen(reason_str) + 3);
530         sprintf(reason_buf, "%s%s%s", quote_str, reason_str, quote_str);/* Safe */
531
532         param->value = pj_str(reason_buf);
533
534         pj_list_insert_before(&hdr->other_param, param);
535
536         hdr->uri = (pjsip_uri *) name_addr;
537         old_hdr = pjsip_msg_find_hdr_by_name(tdata->msg, &diversion_name, NULL);
538         if (old_hdr) {
539                 pj_list_erase(old_hdr);
540         }
541         pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
542 }
543
544 /*!
545  * \internal
546  * \brief Adds history-info header information to an outbound SIP message
547  *
548  * \param tdata The outbound message
549  * \param data The redirecting data used to fill parts of the history-info header
550  */
551 static void add_history_info_header(pjsip_tx_data *tdata, struct ast_party_redirecting *data)
552 {
553         static const pj_str_t index_name = { "index", 5 };
554         static const pj_str_t cause_name = { "cause", 5 };
555         static const pj_str_t first_index = { "1", 1 };
556         static const pj_str_t last_index = { "1.1", 3 };
557
558         pjsip_fromto_hdr *hdr;
559         pjsip_name_addr *name_addr;
560         pjsip_sip_uri *uri;
561         pjsip_param *param;
562         pjsip_fromto_hdr *old_hdr;
563         unsigned int cause;
564         char *cause_buf;
565
566         struct ast_party_id *to = &data->to;
567         struct ast_party_id *from = &data->from;
568
569         pjsip_uri *base = PJSIP_MSG_TO_HDR(tdata->msg)->uri;
570
571
572         hdr = pjsip_from_hdr_create(tdata->pool);
573         hdr->type = PJSIP_H_OTHER;
574         hdr->sname = hdr->name = history_info_name;
575
576         name_addr = pjsip_uri_clone(tdata->pool, base);
577         uri = pjsip_uri_get_uri(name_addr->uri);
578
579         /* if no redirecting information, then TO is the original destination */
580         if (from->number.valid && !ast_strlen_zero(from->number.str)) {
581                 pj_strdup2(tdata->pool, &name_addr->display, from->name.str);
582                 pj_strdup2(tdata->pool, &uri->user, from->number.str);
583         }
584
585         param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
586         param->name = index_name;
587         param->value = first_index;
588
589
590         pj_list_insert_before(&hdr->other_param, param);
591         hdr->uri = (pjsip_uri *) name_addr;
592
593         while ((old_hdr = pjsip_msg_find_hdr_by_name(tdata->msg, &history_info_name, NULL)) != NULL) {
594                 pj_list_erase(old_hdr);
595         }
596
597         pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
598
599         if (!to->number.valid || ast_strlen_zero(to->number.str)) {
600                 return;
601         }
602
603         hdr = pjsip_from_hdr_create(tdata->pool);
604         hdr->type = PJSIP_H_OTHER;
605         hdr->sname = hdr->name = history_info_name;
606
607         name_addr = pjsip_uri_clone(tdata->pool, base);
608         uri = pjsip_uri_get_uri(name_addr->uri);
609
610         pj_strdup2(tdata->pool, &name_addr->display, to->name.str);
611         pj_strdup2(tdata->pool, &uri->user, to->number.str);
612
613         param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
614         param->name = index_name;
615         param->value = last_index;
616         pj_list_insert_before(&hdr->other_param, param);
617
618         param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
619         param->name = cause_name;
620         cause = reason_code_to_cause(&data->reason);
621         cause_buf = pj_pool_alloc(tdata->pool, 4);
622         snprintf(cause_buf, 4, "%ud", cause);
623         param->value = pj_str(cause_buf);
624         pj_list_insert_before(&uri->other_param, param);
625         hdr->uri = (pjsip_uri *) name_addr;
626
627         pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
628 }
629
630 static void get_redirecting_add_diversion(struct ast_sip_session *session, pjsip_tx_data *tdata)
631 {
632         struct ast_party_redirecting *data;
633
634         add_supported(tdata);
635
636         if (session->channel && session->endpoint->id.send_diversion &&
637             (data = ast_channel_redirecting(session->channel))->count) {
638                 add_diversion_header(tdata, data);
639         }
640         if (session->channel && session->endpoint->id.send_history_info) {
641                 data = ast_channel_redirecting(session->channel);
642                 add_history_info_header(tdata, data);
643         }
644 }
645
646 /*!
647  * \internal
648  * \brief Adds a diversion header to an outgoing INVITE request if
649  *  redirecting information is available.
650  *
651  * \param session The session on which the INVITE request is to be sent
652  * \param tdata The outbound INVITE request
653  */
654 static void diversion_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
655 {
656         get_redirecting_add_diversion(session, tdata);
657 }
658
659 /*!
660  * \internal
661  * \brief Adds a diversion header to an outgoing 3XX response
662  *
663  * \param session The session on which the INVITE response is to be sent
664  * \param tdata The outbound INVITE response
665  */
666 static void diversion_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
667 {
668         struct pjsip_status_line status = tdata->msg->line.status;
669
670         /* add to 302 and 181 */
671         if (PJSIP_IS_STATUS_IN_CLASS(status.code, 300) || (status.code == 181)) {
672                 get_redirecting_add_diversion(session, tdata);
673         }
674 }
675
676 static struct ast_sip_session_supplement diversion_supplement = {
677         .method = "INVITE",
678         /* this supplement needs to be called after caller id
679            and after the channel has been created */
680         .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 100,
681         .incoming_request = diversion_incoming_request,
682         .incoming_response = diversion_incoming_response,
683         .outgoing_request = diversion_outgoing_request,
684         .outgoing_response = diversion_outgoing_response,
685         .response_priority = AST_SIP_SESSION_BEFORE_REDIRECTING|AST_SIP_SESSION_BEFORE_MEDIA,
686 };
687
688 static int load_module(void)
689 {
690         /* Because we are passing static memory to pjsip, we need to make sure it
691          * stays valid while we potentially have active sessions */
692         ast_module_shutdown_ref(ast_module_info->self);
693         ast_sip_session_register_supplement(&diversion_supplement);
694         return AST_MODULE_LOAD_SUCCESS;
695 }
696
697 static int unload_module(void)
698 {
699         ast_sip_session_unregister_supplement(&diversion_supplement);
700         return 0;
701 }
702
703 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Add Diversion Header Support",
704         .support_level = AST_MODULE_SUPPORT_CORE,
705         .load = load_module,
706         .unload = unload_module,
707         .load_pri = AST_MODPRI_APP_DEPEND,
708         .requires = "res_pjsip,res_pjsip_session",
709 );