[sr-dev] git:master: core: script engine cleanups

Andrei Pelinescu-Onciul andrei at iptel.org
Sat Jul 18 11:10:35 CEST 2009


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

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Sat Jul 18 11:05:05 2009 +0200

core: script engine cleanups

Use named enums for action types, expression types, expressions
left operands, expression right operands and action parameter
types. This should avoid future type mismatching bugs (like trying
 to compare a left expr. operand type with a type that can exist
 only on the right part) by triggering compile time warnings.
For extra protection the expression left operands and right
operands possible value do not overlap anymore.

---

 action.c       |    3 ++-
 route.c        |    8 ++++++--
 route_struct.c |   13 +++++++++++--
 route_struct.h |   53 ++++++++++++++++++++++++++++++++++++++---------------
 4 files changed, 57 insertions(+), 20 deletions(-)

diff --git a/action.c b/action.c
index a793263..848ec59 100644
--- a/action.c
+++ b/action.c
@@ -1202,9 +1202,10 @@ match_cleanup:
 			else
 				ret=v;
 			break;
-
+/*
 		default:
 			LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
+*/
 	}
 skip:
 	return ret;
diff --git a/route.c b/route.c
index 7c01a3b..fc25c64 100644
--- a/route.c
+++ b/route.c
@@ -623,6 +623,7 @@ int fix_actions(struct action* a)
 	struct proxy_l* p;
 	char *tmp;
 	int ret;
+	int i;
 	union cmd_export_u* cmd;
 	str s;
 	struct hostent* he;
@@ -886,7 +887,6 @@ int fix_actions(struct action* a)
 			case MODULEX_T:
 				cmd = t->val[0].u.data;
 				if (cmd && cmd->c.fixup) {
-					int i;
 					DBG("fixing %s()\n", cmd->c.name);
 					if (t->val[1].u.number==0) {
 						ret = cmd->c.fixup(0, 0);
@@ -976,6 +976,9 @@ int fix_actions(struct action* a)
 									(unsigned int)t->val[0].u.number);
 				}
 				break;
+			default:
+				/* no fixup required for the rest */
+				break;
 		}
 	}
 	return 0;
@@ -1699,10 +1702,11 @@ inline static int eval_elem(struct run_act_ctx* h, struct expr* e,
 	case PVAR_O:
 		ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
 		break;
-
+/*
 	default:
 		LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
 		    e->l_type);
+*/
 	}
 	return ret;
 error:
diff --git a/route_struct.c b/route_struct.c
index 7fce4b8..9f4d6b2 100644
--- a/route_struct.c
+++ b/route_struct.c
@@ -113,7 +113,8 @@ error:
 	return 0;
 }
 
-struct expr* mk_elem(int op, int ltype, void* lparam, int rtype, void* rparam)
+struct expr* mk_elem(int op, expr_l_type ltype, void* lparam,
+							 expr_r_type rtype, void* rparam)
 {
 	struct expr * e;
 	e=(struct expr*)pkg_malloc(sizeof (struct expr));
@@ -131,7 +132,15 @@ error:
 }
 
 
-struct action* mk_action(int type, int count/* of couples {type,val} */, .../* int type1, void *val1 [, int type2, void *val2, ...] */) {
+/** create an action structure (parser use).
+ * @param type - type of the action
+ * @param count - count of couples {param_type,val}
+ * @param ... -   count {param_type, val} pairs, where param_type is
+ *                action_param_type.
+ * @return  new action structure on success (pkg_malloc'ed) or 0 on error.
+ */
+struct action* mk_action(enum action_type type, int count, ...)
+{
 	va_list args;
 	int i;
 	struct action* a;
diff --git a/route_struct.h b/route_struct.h
index 340cbf6..090388c 100644
--- a/route_struct.h
+++ b/route_struct.h
@@ -60,15 +60,26 @@
  */
 
 
-enum { EXP_T=1, ELEM_T };
-enum { LOGAND_OP=1, LOGOR_OP, NOT_OP, BINAND_OP, BINOR_OP };
-enum { EQUAL_OP=10, MATCH_OP, GT_OP, LT_OP, GTE_OP, LTE_OP, DIFF_OP, NO_OP };
-enum { METHOD_O=1, URI_O, FROM_URI_O, TO_URI_O, SRCIP_O, SRCPORT_O,
-	   DSTIP_O, DSTPORT_O, PROTO_O, AF_O, MSGLEN_O, DEFAULT_O, ACTION_O,
+/* expression type */
+enum expr_type { EXP_T=1, ELEM_T };
+enum expr_op {
+	/* expression operator if type==EXP_T */
+	LOGAND_OP=1, LOGOR_OP, NOT_OP, BINAND_OP, BINOR_OP,
+	/* expression operator if type==ELEM_T */
+	EQUAL_OP=10, MATCH_OP, GT_OP, LT_OP, GTE_OP, LTE_OP, DIFF_OP, NO_OP
+};
+/* expression left member "special" type (if type==ELEM_T)
+  (start at 51 for debugging purposes: it's better to not overlap with 
+   expr_r_type)
+*/
+enum _expr_l_type{
+	   METHOD_O=51, URI_O, FROM_URI_O, TO_URI_O, SRCIP_O, SRCPORT_O,
+	   DSTIP_O, DSTPORT_O, PROTO_O, AF_O, MSGLEN_O, ACTION_O,
 	   NUMBER_O, AVP_O, SNDIP_O, SNDPORT_O, TOIP_O, TOPORT_O, SNDPROTO_O,
 	   SNDAF_O, RETCODE_O, SELECT_O, PVAR_O, RVEXP_O};
-
-enum { FORWARD_T=1, SEND_T, DROP_T, LOG_T, ERROR_T, ROUTE_T, EXEC_T,
+/* action types */
+enum action_type{
+		FORWARD_T=1, SEND_T, DROP_T, LOG_T, ERROR_T, ROUTE_T, EXEC_T,
 		SET_HOST_T, SET_HOSTPORT_T, SET_USER_T, SET_USERPASS_T,
 		SET_PORT_T, SET_URI_T, SET_HOSTPORTTRANS_T, SET_HOSTALL_T,
 		SET_USERPHONE_T,
@@ -96,7 +107,10 @@ enum { FORWARD_T=1, SEND_T, DROP_T, LOG_T, ERROR_T, ROUTE_T, EXEC_T,
 		ADD_T,
 		UDP_MTU_TRY_PROTO_T
 };
-enum { NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
+/* parameter types for actions or types for expression right operands
+   (WARNING right operands only, not working for left operands) */
+enum _operand_subtype{
+		NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
 		EXPR_ST, ACTIONS_ST, MODEXP_ST, MODFIXUP_ST, URIHOST_ST, URIPORT_ST,
 		MYSELF_ST, STR_ST, SOCKID_ST, SOCKETINFO_ST, ACTION_ST, AVP_ST,
 		SELECT_ST, PVAR_ST,
@@ -105,6 +119,10 @@ enum { NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
 		BLOCK_ST, JUMPTABLE_ST, CONDTABLE_ST, MATCH_CONDTABLE_ST
 };
 
+typedef enum _expr_l_type expr_l_type;
+typedef enum _operand_subtype expr_r_type;
+typedef enum _operand_subtype action_param_type;
+
 /* run flags */
 #define EXIT_R_F   1
 #define RETURN_R_F 2
@@ -134,15 +152,16 @@ union exp_op {
 };
 
 struct expr{
-	int type; /* exp, exp_elem */
-	int op; /* and, or, not | ==,  =~ */
-	int l_type, r_type;
+	enum expr_type type; /* exp, exp_elem */
+	enum expr_op op; /* and, or, not | ==,  =~ */
+	expr_l_type l_type;
+	expr_r_type r_type;
 	union exp_op l;
 	union exp_op r;
 };
 
 typedef struct {
-	int type;
+	action_param_type type;
 	union {
 		long number;
 		char* string;
@@ -160,16 +179,20 @@ typedef struct {
 #define MAX_ACTIONS (2+6)
 
 struct action{
-	int type;  /* forward, drop, log, send ...*/
+	enum action_type type;  /* forward, drop, log, send ...*/
 	int count;
 	struct action* next;
 	action_u_t val[MAX_ACTIONS];
 };
 
 struct expr* mk_exp(int op, struct expr* left, struct expr* right);
-struct expr* mk_elem(int op, int ltype, void* lparam, int rtype, void* rparam);
+struct expr* mk_elem(int op, expr_l_type ltype, void* lparam,
+							 expr_r_type rtype, void* rparam);
+
+/* @param type - type of the action
+   @param count - count of couples {type,val} (variable number of parameters)*/
+struct action* mk_action(enum action_type type, int count, ...);
 
-struct action* mk_action(int type, int count/* of couples {type,val} */, .../* int type1, void *val1 [, int type2, void *val2, ...] */);
 struct action* append_action(struct action* a, struct action* b);
 
 void print_action(struct action* a);




More information about the sr-dev mailing list