[sr-dev] git:master: cfg framework: @cfg_selected.group added

Miklos Tirpak miklos at iptel.org
Mon Jan 31 15:47:26 CET 2011


Module: sip-router
Branch: master
Commit: f9ad9ccf83b9341fa5a063cb3bd844567f3f06e6
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=f9ad9ccf83b9341fa5a063cb3bd844567f3f06e6

Author: Miklos Tirpak <miklos at iptel.org>
Committer: Miklos Tirpak <miklos at iptel.org>
Date:   Mon Jan 31 12:45:57 2011 +0100

cfg framework: @cfg_selected.group added

@cfg_selected.<group_name> returns the selected instance id of the
specified group. If no group instance is selected, i.e. the default
is used, then empty string is returned.

---

 cfg/cfg_select.c |  102 +++++++++++++++++++++++++++++++++++++++++++++++-------
 cfg/cfg_select.h |    3 ++
 select_core.h    |    2 +
 3 files changed, 94 insertions(+), 13 deletions(-)

diff --git a/cfg/cfg_select.c b/cfg/cfg_select.c
index 68c7e41..cb0a2f1 100644
--- a/cfg/cfg_select.c
+++ b/cfg/cfg_select.c
@@ -60,14 +60,17 @@ static int cfg_new_select(str *gname, str *vname, void **group_p, void **var_p)
 	if (!sel->gname.s) goto error;
 	memcpy(sel->gname.s, gname->s, gname->len);
 	sel->gname.len = gname->len;
+	sel->group_p = group_p;
 
-	sel->vname.s = (char *)pkg_malloc(sizeof(char)*vname->len);
-	if (!sel->vname.s) goto error;
-	memcpy(sel->vname.s, vname->s, vname->len);
-	sel->vname.len = vname->len;
+	if (vname) {
+		sel->vname.s = (char *)pkg_malloc(sizeof(char)*vname->len);
+		if (!sel->vname.s) goto error;
+		memcpy(sel->vname.s, vname->s, vname->len);
+		sel->vname.len = vname->len;
+
+		sel->var_p = var_p;
+	}
 
-	sel->group_p = group_p;
-	sel->var_p = var_p;
 
 	sel->next = cfg_non_fixed_selects;
 	cfg_non_fixed_selects = sel;
@@ -111,14 +114,23 @@ int cfg_fixup_selects()
 
 	for (sel=cfg_non_fixed_selects; sel; sel=sel->next) {
 
-		if (cfg_lookup_var(&sel->gname, &sel->vname, &group, &var)) {
-			LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown variable: %.*s.%.*s\n",
-				sel->gname.len, sel->gname.s,
-				sel->vname.len, sel->vname.s);
-			return -1;
+		if (sel->var_p) {
+			if (cfg_lookup_var(&sel->gname, &sel->vname, &group, &var)) {
+				LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown variable: %.*s.%.*s\n",
+					sel->gname.len, sel->gname.s,
+					sel->vname.len, sel->vname.s);
+				return -1;
+			}
+			*(sel->group_p) = (void *)group;
+			*(sel->var_p) = (void *)var;
+		} else {
+			if (!(group = cfg_lookup_group(sel->gname.s, sel->gname.len))) {
+				LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown configuration group: %.*s\n",
+					sel->gname.len, sel->gname.s);
+				return -1;
+			}
+			*(sel->group_p) = (void *)group;
 		}
-		*(sel->group_p) = (void *)group;
-		*(sel->var_p) = (void *)var;
 	}
 	/* the select list is not needed anymore */
 	cfg_free_selects();
@@ -360,3 +372,67 @@ int read_cfg_var_str(struct cfg_read_handle *read_handle, str *val)
 	*val = *(str *)(v2);
 	return 0;
 }
+
+/* return the selected group instance */
+int cfg_selected_inst(str *res, select_t *s, struct sip_msg *msg)
+{
+	cfg_group_t	*group;
+	cfg_group_inst_t	*inst;
+
+	if (msg == NULL) {
+		/* fixup call */
+
+		/* one parameter is mandatory: group name */
+		if (s->n != 2) {
+			LOG(L_ERR, "ERROR: selected_inst(): One parameter is expected\n");
+			return -1;
+		}
+
+		if (s->params[1].type != SEL_PARAM_STR) {
+			LOG(L_ERR, "ERROR: selected_inst(): string parameter is expected\n");
+			return -1;
+		}
+
+		/* look-up the group and the variable */
+		if (!(group = cfg_lookup_group(s->params[1].v.s.s, s->params[1].v.s.len))) {
+			if (cfg_shmized) {
+				LOG(L_ERR, "ERROR: selected_inst(): unknown configuration group: %.*s\n",
+					s->params[1].v.s.len, s->params[1].v.s.s);
+				return -1;
+			}
+			/* The group was not found, add it to the non-fixed select list.
+			 * So we act as if the fixup was successful, and we retry it later */
+			if (cfg_new_select(&s->params[1].v.s, NULL,
+						&s->params[1].v.p, NULL))
+				return -1;
+
+			LOG(L_DBG, "DEBUG: selected_inst(): select fixup is postponed: %.*s\n",
+				s->params[1].v.s.len, s->params[1].v.s.s);
+
+			s->params[1].type = SEL_PARAM_PTR;
+			s->params[1].v.p = NULL;
+
+			return 0;
+		}
+
+		s->params[1].type = SEL_PARAM_PTR;
+		s->params[1].v.p = (void *)group;
+
+		return 1;
+	}
+
+	group = (cfg_group_t *)s->params[1].v.p;
+	if (!group) return -1;
+
+	/* Get the current group instance from the group handle. */
+	inst = CFG_HANDLE_TO_GINST(*(group->handle));
+
+	if (inst) {
+		res->s = int2str(inst->id, &res->len);
+	} else {
+		res->s = "";
+		res->len = 0;
+	}
+	return 0;
+}
+
diff --git a/cfg/cfg_select.h b/cfg/cfg_select.h
index b7d454e..743b707 100644
--- a/cfg/cfg_select.h
+++ b/cfg/cfg_select.h
@@ -61,4 +61,7 @@ unsigned int read_cfg_var(struct cfg_read_handle *read_handle, void **val);
 int read_cfg_var_int(struct cfg_read_handle *read_handle, int *val);
 int read_cfg_var_str(struct cfg_read_handle *read_handle, str *val);
 
+/* return the selected group instance */
+int cfg_selected_inst(str *res, select_t *s, struct sip_msg *msg);
+
 #endif /* _CFG_SELECT_H */
diff --git a/select_core.h b/select_core.h
index d28e0e6..8cec775 100644
--- a/select_core.h
+++ b/select_core.h
@@ -215,6 +215,7 @@ SELECT_F(select_identity)
 SELECT_F(select_identity_info)
 
 SELECT_F(select_cfg_var)
+SELECT_F(cfg_selected_inst)
 
 static select_row_t select_core[] = {
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("ruri"), select_ruri, 0}, /* not the same as request.uri because it is involved by new_uri */
@@ -410,6 +411,7 @@ static select_row_t select_core[] = {
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("identity_info"), select_identity_info, 0},
 
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("cfg_get"), select_cfg_var, CONSUME_ALL | FIXUP_CALL },
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("cfg_selected"), cfg_selected_inst, CONSUME_NEXT_STR | FIXUP_CALL },
 
 	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
 };




More information about the sr-dev mailing list