76b0d08b0ef32d4de10dfe8b51e675c76558f9f7
[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         <support_level>core</support_level>
23  ***/
24
25 #include "asterisk.h"
26
27 #include <pjsip.h>
28 #include <pjsip_ua.h>
29
30 #include "asterisk/res_pjsip.h"
31 #include "asterisk/res_pjsip_session.h"
32 #include "asterisk/module.h"
33 #include "asterisk/causes.h"
34 #include "asterisk/threadpool.h"
35
36 static void rfc3326_use_reason_header(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
37 {
38         static const pj_str_t str_reason = { "Reason", 6 };
39         pjsip_generic_string_hdr *header;
40         char buf[20];
41         char *cause;
42         char *text;
43         int code;
44
45         header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_reason, NULL);
46         for (; header;
47                 header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_reason, header->next)) {
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                         continue;
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                         continue;
62                 }
63
64                 ast_channel_hangupcause_set(session->channel, code & 0x7f);
65                 break;
66         }
67 }
68
69 static int rfc3326_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
70 {
71         if ((pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method) &&
72              pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method)) ||
73             !session->channel) {
74                 return 0;
75         }
76
77         rfc3326_use_reason_header(session, rdata);
78
79         return 0;
80 }
81
82 static void rfc3326_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
83 {
84         struct pjsip_status_line status = rdata->msg_info.msg->line.status;
85
86         if ((status.code < 300) || !session->channel) {
87                 return;
88         }
89
90         rfc3326_use_reason_header(session, rdata);
91 }
92
93 static void rfc3326_add_reason_header(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
94 {
95         char buf[20];
96
97         if (ast_channel_hangupcause(session->channel) == AST_CAUSE_ANSWERED_ELSEWHERE) {
98                 ast_sip_add_header(tdata, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
99         }
100
101         snprintf(buf, sizeof(buf), "Q.850;cause=%i", ast_channel_hangupcause(session->channel) & 0x7f);
102         ast_sip_add_header(tdata, "Reason", buf);
103 }
104
105 static void rfc3326_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
106 {
107         if ((pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_bye_method)
108                         && pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method))
109                 || !session->channel
110                 /*
111                  * The session->channel has been seen to go away on us between
112                  * checks so we must also be running under the call's serializer
113                  * thread.
114                  */
115                 || session->serializer != ast_threadpool_serializer_get_current()) {
116                 return;
117         }
118
119         rfc3326_add_reason_header(session, tdata);
120 }
121
122 static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
123 {
124         struct pjsip_status_line status = tdata->msg->line.status;
125
126         if (status.code < 300
127                 || !session->channel
128                 || session->serializer != ast_threadpool_serializer_get_current()) {
129                 return;
130         }
131
132         rfc3326_add_reason_header(session, tdata);
133 }
134
135 static struct ast_sip_session_supplement rfc3326_supplement = {
136         .incoming_request = rfc3326_incoming_request,
137         .incoming_response = rfc3326_incoming_response,
138         .outgoing_request = rfc3326_outgoing_request,
139         .outgoing_response = rfc3326_outgoing_response,
140 };
141
142 static int load_module(void)
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         .requires = "res_pjsip,res_pjsip_session",
160 );