PJSIP: Enforce module load dependencies
[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
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         snprintf(buf, sizeof(buf), "Q.850;cause=%i", ast_channel_hangupcause(session->channel) & 0x7f);
95         ast_sip_add_header(tdata, "Reason", buf);
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
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                 return;
108         }
109
110         rfc3326_add_reason_header(session, tdata);
111 }
112
113 static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
114 {
115         struct pjsip_status_line status = tdata->msg->line.status;
116
117         if ((status.code < 300) || !session->channel) {
118                 return;
119         }
120
121         rfc3326_add_reason_header(session, tdata);
122 }
123
124 static struct ast_sip_session_supplement rfc3326_supplement = {
125         .incoming_request = rfc3326_incoming_request,
126         .incoming_response = rfc3326_incoming_response,
127         .outgoing_request = rfc3326_outgoing_request,
128         .outgoing_response = rfc3326_outgoing_response,
129 };
130
131 static int load_module(void)
132 {
133         CHECK_PJSIP_SESSION_MODULE_LOADED();
134
135         ast_sip_session_register_supplement(&rfc3326_supplement);
136         return AST_MODULE_LOAD_SUCCESS;
137 }
138
139 static int unload_module(void)
140 {
141         ast_sip_session_unregister_supplement(&rfc3326_supplement);
142         return 0;
143 }
144
145 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP RFC3326 Support",
146                 .support_level = AST_MODULE_SUPPORT_CORE,
147                 .load = load_module,
148                 .unload = unload_module,
149                 .load_pri = AST_MODPRI_APP_DEPEND,
150                );