Merge the pimp_my_sip branch into trunk.
[asterisk/asterisk.git] / res / res_sip_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         <support_level>core</support_level>
22  ***/
23
24 #include "asterisk.h"
25
26 #include <pjsip.h>
27 #include <pjsip_ua.h>
28
29 #include "asterisk/res_sip.h"
30 #include "asterisk/res_sip_session.h"
31 #include "asterisk/module.h"
32 #include "asterisk/causes.h"
33
34 static void rfc3326_use_reason_header(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
35 {
36         const pj_str_t str_reason = { "Reason", 6 };
37         pjsip_generic_string_hdr *header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_reason, NULL);
38         char buf[20], *cause, *text;
39         int code;
40
41         if (!header) {
42                 return;
43         }
44
45         ast_copy_pj_str(buf, &header->hvalue, sizeof(buf));
46         cause = ast_skip_blanks(buf);
47
48         if (strncasecmp(cause, "Q.850", 5) || !(cause = strstr(cause, "cause="))) {
49                 return;
50         }
51
52         /* If text is present get rid of it */
53         if ((text = strstr(cause, ";"))) {
54                 *text = '\0';
55         }
56
57         if (sscanf(cause, "cause=%30d", &code) != 1) {
58                 return;
59         }
60
61         ast_channel_hangupcause_set(session->channel, code & 0x7f);
62 }
63
64 static int rfc3326_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
65 {
66         if ((pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method) &&
67              pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method)) ||
68             !session->channel) {
69                 return 0;
70         }
71
72         rfc3326_use_reason_header(session, rdata);
73
74         return 0;
75 }
76
77 static void rfc3326_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
78 {
79         struct pjsip_status_line status = rdata->msg_info.msg->line.status;
80
81         if ((status.code < 300) || !session->channel) {
82                 return;
83         }
84
85         rfc3326_use_reason_header(session, rdata);
86 }
87
88 static void rfc3326_add_reason_header(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
89 {
90         char buf[20];
91
92         snprintf(buf, sizeof(buf), "Q.850;cause=%i", ast_channel_hangupcause(session->channel) & 0x7f);
93         ast_sip_add_header(tdata, "Reason", buf);
94
95         if (ast_channel_hangupcause(session->channel) == AST_CAUSE_ANSWERED_ELSEWHERE) {
96                 ast_sip_add_header(tdata, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
97         }
98 }
99
100 static void rfc3326_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
101 {
102         if ((pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_bye_method) &&
103              pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method)) ||
104             !session->channel) {
105                 return;
106         }
107
108         rfc3326_add_reason_header(session, tdata);
109 }
110
111 static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
112 {
113         struct pjsip_status_line status = tdata->msg->line.status;
114
115         if ((status.code < 300) || !session->channel) {
116                 return;
117         }
118
119         rfc3326_add_reason_header(session, tdata);
120 }
121
122 static struct ast_sip_session_supplement rfc3326_supplement = {
123         .incoming_request = rfc3326_incoming_request,
124         .incoming_response = rfc3326_incoming_response,
125         .outgoing_request = rfc3326_outgoing_request,
126         .outgoing_response = rfc3326_outgoing_response,
127 };
128
129 static int load_module(void)
130 {
131         ast_sip_session_register_supplement(&rfc3326_supplement);
132         return AST_MODULE_LOAD_SUCCESS;
133 }
134
135 static int unload_module(void)
136 {
137         ast_sip_session_unregister_supplement(&rfc3326_supplement);
138         return 0;
139 }
140
141 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP RFC3326 Support",
142                 .load = load_module,
143                 .unload = unload_module,
144                 .load_pri = AST_MODPRI_APP_DEPEND,
145                );