浏览代码

pv: function to manage ruri and branches using $sbranch(key)

- sbranch_set_ruri() -- use the $sbranch(key) attributes to update first
  branch fields (corresponding to r-uri)
- sbranch_append() -- use the $sbranch(key) attributes to append a
  branch (alternative to append_branch() which is not affecting r-uri
  branch)
- sbranch_reset() -- reset the attributes of $sbranch(key)
Daniel-Constantin Mierla 10 年之前
父节点
当前提交
bc96c20de0
共有 5 个文件被更改,包括 348 次插入6 次删除
  1. 69 4
      modules/pv/README
  2. 79 0
      modules/pv/doc/pv_admin.xml
  3. 40 0
      modules/pv/pv.c
  4. 156 2
      modules/pv/pv_branch.c
  5. 4 0
      modules/pv/pv_branch.h

+ 69 - 4
modules/pv/README

@@ -42,6 +42,9 @@ Daniel-Constantin Mierla
               4.4. typeof(pvar, vtype)
               4.5. not_empty(pvar)
               4.6. xavp_params_explode(sparams, xname)
+              4.7. sbranch_set_ruri()
+              4.8. sbranch_append()
+              4.9. sbranch_reset()
 
         5. MI Commands
 
@@ -64,8 +67,11 @@ Daniel-Constantin Mierla
    1.7. typeof() usage
    1.8. not_empty() usage
    1.9. xavp_params_explode usage
-   1.10. shv_set usage
-   1.11. shv_get usage
+   1.10. sbranch_set_ruri() usage
+   1.11. sbranch_append() usage
+   1.12. sbranch_append() usage
+   1.13. shv_set usage
+   1.14. shv_get usage
 
 Chapter 1. Admin Guide
 
@@ -91,6 +97,9 @@ Chapter 1. Admin Guide
         4.4. typeof(pvar, vtype)
         4.5. not_empty(pvar)
         4.6. xavp_params_explode(sparams, xname)
+        4.7. sbranch_set_ruri()
+        4.8. sbranch_append()
+        4.9. sbranch_reset()
 
    5. MI Commands
 
@@ -191,6 +200,9 @@ modparam("pv","avp_aliases","email=s:email_addr;tmp=i:100")
    4.4. typeof(pvar, vtype)
    4.5. not_empty(pvar)
    4.6. xavp_params_explode(sparams, xname)
+   4.7. sbranch_set_ruri()
+   4.8. sbranch_append()
+   4.9. sbranch_reset()
 
 4.1. pv_isset(pvar)
 
@@ -287,6 +299,59 @@ xavp_params_explode("a=b;c=d;e=d", "x");
 #    $xavp(x=>e) = "f";
 ...
 
+4.7. sbranch_set_ruri()
+
+   Use the attributes from static branch ($sbranch(key) variable) to set
+   request URI and the other fields of the branch associated with request
+   URI (destination URI, path, ...).
+
+   Content of the static branch is not reset after this function is
+   executed. It has to be done explicitely with sbranch_reset().
+
+   Function can be used from REQUEST_ROUTE, BRANCH_ROUTE or FAILURE_ROUTE.
+
+   Example 1.10. sbranch_set_ruri() usage
+...
+sbranch_reset();
+$sbranch(uri) = "sip:127.0.0.1:5080";
+$sbranch(dst_uri) =  "sip:127.0.0.1:5090";
+$sbranch(path) =  "sip:127.0.0.1:5090, sip:127.0.0.1:5094";
+$sbranch(send_socket) =  "udp:127.0.0.1:5060";
+sbranch_set_ruri();
+...
+
+4.8. sbranch_append()
+
+   Use the attributes from static branch ($sbranch(key) variable) to
+   append a new branch to destination set. It is an alternative to
+   append_branch() that allows setting each attribute specific to the
+   branch.
+
+   Content of the static branch is not reset after this function is
+   executed. It has to be done explicitely with sbranch_reset().
+
+   Function can be used from REQUEST_ROUTE, BRANCH_ROUTE or FAILURE_ROUTE.
+
+   Example 1.11. sbranch_append() usage
+...
+sbranch_reset();
+$sbranch(uri) = "sip:127.0.0.1:5080";
+$sbranch(dst_uri) =  "sip:127.0.0.1:5090";
+$sbranch(send_socket) =  "udp:127.0.0.1:5060";
+sbranch_append();
+...
+
+4.9. sbranch_reset()
+
+   Reset the content of static branch ($sbranch(key) variable.
+
+   Function can be used from REQUEST_ROUTE, BRANCH_ROUTE or FAILURE_ROUTE.
+
+   Example 1.12. sbranch_append() usage
+...
+sbranch_reset();
+...
+
 5. MI Commands
 
    5.1. shv_set
@@ -310,7 +375,7 @@ xavp_params_explode("a=b;c=d;e=d", "x");
                 _value_
                 _empty_line_
 
-   Example 1.10. shv_set usage
+   Example 1.13. shv_set usage
 ...
 $ kamctl fifo shv_set debug int 0
 ...
@@ -328,7 +393,7 @@ $ kamctl fifo shv_set debug int 0
                 _name_
                 _empty_line_
 
-   Example 1.11. shv_get usage
+   Example 1.14. shv_get usage
 ...
 $ kamctl fifo shv_get debug
 $ kamctl fifo shv_get

+ 79 - 0
modules/pv/doc/pv_admin.xml

@@ -293,6 +293,85 @@ xavp_params_explode("a=b;c=d;e=d", "x");
 #    $xavp(x=>a) = "b";
 #    $xavp(x=>c) = "d";
 #    $xavp(x=>e) = "f";
+...
+				</programlisting>
+			</example>
+		</section>
+		<section id="pv.f.sbranch_set_ruri">
+			<title>
+				<function moreinfo="none">sbranch_set_ruri()</function>
+			</title>
+			<para>
+				Use the attributes from static branch ($sbranch(key) variable) to
+				set request URI and the other fields of the branch associated with
+				request URI (destination URI, path, ...).
+			</para>
+			<para>
+				Content of the static branch is not reset after this function is
+				executed. It has to be done explicitely with sbranch_reset().
+			</para>
+			<para>
+			Function can be used from REQUEST_ROUTE, BRANCH_ROUTE or FAILURE_ROUTE.
+			</para>
+			<example>
+				<title><function>sbranch_set_ruri()</function> usage</title>
+				<programlisting format="linespecific">
+...
+sbranch_reset();
+$sbranch(uri) = "sip:127.0.0.1:5080";
+$sbranch(dst_uri) =  "sip:127.0.0.1:5090";
+$sbranch(path) =  "sip:127.0.0.1:5090, sip:127.0.0.1:5094";
+$sbranch(send_socket) =  "udp:127.0.0.1:5060";
+sbranch_set_ruri();
+...
+				</programlisting>
+			</example>
+		</section>
+		<section id="pv.f.sbranch_append">
+			<title>
+				<function moreinfo="none">sbranch_append()</function>
+			</title>
+			<para>
+				Use the attributes from static branch ($sbranch(key) variable) to
+				append a new branch to destination set. It is an alternative to
+				append_branch() that allows setting each attribute specific to
+				the branch.
+			</para>
+			<para>
+				Content of the static branch is not reset after this function is
+				executed. It has to be done explicitely with sbranch_reset().
+			</para>
+			<para>
+			Function can be used from REQUEST_ROUTE, BRANCH_ROUTE or FAILURE_ROUTE.
+			</para>
+			<example>
+				<title><function>sbranch_append()</function> usage</title>
+				<programlisting format="linespecific">
+...
+sbranch_reset();
+$sbranch(uri) = "sip:127.0.0.1:5080";
+$sbranch(dst_uri) =  "sip:127.0.0.1:5090";
+$sbranch(send_socket) =  "udp:127.0.0.1:5060";
+sbranch_append();
+...
+				</programlisting>
+			</example>
+		</section>
+		<section id="pv.f.sbranch_reset">
+			<title>
+				<function moreinfo="none">sbranch_reset()</function>
+			</title>
+			<para>
+				Reset the content of static branch ($sbranch(key) variable.
+			</para>
+			<para>
+			Function can be used from REQUEST_ROUTE, BRANCH_ROUTE or FAILURE_ROUTE.
+			</para>
+			<example>
+				<title><function>sbranch_append()</function> usage</title>
+				<programlisting format="linespecific">
+...
+sbranch_reset();
 ...
 				</programlisting>
 			</example>

+ 40 - 0
modules/pv/pv.c

@@ -479,6 +479,10 @@ static int is_int(struct sip_msg* msg, char* pvar, char* s2);
 static int pv_typeof(sip_msg_t *msg, char *pv, char *t);
 static int pv_not_empty(sip_msg_t *msg, char *pv, char *s2);
 static int w_xavp_params_explode(sip_msg_t *msg, char *pparams, char *pxname);
+static int w_sbranch_set_ruri(sip_msg_t *msg, char p1, char *p2);
+static int w_sbranch_append(sip_msg_t *msg, char p1, char *p2);
+static int w_sbranch_reset(sip_msg_t *msg, char p1, char *p2);
+
 static int pv_init_rpc(void);
 
 static cmd_export_t cmds[]={
@@ -501,6 +505,12 @@ static cmd_export_t cmds[]={
 	{"xavp_params_explode", (cmd_function)w_xavp_params_explode,
 		2, fixup_spve_spve, fixup_free_spve_spve,
 		ANY_ROUTE},
+	{"sbranch_set_ruri",  (cmd_function)w_sbranch_set_ruri,  0, 0, 0,
+		ANY_ROUTE },
+	{"sbranch_append",    (cmd_function)w_sbranch_append,    0, 0, 0,
+		ANY_ROUTE },
+	{"sbranch_reset",     (cmd_function)w_sbranch_reset,     0, 0, 0,
+		ANY_ROUTE },
 
 	{0,0,0,0,0,0}
 };
@@ -693,6 +703,36 @@ static int w_xavp_params_explode(sip_msg_t *msg, char *pparams, char *pxname)
 	return 1;
 }
 
+/**
+ *
+ */
+static int w_sbranch_set_ruri(sip_msg_t *msg, char p1, char *p2)
+{
+	if(sbranch_set_ruri(msg)<0)
+		return -1;
+	return 1;
+}
+
+/**
+ *
+ */
+static int w_sbranch_append(sip_msg_t *msg, char p1, char *p2)
+{
+	if(sbranch_append(msg)<0)
+		return -1;
+	return 1;
+}
+
+/**
+ *
+ */
+static int w_sbranch_reset(sip_msg_t *msg, char p1, char *p2)
+{
+	if(sbranch_reset()<0)
+		return -1;
+	return 1;
+}
+
 static const char* rpc_shv_set_doc[2] = {
 	"Set a shared variable (args: name type value)",
 	0

+ 156 - 2
modules/pv/pv_branch.c

@@ -270,7 +270,11 @@ int pv_set_branchx_helper(sip_msg_t *msg, pv_param_t *param,
 			/* 0 - uri */
 			if(val==NULL || (val->flags&PV_VAL_NULL))
 			{
-				drop_sip_branch(idx);
+				if(btype==1) {
+					memset(br, 0, sizeof(branch_t));
+				} else {
+					drop_sip_branch(idx);
+				}
 			} else {
 				if(!(val->flags&PV_VAL_STR))
 				{
@@ -279,7 +283,11 @@ int pv_set_branchx_helper(sip_msg_t *msg, pv_param_t *param,
 				}
 				if(val->rs.len<=0)
 				{
-					drop_sip_branch(idx);
+					if(btype==1) {
+						memset(br, 0, sizeof(branch_t));
+					} else {
+						drop_sip_branch(idx);
+					}
 				} else {
 					if (unlikely(val->rs.len > MAX_URI_SIZE - 1))
 					{
@@ -581,3 +589,149 @@ error:
 	return -1;
 }
 
+/**
+ *
+ */
+int sbranch_set_ruri(sip_msg_t *msg)
+{
+	str sv;
+	flag_t old_bflags;
+	branch_t *br;
+	int ret;
+
+	ret = 0;
+	br = &_pv_sbranch;
+	if(br->len==0)
+		return -1;
+
+	sv.s = br->uri;
+	sv.len = br->len;
+
+	if (rewrite_uri(msg, &sv) < 0) {
+		LM_ERR("unable to rewrite Request-URI\n");
+		ret = -3;
+		goto error;
+	}
+
+	/* reset next hop address */
+	reset_dst_uri(msg);
+	if(br->dst_uri_len>0) {
+		sv.s = br->dst_uri;
+		sv.len = br->dst_uri_len;
+		if (set_dst_uri(msg, &sv) < 0) {
+			ret = -3;
+			goto error;
+		}
+	}
+
+	reset_path_vector(msg);
+	if(br->path_len==0) {
+		sv.s = br->path;
+		sv.len = br->path_len;
+		if(set_path_vector(msg, &sv) < 0) {
+			ret = -4;
+			goto error;
+		}
+	}
+
+	reset_instance(msg);
+	if (br->instance_len) {
+		sv.s = br->instance;
+		sv.len = br->instance_len;
+	    if (set_instance(msg, &sv) < 0) {
+			ret = -5;
+			goto error;
+	    }
+	}
+
+	reset_ruid(msg);
+	if (br->ruid_len) {
+		sv.s = br->ruid;
+		sv.len = br->ruid_len;
+	    if (set_ruid(msg, &sv) < 0) {
+			ret = -6;
+			goto error;
+	    }
+	}
+
+	reset_ua(msg);
+	if (br->location_ua_len) {
+		sv.s = br->location_ua;
+		sv.len = br->location_ua_len;
+	    if (set_ua(msg, &sv) < 0) {
+			ret = -7;
+			goto error;
+	    }
+	}
+
+	if (br->force_send_socket)
+		set_force_socket(msg, br->force_send_socket);
+
+	msg->reg_id = br->reg_id;
+	set_ruri_q(br->q);
+	old_bflags = 0;
+	getbflagsval(0, &old_bflags);
+	setbflagsval(0, old_bflags|br->flags);
+
+	return 0;
+error:
+	return ret;
+}
+
+/**
+ *
+ */
+int sbranch_append(sip_msg_t *msg)
+{
+	str uri = {0};
+	str duri = {0};
+	int lq = 0;
+	str path = {0};
+	unsigned int fl = 0;
+	struct socket_info* fsocket = NULL;
+	str ruid = {0};
+	str location_ua = {0};
+	branch_t *br;
+
+	br = &_pv_sbranch;
+	if(br->len==0)
+		return -1;
+
+	uri.s = br->uri;
+	uri.len = br->len;
+
+	if(br->dst_uri_len==0) {
+		duri.s = br->dst_uri;
+		duri.len = br->dst_uri_len;
+	}
+	if(br->path_len==0) {
+		path.s = br->path;
+		path.len = br->path_len;
+	}
+	if(br->ruid_len==0) {
+		ruid.s = br->ruid;
+		ruid.len = br->ruid_len;
+	}
+	if(br->location_ua_len==0) {
+		location_ua.s = br->location_ua;
+		location_ua.len = br->location_ua_len;
+	}
+
+	if (append_branch(msg, &uri, &duri, &path, br->q, br->flags,
+					  br->force_send_socket, 0 /*instance*/, br->reg_id,
+					  &ruid, &location_ua)
+			    == -1) {
+		LM_ERR("failed to append static branch\n");
+		return -1;
+	}
+	return 0;
+}
+
+/**
+ *
+ */
+int sbranch_reset(void)
+{
+	memset(&_pv_sbranch, 0, sizeof(branch_t));
+	return 0;
+}

+ 4 - 0
modules/pv/pv_branch.h

@@ -44,5 +44,9 @@ int pv_get_nh(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res);
 int pv_parse_nh_name(pv_spec_p sp, str *in);
 
+int sbranch_set_ruri(sip_msg_t *msg);
+int sbranch_append(sip_msg_t *msg);
+int sbranch_reset(void);
+
 #endif