Asterisk - The Open Source Telephony Project  18.5.0
res_pjsip_rfc3326.c
Go to the documentation of this file.
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"
33 #include "asterisk/module.h"
34 #include "asterisk/causes.h"
35 #include "asterisk/threadpool.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  char *text;
44  int code;
45 
46  header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_reason, NULL);
47  for (; header;
48  header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_reason, header->next)) {
49  ast_copy_pj_str(buf, &header->hvalue, sizeof(buf));
50  cause = ast_skip_blanks(buf);
51 
52  if (strncasecmp(cause, "Q.850", 5) || !(cause = strstr(cause, "cause="))) {
53  continue;
54  }
55 
56  /* If text is present get rid of it */
57  if ((text = strstr(cause, ";"))) {
58  *text = '\0';
59  }
60 
61  if (sscanf(cause, "cause=%30d", &code) != 1) {
62  continue;
63  }
64 
65  ast_channel_hangupcause_set(session->channel, code & 0x7f);
66  break;
67  }
68 }
69 
70 static int rfc3326_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
71 {
72  if ((pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method) &&
73  pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method)) ||
74  !session->channel) {
75  return 0;
76  }
77 
78  rfc3326_use_reason_header(session, rdata);
79 
80  return 0;
81 }
82 
83 static void rfc3326_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
84 {
85  struct pjsip_status_line status = rdata->msg_info.msg->line.status;
86 
87  if ((status.code < 300) || !session->channel) {
88  return;
89  }
90 
91  rfc3326_use_reason_header(session, rdata);
92 }
93 
94 static void rfc3326_add_reason_header(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
95 {
96  char buf[20];
97 
99  ast_sip_add_header(tdata, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
100  }
101 
102  if (session->endpoint && session->endpoint->suppress_q850_reason_headers) {
103  ast_debug(1, "A Q.850 '%s'(%i) Reason header was suppresed for endpoint '%s'\n",
104  ast_cause2str((ast_channel_hangupcause(session->channel) & 0x7f)),
105  (ast_channel_hangupcause(session->channel) & 0x7f),
107  } else {
108  snprintf(buf, sizeof(buf), "Q.850;cause=%i", ast_channel_hangupcause(session->channel) & 0x7f);
109  ast_sip_add_header(tdata, "Reason", buf);
110  }
111 }
112 
113 static void rfc3326_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
114 {
115  if ((pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_bye_method)
116  && pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method))
117  || !session->channel
118  /*
119  * The session->channel has been seen to go away on us between
120  * checks so we must also be running under the call's serializer
121  * thread.
122  */
124  return;
125  }
126 
127  rfc3326_add_reason_header(session, tdata);
128 }
129 
130 static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
131 {
132  struct pjsip_status_line status = tdata->msg->line.status;
133 
134  if (status.code < 300
135  || !session->channel
137  return;
138  }
139 
140  rfc3326_add_reason_header(session, tdata);
141 }
142 
145  .incoming_response = rfc3326_incoming_response,
146  .outgoing_request = rfc3326_outgoing_request,
147  .outgoing_response = rfc3326_outgoing_response,
148 };
149 
150 static int load_module(void)
151 {
152  ast_sip_session_register_supplement(&rfc3326_supplement);
154 }
155 
156 static int unload_module(void)
157 {
158  ast_sip_session_unregister_supplement(&rfc3326_supplement);
159  return 0;
160 }
161 
163  .support_level = AST_MODULE_SUPPORT_CORE,
164  .load = load_module,
165  .unload = unload_module,
166  .load_pri = AST_MODPRI_APP_DEPEND,
167  .requires = "res_pjsip,res_pjsip_session",
168 );
struct ast_sip_endpoint * endpoint
Asterisk main include file. File version handling, generic pbx functions.
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
int(* incoming_request)(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Called on incoming SIP request This method can indicate a failure in processing in its return...
static void rfc3326_add_reason_header(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
char * text
Definition: app_queue.c:1508
void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
Copy a pj_str_t into a standard character buffer.
Definition: res_pjsip.c:5240
void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement)
Unregister a an supplement to SIP session processing.
Definition: pjsip_session.c:63
#define NULL
Definition: resample.c:96
struct ast_taskprocessor * ast_threadpool_serializer_get_current(void)
Get the threadpool serializer currently associated with this thread.
Definition: threadpool.c:1397
A structure describing a SIP session.
int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
Add a header to an outbound SIP message.
Definition: res_pjsip.c:5063
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static int unload_module(void)
static struct ast_mansession session
#define AST_CAUSE_ANSWERED_ELSEWHERE
Definition: causes.h:113
static int load_module(void)
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
struct ast_channel * channel
static int rfc3326_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
struct ast_taskprocessor * serializer
static void rfc3326_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:157
static void rfc3326_use_reason_header(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
static void rfc3326_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
const char * ast_cause2str(int state) attribute_pure
Gives the string form of a given cause code.
Definition: channel.c:612
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
A supplement to SIP message processing.
static struct ast_sip_session_supplement rfc3326_supplement
int ast_channel_hangupcause(const struct ast_channel *chan)
Internal Asterisk hangup causes.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
unsigned int suppress_q850_reason_headers
Definition: res_pjsip.h:899
#define ast_sip_session_register_supplement(supplement)