Debian Codegraph @

res/res_pjsip_rfc3326.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
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/taskpool.h"
36
37 static void rfc3326_use_reason_header(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
38 {
39 static const pj_str_t str_reason = { "Reason", 6 };
40 pjsip_generic_string_hdr *header;
41 char buf[20];
42 char *cause;
43 int code_q850 = 0, code_sip = 0;
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 int cause_q850, cause_sip;
49 ast_copy_pj_str(buf, &header->hvalue, sizeof(buf));
50 cause = ast_skip_blanks(buf);
51
52 cause_q850 = !strncasecmp(cause, "Q.850", 5);
53 cause_sip = !strncasecmp(cause, "SIP", 3);
54 if ((cause_q850 || cause_sip) && (cause = strstr(cause, "cause="))) {
55 int *code = cause_q850 ? &code_q850 : &code_sip;
56 if (sscanf(cause, "cause=%30d", code) != 1) {
57 *code = 0;
58 }
59 }
60 }
61
62 if (code_q850) {
63 ast_channel_hangupcause_set(session->channel, code_q850 & 0x7f);
64 } else if (code_sip) {
65 ast_channel_hangupcause_set(session->channel, ast_sip_hangup_sip2cause(code_sip));
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 if (session->endpoint && session->endpoint->suppress_q850_reason_headers) {
102 ast_debug(1, "A Q.850 '%s'(%i) Reason header was suppressed for endpoint '%s'\n",
103 ast_cause2str((ast_channel_hangupcause(session->channel) & 0x7f)),
104 (ast_channel_hangupcause(session->channel) & 0x7f),
105 ast_sorcery_object_get_id(session->endpoint));
106 } else {
107 snprintf(buf, sizeof(buf), "Q.850;cause=%i", ast_channel_hangupcause(session->channel) & 0x7f);
108 ast_sip_add_header(tdata, "Reason", buf);
109 }
110 }
111
112 static void rfc3326_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
113 {
114 if ((pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_bye_method)
115 && pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method))
116 || !session->channel
117 /*
118 * The session->channel has been seen to go away on us between
119 * checks so we must also be running under the call's serializer
120 * thread.
121 */
122 || session->serializer != ast_taskpool_serializer_get_current()) {
123 return;
124 }
125
126 rfc3326_add_reason_header(session, tdata);
127 }
128
129 static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
130 {
131 struct pjsip_status_line status = tdata->msg->line.status;
132
133 if (status.code < 300
134 || !session->channel
135 || session->serializer != ast_taskpool_serializer_get_current()) {
136 return;
137 }
138
139 rfc3326_add_reason_header(session, tdata);
140 }
141
142 static struct ast_sip_session_supplement rfc3326_supplement = {
143 .incoming_request = rfc3326_incoming_request,
144 .incoming_response = rfc3326_incoming_response,
145 .outgoing_request = rfc3326_outgoing_request,
146 .outgoing_response = rfc3326_outgoing_response,
147 };
148
149 static int load_module(void)
150 {
151 ast_sip_session_register_supplement(&rfc3326_supplement);
152 return AST_MODULE_LOAD_SUCCESS;
153 }
154
155 static int unload_module(void)
156 {
157 ast_sip_session_unregister_supplement(&rfc3326_supplement);
158 return 0;
159 }
160
161 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP RFC3326 Support",
162 .support_level = AST_MODULE_SUPPORT_CORE,
163 .load = load_module,
164 .unload = unload_module,
165 .load_pri = AST_MODPRI_APP_DEPEND,
166 .requires = "res_pjsip,res_pjsip_session",
167 );