浏览代码

- change interface of find_carrier to const str * (the same as
the add_domain function)
- adapt the fixup function for the carrier to this change
- merge the two almost identical gp2carrier_id and gp2domain_id
functions, use a generic function with function pointer for the
lookup method instead


git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@5152 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt 17 年之前
父节点
当前提交
d8de7bdc43
共有 4 个文件被更改,包括 35 次插入98 次删除
  1. 8 12
      modules/carrierroute/cr_fixup.c
  2. 23 82
      modules/carrierroute/cr_func.c
  3. 3 3
      modules/carrierroute/cr_map.c
  4. 1 1
      modules/carrierroute/cr_map.h

+ 8 - 12
modules/carrierroute/cr_fixup.c

@@ -61,7 +61,7 @@ static enum hash_source hash_fixup(const char * my_hash_source) {
 
 
 /**
- * fixes the module functions' parameters if it is a carrier.
+ * Fixes the module functions' parameters if it is a carrier.
  * supports name string and PVs.
  *
  * @param param the parameter
@@ -78,8 +78,8 @@ static int carrier_fixup(void ** param) {
 	if (((gparam_p)(*param))->type == GPARAM_TYPE_STR) {
 		/* This is a name string, convert to a int */
 		((gparam_p)(*param))->type=GPARAM_TYPE_INT;
-		/* get domain id */
-		if ((((gparam_p)(*param))->v.ival = find_carrier(((gparam_p)(*param))->v.sval)) < 0) {
+		/* get carrier id */
+		if ((((gparam_p)(*param))->v.ival = find_carrier(&((gparam_p)(*param))->v.sval)) < 0) {
 			LM_ERR("could not add carrier\n");
 			pkg_free(*param);
 			return -1;
@@ -90,7 +90,7 @@ static int carrier_fixup(void ** param) {
 
 
 /**
- * fixes the module functions' parameters if it is a domain.
+ * Fixes the module functions' parameters if it is a domain.
  * supports name string, and PVs.
  *
  * @param param the parameter
@@ -119,7 +119,7 @@ static int domain_fixup(void ** param) {
 
 
 /**
- * fixes the module functions' parameters in case of AVP names.
+ * Fixes the module functions' parameters in case of AVP names.
  *
  * @param param the parameter
  *
@@ -169,8 +169,7 @@ int cr_route_fixup(void ** param, int param_no) {
 		}
 	}
 	else if ((param_no == 3) || (param_no == 4)){
-		/* prefix matching */
-		/* rewrite user */
+		/* prefix matching, rewrite user */
 		if (fixup_spve_null(param, 1) != 0) {
 			LM_ERR("cannot fixup parameter %d\n", param_no);
 			return -1;
@@ -223,9 +222,7 @@ int cr_load_next_domain_fixup(void ** param, int param_no) {
 		}
 	}
 	else if ((param_no == 3) || (param_no == 4) || (param_no == 5)) {
-		/* prefix matching */
-		/* host */
-		/* reply code */
+		/* prefix matching, host, reply code */
 		if (fixup_spve_null(param, 1) != 0) {
 			LM_ERR("cannot fixup parameter %d\n", param_no);
 			return -1;
@@ -250,8 +247,7 @@ int cr_load_user_carrier_fixup(void ** param, int param_no) {
 	}
 
 	if ((param_no == 1) || (param_no == 2)) {
-		/* user */
-		/* domain */
+		/* user, domain */
 		if (fixup_spve_null(param, 1) != 0) {
 			LM_ERR("cannot fixup parameter %d\n", param_no);
 			return -1;

+ 23 - 82
modules/carrierroute/cr_func.c

@@ -62,80 +62,21 @@ static const str AT_SIGN  = { .s="@",     .len=1 };
 
 
 /**
- * Get the carrier id from gparam_t structure.
+ * Get the id that belongs to a string name from gparam_t structure.
  *
- * @param mp carrier id as integer, pseudo-variable or AVP name of carrier
+ * Get the id that belongs to a string name from gparam_t structure, use the
+ * search_id function for the lookup.
  * @param _msg SIP message
- * @return carrier id on success, -1 otherwise
- *
- */
-int gp2carrier_id(struct sip_msg * _msg, gparam_t *gp) {
-	int carrier_id;
-	struct usr_avp *avp;
-	int_str avp_val;
-	str tmp;
-
-	/* TODO combine the redundant parts of the logic */
-	switch (gp->type) {
-	case GPARAM_TYPE_INT:
-		return gp->v.ival;
-		break;
-	case GPARAM_TYPE_PVE:
-		if (gp->v.pve->spec.type==PVT_AVP) {
-			avp = search_first_avp(gp->v.pve->spec.pvp.pvn.u.isname.type,
-						gp->v.pve->spec.pvp.pvn.u.isname.name, &avp_val, 0);
-			if (!avp) {
-				LM_ERR("cannot find AVP '%.*s'\n", gp->v.pve->spec.pvp.pvn.u.isname.name.s.len,
-						gp->v.pve->spec.pvp.pvn.u.isname.name.s.s);
-				return -1;
-			}
-			if ((avp->flags&AVP_VAL_STR)==0) {
-				return avp_val.n;
-			} else {
-				carrier_id = find_carrier(avp_val.s);
-				if (carrier_id < 0) {
-					LM_ERR("could not find carrier '%.*s' from AVP\n",
-							gp->v.pve->spec.pvp.pvn.u.isname.name.s.len,
-							gp->v.pve->spec.pvp.pvn.u.isname.name.s.s);
-					return -1;
-				}
-				return carrier_id;
-			}
-		} else {
-			/* retrieve carrier name from parameter */
-			if (fixup_get_svalue(_msg, gp, &tmp)<0) {
-				LM_ERR("cannot print the carrier\n");
-				return -1;
-			}
-			carrier_id = find_carrier(tmp);
-			if (carrier_id < 0) {
-				LM_WARN("could not find carrier '%.*s' from PV\n", tmp.len, tmp.s);
-				/* might be using fallback later... */
-			}
-			return carrier_id;
-		}
-	default:
-		LM_ERR("invalid carrier type\n");
-		return -1;
-	}
-}
-
-
-/**
- * Get the domain id from gparam_t structure.
- *
- * @param _msg SIP message
- * @param mp carrier id as integer, pseudo-variable or AVP name of carrier
- * @return carrier id on success, -1 otherwise
- *
+ * @param gp id as integer, pseudo-variable or AVP name of carrier
+ * @param search_if lookup function
+ * @return id on success, -1 otherwise
  */
-int gp2domain_id(struct sip_msg * _msg, gparam_t *gp) {
-	int domain_id;
+static inline int cr_gp2id(struct sip_msg *_msg, gparam_t *gp, int (*search_id)(const str* name)) {
+	int id;
 	struct usr_avp *avp;
 	int_str avp_val;
 	str tmp;
 
-	/* TODO combine the redundant parts of the logic */
 	switch (gp->type) {
 	case GPARAM_TYPE_INT:
 		return gp->v.ival;
@@ -153,30 +94,30 @@ int gp2domain_id(struct sip_msg * _msg, gparam_t *gp) {
 			if ((avp->flags&AVP_VAL_STR)==0) {
 				return avp_val.n;
 			} else {
-				domain_id = add_domain(&avp_val.s);
-				if (domain_id < 0) {
-					LM_ERR("could not find domain '%.*s' from AVP\n",
+				id = search_id(&avp_val.s);
+				if (id < 0) {
+					LM_ERR("could not find id '%.*s' from AVP\n",
 							gp->v.pve->spec.pvp.pvn.u.isname.name.s.len,
 							gp->v.pve->spec.pvp.pvn.u.isname.name.s.s);
 					return -1;
 				}
-				return domain_id;
+				return id;
 			}
 		} else {
-			/* retrieve domain name from parameter */
+			/* retrieve name from parameter */
 			if (fixup_get_svalue(_msg, gp, &tmp)<0) {
-				LM_ERR("cannot print the domain\n");
+				LM_ERR("cannot print the name from PV\n");
 				return -1;
 			}
-			domain_id = add_domain(&tmp);
-			if (domain_id < 0) {
-				LM_ERR("could not find domain '%.*s' from PV\n", tmp.len, tmp.s);
+			id = search_id(&tmp);
+			if (id < 0) {
+				LM_ERR("could not find id '%.*s' from PV\n", tmp.len, tmp.s);
 				return -1;
 			}
-			return domain_id;
+			return id;
 		}
 	default:
-		LM_ERR("invalid domain type\n");
+		LM_ERR("invalid parameter type\n");
 		return -1;
 	}
 }
@@ -552,8 +493,8 @@ int cr_do_route(struct sip_msg * _msg, gparam_t *_carrier,
 	struct domain_data_t * domain_data;
 	struct action act;
 
-	carrier_id = gp2carrier_id(_msg, _carrier);
-	domain_id = gp2domain_id(_msg, _domain);
+	carrier_id = cr_gp2id(_msg, _carrier, find_carrier);
+	domain_id = cr_gp2id(_msg, _domain, add_domain);
 	if (domain_id < 0) {
 		LM_ERR("invalid domain id %d\n", domain_id);
 		return -1;
@@ -742,8 +683,8 @@ int cr_load_next_domain(struct sip_msg * _msg, gparam_t *_carrier,
 	struct carrier_data_t * carrier_data;
 	struct domain_data_t * domain_data;
 
-	carrier_id = gp2carrier_id(_msg, _carrier);
-	domain_id = gp2domain_id(_msg, _domain);
+	carrier_id = cr_gp2id(_msg, _carrier, find_carrier);
+	domain_id = cr_gp2id(_msg, _domain, add_domain);
 	if (domain_id < 0) {
 		LM_ERR("invalid domain id %d\n", domain_id);
 		return -1;

+ 3 - 3
modules/carrierroute/cr_map.c

@@ -195,18 +195,18 @@ int add_carrier(const str * tree, int carrier_id) {
  * @return values: on succcess the id for this carrier name,
  * -1 on failure
  */
-int find_carrier(str carrier_name) {
+int find_carrier(const str * carrier_name) {
 	struct carrier_map_t * tmp;
 	if (!carrier_map) {
 		return -1;
 	}
-	if (carrier_name.len <= 0) {
+	if (carrier_name->len <= 0) {
 		return -1;
 	}
 	tmp = *carrier_map;
 
 	while (tmp) {
-		if (str_strcmp(&carrier_name, &tmp->name) == 0) {
+		if (str_strcmp(carrier_name, &tmp->name) == 0) {
 			return tmp->id;
 		}
 		tmp = tmp->next;

+ 1 - 1
modules/carrierroute/cr_map.h

@@ -72,7 +72,7 @@ int add_carrier(const str * tree, int carrier_id);
  * @return values: on succcess the id for this carrier name,
  * -1 on failure
  */
-int find_carrier(str carrier_name);
+int find_carrier(const str * carrier_name);
 
 
 /**