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