Browse Source

modules_k/tmx: add $T_reply_reason PV

Especially for internally generated (faked) replies it was hard to
get the reason phrase. This PV provides just that.
Alex Hermann 14 năm trước cách đây
mục cha
commit
87bce89fe7

+ 24 - 23
modules_k/tmx/README

@@ -11,7 +11,7 @@ Daniel-Constantin Mierla
 
    <[email protected]>
 
-   Copyright © 2009 Daniel-Constantin Mierla
+   Copyright © 2009 Daniel-Constantin Mierla
      __________________________________________________________________
 
    Table of Contents
@@ -147,7 +147,7 @@ Chapter 1. Admin Guide
    3.7. t_suspend()
    3.8. t_continue(tindex, tlabel, rtname)
 
-3.1. t_cancel_branches(which)
+3.1.  t_cancel_branches(which)
 
    Cancel branches of an active SIP transaction. The function can be
    called for a SIP reply that will identify the current branch.
@@ -166,7 +166,7 @@ if (t_cancel_branches("all")) {
 }
 ...
 
-3.2. t_cancel_callid(callid, cseq, flag)
+3.2.  t_cancel_callid(callid, cseq, flag)
 
    Cancel first INVITE transaction identified by callid and cseq. It sets
    the flag if the value is greater than zero
@@ -185,7 +185,7 @@ if (t_cancel_callid("123qaz", "5", "22")) {
 }
 ...
 
-3.3. t_reply_callid(callid, cseq, code, reason)
+3.3.  t_reply_callid(callid, cseq, code, reason)
 
    Send reply to first INVITE transaction identified by callid and cseq.
 
@@ -204,7 +204,7 @@ if (t_reply_callid("123qaz", "5", "458", "Replied remotely")) {
 }
 ...
 
-3.4. t_flush_flags()
+3.4.  t_flush_flags()
 
    Flush the flags from current SIP message into the already created
    transaction. It make sense only in routing block if the transaction was
@@ -217,7 +217,7 @@ if (t_reply_callid("123qaz", "5", "458", "Replied remotely")) {
 t_flush_flags();
 ...
 
-3.5. t_is_failure_route()
+3.5.  t_is_failure_route()
 
    Returns true if the top-executed route block is failure_route.
 
@@ -234,7 +234,7 @@ route[abc] {
 }
 ...
 
-3.6. t_is_branch_route()
+3.6.  t_is_branch_route()
 
    Returns true if the top-executed route block is branch_route.
 
@@ -251,7 +251,7 @@ route[abc] {
 }
 ...
 
-3.7. t_suspend()
+3.7.  t_suspend()
 
    Suspend the execution of SIP request in a transaction. If transaction
    was not created yet, it is created by this function. Returns true in
@@ -269,7 +269,7 @@ if(t_suspend())
 }
 ...
 
-3.8. t_continue(tindex, tlabel, rtname)
+3.8.  t_continue(tindex, tlabel, rtname)
 
    Continue the execution of the transaction identified by tindex and
    tlabel with the actions defined in route[rtname].
@@ -293,6 +293,7 @@ tcontinue('123', '456', 'MYROUTE');
 
      * $T_branch_idx
      * $T_reply_code
+     * $T_reply_reason
      * $T_req(pv)
      * $T_rpl(pv)
 
@@ -307,22 +308,22 @@ tcontinue('123', '456', 'MYROUTE');
    5.4. t_reply
    5.5. t_reply_callid
 
-5.1. t_uac_dlg
+5.1.  t_uac_dlg
 
    Generates and sends a local SIP request.
 
    Parameters:
      * method - request method
      * RURI - request SIP URI
-     * NEXT HOP - next hop SIP URI (OBP); use "." if no value.
-     * socket - local socket to be used for sending the request; use "."
+     * NEXT HOP - next hop SIP URI (OBP); use “.� if no value.
+     * socket - local socket to be used for sending the request; use “.�
        if no value.
      * headers - set of additional headers to be added to the request; at
-       least "From" and "To" headers must be specify)
+       least “From� and “To� headers must be specify)
      * body - (optional, may not be present) request body (if present,
-       requires the "Content-Type" and "Content-length" headers)
+       requires the “Content-Type� and “Content-length� headers)
 
-5.2. t_uac_cancel
+5.2.  t_uac_cancel
 
    Generates and sends a CANCEL for an existing local SIP request.
 
@@ -330,14 +331,14 @@ tcontinue('123', '456', 'MYROUTE');
      * callid - callid of the INVITE request to be cancelled.
      * cseq - cseq of the INVITE request to be cancelled.
 
-5.3. t_hash
+5.3.  t_hash
 
    Gets information about the load of TM internal hash table.
 
    Parameters:
      * none
 
-5.4. t_reply
+5.4.  t_reply
 
    Generates and sends a reply for an existing inbound SIP transaction.
 
@@ -347,11 +348,11 @@ tcontinue('123', '456', 'MYROUTE');
      * trans_id - transaction identifier (has the hash_entry:label format)
      * to_tag - To tag to be added to TO header
      * new_headers - extra headers to be appended to the reply; use a dot
-       (".") char only if there are no headers;
+       (“.�) char only if there are no headers;
      * body - (optional, may not be present) reply body (if present,
-       requires the "Content-Type" and "Content-length" headers)
+       requires the “Content-Type� and “Content-length� headers)
 
-5.5. t_reply_callid
+5.5.  t_reply_callid
 
    Generates and sends a reply for an existing inbound SIP transaction.
 
@@ -362,9 +363,9 @@ tcontinue('123', '456', 'MYROUTE');
      * cseq - SIP CSeq header
      * to_tag - To tag to be added to TO header
      * new_headers - extra headers to be appended to the reply; use a dot
-       (".") char only if there are no headers;
+       (“.�) char only if there are no headers;
      * body - (optional, may not be present) reply body (if present,
-       requires the "Content-Type" and "Content-length" headers)
+       requires the “Content-Type� and “Content-length� headers)
 
 6. Statistics
 
@@ -381,7 +382,7 @@ tcontinue('123', '456', 'MYROUTE');
    6.11. inuse_transactions
 
    Exported statistics are listed in the next sections. All statistics
-   except "inuse_transactions" can be reset.
+   except “inuse_transactions� can be reset.
 
 6.1. received_replies
 

+ 3 - 0
modules_k/tmx/doc/tmx_admin.xml

@@ -318,6 +318,9 @@ tcontinue('123', '456', 'MYROUTE');
 			<listitem><para>
 				<emphasis>$T_reply_code</emphasis>
 			</para></listitem>
+			<listitem><para>
+				<emphasis>$T_reply_reason</emphasis>
+			</para></listitem>
 			<listitem><para>
 				<emphasis>$T_req(pv)</emphasis>
 			</para></listitem>

+ 56 - 0
modules_k/tmx/t_var.c

@@ -39,6 +39,8 @@ static struct _pv_tmx_data _pv_treq;
 static struct _pv_tmx_data _pv_trpl;
 static struct _pv_tmx_data _pv_tinv;
 
+static str _empty_str = {"", 0};
+
 void pv_tmx_data_init(void)
 {
 	memset(&_pv_treq, 0, sizeof(struct _pv_tmx_data));
@@ -471,6 +473,60 @@ int pv_get_tm_reply_code(struct sip_msg *msg, pv_param_t *param,
 	return 0;
 }
 
+int pv_get_tm_reply_reason(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res)
+{
+	struct cell *t;
+	struct sip_msg *reply;
+	int branch;
+
+	if(msg==NULL || res==NULL)
+		return -1;
+
+	/* first get the transaction */
+	if (_tmx_tmb.t_check( msg , 0 )==-1) return -1;
+	if ( (t=_tmx_tmb.t_gett())==0) {
+		/* no T */
+		res->rs = _empty_str;
+	} else {
+		switch (get_route_type()) {
+			case CORE_ONREPLY_ROUTE:
+				/*  t_check() above has the side effect of setting T and
+				    REFerencing T => we must unref and unset it for the 
+				    main/core onreply_route. */
+				_tmx_tmb.t_unref(msg);
+				/* no break */
+			case TM_ONREPLY_ROUTE:
+				/* use the reason of the current reply */
+				res->rs.s = msg->first_line.u.reply.reason.s;
+				res->rs.len = msg->first_line.u.reply.reason.len;
+				break;
+			case FAILURE_ROUTE:
+				/* use the reason of the winning reply */
+				if ( (branch=_tmx_tmb.t_get_picked_branch())<0 ) {
+					LM_CRIT("no picked branch (%d) for a final response"
+							" in MODE_ONFAILURE\n", branch);
+					return -1;
+				}
+				reply = t->uac[branch].reply;
+				if (reply == FAKED_REPLY) {
+					res->rs.s = error_text(t->uac[branch].last_received);
+					res->rs.len = strlen(res->rs.s);
+				} else {
+					res->rs.s = reply->first_line.u.reply.reason.s;
+					res->rs.len = reply->first_line.u.reply.reason.len;
+				}
+				break;
+			default:
+				LM_ERR("unsupported route_type %d\n", get_route_type());
+				return -1;
+		}
+	}
+	LM_DBG("reply reason is [%.*s]\n", res->rs.len, res->rs.s);
+	res->flags = PV_VAL_STR;
+	return 0;
+}
+
 int pv_parse_t_name(pv_spec_p sp, str *in)
 {
 	if(sp==NULL || in==NULL || in->len<=0)

+ 2 - 0
modules_k/tmx/t_var.h

@@ -40,6 +40,8 @@ int pv_get_tm_branch_idx(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res);
 int pv_get_tm_reply_code(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res);
+int pv_get_tm_reply_reason(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res);
 
 int pv_get_t(struct sip_msg *msg,  pv_param_t *param,
 		pv_value_t *res);

+ 3 - 0
modules_k/tmx/tmx_mod.c

@@ -123,6 +123,9 @@ static pv_export_t mod_pvs[] = {
 	{ {"T_reply_code", sizeof("T_reply_code")-1}, PVT_OTHER,
 		pv_get_tm_reply_code, 0,
 		 0, 0, 0, 0 },
+	{ {"T_reply_reason", sizeof("T_reply_reason")-1}, PVT_OTHER,
+		pv_get_tm_reply_reason, 0,
+		 0, 0, 0, 0 },
 	{ {"T_inv", sizeof("T_inv")-1}, PVT_OTHER, pv_get_t_var_inv, 0,
 		pv_parse_t_var_name, 0, 0, 0 },
 	{ {"T_req", sizeof("T_req")-1}, PVT_OTHER, pv_get_t_var_req, 0,