[Asterisk-code-review] chan sip: Support parsing of Q.850 reason header in SIP BYE ... (asterisk[master])

Christof Lauber asteriskteam at digium.com
Fri Nov 13 08:15:08 CST 2015


Christof Lauber has uploaded a new change for review.

  https://gerrit.asterisk.org/1620

Change subject: chan_sip: Support parsing of Q.850 reason header in SIP BYE and CANCEL requests.
......................................................................

chan_sip: Support parsing of Q.850 reason header in SIP BYE and CANCEL requests.

Current support for reason header did work only in SIP responses.
According to RFC3336 the reason header might appear in any SIP request.
But it seems to make most sence in BYE and CANCEL so parasing is done
there too (if use_q850_reason=yes).

Change-Id: Ifeed95a9c7c7fa86a67d0c512f3e75f28f86e66c
---
M channels/chan_sip.c
1 file changed, 43 insertions(+), 19 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/20/1620/1

diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 158dc7b..52d26f5 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -1399,6 +1399,7 @@
 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req);
 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq);
 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req);
+static int parse_reason_header(struct sip_pvt *pvt, struct sip_request *req);
 static int set_address_from_contact(struct sip_pvt *pvt);
 static void check_via(struct sip_pvt *p, const struct sip_request *req);
 static int get_rpid(struct sip_pvt *p, struct sip_request *oreq);
@@ -16111,6 +16112,40 @@
 	return TRUE;
 }
 
+/*! \brief Parses SIP reason header according to RFC3326 and sets channel's hangupcause if configured so
+ *  and header present
+ *
+ * \note This is used in BYE and CANCEL request and SIP response, but according to RFC3326 it could
+ *       appear in any request, but makes not a lot of sense in others than BYE or CANCEL.
+ *       Currently only implemented for Q.850 status codes.
+ * \retval TRUE success
+ * \retval FALSE on failure or if not configured
+ */
+static int parse_reason_header(struct sip_pvt *pvt, struct sip_request *req)
+{
+	struct ast_channel *owner;
+	owner = pvt->owner;
+	int ret=FALSE;
+	if (owner) {
+		const char *rp = NULL, *rh = NULL;
+		ast_channel_hangupcause_set(owner, 0);
+		if (ast_test_flag(&pvt->flags[1], SIP_PAGE2_Q850_REASON) && (rh = sip_get_header(req, "Reason"))) {
+			rh = ast_skip_blanks(rh);
+			if (!strncasecmp(rh, "Q.850", 5)) {
+				int cause = ast_channel_hangupcause(owner);
+				rp = strstr(rh, "cause=");
+				if (rp && sscanf(rp + 6, "%3d", &cause) == 1) {
+					ret=TRUE;
+					ast_channel_hangupcause_set(owner, cause & 0x7f);
+					if (req->debug)
+						ast_verbose("Using Reason header for cause code: %d\n", ast_channel_hangupcause(owner));
+				}
+			}
+		}
+	}
+	return ret;
+}
+
 /*! \brief parse uri in a way that allows semicolon stripping if legacy mode is enabled
  *
  * \note This calls parse_uri which has the unexpected property that passing more
@@ -24100,23 +24135,7 @@
 
 	owner = p->owner;
 	if (owner) {
-		const char *rp = NULL, *rh = NULL;
-
-		ast_channel_hangupcause_set(owner, 0);
-		if (ast_test_flag(&p->flags[1], SIP_PAGE2_Q850_REASON) && (rh = sip_get_header(req, "Reason"))) {
-			rh = ast_skip_blanks(rh);
-			if (!strncasecmp(rh, "Q.850", 5)) {
-				int cause = ast_channel_hangupcause(owner);
-				rp = strstr(rh, "cause=");
-				if (rp && sscanf(rp + 6, "%30d", &cause) == 1) {
-					ast_channel_hangupcause_set(owner, cause & 0x7f);
-					if (req->debug)
-						ast_verbose("Using Reason header for cause code: %d\n", ast_channel_hangupcause(owner));
-				}
-			}
-		}
-
-		if (!ast_channel_hangupcause(owner))
+		if (!parse_reason_header(p, req))
 			ast_channel_hangupcause_set(owner, hangup_sip2cause(resp));
 	}
 
@@ -26409,6 +26428,8 @@
 		return 0;
 	}
 
+	parse_reason_header(p, req);
+	
 	/* At this point, we could have cancelled the invite at the same time
 	   as the other side sends a CANCEL. Our final reply with error code
 	   might not have been received by the other side before the CANCEL
@@ -26425,7 +26446,8 @@
 
 	stop_media_flows(p); /* Immediately stop RTP, VRTP and UDPTL as applicable */
 	if (p->owner) {
-		sip_queue_hangup_cause(p, 0);
+		int cause = ast_channel_hangupcause(p->owner);
+		sip_queue_hangup_cause(p, cause);
 	} else {
 		sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
 	}
@@ -26604,6 +26626,7 @@
 		stop_session_timer(p); /* Stop Session-Timer */
 	}
 
+	parse_reason_header(p, req);
 	if (!ast_strlen_zero(sip_get_header(req, "Also"))) {
 		ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method.  Ask vendor to support REFER instead\n",
 			ast_sockaddr_stringify(&p->recv));
@@ -26644,7 +26667,8 @@
 				ast_queue_hangup_with_cause(p->owner, AST_CAUSE_PROTOCOL_ERROR);
 		}
 	} else if (p->owner) {
-		sip_queue_hangup_cause(p, 0);
+		int cause = ast_channel_hangupcause(p->owner);
+		sip_queue_hangup_cause(p, cause);
 		sip_scheddestroy_final(p, DEFAULT_TRANS_TIMEOUT);
 		ast_debug(3, "Received bye, issuing owner hangup\n");
 	} else {

-- 
To view, visit https://gerrit.asterisk.org/1620
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifeed95a9c7c7fa86a67d0c512f3e75f28f86e66c
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Christof Lauber <christof.lauber at annax.ch>



More information about the asterisk-code-review mailing list