Browse Source

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.
Andrei Pelinescu-Onciul 16 years ago
parent
commit
aa9fa735a8
4 changed files with 57 additions and 20 deletions
  1. 2 1
      action.c
  2. 6 2
      route.c
  3. 11 2
      route_struct.c
  4. 38 15
      route_struct.h

+ 2 - 1
action.c

@@ -1202,9 +1202,10 @@ match_cleanup:
 			else
 			else
 				ret=v;
 				ret=v;
 			break;
 			break;
-
+/*
 		default:
 		default:
 			LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
 			LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
+*/
 	}
 	}
 skip:
 skip:
 	return ret;
 	return ret;

+ 6 - 2
route.c

@@ -623,6 +623,7 @@ int fix_actions(struct action* a)
 	struct proxy_l* p;
 	struct proxy_l* p;
 	char *tmp;
 	char *tmp;
 	int ret;
 	int ret;
+	int i;
 	union cmd_export_u* cmd;
 	union cmd_export_u* cmd;
 	str s;
 	str s;
 	struct hostent* he;
 	struct hostent* he;
@@ -886,7 +887,6 @@ int fix_actions(struct action* a)
 			case MODULEX_T:
 			case MODULEX_T:
 				cmd = t->val[0].u.data;
 				cmd = t->val[0].u.data;
 				if (cmd && cmd->c.fixup) {
 				if (cmd && cmd->c.fixup) {
-					int i;
 					DBG("fixing %s()\n", cmd->c.name);
 					DBG("fixing %s()\n", cmd->c.name);
 					if (t->val[1].u.number==0) {
 					if (t->val[1].u.number==0) {
 						ret = cmd->c.fixup(0, 0);
 						ret = cmd->c.fixup(0, 0);
@@ -976,6 +976,9 @@ int fix_actions(struct action* a)
 									(unsigned int)t->val[0].u.number);
 									(unsigned int)t->val[0].u.number);
 				}
 				}
 				break;
 				break;
+			default:
+				/* no fixup required for the rest */
+				break;
 		}
 		}
 	}
 	}
 	return 0;
 	return 0;
@@ -1699,10 +1702,11 @@ inline static int eval_elem(struct run_act_ctx* h, struct expr* e,
 	case PVAR_O:
 	case PVAR_O:
 		ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
 		ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
 		break;
 		break;
-
+/*
 	default:
 	default:
 		LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
 		LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
 		    e->l_type);
 		    e->l_type);
+*/
 	}
 	}
 	return ret;
 	return ret;
 error:
 error:

+ 11 - 2
route_struct.c

@@ -113,7 +113,8 @@ error:
 	return 0;
 	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;
 	struct expr * e;
 	e=(struct expr*)pkg_malloc(sizeof (struct expr));
 	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;
 	va_list args;
 	int i;
 	int i;
 	struct action* a;
 	struct action* a;

+ 38 - 15
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,
 	   NUMBER_O, AVP_O, SNDIP_O, SNDPORT_O, TOIP_O, TOPORT_O, SNDPROTO_O,
 	   SNDAF_O, RETCODE_O, SELECT_O, PVAR_O, RVEXP_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_HOST_T, SET_HOSTPORT_T, SET_USER_T, SET_USERPASS_T,
 		SET_PORT_T, SET_URI_T, SET_HOSTPORTTRANS_T, SET_HOSTALL_T,
 		SET_PORT_T, SET_URI_T, SET_HOSTPORTTRANS_T, SET_HOSTALL_T,
 		SET_USERPHONE_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,
 		ADD_T,
 		UDP_MTU_TRY_PROTO_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,
 		EXPR_ST, ACTIONS_ST, MODEXP_ST, MODFIXUP_ST, URIHOST_ST, URIPORT_ST,
 		MYSELF_ST, STR_ST, SOCKID_ST, SOCKETINFO_ST, ACTION_ST, AVP_ST,
 		MYSELF_ST, STR_ST, SOCKID_ST, SOCKETINFO_ST, ACTION_ST, AVP_ST,
 		SELECT_ST, PVAR_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
 		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 */
 /* run flags */
 #define EXIT_R_F   1
 #define EXIT_R_F   1
 #define RETURN_R_F 2
 #define RETURN_R_F 2
@@ -134,15 +152,16 @@ union exp_op {
 };
 };
 
 
 struct expr{
 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 l;
 	union exp_op r;
 	union exp_op r;
 };
 };
 
 
 typedef struct {
 typedef struct {
-	int type;
+	action_param_type type;
 	union {
 	union {
 		long number;
 		long number;
 		char* string;
 		char* string;
@@ -160,16 +179,20 @@ typedef struct {
 #define MAX_ACTIONS (2+6)
 #define MAX_ACTIONS (2+6)
 
 
 struct action{
 struct action{
-	int type;  /* forward, drop, log, send ...*/
+	enum action_type type;  /* forward, drop, log, send ...*/
 	int count;
 	int count;
 	struct action* next;
 	struct action* next;
 	action_u_t val[MAX_ACTIONS];
 	action_u_t val[MAX_ACTIONS];
 };
 };
 
 
 struct expr* mk_exp(int op, struct expr* left, struct expr* right);
 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);
 struct action* append_action(struct action* a, struct action* b);
 
 
 void print_action(struct action* a);
 void print_action(struct action* a);