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 887
 			case MODULEX_T:
887 888
 				cmd = t->val[0].u.data;
888 889
 				if (cmd && cmd->c.fixup) {
889
-					int i;
890 890
 					DBG("fixing %s()\n", cmd->c.name);
891 891
 					if (t->val[1].u.number==0) {
892 892
 						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 1702
 	case PVAR_O:
1700 1703
 		ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
1701 1704
 		break;
1702
-
1705
+/*
1703 1706
 	default:
1704 1707
 		LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
1705 1708
 		    e->l_type);
1709
+*/
1706 1710
 	}
1707 1711
 	return ret;
1708 1712
 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 132
 }
132 133
 
133 134
 
134
-struct action* mk_action(int type, int count/* of couples {type,val} */, .../* int type1, void *val1 [, int type2, void *val2, ...] */) {
135
+/** create an action structure (parser use).
136
+ * @param type - type of the action
137
+ * @param count - count of couples {param_type,val}
138
+ * @param ... -   count {param_type, val} pairs, where param_type is
139
+ *                action_param_type.
140
+ * @return  new action structure on success (pkg_malloc'ed) or 0 on error.
141
+ */
142
+struct action* mk_action(enum action_type type, int count, ...)
143
+{
135 144
 	va_list args;
136 145
 	int i;
137 146
 	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 107
 		ADD_T,
97 108
 		UDP_MTU_TRY_PROTO_T
98 109
 };
99
-enum { NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
110
+/* parameter types for actions or types for expression right operands
111
+   (WARNING right operands only, not working for left operands) */
112
+enum _operand_subtype{
113
+		NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
100 114
 		EXPR_ST, ACTIONS_ST, MODEXP_ST, MODFIXUP_ST, URIHOST_ST, URIPORT_ST,
101 115
 		MYSELF_ST, STR_ST, SOCKID_ST, SOCKETINFO_ST, ACTION_ST, AVP_ST,
102 116
 		SELECT_ST, PVAR_ST,
... ...
@@ -105,6 +119,10 @@ enum { NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
105 119
 		BLOCK_ST, JUMPTABLE_ST, CONDTABLE_ST, MATCH_CONDTABLE_ST
106 120
 };
107 121
 
122
+typedef enum _expr_l_type expr_l_type;
123
+typedef enum _operand_subtype expr_r_type;
124
+typedef enum _operand_subtype action_param_type;
125
+
108 126
 /* run flags */
109 127
 #define EXIT_R_F   1
110 128
 #define RETURN_R_F 2
... ...
@@ -134,15 +152,16 @@ union exp_op {
134 152
 };
135 153
 
136 154
 struct expr{
137
-	int type; /* exp, exp_elem */
138
-	int op; /* and, or, not | ==,  =~ */
139
-	int l_type, r_type;
155
+	enum expr_type type; /* exp, exp_elem */
156
+	enum expr_op op; /* and, or, not | ==,  =~ */
157
+	expr_l_type l_type;
158
+	expr_r_type r_type;
140 159
 	union exp_op l;
141 160
 	union exp_op r;
142 161
 };
143 162
 
144 163
 typedef struct {
145
-	int type;
164
+	action_param_type type;
146 165
 	union {
147 166
 		long number;
148 167
 		char* string;
... ...
@@ -160,16 +179,20 @@ typedef struct {
160 179
 #define MAX_ACTIONS (2+6)
161 180
 
162 181
 struct action{
163
-	int type;  /* forward, drop, log, send ...*/
182
+	enum action_type type;  /* forward, drop, log, send ...*/
164 183
 	int count;
165 184
 	struct action* next;
166 185
 	action_u_t val[MAX_ACTIONS];
167 186
 };
168 187
 
169 188
 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);
189
+struct expr* mk_elem(int op, expr_l_type ltype, void* lparam,
190
+							 expr_r_type rtype, void* rparam);
191
+
192
+/* @param type - type of the action
193
+   @param count - count of couples {type,val} (variable number of parameters)*/
194
+struct action* mk_action(enum action_type type, int count, ...);
171 195
 
172
-struct action* mk_action(int type, int count/* of couples {type,val} */, .../* int type1, void *val1 [, int type2, void *val2, ...] */);
173 196
 struct action* append_action(struct action* a, struct action* b);
174 197
 
175 198
 void print_action(struct action* a);