Merge "res_pjsip: New endpoint option "refer_blind_progress""
[asterisk/asterisk.git] / res / res_pjsip_rfc3326.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@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/module.h"
34 #include "asterisk/causes.h"
35 #include "asterisk/threadpool.h"
36
37 static void rfc3326_use_reason_header(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
38 {
39         const pj_str_t str_reason = { "Reason", 6 };
40         pjsip_generic_string_hdr *header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_reason, NULL);
41         char buf[20], *cause, *text;
42         int code;
43
44         if (!header) {
45                 return;
46         }
47
48         ast_copy_pj_str(buf, &header->hvalue, sizeof(buf));
49         cause = ast_skip_blanks(buf);
50
51         if (strncasecmp(cause, "Q.850", 5) || !(cause = strstr(cause, "cause="))) {
52                 return;
53         }
54
55         /* If text is present get rid of it */
56         if ((text = strstr(cause, ";"))) {
57                 *text = '\0';
58         }
59
60         if (sscanf(cause, "cause=%30d", &code) != 1) {
61                 return;
62         }
63
64         ast_channel_hangupcause_set(session->channel, code & 0x7f);
65 }
66
67 static int rfc3326_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
68 {
69         if ((pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method) &&
70              pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method)) ||
71             !session->channel) {
72                 return 0;
73         }
74
75         rfc3326_use_reason_header(session, rdata);
76
77         return 0;
78 }
79
80 static void rfc3326_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
81 {
82         struct pjsip_status_line status = rdata->msg_info.msg->line.status;
83
84         if ((status.code < 300) || !session->channel) {
85                 return;
86         }
87
88         rfc3326_use_reason_header(session, rdata);
89 }
90
91 static void rfc3326_add_reason_header(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
92 {
93         char buf[20];
94
95         snprintf(buf, sizeof(buf), "Q.850;cause=%i", ast_channel_hangupcause(session->channel) & 0x7f);
96         ast_sip_add_header(tdata, "Reason", buf);
97
98         if (ast_channel_hangupcause(session->channel) == AST_CAUSE_ANSWERED_ELSEWHERE) {
99                 ast_sip_add_header(tdata, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
100         }
101 }
102
103 static void rfc3326_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
104 {
105         if ((pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_bye_method)
106                         && pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method))
107                 || !session->channel
108                 /*
109                  * The session->channel has been seen to go away on us between
110                  * checks so we must also be running under the call's serializer
111                  * thread.
112                  */
113                 || session->serializer != ast_threadpool_serializer_get_current()) {
114                 return;
115         }
116
117         rfc3326_add_reason_header(session, tdata);
118 }
119
120 static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
121 {
122         struct pjsip_status_line status = tdata->msg->line.status;
123
124         if (status.code < 300
125                 || !session->channel
126                 || session->serializer != ast_threadpool_serializer_get_current()) {
127                 return;
128         }
129
130         rfc3326_add_reason_header(session, tdata);
131 }
132
133 static struct ast_sip_session_supplement rfc3326_supplement = {
134         .incoming_request = rfc3326_incoming_request,
135         .incoming_response = rfc3326_incoming_response,
136         .outgoing_request = rfc3326_outgoing_request,
137         .outgoing_response = rfc3326_outgoing_response,
138 };
139
140 static int load_module(void)
141 {
142         CHECK_PJSIP_SESSION_MODULE_LOADED();
143
144         ast_sip_session_register_supplement(&rfc3326_supplement);
145         return AST_MODULE_LOAD_SUCCESS;
146 }
147
148 static int unload_module(void)
149 {
150         ast_sip_session_unregister_supplement(&rfc3326_supplement);
151         return 0;
152 }
153
154 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP RFC3326 Support",
155         .support_level = AST_MODULE_SUPPORT_CORE,
156         .load = load_module,
157         .unload = unload_module,
158         .load_pri = AST_MODPRI_APP_DEPEND,
159 );