Browse code

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 authored on 18/07/2009 09:05:05
Showing 4 changed files
... ...
@@ -1202,9 +1202,10 @@ match_cleanup:
1202 1202
 			else
1203 1203
 				ret=v;
1204 1204
 			break;
1205
-
1205
+/*
1206 1206
 		default:
1207 1207
 			LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
1208
+*/
1208 1209
 	}
1209 1210
 skip:
1210 1211
 	return ret;
... ...
@@ -623,6 +623,7 @@ int fix_actions(struct action* a)
623 623
 	struct proxy_l* p;
624 624
 	char *tmp;
625 625
 	int ret;
626
+	int i;
626 627
 	union cmd_export_u* cmd;
627 628
 	str s;
628 629
 	struct hostent* he;
... ...
@@ -886,7 +887,6 @@ int fix_actions(struct action* a)
886 886
 			case MODULEX_T:
887 887
 				cmd = t->val[0].u.data;
888 888
 				if (cmd && cmd->c.fixup) {
889
-					int i;
890 889
 					DBG("fixing %s()\n", cmd->c.name);
891 890
 					if (t->val[1].u.number==0) {
892 891
 						ret = cmd->c.fixup(0, 0);
... ...
@@ -976,6 +976,9 @@ int fix_actions(struct action* a)
976 976
 									(unsigned int)t->val[0].u.number);
977 977
 				}
978 978
 				break;
979
+			default:
980
+				/* no fixup required for the rest */
981
+				break;
979 982
 		}
980 983
 	}
981 984
 	return 0;
... ...
@@ -1699,10 +1702,11 @@ inline static int eval_elem(struct run_act_ctx* h, struct expr* e,
1699 1699
 	case PVAR_O:
1700 1700
 		ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
1701 1701
 		break;
1702
-
1702
+/*
1703 1703
 	default:
1704 1704
 		LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
1705 1705
 		    e->l_type);
1706
+*/
1706 1707
 	}
1707 1708
 	return ret;
1708 1709
 error:
... ...
@@ -113,7 +113,8 @@ error:
113 113
 	return 0;
114 114
 }
115 115
 
116
-struct expr* mk_elem(int op, int ltype, void* lparam, int rtype, void* rparam)
116
+struct expr* mk_elem(int op, expr_l_type ltype, void* lparam,
117
+							 expr_r_type rtype, void* rparam)
117 118
 {
118 119
 	struct expr * e;
119 120
 	e=(struct expr*)pkg_malloc(sizeof (struct expr));
... ...
@@ -131,7 +132,15 @@ error:
131 131
 }
132 132
 
133 133
 
134
-struct action* mk_action(int type, int count/* of couples {type,val} */, .../* int type1, void *val1 [, int type2, void *val2, ...] */) {
134
+/** create an action structure (parser use).
135
+ * @param type - type of the action
136
+ * @param count - count of couples {param_type,val}
137
+ * @param ... -   count {param_type, val} pairs, where param_type is
138
+ *                action_param_type.
139
+ * @return  new action structure on success (pkg_malloc'ed) or 0 on error.
140
+ */
141
+struct action* mk_action(enum action_type type, int count, ...)
142
+{
135 143
 	va_list args;
136 144
 	int i;
137 145
 	struct action* a;
... ...
@@ -60,15 +60,26 @@
60 60
  */
61 61
 
62 62
 
63
-enum { EXP_T=1, ELEM_T };
64
-enum { LOGAND_OP=1, LOGOR_OP, NOT_OP, BINAND_OP, BINOR_OP };
65
-enum { EQUAL_OP=10, MATCH_OP, GT_OP, LT_OP, GTE_OP, LTE_OP, DIFF_OP, NO_OP };
66
-enum { METHOD_O=1, URI_O, FROM_URI_O, TO_URI_O, SRCIP_O, SRCPORT_O,
67
-	   DSTIP_O, DSTPORT_O, PROTO_O, AF_O, MSGLEN_O, DEFAULT_O, ACTION_O,
63
+/* expression type */
64
+enum expr_type { EXP_T=1, ELEM_T };
65
+enum expr_op {
66
+	/* expression operator if type==EXP_T */
67
+	LOGAND_OP=1, LOGOR_OP, NOT_OP, BINAND_OP, BINOR_OP,
68
+	/* expression operator if type==ELEM_T */
69
+	EQUAL_OP=10, MATCH_OP, GT_OP, LT_OP, GTE_OP, LTE_OP, DIFF_OP, NO_OP
70
+};
71
+/* expression left member "special" type (if type==ELEM_T)
72
+  (start at 51 for debugging purposes: it's better to not overlap with 
73
+   expr_r_type)
74
+*/
75
+enum _expr_l_type{
76
+	   METHOD_O=51, URI_O, FROM_URI_O, TO_URI_O, SRCIP_O, SRCPORT_O,
77
+	   DSTIP_O, DSTPORT_O, PROTO_O, AF_O, MSGLEN_O, ACTION_O,
68 78
 	   NUMBER_O, AVP_O, SNDIP_O, SNDPORT_O, TOIP_O, TOPORT_O, SNDPROTO_O,
69 79
 	   SNDAF_O, RETCODE_O, SELECT_O, PVAR_O, RVEXP_O};
70
-
71
-enum { FORWARD_T=1, SEND_T, DROP_T, LOG_T, ERROR_T, ROUTE_T, EXEC_T,
80
+/* action types */
81
+enum action_type{
82
+		FORWARD_T=1, SEND_T, DROP_T, LOG_T, ERROR_T, ROUTE_T, EXEC_T,
72 83
 		SET_HOST_T, SET_HOSTPORT_T, SET_USER_T, SET_USERPASS_T,
73 84
 		SET_PORT_T, SET_URI_T, SET_HOSTPORTTRANS_T, SET_HOSTALL_T,
74 85
 		SET_USERPHONE_T,
... ...
@@ -96,7 +107,10 @@ enum { FORWARD_T=1, SEND_T, DROP_T, LOG_T, ERROR_T, ROUTE_T, EXEC_T,
96 96
 		ADD_T,
97 97
 		UDP_MTU_TRY_PROTO_T
98 98
 };
99
-enum { NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
99
+/* parameter types for actions or types for expression right operands
100
+   (WARNING right operands only, not working for left operands) */
101
+enum _operand_subtype{
102
+		NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
100 103
 		EXPR_ST, ACTIONS_ST, MODEXP_ST, MODFIXUP_ST, URIHOST_ST, URIPORT_ST,
101 104
 		MYSELF_ST, STR_ST, SOCKID_ST, SOCKETINFO_ST, ACTION_ST, AVP_ST,
102 105
 		SELECT_ST, PVAR_ST,
... ...
@@ -105,6 +119,10 @@ enum { NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
105 105
 		BLOCK_ST, JUMPTABLE_ST, CONDTABLE_ST, MATCH_CONDTABLE_ST
106 106
 };
107 107
 
108
+typedef enum _expr_l_type expr_l_type;
109
+typedef enum _operand_subtype expr_r_type;
110
+typedef enum _operand_subtype action_param_type;
111
+
108 112
 /* run flags */
109 113
 #define EXIT_R_F   1
110 114
 #define RETURN_R_F 2
... ...
@@ -134,15 +152,16 @@ union exp_op {
134 134
 };
135 135
 
136 136
 struct expr{
137
-	int type; /* exp, exp_elem */
138
-	int op; /* and, or, not | ==,  =~ */
139
-	int l_type, r_type;
137
+	enum expr_type type; /* exp, exp_elem */
138
+	enum expr_op op; /* and, or, not | ==,  =~ */
139
+	expr_l_type l_type;
140
+	expr_r_type r_type;
140 141
 	union exp_op l;
141 142
 	union exp_op r;
142 143
 };
143 144
 
144 145
 typedef struct {
145
-	int type;
146
+	action_param_type type;
146 147
 	union {
147 148
 		long number;
148 149
 		char* string;
... ...
@@ -160,16 +179,20 @@ typedef struct {
160 160
 #define MAX_ACTIONS (2+6)
161 161
 
162 162
 struct action{
163
-	int type;  /* forward, drop, log, send ...*/
163
+	enum action_type type;  /* forward, drop, log, send ...*/
164 164
 	int count;
165 165
 	struct action* next;
166 166
 	action_u_t val[MAX_ACTIONS];
167 167
 };
168 168
 
169 169
 struct expr* mk_exp(int op, struct expr* left, struct expr* right);
170
-struct expr* mk_elem(int op, int ltype, void* lparam, int rtype, void* rparam);
170
+struct expr* mk_elem(int op, expr_l_type ltype, void* lparam,
171
+							 expr_r_type rtype, void* rparam);
172
+
173
+/* @param type - type of the action
174
+   @param count - count of couples {type,val} (variable number of parameters)*/
175
+struct action* mk_action(enum action_type type, int count, ...);
171 176
 
172
-struct action* mk_action(int type, int count/* of couples {type,val} */, .../* int type1, void *val1 [, int type2, void *val2, ...] */);
173 177
 struct action* append_action(struct action* a, struct action* b);
174 178
 
175 179
 void print_action(struct action* a);